AGXDynamics  2.31.1.0
Math.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/config.h>
20 
21 #include <agx/macros.h>
22 #include <agx/agxCore_export.h>
23 #include <agx/debug.h>
24 #include <agx/Real.h>
25 #include <agx/Integer.h>
26 #include <cmath>
27 
28 #ifdef _MSC_VER
29  #define _USE_MATH_DEFINES
30  #include <math.h>
31 
32  #ifndef NOMINMAX
33  #define NOMINMAX
34  #endif
35 
36 #else
37  #include <math.h>
38 #endif
39 
40 #include <stdlib.h>
41 #include <limits>
42 #include <algorithm>
43 
44 
45 #include <float.h>
46 
47 namespace agx
48 {
49  // physics related constants
50  const Real GRAVITY_ACCELERATION = Real(9.80665); // in m/s2, SI standard acceleration due to gravity
51 
52 
53  #define NUMERIC_MAX(type) std::numeric_limits<type>::max()
54  #define NUMERIC_MIN(type) std::numeric_limits<type>::min()
55  const Real AGX_EQUIVALENT_EPSILON = (double)1E-9;
56  #define AGX_BIT_MASK(bit) (1 << bit)
57  #define AGX_TEST_BIT(val, bit) (val & AGX_BIT_MASK(bit))
58  #define AGX_SET_BIT(val, bit) (val |= AGX_BIT_MASK(bit))
59  #define AGX_UNSET_BIT(val, bit) (val &= ~AGX_BIT_MASK(bit))
60 
61  const Real PI = agx::Real(M_PI);
62  const Real PI_2 = agx::Real(M_PI_2);
63  const Real PI_4 = agx::Real(M_PI_4);
64 
65  // define the standard trig values
66 
67  const Real DEG_TO_RAD = agx::Real(0.017453292519943295769236907684886);
68  const Real RAD_TO_DEG = agx::Real(57.295779513082320876798154814105);
69 
70  AGXCORE_EXPORT extern const Real Infinity;
72 
73 
74 
75  template <typename T>
76  AGX_FORCE_INLINE T inverse(const T& value)
77  {
78  return T(1) / value;
79  }
80 
81  template <typename T>
83  {
84  return !(bool)(val & 0x1);
85  }
86 
87 
91  inline Real sinc( Real x, Real nearZero = Real(1E-4) )
92  {
93  if ( std::abs( x ) < nearZero )
94  {
95  return Real(1) - (x * x) / Real(6) + (x * x * x * x) / Real(120);
96  }
97  else
98  {
99  return std::sin( x ) / x;
100  }
101  }
102 
108  template<typename T>
109  AGX_FORCE_INLINE bool _equivalent( T lhs, T rhs, T epsilon = T(AGX_EQUIVALENT_EPSILON) )
110  {
111  return (lhs + epsilon >= rhs) && (lhs - epsilon <= rhs);
112  }
113 
119  AGX_FORCE_INLINE bool equivalent(float lhs, float rhs, float epsilon = (float)AGX_EQUIVALENT_EPSILON)
120  {
121  return _equivalent< float >(lhs, rhs, epsilon);
122  }
123 
129  AGX_FORCE_INLINE bool equivalent(double lhs, double rhs, double epsilon = (double)AGX_EQUIVALENT_EPSILON)
130  {
131  return _equivalent< double >(lhs, rhs, epsilon);
132  }
133 
139  AGX_FORCE_INLINE bool equivalent(float lhs, float rhs, double epsilon)
140  {
141  return _equivalent(lhs, rhs, (float)epsilon);
142  }
143 
144 
151  template<typename T>
152  AGX_FORCE_INLINE bool _relativelyEquivalent( T lhs, T rhs, T relativeEpsilon = T(AGX_EQUIVALENT_EPSILON) )
153  {
154  return _equivalent(lhs, rhs, (std::abs(lhs) + std::abs(rhs) + T(1)) * relativeEpsilon);
155  }
156 
163  AGX_FORCE_INLINE bool relativelyEquivalent(float lhs, float rhs, float epsilon = (float)AGX_EQUIVALENT_EPSILON)
164  {
165  return _relativelyEquivalent< float >(lhs, rhs, epsilon);
166  }
167 
174  AGX_FORCE_INLINE bool relativelyEquivalent(double lhs, double rhs, double epsilon = (double)AGX_EQUIVALENT_EPSILON)
175  {
176  return _relativelyEquivalent< double >(lhs, rhs, epsilon);
177  }
178 
185  AGX_FORCE_INLINE bool relativelyEquivalent(float lhs, float rhs, double epsilon)
186  {
187  return _relativelyEquivalent(lhs, rhs, (float)epsilon);
188  }
189 
190 
192 #define INT_EQUALS_ZERO( T ) \
193  AGX_FORCE_INLINE bool equalsZero( T f ) \
194  { \
195  return ( f == 0 ); \
196  }
197 
198 #define INT_IS_NAN( T ) \
199  AGX_FORCE_INLINE bool isNaN( T /*v*/ ) \
200  { \
201  return false; \
202  }
203 
204 #define INT_IS_INF( T ) \
205  AGX_FORCE_INLINE bool isInf( T /*v*/ ) \
206  { \
207  return false; \
208  }
209 
210 #define INT_IS_FINITE( T ) \
211  AGX_FORCE_INLINE bool isFinite( T /*v*/ ) \
212  { \
213  return true; \
214  }
215 
216 #define UINT_ABSOLUTE( T ) \
217  AGX_FORCE_INLINE T absolute( T v ) \
218  { \
219  return v; \
220  }
221 
226 
231 
232  INT_IS_NAN( Int8 )
233  INT_IS_NAN( Int16 )
234  INT_IS_NAN( Int32 )
235  INT_IS_NAN( Int64 )
236 
237  INT_IS_NAN( UInt8 )
238  INT_IS_NAN( UInt16 )
239  INT_IS_NAN( UInt32 )
240  INT_IS_NAN( UInt64 )
241 
242  INT_IS_INF( Int8 )
243  INT_IS_INF( Int16 )
244  INT_IS_INF( Int32 )
245  INT_IS_INF( Int64 )
246 
247  INT_IS_INF( UInt8 )
248  INT_IS_INF( UInt16 )
249  INT_IS_INF( UInt32 )
250  INT_IS_INF( UInt64 )
251 
256 
261 
266 
267 #undef INT_EQUALS_ZERO
268 #undef INT_IS_NAN
269 #undef INT_IS_INF
270 #undef INT_IS_FINITE
271 #undef UINT_ABSOLUTE
272 
274  AGX_FORCE_INLINE bool equalsZero( float f, float eps=FLT_EPSILON )
275  {
276  return ( f < eps && f > -eps );
277  }
278 
280  AGX_FORCE_INLINE bool equalsZero( double d, double eps=DBL_EPSILON )
281  {
282  return ( d < eps && d > -eps );
283  }
284 
285 
288  template<typename T>
290  {
291  return std::abs(v);
292  }
293 
294  template<typename T>
296  {
297  return std::isinf( v ) != 0;
298  }
299 
300  template<typename T>
302  {
303  return std::isnan( v );
304  }
305 
306  template<typename T>
308  {
309  return std::isfinite(v);
310  }
311 
315  template<typename T1, typename T2, typename T3>
316  AGX_FORCE_INLINE T1 clamp( T1 v, T2 minimum, T3 maximum )
317  {
318  return v < minimum ? minimum : v > maximum ? maximum : v;
319  }
320 
321 
325  template<typename T>
327  {
328  return v < T(0) ? T(-1) : T(1);
329  }
330 
332  template<typename T>
334  {
335  return v*v;
336  }
337 
339  template<typename T>
341  {
342  return v < ( T )0 ? -v*v : v*v;
343  }
344 
347  {
348  return angle * Real(DEG_TO_RAD);
349  }
350 
353  {
354  return angle*Real(RAD_TO_DEG);
355  }
356 
364  AGXCORE_EXPORT Real normalizedAngle( Real angle, bool positiveRange = false );
365 
367  AGX_FORCE_INLINE bool leq( double a, double b, double eps = (double)AGX_EQUIVALENT_EPSILON )
368  {
369  return a < b + eps;
370  }
371 
373  AGX_FORCE_INLINE bool leq(float a, float b, float eps = (float)AGX_EQUIVALENT_EPSILON)
374  {
375  return a < b + eps;
376  }
377 
379  AGX_FORCE_INLINE bool geq(double a, double b, double eps = (double)AGX_EQUIVALENT_EPSILON)
380  {
381  return a > b - eps;
382  }
383 
385  AGX_FORCE_INLINE bool geq(float a, float b, float eps = (float)AGX_EQUIVALENT_EPSILON)
386  {
387  return a > b - eps;
388  }
389 
390 
392  {
393  Int32 ret = -1;
394  while (val != 0) {
395  val >>= 1;
396  ret++;
397  }
398  return ret;
399  }
400 
401 
405  template <typename T>
407  {
408  return (value & (value-1)) == 0;
409  }
410 
412  {
413  value--;
414  value |= (value >> 1);
415  value |= (value >> 2);
416  value |= (value >> 4);
417  value |= (value >> 8);
418  value |= (value >> 16);
419  value++;
420  return value;
421  }
422 
424  {
425  value--;
426  value |= (value >> 1);
427  value |= (value >> 2);
428  value |= (value >> 4);
429  value |= (value >> 8);
430  value |= (value >> 16);
431  value++;
432  return value;
433  }
434 
435 
437  {
438  value--;
439  value |= (value >> 1);
440  value |= (value >> 2);
441  value |= (value >> 4);
442  value |= (value >> 8);
443  value |= (value >> 16);
444  value |= (value >> 32);
445  value++;
446  return value;
447  }
448 
450  {
451  value--;
452  value |= (value >> 1);
453  value |= (value >> 2);
454  value |= (value >> 4);
455  value |= (value >> 8);
456  value |= (value >> 16);
457  value |= (value >> 32);
458  value++;
459  return value;
460  }
461 
462 
463  template <typename T>
464  AGX_FORCE_INLINE T align_ceil(T value, T alignment)
465  {
466  agxAssert(alignment > 0);
467  return value > 0 ? (value + (alignment-1) - ((value-1) % alignment)) : 0;
468  }
469 
470  template <typename T1, typename T2>
471  AGX_FORCE_INLINE T1 *align_ceil(const T1 *ptr, T2 alignment)
472  {
473  return (T1 *)align_ceil<agx::UInt64>((agx::UInt64)ptr, (agx::UInt64)alignment);
474  }
475 
476 
477  template <typename T>
478  AGX_FORCE_INLINE T align_floor(T value, T alignment)
479  {
480  agxAssert(alignment > 0);
481  return value - (value % alignment);
482  }
483 
484  template <typename T1, typename T2>
485  AGX_FORCE_INLINE T1 *align_floor(const T1 *ptr, T2 alignment)
486  {
487  return (T1 *)align_floor<agx::UInt64>((agx::UInt64)ptr, (agx::UInt64)alignment);
488  }
489 
490  template <typename T>
491  AGX_FORCE_INLINE bool isAligned(T value, T alignment)
492  {
493  return value % alignment == 0;
494  }
495 
496  template <typename T1, typename T2>
497  AGX_FORCE_INLINE bool isAligned(const T1 *ptr, T2 alignment)
498  {
499  return isAligned((uintptr_t)ptr, (uintptr_t)alignment);
500  }
501 
502  template <typename T>
503  AGX_FORCE_INLINE T align_ceil2(T value, T alignment)
504  {
505  agxAssert(isPowerOfTwo(alignment));
506  T tmp = alignment-1;
507  return ((value + tmp) & ~tmp);
508  }
509 
510  template <typename T1, typename T2>
511  AGX_FORCE_INLINE T1 *align_ceil2(const T1 *ptr, T2 alignment)
512  {
513  return (T1 *)align_ceil2<agx::UInt64>((agx::UInt64)ptr, (agx::UInt64)alignment);
514  }
515 
516  template <typename T>
517  AGX_FORCE_INLINE T align_floor2(T value, T alignment)
518  {
519  agxAssert(isPowerOfTwo(alignment));
520  T tmp = alignment-1;
521  return (value & ~tmp);
522  }
523 
524  template <typename T1, typename T2>
525  AGX_FORCE_INLINE T1 *align_floor2(const T1 *ptr, T2 alignment)
526  {
527  return (T1 *)align_floor2<agx::UInt64>((agx::UInt64)ptr, (agx::UInt64)alignment);
528  }
529 
530  // basic method for generating primes
531  AGXCORE_EXPORT int nextPrime(int n);
532 
534  AGXCORE_EXPORT double strtod(const char *nptr, char **endptr);
535 
537  AGXCORE_EXPORT float strtof(const char *nptr, char **endptr);
538 
539 
540 
545  {
546  return rand() % (max - min) + min;
547  }
548 
550  template <typename T>
552  {
553  Real sample = static_cast<Real>(rand());
554  Real randEnd = static_cast<Real>(RAND_MAX);
555  Real range_fraction = sample / randEnd;
556  Real range = static_cast<Real>(max - min);
557  T offset = static_cast<T>(range_fraction * range);
558  T value = static_cast<T>(min + offset);
559  return value;
560  }
561 
562 
566  template<typename T>
567  AGX_FORCE_INLINE Real computeVolume( const T& a, const T& b, const T& c, const T& d )
568  {
569  return fabsf( ( ( b -c ) ^ ( a - b ) )*( d - b ) );
570  }
571 
573  template<typename T>
574  AGX_FORCE_INLINE Real computeVolume( const T& f1, const T& f2, const T& f3,
575  const T& b1, const T& b2, const T& b3 )
576  {
577  return computeVolume( f1, f2, f3, b1 ) +
578  computeVolume( b1, b2, b3, f2 ) +
579  computeVolume( b1, b3, f2, f3 );
580  }
581 
583  template<typename T1, typename T2>
584  AGX_FORCE_INLINE T1 const lerp(T1 const& a, T1 const& b, T2 s)
585  {
586  if (s > 1)
587  return b;
588  if (s <=0)
589  return a;
590 
591  return (T1)(a*(1-s) + b*s);
592  }
593 
595  template<typename T>
596  AGX_FORCE_INLINE T const logInterpolate(T const& a, T const& b, float s)
597  {
598  return std::pow(T(10), agx::lerp(std::log10(a), std::log10(b), s));
599  }
600 
606  template<typename T>
608  {
609  static const agx::UInt32 multiplyDeBruijnBitPosition2[ 32 ] =
610  {
611  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
612  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
613  };
614 
615  return multiplyDeBruijnBitPosition2[ ( (unsigned int)value * 0x077CB531U ) >> 27 ];
616  }
617 
630  template<typename T>
632  {
633  if (n == 0)
634  return 0;
635 
636  agx::UInt8 msb = 0;
637  n = T(n / 2);
638  while (n != 0) {
639  n = T(n / 2);
640  msb++;
641  }
642 
643  return msb;
644  }
645 
646 
647 } // namespace agx
648 
#define INT_IS_INF(T)
Definition: Math.h:204
bool isAligned(T value, T alignment)
Definition: Math.h:491
int64_t Int64
Definition: Integer.h:46
agx::UInt32 pow2Exponent(T value)
Finds and returns n given value = 2^n.
Definition: Math.h:607
bool isEven(T val)
Definition: Math.h:82
T random(T min, T max)
Definition: Math.h:551
const Real PI
Definition: Math.h:61
const Real DEG_TO_RAD
Definition: Math.h:67
Int32 log2(size_t val)
Definition: Math.h:391
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:880
const Real PI_2
Definition: Math.h:62
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874
#define INT_IS_NAN(T)
Definition: Math.h:198
int32_t Int32
Definition: Integer.h:45
T const logInterpolate(T const &a, T const &b, float s)
logarithmic interpolation from a to b using s = {0,..1}
Definition: Math.h:596
T align_floor2(T value, T alignment)
Definition: Math.h:517
uint8_t UInt8
Definition: Integer.h:38
int irandom(int min, int max)
Definition: Math.h:544
const Real AGX_EQUIVALENT_EPSILON
Definition: Math.h:55
T signedSquare(T v)
Definition: Math.h:340
const Real GRAVITY_ACCELERATION
Definition: Math.h:50
Real sinc(Real x, Real nearZero=Real(1E-4))
Sinc function.
Definition: Math.h:91
T align_ceil(T value, T alignment)
Definition: Math.h:464
#define INT_EQUALS_ZERO(T)
Definition: Math.h:192
#define AGX_FORCE_INLINE
Definition: Object.h:63
T inverse(const T &value)
Definition: Math.h:76
bool geq(double a, double b, double eps=(double) AGX_EQUIVALENT_EPSILON)
Definition: Math.h:379
const Real RAD_TO_DEG
Definition: Math.h:68
agx::UInt8 highestBitToIndex(T n)
Definition: Math.h:631
bool isNaN(T v)
Definition: Math.h:301
bool leq(double a, double b, double eps=(double) AGX_EQUIVALENT_EPSILON)
Definition: Math.h:367
bool relativelyEquivalent(float lhs, float rhs, float epsilon=(float) AGX_EQUIVALENT_EPSILON)
Compare two values for relative equality.
Definition: Math.h:163
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
T align_floor(T value, T alignment)
Definition: Math.h:478
AGXCORE_EXPORT const Real REAL_SQRT_EPSILON
AGXCORE_EXPORT Real normalizedAngle(Real angle, bool positiveRange=false)
Normalize an angle.
bool _equivalent(T lhs, T rhs, T epsilon=T(AGX_EQUIVALENT_EPSILON))
Compare two values for equality.
Definition: Math.h:109
int8_t Int8
Definition: Integer.h:43
#define UINT_ABSOLUTE(T)
Definition: Math.h:216
bool _relativelyEquivalent(T lhs, T rhs, T relativeEpsilon=T(AGX_EQUIVALENT_EPSILON))
Compare two values for relative equality.
Definition: Math.h:152
AGXCORE_EXPORT const Real Infinity
#define agxAssert(expr)
Definition: debug.h:143
Real degreesToRadians(Real angle)
Definition: Math.h:346
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)
AGXCORE_EXPORT int nextPrime(int n)
T absolute(T v)
return the absolute value.
Definition: Math.h:289
T square(T v)
Definition: Math.h:333
AGXCORE_EXPORT float strtof(const char *nptr, char **endptr)
strtod ignoring global locale
AGXCORE_EXPORT double strtod(const char *nptr, char **endptr)
strtod ignoring global locale
uint64_t UInt64
Definition: Integer.h:41
T1 const lerp(T1 const &a, T1 const &b, T2 s)
Linearly interpolate from a to b using s = {0,..1}.
Definition: Math.h:584
Real radiansToDegrees(Real angle)
Definition: Math.h:352
T1 clamp(T1 v, T2 minimum, T3 maximum)
Definition: Math.h:316
Real computeVolume(const T &a, const T &b, const T &c, const T &d)
a, b, c, d sides of the tetrahedron
Definition: Math.h:567
int16_t Int16
Definition: Integer.h:44
const Real PI_4
Definition: Math.h:63
uint16_t UInt16
Definition: Integer.h:39
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
#define INT_IS_FINITE(T)
Definition: Math.h:210
double Real
Definition: Real.h:42
bool isFinite(T v)
Definition: Math.h:307
bool isInf(T v)
Definition: Math.h:295
uint32_t UInt32
Definition: Integer.h:40
T align_ceil2(T value, T alignment)
Definition: Math.h:503
#define AGXCORE_EXPORT
T sign(T v)
Definition: Math.h:326
UInt32 alignPowerOfTwo(UInt32 value)
Definition: Math.h:411
bool isPowerOfTwo(T value)
Definition: Math.h:406