-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg.h
1772 lines (1553 loc) · 48.9 KB
/
alg.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ALG_H
#define ALG_H
#define ALG_INLINE __attribute__((__always_inline__))
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdint.h>
#ifndef M_PI
#define M_PI 3.14159265f
#endif
#ifdef ALG_DEBUG
#include <assert.h>
#endif
#ifndef ALG_NSIMD
#include "nmmintrin.h"
#else
typedef struct {} __m128;
#endif
#define VEC(N, ...) typedef union v ## N { \
__VA_ARGS__; \
float s[N]; \
} v ## N
#define COMP_2 float x; \
float y;
#define COMP_3 COMP_2 float z;
#define COMP_4 COMP_3 float w;
VEC(2, struct { COMP_2 });
VEC(3, struct { float _0; v2 yz; };
struct { v2 xy; float _2; };
struct { COMP_3 });
VEC(4, struct { v2 xy; v2 zw; };
struct { float _0; v2 yz; float _3; };
struct { v3 xyz; float __3; };
struct { float __0; float yzw; };
struct { COMP_4 };
__m128 v);
#undef VEC
typedef v2 ff;
typedef v3 fff;
typedef v4 ffff;
#define MAT(N, ...) typedef union m ## N { \
float s[N * N]; \
v ## N v[N]; \
struct { __VA_ARGS__ }; \
} m ## N
#define VEC_2(N) v ## N c0; \
v ## N c1;
#define VEC_3(N) VEC_2(N) v ## N c2;
#define VEC_4 VEC_3(4) v4 c3;
MAT(2, VEC_2(2));
MAT(3, VEC_3(3));
MAT(4, VEC_4);
#undef MAT
#undef COMP_2
#undef VEC_2
#undef COMP_3
#undef VEC_3
#undef COMP_4
#undef VEC_4
/* Floating point functions */
static inline float minf(const float a, const float b)
{
return a < b ? a : b;
}
static inline float maxf(const float a, const float b)
{
return a < b ? b : a;
}
static inline float clampf(const float s, const float min, const float max)
{
return minf(max, maxf(min, s));
}
static inline float clamp01f(const float s)
{
return clampf(s, 0.f, 1.f);
}
static inline float lerpf(const float a, const float b, const float t)
{
return (1.f - t) * a + t * b;
}
static inline float lerpf_clamp(const float a, const float b, const float t)
{
return lerpf(a, b, clamp01f(t));
}
static float mixf(const ff range, const float t)
{
return lerpf(range.x, range.y, t);
}
static float mixf_safe(const ff range, const float t)
{
return lerpf_clamp(range.x, range.y, t);
}
static inline float signf(const float s)
{
uint32_t u = *((uint32_t*)&s);
return (float)(u >> 31) * -2.f + 1.f;
}
static inline int is01f(const float s)
{
return s == clamp01f(s);
}
/* Vectors */
#define GEN(N, ID, FV) \
ID(N, v ## N ## _ ## ID) \
ID(N, ID ## FV)
#define V2_FILL(S) ((v2) { S, S })
#define V3_FILL(S) ((v3) { S, S, S })
#define V4_FILL(S) ((v4) { S, S, S, S })
#define shift(N, ID) static v ## N ID(v ## N v, const size_t i) \
{ \
v ## N swap = v; \
for (size_t j = 0; j < N; ++j) { \
size_t k = (i + j) % N; \
v.s[j] = swap.s[k]; \
} \
return v; \
}
GEN(2, shift, ff)
GEN(3, shift, fff)
GEN(4, shift, ffff)
#undef shift
#define V2_ZERO ((v2) { 0 })
#define V3_ZERO ((v3) { 0 })
#define V4_ZERO ((v4) { 0 })
#define V2_ONE V2_FILL(1.f)
#define V3_ONE V3_FILL(1.f)
#define V4_ONE V4_FILL(1.f)
#define V2_RT ((v2) { 1.f, 0.f })
#define V3_RT ((v3) { 1.f, 0.f, 0.f })
#define V4_RT ((v4) { 1.f, 0.f, 0.f, 0.f })
#define V2_UP ((v2) { 0.f, 1.f })
#define V3_UP ((v3) { 0.f, 1.f, 0.f })
#define V4_UP ((v4) { 0.f, 1.f, 0.f, 0.f })
#define V3_FWD ((v3) { 0.f, 0.f, 1.f })
#define V4_FWD ((v4) { 0.f, 0.f, 1.f, 0.f })
#define V2_LFT ((v2) { -1.f, 0.f })
#define V3_LFT ((v3) { -1.f, 0.f, 0.f })
#define V4_LFT ((v4) { -1.f, 0.f, 0.f, 0.f })
#define V2_DN ((v2) { 0.f, -1.f })
#define V3_DN ((v3) { 0.f, -1.f, 0.f })
#define V4_DN ((v4) { 0.f, -1.f, 0.f, 0.f })
#define V3_BCK ((v3) { 0.f, 0.f, -1.f })
#define V4_BCK ((v4) { 0.f, 0.f, -1.f, 0.f })
#define EQ(N) static int v ## N ## _eq(v ## N a, v ## N b) \
{ \
int result = 1; \
for (size_t i = 0; i < N; ++i) \
result &= a.s[i] == b.s[i]; \
return result; \
}
EQ(2)
EQ(3)
EQ(4)
#undef EQ
#define neg(N, ID) static v ## N ID(v ## N v) \
{ \
for (size_t i = 0; i < N; ++i) \
v.s[i] *= -1.f; \
return v; \
}
GEN(2, neg, ff)
GEN(3, neg, fff)
GEN(4, neg, ffff)
#undef neg
#define add(N, ID) static v ## N ID(v ## N a, v ## N b) \
{ \
for (size_t i = 0; i < N; ++i) \
a.s[i] += b.s[i]; \
return a; \
}
GEN(2, add, ff)
GEN(3, add, fff)
GEN(4, add, ffff)
#undef add
#define addeq(N, ID) static void ID(v ## N *a, v ## N b) \
{ \
*a = v ## N ## _add(*a, b); \
}
GEN(2, addeq, ff)
GEN(3, addeq, fff)
GEN(4, addeq, ffff)
#undef addeq
#define sub(N, ID) static v ## N ID(v ## N a, v ## N b) \
{ \
for (size_t i = 0; i < N; ++i) \
a.s[i] -= b.s[i]; \
return a; \
}
GEN(2, sub, ff)
GEN(3, sub, fff)
GEN(4, sub, ffff)
#undef sub
#define mul(N, ID) static v ## N ID(v ## N v, float s) \
{ \
for (size_t i = 0; i < N; ++i) \
v.s[i] *= s; \
return v; \
}
GEN(2, mul, ff)
GEN(3, mul, fff)
GEN(4, mul, ffff)
#undef mul
#define muleq(N, ID) static void ID(v ## N *v, float s) \
{ \
*v = v ## N ## _mul(*v, s); \
}
GEN(2, muleq, ff)
GEN(3, muleq, fff)
GEN(4, muleq, ffff)
#undef muleq
#define inv(N, ID) static v ## N ID(v ## N v) \
{ \
for (size_t i = 0; i < N; ++i) \
v.s[i] = 1.f / v.s[i]; \
return v; \
}
GEN(2, inv, ff)
GEN(3, inv, fff)
GEN(4, inv, ffff)
#undef inv
#define schur(N, ID) static v ## N ID(v ## N a, v ## N b) \
{ \
for (size_t i = 0; i < N; ++i) \
a.s[i] *= b.s[i]; \
return a; \
}
GEN(2, schur, ff)
GEN(3, schur, fff)
GEN(4, schur, ffff)
#undef schur
#define mulinv(N, ID) static v ## N ID(float s, v ## N v) \
{ \
for (size_t i = 0; i < N; ++i) \
v.s[i] = s / v.s[i]; \
return v; \
}
GEN(2, mulinv, ff)
GEN(3, mulinv, fff)
GEN(4, mulinv, ffff)
#undef mulinv
#define magsq(N, ID) static float ID(v ## N v) \
{ \
float s = 0.f; \
for (size_t i = 0; i < N; ++i) \
s += v.s[i] * v.s[i]; \
return s; \
}
GEN(2, magsq, ff)
GEN(3, magsq, fff)
GEN(4, magsq, ffff)
#undef magsq
#define mag(N, ID) static float ID(v ## N v) \
{ \
return sqrtf(v ## N ## _magsq(v)); \
}
GEN(2, mag, ff)
GEN(3, mag, fff)
GEN(4, mag, ffff)
#undef mag
#ifdef ALG_DEBUG
#define norm(N, ID) static v ## N ID(v ## N v) \
{ \
float mag = v ## N ## _mag(v); \
assert(mag >= __FLT_MIN__); \
float inv = 1.f / mag; \
for (size_t i = 0; i < N; ++i) \
v.s[i] *= inv; \
return v; \
}
#else
#define norm(N, ID) static v ## N ID(v ## N v) \
{ \
float mag = v ## N ## _mag(v); \
float inv = 1.f / mag; \
for (size_t i = 0; i < N; ++i) \
v.s[i] *= inv; \
return v; \
}
#endif
GEN(2, norm, ff)
GEN(3, norm, fff)
GEN(4, norm, ffff)
#undef norm
#define NORM_SAFE(N) static v ## N v ## N ## _norm_safe(v ## N v) \
{ \
float mag = v ## N ## _mag(v); \
if (mag < __FLT_MIN__) \
return v; \
float inv = 1.f / mag; \
for (size_t i = 0; i < N; ++i) \
v.s[i] *= inv; \
return v; \
}
NORM_SAFE(2)
NORM_SAFE(3)
NORM_SAFE(4)
#undef NORM_SAFE
ALG_INLINE static ff normff_safe (ff v) { return v2_norm_safe(v) ; }
ALG_INLINE static fff normfff_safe (fff v) { return v3_norm_safe(v) ; }
ALG_INLINE static ffff normffff_safe (ffff v) { return v4_norm_safe(v) ; }
#define isnorm(N, ID) static int ID(v ## N v) \
{ \
float magsq = v ## N ## _magsq(v); \
return fabsf(1.f - magsq) < 1e-6; \
}
GEN(2, isnorm, ff)
GEN(3, isnorm, fff)
GEN(4, isnorm, ffff)
#undef isnorm
#define sign(N, ID) static v ## N ID(v ## N v) \
{ \
for (size_t i = 0; i < N; ++i) \
v.s[i] = signf(v.s[i]); \
return v; \
}
GEN(2, sign, ff)
GEN(3, sign, fff)
GEN(4, sign, ffff)
#undef sign
#define abs(N, ID) static v ## N ID(v ## N v) \
{ \
for (size_t i = 0; i < N; ++i) \
v.s[i] = fabsf(v.s[i]); \
return v; \
}
GEN(2, abs, ff)
GEN(3, abs, fff)
GEN(4, abs, ffff)
#undef abs
#define dot(N, ID) static float ID(v ## N a, v ## N b) \
{ \
float s = 0.f; \
for (size_t i = 0; i < N; ++i) \
s += a.s[i] * b.s[i]; \
return s; \
}
GEN(2, dot, ff)
#undef dot
static ALG_INLINE float v4_dot(v4 a, v4 b)
{
#ifndef ALG_NSIMD
v4 result = { .v = _mm_mul_ps(a.v, b.v) };
#else
v4 result = {
a.x * b.x,
a.y * b.y,
a.z * b.z,
a.w * b.w,
};
#endif
return result.x + result.y + result.z + result.w;
}
static ALG_INLINE float v3_dot(v3 a, v3 b)
{
const float xx = a.x * b.x;
const float yy = a.y * b.y;
const float zz = a.z * b.z;
return xx + yy + zz;
}
#define dotfff(a, b) v3_dot(a, b)
#define dotffff(a, b) v4_dot(a, b)
#define LERP(N) static v ## N v ## N ## _lerp(v ## N a, v ## N b, float s) \
{ \
v ## N v = v ## N ## _add( \
v ## N ## _mul(a, (1.f - s)), \
v ## N ## _mul(b, s) \
); \
return v; \
}
LERP(2)
LERP(3)
LERP(4)
#undef LERP
ALG_INLINE static ff mixff (ff a, ff b, float t) { return v2_lerp(a, b, t) ; }
ALG_INLINE static fff mixfff (fff a, fff b, float t) { return v3_lerp(a, b, t) ; }
ALG_INLINE static ffff mixffff (ffff a, ffff b, float t) { return v4_lerp(a, b, t) ; }
#define LERP_CLAMP(N) static v ## N v ## N ## _lerp_clamp(v ## N a, v ## N b, float s) \
{ \
return v ## N ## _lerp(a, b, clamp01f(s)); \
}
LERP_CLAMP(2)
LERP_CLAMP(3)
LERP_CLAMP(4)
#undef LERP_CLAMP
ALG_INLINE static ff mixff_safe (ff a, ff b, float t) { return v2_lerp_clamp(a, b, t) ; }
ALG_INLINE static fff mixfff_safe (fff a, fff b, float t) { return v3_lerp_clamp(a, b, t) ; }
ALG_INLINE static ffff mixffff_safe (ffff a, ffff b, float t) { return v4_lerp_clamp(a, b, t) ; }
static v3 v3_cross(v3 a, v3 b)
{
return (v3) {
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x,
};
}
#define crossfff(a, b) v3_cross(a, b)
#undef VFN_GEN
/* Matrices */
#define M2_ZERO ((m2) { 0 })
#define M3_ZERO ((m3) { 0 })
#define M4_ZERO ((m4) { 0 })
#define M2_ID ((m2) { .s[0] = 1.f, .s[3] = 1.f })
#define M3_ID ((m3) { .s[0] = 1.f, .s[4] = 1.f, .s [8] = 1.f })
#define M4_ID ((m4) { .s[0] = 1.f, .s[5] = 1.f, .s[10] = 1.f, .s[15] = 1.f })
// Note: you can retrieve the columns from the union directly
#define ROW(N) static v ## N m ## N ## _row(m ## N m, size_t r) \
{ \
v ## N v; \
for (size_t i = 0; i < N; ++i) \
v.s[i] = m.v[i].s[r]; \
return v; \
}
ROW(2)
ROW(3)
ROW(4)
#undef ROW
#define MUL(N) static m ## N m ## N ## _mul(m ## N a, m ## N b) \
{ \
m ## N m; \
for (size_t i = 0; i < N; ++i) { \
m.v[i] = V ## N ## _ZERO; \
for (size_t k = 0; k < N; ++k) { \
for (size_t j = 0; j < N; ++j) { \
m.v[i].s[j] += b.v[i].s[k] * a.v[k].s[j]; \
} \
} \
} \
return m; \
}
MUL(2)
MUL(4)
#undef MUL
// Hand-unroll for m4_model() bottleneck
static m3 m3_mul(m3 a, m3 b)
{
const float x0 = a.c0.x * b.c0.x + a.c1.x * b.c0.y + a.c2.x * b.c0.z;
const float x1 = a.c0.x * b.c1.x + a.c1.x * b.c1.y + a.c2.x * b.c1.z;
const float x2 = a.c0.x * b.c2.x + a.c1.x * b.c2.y + a.c2.x * b.c2.z;
const float y0 = a.c0.y * b.c0.x + a.c1.y * b.c0.y + a.c2.y * b.c0.z;
const float y1 = a.c0.y * b.c1.x + a.c1.y * b.c1.y + a.c2.y * b.c1.z;
const float y2 = a.c0.y * b.c2.x + a.c1.y * b.c2.y + a.c2.y * b.c2.z;
const float z0 = a.c0.z * b.c0.x + a.c1.z * b.c0.y + a.c2.z * b.c0.z;
const float z1 = a.c0.z * b.c1.x + a.c1.z * b.c1.y + a.c2.z * b.c1.z;
const float z2 = a.c0.z * b.c2.x + a.c1.z * b.c2.y + a.c2.z * b.c2.z;
return (m3) {
x0, y0, z0,
x1, y1, z1,
x2, y2, z2,
};
}
#define M2_DIAG(X, Y) ((m2) { X, 0.f, 0.f, Y })
#define M3_DIAG(X, Y, Z) ((m3) { X, 0.f, 0.f, 0.f, Y, 0.f, 0.f, 0.f, Z })
#define M4_DIAG(X, Y, Z, W) ((m4)\
{ X, 0.f, 0.f, 0.f, \
0.f, Y, 0.f, 0.f, \
0.f, 0.f, Z, 0.f, \
0.f, 0.f, 0.f, W, })
#define DIAG(N) static m ## N m ## N ## _diag(v ## N v) \
{ \
m ## N m = M ## N ## _ZERO; \
for (size_t i = 0; i < N; ++i) \
m.s[i + N * i] = v.s[i]; \
return m; \
}
DIAG(2)
DIAG(3)
DIAG(4)
#undef DIAG
#define FILL(A, B) static m ## A m ## A ## _fill_m ## B (m ## B n) \
{ \
m ## A m; \
for (size_t i = 0; i < B; ++i) { \
for (size_t j = 0; j < B; ++j) { \
m.v[i].s[j] = n.v[i].s[j]; \
} \
for (size_t j = B; j < A; ++j) { \
m.v[i].s[j] = 0.f; \
} \
} \
for (size_t i = B; i < A; ++i) { \
m.v[i] = V ## A ## _ZERO; \
} \
return m; \
}
FILL(3, 2)
FILL(4, 2)
FILL(4, 3)
#undef FILL
#define TRACE(N) static float m ## N ## _trace(m ## N m) \
{ \
float result = 0.f; \
for (size_t i = 0; i < N; ++i) \
result += m.s[i + N * i]; \
return result; \
}
TRACE(2)
TRACE(3)
TRACE(4)
#undef TRACE
static v4 m3_to_qt(m3 m)
{
const float trace = m3_trace(m);
const float a = m.v[0].s[0];
const float b = m.v[1].s[1];
const float c = m.v[2].s[2];
// TODO: debug check special orthogonal
if (trace > 0.f) {
const float s = 2.f * sqrtf(1.f + trace);
return (v4) {
(m.c1.z - m.c2.y) / s,
(m.c2.x - m.c0.z) / s,
(m.c0.y - m.c1.x) / s,
.25f * s,
};
} else if ((a > b) && (a > c)) {
float s = 2.f * sqrtf(1.f + a - b - c);
return (v4) {
.25f * s,
(m.c1.x + m.c0.y) / s,
(m.c2.x + m.c0.z) / s,
(m.c1.z - m.c2.y) / s,
};
} else if (b > c) {
const float s = 2.f * sqrtf(1.f + b - a - c);
return (v4) {
(m.c1.x + m.c0.y) / s,
.25f * s,
(m.c2.y + m.c1.z) / s,
(m.c2.x - m.c0.z) / s,
};
} else {
const float s = 2.f * sqrtf(1.f + c - a - b);
return (v4) {
(m.c2.x + m.c0.z) / s,
(m.c2.y + m.c1.z) / s,
.25f * s,
(m.c0.y - m.c1.x) / s,
};
}
}
static ALG_INLINE m4 m4_trans(v3 v)
{
m4 m = M4_ID;
m.c3.xyz = v;
return m;
}
static ALG_INLINE m4 m4_scale(float s)
{
return M4_DIAG(s, s, s, 1.f);
}
static ALG_INLINE m4 m4_scale_aniso(float x, float y, float z)
{
return M4_DIAG(x, y, z, 1.f);
}
// 'Standard' Z; Y from top; left handed
static m4 m4_persp_01(
const float vfov, // Degrees
const float asp,
const float near,
const float far
) {
const float f = 1.f / tanf(.5f * vfov * M_PI / 180.f);
const float y_scale = -f;
const float x_scale = f / asp;
const float z_scale = far / (far - near);
const float z_off = near * far / (near - far);
return (m4) {
.c0.x = x_scale,
.c1.y = y_scale,
.c2.z = z_scale,
.c2.w = 1.f,
.c3.z = z_off,
};
}
// 'Reverse' Z; Y from top; left handed
static m4 m4_persp_10(
const float vfov, // Degrees
const float asp,
const float near,
const float far
) {
const float f = 1.f / tanf(.5f * vfov * M_PI / 180.f);
const float y_scale = -f;
const float x_scale = f / asp;
const float range = near - far;
const float z_scale = near / range;
const float z_off = -far * z_scale; // -far * near / range
return (m4) {
.c0.x = x_scale,
.c1.y = y_scale,
.c2.z = z_scale,
.c2.w = 1.f,
.c3.z = z_off,
};
}
// 'Reverse' Z, no far plane; Y from top; left handed
static m4 m4_persp_10_inf(const float vfov, const float asp, const float near)
{
const float f = 1.f / tanf(.5f * vfov * M_PI / 180.f);
const float y_scale = -f;
const float x_scale = f / asp;
const float z_off = near;
return (m4) {
.c0.x = x_scale,
.c1.y = y_scale,
.c2.w = 1.f,
.c3.z = z_off,
};
}
// 'Standard' Z, -1 to +1 range; Y from bottom; left handed
static m4 m4_persp_gl(
const float vfov, // Degrees
const float asp,
const float near,
const float far
) {
const float y_scale = 1.f / tanf(.5f * vfov * M_PI / 180.f);
const float x_scale = y_scale / asp;
const float range = far - near;
const float z_scale = (far + near) / range;
const float z_off = -2.f * far * near / range;
return (m4) {
.c0.x = x_scale,
.c1.y = y_scale,
.c2.z = z_scale,
.c2.w = 1.f,
.c3.z = z_off,
};
}
static m4 m4_persp_gl_inv(
const float vfov, // Degrees
const float asp,
const float near,
const float far
) {
const float y_scale = 1.f / tanf(.5f * vfov * M_PI / 180.f);
const float x_scale = y_scale / asp;
const float range = far - near;
const float z_scale = (far + near) / range;
const float z_off = -2.f * far * near / range;
return (m4) {
.c0.x = 1.f / x_scale,
.c1.y = 1.f / y_scale,
.c2.w = 1.f / z_off,
.c3.z = 1.f,
.c3.w = -z_scale / z_off,
};
}
// Scale -> number of units that can fit in vertical height
static m4 m4_ortho(float scale, float asp, float near, float far)
{
const float range = far - near;
const float z_scale = 1.f / range;
const float z_off = -near / range;
m4 m = {
.c0.x = 2.f / (asp * scale),
.c1.y = -2.f / scale,
.c2.z = z_scale,
.c3.w = 1.f,
.c3.z = z_off,
};
return m;
}
/* Quaternions */
#define QT_ID ((v4) { 0.f, 0.f, 0.f, 1.f })
static inline v4 qt_conj(v4 q)
{
return (v4) { -q.x, -q.y, -q.z, q.w, };
}
// Hamilton convention
static v4 qt_mul(v4 a, v4 b)
{
v4 c, d;
c = a;
c.z *= -1.f;
d.x = b.w;
d.y = b.z;
d.z = b.y;
d.w = b.x;
const float x = v4_dot(c, d);
c = a;
c.x *= -1.f;
d = v4_shift(b, 2);
const float y = v4_dot(c, d);
c = a;
c.y *= -1.f;
d.x = b.y;
d.y = b.x;
d.z = b.w;
d.w = b.z;
const float z = v4_dot(c, d);
c = v4_neg(a);
c.w *= -1.f;
d = b;
const float w = v4_dot(c, d);
return (v4) { x, y, z, w };
}
static v4 qt_vecs(const v3 fwd, const v3 up)
{
m3 m = {
.c0 = v3_cross(up, fwd),
.c1 = up,
.c2 = fwd,
};
return m3_to_qt(m);
}
// Assumes fwd and up are not orthogonal;
// will modify input fwd vector.
static v4 qt_vecs_up(v3 fwd, const v3 up)
{
#ifdef ALG_DEBUG
assert(v3_isnorm(up));
#endif
v3 right = v3_cross(up, fwd);
right = v3_norm(right);
fwd = v3_cross(right, up);
m3 m = {
.c0 = right,
.c1 = up,
.c2 = fwd,
};
return m3_to_qt(m);
}
// Assumes fwd and up are not orthogonal;
// will modify input up vector.
static v4 qt_vecs_fwd(const v3 fwd, v3 up)
{
#ifdef ALG_DEBUG
assert(v3_isnorm(fwd));
#endif
v3 right = v3_cross(up, fwd);
right = v3_norm(right);
up = v3_cross(fwd, right);
m3 m = {
.c0 = right,
.c1 = up,
.c2 = fwd,
};
return m3_to_qt(m);
}
static v4 qt_axis_angle(v3 axis, float angle)
{
#ifdef ALG_DEBUG
assert(v3_isnorm(axis));
#endif
float half = .5f * angle;
float sin_half = sinf(half);
float cos_half = cosf(half);
return (v4) {
axis.x * sin_half,
axis.y * sin_half,
axis.z * sin_half,
cos_half,
};
}
static ALG_INLINE float qt_angle(v4 q)
{
return 2.f * acosf(q.w);
}
static ffff qt_to_axis_angle(v4 q)
{
const float s = 1.f - q.w * q.w;
if (__FLT_MIN__ >= s)
return V4_UP;
const v3 axis = v3_mul(q.xyz, 1.f / sqrtf(s));
return (v4) {
.xyz = axis,
qt_angle(q),
};
}
/* Optimized formulation:
* gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion
*/
static v3 qt_app(v4 q, v3 v)
{
fff b = q.xyz;
float s = q.w;
return v3_add(
v3_mul(b, 2.f * v3_dot(v, b)),
v3_add(
v3_mul(v, s * s - v3_dot(b, b)),
v3_mul(v3_cross(b, v), 2.f * s)
)
);
}
static v4 qt_nlerp(v4 a, v4 b, float t)
{
return v4_norm(
v4_add(a, v4_mul(v4_sub(b, a), t))
);
}
// Hamilton convention
static m3 qt_to_m3(v4 q)
{
const float xx = q.x * q.x;
const float yy = q.y * q.y;
const float zz = q.z * q.z;
const float ww = q.w * q.w;
const float xy = q.x * q.y;
const float zw = q.z * q.w;
const float xz = q.x * q.z;
const float yw = q.y * q.w;
const float yz = q.y * q.z;
const float xw = q.x * q.w;
float denom = xx + yy + zz + ww;
#ifdef ALG_DEBUG
assert(denom >= __FLT_MIN__);
#endif
float inv = 1.f / denom;
float x = inv * ( xx - yy - zz + ww);
float y = inv * (-xx + yy - zz + ww);
float z = inv * (-xx - yy + zz + ww);
float _2inv = 2.f * inv;
return (m3) {
.c0 = { x, _2inv * (xy + zw), _2inv * (xz - yw) },
.c1 = { _2inv * (xy - zw), y, _2inv * (yz + xw) },
.c2 = { _2inv * (xz + yw), _2inv * (yz - xw), z },
};
}
static m4 qt_to_m4(v4 q)
{
m4 m = m4_fill_m3(qt_to_m3(q));
m.s[15] = 1.f;
return m;
}
/* Runoff */
static m4 m4_model(v3 pos, v4 rot, float scale)
{
const m3 s = M3_DIAG(scale, scale, scale);
const m3 r = qt_to_m3(rot);
const m3 rs = m3_mul(r, s);
m4 result = {
.c0 = { rs.c0.x, rs.c0.y, rs.c0.z, 0.f },
.c1 = { rs.c1.x, rs.c1.y, rs.c1.z, 0.f },
.c2 = { rs.c2.x, rs.c2.y, rs.c2.z, 0.f },
.c3 = { pos.x, pos.y, pos.z, 1.f },
};
return result;
}