/
matrix4.dart
1881 lines (1742 loc) · 60.8 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;
/// 4D Matrix.
/// Values are stored in column major order.
class Matrix4 {
final Float32List _m4storage;
/// The components of the matrix.
Float32List get storage => _m4storage;
/// Solve [A] * [x] = [b].
static void solve2(Matrix4 A, Vector2 x, Vector2 b) {
final double a11 = A.entry(0, 0);
final double a12 = A.entry(0, 1);
final double a21 = A.entry(1, 0);
final double a22 = A.entry(1, 1);
final double bx = b.x - A._m4storage[8];
final double by = b.y - A._m4storage[9];
double det = a11 * a22 - a12 * a21;
if (det != 0.0) {
det = 1.0 / det;
}
x.x = det * (a22 * bx - a12 * by);
x.y = det * (a11 * by - a21 * bx);
}
/// Solve [A] * [x] = [b].
static void solve3(Matrix4 A, Vector3 x, Vector3 b) {
final double A0x = A.entry(0, 0);
final double A0y = A.entry(1, 0);
final double A0z = A.entry(2, 0);
final double A1x = A.entry(0, 1);
final double A1y = A.entry(1, 1);
final double A1z = A.entry(2, 1);
final double A2x = A.entry(0, 2);
final double A2y = A.entry(1, 2);
final double A2z = A.entry(2, 2);
final double bx = b.x - A._m4storage[12];
final double by = b.y - A._m4storage[13];
final double 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 double 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 double 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 double z_ = det * (A0x * rx + A0y * ry + A0z * rz);
x.x = x_;
x.y = y_;
x.z = z_;
}
/// Solve [A] * [x] = [b].
static void solve(Matrix4 A, Vector4 x, Vector4 b) {
final double a00 = A._m4storage[0];
final double a01 = A._m4storage[1];
final double a02 = A._m4storage[2];
final double a03 = A._m4storage[3];
final double a10 = A._m4storage[4];
final double a11 = A._m4storage[5];
final double a12 = A._m4storage[6];
final double a13 = A._m4storage[7];
final double a20 = A._m4storage[8];
final double a21 = A._m4storage[9];
final double a22 = A._m4storage[10];
final double a23 = A._m4storage[11];
final double a30 = A._m4storage[12];
final double a31 = A._m4storage[13];
final double a32 = A._m4storage[14];
final double a33 = A._m4storage[15];
final double b00 = a00 * a11 - a01 * a10;
final double b01 = a00 * a12 - a02 * a10;
final double b02 = a00 * a13 - a03 * a10;
final double b03 = a01 * a12 - a02 * a11;
final double b04 = a01 * a13 - a03 * a11;
final double b05 = a02 * a13 - a03 * a12;
final double b06 = a20 * a31 - a21 * a30;
final double b07 = a20 * a32 - a22 * a30;
final double b08 = a20 * a33 - a23 * a30;
final double b09 = a21 * a32 - a22 * a31;
final double b10 = a21 * a33 - a23 * a31;
final double b11 = a22 * a33 - a23 * a32;
final double bX = b.storage[0];
final double bY = b.storage[1];
final double bZ = b.storage[2];
final double bW = b.storage[3];
double 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);
x.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);
x.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);
x.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);
}
/// 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].
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) => new Matrix4.zero()
..setValues(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9,
arg10, arg11, arg12, arg13, arg14, arg15);
/// Zero matrix.
Matrix4.zero() : _m4storage = new Float32List(16);
/// Identity matrix.
factory Matrix4.identity() => new Matrix4.zero()..setIdentity();
/// Copies values from [other].
factory Matrix4.copy(Matrix4 other) => new Matrix4.zero()..setFrom(other);
/// Constructs a new mat4 from columns.
factory Matrix4.columns(
Vector4 arg0, Vector4 arg1, Vector4 arg2, Vector4 arg3) =>
new Matrix4.zero()..setColumns(arg0, arg1, arg2, arg3);
/// Outer product of [u] and [v].
factory Matrix4.outer(Vector4 u, Vector4 v) =>
new Matrix4.zero()..setOuter(u, v);
/// Rotation of [radians_] around X.
factory Matrix4.rotationX(double radians) => new Matrix4.zero()
.._m4storage[15] = 1.0
..setRotationX(radians);
/// Rotation of [radians_] around Y.
factory Matrix4.rotationY(double radians) => new Matrix4.zero()
.._m4storage[15] = 1.0
..setRotationY(radians);
/// Rotation of [radians_] around Z.
factory Matrix4.rotationZ(double radians) => new Matrix4.zero()
.._m4storage[15] = 1.0
..setRotationZ(radians);
/// Translation matrix.
factory Matrix4.translation(Vector3 translation) => new Matrix4.zero()
..setIdentity()
..setTranslation(translation);
/// Translation matrix.
factory Matrix4.translationValues(double x, double y, double z) =>
new Matrix4.zero()
..setIdentity()
..setTranslationRaw(x, y, z);
/// Scale matrix.
factory Matrix4.diagonal3(Vector3 scale) {
final m = new 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) =>
new Matrix4.zero()
.._m4storage[15] = 1.0
.._m4storage[10] = z
.._m4storage[5] = y
.._m4storage[0] = x;
/// Constructs Matrix4 with given [Float32List] as [storage].
Matrix4.fromFloat32List(this._m4storage);
/// Constructs Matrix4 with a [storage] that views given [buffer] starting at
/// [offset]. [offset] has to be multiple of [Float32List.BYTES_PER_ELEMENT].
Matrix4.fromBuffer(ByteBuffer buffer, int offset)
: _m4storage = new Float32List.view(buffer, offset, 16);
/// Constructs Matrix4 from [translation], [rotation] and [scale].
factory Matrix4.compose(
Vector3 translation, Quaternion rotation, Vector3 scale) =>
new Matrix4.zero()
..setFromTranslationRotationScale(translation, rotation, scale);
/// Sets the diagonal to [arg].
Matrix4 splatDiagonal(double arg) {
_m4storage[0] = arg;
_m4storage[5] = arg;
_m4storage[10] = arg;
_m4storage[15] = arg;
return this;
}
/// Sets the matrix with specified values.
Matrix4 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;
return this;
}
/// Sets the entire matrix to the column values.
Matrix4 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];
return this;
}
/// Sets the entire matrix to the matrix in [arg].
Matrix4 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];
return this;
}
/// Sets the matrix from translation [arg0] and rotation [arg1].
Matrix4 setFromTranslationRotation(Vector3 arg0, Quaternion arg1) {
final arg1Storage = arg1._qStorage;
double x = arg1Storage[0];
double y = arg1Storage[1];
double z = arg1Storage[2];
double w = arg1Storage[3];
double x2 = x + x;
double y2 = y + y;
double z2 = z + z;
double xx = x * x2;
double xy = x * y2;
double xz = x * z2;
double yy = y * y2;
double yz = y * z2;
double zz = z * z2;
double wx = w * x2;
double wy = w * y2;
double 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;
return this;
}
/// Sets the matrix from [translation], [rotation] and [scale].
Matrix4 setFromTranslationRotationScale(
Vector3 translation, Quaternion rotation, Vector3 scale) {
setFromTranslationRotation(translation, rotation);
this.scale(scale);
return this;
}
/// Sets the upper 2x2 of the matrix to be [arg].
Matrix4 setUpper2x2(Matrix2 arg) {
final argStorage = arg._m2storage;
_m4storage[0] = argStorage[0];
_m4storage[1] = argStorage[1];
_m4storage[4] = argStorage[2];
_m4storage[5] = argStorage[3];
return this;
}
/// Sets the diagonal of the matrix to be [arg].
Matrix4 setDiagonal(Vector4 arg) {
final argStorage = arg._v4storage;
_m4storage[0] = argStorage[0];
_m4storage[5] = argStorage[1];
_m4storage[10] = argStorage[2];
_m4storage[15] = argStorage[3];
return this;
}
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
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.
bool operator ==(other) {
return (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]);
}
int get hashCode => quiver.hashObjects(_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) {
Vector4 r = new 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) {
int 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) {
Vector4 r = new Vector4.zero();
final rStorage = r._v4storage;
int 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() => new 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.dimension == 4) {
return multiplied(arg);
}
throw new 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
Matrix4 translate(x, [double y = 0.0, double z = 0.0]) {
double tx;
double ty;
double tz;
double tw = x is Vector4 ? x.w : 1.0;
if (x is Vector3 || x is Vector4) {
tx = x.x;
ty = x.y;
tz = x.z;
} else {
tx = x;
ty = y;
tz = z;
}
var t1 = _m4storage[0] * tx +
_m4storage[4] * ty +
_m4storage[8] * tz +
_m4storage[12] * tw;
var t2 = _m4storage[1] * tx +
_m4storage[5] * ty +
_m4storage[9] * tz +
_m4storage[13] * tw;
var t3 = _m4storage[2] * tx +
_m4storage[6] * ty +
_m4storage[10] * tz +
_m4storage[14] * tw;
var 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;
return this;
}
/// Rotate this [angle] radians around [axis]
Matrix4 rotate(Vector3 axis, double angle) {
var len = axis.length;
final axisStorage = axis._v3storage;
var x = axisStorage[0] / len;
var y = axisStorage[1] / len;
var z = axisStorage[2] / len;
var c = Math.cos(angle);
var s = Math.sin(angle);
var C = 1.0 - c;
var m11 = x * x * C + c;
var m12 = x * y * C - z * s;
var m13 = x * z * C + y * s;
var m21 = y * x * C + z * s;
var m22 = y * y * C + c;
var m23 = y * z * C - x * s;
var m31 = z * x * C - y * s;
var m32 = z * y * C + x * s;
var m33 = z * z * C + c;
var t1 = _m4storage[0] * m11 + _m4storage[4] * m21 + _m4storage[8] * m31;
var t2 = _m4storage[1] * m11 + _m4storage[5] * m21 + _m4storage[9] * m31;
var t3 = _m4storage[2] * m11 + _m4storage[6] * m21 + _m4storage[10] * m31;
var t4 = _m4storage[3] * m11 + _m4storage[7] * m21 + _m4storage[11] * m31;
var t5 = _m4storage[0] * m12 + _m4storage[4] * m22 + _m4storage[8] * m32;
var t6 = _m4storage[1] * m12 + _m4storage[5] * m22 + _m4storage[9] * m32;
var t7 = _m4storage[2] * m12 + _m4storage[6] * m22 + _m4storage[10] * m32;
var t8 = _m4storage[3] * m12 + _m4storage[7] * m22 + _m4storage[11] * m32;
var t9 = _m4storage[0] * m13 + _m4storage[4] * m23 + _m4storage[8] * m33;
var t10 = _m4storage[1] * m13 + _m4storage[5] * m23 + _m4storage[9] * m33;
var t11 = _m4storage[2] * m13 + _m4storage[6] * m23 + _m4storage[10] * m33;
var 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;
return this;
}
/// Rotate this [angle] radians around X
Matrix4 rotateX(double angle) {
double cosAngle = Math.cos(angle);
double sinAngle = Math.sin(angle);
var t1 = _m4storage[4] * cosAngle + _m4storage[8] * sinAngle;
var t2 = _m4storage[5] * cosAngle + _m4storage[9] * sinAngle;
var t3 = _m4storage[6] * cosAngle + _m4storage[10] * sinAngle;
var t4 = _m4storage[7] * cosAngle + _m4storage[11] * sinAngle;
var t5 = _m4storage[4] * -sinAngle + _m4storage[8] * cosAngle;
var t6 = _m4storage[5] * -sinAngle + _m4storage[9] * cosAngle;
var t7 = _m4storage[6] * -sinAngle + _m4storage[10] * cosAngle;
var 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;
return this;
}
/// Rotate this matrix [angle] radians around Y
Matrix4 rotateY(double angle) {
double cosAngle = Math.cos(angle);
double sinAngle = Math.sin(angle);
var t1 = _m4storage[0] * cosAngle + _m4storage[8] * -sinAngle;
var t2 = _m4storage[1] * cosAngle + _m4storage[9] * -sinAngle;
var t3 = _m4storage[2] * cosAngle + _m4storage[10] * -sinAngle;
var t4 = _m4storage[3] * cosAngle + _m4storage[11] * -sinAngle;
var t5 = _m4storage[0] * sinAngle + _m4storage[8] * cosAngle;
var t6 = _m4storage[1] * sinAngle + _m4storage[9] * cosAngle;
var t7 = _m4storage[2] * sinAngle + _m4storage[10] * cosAngle;
var 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;
return this;
}
/// Rotate this matrix [angle] radians around Z
Matrix4 rotateZ(double angle) {
double cosAngle = Math.cos(angle);
double sinAngle = Math.sin(angle);
var t1 = _m4storage[0] * cosAngle + _m4storage[4] * sinAngle;
var t2 = _m4storage[1] * cosAngle + _m4storage[5] * sinAngle;
var t3 = _m4storage[2] * cosAngle + _m4storage[6] * sinAngle;
var t4 = _m4storage[3] * cosAngle + _m4storage[7] * sinAngle;
var t5 = _m4storage[0] * -sinAngle + _m4storage[4] * cosAngle;
var t6 = _m4storage[1] * -sinAngle + _m4storage[5] * cosAngle;
var t7 = _m4storage[2] * -sinAngle + _m4storage[6] * cosAngle;
var 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;
return this;
}
/// Scale this matrix by a [Vector3], [Vector4], or x,y,z
Matrix4 scale(x, [double y, double z]) {
double sx;
double sy;
double sz;
double sw = x is Vector4 ? x.w : 1.0;
if (x is Vector3 || x is Vector4) {
sx = x.x;
sy = x.y;
sz = x.z;
} else {
sx = x;
sy = y == null ? x : y.toDouble();
sz = z == null ? x : z.toDouble();
}
_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;
return this;
}
/// Create a copy of [this] scaled by a [Vector3], [Vector4] or [x],[y], and
/// [z].
Matrix4 scaled(x, [double y = null, double z = null]) =>
clone()..scale(x, y, z);
/// Zeros [this].
Matrix4 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;
return this;
}
/// Makes [this] into the identity matrix.
Matrix4 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;
return this;
}
/// Returns the tranpose of this.
Matrix4 transposed() => clone()..transpose();
Matrix4 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;
return this;
}
/// Returns the component wise absolute value of this.
Matrix4 absolute() {
Matrix4 r = new 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() {
double det2_01_01 =
_m4storage[0] * _m4storage[5] - _m4storage[1] * _m4storage[4];
double det2_01_02 =
_m4storage[0] * _m4storage[6] - _m4storage[2] * _m4storage[4];
double det2_01_03 =
_m4storage[0] * _m4storage[7] - _m4storage[3] * _m4storage[4];
double det2_01_12 =
_m4storage[1] * _m4storage[6] - _m4storage[2] * _m4storage[5];
double det2_01_13 =
_m4storage[1] * _m4storage[7] - _m4storage[3] * _m4storage[5];
double det2_01_23 =
_m4storage[2] * _m4storage[7] - _m4storage[3] * _m4storage[6];
double det3_201_012 = _m4storage[8] * det2_01_12 -
_m4storage[9] * det2_01_02 +
_m4storage[10] * det2_01_01;
double det3_201_013 = _m4storage[8] * det2_01_13 -
_m4storage[9] * det2_01_03 +
_m4storage[11] * det2_01_01;
double det3_201_023 = _m4storage[8] * det2_01_23 -
_m4storage[10] * det2_01_03 +
_m4storage[11] * det2_01_02;
double det3_201_123 = _m4storage[9] * det2_01_23 -
_m4storage[10] * det2_01_13 +
_m4storage[11] * det2_01_12;
return -det3_201_123 * _m4storage[12] +
det3_201_023 * _m4storage[13] -
det3_201_013 * _m4storage[14] +
det3_201_012 * _m4storage[15];
}
/// Returns the dot product of row [i] and [v].
double dotRow(int i, Vector4 v) {
final vStorage = v._v4storage;
return _m4storage[i] * vStorage[0] +
_m4storage[4 + i] * vStorage[1] +
_m4storage[8 + i] * vStorage[2] +
_m4storage[12 + i] * vStorage[3];
}
/// Returns the dot product of column [j] and [v].
double dotColumn(int j, Vector4 v) {
final vStorage = v._v4storage;
return _m4storage[j * 4] * vStorage[0] +
_m4storage[j * 4 + 1] * vStorage[1] +
_m4storage[j * 4 + 2] * vStorage[2] +
_m4storage[j * 4 + 3] * vStorage[3];
}
/// Returns the trace of the matrix. The trace of a matrix is the sum of the
/// diagonal entries.
double trace() {
double t = 0.0;
t += _m4storage[0];
t += _m4storage[5];
t += _m4storage[10];
t += _m4storage[15];
return t;
}
/// Returns infinity norm of the matrix. Used for numerical analysis.
double infinityNorm() {
double norm = 0.0;
{
double row_norm = 0.0;
row_norm += _m4storage[0].abs();
row_norm += _m4storage[1].abs();
row_norm += _m4storage[2].abs();
row_norm += _m4storage[3].abs();
norm = row_norm > norm ? row_norm : norm;
}
{
double row_norm = 0.0;
row_norm += _m4storage[4].abs();
row_norm += _m4storage[5].abs();
row_norm += _m4storage[6].abs();
row_norm += _m4storage[7].abs();
norm = row_norm > norm ? row_norm : norm;
}
{
double row_norm = 0.0;
row_norm += _m4storage[8].abs();
row_norm += _m4storage[9].abs();
row_norm += _m4storage[10].abs();
row_norm += _m4storage[11].abs();
norm = row_norm > norm ? row_norm : norm;
}
{
double row_norm = 0.0;
row_norm += _m4storage[12].abs();
row_norm += _m4storage[13].abs();
row_norm += _m4storage[14].abs();
row_norm += _m4storage[15].abs();
norm = row_norm > norm ? row_norm : norm;
}
return norm;
}
/// Returns relative error between [this] and [correct]
double relativeError(Matrix4 correct) {
Matrix4 diff = correct - this;
double correct_norm = correct.infinityNorm();
double diff_norm = diff.infinityNorm();
return diff_norm / correct_norm;
}
/// Returns absolute error between [this] and [correct]
double absoluteError(Matrix4 correct) {
double this_norm = infinityNorm();
double correct_norm = correct.infinityNorm();
double diff_norm = (this_norm - correct_norm).abs();
return diff_norm;
}
/// Returns the translation vector from this homogeneous transformation matrix.
Vector3 getTranslation() {
double z = _m4storage[14];
double y = _m4storage[13];
double x = _m4storage[12];
return new Vector3(x, y, z);
}
/// Sets the translation vector in this homogeneous transformation matrix.
void setTranslation(Vector3 t) {
final tStorage = t._v3storage;
double z = tStorage[2];
double y = tStorage[1];
double x = tStorage[0];
_m4storage[14] = z;
_m4storage[13] = y;
_m4storage[12] = x;
}
/// Sets the translation vector in this homogeneous transformation matrix.
void setTranslationRaw(double x, double y, double z) {
_m4storage[14] = z;
_m4storage[13] = y;