Momentum Scripting v1
Loading...
Searching...
No Matches
momentum_math.h
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material without having a written signed agreement with Algoryx Simulation AB.
9
10Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
11from using this software, unless otherwise stated in written agreements with
12Algoryx Simulation AB.
13*/
14
15#ifndef MOMENTUM_MATH_H
16#define MOMENTUM_MATH_H
17
18#include "momentum_namespace.h"
19#include "momentum_export.h"
20
21#include "macros.h"
22
23#include <agx/Math.h>
24
25namespace MOMENTUM_NAMESPACE
26{
27 // physics related constants
28 const double GRAVITY_ACCELERATION = double(9.80665); // in m/s2, SI standard acceleration due to gravity
29
30 const double MOMENTUM_EQUIVALENT_EPSILON = (double)1E-9;
31
32 const double Infinity = std::numeric_limits<double>::infinity();
33
34 const double REAL_SQRT_EPSILON = std::sqrt(std::numeric_limits< double >::epsilon());
35
36
37 const double PI = double(M_PI);
38 const double PI_2 = double(M_PI_2);
39 const double PI_4 = double(M_PI_4);
40
46 template<typename T>
47 inline bool _equivalent( T lhs, T rhs, T epsilon = T(MOMENTUM_EQUIVALENT_EPSILON) )
48 {
49 return (lhs + epsilon >= rhs) && (lhs - epsilon <= rhs);
50 }
51
57 inline bool equivalent(double lhs, double rhs, double epsilon = (double)MOMENTUM_EQUIVALENT_EPSILON)
58 {
59 return _equivalent< double >(lhs, rhs, epsilon);
60 }
61
62
69 template<typename T>
70 inline bool _relativelyEquivalent( T lhs, T rhs, T relativeEpsilon = T(MOMENTUM_EQUIVALENT_EPSILON) )
71 {
72 return _equivalent(lhs, rhs, (std::abs(lhs) + std::abs(rhs) + T(1)) * relativeEpsilon);
73 }
74
81 inline bool relativelyEquivalent(double lhs, double rhs, double epsilon = (double)MOMENTUM_EQUIVALENT_EPSILON)
82 {
83 return _relativelyEquivalent< double >(lhs, rhs, epsilon);
84 }
85
87 inline bool equalsZero( double d, double eps=DBL_EPSILON )
88 {
89 return ( d < eps && d > -eps );
90 }
91
92
94 template<typename T1, typename T2>
95 inline T1 const lerp(T1 const& a, T1 const& b, T2 s)
96 {
97 if (s > 1)
98 return b;
99 if (s <=0)
100 return a;
101
102 return (T1)(a*(1-s) + b*s);
103 }
104
106 template<typename T>
107 inline T const logInterpolate(T const& a, T const& b, float s)
108 {
109 return std::pow(T(10), agx::lerp(std::log10(a), std::log10(b), s));
110 }
111
112} // namespace momentum
113
114#endif // MOMENTUM_MATH_H
Namespace for Momentum Scripting API.
Definition: AffineMatrix4x4.h:29
bool relativelyEquivalent(double lhs, double rhs, double epsilon=(double) MOMENTUM_EQUIVALENT_EPSILON)
Compare two values for relative equality.
Definition: momentum_math.h:81
const double MOMENTUM_EQUIVALENT_EPSILON
Definition: momentum_math.h:30
const double PI_4
Definition: momentum_math.h:39
const double GRAVITY_ACCELERATION
Definition: momentum_math.h:28
const double PI_2
Definition: momentum_math.h:38
T1 const lerp(T1 const &a, T1 const &b, T2 s)
Linearly interpolate from a to b using s = {0,..1}.
Definition: momentum_math.h:95
bool _relativelyEquivalent(T lhs, T rhs, T relativeEpsilon=T(MOMENTUM_EQUIVALENT_EPSILON))
Compare two values for relative equality.
Definition: momentum_math.h:70
T const logInterpolate(T const &a, T const &b, float s)
logarithmic interpolation from a to b using s = {0,..1}
Definition: momentum_math.h:107
bool _equivalent(T lhs, T rhs, T epsilon=T(MOMENTUM_EQUIVALENT_EPSILON))
Compare two values for equality.
Definition: momentum_math.h:47
bool equalsZero(double d, double eps=DBL_EPSILON)
Definition: momentum_math.h:87
const double Infinity
Definition: momentum_math.h:32
const double REAL_SQRT_EPSILON
Definition: momentum_math.h:34
const double PI
Definition: momentum_math.h:37
bool equivalent(const Matrix3x3 &a, const Matrix3x3 &b, double epsilon=1e-6)
Definition: Matrix3x3.h:905