Debian Codegraph @

engine/core/util/math/fife_math.h

raw
1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the FIFE team *
3 * http://www.fifengine.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #ifndef FIFE_UTIL_FIFE_MATH_H
23 #define FIFE_UTIL_FIFE_MATH_H
24
25 // Standard C++ library includes
26 #include <cassert>
27 #include <cmath>
28 #include <limits>
29 #include <algorithm>
30
31 // Platform specific includes
32
33 // 3rd party library includes
34
35 // FIFE includes
36 // These includes are split up in two parts, separated by one empty line
37 // First block: files included from the FIFE root src directory
38 // Second block: files included from the same folder
39
40 #ifndef ABS
41 #define ABS(x) ((x)<0?-(x):(x))
42
43 #endif
44
45 // Sort out the missing round function in MSVC:
46 #if defined( WIN32 ) && defined( _MSC_VER )
47 inline double round(const double x) {
48 return x < 0.0 ? ceil(x - 0.5) : floor(x + 0.5);
49 }
50 #endif
51
52 namespace FIFE {
53
54 static const float FLT_STD_EPSILON = std::numeric_limits<float>::epsilon();
55 static const float FLT_STD_MAX = (std::numeric_limits<float>::max)();
56 static const float FLT_ZERO_TOLERANCE = 1e-06f;
57 static const float FLT_PI = 4.0f*std::atan(1.0f);
58 static const float FLT_TWO_PI = 2.0f*FLT_PI;
59 static const float FLT_HALF_PI = 0.5f*FLT_PI;
60 static const float FLT_INVERSE_PI = 1.0f/FLT_PI;
61 static const float FLT_INVERSE_TWO_PI = 1.0f/FLT_TWO_PI;
62 static const float FLT_DEG_TO_RAD = FLT_PI/180.0f;
63 static const float FLT_RAD_TO_DEG = 180.0f/FLT_PI;
64 static const float FLT_LOG_2 = std::log(2.0f);
65 static const float FLT_LOG_10 = std::log(10.0f);
66 static const float FLT_INV_LOG_2 = 1.0f/std::log(2.0f);
67 static const float FLT_INV_LOG_10 = 1.0f/std::log(10.0f);
68
69 static const double DBL_STD_EPSILON = std::numeric_limits<double>::epsilon();
70 static const double DBL_STD_MAX = (std::numeric_limits<double>::max)();
71 static const double DBL_ZERO_TOLERANCE = 1e-08;
72 static const double DBL_PI = 4.0*std::atan(1.0);
73 static const double DBL_TWO_PI = 2.0*DBL_PI;
74 static const double DBL_HALF_PI = 0.5*DBL_PI;
75 static const double DBL_INVERSE_PI = 1.0/DBL_PI;
76 static const double DBL_INVERSE_TWO_PI = 1.0/DBL_TWO_PI;
77 static const double DBL_DEG_TO_RAD = DBL_PI/180.0;
78 static const double DBL_RAD_TO_DEG = 180.0f/DBL_PI;
79 static const double DBL_LOG_2 = std::log(2.0);
80 static const double DBL_LOG_10 = std::log(10.0);
81 static const double DBL_INV_LOG_2 = 1.0/std::log(2.0);
82 static const double DBL_INV_LOG_10 = 1.0/std::log(10.0);
83
84 template <class numT>
85 struct float_traits { };
86
87 template <>
88 struct float_traits<float> {
89 typedef float float_type;
90 static inline float_type epsilon() { return FLT_STD_EPSILON; }
91 static inline float_type zeroTolerance() { return FLT_ZERO_TOLERANCE; }
92 static inline float_type max() { return FLT_STD_MAX; }
93 static inline float_type pi() { return FLT_PI; }
94 static inline float_type twoPi() { return FLT_TWO_PI; }
95 static inline float_type halfPi() { return FLT_HALF_PI; }
96 static inline float_type inversePi() { return FLT_INVERSE_PI; }
97 static inline float_type inverseTwoPi() { return FLT_INVERSE_TWO_PI; }
98 static inline float_type degToRad() { return FLT_DEG_TO_RAD; }
99 static inline float_type radToDeg() { return FLT_RAD_TO_DEG; }
100 static inline float_type log2() { return FLT_LOG_2; }
101 static inline float_type log10() { return FLT_LOG_10; }
102 static inline float_type invLog2() { return FLT_INV_LOG_2; }
103 static inline float_type invLog10() { return FLT_INV_LOG_10; }
104 };
105
106 template <>
107 struct float_traits<double> {
108 typedef double float_type;
109 static inline float_type epsilon() { return DBL_STD_EPSILON; }
110 static inline float_type zeroTolerance() { return DBL_ZERO_TOLERANCE; }
111 static inline float_type max() { return DBL_STD_MAX; }
112 static inline float_type pi() { return DBL_PI; }
113 static inline float_type twoPi() { return DBL_TWO_PI; }
114 static inline float_type halfPi() { return DBL_HALF_PI; }
115 static inline float_type inversePi() { return DBL_INVERSE_PI; }
116 static inline float_type inverseTwoPi() { return DBL_INVERSE_TWO_PI; }
117 static inline float_type degToRad() { return DBL_DEG_TO_RAD; }
118 static inline float_type radToDeg() { return DBL_RAD_TO_DEG; }
119 static inline float_type log2() { return DBL_LOG_2; }
120 static inline float_type log10() { return DBL_LOG_10; }
121 static inline float_type invLog2() { return DBL_INV_LOG_2; }
122 static inline float_type invLog10() { return DBL_INV_LOG_10; }
123 };
124
125 template <typename T>
126 class Math {
127 public:
128 typedef T num_type;
129 typedef float_traits<num_type> traits_type;
130
131 static inline num_type epsilon() { return traits_type::epsilon(); }
132 static inline num_type zeroTolerance() { return traits_type::zeroTolerance(); }
133 static inline num_type max() { return traits_type::max(); }
134 static inline num_type pi() { return traits_type::pi(); }
135 static inline num_type twoPi() { return traits_type::twoPi(); }
136 static inline num_type halfPi() { return traits_type::halfPi(); }
137 static inline num_type inversePi() { return traits_type::inversePi(); }
138 static inline num_type inverseTwoPi() { return traits_type::inverseTwoPi(); }
139 static inline num_type degToRad() { return traits_type::degToRad(); }
140 static inline num_type radToDeg() { return traits_type::radToDeg(); }
141 static inline num_type log2() { return traits_type::log2(); }
142 static inline num_type log10() { return traits_type::log10(); }
143 static inline num_type invLog2() { return traits_type::invLog2(); }
144 static inline num_type invLog10() { return traits_type::invLog10(); }
145
146 static T ACos(T _val);
147 static T ASin(T _val);
148 static T ATan(T _val);
149 static T ATan2(T _x, T _y);
150 static T Ceil(T _val);
151 static T Cos(T _val);
152 static T Exp(T _val);
153 static T FAbs(T _val);
154 static T Floor(T _val);
155 static T FMod (T _x, T _y);
156 static T InvSqrt(T _val);
157 static T Log(T _val);
158 static T Log2(T _val);
159 static T Log10(T _val);
160 static T Pow(T _base, T _exponent);
161 static T Sin(T _val);
162 static T Sqr(T _val);
163 static T Sqrt(T _val);
164 static T Tan(T _val);
165 static bool Equal(T _val1, T _val2);
166 };
167
168 typedef Math<float> Mathf;
169 typedef Math<double> Mathd;
170
171 template<typename T>
172 inline T Math<T>::ACos(T _val) {
173 if (-static_cast<T>(1) < _val) {
174 if (_val < static_cast<T>(1)) {
175 return static_cast<T>(std::acos(_val));
176 }
177 else {
178 return static_cast<T>(0);
179 }
180 }
181 else {
182 return pi();
183 }
184 }
185
186 template <class T>
187 inline T Math<T>::ASin(T _val) {
188 if (-static_cast<T>(1) < _val) {
189 if (_val < static_cast<T>(1)) {
190 return static_cast<T>(std::asin(_val));
191 }
192 else {
193 return halfPi();
194 }
195 }
196 else {
197 return -halfPi();
198 }
199 }
200
201 template <class T>
202 inline T Math<T>::ATan(T _val) {
203 return static_cast<T>(std::atan(_val));
204 }
205
206 template <class T>
207 inline T Math<T>::ATan2(T _x, T _y) {
208 return static_cast<T>(std::atan2(_x, _y));
209 }
210
211 template <class T>
212 inline T Math<T>::Ceil(T _val) {
213 return static_cast<T>(std::ceil(_val));
214 }
215
216 template <class T>
217 inline T Math<T>::Cos(T _val) {
218 return static_cast<T>(std::cos(_val));
219 }
220
221 template <class T>
222 inline T Math<T>::Exp(T _val){
223 return static_cast<T>(std::exp(_val));
224 }
225
226 template <class T>
227 inline T Math<T>::FAbs(T _val) {
228 return static_cast<T>(std::fabs(_val));
229 }
230
231 template <class T>
232 inline T Math<T>::Floor(T _val) {
233 return static_cast<T>(std::floor(_val));
234 }
235
236 template <class T>
237 inline T Math<T>::FMod(T _x, T _y) {
238 return static_cast<T>(std::fmod(_x, _y));
239 }
240
241 template <class T>
242 inline T Math<T>::InvSqrt(T _val) {
243 return static_cast<T>(1/std::sqrt(_val));
244 }
245
246 template <class T>
247 inline T Math<T>::Log(T _val) {
248 return static_cast<T>(std::log(_val));
249 }
250
251 template <class T>
252 inline T Math<T>::Log2(T _val) {
253 return invLog2() * static_cast<T>(std::log(_val));
254 }
255 template <class T>
256 inline T Math<T>::Log10(T _val) {
257
258 return invLog10() * static_cast<T>(std::log(_val));
259 }
260
261 template <class T>
262 inline T Math<T>::Pow(T _base, T _exponent) {
263 return static_cast<T>(std::pow(_base, _exponent));
264 }
265
266 template <class T>
267 inline T Math<T>::Sin(T _val) {
268 return static_cast<T>(std::sin(_val));
269 }
270
271 template <class T>
272 inline T Math<T>::Sqr(T _val) {
273 return _val*_val;
274 }
275
276 template <class T>
277 inline T Math<T>::Sqrt(T _val) {
278 return static_cast<T>(std::sqrt(_val));
279 }
280
281 template <class T>
282 inline T Math<T>::Tan(T _val) {
283 return static_cast<T>(std::tan(_val));
284 }
285
286 template <class T>
287 inline bool Math<T>::Equal(T _val1, T _val2) {
288 return std::fabs(_val1 - _val2) < epsilon();
289 }
290
291 /** Returns the next higher power of 2 based on the passed argument
292 */
293 inline unsigned nextPow2(unsigned x)
294 {
295 --x;
296 x |= x >> 1;
297 x |= x >> 2;
298 x |= x >> 4;
299 x |= x >> 8;
300 x |= x >> 16;
301 return ++x;
302 }
303 } //FIFE
304
305 #endif // FIFE_UTIL_FIFE_MATH_H