]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
cppbind.Ghost: clean up a bit
[simgear.git] / simgear / math / SGMisc.hxx
1 // Copyright (C) 2006  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef SGMisc_H
19 #define SGMisc_H
20
21 template<typename T>
22 class SGMisc {
23 public:
24   static T pi() { return T(3.1415926535897932384626433832795029L); }
25   static T twopi() { return 2*T(3.1415926535897932384626433832795029L); }
26
27   static T min(const T& a, const T& b)
28   { return a < b ? a : b; }
29   static T min(const T& a, const T& b, const T& c)
30   { return min(min(a, b), c); }
31   static T min(const T& a, const T& b, const T& c, const T& d)
32   { return min(min(min(a, b), c), d); }
33   static T max(const T& a, const T& b)
34   { return a > b ? a : b; }
35   static T max(const T& a, const T& b, const T& c)
36   { return max(max(a, b), c); }
37   static T max(const T& a, const T& b, const T& c, const T& d)
38   { return max(max(max(a, b), c), d); }
39
40   // clip the value of a to be in the range between and including _min and _max
41   static T clip(const T& a, const T& _min, const T& _max)
42   { return max(_min, min(_max, a)); }
43
44   /**
45    * Seek a variable towards a target value with given rate and timestep
46    *
47    * @param var     Variable or eg. SGPropObj
48    * @param target  Target value
49    * @param rate    Max. change rate/sec
50    * @param dt      Time step (sec)
51    */
52   template<class Var>
53   static T seek(Var& var, T target, T rate, T dt)
54   {
55     if( var < target )
56       return var = min(var + rate * dt, target);
57     else
58       return var = max(var - rate * dt, target);
59   }
60
61   /**
62    * Get @c base raised to the power of @c N
63    *
64    * @tparam N      Exponent
65    * @param base    Base
66    */
67   template<int N>
68   static T pow(T base)
69   {
70     return (N < 0)
71       ? (1. / pow<-N>(base))
72       : (  ((N & 1) ? base : 1)
73         * ((N > 1) ? pow<N / 2>(base * base) : 1)
74         );
75   }
76
77   static int sign(const T& a)
78   {
79     if (a < -SGLimits<T>::min())
80       return -1;
81     else if (SGLimits<T>::min() < a)
82       return 1;
83     else
84       return 0;
85   }
86
87   static T rad2deg(const T& val)
88   { return val*180/pi(); }
89   static T deg2rad(const T& val)
90   { return val*pi()/180; }
91
92   // normalize the value to be in a range between [min, max[
93   static T
94   normalizePeriodic(const T& min, const T& max, const T& value)
95   {
96     T range = max - min;
97     if (range < SGLimits<T>::min())
98       return min;
99     T normalized = value - range*floor((value - min)/range);
100     // two security checks that can only happen due to roundoff
101     if (normalized <= min)
102       return min;
103     if (max <= normalized)
104       return min;
105     return normalized;
106   }
107
108   // normalize the angle to be in a range between [-pi, pi[
109   static T
110   normalizeAngle(const T& angle)
111   { return normalizePeriodic(-pi(), pi(), angle); }
112
113   // normalize the angle to be in a range between [0, 2pi[
114   static T
115   normalizeAngle2(const T& angle)
116   { return normalizePeriodic(0, twopi(), angle); }
117
118   static T round(const T& v)
119   { return floor(v + T(0.5)); }
120   static int roundToInt(const T& v)
121   { return int(round(v)); }
122
123   // Linear interpolation between two arbitrary typed values
124   template<typename S>
125   static S lerp(const S& val0, const S& val1, const T& t)
126   { return val0*(T(1) - t) + val1*t; }
127
128   /// Returns true if v is a NaN value
129   /// Use with care: allways code that you do not need to use that!
130   static bool isNaN(const T& v)
131   {
132 #ifdef HAVE_ISNAN
133     return isnan(v);
134 #elif defined HAVE_STD_ISNAN
135     return std::isnan(v);
136 #else
137     // Use that every compare involving a NaN returns false
138     // But be careful, some usual compiler switches like for example
139     // -fast-math from gcc might optimize that expression to v != v which
140     // behaves exactly like the opposite ...
141     return !(v == v);
142 #endif
143   }
144 };
145
146 #endif