-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.h
601 lines (490 loc) · 13.9 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#ifndef _MATH_H_
#define _MATH_H_
#define m_min(a, b) ((a) < (b) ? (a) : (b))
#define m_max(a, b) ((a) > (b) ? (a) : (b))
#define m_sign(a) ((a) < 0 ? -1 : 1)
#define m_abs(a) ((a) > 0 ? (a) : -(a))
#define m_mod(a, m) (((a) % (m)) >= 0 ? ((a) % (m)) : (((a) % (m)) + (m)))
#define m_clamp(x, a, b) (m_min(b, m_max(a, x)))
#define m_square(a) ((a)*(a))
//#define INFINITY (*(float *)&positive_inf)
#define vec3_f(f) ((Vec3) { f, f, f })
#define vec3_x ((Vec3) { 1.0f, 0.0f, 0.0f })
#define vec3_y ((Vec3) { 0.0f, 1.0f, 0.0f })
#define vec3_z ((Vec3) { 0.0f, 0.0f, 1.0f })
#define vec4_f(f) ((Vec4) { f, f, f, f })
#define sat_i8(x) (int8_t) (m_clamp((x), -128, 127))
#define PI_f (3.14159265359f)
const uint32_t positive_inf = 0x7F800000; // 0xFF << 23
typedef struct { float x, y; } Vec2;
typedef struct { float x, y, z; } Vec3;
typedef union {
struct { float x, y, z, w; };
float nums[4];
} Vec4;
typedef union {
float nums[4][4];
struct { Vec4 x, y, z, w; };
Vec4 cols[4];
} Mat4;
typedef union {
struct {
union { Vec3 xyz; struct { float x,y,z; }; };
float w;
};
float elements[4];
} Quaternion;
//Utillity
static float to_radians(float degrees);
static float lerp(float a, float b, float t);
static float lerp_rad(float a, float b, float t);
static float sign(float f);
static float step(float edge, float x);
static uint32_t rand32(void);
static void seed_rand(uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3);
static float randf(void);
static uint32_t rotl(const uint32_t x, int k);
//Vector operations
//2d
static Vec2 vec2(float x, float y);
static Vec2 add2(Vec2 a, Vec2 b);
static Vec2 add2_f(Vec2 a, float f);
static Vec2 sub2(Vec2 a, Vec2 b);
static Vec2 sub2_f(Vec2 a, float f);
static Vec2 div2(Vec2 a, Vec2 b);
static Vec2 div2_f(Vec2 a, float f);
static Vec2 mul2(Vec2 a, Vec2 b);
static Vec2 mul2_f(Vec2 a, float f);
static float dot2(Vec2 a, Vec2 b);
static float mag2(Vec2 a);
static float magmag2(Vec2 a);
static Vec2 norm2(Vec2 a);
static Vec2 abs2(Vec2 a);
static Vec2 sign2(Vec2 a);
static Vec2 vec2_swap(Vec2 v);
static Vec2 vec2_rot(float rot);
static float rot_vec2(Vec2 rot);
static Vec2 rand2(void);
//3d
static Vec3 vec3(float x, float y, float z);
static Vec3 add3(Vec3 a, Vec3 b);
static Vec3 add3_f(Vec3 a, float f);
static Vec3 sub3(Vec3 a, Vec3 b);
static Vec3 sub3_f(Vec3 a, float f);
static Vec3 div3(Vec3 a, Vec3 b);
static Vec3 div3_f(Vec3 a, float f);
static Vec3 mul3(Vec3 a, Vec3 b);
static Vec3 mul3_f(Vec3 a, float f);
static float dot3(Vec3 a, Vec3 b);
static float mag3(Vec3 a);
static float magmag3(Vec3 a);
static Vec3 norm3(Vec3 a);
static Vec3 abs3(Vec3 a);
static Vec3 sign3(Vec3 a);
static Vec3 max3_f(Vec3 v, float f);
static Vec3 yzx3(Vec3 v);
static Vec3 zxy3(Vec3 v);
static Vec3 step3(Vec3 a, Vec3 b);
static Vec3 lerp3(Vec3 a, Vec3 b, float t);
static Vec3 cross3(Vec3 a, Vec3 b);
static Vec3 rand3(void);
//4d
static Vec4 vec4(float x, float y, float z, float w);
static Vec4 add4(Vec4 a, Vec4 b);
static Vec4 add4_f(Vec4 a, float f);
static Vec4 sub4(Vec4 a, Vec4 b);
static Vec4 sub4_f(Vec4 a, float f);
static Vec4 div4(Vec4 a, Vec4 b);
static Vec4 div4_f(Vec4 a, float f);
static Vec4 mul4(Vec4 a, Vec4 b);
static Vec4 mul4_f(Vec4 a, float f);
static float dot4(Vec4 a, Vec4 b);
static float mag4(Vec4 a);
static float magmag4(Vec4 a);
static Vec4 norm4(Vec4 a);
static Vec4 abs4(Vec4 a);
static Vec4 sign4(Vec4 a);
//Matrix
static Mat4 mul4x4(Mat4 a, Mat4 b);
static Vec4 mul4x44(Mat4 a, Vec4 b);
static Mat4 scale4x4(Vec3 v);
static Mat4 ident4x4();
static Mat4 transpose4x4(Mat4 a);
static Mat4 translate4x4(Vec3 pos);
static Mat4 rotate4x4(Vec3 axis, float angle);
static Mat4 x_rotate4x4(float angle);
static Mat4 y_rotate4x4(float angle);
static Mat4 z_rotate4x4(float angle);
static Mat4 perspective4x4(float fov, float aspect, float n, float f);
static Mat4 look_at4x4(Vec3 eye, Vec3 focus, Vec3 up);
//Uncomment to use in true single header style
#define MATH_IMPLEMENTATION
#ifdef MATH_IMPLEMENTATION
#ifndef MATH_IMPLEMENTATION_ONCE
#define MATH_IMPLEMENTATION_ONCE
static float to_radians(float degrees) {
float result = degrees * (PI_f / 180.0f);
return (result);
}
static float lerp(float a, float b, float t) {
return (1.0f-t)*a+t*b;
}
static float lerp_rad(float a, float b, float t) {
float difference = fmodf(b - a, PI_f*2.0f),
distance = fmodf(2.0f * difference, PI_f*2.0f) - difference;
return a + distance * t;
}
static float sign(float f) {
if (f > 0.0) return -1.0f;
if (f < 0.0) return 1.0f;
else return 0.0f;
}
static float step(float edge, float x) {
return (x < edge) ? 0.0f : 1.0f;
}
static uint32_t rotl(const uint32_t x, int k) {
return (x << k) | (x >> (32 - k));
}
static uint32_t _math_rand_seed[4];
/* source: http://prng.di.unimi.it/xoshiro128plus.c
NOTE: The state must be seeded so that it is not everywhere zero. */
static uint32_t rand32(void) {
const uint32_t result = _math_rand_seed[0] + _math_rand_seed[3],
t = _math_rand_seed[1] << 9;
_math_rand_seed[2] ^= _math_rand_seed[0];
_math_rand_seed[3] ^= _math_rand_seed[1];
_math_rand_seed[1] ^= _math_rand_seed[2];
_math_rand_seed[0] ^= _math_rand_seed[3];
_math_rand_seed[2] ^= t;
_math_rand_seed[3] = rotl(_math_rand_seed[3], 11);
return result;
}
static void seed_rand(uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3) {
_math_rand_seed[0] = s0;
_math_rand_seed[1] = s1;
_math_rand_seed[2] = s2;
_math_rand_seed[3] = s3;
}
static inline float randf(void) {
return (rand32() >> 8) * 0x1.0p-24f;
}
static Vec2 vec2(float x, float y) {
return (Vec2) { x, y };
}
static Vec3 vec3(float x, float y, float z) {
return (Vec3) { x, y, z };
}
static Vec4 vec4(float x, float y, float z, float w) {
return (Vec4) { x, y, z, w};
}
static Vec2 add2(Vec2 a, Vec2 b) {
return vec2(a.x+b.x,a.y+b.y);
}
static Vec3 add3(Vec3 a, Vec3 b) {
return vec3(a.x+b.x,a.y+b.y,a.z+b.z);
}
static Vec4 add4(Vec4 a, Vec4 b) {
return vec4(a.x+b.x,a.y+b.y,a.z+b.z,a.w+b.w);
}
static Vec2 sub2(Vec2 a, Vec2 b) {
return vec2(a.x-b.x,a.y-b.y);
}
static Vec3 sub3(Vec3 a, Vec3 b) {
return vec3(a.x-b.x,a.y-b.y,a.z-b.z);
}
static Vec4 sub4(Vec4 a, Vec4 b) {
return vec4(a.x-b.x,a.y-b.y,a.z-b.z,a.w-b.w);
}
static Vec2 add2_f(Vec2 a, float f) {
return vec2(a.x+f,a.y+f);
}
static Vec3 add3_f(Vec3 a, float f) {
return vec3(a.x+f,a.y+f,a.z+f);
}
static Vec4 add4_f(Vec4 a, float f) {
return vec4(a.x+f,a.y+f,a.z+f,a.w+f);
}
static Vec2 sub2_f(Vec2 a, float f) {
return vec2(a.x-f,a.y-f);
}
static Vec3 sub3_f(Vec3 a, float f) {
return vec3(a.x-f,a.y-f,a.z-f);
}
static Vec4 sub4_f(Vec4 a, float f) {
return vec4(a.x-f,a.y-f,a.z-f,a.w-f);
}
static Vec2 div2(Vec2 a, Vec2 b) {
return vec2(a.x/b.x,a.y/b.y);
}
static Vec3 div3(Vec3 a, Vec3 b) {
return vec3(a.x/b.x,a.y/b.y,a.z/b.z);
}
static Vec4 div4(Vec4 a, Vec4 b) {
return vec4(a.x/b.x,a.y/b.y,a.z/b.z,a.w/b.w);
}
static Vec2 div2_f(Vec2 a, float f) {
return vec2(a.x/f,a.y/f);
}
static Vec3 div3_f(Vec3 a, float f) {
return vec3(a.x/f,a.y/f,a.z/f);
}
static Vec4 div4_f(Vec4 a, float f) {
return vec4(a.x/f,a.y/f,a.z/f,a.w/f);
}
static Vec2 mul2(Vec2 a, Vec2 b) {
return vec2(a.x*b.x,a.y*b.y);
}
static Vec3 mul3(Vec3 a, Vec3 b) {
return vec3(a.x*b.x,a.y*b.y,a.z*b.z);
}
static Vec4 mul4(Vec4 a, Vec4 b) {
return vec4(a.x*b.x,a.y*b.y,a.z*b.z,a.w*b.w);
}
static Vec2 mul2_f(Vec2 a, float f) {
return vec2(a.x*f,a.y*f);
}
static Vec3 mul3_f(Vec3 a, float f) {
return vec3(a.x*f,a.y*f,a.z*f);
}
static Vec4 mul4_f(Vec4 a, float f) {
return vec4(a.x*f,a.y*f,a.z*f,a.w*f);
}
static float dot2(Vec2 a, Vec2 b) {
return a.x*b.x+a.y*b.y;
}
static float dot3(Vec3 a, Vec3 b) {
return a.x*b.x+a.y*b.y+a.z*b.z;
}
static float dot4(Vec4 a, Vec4 b) {
return a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w;
}
static float mag2(Vec2 a) {
return sqrtf(dot2(a, a));
}
static float mag3(Vec3 a) {
return sqrtf(dot3(a, a));
}
static float mag4(Vec4 a) {
return sqrtf(dot4(a, a));
}
static float magmag2(Vec2 a) {
return dot2(a, a);
}
static float magmag3(Vec3 a) {
return dot3(a, a);
}
static float magmag4(Vec4 a) {
return dot4(a, a);
}
static Vec2 norm2(Vec2 a) {
return div2_f(a, mag2(a));
}
static Vec3 norm3(Vec3 a) {
return div3_f(a, mag3(a));
}
static Vec4 norm4(Vec4 a) {
return div4_f(a, mag4(a));
}
static Vec2 abs2(Vec2 a) {
return vec2(fabsf(a.x), fabsf(a.y));
}
static Vec3 abs3(Vec3 a) {
return vec3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
}
static Vec4 abs4(Vec4 a) {
return vec4(fabsf(a.x), fabsf(a.y), fabsf(a.z), fabsf(a.w));
}
static Vec2 sign2(Vec2 a) {
return vec2(sign(a.x), sign(a.y));
}
static Vec3 sign3(Vec3 a) {
return vec3(sign(a.x), sign(a.y), sign(a.z));
}
static Vec4 sign4(Vec4 a) {
return vec4(sign(a.x), sign(a.y), sign(a.z), sign(a.w));
}
static Vec2 vec2_swap(Vec2 v) {
return vec2(v.y, v.x);
}
static Vec2 vec2_rot(float rot) {
return vec2(cosf(rot), sinf(rot));
}
static float rot_vec2(Vec2 rot) {
return atan2f(rot.y, rot.x);
}
static Vec2 rand2(void) {
return vec2_rot(randf() * PI_f);
}
static Vec3 max3_f(Vec3 v, float f) {
return vec3(m_max(v.x, f), m_max(v.y, f), m_max(v.z, f));
}
static Vec3 yzx3(Vec3 v) { return vec3(v.y, v.z, v.x); }
static Vec3 zxy3(Vec3 v) { return vec3(v.z, v.x, v.y); }
static Vec3 step3(Vec3 a, Vec3 b) {
return vec3(step(a.x, b.x), step(a.y, b.y), step(a.z, b.z));
}
static Vec3 lerp3(Vec3 a, Vec3 b, float t) {
return add3(mul3_f(a, 1.0f - t), mul3_f(b, t));
}
static Vec3 cross3(Vec3 a, Vec3 b) {
return vec3((a.y * b.z) - (a.z * b.y),
(a.z * b.x) - (a.x * b.z),
(a.x * b.y) - (a.y * b.x));
}
static Vec3 rand3(void) {
float theta = randf() * PI_f * 2.0f,
z = 1.0f - randf() * 2.0f,
cz = sqrtf(1.0f - powf(z, 2.0f));
return vec3(cz * cosf(theta),
cz * sinf(theta),
z );
}
static Mat4 mul4x4(Mat4 a, Mat4 b) {
Mat4 out = {0};
int8_t k, r, c;
for (c = 0; c < 4; ++c)
for (r = 0; r < 4; ++r) {
out.nums[c][r] = 0.0f;
for (k = 0; k < 4; ++k)
out.nums[c][r] += a.nums[k][r] * b.nums[c][k];
}
return out;
}
static Vec4 mul4x44(Mat4 m, Vec4 v) {
Vec4 res;
for(int x = 0; x < 4; ++x) {
float sum = 0;
for(int y = 0; y < 4; ++y)
sum += m.nums[y][x] * v.nums[y];
res.nums[x] = sum;
}
return res;
}
static Mat4 scale4x4(Vec3 v) {
Mat4 res = {0};
res.nums[0][0] = v.x;
res.nums[1][1] = v.y;
res.nums[2][2] = v.z;
res.nums[3][3] = 1.0;
return res;
}
static Mat4 ident4x4() { return scale4x4(vec3_f(1.0)); }
static Mat4 transpose4x4(Mat4 a) {
Mat4 res;
for(int c = 0; c < 4; ++c)
for(int r = 0; r < 4; ++r)
res.nums[r][c] = a.nums[c][r];
return res;
}
static Mat4 translate4x4(Vec3 pos) {
Mat4 res = ident4x4();
res.nums[3][0] = pos.x;
res.nums[3][1] = pos.y;
res.nums[3][2] = pos.z;
return res;
}
static Mat4 rotate4x4(Vec3 axis, float angle) {
Mat4 res = ident4x4();
axis = norm3(axis);
float sin_theta = sinf(angle);
float cos_theta = cosf(angle);
float cos_value = 1.0f - cos_theta;
res.nums[0][0] = (axis.x * axis.x * cos_value) + cos_theta;
res.nums[0][1] = (axis.x * axis.y * cos_value) + (axis.z * sin_theta);
res.nums[0][2] = (axis.x * axis.z * cos_value) - (axis.y * sin_theta);
res.nums[1][0] = (axis.y * axis.x * cos_value) - (axis.z * sin_theta);
res.nums[1][1] = (axis.y * axis.y * cos_value) + cos_theta;
res.nums[1][2] = (axis.y * axis.z * cos_value) + (axis.x * sin_theta);
res.nums[2][0] = (axis.z * axis.x * cos_value) + (axis.y * sin_theta);
res.nums[2][1] = (axis.z * axis.y * cos_value) - (axis.x * sin_theta);
res.nums[2][2] = (axis.z * axis.z * cos_value) + cos_theta;
return res;
}
static Mat4 x_rotate4x4(float angle) {
float c = cosf(angle);
float s = sinf(angle);
return (Mat4) {{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, c, s, 0.0f,
0.0f, -s, c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
}};
}
static Mat4 y_rotate4x4(float angle) {
float c = cosf(angle);
float s = sinf(angle);
return (Mat4) {{
c, 0.0f, -s, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
s, 0.0f, c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
}};
}
static Mat4 z_rotate4x4(float angle) {
float c = cosf(angle);
float s = sinf(angle);
return (Mat4) {{
c, s, 0.0f, 0.0f,
-s, c, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
}};
}
/* equivalent to XMMatrixPerspectiveFovLH
https://docs.microsoft.com/en-us/windows/win32/api/directxmath/nf-directxmath-xmmatrixperspectivefovlh
*/
static Mat4 perspective4x4(float fov, float aspect, float n, float f) {
fov *= 0.5f;
float height = cosf(fov) / sinf(fov);
float width = height / aspect;
float f_range = f / (f - n);
Mat4 res = {0};
res.nums[0][0] = width;
res.nums[1][1] = height;
res.nums[2][3] = 1.0f;
res.nums[2][2] = f_range;
res.nums[3][2] = -f_range * n;
return res;
}
static Mat4 look_at4x4(Vec3 eye, Vec3 focus, Vec3 up) {
Vec3 eye_dir = sub3(focus, eye);
Vec3 R2 = norm3(eye_dir);
Vec3 R0 = norm3(cross3(up, R2));
Vec3 R1 = cross3(R2, R0);
Vec3 neg_eye = mul3_f(eye, -1.0f);
float D0 = dot3(R0, neg_eye);
float D1 = dot3(R1, neg_eye);
float D2 = dot3(R2, neg_eye);
return (Mat4) {{
{ R0.x, R1.x, R2.x, 0.0f },
{ R0.y, R1.y, R2.y, 0.0f },
{ R0.z, R1.z, R2.z, 0.0f },
{ D0, D1, D2, 1.0f }
}};
}
static Mat4 ortho4x4(
float view_width,
float view_height,
float near_z,
float far_z
) {
float f_range = 1.0f / (far_z - near_z);
Mat4 res;
res.nums[0][0] = 2.0f / view_width;
res.nums[1][1] = 2.0f / view_height;
res.nums[2][2] = f_range;
res.nums[3][2] = -f_range * near_z;
res.nums[3][3] = 1.0f;
return res;
}
static Mat4 shear2d4x4(float x, float y) {
return (Mat4) {{
{ 1.0, y, 0.0, 0.0 },
{ x, 1.0, 0.0, 0.0 },
{ 0.0, 0.0, 1.0, 0.0 },
{ 0.0, 0.0, 0.0, 1.0 },
}};
}
#endif
#endif
#endif