]> git.mxchange.org Git - simgear.git/blob - simgear/sg_inlines.h
Remove stray ';'
[simgear.git] / simgear / sg_inlines.h
1 /** 
2  * \file sg_inlines.h
3  * Various inline template definitions.
4  */
5
6 // Written by Norman Vine, started June 2000.
7 //
8 // Copyright (C) 2000  Norman Vine  - nhv@cape.com
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_INLINES_H
28 #define _SG_INLINES_H
29
30 // return the sign of a value
31 template <class T>
32 inline int SG_SIGN(const T x) {
33     return x < T(0) ? -1 : 1;
34 }
35
36 // return the minimum of two values
37 template <class T>
38 inline T SG_MIN2(const T a, const T b) {
39     return a < b ? a : b;
40 }
41
42 // return the minimum of three values
43 template <class T>
44 inline T SG_MIN3( const T a, const T b, const T c) {
45     return (a < b ? SG_MIN2 (a, c) : SG_MIN2 (b, c));
46 }
47
48 // return the maximum of two values
49 template <class T>
50 inline T SG_MAX2(const T a, const T b) {
51     return  a > b ? a : b;
52 }
53
54 // return the maximum of three values
55 template <class T>
56 inline T SG_MAX3 (const T a, const T b, const T c) {
57     return (a > b ? SG_MAX2 (a, c) : SG_MAX2 (b, c));
58 }
59
60 // return the minimum and maximum of three values
61 template <class T>
62 inline void SG_MIN_MAX3 ( T &min, T &max, const T a, const T b, const T c) {
63     if( a > b ) {
64         if( a > c ) {
65             max = a;
66             min = SG_MIN2 (b, c);
67         } else {
68             max = c;
69             min = SG_MIN2 (a, b);
70         }
71     } else {
72         if( b > c ) {
73             max = b;
74             min = SG_MIN2 (a, c);
75         } else {
76             max = c;
77             min = SG_MIN2 (a, b);
78         }
79     }
80 }
81
82 // swap two values
83 template <class T>
84 inline void SG_SWAP( T &a, T &b) {
85     T c = a;  a = b;  b = c;
86 }
87
88 // clamp a value to lie between min and max
89 template <class T>
90 inline void SG_CLAMP_RANGE(T &x, const T min, const T max ) {
91     if ( x < min ) { x = min; }
92     if ( x > max ) { x = max; }
93 }
94
95 // normalize a value to lie between min and max
96 template <class T>
97 inline void SG_NORMALIZE_RANGE( T &val, const T min, const T max ) {
98     T step = max - min;
99     while( val >= max )  val -= step;
100     while( val < min ) val += step;
101 }
102
103 #endif // _SG_INLINES_H