-
Notifications
You must be signed in to change notification settings - Fork 1
/
mathutils.h
80 lines (68 loc) · 1.79 KB
/
mathutils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <cmath>
#include <limits>
class MathUtils
{
public:
template <typename _FloatingPointType>
static bool AlmostEqualWithTolerance(_FloatingPointType A, _FloatingPointType B, _FloatingPointType maxRelativeError, _FloatingPointType maxAbsoluteError)
{
// Check regarding absolute error first
if (fabs(A - B) < maxAbsoluteError)
{
return true;
}
// Then check regarding relative error
_FloatingPointType relativeError = std::numeric_limits<_FloatingPointType>::max();
if (fabs(A) > fabs(B) && A != 0.f)
{
relativeError = fabs((A - B) / A);
}
else if (B != 0.f)
{
relativeError = fabs((A - B) / B);
}
if (relativeError < maxRelativeError)
{
return true;
}
return false;
}
template <typename _NumType>
static _NumType LinearMap( _NumType value,
_NumType originalRangeLowBound,
_NumType originalRangeHighBound,
_NumType targetRangeLowBound,
_NumType targetRangeHighBound)
{
_NumType divisor = originalRangeHighBound - originalRangeLowBound;
if (!divisor)
{
return 0;
}
_NumType num = (value - originalRangeLowBound) * (targetRangeHighBound - targetRangeLowBound);
return targetRangeLowBound + (num / divisor);
}
static long Round(double value)
{
if (value < LONG_MIN - 0.5)
{
return LONG_MIN;
}
if (value > LONG_MAX)
{
return LONG_MAX;
}
if (value - std::floor(value) < 0.5)
{
return static_cast<long>(std::floor(value));
}
return static_cast<long>(std::ceil(value));
}
static bool IsValidTime(double timeValue)
{
return timeValue != std::numeric_limits<double>::infinity() &&
timeValue != std::numeric_limits<double>::quiet_NaN() &&
timeValue != std::numeric_limits<double>::signaling_NaN() &&
timeValue != std::numeric_limits<double>::denorm_min();
}
};