-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmath.cpp
More file actions
295 lines (244 loc) · 5.9 KB
/
Copy pathmath.cpp
File metadata and controls
295 lines (244 loc) · 5.9 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "prism/math.h"
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <time.h>
namespace prism {
static struct {
uint32_t mState = 1;
} gPrismRandomData;
void setTimeBasedRandomSeed()
{
setRandomSeed((unsigned int)time(NULL));
}
void setRandomSeed(unsigned int tSeed)
{
uint32_t z = tSeed + 0x9E3779B9u;
z = (z ^ (z >> 16)) * 0x85EBCA6Bu;
z = (z ^ (z >> 13)) * 0xC2B2AE35u;
z = z ^ (z >> 16);
gPrismRandomData.mState = z ? z : 1;
}
// for rollback determinism tests
#if !defined(DREAMCAST) && !defined(VITA) && !defined(__EMSCRIPTEN__)
#define PRISM_COUNT_RANDOM_CALLS 1
#endif
#ifdef PRISM_COUNT_RANDOM_CALLS
static uint64_t gPrismRandomCallAmount = 0;
#endif
unsigned long long getRandomCallAmount()
{
#ifdef PRISM_COUNT_RANDOM_CALLS
return gPrismRandomCallAmount;
#else
return 0;
#endif
}
static uint32_t getNextRandomInteger()
{
#ifdef PRISM_COUNT_RANDOM_CALLS
gPrismRandomCallAmount++;
#endif
uint32_t x = gPrismRandomData.mState;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
gPrismRandomData.mState = x;
return x;
}
unsigned int getRandomState()
{
return gPrismRandomData.mState;
}
void setRandomState(unsigned int tState)
{
gPrismRandomData.mState = tState ? tState : 1;
}
float randfrom(float tMin, float tMax) {
const float range = (tMax - tMin);
if (range == 0) return tMin;
return tMin + range * (getNextRandomInteger() * (1.0f / 4294967296.0f));
}
int randfromInteger(int tMin, int tMax)
{
int val = tMin - 1;
int iters = 0;
while (val < tMin || val > tMax) {
val = (int)randfrom(float(tMin), tMax + 0.99f);
if (iters++ > 100) break;
}
return val;
}
Position randPositionInGeoRectangle(const GeoRectangle& tRectangle)
{
return Vector3D(randfrom(tRectangle.mTopLeft.x, tRectangle.mBottomRight.x), randfrom(tRectangle.mTopLeft.y, tRectangle.mBottomRight.y), randfrom(tRectangle.mTopLeft.z, tRectangle.mBottomRight.z));
}
#define PI_FLOAT 3.14159265f
#define PIBY2_FLOAT 1.5707963f
float fatan2(float y, float x) {
if (x == 0.0f)
{
if (y > 0.0f) return PIBY2_FLOAT;
if (y == 0.0f) return 0.0f;
return -PIBY2_FLOAT;
}
float atan;
float z = (float)(y / x);
if (std::fabs(z) < 1.0f)
{
atan = z / (1.0f + 0.28f * z * z);
if (x < 0.0f)
{
if (y < 0.0f) return atan - PI_FLOAT;
return atan + PI_FLOAT;
}
}
else
{
atan = PIBY2_FLOAT - z / (z * z + 0.28f);
if (y < 0.0f) return atan - PI_FLOAT;
}
return atan;
}
float getLinearInterpolationFactor(float a, float b, float p) {
return (p - a) / (b - a);
}
float interpolateLinear(float a, float b, float t) {
return a + t * (b - a);
}
Matrix4D makeIdentityMatrix4D()
{
Matrix4D ret;
ret.m[0][0] = 1;
ret.m[0][1] = 0;
ret.m[0][2] = 0;
ret.m[0][3] = 0;
ret.m[1][0] = 0;
ret.m[1][1] = 1;
ret.m[1][2] = 0;
ret.m[1][3] = 0;
ret.m[2][0] = 0;
ret.m[2][1] = 0;
ret.m[2][2] = 1;
ret.m[2][3] = 0;
ret.m[3][0] = 0;
ret.m[3][1] = 0;
ret.m[3][2] = 0;
ret.m[3][3] = 1;
return ret;
}
Matrix4D matMult4D(const Matrix4D& tA, const Matrix4D& tB)
{
Matrix4D ret;
int i, j, k;
for (j = 0; j < 4; j++) {
for (i = 0; i < 4; i++) {
ret.m[j][i] = 0;
for (k = 0; k < 4; k++) {
ret.m[j][i] += tA.m[k][i] * tB.m[j][k];
}
}
}
return ret;
}
Matrix4D createScaleMatrix4D(const Vector2D& tScale)
{
return createScaleMatrix4D(tScale.xyz(1.0));
}
Matrix4D createScaleMatrix4D(const Vector3D& tScale)
{
Matrix4D ret;
ret.m[0][0] = tScale.x;
ret.m[0][1] = 0;
ret.m[0][2] = 0;
ret.m[0][3] = 0;
ret.m[1][0] = 0;
ret.m[1][1] = tScale.y;
ret.m[1][2] = 0;
ret.m[1][3] = 0;
ret.m[2][0] = 0;
ret.m[2][1] = 0;
ret.m[2][2] = tScale.z;
ret.m[2][3] = 0;
ret.m[3][0] = 0;
ret.m[3][1] = 0;
ret.m[3][2] = 0;
ret.m[3][3] = 1;
return ret;
}
Matrix4D createTranslationMatrix4D(const Vector2D& tTranslation)
{
return createTranslationMatrix4D(tTranslation.xyz(0.0));
}
Matrix4D createTranslationMatrix4D(const Vector3D& tTranslation)
{
Matrix4D ret;
ret.m[0][0] = 1;
ret.m[0][1] = 0;
ret.m[0][2] = 0;
ret.m[0][3] = 0;
ret.m[1][0] = 0;
ret.m[1][1] = 1;
ret.m[1][2] = 0;
ret.m[1][3] = 0;
ret.m[2][0] = 0;
ret.m[2][1] = 0;
ret.m[2][2] = 1;
ret.m[2][3] = 0;
ret.m[3][0] = tTranslation.x;
ret.m[3][1] = tTranslation.y;
ret.m[3][2] = tTranslation.z;
ret.m[3][3] = 1;
return ret;
}
Matrix4D createRotationZMatrix4D(float tAngle)
{
Matrix4D ret;
ret.m[0][0] = std::cos(tAngle);
ret.m[0][1] = std::sin(tAngle);
ret.m[0][2] = 0;
ret.m[0][3] = 0;
ret.m[1][0] = -std::sin(tAngle);
ret.m[1][1] = std::cos(tAngle);
ret.m[1][2] = 0;
ret.m[1][3] = 0;
ret.m[2][0] = 0;
ret.m[2][1] = 0;
ret.m[2][2] = 1;
ret.m[2][3] = 0;
ret.m[3][0] = 0;
ret.m[3][1] = 0;
ret.m[3][2] = 0;
ret.m[3][3] = 1;
return ret;
}
Matrix4D createOrthographicProjectionMatrix4D(float tLeft, float tRight, float tUp, float tBottom, float tNear, float tFar)
{
Matrix4D ret;
ret.m[0][0] = 2 / (tRight - tLeft);
ret.m[0][1] = 0;
ret.m[0][2] = 0;
ret.m[0][3] = 0;
ret.m[1][0] = 0;
ret.m[1][1] = 2 / (tUp - tBottom);
ret.m[1][2] = 0;
ret.m[1][3] = 0;
ret.m[2][0] = 0;
ret.m[2][1] = 0;
ret.m[2][2] = -2 / (tFar - tNear);
ret.m[2][3] = 0;
ret.m[3][0] = -((tRight + tLeft) / (tRight - tLeft));
ret.m[3][1] = -((tUp + tBottom) / (tUp - tBottom));
ret.m[3][2] = -((tFar + tNear) / (tFar - tNear));
ret.m[3][3] = 1;
return ret;
}
Position rotateScaleTranslatePositionByMatrix4D(const Matrix4D& tMatrix, const Position& tPosition)
{
Position ret;
ret.x = tMatrix.m[0][0] * tPosition.x + tMatrix.m[1][0] * tPosition.y + tMatrix.m[2][0] * tPosition.z + tMatrix.m[3][0];
ret.y = tMatrix.m[0][1] * tPosition.x + tMatrix.m[1][1] * tPosition.y + tMatrix.m[2][1] * tPosition.z + tMatrix.m[3][1];
ret.z = tMatrix.m[0][2] * tPosition.x + tMatrix.m[1][2] * tPosition.y + tMatrix.m[2][2] * tPosition.z + tMatrix.m[3][2];
return ret;
}
}