-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGMath.h
194 lines (159 loc) · 4.17 KB
/
GMath.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// Clark Kromenaker
//
// General purpose math functions.
//
// NOTE: Called "GMath" to avoid potential name conflict on Windows.
// NOTE: Using global functions (e.g. ::sqrtf instead of std::sqrtf) due to non-conformant C++11 compiler on Linux.
//
#pragma once
#include <algorithm>
#include <cmath>
namespace Math
{
// Floating-point numbers within 0.000001 units are considered equal to one another.
static const float kEpsilon = 1.0e-6f;
// Pi constants.
static const float kPi = 3.1415926535897932384626433832795f;
static const float k2Pi = 2.0f * kPi;
static const float kPiOver2 = kPi / 2.0f;
static const float kPiOver4 = kPi / 4.0f;
inline float Sqrt(float val)
{
return ::sqrtf(val);
}
inline float InvSqrt(float val)
{
//TODO: this could be replaced by a faster (but approximate) calculation
//TODO: the famous "fast inverse square root!"
//TODO: https://www.slideshare.net/maksym_zavershynskyi/fast-inverse-square-root
return (1.0f / ::sqrtf(val));
}
inline bool IsZero(float val)
{
return (::fabsf(val) < kEpsilon);
}
inline bool AreEqual(float a, float b)
{
return IsZero(a - b);
}
inline bool Approximately(float a, float b, float epsilon = kEpsilon)
{
return (::fabs(a - b) < epsilon);
}
inline float Pow(float base, float exp)
{
return std::pow(base, exp);
}
inline int PowBase2(int exp)
{
return 1 << exp;
}
inline float Mod(float num1, float num2)
{
// floating-point equivalent of "return num1 % num2;"
return std::fmod(num1, num2);
}
inline float Sin(float radians)
{
return ::sinf(radians);
}
inline float Asin(float ratio)
{
return ::asinf(ratio);
}
inline float Cos(float radians)
{
return ::cosf(radians);
}
inline float Acos(float ratio)
{
return ::acosf(ratio);
}
inline float Tan(float radians)
{
return ::tanf(radians);
}
inline float Atan(float ratio)
{
return ::atanf(ratio);
}
inline float Atan2(float y, float x)
{
return std::atan2(y, x);
}
inline float Floor(float val)
{
return std::floor(val);
}
inline float Ceil(float val)
{
return std::ceil(val);
}
inline float Round(float val)
{
return std::round(val);
}
inline int FloorToInt(float val)
{
return static_cast<int>(Floor(val));
}
inline int CeilToInt(float val)
{
return static_cast<int>(Ceil(val));
}
inline int RoundToInt(float val)
{
return static_cast<int>(Round(val));
}
inline float Clamp(float value, float min, float max)
{
return std::fmin(max, std::fmax(value, min));
}
inline int Clamp(int value, int min, int max)
{
return std::min(max, std::max(value, min));
}
inline float Min(float val1, float val2)
{
return std::fmin(val1, val2);
}
inline int Min(int val1, int val2)
{
return std::min(val1, val2);
}
inline float Max(float val1, float val2)
{
return std::fmax(val1, val2);
}
inline int Max(int val1, int val2)
{
return std::max(val1, val2);
}
inline float Abs(float val)
{
return std::abs(val);
}
inline float MagnitudeSign(float mag, float sign)
{
// Take magnitude of first number and sign of second number.
// Return product of those two things. (e.g. 35, -18 => -35)
return std::copysign(mag, sign);
}
inline float ToDegrees(float radians)
{
return (radians * (180.0f / kPi));
}
inline float ToRadians(float degrees)
{
return (degrees * (kPi / 180.0f));
}
inline float Lerp(float a, float b, float t)
{
return a + ((b - a) * t);
}
inline unsigned char Lerp(unsigned char a, unsigned char b, float t)
{
return static_cast<unsigned char>(static_cast<float>(a) + (static_cast<float>(b - a) * t));
}
}