-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathmatrix4.dart
2069 lines (1918 loc) · 64.6 KB
/
matrix4.dart
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
// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
part of '../../vector_math_64.dart';
/// 4D Matrix.
/// Values are stored in column major order.
class Matrix4 {
final Float64List _m4storage;
/// The components of the matrix.
Float64List get storage => _m4storage;
/// Solve [A] * [x] = [b].
static void solve2(Matrix4 A, Vector2 x, Vector2 b) {
final a11 = A.entry(0, 0);
final a12 = A.entry(0, 1);
final a21 = A.entry(1, 0);
final a22 = A.entry(1, 1);
final bx = b.x - A._m4storage[8];
final by = b.y - A._m4storage[9];
var det = a11 * a22 - a12 * a21;
if (det != 0.0) {
det = 1.0 / det;
}
x
..x = det * (a22 * bx - a12 * by)
..y = det * (a11 * by - a21 * bx);
}
/// Solve [A] * [x] = [b].
static void solve3(Matrix4 A, Vector3 x, Vector3 b) {
final A0x = A.entry(0, 0);
final A0y = A.entry(1, 0);
final A0z = A.entry(2, 0);
final A1x = A.entry(0, 1);
final A1y = A.entry(1, 1);
final A1z = A.entry(2, 1);
final A2x = A.entry(0, 2);
final A2y = A.entry(1, 2);
final A2z = A.entry(2, 2);
final bx = b.x - A._m4storage[12];
final by = b.y - A._m4storage[13];
final bz = b.z - A._m4storage[14];
double rx, ry, rz;
double det;
// Column1 cross Column 2
rx = A1y * A2z - A1z * A2y;
ry = A1z * A2x - A1x * A2z;
rz = A1x * A2y - A1y * A2x;
// A.getColumn(0).dot(x)
det = A0x * rx + A0y * ry + A0z * rz;
if (det != 0.0) {
det = 1.0 / det;
}
// b dot [Column1 cross Column 2]
final x_ = det * (bx * rx + by * ry + bz * rz);
// Column2 cross b
rx = -(A2y * bz - A2z * by);
ry = -(A2z * bx - A2x * bz);
rz = -(A2x * by - A2y * bx);
// Column0 dot -[Column2 cross b (Column3)]
final y_ = det * (A0x * rx + A0y * ry + A0z * rz);
// b cross Column 1
rx = -(by * A1z - bz * A1y);
ry = -(bz * A1x - bx * A1z);
rz = -(bx * A1y - by * A1x);
// Column0 dot -[b cross Column 1]
final z_ = det * (A0x * rx + A0y * ry + A0z * rz);
x
..x = x_
..y = y_
..z = z_;
}
/// Solve [A] * [x] = [b].
static void solve(Matrix4 A, Vector4 x, Vector4 b) {
final a00 = A._m4storage[0];
final a01 = A._m4storage[1];
final a02 = A._m4storage[2];
final a03 = A._m4storage[3];
final a10 = A._m4storage[4];
final a11 = A._m4storage[5];
final a12 = A._m4storage[6];
final a13 = A._m4storage[7];
final a20 = A._m4storage[8];
final a21 = A._m4storage[9];
final a22 = A._m4storage[10];
final a23 = A._m4storage[11];
final a30 = A._m4storage[12];
final a31 = A._m4storage[13];
final a32 = A._m4storage[14];
final a33 = A._m4storage[15];
final b00 = a00 * a11 - a01 * a10;
final b01 = a00 * a12 - a02 * a10;
final b02 = a00 * a13 - a03 * a10;
final b03 = a01 * a12 - a02 * a11;
final b04 = a01 * a13 - a03 * a11;
final b05 = a02 * a13 - a03 * a12;
final b06 = a20 * a31 - a21 * a30;
final b07 = a20 * a32 - a22 * a30;
final b08 = a20 * a33 - a23 * a30;
final b09 = a21 * a32 - a22 * a31;
final b10 = a21 * a33 - a23 * a31;
final b11 = a22 * a33 - a23 * a32;
final bX = b.storage[0];
final bY = b.storage[1];
final bZ = b.storage[2];
final bW = b.storage[3];
var det =
b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
if (det != 0.0) {
det = 1.0 / det;
}
x
..x = det *
((a11 * b11 - a12 * b10 + a13 * b09) * bX -
(a10 * b11 - a12 * b08 + a13 * b07) * bY +
(a10 * b10 - a11 * b08 + a13 * b06) * bZ -
(a10 * b09 - a11 * b07 + a12 * b06) * bW)
..y = det *
-((a01 * b11 - a02 * b10 + a03 * b09) * bX -
(a00 * b11 - a02 * b08 + a03 * b07) * bY +
(a00 * b10 - a01 * b08 + a03 * b06) * bZ -
(a00 * b09 - a01 * b07 + a02 * b06) * bW)
..z = det *
((a31 * b05 - a32 * b04 + a33 * b03) * bX -
(a30 * b05 - a32 * b02 + a33 * b01) * bY +
(a30 * b04 - a31 * b02 + a33 * b00) * bZ -
(a30 * b03 - a31 * b01 + a32 * b00) * bW)
..w = det *
-((a21 * b05 - a22 * b04 + a23 * b03) * bX -
(a20 * b05 - a22 * b02 + a23 * b01) * bY +
(a20 * b04 - a21 * b02 + a23 * b00) * bZ -
(a20 * b03 - a21 * b01 + a22 * b00) * bW);
}
/// Returns a matrix that is the inverse of [other] if [other] is invertible,
/// otherwise `null`.
static Matrix4? tryInvert(Matrix4 other) {
final r = Matrix4.zero();
final determinant = r.copyInverse(other);
if (determinant == 0.0) {
return null;
}
return r;
}
/// Return index in storage for [row], [col] value.
int index(int row, int col) => (col * 4) + row;
/// Value at [row], [col].
double entry(int row, int col) {
assert((row >= 0) && (row < dimension));
assert((col >= 0) && (col < dimension));
return _m4storage[index(row, col)];
}
/// Set value at [row], [col] to be [v].
void setEntry(int row, int col, double v) {
assert((row >= 0) && (row < dimension));
assert((col >= 0) && (col < dimension));
_m4storage[index(row, col)] = v;
}
/// Constructs a new mat4.
factory Matrix4(
double arg0,
double arg1,
double arg2,
double arg3,
double arg4,
double arg5,
double arg6,
double arg7,
double arg8,
double arg9,
double arg10,
double arg11,
double arg12,
double arg13,
double arg14,
double arg15) =>
Matrix4.zero()
..setValues(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9,
arg10, arg11, arg12, arg13, arg14, arg15);
/// New matrix from [values].
factory Matrix4.fromList(List<double> values) => Matrix4.zero()
..setValues(
values[0],
values[1],
values[2],
values[3],
values[4],
values[5],
values[6],
values[7],
values[8],
values[9],
values[10],
values[11],
values[12],
values[13],
values[14],
values[15]);
/// Zero matrix.
Matrix4.zero() : _m4storage = Float64List(16);
/// Identity matrix.
factory Matrix4.identity() => Matrix4.zero()..setIdentity();
/// Copies values from [other].
factory Matrix4.copy(Matrix4 other) => Matrix4.zero()..setFrom(other);
/// Constructs a matrix that is the inverse of [other].
factory Matrix4.inverted(Matrix4 other) {
final r = Matrix4.zero();
final determinant = r.copyInverse(other);
if (determinant == 0.0) {
throw ArgumentError.value(other, 'other', 'Matrix cannot be inverted');
}
return r;
}
/// Constructs a new mat4 from columns.
factory Matrix4.columns(
Vector4 arg0, Vector4 arg1, Vector4 arg2, Vector4 arg3) =>
Matrix4.zero()..setColumns(arg0, arg1, arg2, arg3);
/// Outer product of [u] and [v].
factory Matrix4.outer(Vector4 u, Vector4 v) => Matrix4.zero()..setOuter(u, v);
/// Rotation of [radians] around X.
factory Matrix4.rotationX(double radians) => Matrix4.zero()
.._m4storage[15] = 1.0
..setRotationX(radians);
/// Rotation of [radians] around Y.
factory Matrix4.rotationY(double radians) => Matrix4.zero()
.._m4storage[15] = 1.0
..setRotationY(radians);
/// Rotation of [radians] around Z.
factory Matrix4.rotationZ(double radians) => Matrix4.zero()
.._m4storage[15] = 1.0
..setRotationZ(radians);
/// Translation matrix.
factory Matrix4.translation(Vector3 translation) => Matrix4.zero()
..setIdentity()
..setTranslation(translation);
/// Translation matrix.
factory Matrix4.translationValues(double x, double y, double z) =>
Matrix4.zero()
..setIdentity()
..setTranslationRaw(x, y, z);
/// Scale matrix.
factory Matrix4.diagonal3(Vector3 scale) {
final m = Matrix4.zero();
final mStorage = m._m4storage;
final scaleStorage = scale._v3storage;
mStorage[15] = 1.0;
mStorage[10] = scaleStorage[2];
mStorage[5] = scaleStorage[1];
mStorage[0] = scaleStorage[0];
return m;
}
/// Scale matrix.
factory Matrix4.diagonal3Values(double x, double y, double z) =>
Matrix4.zero()
.._m4storage[15] = 1.0
.._m4storage[10] = z
.._m4storage[5] = y
.._m4storage[0] = x;
/// Skew matrix around X axis
factory Matrix4.skewX(double alpha) {
final m = Matrix4.identity();
m._m4storage[4] = math.tan(alpha);
return m;
}
/// Skew matrix around Y axis.
factory Matrix4.skewY(double beta) {
final m = Matrix4.identity();
m._m4storage[1] = math.tan(beta);
return m;
}
/// Skew matrix around X axis (alpha) and Y axis (beta).
factory Matrix4.skew(double alpha, double beta) {
final m = Matrix4.identity();
m._m4storage[1] = math.tan(beta);
m._m4storage[4] = math.tan(alpha);
return m;
}
/// Constructs Matrix4 with given [Float64List] as [storage].
Matrix4.fromFloat64List(this._m4storage);
/// Constructs Matrix4 with a [storage] that views given [buffer] starting at
/// [offset]. [offset] has to be multiple of [Float64List.bytesPerElement].
Matrix4.fromBuffer(ByteBuffer buffer, int offset)
: _m4storage = Float64List.view(buffer, offset, 16);
/// Constructs Matrix4 from [translation], [rotation] and [scale].
factory Matrix4.compose(
Vector3 translation, Quaternion rotation, Vector3 scale) =>
Matrix4.zero()
..setFromTranslationRotationScale(translation, rotation, scale);
/// Sets the diagonal to [arg].
void splatDiagonal(double arg) {
_m4storage[0] = arg;
_m4storage[5] = arg;
_m4storage[10] = arg;
_m4storage[15] = arg;
}
/// Sets the matrix with specified values.
void setValues(
double arg0,
double arg1,
double arg2,
double arg3,
double arg4,
double arg5,
double arg6,
double arg7,
double arg8,
double arg9,
double arg10,
double arg11,
double arg12,
double arg13,
double arg14,
double arg15) {
_m4storage[15] = arg15;
_m4storage[14] = arg14;
_m4storage[13] = arg13;
_m4storage[12] = arg12;
_m4storage[11] = arg11;
_m4storage[10] = arg10;
_m4storage[9] = arg9;
_m4storage[8] = arg8;
_m4storage[7] = arg7;
_m4storage[6] = arg6;
_m4storage[5] = arg5;
_m4storage[4] = arg4;
_m4storage[3] = arg3;
_m4storage[2] = arg2;
_m4storage[1] = arg1;
_m4storage[0] = arg0;
}
/// Sets the entire matrix to the column values.
void setColumns(Vector4 arg0, Vector4 arg1, Vector4 arg2, Vector4 arg3) {
final arg0Storage = arg0._v4storage;
final arg1Storage = arg1._v4storage;
final arg2Storage = arg2._v4storage;
final arg3Storage = arg3._v4storage;
_m4storage[0] = arg0Storage[0];
_m4storage[1] = arg0Storage[1];
_m4storage[2] = arg0Storage[2];
_m4storage[3] = arg0Storage[3];
_m4storage[4] = arg1Storage[0];
_m4storage[5] = arg1Storage[1];
_m4storage[6] = arg1Storage[2];
_m4storage[7] = arg1Storage[3];
_m4storage[8] = arg2Storage[0];
_m4storage[9] = arg2Storage[1];
_m4storage[10] = arg2Storage[2];
_m4storage[11] = arg2Storage[3];
_m4storage[12] = arg3Storage[0];
_m4storage[13] = arg3Storage[1];
_m4storage[14] = arg3Storage[2];
_m4storage[15] = arg3Storage[3];
}
/// Sets the entire matrix to the matrix in [arg].
void setFrom(Matrix4 arg) {
final argStorage = arg._m4storage;
_m4storage[15] = argStorage[15];
_m4storage[14] = argStorage[14];
_m4storage[13] = argStorage[13];
_m4storage[12] = argStorage[12];
_m4storage[11] = argStorage[11];
_m4storage[10] = argStorage[10];
_m4storage[9] = argStorage[9];
_m4storage[8] = argStorage[8];
_m4storage[7] = argStorage[7];
_m4storage[6] = argStorage[6];
_m4storage[5] = argStorage[5];
_m4storage[4] = argStorage[4];
_m4storage[3] = argStorage[3];
_m4storage[2] = argStorage[2];
_m4storage[1] = argStorage[1];
_m4storage[0] = argStorage[0];
}
/// Sets the matrix from translation [arg0] and rotation [arg1].
void setFromTranslationRotation(Vector3 arg0, Quaternion arg1) {
final arg1Storage = arg1._qStorage;
final x = arg1Storage[0];
final y = arg1Storage[1];
final z = arg1Storage[2];
final w = arg1Storage[3];
final x2 = x + x;
final y2 = y + y;
final z2 = z + z;
final xx = x * x2;
final xy = x * y2;
final xz = x * z2;
final yy = y * y2;
final yz = y * z2;
final zz = z * z2;
final wx = w * x2;
final wy = w * y2;
final wz = w * z2;
final arg0Storage = arg0._v3storage;
_m4storage[0] = 1.0 - (yy + zz);
_m4storage[1] = xy + wz;
_m4storage[2] = xz - wy;
_m4storage[3] = 0.0;
_m4storage[4] = xy - wz;
_m4storage[5] = 1.0 - (xx + zz);
_m4storage[6] = yz + wx;
_m4storage[7] = 0.0;
_m4storage[8] = xz + wy;
_m4storage[9] = yz - wx;
_m4storage[10] = 1.0 - (xx + yy);
_m4storage[11] = 0.0;
_m4storage[12] = arg0Storage[0];
_m4storage[13] = arg0Storage[1];
_m4storage[14] = arg0Storage[2];
_m4storage[15] = 1.0;
}
/// Sets the matrix from [translation], [rotation] and [scale].
void setFromTranslationRotationScale(
Vector3 translation, Quaternion rotation, Vector3 scale) {
setFromTranslationRotation(translation, rotation);
this.scale(scale);
}
/// Sets the upper 2x2 of the matrix to be [arg].
void setUpper2x2(Matrix2 arg) {
final argStorage = arg._m2storage;
_m4storage[0] = argStorage[0];
_m4storage[1] = argStorage[1];
_m4storage[4] = argStorage[2];
_m4storage[5] = argStorage[3];
}
/// Sets the diagonal of the matrix to be [arg].
void setDiagonal(Vector4 arg) {
final argStorage = arg._v4storage;
_m4storage[0] = argStorage[0];
_m4storage[5] = argStorage[1];
_m4storage[10] = argStorage[2];
_m4storage[15] = argStorage[3];
}
void setOuter(Vector4 u, Vector4 v) {
final uStorage = u._v4storage;
final vStorage = v._v4storage;
_m4storage[0] = uStorage[0] * vStorage[0];
_m4storage[1] = uStorage[0] * vStorage[1];
_m4storage[2] = uStorage[0] * vStorage[2];
_m4storage[3] = uStorage[0] * vStorage[3];
_m4storage[4] = uStorage[1] * vStorage[0];
_m4storage[5] = uStorage[1] * vStorage[1];
_m4storage[6] = uStorage[1] * vStorage[2];
_m4storage[7] = uStorage[1] * vStorage[3];
_m4storage[8] = uStorage[2] * vStorage[0];
_m4storage[9] = uStorage[2] * vStorage[1];
_m4storage[10] = uStorage[2] * vStorage[2];
_m4storage[11] = uStorage[2] * vStorage[3];
_m4storage[12] = uStorage[3] * vStorage[0];
_m4storage[13] = uStorage[3] * vStorage[1];
_m4storage[14] = uStorage[3] * vStorage[2];
_m4storage[15] = uStorage[3] * vStorage[3];
}
/// Returns a printable string
@override
String toString() => '[0] ${getRow(0)}\n[1] ${getRow(1)}\n'
'[2] ${getRow(2)}\n[3] ${getRow(3)}\n';
/// Dimension of the matrix.
int get dimension => 4;
/// Access the element of the matrix at the index [i].
double operator [](int i) => _m4storage[i];
/// Set the element of the matrix at the index [i].
void operator []=(int i, double v) {
_m4storage[i] = v;
}
/// Check if two matrices are the same.
@override
bool operator ==(Object other) =>
(other is Matrix4) &&
(_m4storage[0] == other._m4storage[0]) &&
(_m4storage[1] == other._m4storage[1]) &&
(_m4storage[2] == other._m4storage[2]) &&
(_m4storage[3] == other._m4storage[3]) &&
(_m4storage[4] == other._m4storage[4]) &&
(_m4storage[5] == other._m4storage[5]) &&
(_m4storage[6] == other._m4storage[6]) &&
(_m4storage[7] == other._m4storage[7]) &&
(_m4storage[8] == other._m4storage[8]) &&
(_m4storage[9] == other._m4storage[9]) &&
(_m4storage[10] == other._m4storage[10]) &&
(_m4storage[11] == other._m4storage[11]) &&
(_m4storage[12] == other._m4storage[12]) &&
(_m4storage[13] == other._m4storage[13]) &&
(_m4storage[14] == other._m4storage[14]) &&
(_m4storage[15] == other._m4storage[15]);
@override
int get hashCode => Object.hashAll(_m4storage);
/// Returns row 0
Vector4 get row0 => getRow(0);
/// Returns row 1
Vector4 get row1 => getRow(1);
/// Returns row 2
Vector4 get row2 => getRow(2);
/// Returns row 3
Vector4 get row3 => getRow(3);
/// Sets row 0 to [arg]
set row0(Vector4 arg) => setRow(0, arg);
/// Sets row 1 to [arg]
set row1(Vector4 arg) => setRow(1, arg);
/// Sets row 2 to [arg]
set row2(Vector4 arg) => setRow(2, arg);
/// Sets row 3 to [arg]
set row3(Vector4 arg) => setRow(3, arg);
/// Assigns the [row] of the matrix [arg]
void setRow(int row, Vector4 arg) {
final argStorage = arg._v4storage;
_m4storage[index(row, 0)] = argStorage[0];
_m4storage[index(row, 1)] = argStorage[1];
_m4storage[index(row, 2)] = argStorage[2];
_m4storage[index(row, 3)] = argStorage[3];
}
/// Gets the [row] of the matrix
Vector4 getRow(int row) {
final r = Vector4.zero();
final rStorage = r._v4storage;
rStorage[0] = _m4storage[index(row, 0)];
rStorage[1] = _m4storage[index(row, 1)];
rStorage[2] = _m4storage[index(row, 2)];
rStorage[3] = _m4storage[index(row, 3)];
return r;
}
/// Assigns the [column] of the matrix [arg]
void setColumn(int column, Vector4 arg) {
final entry = column * 4;
final argStorage = arg._v4storage;
_m4storage[entry + 3] = argStorage[3];
_m4storage[entry + 2] = argStorage[2];
_m4storage[entry + 1] = argStorage[1];
_m4storage[entry + 0] = argStorage[0];
}
/// Gets the [column] of the matrix
Vector4 getColumn(int column) {
final r = Vector4.zero();
final rStorage = r._v4storage;
final entry = column * 4;
rStorage[3] = _m4storage[entry + 3];
rStorage[2] = _m4storage[entry + 2];
rStorage[1] = _m4storage[entry + 1];
rStorage[0] = _m4storage[entry + 0];
return r;
}
/// Clone matrix.
Matrix4 clone() => Matrix4.copy(this);
/// Copy into [arg].
Matrix4 copyInto(Matrix4 arg) {
final argStorage = arg._m4storage;
argStorage[0] = _m4storage[0];
argStorage[1] = _m4storage[1];
argStorage[2] = _m4storage[2];
argStorage[3] = _m4storage[3];
argStorage[4] = _m4storage[4];
argStorage[5] = _m4storage[5];
argStorage[6] = _m4storage[6];
argStorage[7] = _m4storage[7];
argStorage[8] = _m4storage[8];
argStorage[9] = _m4storage[9];
argStorage[10] = _m4storage[10];
argStorage[11] = _m4storage[11];
argStorage[12] = _m4storage[12];
argStorage[13] = _m4storage[13];
argStorage[14] = _m4storage[14];
argStorage[15] = _m4storage[15];
return arg;
}
/// Returns new matrix -this
Matrix4 operator -() => clone()..negate();
/// Returns a new vector or matrix by multiplying this with [arg].
dynamic operator *(dynamic arg) {
if (arg is double) {
return scaled(arg);
}
if (arg is Vector4) {
return transformed(arg);
}
if (arg is Vector3) {
return transformed3(arg);
}
if (arg is Matrix4) {
return multiplied(arg);
}
throw ArgumentError(arg);
}
/// Returns new matrix after component wise this + [arg]
Matrix4 operator +(Matrix4 arg) => clone()..add(arg);
/// Returns new matrix after component wise this - [arg]
Matrix4 operator -(Matrix4 arg) => clone()..sub(arg);
/// Translate this matrix by a [Vector3], [Vector4], or x,y,z
void translate(dynamic x, [double y = 0.0, double z = 0.0]) {
double tx;
double ty;
double tz;
final tw = x is Vector4 ? x.w : 1.0;
if (x is Vector3) {
tx = x.x;
ty = x.y;
tz = x.z;
} else if (x is Vector4) {
tx = x.x;
ty = x.y;
tz = x.z;
} else if (x is double) {
tx = x;
ty = y;
tz = z;
} else {
throw UnimplementedError();
}
final t1 = _m4storage[0] * tx +
_m4storage[4] * ty +
_m4storage[8] * tz +
_m4storage[12] * tw;
final t2 = _m4storage[1] * tx +
_m4storage[5] * ty +
_m4storage[9] * tz +
_m4storage[13] * tw;
final t3 = _m4storage[2] * tx +
_m4storage[6] * ty +
_m4storage[10] * tz +
_m4storage[14] * tw;
final t4 = _m4storage[3] * tx +
_m4storage[7] * ty +
_m4storage[11] * tz +
_m4storage[15] * tw;
_m4storage[12] = t1;
_m4storage[13] = t2;
_m4storage[14] = t3;
_m4storage[15] = t4;
}
/// Multiply this by a translation from the left.
/// The translation can be specified with a [Vector3], [Vector4], or x, y, z.
void leftTranslate(dynamic x, [double y = 0.0, double z = 0.0]) {
double tx;
double ty;
double tz;
final tw = x is Vector4 ? x.w : 1.0;
if (x is Vector3) {
tx = x.x;
ty = x.y;
tz = x.z;
} else if (x is Vector4) {
tx = x.x;
ty = x.y;
tz = x.z;
} else if (x is double) {
tx = x;
ty = y;
tz = z;
} else {
throw UnimplementedError();
}
// Column 1
_m4storage[0] += tx * _m4storage[3];
_m4storage[1] += ty * _m4storage[3];
_m4storage[2] += tz * _m4storage[3];
_m4storage[3] = tw * _m4storage[3];
// Column 2
_m4storage[4] += tx * _m4storage[7];
_m4storage[5] += ty * _m4storage[7];
_m4storage[6] += tz * _m4storage[7];
_m4storage[7] = tw * _m4storage[7];
// Column 3
_m4storage[8] += tx * _m4storage[11];
_m4storage[9] += ty * _m4storage[11];
_m4storage[10] += tz * _m4storage[11];
_m4storage[11] = tw * _m4storage[11];
// Column 4
_m4storage[12] += tx * _m4storage[15];
_m4storage[13] += ty * _m4storage[15];
_m4storage[14] += tz * _m4storage[15];
_m4storage[15] = tw * _m4storage[15];
}
/// Rotate this [angle] radians around [axis]
void rotate(Vector3 axis, double angle) {
final len = axis.length;
final axisStorage = axis._v3storage;
final x = axisStorage[0] / len;
final y = axisStorage[1] / len;
final z = axisStorage[2] / len;
final c = math.cos(angle);
final s = math.sin(angle);
final C = 1.0 - c;
final m11 = x * x * C + c;
final m12 = x * y * C - z * s;
final m13 = x * z * C + y * s;
final m21 = y * x * C + z * s;
final m22 = y * y * C + c;
final m23 = y * z * C - x * s;
final m31 = z * x * C - y * s;
final m32 = z * y * C + x * s;
final m33 = z * z * C + c;
final t1 = _m4storage[0] * m11 + _m4storage[4] * m21 + _m4storage[8] * m31;
final t2 = _m4storage[1] * m11 + _m4storage[5] * m21 + _m4storage[9] * m31;
final t3 = _m4storage[2] * m11 + _m4storage[6] * m21 + _m4storage[10] * m31;
final t4 = _m4storage[3] * m11 + _m4storage[7] * m21 + _m4storage[11] * m31;
final t5 = _m4storage[0] * m12 + _m4storage[4] * m22 + _m4storage[8] * m32;
final t6 = _m4storage[1] * m12 + _m4storage[5] * m22 + _m4storage[9] * m32;
final t7 = _m4storage[2] * m12 + _m4storage[6] * m22 + _m4storage[10] * m32;
final t8 = _m4storage[3] * m12 + _m4storage[7] * m22 + _m4storage[11] * m32;
final t9 = _m4storage[0] * m13 + _m4storage[4] * m23 + _m4storage[8] * m33;
final t10 = _m4storage[1] * m13 + _m4storage[5] * m23 + _m4storage[9] * m33;
final t11 =
_m4storage[2] * m13 + _m4storage[6] * m23 + _m4storage[10] * m33;
final t12 =
_m4storage[3] * m13 + _m4storage[7] * m23 + _m4storage[11] * m33;
_m4storage[0] = t1;
_m4storage[1] = t2;
_m4storage[2] = t3;
_m4storage[3] = t4;
_m4storage[4] = t5;
_m4storage[5] = t6;
_m4storage[6] = t7;
_m4storage[7] = t8;
_m4storage[8] = t9;
_m4storage[9] = t10;
_m4storage[10] = t11;
_m4storage[11] = t12;
}
/// Rotate this [angle] radians around X
void rotateX(double angle) {
final cosAngle = math.cos(angle);
final sinAngle = math.sin(angle);
final t1 = _m4storage[4] * cosAngle + _m4storage[8] * sinAngle;
final t2 = _m4storage[5] * cosAngle + _m4storage[9] * sinAngle;
final t3 = _m4storage[6] * cosAngle + _m4storage[10] * sinAngle;
final t4 = _m4storage[7] * cosAngle + _m4storage[11] * sinAngle;
final t5 = _m4storage[4] * -sinAngle + _m4storage[8] * cosAngle;
final t6 = _m4storage[5] * -sinAngle + _m4storage[9] * cosAngle;
final t7 = _m4storage[6] * -sinAngle + _m4storage[10] * cosAngle;
final t8 = _m4storage[7] * -sinAngle + _m4storage[11] * cosAngle;
_m4storage[4] = t1;
_m4storage[5] = t2;
_m4storage[6] = t3;
_m4storage[7] = t4;
_m4storage[8] = t5;
_m4storage[9] = t6;
_m4storage[10] = t7;
_m4storage[11] = t8;
}
/// Rotate this matrix [angle] radians around Y
void rotateY(double angle) {
final cosAngle = math.cos(angle);
final sinAngle = math.sin(angle);
final t1 = _m4storage[0] * cosAngle + _m4storage[8] * -sinAngle;
final t2 = _m4storage[1] * cosAngle + _m4storage[9] * -sinAngle;
final t3 = _m4storage[2] * cosAngle + _m4storage[10] * -sinAngle;
final t4 = _m4storage[3] * cosAngle + _m4storage[11] * -sinAngle;
final t5 = _m4storage[0] * sinAngle + _m4storage[8] * cosAngle;
final t6 = _m4storage[1] * sinAngle + _m4storage[9] * cosAngle;
final t7 = _m4storage[2] * sinAngle + _m4storage[10] * cosAngle;
final t8 = _m4storage[3] * sinAngle + _m4storage[11] * cosAngle;
_m4storage[0] = t1;
_m4storage[1] = t2;
_m4storage[2] = t3;
_m4storage[3] = t4;
_m4storage[8] = t5;
_m4storage[9] = t6;
_m4storage[10] = t7;
_m4storage[11] = t8;
}
/// Rotate this matrix [angle] radians around Z
void rotateZ(double angle) {
final cosAngle = math.cos(angle);
final sinAngle = math.sin(angle);
final t1 = _m4storage[0] * cosAngle + _m4storage[4] * sinAngle;
final t2 = _m4storage[1] * cosAngle + _m4storage[5] * sinAngle;
final t3 = _m4storage[2] * cosAngle + _m4storage[6] * sinAngle;
final t4 = _m4storage[3] * cosAngle + _m4storage[7] * sinAngle;
final t5 = _m4storage[0] * -sinAngle + _m4storage[4] * cosAngle;
final t6 = _m4storage[1] * -sinAngle + _m4storage[5] * cosAngle;
final t7 = _m4storage[2] * -sinAngle + _m4storage[6] * cosAngle;
final t8 = _m4storage[3] * -sinAngle + _m4storage[7] * cosAngle;
_m4storage[0] = t1;
_m4storage[1] = t2;
_m4storage[2] = t3;
_m4storage[3] = t4;
_m4storage[4] = t5;
_m4storage[5] = t6;
_m4storage[6] = t7;
_m4storage[7] = t8;
}
/// Scale this matrix by a [Vector3], [Vector4], or x,y,z
void scale(dynamic x, [double? y, double? z]) {
double sx;
double sy;
double sz;
final sw = x is Vector4 ? x.w : 1.0;
if (x is Vector3) {
sx = x.x;
sy = x.y;
sz = x.z;
} else if (x is Vector4) {
sx = x.x;
sy = x.y;
sz = x.z;
} else if (x is double) {
sx = x;
sy = y ?? x;
sz = z ?? x;
} else {
throw UnimplementedError();
}
_m4storage[0] *= sx;
_m4storage[1] *= sx;
_m4storage[2] *= sx;
_m4storage[3] *= sx;
_m4storage[4] *= sy;
_m4storage[5] *= sy;
_m4storage[6] *= sy;
_m4storage[7] *= sy;
_m4storage[8] *= sz;
_m4storage[9] *= sz;
_m4storage[10] *= sz;
_m4storage[11] *= sz;
_m4storage[12] *= sw;
_m4storage[13] *= sw;
_m4storage[14] *= sw;
_m4storage[15] *= sw;
}
/// Create a copy of this scaled by a [Vector3], [Vector4] or [x],[y], and
/// [z].
Matrix4 scaled(dynamic x, [double? y, double? z]) => clone()..scale(x, y, z);
/// Zeros this.
void setZero() {
_m4storage[0] = 0.0;
_m4storage[1] = 0.0;
_m4storage[2] = 0.0;
_m4storage[3] = 0.0;
_m4storage[4] = 0.0;
_m4storage[5] = 0.0;
_m4storage[6] = 0.0;
_m4storage[7] = 0.0;
_m4storage[8] = 0.0;
_m4storage[9] = 0.0;
_m4storage[10] = 0.0;
_m4storage[11] = 0.0;
_m4storage[12] = 0.0;
_m4storage[13] = 0.0;
_m4storage[14] = 0.0;
_m4storage[15] = 0.0;
}
/// Makes this into the identity matrix.
void setIdentity() {
_m4storage[0] = 1.0;
_m4storage[1] = 0.0;
_m4storage[2] = 0.0;
_m4storage[3] = 0.0;
_m4storage[4] = 0.0;
_m4storage[5] = 1.0;
_m4storage[6] = 0.0;
_m4storage[7] = 0.0;
_m4storage[8] = 0.0;
_m4storage[9] = 0.0;
_m4storage[10] = 1.0;
_m4storage[11] = 0.0;
_m4storage[12] = 0.0;
_m4storage[13] = 0.0;
_m4storage[14] = 0.0;
_m4storage[15] = 1.0;
}
/// Returns the tranpose of this.
Matrix4 transposed() => clone()..transpose();
void transpose() {
double temp;
temp = _m4storage[4];
_m4storage[4] = _m4storage[1];
_m4storage[1] = temp;
temp = _m4storage[8];
_m4storage[8] = _m4storage[2];
_m4storage[2] = temp;
temp = _m4storage[12];
_m4storage[12] = _m4storage[3];
_m4storage[3] = temp;
temp = _m4storage[9];
_m4storage[9] = _m4storage[6];
_m4storage[6] = temp;
temp = _m4storage[13];
_m4storage[13] = _m4storage[7];
_m4storage[7] = temp;
temp = _m4storage[14];
_m4storage[14] = _m4storage[11];
_m4storage[11] = temp;
}
/// Returns the component wise absolute value of this.
Matrix4 absolute() {
final r = Matrix4.zero();
final rStorage = r._m4storage;
rStorage[0] = _m4storage[0].abs();
rStorage[1] = _m4storage[1].abs();
rStorage[2] = _m4storage[2].abs();
rStorage[3] = _m4storage[3].abs();
rStorage[4] = _m4storage[4].abs();
rStorage[5] = _m4storage[5].abs();
rStorage[6] = _m4storage[6].abs();
rStorage[7] = _m4storage[7].abs();
rStorage[8] = _m4storage[8].abs();
rStorage[9] = _m4storage[9].abs();
rStorage[10] = _m4storage[10].abs();
rStorage[11] = _m4storage[11].abs();
rStorage[12] = _m4storage[12].abs();
rStorage[13] = _m4storage[13].abs();
rStorage[14] = _m4storage[14].abs();
rStorage[15] = _m4storage[15].abs();
return r;
}
/// Returns the determinant of this matrix.
double determinant() {
final det2_01_01 =