-
Notifications
You must be signed in to change notification settings - Fork 8
/
Math.h
280 lines (234 loc) · 6.51 KB
/
Math.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once
#include <cmath>
class Vector4 {
public:
float x, y, z, w;
};
class Vector3
{
public:
float x, y, z;
inline Vector3() {
x = y = z = 0.0f;
}
inline Vector3(float var) {
x = y = z = var;
}
inline Vector3(float X, float Y, float Z) {
x = X; y = Y; z = Z;
}
inline float operator[](int i) const {
return ((float*)this)[i];
}
inline Vector3& operator-=(float v) {
x -= v; y -= v; z -= v; return *this;
}
inline Vector3 operator*(float v) const {
return Vector3(x * v, y * v, z * v);
}
inline Vector3 operator/(float v) const
{
return Vector3(x / v, y / v, z / v);
}
inline Vector3& operator+=(const Vector3& v) {
x += v.x; y += v.y; z += v.z; return *this;
}
inline Vector3 operator-(const Vector3& v) const {
return Vector3(x - v.x, y - v.y, z - v.z);
}
inline Vector3 operator+(const Vector3& v) const {
return Vector3(x + v.x, y + v.y, z + v.z);
}
inline float Length() {
return sqrtf(x * x + y * y + z * z);
}
inline float dot(Vector3 input) const {
return (x * input.x) + (y * input.y) + (z * input.z);
}
};
class Vector2
{
public:
float x, y;
inline Vector2() {
x = y = 0.0f;
}
inline Vector2 operator/(float v) const {
return Vector2(x / v, y / v);
}
inline Vector2(float X, float Y) {
x = X; y = Y;
}
inline Vector2 operator-(const Vector2& v) const {
return Vector2(x - v.x, y - v.y);
}
inline Vector2 operator+(const Vector2& v) const {
return Vector2(x + v.x, y + v.y);
}
inline Vector2& operator+=(const Vector2& v) {
x += v.x; y += v.y; return *this;
}
inline bool Zero() const {
return (x > -0.1f && x < 0.1f && y > -0.1f && y < 0.1f);
}
};
struct Matrix4x4
{
union
{
struct
{
float _11, _12, _13, _14;
float _21, _22, _23, _24;
float _31, _32, _33, _34;
float _41, _42, _43, _44;
};
float m[4][4];
};
};
namespace Math
{
#define M_PI 3.14159265358979323846f
#define M_RADPI 57.295779513082f
#define M_PI_F ((float)(M_PI))
#define RAD2DEG(x) ((float)(x) * (float)(180.f / M_PI_F))
#define DEG2RAD(x) ((float)(x) * (float)(M_PI_F / 180.f))
#define atan2(a, b) ((float)atan2((double)(a), (double)(b)))
static volatile const double Tiny = 0x1p-1022;
static volatile const double Infinity = INFINITY;
typedef struct { double d0, d1; } double2;
static inline double2 Mul112(double a, double b)
{
static const double c = 0x1p27 + 1;
double
ap = a * c, bp = b * c,
a0 = (a - ap) + ap, b0 = (b - bp) + bp,
a1 = a - a0, b1 = b - b0,
d0 = a * b,
d1 = a0 * b0 - d0 + a0 * b1 + a1 * b0 + a1 * b1;
return double2{ d0, d1 };
}
static inline double2 Mul222(double2 a, double2 b)
{
static const double c = 0x1p27 + 1;
double
ap = a.d0 * c, bp = b.d0 * c,
a0 = (a.d0 - ap) + ap, b0 = (b.d0 - bp) + bp,
a1 = a.d0 - a0, b1 = b.d0 - b0,
m0 = a.d0 * b.d0,
m1 = a0 * b0 - m0 + a0 * b1 + a1 * b0 + a1 * b1 + (a.d0 * b.d1 + a.d1 * b.d0),
d0 = m0 + m1,
d1 = m0 - d0 + m1;
return double2{ d0, d1 };
}
static inline double Mul121Special(double a, double2 b)
{
static const double c = 0x1p27 + 1;
double
ap = a * c, bp = b.d0 * c,
a0 = (a - ap) + ap, b0 = (b.d0 - bp) + bp,
a1 = a - a0, b1 = b.d0 - b0,
m1 = a0 * b0 - 1 + a0 * b1 + a1 * b0 + a1 * b1 + a * b.d1;
return m1;
}
static inline double2 Add212RightSmaller(double2 a, double b)
{
double
c0 = a.d0 + b,
c1 = a.d0 - c0 + b + a.d1,
d0 = c0 + c1,
d1 = c0 - d0 + c1;
return double2{ d0, d1 };
}
static inline double Add221RightSmaller(double2 a, double2 b)
{
double
c0 = a.d0 + b.d0,
c1 = a.d0 - c0 + b.d0 + b.d1 + a.d1,
d0 = c0 + c1;
return d0;
}
static double Tail(double x)
{
if (1 <= x)
return 1 == x
// If x is 1, generate inexact and return Pi/2 rounded down.
? 0x3.243f6a8885a308d313198a2e03707344ap-1 + Tiny
// If x is outside the domain, generate invalid and return NaN.
: Infinity - Infinity;
static const double p01 = 0.2145993335526539017488949;
static const double p02 = -0.0890259194305537131666744;
static const double p03 = 0.0506659694457588602631748;
static const double p04 = -0.0331919619444009606270380;
static const double p05 = 0.0229883479552557203133368;
static const double p06 = -0.0156746038587246716524035;
static const double p07 = 0.0097868293573384001221447;
static const double p08 = -0.0052049731575223952626203;
static const double p09 = 0.0021912255981979442677477;
static const double p10 = -0.0006702485124770180942917;
static const double p11 = 0.0001307564187657962919394;
static const double p12 = -0.0000121189820098929624806;
double polynomial = ((((((((((((
+p12) * x
+ p11) * x
+ p10) * x
+ p09) * x
+ p08) * x
+ p07) * x
+ p06) * x
+ p05) * x
+ p04) * x
+ p03) * x
+ p02) * x
+ p01) * x;
#if UseLongDouble
static const long double HalfPi = 0x3.243f6a8885a308d313198a2ep-1L;
static const long double p00 = -1.5707961988153774692344105L;
return HalfPi + sqrtl(1 - x) * (polynomial + p00);
#else // #if UseLongDouble
static const double2
HalfPi = { 0x1.921fb54442d18p+0, 0x1.1a62633145c07p-54 },
p00 = { -0x1.921fb31e97d96p0, +0x1.eab77149ad27cp-54 };
// Estimate 1 / sqrt(1-x).
double e = 1 / sqrt(1 - x);
double2 ex = Mul112(e, 1 - x); // e * x.
double e2x = Mul121Special(e, ex); // e**2 * x - 1.
// Set t0 to an improved approximation of sqrt(1-x) with Newton-Raphson.
double2 t0 = Add212RightSmaller(ex, ex.d0 * -.5 * e2x);
// Calculate pi/2 + sqrt(1-x) * p(x).
return Add221RightSmaller(HalfPi, Mul222(
t0,
Add212RightSmaller(p00, polynomial)));
#endif // #if UseLongDouble
}
static float abs(float a) {
if (a < 0.f) return -a;
else return a;
}
static float asin(float x) {
float negate = float(x < 0);
x = abs(x);
float ret = -0.0187293;
ret *= x;
ret += 0.0742610;
ret *= x;
ret -= 0.2121144;
ret *= x;
ret += 1.5707288;
ret = 3.14159265358979 * 0.5 - sqrt(1.0 - x) * ret;
return ret - 2 * negate * ret;
}
__forceinline float Dot(const Vector3& Vec1, const Vector3& Vec2) {
return Vec1[0] * Vec2[0] + Vec1[1] * Vec2[1] + Vec1[2] * Vec2[2];
}
__forceinline float Calc3D_Dist(const Vector3& Src, const Vector3& Dst) {
return sqrtf(pow((Src.x - Dst.x), 2) + pow((Src.y - Dst.y), 2) + pow((Src.z - Dst.z), 2));
}
__forceinline float Calc2D_Dist(const Vector2& Src, const Vector2& Dst) {
return sqrt(powf(Src.x - Dst.x, 2) + powf(Src.y - Dst.y, 2));
}
__forceinline Vector2 CalcAngle(const Vector3& Src, const Vector3& Dst) {
Vector3 dir = Src - Dst;
return Vector2{ RAD2DEG(asin(dir.y / dir.Length())), RAD2DEG(-atan2(dir.x, -dir.z)) };
}
}