]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
Canvas: fix element mouse hit detection with OSG 3.3.2.
[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   /// Add two (integer) values taking care of overflows.
46   static T addClipOverflow(T a, T b)
47   {
48     if( b > 0 )
49     {
50       if( SGLimits<T>::max() - b < a )
51         return SGLimits<T>::max();
52     }
53     else
54     {
55       if( SGLimits<T>::min() - b > a )
56         return SGLimits<T>::min();
57     }
58
59     return a + b;
60   }
61
62   /// Add two (integer) values in place, taking care of overflows.
63   static T& addClipOverflowInplace(T& a, T b)
64   {
65     return a = addClipOverflow(a, b);
66   }
67
68   /**
69    * Seek a variable towards a target value with given rate and timestep
70    *
71    * @param var     Variable or eg. SGPropObj
72    * @param target  Target value
73    * @param rate    Max. change rate/sec
74    * @param dt      Time step (sec)
75    */
76   template<class Var>
77   static T seek(Var& var, T target, T rate, T dt)
78   {
79     if( var < target )
80       return var = min(var + rate * dt, target);
81     else
82       return var = max(var - rate * dt, target);
83   }
84
85   /**
86    * Get @c base raised to the power of @c N
87    *
88    * @tparam N      Exponent
89    * @param base    Base
90    */
91   template<int N>
92   static T pow(T base)
93   {
94     return (N < 0)
95       ? (1. / pow<-N>(base))
96       : (  ((N & 1) ? base : 1)
97         * ((N > 1) ? pow<N / 2>(base * base) : 1)
98         );
99   }
100
101   static int sign(const T& a)
102   {
103     if (a < -SGLimits<T>::min())
104       return -1;
105     else if (SGLimits<T>::min() < a)
106       return 1;
107     else
108       return 0;
109   }
110
111   static T rad2deg(const T& val)
112   { return val*180/pi(); }
113   static T deg2rad(const T& val)
114   { return val*pi()/180; }
115
116   // normalize the value to be in a range between [min, max[
117   static T
118   normalizePeriodic(const T& min, const T& max, const T& value)
119   {
120     T range = max - min;
121     if (range < SGLimits<T>::min())
122       return min;
123     T normalized = value - range*floor((value - min)/range);
124     // two security checks that can only happen due to roundoff
125     if (normalized <= min)
126       return min;
127     if (max <= normalized)
128       return min;
129     return normalized;
130   }
131
132   // normalize the angle to be in a range between [-pi, pi[
133   static T
134   normalizeAngle(const T& angle)
135   { return normalizePeriodic(-pi(), pi(), angle); }
136
137   // normalize the angle to be in a range between [0, 2pi[
138   static T
139   normalizeAngle2(const T& angle)
140   { return normalizePeriodic(0, twopi(), angle); }
141
142   static T round(const T& v)
143   { return floor(v + T(0.5)); }
144   static int roundToInt(const T& v)
145   { return int(round(v)); }
146
147   // Linear interpolation between two arbitrary typed values
148   template<typename S>
149   static S lerp(const S& val0, const S& val1, const T& t)
150   { return val0*(T(1) - t) + val1*t; }
151
152   /// Returns true if v is a NaN value
153   /// Use with care: allways code that you do not need to use that!
154   static bool isNaN(const T& v)
155   {
156 #ifdef HAVE_ISNAN
157     return isnan(v);
158 #elif defined HAVE_STD_ISNAN
159     return std::isnan(v);
160 #else
161     // Use that every compare involving a NaN returns false
162     // But be careful, some usual compiler switches like for example
163     // -fast-math from gcc might optimize that expression to v != v which
164     // behaves exactly like the opposite ...
165     return !(v == v);
166 #endif
167   }
168 };
169
170 #endif