-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mat4.cs
1716 lines (1559 loc) · 58.5 KB
/
Mat4.cs
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
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A readonly struct influenced by GLSL, OSL and Processing's PMatrix3D.
/// Although this is a 4 x 4 matrix, it is generally assumed to be a 3D affine
/// transform matrix, where the last row is (0.0, 0.0, 0.0, 1.0) .
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit, Pack = 64)]
public readonly struct Mat4 : IEquatable<Mat4>, IEnumerable
{
/// <summary>
/// Component in row 0, column 0. The right axis x component.
/// </summary>
[FieldOffset(0)] private readonly float m00;
/// <summary>
/// Component in row 0, column 1. The forward axis x component.
/// </summary>
[FieldOffset(4)] private readonly float m01;
/// <summary>
/// Component in row 0, column 2. The up axis x component.
/// </summary>
[FieldOffset(8)] private readonly float m02;
/// <summary>
/// Component in row 0, column 3. The translation x component.
/// </summary>
[FieldOffset(12)] private readonly float m03;
/// <summary>
/// Component in row 1, column 0. The right axis y component.
/// </summary>
[FieldOffset(16)] private readonly float m10;
/// <summary>
/// Component in row 1, column 1. The forward axis y component.
/// </summary>
[FieldOffset(20)] private readonly float m11;
/// <summary>
/// Component in row 1, column 2. The up axis y component.
/// </summary>
[FieldOffset(24)] private readonly float m12;
/// <summary>
/// Component in row 1, column 3. The translation y component.
/// </summary>
[FieldOffset(28)] private readonly float m13;
/// <summary>
/// Component in row 2, column 0. The right axis z component.
/// </summary>
[FieldOffset(32)] private readonly float m20;
/// <summary>
/// Component in row 2, column 1. The forward axis z component.
/// </summary>
[FieldOffset(36)] private readonly float m21;
/// <summary>
/// Component in row 2, column 2. The up axis z component.
/// </summary>
[FieldOffset(40)] private readonly float m22;
/// <summary>
/// Component in row 2, column 3. The translation z component.
/// </summary>
[FieldOffset(44)] private readonly float m23;
/// <summary>
/// Component in row 3, column 0. The right axis w component.
/// </summary>
[FieldOffset(48)] private readonly float m30;
/// <summary>
/// Component in row 3, column 1. The forward axis w component.
/// </summary>
[FieldOffset(52)] private readonly float m31;
/// <summary>
/// Component in row 3, column 2. The up axis w component.
/// </summary>
[FieldOffset(56)] private readonly float m32;
/// <summary>
/// Component in row 3, column 3. The translation w component.
/// </summary>
[FieldOffset(60)] private readonly float m33;
/// <summary>
/// Returns the number of values in this matrix.
/// </summary>
/// <value>length</value>
public int Length { get { return 16; } }
/// <summary>
/// Component in row 0, column 0. The right axis x component.
/// </summary>
/// <value>right axis x</value>
public float M00 { get { return this.m00; } }
/// <summary>
/// Component in row 0, column 1. The forward axis x component.
/// </summary>
/// <value>forward axis x</value>
public float M01 { get { return this.m01; } }
/// <summary>
/// Component in row 0, column 2. The up axis x component.
/// </summary>
/// <value>up axis x</value>
public float M02 { get { return this.m02; } }
/// <summary>
/// Component in row 0, column 3. The translation x component.
/// </summary>
/// <value>translation x</value>
public float M03 { get { return this.m03; } }
/// <summary>
/// Component in row 1, column 0. The right axis y component.
/// </summary>
/// <value>right axis y</value>
public float M10 { get { return this.m10; } }
/// <summary>
/// Component in row 1, column 1. The forward axis y component.
/// </summary>
/// <value>forward axis y</value>
public float M11 { get { return this.m11; } }
/// <summary>
/// Component in row 1, column 2. The up axis y component.
/// </summary>
/// <value>up axis y</value>
public float M12 { get { return this.m12; } }
/// <summary>
/// Component in row 1, column 3. The translation y component.
/// </summary>
/// <value>translation y</value>
public float M13 { get { return this.m13; } }
/// <summary>
/// Component in row 2, column 0. The right axis z component.
/// </summary>
/// <value>right axis z</value>
public float M20 { get { return this.m20; } }
/// <summary>
/// Component in row 2, column 1. The forward axis z component.
/// </summary>
/// <value>forward axis z</value>
public float M21 { get { return this.m21; } }
/// <summary>
/// Component in row 2, column 2. The up axis z component.
/// </summary>
/// <value>up axis z</value>
public float M22 { get { return this.m22; } }
/// <summary>
/// Component in row 2, column 3. The translation z component.
/// </summary>
/// <value>translation z</value>
public float M23 { get { return this.m23; } }
/// <summary>
/// Component in row 3, column 0. The right axis w component.
/// </summary>
/// <value>right axis w</value>
public float M30 { get { return this.m30; } }
/// <summary>
/// Component in row 3, column 1. The forward axis w component.
/// </summary>
/// <value>forward axis w</value>
public float M31 { get { return this.m31; } }
/// <summary>
/// Component in row 3, column 2. The up axis w component.
/// </summary>
/// <value>up axis w</value>
public float M32 { get { return this.m32; } }
/// <summary>
/// Component in row 3, column 3. The translation w component.
/// </summary>
/// <value>translation w</value>
public float M33 { get { return this.m33; } }
/// <summary>
/// The first column, or right axis.
/// </summary>
/// <returns>right axis</returns>
public Vec4 Right
{
get
{
return new Vec4(this.m00, this.m10, this.m20, this.m30);
}
}
/// <summary>
/// The second column, or forward axis.
/// </summary>
/// <returns>forward axis</returns>
public Vec4 Forward
{
get
{
return new Vec4(this.m01, this.m11, this.m21, this.m31);
}
}
/// <summary>
/// The third column, or up axis.
/// </summary>
/// <returns>up axis</returns>
public Vec4 Up
{
get
{
return new Vec4(this.m02, this.m12, this.m22, this.m32);
}
}
/// <summary>
/// The fourth column, or translation.
/// </summary>
/// <returns>translation</returns>
public Vec4 Translation
{
get
{
return new Vec4(this.m03, this.m13, this.m23, this.m33);
}
}
/// <summary>
/// Constructs a matrix from float values.
/// </summary>
/// <param name="m00">row 0, column 0</param>
/// <param name="m01">row 0, column 1</param>
/// <param name="m02">row 0, column 2</param>
/// <param name="m03">row 0, column 3</param>
/// <param name="m10">row 1, column 0</param>
/// <param name="m11">row 1, column 1</param>
/// <param name="m12">row 1, column 2</param>
/// <param name="m13">row 1, column 3</param>
/// <param name="m20">row 2, column 0</param>
/// <param name="m21">row 2, column 1</param>
/// <param name="m22">row 2, column 2</param>
/// <param name="m23">row 2, column 3</param>
/// <param name="m30">row 3, column 0</param>
/// <param name="m31">row 3, column 1</param>
/// <param name="m32">row 3, column 2</param>
/// <param name="m33">row 3, column 3</param>
public Mat4(
in float m00 = 1.0f, in float m01 = 0.0f, in float m02 = 0.0f, in float m03 = 0.0f,
in float m10 = 0.0f, in float m11 = 1.0f, in float m12 = 0.0f, in float m13 = 0.0f,
in float m20 = 0.0f, in float m21 = 0.0f, in float m22 = 1.0f, in float m23 = 0.0f,
in float m30 = 0.0f, in float m31 = 0.0f, in float m32 = 0.0f, in float m33 = 1.0f)
{
this.m00 = m00;
this.m01 = m01;
this.m02 = m02;
this.m03 = m03;
this.m10 = m10;
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m20 = m20;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
this.m30 = m30;
this.m31 = m31;
this.m32 = m32;
this.m33 = m33;
}
/// <summary>
/// Constructs a matrix from Boolean values.
/// </summary>
/// <param name="m00">row 0, column 0</param>
/// <param name="m01">row 0, column 1</param>
/// <param name="m02">row 0, column 2</param>
/// <param name="m03">row 0, column 3</param>
/// <param name="m10">row 1, column 0</param>
/// <param name="m11">row 1, column 1</param>
/// <param name="m12">row 1, column 2</param>
/// <param name="m13">row 1, column 3</param>
/// <param name="m20">row 2, column 0</param>
/// <param name="m21">row 2, column 1</param>
/// <param name="m22">row 2, column 2</param>
/// <param name="m23">row 2, column 3</param>
/// <param name="m30">row 3, column 0</param>
/// <param name="m31">row 3, column 1</param>
/// <param name="m32">row 3, column 2</param>
/// <param name="m33">row 3, column 3</param>
public Mat4(
in bool m00 = true, in bool m01 = false, in bool m02 = false, in bool m03 = false,
in bool m10 = false, in bool m11 = true, in bool m12 = false, in bool m13 = false,
in bool m20 = false, in bool m21 = false, in bool m22 = true, in bool m23 = false,
in bool m30 = false, in bool m31 = false, in bool m32 = false, in bool m33 = true)
{
this.m00 = m00 ? 1.0f : 0.0f;
this.m01 = m01 ? 1.0f : 0.0f;
this.m02 = m02 ? 1.0f : 0.0f;
this.m03 = m03 ? 1.0f : 0.0f;
this.m10 = m10 ? 1.0f : 0.0f;
this.m11 = m11 ? 1.0f : 0.0f;
this.m12 = m12 ? 1.0f : 0.0f;
this.m13 = m13 ? 1.0f : 0.0f;
this.m20 = m20 ? 1.0f : 0.0f;
this.m21 = m21 ? 1.0f : 0.0f;
this.m22 = m22 ? 1.0f : 0.0f;
this.m23 = m23 ? 1.0f : 0.0f;
this.m30 = m30 ? 1.0f : 0.0f;
this.m31 = m31 ? 1.0f : 0.0f;
this.m32 = m32 ? 1.0f : 0.0f;
this.m33 = m33 ? 1.0f : 0.0f;
}
/// <summary>
/// Tests this matrix for equivalence with an object.
/// </summary>
/// <param name="value">the object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Mat4 mat) { return this.Equals(mat); }
return false;
}
/// <summary>
/// Returns a hash code representing this matrix.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
int hash = Utils.MulBase ^ this.m00.GetHashCode();
hash = hash * Utils.HashMul ^ this.m01.GetHashCode();
hash = hash * Utils.HashMul ^ this.m02.GetHashCode();
hash = hash * Utils.HashMul ^ this.m03.GetHashCode();
hash = hash * Utils.HashMul ^ this.m10.GetHashCode();
hash = hash * Utils.HashMul ^ this.m11.GetHashCode();
hash = hash * Utils.HashMul ^ this.m12.GetHashCode();
hash = hash * Utils.HashMul ^ this.m13.GetHashCode();
hash = hash * Utils.HashMul ^ this.m20.GetHashCode();
hash = hash * Utils.HashMul ^ this.m21.GetHashCode();
hash = hash * Utils.HashMul ^ this.m22.GetHashCode();
hash = hash * Utils.HashMul ^ this.m23.GetHashCode();
hash = hash * Utils.HashMul ^ this.m30.GetHashCode();
hash = hash * Utils.HashMul ^ this.m31.GetHashCode();
hash = hash * Utils.HashMul ^ this.m32.GetHashCode();
hash = hash * Utils.HashMul ^ this.m33.GetHashCode();
return hash;
}
/// <summary>
/// Returns a string representation of this matrix.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Mat4.ToString(this);
}
/// <summary>
/// Tests this matrix for equivalence with another in compliance with the
/// IEquatable interface.
/// </summary>
/// <param name="m">matrix</param>
/// <returns>equivalence</returns>
public bool Equals(Mat4 m)
{
if (this.m00.GetHashCode() != m.m00.GetHashCode()) { return false; }
if (this.m01.GetHashCode() != m.m01.GetHashCode()) { return false; }
if (this.m02.GetHashCode() != m.m02.GetHashCode()) { return false; }
if (this.m03.GetHashCode() != m.m03.GetHashCode()) { return false; }
if (this.m10.GetHashCode() != m.m10.GetHashCode()) { return false; }
if (this.m11.GetHashCode() != m.m11.GetHashCode()) { return false; }
if (this.m12.GetHashCode() != m.m12.GetHashCode()) { return false; }
if (this.m13.GetHashCode() != m.m13.GetHashCode()) { return false; }
if (this.m20.GetHashCode() != m.m20.GetHashCode()) { return false; }
if (this.m21.GetHashCode() != m.m21.GetHashCode()) { return false; }
if (this.m22.GetHashCode() != m.m22.GetHashCode()) { return false; }
if (this.m23.GetHashCode() != m.m23.GetHashCode()) { return false; }
if (this.m30.GetHashCode() != m.m30.GetHashCode()) { return false; }
if (this.m31.GetHashCode() != m.m31.GetHashCode()) { return false; }
if (this.m32.GetHashCode() != m.m32.GetHashCode()) { return false; }
if (this.m33.GetHashCode() != m.m33.GetHashCode()) { return false; }
return true;
}
/// <summary>
/// Returns an enumerator (or iterator) for this matrix, allowing its
/// components to be accessed in a foreach loop.
/// </summary>
/// <returns>enumerator</returns>
public IEnumerator GetEnumerator()
{
yield return this.m00;
yield return this.m01;
yield return this.m02;
yield return this.m03;
yield return this.m10;
yield return this.m11;
yield return this.m12;
yield return this.m13;
yield return this.m20;
yield return this.m21;
yield return this.m22;
yield return this.m23;
yield return this.m30;
yield return this.m31;
yield return this.m32;
yield return this.m33;
}
/// <summary>
/// Converts a boolean to a matrix by supplying the boolean to all the
/// matrix's components: 1.0 for true; 0.0 for false.
/// </summary>
/// <param name="b">the boolean</param>
/// <returns>the vector</returns>
public static implicit operator Mat4(in bool b)
{
float eval = b ? 1.0f : 0.0f;
return new Mat4(
eval, eval, eval, eval,
eval, eval, eval, eval,
eval, eval, eval, eval,
eval, eval, eval, eval);
}
/// <summary>
/// Converts a rotation from quaternion to matrix representation.
/// </summary>
/// <param name="q">quaternion</param>
public static implicit operator Mat4(in Quat q)
{
return Mat4.FromRotation(q);
}
/// <summary>
/// A matrix evaluates to true when all of its components are not equal to
/// zero.
/// </summary>
/// <param name="m">the input matrix</param>
/// <returns>evaluation</returns>
public static bool operator true(in Mat4 m)
{
return Mat4.All(m);
}
/// <summary>
/// A matrix evaluates to false when all of its elements are equal to zero.
/// </summary>
/// <param name="m">the input matrix</param>
/// <returns>evaluation</returns>
public static bool operator false(in Mat4 m)
{
return Mat4.None(m);
}
/// <summary>
/// Evaluates two matrices like booleans, using the and logic gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Mat4 operator &(in Mat4 a, in Mat4 b)
{
return new Mat4(
Utils.And(a.m00, b.m00), Utils.And(a.m01, b.m01),
Utils.And(a.m02, b.m02), Utils.And(a.m03, b.m03),
Utils.And(a.m10, b.m10), Utils.And(a.m11, b.m11),
Utils.And(a.m12, b.m12), Utils.And(a.m13, b.m13),
Utils.And(a.m20, b.m20), Utils.And(a.m21, b.m21),
Utils.And(a.m22, b.m22), Utils.And(a.m23, b.m23),
Utils.And(a.m30, b.m30), Utils.And(a.m31, b.m31),
Utils.And(a.m32, b.m32), Utils.And(a.m33, b.m33));
}
/// <summary>
/// Evaluates two matrices like booleans, using the inclusive or (OR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Mat4 operator |(in Mat4 a, in Mat4 b)
{
return new Mat4(
Utils.Or(a.m00, b.m00), Utils.Or(a.m01, b.m01),
Utils.Or(a.m02, b.m02), Utils.Or(a.m03, b.m03),
Utils.Or(a.m10, b.m10), Utils.Or(a.m11, b.m11),
Utils.Or(a.m12, b.m12), Utils.Or(a.m13, b.m13),
Utils.Or(a.m20, b.m20), Utils.Or(a.m21, b.m21),
Utils.Or(a.m22, b.m22), Utils.Or(a.m23, b.m23),
Utils.Or(a.m30, b.m30), Utils.Or(a.m31, b.m31),
Utils.Or(a.m32, b.m32), Utils.Or(a.m33, b.m33));
}
/// <summary>
/// Evaluates two matrices like booleans, using the exclusive or (XOR) logic
/// gate.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>evaluation</returns>
public static Mat4 operator ^(in Mat4 a, in Mat4 b)
{
return new Mat4(
Utils.Xor(a.m00, b.m00), Utils.Xor(a.m01, b.m01),
Utils.Xor(a.m02, b.m02), Utils.Xor(a.m03, b.m03),
Utils.Xor(a.m10, b.m10), Utils.Xor(a.m11, b.m11),
Utils.Xor(a.m12, b.m12), Utils.Xor(a.m13, b.m13),
Utils.Xor(a.m20, b.m20), Utils.Xor(a.m21, b.m21),
Utils.Xor(a.m22, b.m22), Utils.Xor(a.m23, b.m23),
Utils.Xor(a.m30, b.m30), Utils.Xor(a.m31, b.m31),
Utils.Xor(a.m32, b.m32), Utils.Xor(a.m33, b.m33));
}
/// <summary>
/// Negates the input matrix.
/// </summary>
/// <param name="m">matrix</param>
/// <returns>negation</returns>
public static Mat4 operator -(in Mat4 m)
{
return new Mat4(
-m.m00, -m.m01, -m.m02, -m.m03,
-m.m10, -m.m11, -m.m12, -m.m13,
-m.m20, -m.m21, -m.m22, -m.m23,
-m.m30, -m.m31, -m.m32, -m.m33);
}
/// <summary>
/// Multiplies two matrices.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Mat4 operator *(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 * b.m00 + a.m01 * b.m10 + a.m02 * b.m20 + a.m03 * b.m30,
a.m00 * b.m01 + a.m01 * b.m11 + a.m02 * b.m21 + a.m03 * b.m31,
a.m00 * b.m02 + a.m01 * b.m12 + a.m02 * b.m22 + a.m03 * b.m32,
a.m00 * b.m03 + a.m01 * b.m13 + a.m02 * b.m23 + a.m03 * b.m33,
a.m10 * b.m00 + a.m11 * b.m10 + a.m12 * b.m20 + a.m13 * b.m30,
a.m10 * b.m01 + a.m11 * b.m11 + a.m12 * b.m21 + a.m13 * b.m31,
a.m10 * b.m02 + a.m11 * b.m12 + a.m12 * b.m22 + a.m13 * b.m32,
a.m10 * b.m03 + a.m11 * b.m13 + a.m12 * b.m23 + a.m13 * b.m33,
a.m20 * b.m00 + a.m21 * b.m10 + a.m22 * b.m20 + a.m23 * b.m30,
a.m20 * b.m01 + a.m21 * b.m11 + a.m22 * b.m21 + a.m23 * b.m31,
a.m20 * b.m02 + a.m21 * b.m12 + a.m22 * b.m22 + a.m23 * b.m32,
a.m20 * b.m03 + a.m21 * b.m13 + a.m22 * b.m23 + a.m23 * b.m33,
a.m30 * b.m00 + a.m31 * b.m10 + a.m32 * b.m20 + a.m33 * b.m30,
a.m30 * b.m01 + a.m31 * b.m11 + a.m32 * b.m21 + a.m33 * b.m31,
a.m30 * b.m02 + a.m31 * b.m12 + a.m32 * b.m22 + a.m33 * b.m32,
a.m30 * b.m03 + a.m31 * b.m13 + a.m32 * b.m23 + a.m33 * b.m33);
}
/// <summary>
/// Multiplies each component in a matrix by a scalar.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Mat4 operator *(in Mat4 a, in float b)
{
return new Mat4(
a.m00 * b, a.m01 * b, a.m02 * b, a.m03 * b,
a.m10 * b, a.m11 * b, a.m12 * b, a.m13 * b,
a.m20 * b, a.m21 * b, a.m22 * b, a.m23 * b,
a.m30 * b, a.m31 * b, a.m32 * b, a.m33 * b);
}
/// <summary>
/// Multiplies each component in a matrix by a scalar.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>product</returns>
public static Mat4 operator *(in float a, in Mat4 b)
{
return new Mat4(
a * b.m00, a * b.m01, a * b.m02, a * b.m03,
a * b.m10, a * b.m11, a * b.m12, a * b.m13,
a * b.m20, a * b.m21, a * b.m22, a * b.m23,
a * b.m30, a * b.m31, a * b.m32, a * b.m33);
}
/// <summary>
/// Multiplies a matrix and a vector.
/// </summary>
/// <param name="a">matrix</param>
/// <param name="b">vector</param>
/// <returns>product</returns>
public static Vec4 operator *(in Mat4 a, in Vec4 b)
{
float x = b.X;
float y = b.Y;
float z = b.Z;
float w = b.W;
return new Vec4(
a.m00 * x + a.m01 * y + a.m02 * z + a.m03 * w,
a.m10 * x + a.m11 * y + a.m12 * z + a.m13 * w,
a.m20 * x + a.m21 * y + a.m22 * z + a.m23 * w,
a.m30 * x + a.m31 * y + a.m32 * z + a.m33 * w);
}
/// <summary>
/// Multiplies a vector and a matrix's transpose, then transposes the
/// result.
/// </summary>
/// <param name="a">vector</param>
/// <param name="b">matrix</param>
/// <returns>product</returns>
public static Vec4 operator *(in Vec4 a, in Mat4 b)
{
float x = a.X;
float y = a.Y;
float z = a.Z;
float w = a.W;
return new Vec4(
b.m00 * x + b.m10 * y + b.m20 * z + b.m30 * w,
b.m01 * x + b.m11 * y + b.m21 * z + b.m31 * w,
b.m02 * x + b.m12 * y + b.m22 * z + b.m32 * w,
b.m03 * x + b.m13 * y + b.m23 * z + b.m33 * w);
}
/// <summary>
/// Adds two matrices together.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>sum</returns>
public static Mat4 operator +(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 + b.m00, a.m01 + b.m01, a.m02 + b.m02, a.m03 + b.m03,
a.m10 + b.m10, a.m11 + b.m11, a.m12 + b.m12, a.m13 + b.m13,
a.m20 + b.m20, a.m21 + b.m21, a.m22 + b.m22, a.m23 + b.m23,
a.m30 + b.m30, a.m31 + b.m31, a.m32 + b.m32, a.m33 + b.m33);
}
/// <summary>
/// Subtracts the right matrix from the left matrix.
/// </summary>
/// <param name="a">left operand</param>
/// <param name="b">right operand</param>
/// <returns>result</returns>
public static Mat4 operator -(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 - b.m00, a.m01 - b.m01, a.m02 - b.m02, a.m03 - b.m03,
a.m10 - b.m10, a.m11 - b.m11, a.m12 - b.m12, a.m13 - b.m13,
a.m20 - b.m20, a.m21 - b.m21, a.m22 - b.m22, a.m23 - b.m23,
a.m30 - b.m30, a.m31 - b.m31, a.m32 - b.m32, a.m33 - b.m33);
}
/// <summary>
/// Evaluates whether the left operand is less than the right operand.
///
/// The return type is not a boolean, but a matrix, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Mat4 operator <(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 < b.m00, a.m01 < b.m01, a.m02 < b.m02, a.m03 < b.m03,
a.m10 < b.m10, a.m11 < b.m11, a.m12 < b.m12, a.m13 < b.m13,
a.m20 < b.m20, a.m21 < b.m21, a.m22 < b.m22, a.m23 < b.m23,
a.m30 < b.m30, a.m31 < b.m31, a.m32 < b.m32, a.m33 < b.m33);
}
/// <summary>
/// Evaluates whether the left operand is greater than the right operand.
///
/// The return type is not a boolean, but a matrix, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Mat4 operator >(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 > b.m00, a.m01 > b.m01, a.m02 > b.m02, a.m03 > b.m03,
a.m10 > b.m10, a.m11 > b.m11, a.m12 > b.m12, a.m13 > b.m13,
a.m20 > b.m20, a.m21 > b.m21, a.m22 > b.m22, a.m23 > b.m23,
a.m30 > b.m30, a.m31 > b.m31, a.m32 > b.m32, a.m33 > b.m33);
}
/// <summary>
/// Evaluates whether the left operand is less than or equal to the right
/// operand.
///
/// The return type is not a boolean, but a matrix, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Mat4 operator <=(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 <= b.m00, a.m01 <= b.m01, a.m02 <= b.m02, a.m03 <= b.m03,
a.m10 <= b.m10, a.m11 <= b.m11, a.m12 <= b.m12, a.m13 <= b.m13,
a.m20 <= b.m20, a.m21 <= b.m21, a.m22 <= b.m22, a.m23 <= b.m23,
a.m30 <= b.m30, a.m31 <= b.m31, a.m32 <= b.m32, a.m33 <= b.m33);
}
/// <summary>
/// Evaluates whether the left operand is greater than or equal to the right
/// operand.
///
/// The return type is not a boolean, but a matrix, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Mat4 operator >=(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 >= b.m00, a.m01 >= b.m01, a.m02 >= b.m02, a.m03 >= b.m03,
a.m10 >= b.m10, a.m11 >= b.m11, a.m12 >= b.m12, a.m13 >= b.m13,
a.m20 >= b.m20, a.m21 >= b.m21, a.m22 >= b.m22, a.m23 >= b.m23,
a.m30 >= b.m30, a.m31 >= b.m31, a.m32 >= b.m32, a.m33 >= b.m33);
}
/// <summary>
/// Evaluates whether two matrices do not equal to each other.
///
/// The return type is not a boolean, but a matrix, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Mat4 operator !=(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 != b.m00, a.m01 != b.m01, a.m02 != b.m02, a.m03 != b.m03,
a.m10 != b.m10, a.m11 != b.m11, a.m12 != b.m12, a.m13 != b.m13,
a.m20 != b.m20, a.m21 != b.m21, a.m22 != b.m22, a.m23 != b.m23,
a.m30 != b.m30, a.m31 != b.m31, a.m32 != b.m32, a.m33 != b.m33);
}
/// <summary>
/// Evaluates whether two matrices are equal to each other.
///
/// The return type is not a boolean, but a matrix, where 1.0 is true and
/// 0.0 is false.
/// </summary>
/// <param name="a">left comparisand</param>
/// <param name="b">right comparisand</param>
/// <returns>evaluation</returns>
public static Mat4 operator ==(in Mat4 a, in Mat4 b)
{
return new Mat4(
a.m00 == b.m00, a.m01 == b.m01, a.m02 == b.m02, a.m03 == b.m03,
a.m10 == b.m10, a.m11 == b.m11, a.m12 == b.m12, a.m13 == b.m13,
a.m20 == b.m20, a.m21 == b.m21, a.m22 == b.m22, a.m23 == b.m23,
a.m30 == b.m30, a.m31 == b.m31, a.m32 == b.m32, a.m33 == b.m33);
}
/// <summary>
/// Evaluates whether all elements of a matrix are not equal to zero.
/// </summary>
/// <param name="m">matrix</param>
/// <returns>evaluation</returns>
public static bool All(in Mat4 m)
{
return (m.m00 != 0.0f) && (m.m01 != 0.0f) && (m.m02 != 0.0f) && (m.m03 != 0.0f) &&
(m.m10 != 0.0f) && (m.m11 != 0.0f) && (m.m12 != 0.0f) && (m.m13 != 0.0f) &&
(m.m20 != 0.0f) && (m.m21 != 0.0f) && (m.m22 != 0.0f) && (m.m23 != 0.0f) &&
(m.m30 != 0.0f) && (m.m31 != 0.0f) && (m.m32 != 0.0f) && (m.m33 != 0.0f);
}
/// <summary>
/// Evaluates whether any elements of a matrix are not equal to zero.
/// </summary>
/// <param name="m">matrix</param>
/// <returns>evaluation</returns>
public static bool Any(in Mat4 m)
{
return (m.m00 != 0.0f) || (m.m01 != 0.0f) || (m.m02 != 0.0f) || (m.m03 != 0.0f) ||
(m.m10 != 0.0f) || (m.m11 != 0.0f) || (m.m12 != 0.0f) || (m.m13 != 0.0f) ||
(m.m20 != 0.0f) || (m.m21 != 0.0f) || (m.m22 != 0.0f) || (m.m23 != 0.0f) ||
(m.m30 != 0.0f) || (m.m31 != 0.0f) || (m.m32 != 0.0f) || (m.m33 != 0.0f);
}
/// <summary>
/// Finds the determinant of the matrix.
/// </summary>
/// <param name="m">matrix</param>
/// <returns>determinant</returns>
public static float Determinant(in Mat4 m)
{
return m.m00 * (m.m11 * m.m22 * m.m33 +
m.m12 * m.m23 * m.m31 +
m.m13 * m.m21 * m.m32 -
m.m13 * m.m22 * m.m31 -
m.m11 * m.m23 * m.m32 -
m.m12 * m.m21 * m.m33) -
m.m01 * (m.m10 * m.m22 * m.m33 +
m.m12 * m.m23 * m.m30 +
m.m13 * m.m20 * m.m32 -
m.m13 * m.m22 * m.m30 -
m.m10 * m.m23 * m.m32 -
m.m12 * m.m20 * m.m33) +
m.m02 * (m.m10 * m.m21 * m.m33 +
m.m11 * m.m23 * m.m30 +
m.m13 * m.m20 * m.m31 -
m.m13 * m.m21 * m.m30 -
m.m10 * m.m23 * m.m31 -
m.m11 * m.m20 * m.m33) -
m.m03 * (m.m10 * m.m21 * m.m32 +
m.m11 * m.m22 * m.m30 +
m.m12 * m.m20 * m.m31 -
m.m12 * m.m21 * m.m30 -
m.m10 * m.m22 * m.m31 -
m.m11 * m.m20 * m.m32);
}
/// <summary>
/// Creates a matrix from two axes. The third axis, up, is assumed to be
/// (0.0, 0.0, 1.0, 0.0) . The fourth row and column are assumed to be (0.0,
/// 0.0, 0.0, 1.0) .
/// </summary>
/// <param name="right">right axis</param>
/// <param name="forward">forward axis</param>
/// <returns>matrix</returns>
public static Mat4 FromAxes(
in Vec2 right,
in Vec2 forward)
{
return new Mat4(
right.X, forward.X, 0.0f, 0.0f,
right.Y, forward.Y, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
/// <summary>
/// Creates a matrix from two axes and a translation. The third axis, up, is
/// assumed to be (0.0, 0.0, 1.0, 0.0) . The fourth row, w, is assumed to be
/// (0.0, 0.0, 0.0, 1.0) .
/// </summary>
/// <param name="right">right axis</param>
/// <param name="forward">forward axis</param>
/// <param name="translation">translation</param>
/// <returns>matrix</returns>
public static Mat4 FromAxes(
in Vec2 right,
in Vec2 forward,
in Vec2 translation)
{
return new Mat4(
right.X, forward.X, 0.0f, translation.X,
right.Y, forward.Y, 0.0f, translation.Y,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
/// <summary>
/// Creates a matrix from three axes. The fourth row and column are assumed
/// to be (0.0, 0.0, 0.0, 1.0) .
/// </summary>
/// <param name="right">right axis</param>
/// <param name="forward">forward axis</param>
/// <param name="up">up axis</param>
/// <returns>matrix</returns>
public static Mat4 FromAxes(
in Vec3 right,
in Vec3 forward,
in Vec3 up)
{
return new Mat4(
right.X, forward.X, up.X, 0.0f,
right.Y, forward.Y, up.Y, 0.0f,
right.Z, forward.Z, up.Z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
/// <summary>
/// Creates a matrix from three axes and a translation. The fourth row, w,
/// is assumed to be (0.0, 0.0, 0.0, 1.0) .
/// </summary>
/// <param name="right">right axis</param>
/// <param name="forward">forward axis</param>
/// <param name="up">up axis</param>
/// <param name="translation">translation axis</param>
/// <returns>matrix</returns>
public static Mat4 FromAxes(
in Vec3 right,
in Vec3 forward,
in Vec3 up,
in Vec3 translation)
{
return new Mat4(
right.X, forward.X, up.X, translation.X,
right.Y, forward.Y, up.Y, translation.Y,
right.Z, forward.Z, up.Z, translation.Z,
0.0f, 0.0f, 0.0f, 1.0f);
}
/// <summary>
/// Creates a matrix from three axes. The fourth column, translation, is
/// assumed to be (0.0, 0.0, 0.0, 1.0) .
/// </summary>
/// <param name="right">right axis</param>
/// <param name="forward">forward axis</param>
/// <param name="up">up axis</param>
/// <returns>matrix</returns>
public static Mat4 FromAxes(
in Vec4 right,
in Vec4 forward,
in Vec4 up)
{
return new Mat4(
right.X, forward.X, up.X, 0.0f,
right.Y, forward.Y, up.Y, 0.0f,
right.Z, forward.Z, up.Z, 0.0f,
right.W, forward.W, up.W, 1.0f);
}
/// <summary>
/// Creates a matrix from three axes and a translation.
/// </summary>
/// <param name="right">right axis</param>
/// <param name="forward">forward axis</param>
/// <param name="up">up axis</param>
/// <param name="translation">translation</param>
/// <returns>matrix</returns>
public static Mat4 FromAxes(
in Vec4 right,
in Vec4 forward,
in Vec4 up,
in Vec4 translation)
{
return new Mat4(
right.X, forward.X, up.X, translation.X,
right.Y, forward.Y, up.Y, translation.Y,
right.Z, forward.Z, up.Z, translation.Z,
right.W, forward.W, up.W, translation.W);
}
/// <summary>
/// Creates a reflection matrix from a plane represented
/// by an axis. The vector will be normalized by the function.
/// </summary>
/// <param name="v">axis</param>
/// <returns>matrix</returns>
public static Mat4 FromReflection(in Vec3 v)
{
float mSq = Vec3.MagSq(v);
if (mSq != 0.0f)
{
float mInv = Utils.InvSqrtUnchecked(mSq);
float ax = v.X * mInv;
float ay = v.Y * mInv;