-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath.h
1597 lines (1308 loc) · 28.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
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 RR_MATH
#define RR_MATH
#define PI 3.14159265359f
#define pi32 3.14159265359f
#include <math.h>
struct Interval
{
f32 min;
f32 max;
};
struct integerPoint
{
int x;
int y;
};
static Interval InvertedInfinityInterval()
{
Interval ret;
ret.max = MINFLOAT;
ret.min = MAXFLOAT;
return ret;
}
static f32 Floor(f32 f)
{
return floorf(f);
}
static f32 Ceil(f32 f)
{
return ceilf(f);
}
static i32 Round(float f)
{
return _mm_cvtss_si32(_mm_set_ss(f));
}
static f32 Max(f32 a, f32 b)
{
return _mm_cvtss_f32(_mm_max_ss(_mm_set_ss(b), _mm_set_ss(a)));
}
static f64 Max(f64 a, f64 b)
{
return _mm_cvtsd_f64(_mm_max_sd(_mm_set_sd(b), _mm_set_sd(a)));
}
static float Min(float a, float b)
{
return _mm_cvtss_f32(_mm_min_ss(_mm_set_ss(b), _mm_set_ss(a)));
}
static f32 Clamp(f32 val, f32 min, f32 max)
{
return Min(Max(val, min), max);
}
static float MapRangeToRangeCapped(float f, float minIn, float maxIn, float minOut, float maxOut)
{
if (maxIn == minIn)
{
return 0.0f;
}
return (maxOut - minOut) / (maxIn - minIn) * (Clamp(f, minIn, maxIn) - minIn) + minOut;
}
static float Cos(float a)
{
return cosf(a);
}
static float Sin(float a)
{
return sinf(a);
}
static f32 Sqrt(f32 f)
{
return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(f)));
}
static f32 FastInvSqrt(f32 f)
{
return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(f)));
}
static f32 Square(f32 f)
{
return (f*f);
}
static u32 Square(u32 f)
{
return (f*f);
}
static float Abs(f32 a)
{
return (a > 0) * a - (a < 0) * a;
}
static f64 Abs(f64 a)
{
return Max(a, -a);
}
static u32 Abs(i32 a)
{
return (a > 0) * a - (a < 0) * a;
}
static u32 AbsDiff(u32 a, u32 b)
{
if (a > b)
{
return (a - b);
}
else
{
return (b - a);
}
}
static f32 Dist(f32 a, f32 b)
{
if (a > b)
{
return (a - b);
}
else
{
return (b - a);
}
}
static u32 Min(u32 u1, u32 u2)
{
return (u32)_mm_cvtsi128_si32(_mm_min_epi32(_mm_set1_epi32(u1), _mm_set1_epi32(u2)));
}
static u32 Max(u32 u1, u32 u2)
{
return (u32)_mm_cvtsi128_si32(_mm_max_epi32(_mm_set1_epi32(u1), _mm_set1_epi32(u2)));
}
static i32 Max(i32 u1, i32 u2)
{
return _mm_cvtsi128_si32(_mm_max_epi32(_mm_set1_epi32(u1), _mm_set1_epi32(u2)));
}
static u32 SaveSubstract(u32 from, u32 b)
{
return (from > b) ? (from - b) : 0;
}
static float Norm(v2 a)
{
return Sqrt(a.x * a.x + a.y * a.y);
}
static v2 Normalize(v2 a)
{
float norm = Norm(a);
if (norm != 0.0f)
{
return a / norm;
}
else
{
return V2();
}
}
static float Dot(v2 a, v2 b)
{
return a.x * b.x + a.y * b.y;
}
static v2 Project(v2 a, v2 b)
{
return V2(
Dot(a, b) / ((float)pow(b.x, 2) + (float)pow(b.y, 2))*b.x,
Dot(a, b) / ((float)pow(b.x, 2) + (float)pow(b.y, 2))*b.y
);
}
static float QuadNorm(v2 a)
{
return (powf(a.x, 2) + powf(a.y, 2));
}
static float BoxNorm(v2 a)
{
return Max(Abs(a.x), Abs(a.y));
}
static float BoxDist(v2 a, v2 b)
{
return BoxNorm(a - b);
}
static float Dist(v2 a, v2 b)
{
return Norm(a - b);
}
static float QuadDist(v2 a, v2 b)
{
return QuadNorm(a - b);
}
static u32 BitwiseScanForward(u32 value)
{
unsigned long result = 32;
#if COMPILER_MSVC
if (!_BitScanForward(&result, value))
{
result = 32;
}
#else
for (int i = 0; i < 32; i++)
{
if (value & 1 << i)
{
result = i;
break;
}
}
#endif
return (u32)result;
}
static u32 BitwiseScanReverse(u32 value)
{
unsigned long result;
#if COMPILER_MSVC
if (!_BitScanReverse(&result, value))
{
result = 0;
}
#else
for (int i = 31; i > 0; i++)
{
if (value & (1 << i))
{
result = i;
break;
}
}
#endif
return (u32)result;
}
static bool IsPowerOfTwo(u32 value)
{
u8 highestBit = (u8)BitwiseScanReverse(value);
return (value == (1u << highestBit));
};
static float AngleBetween(v2 a, v2 b)
{
return (float)(atan2(b.y, b.x) - atan2(a.y, a.x));
}
static v2 PerpendicularVector(v2 a) //rotates 90° to the left
{
return V2(-a.y, a.x);
}
static v2 RotateAroundOrigin(v2 vec, float angle)
{
v2 temp = V2(vec.x, vec.y);
vec.x = temp.x * cosf(angle) - temp.y *sinf(angle);
vec.y = temp.y * cosf(angle) + temp.x *sinf(angle);
return vec;
}
static v2 RotateAround(v2 origin, float angle, v2 v)
{
v2 ret;
ret.x = (v.x - origin.x)*cosf(angle) - (v.y - origin.y) * sinf(angle) + origin.x;
ret.y = (v.y - origin.y)*cosf(angle) + (v.x - origin.x) * sinf(angle) + origin.y;
return ret;
}
static v2 LerpVector2(v2 p1, float f1, v2 p2, float f2, float t)
{
//Assert(f1 != f2);
return (p1 + (t - f1) / (f2 - f1) * p2);
}
static v2 Min(v2 a, v2 b)
{
return V2(Min(a.x, b.x), Min(a.y, b.y));
}
static v2 Max(v2 a, v2 b)
{
return V2(Max(a.x, b.x), Max(a.y, b.y));
}
static float Dot(v3 a, v3 b)
{
return
a.x * b.x +
a.y * b.y +
a.z * b.z;
}
static float BoxNorm(v3 a)
{
return Max(Abs(a.x), Max(Abs(a.x), Abs(a.y)));
}
static float Norm(v3 a)
{
return Sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
static v3 Normalize(v3 a)
{
float norm = Norm(a);
if (norm)
{
return a * (1.0f / norm);
}
else
{
return v3();
}
}
static v3 FastNormalize(v3 a)
{
return a * FastInvSqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
static v2 Lerp(v2 a, f32 t, v2 b)
{
return a*(1.0f - t) + b*t;
}
static v3 Lerp(v3 a, f32 t, v3 b)
{
return a*(1.0f - t) + b*t;
}
static float Dist(v3 a, v3 b)
{
return Norm(a - b);
}
static v3 i12(v2 a)
{
return V3(a.x, a.y, 0);
}
static v3 i12(float ax, float ay)
{
return V3(ax, ay, 0);
}
static v3 CrossProduct(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
);
}
static float QuadNorm(v3 a)
{
return ((a.x * a.x) + (a.y * a.y) + (a.z * a.z));
}
static float NormSquared(v3 a)
{
return ((a.x * a.x) + (a.y * a.y) + (a.z * a.z));
}
static float QuadDist(v3 a, v3 b)
{
return QuadNorm(a - b);
}
static v3 LerpVector3(v3 v1, v3 v2, float t)
{
return t * v2 + (1.0f - t) * v1;
}
static v3 LerpVector3(v3 v1, float f1, v3 v2, float f2, float t)
{
if (f1 == f2) return v1;
return (v1 + (t - f1) / (f2 - f1) * (v2 - v1));
}
static v3 Unpack3x8(u32 *pack)
{
//float ignored = (f32)((*pack >> 24) & 0xFF) / 255.0f;
float r = (f32)((*pack >> 16) & 0xFF) / 255.0f;
float g = (f32)((*pack >> 8) & 0xFF) / 255.0f;
float b = (f32)((*pack >> 0) & 0xFF) / 255.0f;
return V3(r, g, b);
}
static v3 Unpack3x8(u32 pack)
{
//float ignored = (f32)((pack >> 24) & 0xFF) / 255.0f;
float b = (f32)((pack >> 16) & 0xFF) / 255.0f;
float g = (f32)((pack >> 8) & 0xFF) / 255.0f;
float r = (f32)((pack >> 0) & 0xFF) / 255.0f;
return V3(r, g, b);
}
static u32 Pack3x8(v3 pack)
{
float a = (1.0f * 255.0f);
float r = (pack.r * 255.0f);
float g = (pack.g * 255.0f);
float b = (pack.b * 255.0f);
return (((u32)(a + 0.5f) << 24) |
((u32)(b + 0.5f) << 16) |
((u32)(g + 0.5f) << 8) |
((u32)(r + 0.5f) << 0));
}
static u32 SavePack3x8(v3 pack)
{
float a = (1.0f * 255.0f);
float r = (Min(pack.r, 1.0f) *255.0f);
float g = (Min(pack.g, 1.0f)* 255.0f);
float b = (Min(pack.b, 1.0f)* 255.0f);
return (((u32)(a + 0.5f) << 24) |
((u32)(b + 0.5f) << 16) |
((u32)(g + 0.5f) << 8) |
((u32)(r + 0.5f) << 0));
}
static v3 Zinversion(v3 v)
{
return V3(v.x, v.y, -v.z);
}
static float Norm(v4 a)
{
return Sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
}
static v4 Normalize(v4 a)
{
float norm = Norm(a);
if (norm)
{
return a * (1.0f / norm);
}
else
{
return V4();
}
}
static v4 Lerp(v4 a, float t, v4 b)
{
return a*(1.0f - t) + b*t;
}
static float Dot(v4 a, v4 b)
{
return a.r * b.r + a.g * b.g;
}
static float QuadNorm(v4 a)
{
return (powf(a.r, 2) + powf(a.g, 2) + powf(a.a, 2) + powf(a.b, 2));
}
static float Dist(v4 a, v4 b)
{
return Norm(a - b);
}
static float QuadDist(v4 a, v4 b)
{
return QuadNorm(a - b);
}
//RGBA
static v4 Unpack4x8(u32 *pack)
{
float a = (f32)((*pack >> 24) & 0xFF) / 255.0f;
float b = (f32)((*pack >> 16) & 0xFF) / 255.0f;
float g = (f32)((*pack >> 8) & 0xFF) / 255.0f;
float r = (f32)((*pack >> 0) & 0xFF) / 255.0f;
return V4(a, r, g, b);
}
static v4 Unpack4x8(u32 pack)
{
float a = (f32)((pack >> 24) & 0xFF) / 255.0f;
float b = (f32)((pack >> 16) & 0xFF) / 255.0f;
float g = (f32)((pack >> 8) & 0xFF) / 255.0f;
float r = (f32)((pack >> 0) & 0xFF) / 255.0f;
return V4(a, r, g, b);
}
static u32 Pack4x8(v4 pack)
{
float a = (pack.a * 255.0f);
float r = (pack.r * 255.0f);
float g = (pack.g * 255.0f);
float b = (pack.b * 255.0f);
return (((u32)(a + 0.5f) << 24) |
((u32)(b + 0.5f) << 16) |
((u32)(g + 0.5f) << 8) |
((u32)(r + 0.5f) << 0));
}
struct Quaternion
{
union
{
struct
{
f32 x, y, z, w;
};
struct
{
v3 v;
f32 r;
};
f32 component[4];
};
};
static Quaternion QuaternionId()
{
Quaternion ret;
ret.w = 1;
ret.x = 0;
ret.y = 0;
ret.z = 0;
return ret;
}
static Quaternion AxisAngleToQuaternion(f32 angle, v3 rotationAxis)
{
v3 normalized = Normalize(rotationAxis);
Quaternion ret;
ret.r = Cos(angle / 2.0f);
ret.v = Sin(angle / 2.0f) * normalized;
return ret;
}
static Quaternion operator*(Quaternion a, Quaternion b)
{
Quaternion ret;
ret.r = a.r * b.r - Dot(a.v, b.v);
ret.v = a.r * b.v + b.r * a.v + CrossProduct(a.v, b.v);
return ret;
}
static Quaternion &operator*=(Quaternion &a, Quaternion b)
{
a = a * b;
return a;
}
static Quaternion operator+(Quaternion a, Quaternion b)
{
Quaternion ret;
ret.w = a.w + b.w;
ret.x = a.x + b.x;
ret.y = a.y + b.y;
ret.z = a.z + b.z;
return ret;
}
static Quaternion operator*(Quaternion a, f32 b)
{
Quaternion ret;
ret.w = a.w * b;
ret.x = a.x * b;
ret.y = a.y * b;
ret.z = a.z * b;
return ret;
}
static Quaternion operator*(f32 b, Quaternion a)
{
Quaternion ret;
ret.w = a.w * b;
ret.x = a.x * b;
ret.y = a.y * b;
ret.z = a.z * b;
return ret;
}
static Quaternion operator/(Quaternion a, f32 b)
{
Quaternion ret;
ret.w = a.w / b;
ret.x = a.x / b;
ret.y = a.y / b;
ret.z = a.z / b;
return ret;
}
static Quaternion operator/(f32 b, Quaternion a)
{
Quaternion ret;
ret.w = a.w / b;
ret.x = a.x / b;
ret.y = a.y / b;
ret.z = a.z / b;
return ret;
}
static f32 Norm(Quaternion a)
{
return Sqrt(a.w * a.w + a.x * a.x + a.y * a.y + a.z * a.z);
}
static Quaternion Normalize(Quaternion a)
{
return (a / Norm(a));
}
static Quaternion Conjugate(Quaternion a)
{
Quaternion ret;
ret.w = a.w;
ret.x = -a.x;
ret.y = -a.y;
ret.z = -a.z;
return ret;
}
static Quaternion Inverse(Quaternion a)
{
Quaternion ret = Conjugate(a);
f32 norm = Norm(a);
ret = ret / (norm * norm);
return ret;
}
// we are using double here, cuz I guess this can be slow, I guess also copy paste from some random blog
static Quaternion Slerp(Quaternion a, f32 t, Quaternion b)
{
Quaternion ret;
// Calculate angle between them.
f32 cosHalfTheta = a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z;
// if a=b or a=-b then theta = 0 and we can return a
if (Abs(cosHalfTheta) >= 1.0) {
return a;
}
f32 halfTheta = acosf(cosHalfTheta);
f32 sinHalfTheta = Sqrt(1.0f - cosHalfTheta*cosHalfTheta);
// if theta = 180 degrees then result is not fully defined
// we could rotate around any axis normal to a or b
if (fabsf(sinHalfTheta) < 0.001f) {
ret.w = (a.w * 0.5f + b.w * 0.5f);
ret.x = (a.x * 0.5f + b.x * 0.5f);
ret.y = (a.y * 0.5f + b.y * 0.5f);
ret.z = (a.z * 0.5f + b.z * 0.5f);
return ret;
}
f32 ratioA = Sin((1 - t) * halfTheta) / sinHalfTheta;
f32 ratioB = Sin( t * halfTheta) / sinHalfTheta;
//calculate Quaternion.
ret.w = (f32)(a.w * ratioA + b.w * ratioB);
ret.x = (f32)(a.x * ratioA + b.x * ratioB);
ret.y = (f32)(a.y * ratioA + b.y * ratioB);
ret.z = (f32)(a.z * ratioA + b.z * ratioB);
return ret;
}
static Quaternion NOID(Quaternion q)
{
f32 norm = Norm(q);
if (!norm)
{
return { 1, 0, 0, 0 };
}
else
{
return q / norm;
}
}
#if ß
static Quaternion operator*(f32 a, Quaternion b)
{
Quaternion ret;
ret.x = a * b.x;
ret.y = a * b.y;
ret.z = a * b.z;
ret.w = a * b.w;
return ret;
}
#endif
static Quaternion Lerp(Quaternion a, f32 t, Quaternion b)
{
return ((1.0f - t) * a + t * b);
}
static Quaternion NLerp(Quaternion a, f32 t, Quaternion b)
{
Quaternion lerp = Lerp(a, t, b);
f32 norm = Norm(lerp);
Assert(norm); // this might be zero, but then we kinda really fucked up.
return lerp / norm;
}
struct m3x3
{
f32 a[3][3];
};
static m3x3 Invert(m3x3 m)
{
double det = m.a[0][0] * (m.a[1][1] * m.a[2][2] - m.a[2][1] * m.a[1][2]) -
m.a[0][1] * (m.a[1][0] * m.a[2][2] - m.a[1][2] * m.a[2][0]) +
m.a[0][2] * (m.a[1][0] * m.a[2][1] - m.a[1][1] * m.a[2][0]);
if (det == 0.0) return {};
double invdet = 1.0 / det;
m3x3 minv;
minv.a[0][0] = (f32)((m.a[1][1] * m.a[2][2] - m.a[2][1] * m.a[1][2]) * invdet);
minv.a[0][1] = (f32)((m.a[0][2] * m.a[2][1] - m.a[0][1] * m.a[2][2]) * invdet);
minv.a[0][2] = (f32)((m.a[0][1] * m.a[1][2] - m.a[0][2] * m.a[1][1]) * invdet);
minv.a[1][0] = (f32)((m.a[1][2] * m.a[2][0] - m.a[1][0] * m.a[2][2]) * invdet);
minv.a[1][1] = (f32)((m.a[0][0] * m.a[2][2] - m.a[0][2] * m.a[2][0]) * invdet);
minv.a[1][2] = (f32)((m.a[1][0] * m.a[0][2] - m.a[0][0] * m.a[1][2]) * invdet);
minv.a[2][0] = (f32)((m.a[1][0] * m.a[2][1] - m.a[2][0] * m.a[1][1]) * invdet);
minv.a[2][1] = (f32)((m.a[2][0] * m.a[0][1] - m.a[0][0] * m.a[2][1]) * invdet);
minv.a[2][2] = (f32)((m.a[0][0] * m.a[1][1] - m.a[1][0] * m.a[0][1]) * invdet);
return minv;
}
static m3x3 Rows3x3(v3 X, v3 Y, v3 Z) // zeilen
{
m3x3 R =
{
{
{ X.x, X.y, X.z},
{ Y.x, Y.y, Y.z},
{ Z.x, Z.y, Z.z},
}
};
return(R);
}
static m3x3 Columns3x3(v3 X, v3 Y, v3 Z) // spalten
{
m3x3 R =
{
{
{ X.x, Y.x, Z.x},
{ X.y, Y.y, Z.y },
{ X.z, Y.z, Z.z },
}
};
return(R);
}
static v3 GetColumn(m3x3 A, u32 C)
{
v3 R = { A.a[0][C], A.a[1][C], A.a[2][C] };
return(R);
}
static v3 GetRow(m3x3 A, u32 R)
{
v3 Result = { A.a[R][0], A.a[R][1], A.a[R][2] };
return(Result);
}
static v3 operator*(m3x3 a, v3 p) // tested, don't fall for this a third time.
{
v3 r;
// second one is row
r.x = p.x*a.a[0][0] + p.y*a.a[0][1] + p.z*a.a[0][2];
r.y = p.x*a.a[1][0] + p.y*a.a[1][1] + p.z*a.a[1][2];
r.z = p.x*a.a[2][0] + p.y*a.a[2][1] + p.z*a.a[2][2];
return r;
}
static m3x3 operator*(m3x3 A, m3x3 B)
{
m3x3 R = {};
for (int r = 0; r < 3; ++r)
{
for (int c = 0; c < 3; ++c)
{
for (int i = 0; i < 3; ++i)
{
R.a[r][c] += A.a[r][i] * B.a[i][c];
}
}
}
return R;
}
static m3x3 XRotation3x3(float Angle)
{
float c = cosf(Angle);
float s = sinf(Angle);
m3x3 R =
{
{
{ 1, 0, 0},
{ 0, c,-s},
{ 0, s, c},
},
};
return(R);
}
static m3x3 YRotation3x3(float Angle)
{
float c = cosf(Angle);
float s = sinf(Angle);
m3x3 R =
{
{
{ c, 0, s },
{ 0, 1, 0 },
{ -s, 0, c },
},
};
return(R);
}
static m3x3 ZRotation3x3(float Angle)
{
float c = cosf(Angle);
float s = sinf(Angle);
m3x3 R =
{
{
{ c,-s, 0 },
{ s, c, 0 },
{ 0, 0, 1 },
},
};
return(R);
}
struct m4x4
{
f32 a[4][4];
};
static m4x4 operator*(m4x4 A, m4x4 B)
{
m4x4 R = {};
for (int r = 0; r <= 3; ++r)
{
for (int c = 0; c <= 3; ++c)
{
for (int i = 0; i <= 3; ++i)
{
R.a[r][c] += A.a[r][i] * B.a[i][c];
}
}
}
return R;
}
#if 0
static m4x4 operator*(f32 f, m4x4 B)
{
m4x4 R = {};
for (int r = 0; r <= 3; ++r)
{
for (int c = 0; c <= 3; ++c)
{
R.a[r][c] = B.a[r][c] * f;
}
}
return R;
}
#endif
static v4 operator*(m4x4 A, v4 P)
{
v4 r;
r.x = P.x*A.a[0][0] + P.y*A.a[0][1] + P.z*A.a[0][2] + P.w*A.a[0][3];
r.y = P.x*A.a[1][0] + P.y*A.a[1][1] + P.z*A.a[1][2] + P.w*A.a[1][3];
r.z = P.x*A.a[2][0] + P.y*A.a[2][1] + P.z*A.a[2][2] + P.w*A.a[2][3];
r.w = P.x*A.a[3][0] + P.y*A.a[3][1] + P.z*A.a[3][2] + P.w*A.a[3][3];
return r;
}
static v3 operator*(m4x4 A, v3 P)
{
v4 R = A * V4(P, 1.0f);
return (R.xyz / R.w);
}
static v3 TransformDirection(m4x4 a, v3 d)
{
v4 R = a * V4(d, 0.0f);
return (R.xyz);
}
static m4x4 Identity(void)
{
m4x4 R =
{
{ { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } },
};
return(R);
}
static m4x4 Translation(v3 a)
{
m4x4 R =
{
{ { 1, 0, 0, a.x },
{ 0, 1, 0, a.y },
{ 0, 0, 1, a.z },
{ 0, 0, 0, 1 } },
};
return(R);
}
static m4x4 XRotation(float Angle)
{
float c = cosf(Angle);
float s = sinf(Angle);
m4x4 R =
{
{ { 1, 0, 0, 0 },
{ 0, c,-s, 0 },