1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __IRR_MATH_H_INCLUDED__
\r
6 #define __IRR_MATH_H_INCLUDED__
\r
8 #include "IrrCompileConfig.h"
\r
9 #include "irrTypes.h"
\r
12 #include <stdlib.h> // for abs() etc.
\r
13 #include <limits.h> // For INT_MAX / UINT_MAX
\r
20 //! Rounding error constant often used when comparing f32 values.
\r
22 const s32 ROUNDING_ERROR_S32 = 0;
\r
24 const s64 ROUNDING_ERROR_S64 = 0;
\r
25 const f32 ROUNDING_ERROR_f32 = 0.000001f;
\r
26 const f64 ROUNDING_ERROR_f64 = 0.00000001;
\r
28 #ifdef PI // make sure we don't collide with a define
\r
31 //! Constant for PI.
\r
32 const f32 PI = 3.14159265359f;
\r
34 //! Constant for reciprocal of PI.
\r
35 const f32 RECIPROCAL_PI = 1.0f/PI;
\r
37 //! Constant for half of PI.
\r
38 const f32 HALF_PI = PI/2.0f;
\r
40 #ifdef PI64 // make sure we don't collide with a define
\r
43 //! Constant for 64bit PI.
\r
44 const f64 PI64 = 3.1415926535897932384626433832795028841971693993751;
\r
46 //! Constant for 64bit reciprocal of PI.
\r
47 const f64 RECIPROCAL_PI64 = 1.0/PI64;
\r
49 //! 32bit Constant for converting from degrees to radians
\r
50 const f32 DEGTORAD = PI / 180.0f;
\r
52 //! 32bit constant for converting from radians to degrees (formally known as GRAD_PI)
\r
53 const f32 RADTODEG = 180.0f / PI;
\r
55 //! 64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
\r
56 const f64 DEGTORAD64 = PI64 / 180.0;
\r
58 //! 64bit constant for converting from radians to degrees
\r
59 const f64 RADTODEG64 = 180.0 / PI64;
\r
61 //! Utility function to convert a radian value to degrees
\r
62 /** Provided as it can be clearer to write radToDeg(X) than RADTODEG * X
\r
63 \param radians The radians value to convert to degrees.
\r
65 inline f32 radToDeg(f32 radians)
\r
67 return RADTODEG * radians;
\r
70 //! Utility function to convert a radian value to degrees
\r
71 /** Provided as it can be clearer to write radToDeg(X) than RADTODEG * X
\r
72 \param radians The radians value to convert to degrees.
\r
74 inline f64 radToDeg(f64 radians)
\r
76 return RADTODEG64 * radians;
\r
79 //! Utility function to convert a degrees value to radians
\r
80 /** Provided as it can be clearer to write degToRad(X) than DEGTORAD * X
\r
81 \param degrees The degrees value to convert to radians.
\r
83 inline f32 degToRad(f32 degrees)
\r
85 return DEGTORAD * degrees;
\r
88 //! Utility function to convert a degrees value to radians
\r
89 /** Provided as it can be clearer to write degToRad(X) than DEGTORAD * X
\r
90 \param degrees The degrees value to convert to radians.
\r
92 inline f64 degToRad(f64 degrees)
\r
94 return DEGTORAD64 * degrees;
\r
97 //! returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
\r
99 inline const T& min_(const T& a, const T& b)
\r
101 return a < b ? a : b;
\r
104 //! returns minimum of three values. Own implementation to get rid of the STL (VS6 problems)
\r
106 inline const T& min_(const T& a, const T& b, const T& c)
\r
108 return a < b ? min_(a, c) : min_(b, c);
\r
111 //! returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
\r
113 inline const T& max_(const T& a, const T& b)
\r
115 return a < b ? b : a;
\r
118 //! returns maximum of three values. Own implementation to get rid of the STL (VS6 problems)
\r
120 inline const T& max_(const T& a, const T& b, const T& c)
\r
122 return a < b ? max_(b, c) : max_(a, c);
\r
125 //! returns abs of two values. Own implementation to get rid of STL (VS6 problems)
\r
127 inline T abs_(const T& a)
\r
129 return a < (T)0 ? -a : a;
\r
132 //! returns linear interpolation of a and b with ratio t
\r
133 //! \return: a if t==0, b if t==1, and the linear interpolation else
\r
135 inline T lerp(const T& a, const T& b, const f32 t)
\r
137 return (T)(a*(1.f-t)) + (b*t);
\r
140 //! clamps a value between low and high
\r
142 inline const T clamp (const T& value, const T& low, const T& high)
\r
144 return min_ (max_(value,low), high);
\r
147 //! swaps the content of the passed parameters
\r
148 // Note: We use the same trick as boost and use two template arguments to
\r
149 // avoid ambiguity when swapping objects of an Irrlicht type that has not
\r
150 // it's own swap overload. Otherwise we get conflicts with some compilers
\r
151 // in combination with stl.
\r
152 template <class T1, class T2>
\r
153 inline void swap(T1& a, T2& b)
\r
161 inline T roundingError();
\r
164 inline f32 roundingError()
\r
166 return ROUNDING_ERROR_f32;
\r
170 inline f64 roundingError()
\r
172 return ROUNDING_ERROR_f64;
\r
176 inline s32 roundingError()
\r
178 return ROUNDING_ERROR_S32;
\r
182 inline u32 roundingError()
\r
184 return ROUNDING_ERROR_S32;
\r
188 inline s64 roundingError()
\r
190 return ROUNDING_ERROR_S64;
\r
194 inline u64 roundingError()
\r
196 return ROUNDING_ERROR_S64;
\r
200 inline T relativeErrorFactor()
\r
206 inline f32 relativeErrorFactor()
\r
212 inline f64 relativeErrorFactor()
\r
217 //! returns if a equals b, taking possible rounding errors into account
\r
219 inline bool equals(const T a, const T b, const T tolerance = roundingError<T>())
\r
221 return (a + tolerance >= b) && (a - tolerance <= b);
\r
225 //! returns if a equals b, taking relative error in form of factor
\r
226 //! this particular function does not involve any division.
\r
228 inline bool equalsRelative( const T a, const T b, const T factor = relativeErrorFactor<T>())
\r
230 //https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/
\r
232 const T maxi = max_( a, b);
\r
233 const T mini = min_( a, b);
\r
234 const T maxMagnitude = max_( maxi, -mini);
\r
236 return (maxMagnitude*factor + maxi) == (maxMagnitude*factor + mini); // MAD Wise
\r
239 union FloatIntUnion32
\r
241 FloatIntUnion32(float f1 = 0.0f) : f(f1) {}
\r
242 // Portable sign-extraction
\r
243 bool sign() const { return (i >> 31) != 0; }
\r
249 //! We compare the difference in ULP's (spacing between floating-point numbers, aka ULP=1 means there exists no float between).
\r
250 //\result true when numbers have a ULP <= maxUlpDiff AND have the same sign.
\r
251 inline bool equalsByUlp(f32 a, f32 b, int maxUlpDiff)
\r
253 // Based on the ideas and code from Bruce Dawson on
\r
254 // http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/
\r
255 // When floats are interpreted as integers the two nearest possible float numbers differ just
\r
256 // by one integer number. Also works the other way round, an integer of 1 interpreted as float
\r
257 // is for example the smallest possible float number.
\r
259 const FloatIntUnion32 fa(a);
\r
260 const FloatIntUnion32 fb(b);
\r
262 // Different signs, we could maybe get difference to 0, but so close to 0 using epsilons is better.
\r
263 if ( fa.sign() != fb.sign() )
\r
265 // Check for equality to make sure +0==-0
\r
271 // Find the difference in ULPs.
\r
272 const int ulpsDiff = abs_(fa.i- fb.i);
\r
273 if (ulpsDiff <= maxUlpDiff)
\r
279 //! returns if a equals zero, taking rounding errors into account
\r
280 inline bool iszero(const f64 a, const f64 tolerance = ROUNDING_ERROR_f64)
\r
282 return fabs(a) <= tolerance;
\r
285 //! returns if a equals zero, taking rounding errors into account
\r
286 inline bool iszero(const f32 a, const f32 tolerance = ROUNDING_ERROR_f32)
\r
288 return fabsf(a) <= tolerance;
\r
291 //! returns if a equals not zero, taking rounding errors into account
\r
292 inline bool isnotzero(const f32 a, const f32 tolerance = ROUNDING_ERROR_f32)
\r
294 return fabsf(a) > tolerance;
\r
297 //! returns if a equals zero, taking rounding errors into account
\r
298 inline bool iszero(const s32 a, const s32 tolerance = 0)
\r
300 return ( a & 0x7ffffff ) <= tolerance;
\r
303 //! returns if a equals zero, taking rounding errors into account
\r
304 inline bool iszero(const u32 a, const u32 tolerance = 0)
\r
306 return a <= tolerance;
\r
309 //! returns if a equals zero, taking rounding errors into account
\r
310 inline bool iszero(const s64 a, const s64 tolerance = 0)
\r
312 return abs_(a) <= tolerance;
\r
315 inline s32 s32_min(s32 a, s32 b)
\r
320 inline s32 s32_max(s32 a, s32 b)
\r
325 inline s32 s32_clamp (s32 value, s32 low, s32 high)
\r
327 return clamp(value, low, high);
\r
331 float IEEE-754 bit representation
\r
339 +NaN 0x7fc00000 or 0x7ff00000
\r
340 in general: number = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits)
\r
343 typedef union { u32 u; s32 s; f32 f; } inttofloat;
\r
345 #define F32_AS_S32(f) (*((s32 *) &(f)))
\r
346 #define F32_AS_U32(f) (*((u32 *) &(f)))
\r
347 #define F32_AS_U32_POINTER(f) ( ((u32 *) &(f)))
\r
349 #define F32_VALUE_0 0x00000000
\r
350 #define F32_VALUE_1 0x3f800000
\r
352 //! code is taken from IceFPU
\r
353 //! Integer representation of a floating-point value.
\r
354 inline u32 IR(f32 x) {inttofloat tmp; tmp.f=x; return tmp.u;}
\r
356 //! Floating-point representation of an integer value.
\r
357 inline f32 FR(u32 x) {inttofloat tmp; tmp.u=x; return tmp.f;}
\r
358 inline f32 FR(s32 x) {inttofloat tmp; tmp.s=x; return tmp.f;}
\r
360 #define F32_LOWER_0(n) ((n) < 0.0f)
\r
361 #define F32_LOWER_EQUAL_0(n) ((n) <= 0.0f)
\r
362 #define F32_GREATER_0(n) ((n) > 0.0f)
\r
363 #define F32_GREATER_EQUAL_0(n) ((n) >= 0.0f)
\r
364 #define F32_EQUAL_1(n) ((n) == 1.0f)
\r
365 #define F32_EQUAL_0(n) ((n) == 0.0f)
\r
366 #define F32_A_GREATER_B(a,b) ((a) > (b))
\r
370 #define REALINLINE __forceinline
\r
372 #define REALINLINE inline
\r
377 // NOTE: This is not as exact as the c99/c++11 round function, especially at high numbers starting with 8388609
\r
378 // (only low number which seems to go wrong is 0.49999997 which is rounded to 1)
\r
379 // Also negative 0.5 is rounded up not down unlike with the standard function (p.E. input -0.5 will be 0 and not -1)
\r
380 inline f32 round_( f32 x )
\r
382 return floorf( x + 0.5f );
\r
385 // calculate: sqrt ( x )
\r
386 REALINLINE f32 squareroot(const f32 f)
\r
391 // calculate: sqrt ( x )
\r
392 REALINLINE f64 squareroot(const f64 f)
\r
397 // calculate: sqrt ( x )
\r
398 REALINLINE s32 squareroot(const s32 f)
\r
400 return static_cast<s32>(squareroot(static_cast<f32>(f)));
\r
403 // calculate: sqrt ( x )
\r
404 REALINLINE s64 squareroot(const s64 f)
\r
406 return static_cast<s64>(squareroot(static_cast<f64>(f)));
\r
409 // calculate: 1 / sqrt ( x )
\r
410 REALINLINE f64 reciprocal_squareroot(const f64 x)
\r
412 return 1.0 / sqrt(x);
\r
415 // calculate: 1 / sqrtf ( x )
\r
416 REALINLINE f32 reciprocal_squareroot(const f32 f)
\r
418 return 1.f / sqrtf(f);
\r
421 // calculate: 1 / sqrtf( x )
\r
422 REALINLINE s32 reciprocal_squareroot(const s32 x)
\r
424 return static_cast<s32>(reciprocal_squareroot(static_cast<f32>(x)));
\r
427 // calculate: 1 / x
\r
428 REALINLINE f32 reciprocal( const f32 f )
\r
433 // calculate: 1 / x
\r
434 REALINLINE f64 reciprocal ( const f64 f )
\r
440 // calculate: 1 / x, low precision allowed
\r
441 REALINLINE f32 reciprocal_approxim ( const f32 f )
\r
446 REALINLINE s32 floor32(f32 x)
\r
448 return (s32) floorf ( x );
\r
451 REALINLINE s32 ceil32 ( f32 x )
\r
453 return (s32) ceilf ( x );
\r
456 // NOTE: Please check round_ documentation about some inaccuracies in this compared to standard library round function.
\r
457 REALINLINE s32 round32(f32 x)
\r
459 return (s32) round_(x);
\r
462 inline f32 f32_max3(const f32 a, const f32 b, const f32 c)
\r
464 return a > b ? (a > c ? a : c) : (b > c ? b : c);
\r
467 inline f32 f32_min3(const f32 a, const f32 b, const f32 c)
\r
469 return a < b ? (a < c ? a : c) : (b < c ? b : c);
\r
472 inline f32 fract ( f32 x )
\r
474 return x - floorf ( x );
\r
477 } // end namespace core
\r
478 } // end namespace irr
\r
480 using irr::core::IR;
\r
481 using irr::core::FR;
\r