-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatrix.go
2253 lines (1973 loc) · 73 KB
/
matrix.go
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
package glm
import (
"bytes"
"fmt"
"text/tabwriter"
"github.com/EngoEngine/math"
)
// Mat2 represents a column major 2x2 matrix.
type Mat2 [4]float32
// Mat3 represents a column major 3x3 matrix.
type Mat3 [9]float32
// Mat4 represents a column major 4x4 matrix.
type Mat4 [16]float32
// Mat3x4 is a 3 row 4 column matrix.
type Mat3x4 [12]float32
// Mat2x3 is a 2 row 3 column matrix.
type Mat2x3 [6]float32
// RowLen returns the length of a row for this matrix type.
func (Mat2) RowLen() int { return 2 }
// ColLen returns the length of a col for this matrix type.
func (Mat2) ColLen() int { return 2 }
// RowLen returns the length of the row of this matrix type.
func (Mat3) RowLen() int { return 3 }
// ColLen returns the length of the col of this matrix type.
func (Mat3) ColLen() int { return 3 }
// RowLen returns the row length for this matrix type.
func (Mat4) RowLen() int { return 4 }
// ColLen returns the col length for this matrix type.
func (Mat4) ColLen() int { return 4 }
// RowLen returns the row length for this matrix type.
func (Mat3x4) RowLen() int { return 4 }
// ColLen returns the col length for this matrix type.
func (Mat3x4) ColLen() int { return 3 }
// RowLen returns the row length for this matrix type.
func (Mat2x3) RowLen() int { return 3 }
// ColLen returns the col length for this matrix type.
func (Mat2x3) ColLen() int { return 2 }
// String pretty prints the matrix
func (m1 *Mat2) String() string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < m1.ColLen(); i++ {
row := m1.Row(i)
for _, col := range []float32{row[0], row[1]} {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
// String pretty prints the matrix
func (m1 *Mat3) String() string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < m1.ColLen(); i++ {
row := m1.Row(i)
for _, col := range []float32{row[0], row[1], row[2]} {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
// String pretty prints the matrix.
func (m1 *Mat4) String() string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < m1.ColLen(); i++ {
for _, col := range m1.Row(i) {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
// String pretty prints the matrix
func (m1 *Mat3x4) String() string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < m1.ColLen(); i++ {
for _, col := range m1.Row(i) {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
// String pretty prints the matrix
func (m1 *Mat2x3) String() string {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 4, 4, 1, ' ', tabwriter.AlignRight)
for i := 0; i < 2; i++ {
for _, col := range m1.Row(i) {
fmt.Fprintf(w, "%f\t", col)
}
fmt.Fprintln(w, "")
}
w.Flush()
return buf.String()
}
// Mat3 returns the mat3 values in the top-left corner and the rest filled with
// the identity matrix values.
// [m0 m2 0]
// [m1 m3 0]
// [ 0 0 1]
func (m1 *Mat2) Mat3() Mat3 {
return Mat3{
m1[0], m1[1], 0,
m1[2], m1[3], 0,
0, 0, 1,
}
}
// Mat4 returns the mat2 values in the top-left corner and the rest filled with
// the identity matrix values.
// [m0 m2 0 0]
// [m1 m3 0 0]
// [ 0 0 1 0]
// [ 0 0 0 1]
func (m1 *Mat2) Mat4() Mat4 {
return Mat4{
m1[0], m1[1], 0, 0,
m1[2], m1[3], 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
}
}
// Mat2 returns the upper 2x2 matrix.
// [m0 m3 ?]
// [m1 m4 ?]
// [ ? ? ?]
func (m1 *Mat3) Mat2() Mat2 {
return Mat2{
m1[0], m1[1],
m1[3], m1[4],
}
}
// Mat4 returns the mat3 values in the top-left corner and the rest filled with
// the identity matrix values.
// [m0 m3 m6 0]
// [m1 m4 m7 0]
// [m2 m5 m8 0]
// [ 0 0 0 1]
func (m1 *Mat3) Mat4() Mat4 {
return Mat4{
m1[0], m1[1], m1[2], 0,
m1[3], m1[4], m1[5], 0,
m1[6], m1[7], m1[8], 0,
0, 0, 0, 1,
}
}
// Mat2x3 returns the top 2x3 matrix.
// [m0 m3 m6]
// [m1 m4 m7]
// [ ? ? ?]
func (m1 *Mat3) Mat2x3() Mat2x3 {
return Mat2x3{
m1[0], m1[1],
m1[3], m1[4],
m1[6], m1[7],
}
}
// Mat3x4 returns the top 2x3 matrix.
// [m0 m3 m6 0]
// [m1 m4 m7 0]
// [m2 m5 m8 0]
func (m1 *Mat3) Mat3x4() Mat3x4 {
return Mat3x4{
m1[0], m1[1], m1[2],
m1[3], m1[4], m1[5],
m1[6], m1[7], m1[8],
0, 0, 0,
}
}
// Mat2 returns the upper 2x2 matrix.
// [m0 m4 ? ?]
// [m1 m5 ? ?]
// [ ? ? ? ?]
// [ ? ? ? ?]
func (m1 *Mat4) Mat2() Mat2 {
return Mat2{
m1[0], m1[1],
m1[4], m1[5],
}
}
// Mat3 returns returns the upper 3x3 matrix.
// [m0 m4 m8 ?]
// [m1 m5 m9 ?]
// [m2 m6 m10 ?]
// [ ? ? ? ?]
func (m1 *Mat4) Mat3() Mat3 {
return Mat3{
m1[0], m1[1], m1[2],
m1[4], m1[5], m1[6],
m1[8], m1[9], m1[10],
}
}
// Mat3x4 returns the top 3x4 matrix.
// [m0 m4 m7 m10]
// [m1 m5 m8 m11]
// [m2 m6 m9 m12]
// [ ? ? ? ?]
func (m1 *Mat4) Mat3x4() Mat3x4 {
return Mat3x4{
m1[0], m1[1], m1[2],
m1[4], m1[5], m1[6],
m1[8], m1[9], m1[10],
m1[12], m1[13], m1[14],
}
}
// Mat4 returns a mat4 with the last row as [0 0 0 1].
func (m1 *Mat3x4) Mat4() Mat4 {
return Mat4{
m1[0], m1[1], m1[2], 0,
m1[3], m1[4], m1[5], 0,
m1[6], m1[7], m1[8], 0,
m1[9], m1[10], m1[11], 1,
}
}
// Mat4In is a memory friendly version of Mat4.
func (m1 *Mat3x4) Mat4In(m2 *Mat4) {
m2[0], m2[4], m2[8], m2[12] = m1[0], m1[3], m1[6], m1[9]
m2[1], m2[5], m2[9], m2[13] = m1[1], m1[4], m1[7], m1[10]
m2[2], m2[6], m2[10], m2[14] = m1[2], m1[5], m1[8], m1[11]
m2[3], m2[7], m2[11], m2[15] = 0, 0, 0, 1
}
// Mat2 returns a Mat2 with the last row as [0 0 1].
func (m1 *Mat2x3) Mat2() Mat2 {
return Mat2{
m1[0], m1[1],
m1[2], m1[3],
}
}
// Mat3 returns a Mat3 with the last row as [0 0 1].
func (m1 *Mat2x3) Mat3() Mat3 {
return Mat3{
m1[0], m1[1], 0,
m1[2], m1[3], 0,
m1[4], m1[5], 1,
}
}
// Mat3In is a memory friendly version of Mat3.
func (m1 *Mat2x3) Mat3In(m2 *Mat3) {
m2[0], m2[3], m2[6] = m1[0], m1[2], m1[4]
m2[1], m2[4], m2[7] = m1[1], m1[3], m1[5]
m2[2], m2[5], m2[8] = 0, 0, 1
}
// Mat2In is a memory friendly version of Mat2.
func (m1 *Mat2x3) Mat2In(m2 *Mat2) {
m2[0], m2[2] = m1[0], m1[2]
m2[1], m2[3] = m1[1], m1[3]
}
// Ident2 returns the 2x2 identity matrix.
func Ident2() Mat2 { return Mat2{1, 0, 0, 1} }
// Ident3 returns the 3x3 identity matrix.
func Ident3() Mat3 { return Mat3{1, 0, 0, 0, 1, 0, 0, 0, 1} }
// Ident4 returns the 4x4 identity matrix.
func Ident4() Mat4 { return Mat4{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1} }
// Ident3x4 returns the 3x4 fake identity matrix.
func Ident3x4() Mat3x4 { return Mat3x4{1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0} }
// Ident2x3 returns the 2x3 fake identity matrix.
func Ident2x3() Mat2x3 { return Mat2x3{1, 0, 0, 1, 0, 0} }
// Ident sets this matrix to the identity matrix.
func (m1 *Mat2) Ident() { *m1 = Ident2() }
// Ident sets this matrix to the identity matrix.
func (m1 *Mat3) Ident() { *m1 = Ident3() }
// Ident sets this matrix to the identity matrix.
func (m1 *Mat4) Ident() { *m1 = Ident4() }
// Ident sets this matrix to the identity matrix.
func (m1 *Mat2x3) Ident() { *m1 = Ident2x3() }
// Ident sets this matrix to the identity matrix.
func (m1 *Mat3x4) Ident() { *m1 = Ident3x4() }
// At returns the matrix element at the given row and column.
func (m1 *Mat2) At(row, col int) float32 { return m1[col*2+row] }
// Set sets the corresponding matrix element at the given row and column.
func (m1 *Mat2) Set(row, col int, value float32) { m1[col*2+row] = value }
// Index returns the index of the given row and column. Used to directly access
// the array.
func (Mat2) Index(row, col int) int { return col*2 + row }
// At returns the matrix element at the given row and column.
func (m1 *Mat3) At(row, col int) float32 { return m1[col*3+row] }
// Set sets the corresponding matrix element at the given row and column.
func (m1 *Mat3) Set(row, col int, value float32) { m1[col*3+row] = value }
// Index returns the index of the given row and column. Used to directly access
// the array.
func (Mat3) Index(row, col int) int { return col*3 + row }
// At returns the matrix element at the given row and column.
func (m1 *Mat4) At(row, col int) float32 { return m1[col*4+row] }
// Set sets the corresponding matrix element at the given row and column.
func (m1 *Mat4) Set(row, col int, value float32) { m1[col*4+row] = value }
// Index returns the index of the given row and column. Used to directly access
// the array.
func (Mat4) Index(row, col int) int { return col*4 + row }
// At returns the matrix element at the given row and column.
func (m1 *Mat3x4) At(row, col int) float32 { return m1[col*3+row] }
// Set sets the corresponding matrix element at the given row and column.
func (m1 *Mat3x4) Set(row, col int, value float32) { m1[col*3+row] = value }
// Index returns the index of the given row and column. Used to directly access
// the array.
func (Mat3x4) Index(row, col int) int { return col*3 + row }
// At returns the matrix element at the given row and column.
func (m1 *Mat2x3) At(row, col int) float32 { return m1[col*2+row] }
// Set sets the corresponding matrix element at the given row and column.
func (m1 *Mat2x3) Set(row, col int, value float32) { m1[col*2+row] = value }
// Index returns the index of the given row and column. Used to directly access
// the array.
func (Mat2x3) Index(row, col int) int { return col*2 + row }
// Equal performs an element-wise approximate equality test between two
// matrices, as if FloatEqual had been used.
func (m1 *Mat2) Equal(m2 *Mat2) bool {
return FloatEqual(m1[0], m2[0]) && FloatEqual(m1[1], m2[1]) && FloatEqual(m1[2], m2[2]) && FloatEqual(m1[3], m2[3])
}
// EqualThreshold performs an element-wise approximate equality test
// between two matrices with a given epsilon threshold, as if
// FloatEqualThreshold had been used.
func (m1 *Mat2) EqualThreshold(m2 *Mat2, threshold float32) bool {
return FloatEqualThreshold(m1[0], m2[0], threshold) && FloatEqualThreshold(m1[1], m2[1], threshold) && FloatEqualThreshold(m1[2], m2[2], threshold) && FloatEqualThreshold(m1[3], m2[3], threshold)
}
// Equal performs an element-wise approximate equality test between two matrices,
// as if FloatEqual had been used.
func (m1 *Mat3) Equal(m2 *Mat3) bool {
return FloatEqual(m1[0], m2[0]) && FloatEqual(m1[1], m2[1]) && FloatEqual(m1[2], m2[2]) && FloatEqual(m1[3], m2[3]) && FloatEqual(m1[4], m2[4]) && FloatEqual(m1[5], m2[5]) && FloatEqual(m1[6], m2[6]) && FloatEqual(m1[7], m2[7]) && FloatEqual(m1[8], m2[8])
}
// EqualThreshold performs an element-wise approximate equality test between two matrices
// with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (m1 *Mat3) EqualThreshold(m2 *Mat3, threshold float32) bool {
return FloatEqualThreshold(m1[0], m2[0], threshold) && FloatEqualThreshold(m1[1], m2[1], threshold) && FloatEqualThreshold(m1[2], m2[2], threshold) && FloatEqualThreshold(m1[3], m2[3], threshold) && FloatEqualThreshold(m1[4], m2[4], threshold) && FloatEqualThreshold(m1[5], m2[5], threshold) && FloatEqualThreshold(m1[6], m2[6], threshold) && FloatEqualThreshold(m1[7], m2[7], threshold) && FloatEqualThreshold(m1[8], m2[8], threshold)
}
// Equal performs an element-wise approximate equality test between two matrices,
// as if FloatEqual had been used.
func (m1 *Mat4) Equal(m2 *Mat4) bool {
return FloatEqual(m1[0], m2[0]) && FloatEqual(m1[1], m2[1]) && FloatEqual(m1[2], m2[2]) && FloatEqual(m1[3], m2[3]) && FloatEqual(m1[4], m2[4]) && FloatEqual(m1[5], m2[5]) && FloatEqual(m1[6], m2[6]) && FloatEqual(m1[7], m2[7]) && FloatEqual(m1[8], m2[8]) && FloatEqual(m1[9], m2[9]) && FloatEqual(m1[10], m2[10]) && FloatEqual(m1[11], m2[11]) && FloatEqual(m1[12], m2[12]) && FloatEqual(m1[13], m2[13]) && FloatEqual(m1[14], m2[14]) && FloatEqual(m1[15], m2[15])
}
// EqualThreshold performs an element-wise approximate equality test between two matrices
// with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (m1 *Mat4) EqualThreshold(m2 *Mat4, threshold float32) bool {
return FloatEqualThreshold(m1[0], m2[0], threshold) && FloatEqualThreshold(m1[1], m2[1], threshold) && FloatEqualThreshold(m1[2], m2[2], threshold) && FloatEqualThreshold(m1[3], m2[3], threshold) && FloatEqualThreshold(m1[4], m2[4], threshold) && FloatEqualThreshold(m1[5], m2[5], threshold) && FloatEqualThreshold(m1[6], m2[6], threshold) && FloatEqualThreshold(m1[7], m2[7], threshold) && FloatEqualThreshold(m1[8], m2[8], threshold) && FloatEqualThreshold(m1[9], m2[9], threshold) && FloatEqualThreshold(m1[10], m2[10], threshold) && FloatEqualThreshold(m1[11], m2[11], threshold) && FloatEqualThreshold(m1[12], m2[12], threshold) && FloatEqualThreshold(m1[13], m2[13], threshold) && FloatEqualThreshold(m1[14], m2[14], threshold) && FloatEqualThreshold(m1[15], m2[15], threshold)
}
// Equal performs an element-wise approximate equality test between two matrices,
// as if FloatEqual had been used.
func (m1 *Mat3x4) Equal(m2 *Mat3x4) bool {
return FloatEqual(m1[0], m2[0]) && FloatEqual(m1[1], m2[1]) && FloatEqual(m1[2], m2[2]) && FloatEqual(m1[3], m2[3]) && FloatEqual(m1[4], m2[4]) && FloatEqual(m1[5], m2[5]) && FloatEqual(m1[6], m2[6]) && FloatEqual(m1[7], m2[7]) && FloatEqual(m1[8], m2[8]) && FloatEqual(m1[9], m2[9]) && FloatEqual(m1[10], m2[10]) && FloatEqual(m1[11], m2[11])
}
// EqualThreshold performs an element-wise approximate equality test between two matrices
// with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (m1 *Mat3x4) EqualThreshold(m2 *Mat3x4, threshold float32) bool {
return FloatEqualThreshold(m1[0], m2[0], threshold) && FloatEqualThreshold(m1[1], m2[1], threshold) && FloatEqualThreshold(m1[2], m2[2], threshold) && FloatEqualThreshold(m1[3], m2[3], threshold) && FloatEqualThreshold(m1[4], m2[4], threshold) && FloatEqualThreshold(m1[5], m2[5], threshold) && FloatEqualThreshold(m1[6], m2[6], threshold) && FloatEqualThreshold(m1[7], m2[7], threshold) && FloatEqualThreshold(m1[8], m2[8], threshold) && FloatEqualThreshold(m1[9], m2[9], threshold) && FloatEqualThreshold(m1[10], m2[10], threshold) && FloatEqualThreshold(m1[11], m2[11], threshold)
}
// Equal performs an element-wise approximate equality test between two matrices,
// as if FloatEqual had been used.
func (m1 *Mat2x3) Equal(m2 *Mat2x3) bool {
return FloatEqual(m1[0], m2[0]) && FloatEqual(m1[1], m2[1]) && FloatEqual(m1[2], m2[2]) && FloatEqual(m1[3], m2[3]) && FloatEqual(m1[4], m2[4]) && FloatEqual(m1[5], m2[5])
}
// EqualThreshold performs an element-wise approximate equality test between two matrices
// with a given epsilon threshold, as if FloatEqualThreshold had been used.
func (m1 *Mat2x3) EqualThreshold(m2 *Mat2x3, threshold float32) bool {
return FloatEqualThreshold(m1[0], m2[0], threshold) && FloatEqualThreshold(m1[1], m2[1], threshold) && FloatEqualThreshold(m1[2], m2[2], threshold) && FloatEqualThreshold(m1[3], m2[3], threshold) && FloatEqualThreshold(m1[4], m2[4], threshold) && FloatEqualThreshold(m1[5], m2[5], threshold)
}
// SetCol sets a Column within the Matrix, so it mutates the calling matrix.
func (m1 *Mat2) SetCol(col int, v *Vec2) {
m1[col*2+0], m1[col*2+1] = v[0], v[1]
}
// SetRow sets a Row within the Matrix, so it mutates the calling matrix.
func (m1 *Mat2) SetRow(row int, v *Vec2) {
m1[row+0], m1[row+2] = v[0], v[1]
}
// Diag is a basic operation on a square matrix that simply
// returns main diagonal (meaning all elements such that row==col).
func (m1 *Mat2) Diag() Vec2 {
return Vec2{m1[0], m1[3]}
}
// Diag2 creates a diagonal matrix from the entries of the input vector.
// That is, for each pointer for row==col, vector[row] is the entry. Otherwise
// it's 0.
func Diag2(v *Vec2) Mat2 {
return Mat2{v[0], 0, 0, v[1]}
}
// Mat2FromRows builds a new matrix from row vectors. The resulting matrix will
// still be in column major order, but this can be good for hand-building
// matrices.
func Mat2FromRows(row0, row1 *Vec2) Mat2 {
return Mat2{row0[0], row1[0], row0[1], row1[1]}
}
// Mat2FromCols builds a new matrix from column vectors.
func Mat2FromCols(col0, col1 *Vec2) Mat2 {
return Mat2{col0[0], col0[1], col1[0], col1[1]}
}
// Add performs an element-wise addition of two matrices, this is equivalent to
// iterating over every element of m1 and adding the corresponding value of m2.
func (m1 *Mat2) Add(m2 *Mat2) Mat2 {
return Mat2{m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2], m1[3] + m2[3]}
}
// AddOf is a memory friendly version of Add.
func (m1 *Mat2) AddOf(m2, m3 *Mat2) {
m1[0] = m2[0] + m3[0]
m1[1] = m2[1] + m3[1]
m1[2] = m2[2] + m3[2]
m1[3] = m2[3] + m3[3]
}
// AddWith is a memory friendly version of Add.
func (m1 *Mat2) AddWith(m2 *Mat2) {
m1[0] += m2[0]
m1[1] += m2[1]
m1[2] += m2[2]
m1[3] += m2[3]
}
// Sub performs an element-wise subtraction of two matrices, this is equivalent
// to iterating over every element of m1 and subtracting the corresponding value
// of m2.
func (m1 *Mat2) Sub(m2 *Mat2) Mat2 {
return Mat2{m1[0] - m2[0], m1[1] - m2[1], m1[2] - m2[2], m1[3] - m2[3]}
}
// SubOf is a memory friendly version of Sub.
func (m1 *Mat2) SubOf(m2, m3 *Mat2) {
m1[0] = m2[0] - m3[0]
m1[1] = m2[1] - m3[1]
m1[2] = m2[2] - m3[2]
m1[3] = m2[3] - m3[3]
}
// SubWith is a memory friendly version of Sub.
func (m1 *Mat2) SubWith(m2 *Mat2) {
m1[0] -= m2[0]
m1[1] -= m2[1]
m1[2] -= m2[2]
m1[3] -= m2[3]
}
// Mul performs a scalar multiplcation of the matrix. This is equivalent to
// iterating over every element of the matrix and multiply it by c.
func (m1 *Mat2) Mul(c float32) Mat2 {
return Mat2{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c}
}
// MulOf is a memory friendly version of Mul.
func (m1 *Mat2) MulOf(m2 *Mat2, c float32) {
m1[0] = m2[0] * c
m1[1] = m2[1] * c
m1[2] = m2[2] * c
m1[3] = m2[3] * c
}
// MulWith is a memory friendly version of Mul.
func (m1 *Mat2) MulWith(c float32) {
m1[0] *= c
m1[1] *= c
m1[2] *= c
m1[3] *= c
}
// Mul2x1 performs a "matrix product" between this matrix and another of the
// given dimension. For any two matrices of dimensionality MxN and NxO, the
// result will be MxO. For instance, Mat4 multiplied using Mul4x2 will result
// in a Mat4x2.
func (m1 *Mat2) Mul2x1(m2 *Vec2) Vec2 {
return Vec2{
m1[0]*m2[0] + m1[2]*m2[1],
m1[1]*m2[0] + m1[3]*m2[1],
}
}
// Mul2 performs a "matrix product" between this matrix and another of the given
// dimension. For any two matrices of dimensionality MxN and NxO, the result
// will be MxO. For instance, Mat4 multiplied using Mul4x2 will result in a
// Mat4x2.
func (m1 *Mat2) Mul2(m2 *Mat2) Mat2 {
return Mat2{
m1[0]*m2[0] + m1[2]*m2[1],
m1[1]*m2[0] + m1[3]*m2[1],
m1[0]*m2[2] + m1[2]*m2[3],
m1[1]*m2[2] + m1[3]*m2[3],
}
}
// Mul2Of is a memory friendly version of Mul2.
func (m1 *Mat2) Mul2Of(m2, m3 *Mat2) {
m1[0] = m2[0]*m3[0] + m2[2]*m3[1]
m1[1] = m2[1]*m3[0] + m2[3]*m3[1]
m1[2] = m2[0]*m3[2] + m2[2]*m3[3]
m1[3] = m2[1]*m3[2] + m2[3]*m3[3]
}
// Mul2With is a memory friendly version of Mul2.
func (m1 *Mat2) Mul2With(m2 *Mat2) {
v0 := m1[0]
v1 := m1[1]
v2 := m1[2]
v3 := m1[3]
m1[0] = v0*m2[0] + v2*m2[1]
m1[1] = v1*m2[0] + v3*m2[1]
m1[2] = v0*m2[2] + v2*m2[3]
m1[3] = v1*m2[2] + v3*m2[3]
}
// Transposed produces the transpose of this matrix. For any MxN matrix the
// transpose is an NxM matrix with the rows swapped with the columns. For
// instance the transpose of the Mat3x2 is a Mat2x3 like so:
// [[a b]] [[a c e]]
// [[c d]] = [[b d f]]
// [[e f]]
func (m1 *Mat2) Transposed() Mat2 {
return Mat2{m1[0], m1[2], m1[1], m1[3]}
}
// Transpose transpose this matrix with itself as destination. For any MxN
// matrix the transpose is an NxM matrix with the rows swapped with the columns.
// For instance the transpose of the Mat3x2 is a Mat2x3 like so:
// [[a b]] [[a c e]]
// [[c d]] = [[b d f]]
// [[e f]]
func (m1 *Mat2) Transpose() {
m1[1], m1[2] = m1[2], m1[1]
}
//TransposeOf is a memory friendly version of Transposed.
func (m1 *Mat2) TransposeOf(m2 *Mat2) {
m1[0], m1[1], m1[2], m1[3] = m2[0], m2[2], m2[1], m2[3]
}
// Det returns the determinant of a matrix. The determinant is a measure of a
// square matrix's singularity and invertability, among other things. In this
// library, the determinant is hard coded based on pre-computed cofactor
// expansion, and uses no loops. Of course, the addition and multiplication must
// still be done.
func (m1 *Mat2) Det() float32 {
return m1[0]*m1[3] - m1[1]*m1[2]
}
// Inverse computes the inverse of a square matrix. An inverse is a square
// matrix such that when multiplied by the original, yields the identity. Return
// the zero matrix if the determinant is zero.
func (m1 *Mat2) Inverse() Mat2 {
det := m1.Det()
if FloatEqual(det, 0) {
return Mat2{}
}
over := 1 / det
return Mat2{m1[3] * over, -m1[1] * over, -m1[2] * over, m1[0] * over}
}
// Invert is the same as Inverse but it acts on the caller.
func (m1 *Mat2) Invert() {
det := m1.Det()
if FloatEqual(det, 0) {
*m1 = Mat2{}
return
}
over := 1 / det
*m1 = Mat2{m1[3] * over, -m1[1] * over, -m1[2] * over, m1[0] * over}
}
// InverseOf sets m1 to the inverse of m2.
func (m1 *Mat2) InverseOf(m2 *Mat2) {
det := m2.Det()
if FloatEqual(det, 0) {
*m1 = Mat2{}
return
}
over := 1 / det
*m1 = Mat2{m2[3] * over, -m2[1] * over, -m2[2] * over, m2[0] * over}
}
// Row returns a vector representing the corresponding row (starting at row 0).
// This package makes no distinction between row and column vectors, so it will
// be a normal VecM for a MxN matrix.
func (m1 *Mat2) Row(row int) Vec2 {
return Vec2{m1[row+0], m1[row+2]}
}
// Rows decomposes a matrix into its corresponding row vectors. This is
// equivalent to calling mat.Row for each row.
func (m1 *Mat2) Rows() (row0, row1 Vec2) {
return m1.Row(0), m1.Row(1)
}
// Col returns a vector representing the corresponding column (starting at col
// 0). This package makes no distinction between row and column vectors, so it
// will be a normal VecN for a MxN matrix.
func (m1 *Mat2) Col(col int) Vec2 {
return Vec2{m1[col*2+0], m1[col*2+1]}
}
// Cols decomposes a matrix into its corresponding column vectors.
// This is equivalent to calling mat.Col for each column.
func (m1 *Mat2) Cols() (col0, col1 Vec2) {
return m1.Col(0), m1.Col(1)
}
// Trace is a basic operation on a square matrix that simply sums up all
// elements on the main diagonal (meaning all elements such that row == col).
func (m1 *Mat2) Trace() float32 {
return m1[0] + m1[3]
}
// Abs returns the element-wise absolute value of this matrix
func (m1 *Mat2) Abs() Mat2 {
return Mat2{math.Abs(m1[0]), math.Abs(m1[1]), math.Abs(m1[2]), math.Abs(m1[3])}
}
// AbsSelf is a memory friendly version of Abs.
func (m1 *Mat2) AbsSelf() {
m1[0] = math.Abs(m1[0])
m1[1] = math.Abs(m1[1])
m1[2] = math.Abs(m1[2])
m1[3] = math.Abs(m1[3])
}
// AbsOf is a memory friendly version of Abs.
func (m1 *Mat2) AbsOf(m2 *Mat2) {
m1[0] = math.Abs(m2[0])
m1[1] = math.Abs(m2[1])
m1[2] = math.Abs(m2[2])
m1[3] = math.Abs(m2[3])
}
// SetCol sets a column within the matrix.
func (m1 *Mat3) SetCol(col int, v *Vec3) {
m1[col*3+0], m1[col*3+1], m1[col*3+2] = v[0], v[1], v[2]
}
// SetRow sets a row within the matrix.
func (m1 *Mat3) SetRow(row int, v *Vec3) {
m1[row+0], m1[row+3], m1[row+6] = v[0], v[1], v[2]
}
// Diag is a basic operation on a square matrix that simply
// returns main diagonal (meaning all elements such that row==col).
func (m1 *Mat3) Diag() Vec3 {
return Vec3{m1[0], m1[4], m1[8]}
}
// Diag3 creates a diagonal matrix from the entries of the input vector.
// That is, for each pointer for row==col, vector[row] is the entry. Otherwise it's 0.
//
// Another way to think about it is that the identity is this function where the every vector element is 1.
func Diag3(v *Vec3) Mat3 {
return Mat3{v[0], 0, 0, 0, v[1], 0, 0, 0, v[2]}
}
// Mat3FromRows builds a new matrix from row vectors.
// The resulting matrix will still be in column major order, but this can be
// good for hand-building matrices.
func Mat3FromRows(row0, row1, row2 *Vec3) Mat3 {
return Mat3{row0[0], row1[0], row2[0], row0[1], row1[1], row2[1], row0[2], row1[2], row2[2]}
}
// Mat3FromCols builds a new matrix from column vectors.
func Mat3FromCols(col0, col1, col2 *Vec3) Mat3 {
return Mat3{col0[0], col0[1], col0[2], col1[0], col1[1], col1[2], col2[0], col2[1], col2[2]}
}
// Add performs an element-wise addition of two matrices, this is
// equivalent to iterating over every element of m1 and adding the corresponding value of m2.
func (m1 *Mat3) Add(m2 *Mat3) Mat3 {
return Mat3{m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2], m1[3] + m2[3], m1[4] + m2[4], m1[5] + m2[5], m1[6] + m2[6], m1[7] + m2[7], m1[8] + m2[8]}
}
// AddOf is a memory friendly version of Add.
func (m1 *Mat3) AddOf(m2, m3 *Mat3) {
m1[0] = m2[0] + m3[0]
m1[1] = m2[1] + m3[1]
m1[2] = m2[2] + m3[2]
m1[3] = m2[3] + m3[3]
m1[4] = m2[4] + m3[4]
m1[5] = m2[5] + m3[5]
m1[6] = m2[6] + m3[6]
m1[7] = m2[7] + m3[7]
m1[8] = m2[8] + m3[8]
}
// AddWith is a memory friendly version of Add.
func (m1 *Mat3) AddWith(m2 *Mat3) {
m1[0] += m2[0]
m1[1] += m2[1]
m1[2] += m2[2]
m1[3] += m2[3]
m1[4] += m2[4]
m1[5] += m2[5]
m1[6] += m2[6]
m1[7] += m2[7]
m1[8] += m2[8]
}
// Sub performs an element-wise subtraction of two matrices, this is
// equivalent to iterating over every element of m1 and subtracting the corresponding value of m2.
func (m1 *Mat3) Sub(m2 *Mat3) Mat3 {
return Mat3{m1[0] - m2[0], m1[1] - m2[1], m1[2] - m2[2], m1[3] - m2[3], m1[4] - m2[4], m1[5] - m2[5], m1[6] - m2[6], m1[7] - m2[7], m1[8] - m2[8]}
}
// SubOf is a memory friendly version of Sub.
func (m1 *Mat3) SubOf(m2, m3 *Mat3) {
m1[0] = m2[0] - m3[0]
m1[1] = m2[1] - m3[1]
m1[2] = m2[2] - m3[2]
m1[3] = m2[3] - m3[3]
m1[4] = m2[4] - m3[4]
m1[5] = m2[5] - m3[5]
m1[6] = m2[6] - m3[6]
m1[7] = m2[7] - m3[7]
m1[8] = m2[8] - m3[8]
}
// SubWith is a memory friendly version of Sub.
func (m1 *Mat3) SubWith(m2 *Mat3) {
m1[0] -= m2[0]
m1[1] -= m2[1]
m1[2] -= m2[2]
m1[3] -= m2[3]
m1[4] -= m2[4]
m1[5] -= m2[5]
m1[6] -= m2[6]
m1[7] -= m2[7]
m1[8] -= m2[8]
}
// Mul performs a scalar multiplcation of the matrix. This is equivalent to iterating
// over every element of the matrix and multiply it by c.
func (m1 *Mat3) Mul(c float32) Mat3 {
return Mat3{m1[0] * c, m1[1] * c, m1[2] * c, m1[3] * c, m1[4] * c, m1[5] * c, m1[6] * c, m1[7] * c, m1[8] * c}
}
// MulOf is a memory friendly version fo Mul.
func (m1 *Mat3) MulOf(m2 *Mat3, c float32) {
m1[0] = m2[0] * c
m1[1] = m2[1] * c
m1[2] = m2[2] * c
m1[3] = m2[3] * c
m1[4] = m2[4] * c
m1[5] = m2[5] * c
m1[6] = m2[6] * c
m1[7] = m2[7] * c
m1[8] = m2[8] * c
}
// MulWith is a memory friendly version fo Mul.
func (m1 *Mat3) MulWith(c float32) {
m1[0] *= c
m1[1] *= c
m1[2] *= c
m1[3] *= c
m1[4] *= c
m1[5] *= c
m1[6] *= c
m1[7] *= c
m1[8] *= c
}
// Mul3x1 performs a matrix product between this matrix
// and another of the given dimension. For any two matrices of dimensionality
// MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using
// Mul4x2 will result in a Mat4x2.
func (m1 *Mat3) Mul3x1(m2 *Vec3) Vec3 {
return Vec3{
m1[0]*m2[0] + m1[3]*m2[1] + m1[6]*m2[2],
m1[1]*m2[0] + m1[4]*m2[1] + m1[7]*m2[2],
m1[2]*m2[0] + m1[5]*m2[1] + m1[8]*m2[2],
}
}
// Mul3x1Transpose is the same as Mul3x1 except it uses the inplace transpose of
// this matrix.
func (m1 *Mat3) Mul3x1Transpose(v *Vec3) Vec3 {
return Vec3{
m1[0]*v[0] + m1[1]*v[1] + m1[2]*v[2],
m1[3]*v[0] + m1[4]*v[1] + m1[5]*v[2],
m1[6]*v[0] + m1[7]*v[1] + m1[8]*v[2],
}
}
// Mul3x1In is a memory friendly version of Mul3x1
func (m1 *Mat3) Mul3x1In(m2, dst *Vec3) {
dst[0] = m1[0]*m2[0] + m1[3]*m2[1] + m1[6]*m2[2]
dst[1] = m1[1]*m2[0] + m1[4]*m2[1] + m1[7]*m2[2]
dst[2] = m1[2]*m2[0] + m1[5]*m2[1] + m1[8]*m2[2]
}
// Mul3 performs a "matrix product" between this matrix
// and another of the given dimension. For any two matrices of dimensionality
// MxN and NxO, the result will be MxO. For instance, Mat4 multiplied using
// Mul4x2 will result in a Mat4x2.
func (m1 *Mat3) Mul3(m2 *Mat3) Mat3 {
return Mat3{
m1[0]*m2[0] + m1[3]*m2[1] + m1[6]*m2[2],
m1[1]*m2[0] + m1[4]*m2[1] + m1[7]*m2[2],
m1[2]*m2[0] + m1[5]*m2[1] + m1[8]*m2[2],
m1[0]*m2[3] + m1[3]*m2[4] + m1[6]*m2[5],
m1[1]*m2[3] + m1[4]*m2[4] + m1[7]*m2[5],
m1[2]*m2[3] + m1[5]*m2[4] + m1[8]*m2[5],
m1[0]*m2[6] + m1[3]*m2[7] + m1[6]*m2[8],
m1[1]*m2[6] + m1[4]*m2[7] + m1[7]*m2[8],
m1[2]*m2[6] + m1[5]*m2[7] + m1[8]*m2[8],
}
}
// Mul3Of is a memory friendly version of Mul3.
func (m1 *Mat3) Mul3Of(m2, m3 *Mat3) {
m1[0] = m2[0]*m3[0] + m2[3]*m3[1] + m2[6]*m3[2]
m1[1] = m2[1]*m3[0] + m2[4]*m3[1] + m2[7]*m3[2]
m1[2] = m2[2]*m3[0] + m2[5]*m3[1] + m2[8]*m3[2]
m1[3] = m2[0]*m3[3] + m2[3]*m3[4] + m2[6]*m3[5]
m1[4] = m2[1]*m3[3] + m2[4]*m3[4] + m2[7]*m3[5]
m1[5] = m2[2]*m3[3] + m2[5]*m3[4] + m2[8]*m3[5]
m1[6] = m2[0]*m3[6] + m2[3]*m3[7] + m2[6]*m3[8]
m1[7] = m2[1]*m3[6] + m2[4]*m3[7] + m2[7]*m3[8]
m1[8] = m2[2]*m3[6] + m2[5]*m3[7] + m2[8]*m3[8]
}
// Mul3With is a memory friendly version of Mul3.
func (m1 *Mat3) Mul3With(m2 *Mat3) {
v0 := m1[0]
v1 := m1[1]
v2 := m1[2]
v3 := m1[3]
v4 := m1[4]
v5 := m1[5]
v6 := m1[6]
v7 := m1[7]
v8 := m1[8]
m1[0] = v0*m2[0] + v3*m2[1] + v6*m2[2]
m1[1] = v1*m2[0] + v4*m2[1] + v7*m2[2]
m1[2] = v2*m2[0] + v5*m2[1] + v8*m2[2]
m1[3] = v0*m2[3] + v3*m2[4] + v6*m2[5]
m1[4] = v1*m2[3] + v4*m2[4] + v7*m2[5]
m1[5] = v2*m2[3] + v5*m2[4] + v8*m2[5]
m1[6] = v0*m2[6] + v3*m2[7] + v6*m2[8]
m1[7] = v1*m2[6] + v4*m2[7] + v7*m2[8]
m1[8] = v2*m2[6] + v5*m2[7] + v8*m2[8]
}
// Transposed produces the transpose of this matrix. For any MxN matrix
// the transpose is an NxM matrix with the rows swapped with the columns. For instance
// the transpose of the Mat3x2 is a Mat2x3 like so:
//
// [[a b]] [[a c e]]
// [[c d]] = [[b d f]]
// [[e f]]
func (m1 *Mat3) Transposed() Mat3 {
return Mat3{m1[0], m1[3], m1[6], m1[1], m1[4], m1[7], m1[2], m1[5], m1[8]}
}
// Transpose is a memory friendly version of Transposed.
func (m1 *Mat3) Transpose() {
m1[1], m1[2], m1[3], m1[5], m1[6], m1[7] = m1[3], m1[6], m1[1], m1[7], m1[2], m1[5]
}
// TransposeOf is a memory friendly version of Transposed.
func (m1 *Mat3) TransposeOf(m2 *Mat3) {
m1[0] = m2[0]
m1[1] = m2[3]
m1[2] = m2[6]
m1[3] = m2[1]
m1[4] = m2[4]
m1[5] = m2[7]
m1[6] = m2[2]
m1[7] = m2[5]
m1[8] = m2[8]
}
// Det returns the determinant of a matrix. The determinant is a measure of a square matrix's
// singularity and invertability, among other things. In this library, the
// determinant is hard coded based on pre-computed cofactor expansion, and uses
// no loops. Of course, the addition and multiplication must still be done.
func (m1 *Mat3) Det() float32 {
return m1[0]*m1[4]*m1[8] + m1[3]*m1[7]*m1[2] + m1[6]*m1[1]*m1[5] -
m1[6]*m1[4]*m1[2] - m1[3]*m1[1]*m1[8] - m1[0]*m1[7]*m1[5]
}
// Inverse computes the inverse of a square matrix. An inverse is a square
// matrix such that when multiplied by the original, yields the identity. Return
// the zero matrix if the determinant is zero.
func (m1 *Mat3) Inverse() Mat3 {
det := m1.Det()
if FloatEqual(det, float32(0.0)) {
return Mat3{}
}
retMat := Mat3{
m1[4]*m1[8] - m1[5]*m1[7],
m1[2]*m1[7] - m1[1]*m1[8],
m1[1]*m1[5] - m1[2]*m1[4],
m1[5]*m1[6] - m1[3]*m1[8],
m1[0]*m1[8] - m1[2]*m1[6],
m1[2]*m1[3] - m1[0]*m1[5],
m1[3]*m1[7] - m1[4]*m1[6],
m1[1]*m1[6] - m1[0]*m1[7],
m1[0]*m1[4] - m1[1]*m1[3],
}
return retMat.Mul(1 / det)
}
// Invert is a memory friendly version of Inverse.
func (m1 *Mat3) Invert() {
det := m1.Det()
if FloatEqual(det, float32(0.0)) {
*m1 = Mat3{}
return
}
v0 := m1[0]
v1 := m1[1]
v2 := m1[2]
v3 := m1[3]
v4 := m1[4]