-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathMatrix.js
More file actions
2013 lines (1779 loc) · 85 KB
/
Copy pathMatrix.js
File metadata and controls
2013 lines (1779 loc) · 85 KB
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 2003-2006, 2009, 2017, 2020 United States Government, as represented
* by the Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASAWorldWind/WebWorldWind platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License
* at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASAWorldWind/WebWorldWind also contains the following 3rd party Open Source
* software:
*
* ES6-Promise – under MIT License
* libtess.js – SGI Free Software License B
* Proj4 – under MIT License
* JSZip – under MIT License
*
* A complete listing of 3rd Party software notices and licenses included in
* WebWorldWind can be found in the WebWorldWind 3rd-party notices and licenses
* PDF found in code directory.
*/
/**
* @exports Matrix
*/
define([
'../geom/Angle',
'../error/ArgumentError',
'../util/Logger',
'../geom/Plane',
'../geom/Position',
'../geom/Rectangle',
'../render/Texture',
'../geom/Vec3',
'../util/WWMath'
],
function (Angle,
ArgumentError,
Logger,
Plane,
Position,
Rectangle,
Texture,
Vec3,
WWMath) {
"use strict";
/**
* Constructs a matrix.
* @alias Matrix
* @constructor
* @classdesc Represents a 4 x 4 double precision matrix stored in a Float64Array in row-major order.
* @param {Number} m11 matrix element at row 1, column 1.
* @param {Number} m12 matrix element at row 1, column 2.
* @param {Number} m13 matrix element at row 1, column 3.
* @param {Number} m14 matrix element at row 1, column 4.
* @param {Number} m21 matrix element at row 2, column 1.
* @param {Number} m22 matrix element at row 2, column 2.
* @param {Number} m23 matrix element at row 2, column 3.
* @param {Number} m24 matrix element at row 2, column 4.
* @param {Number} m31 matrix element at row 3, column 1.
* @param {Number} m32 matrix element at row 3, column 2.
* @param {Number} m33 matrix element at row 3, column 3.
* @param {Number} m34 matrix element at row 3, column 4.
* @param {Number} m41 matrix element at row 4, column 1.
* @param {Number} m42 matrix element at row 4, column 2.
* @param {Number} m43 matrix element at row 4, column 3.
* @param {Number} m44 matrix element at row 4, column 4.
*/
var Matrix = function (m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44) {
this[0] = m11;
this[1] = m12;
this[2] = m13;
this[3] = m14;
this[4] = m21;
this[5] = m22;
this[6] = m23;
this[7] = m24;
this[8] = m31;
this[9] = m32;
this[10] = m33;
this[11] = m34;
this[12] = m41;
this[13] = m42;
this[14] = m43;
this[15] = m44;
};
// Derives from Float64Array.
Matrix.prototype = new Float64Array(16);
/**
* Creates an identity matrix.
* @returns {Matrix} A new identity matrix.
*/
Matrix.fromIdentity = function () {
return new Matrix(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
};
/**
* Computes the principal axes of a point collection expressed in a typed array.
* @param {Float32Array} points The points for which to compute the axes,
* expressed as X0, Y0, Z0, X1, Y1, Z1, ...
* @param {Vec3} axis1 A vector in which to return the first (longest) principal axis.
* @param {Vec3} axis2 A vector in which to return the second (mid-length) principal axis.
* @param {Vec3} axis3 A vector in which to return the third (shortest) principal axis.
* @throws {ArgumentError} If the specified points array is null, undefined or empty, or one of the
* specified axes arguments is null or undefined.
*/
Matrix.principalAxesFromPoints = function (points, axis1, axis2, axis3) {
if (!points || points.length < 1) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "principalAxesFromPoints",
"missingPoints"));
}
if (!axis1 || !axis2 || !axis3) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "principalAxesFromPoints",
"An axis argument is null or undefined."));
}
// Compute the covariance matrix.
var covariance = Matrix.fromIdentity();
covariance.setToCovarianceOfPoints(points);
// Compute the eigenvectors from the covariance matrix. Since the covariance matrix is symmetric by
// definition, we can safely use the "symmetric" method below.
covariance.eigensystemFromSymmetricMatrix(axis1, axis2, axis3);
// Normalize the eigenvectors, which are already sorted in order from most prominent to least prominent.
axis1.normalize();
axis2.normalize();
axis3.normalize();
};
/**
* Sets the components of this matrix to specified values.
* @param {Number} m11 matrix element at row 1, column 1.
* @param {Number} m12 matrix element at row 1, column 2.
* @param {Number} m13 matrix element at row 1, column 3.
* @param {Number} m14 matrix element at row 1, column 4.
* @param {Number} m21 matrix element at row 2, column 1.
* @param {Number} m22 matrix element at row 2, column 2.
* @param {Number} m23 matrix element at row 2, column 3.
* @param {Number} m24 matrix element at row 2, column 4.
* @param {Number} m31 matrix element at row 3, column 1.
* @param {Number} m32 matrix element at row 3, column 2.
* @param {Number} m33 matrix element at row 3, column 3.
* @param {Number} m34 matrix element at row 3, column 4.
* @param {Number} m41 matrix element at row 4, column 1.
* @param {Number} m42 matrix element at row 4, column 2.
* @param {Number} m43 matrix element at row 4, column 3.
* @param {Number} m44 matrix element at row 4, column 4.
* @returns {Matrix} This matrix with its components set to the specified values.
*/
Matrix.prototype.set = function (m11, m12, m13, m14,
m21, m22, m23, m24,
m31, m32, m33, m34,
m41, m42, m43, m44) {
this[0] = m11;
this[1] = m12;
this[2] = m13;
this[3] = m14;
this[4] = m21;
this[5] = m22;
this[6] = m23;
this[7] = m24;
this[8] = m31;
this[9] = m32;
this[10] = m33;
this[11] = m34;
this[12] = m41;
this[13] = m42;
this[14] = m43;
this[15] = m44;
return this;
};
/**
* Sets this matrix to the identity matrix.
* @returns {Matrix} This matrix set to the identity matrix.
*/
Matrix.prototype.setToIdentity = function () {
this[0] = 1;
this[1] = 0;
this[2] = 0;
this[3] = 0;
this[4] = 0;
this[5] = 1;
this[6] = 0;
this[7] = 0;
this[8] = 0;
this[9] = 0;
this[10] = 1;
this[11] = 0;
this[12] = 0;
this[13] = 0;
this[14] = 0;
this[15] = 1;
};
/**
* Copies the components of a specified matrix to this matrix.
* @param {Matrix} matrix The matrix to copy.
* @returns {Matrix} This matrix set to the values of the specified matrix.
* @throws {ArgumentError} If the specified matrix is null or undefined.
*/
Matrix.prototype.copy = function (matrix) {
if (!matrix) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "copy", "missingMatrix"));
}
this[0] = matrix[0];
this[1] = matrix[1];
this[2] = matrix[2];
this[3] = matrix[3];
this[4] = matrix[4];
this[5] = matrix[5];
this[6] = matrix[6];
this[7] = matrix[7];
this[8] = matrix[8];
this[9] = matrix[9];
this[10] = matrix[10];
this[11] = matrix[11];
this[12] = matrix[12];
this[13] = matrix[13];
this[14] = matrix[14];
this[15] = matrix[15];
};
/**
* Creates a new matrix that is a copy of this matrix.
* @returns {Matrix} The new matrix.
*/
Matrix.prototype.clone = function () {
var clone = Matrix.fromIdentity();
clone.copy(this);
return clone;
};
/**
* Indicates whether the components of this matrix are equal to those of a specified matrix.
* @param {Matrix} matrix The matrix to test equality with. May be null or undefined, in which case this
* function returns false.
* @returns {boolean} true if all components of this matrix are equal to the corresponding
* components of the specified matrix, otherwise false.
*/
Matrix.prototype.equals = function (matrix) {
return matrix
&& this[0] == matrix[0]
&& this[1] == matrix[1]
&& this[2] == matrix[2]
&& this[3] == matrix[3]
&& this[4] == matrix[4]
&& this[5] == matrix[5]
&& this[6] == matrix[6]
&& this[7] == matrix[7]
&& this[8] == matrix[8]
&& this[9] == matrix[9]
&& this[10] == matrix[10]
&& this[11] == matrix[11]
&& this[12] == matrix[12]
&& this[13] == matrix[13]
&& this[14] == matrix[14]
&& this[15] == matrix[15];
};
/**
* Stores this matrix's components in column-major order in a specified array.
* <p>
* The array must have space for at least 16 elements. This matrix's components are stored in the array
* starting with row 0 column 0 in index 0, row 1 column 0 in index 1, row 2 column 0 in index 2, and so on.
*
* @param {Float32Array | Float64Array | Number[]} result An array of at least 16 elements. Upon return,
* contains this matrix's components in column-major.
* @returns {Float32Array} The specified result array.
* @throws {ArgumentError} If the specified result array in null or undefined.
*/
Matrix.prototype.columnMajorComponents = function (result) {
if (!result) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "columnMajorComponents", "missingResult"));
}
// Column 1
result[0] = this[0];
result[1] = this[4];
result[2] = this[8];
result[3] = this[12];
// Column 2
result[4] = this[1];
result[5] = this[5];
result[6] = this[9];
result[7] = this[13];
// Column 3
result[8] = this[2];
result[9] = this[6];
result[10] = this[10];
result[11] = this[14];
// Column 4
result[12] = this[3];
result[13] = this[7];
result[14] = this[11];
result[15] = this[15];
return result;
};
/**
* Sets this matrix to a translation matrix with specified translation components.
* @param {Number} x The X translation component.
* @param {Number} y The Y translation component.
* @param {Number} z The Z translation component.
* @returns {Matrix} This matrix with its translation components set to those specified and all other
* components set to that of an identity matrix.
*/
Matrix.prototype.setToTranslation = function (x, y, z) {
this[0] = 1;
this[1] = 0;
this[2] = 0;
this[3] = x;
this[4] = 0;
this[5] = 1;
this[6] = 0;
this[7] = y;
this[8] = 0;
this[9] = 0;
this[10] = 1;
this[11] = z;
this[12] = 0;
this[13] = 0;
this[14] = 0;
this[15] = 1;
return this;
};
/**
* Sets the translation components of this matrix to specified values.
* @param {Number} x The X translation component.
* @param {Number} y The Y translation component.
* @param {Number} z The Z translation component.
* @returns {Matrix} This matrix with its translation components set to the specified values and all other
* components unmodified.
*/
Matrix.prototype.setTranslation = function (x, y, z) {
this[3] = x;
this[7] = y;
this[11] = z;
return this;
};
/**
* Sets this matrix to a scale matrix with specified scale components.
* @param {Number} xScale The X scale component.
* @param {Number} yScale The Y scale component.
* @param {Number} zScale The Z scale component.
* @returns {Matrix} This matrix with its scale components set to those specified and all other
* components set to that of an identity matrix.
*/
Matrix.prototype.setToScale = function (xScale, yScale, zScale) {
this[0] = xScale;
this[1] = 0;
this[2] = 0;
this[3] = 0;
this[4] = 0;
this[5] = yScale;
this[6] = 0;
this[7] = 0;
this[8] = 0;
this[9] = 0;
this[10] = zScale;
this[11] = 0;
this[12] = 0;
this[13] = 0;
this[14] = 0;
this[15] = 1;
return this;
};
/**
* Sets the scale components of this matrix to specified values.
* @param {Number} xScale The X scale component.
* @param {Number} yScale The Y scale component.
* @param {Number} zScale The Z scale component.
* @returns {Matrix} This matrix with its scale components set to the specified values and all other
* components unmodified.
*/
Matrix.prototype.setScale = function (xScale, yScale, zScale) {
this[0] = xScale;
this[5] = yScale;
this[10] = zScale;
return this;
};
/**
* Sets this matrix to the transpose of a specified matrix.
* @param {Matrix} matrix The matrix whose transpose is to be copied.
* @returns {Matrix} This matrix, with its values set to the transpose of the specified matrix.
* @throws {ArgumentError} If the specified matrix in null or undefined.
*/
Matrix.prototype.setToTransposeOfMatrix = function (matrix) {
if (!matrix) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToTransposeOfMatrix", "missingMatrix"));
}
this[0] = matrix[0];
this[1] = matrix[4];
this[2] = matrix[8];
this[3] = matrix[12];
this[4] = matrix[1];
this[5] = matrix[5];
this[6] = matrix[9];
this[7] = matrix[13];
this[8] = matrix[2];
this[9] = matrix[6];
this[10] = matrix[10];
this[11] = matrix[14];
this[12] = matrix[3];
this[13] = matrix[7];
this[14] = matrix[11];
this[15] = matrix[15];
return this;
};
/**
* Sets this matrix to the matrix product of two specified matrices.
* @param {Matrix} matrixA The first matrix multiplicand.
* @param {Matrix} matrixB The second matrix multiplicand.
* @returns {Matrix} This matrix set to the product of matrixA x matrixB.
* @throws {ArgumentError} If either specified matrix is null or undefined.
*/
Matrix.prototype.setToMultiply = function (matrixA, matrixB) {
if (!matrixA || !matrixB) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToMultiply", "missingMatrix"));
}
var ma = matrixA,
mb = matrixB;
this[0] = ma[0] * mb[0] + ma[1] * mb[4] + ma[2] * mb[8] + ma[3] * mb[12];
this[1] = ma[0] * mb[1] + ma[1] * mb[5] + ma[2] * mb[9] + ma[3] * mb[13];
this[2] = ma[0] * mb[2] + ma[1] * mb[6] + ma[2] * mb[10] + ma[3] * mb[14];
this[3] = ma[0] * mb[3] + ma[1] * mb[7] + ma[2] * mb[11] + ma[3] * mb[15];
this[4] = ma[4] * mb[0] + ma[5] * mb[4] + ma[6] * mb[8] + ma[7] * mb[12];
this[5] = ma[4] * mb[1] + ma[5] * mb[5] + ma[6] * mb[9] + ma[7] * mb[13];
this[6] = ma[4] * mb[2] + ma[5] * mb[6] + ma[6] * mb[10] + ma[7] * mb[14];
this[7] = ma[4] * mb[3] + ma[5] * mb[7] + ma[6] * mb[11] + ma[7] * mb[15];
this[8] = ma[8] * mb[0] + ma[9] * mb[4] + ma[10] * mb[8] + ma[11] * mb[12];
this[9] = ma[8] * mb[1] + ma[9] * mb[5] + ma[10] * mb[9] + ma[11] * mb[13];
this[10] = ma[8] * mb[2] + ma[9] * mb[6] + ma[10] * mb[10] + ma[11] * mb[14];
this[11] = ma[8] * mb[3] + ma[9] * mb[7] + ma[10] * mb[11] + ma[11] * mb[15];
this[12] = ma[12] * mb[0] + ma[13] * mb[4] + ma[14] * mb[8] + ma[15] * mb[12];
this[13] = ma[12] * mb[1] + ma[13] * mb[5] + ma[14] * mb[9] + ma[15] * mb[13];
this[14] = ma[12] * mb[2] + ma[13] * mb[6] + ma[14] * mb[10] + ma[15] * mb[14];
this[15] = ma[12] * mb[3] + ma[13] * mb[7] + ma[14] * mb[11] + ma[15] * mb[15];
return this;
};
/**
* Sets this matrix to the symmetric covariance Matrix computed from the x, y, z coordinates of a specified
* points array.
* <p/>
* The computed covariance matrix represents the correlation between each pair of x-, y-, and z-coordinates as
* they're distributed about the point array's arithmetic mean. Its layout is as follows:
* <p/>
* <code> C(x, x) C(x, y) C(x, z) <br/> C(x, y) C(y, y) C(y, z) <br/> C(x, z) C(y, z) C(z, z) </code>
* <p/>
* C(i, j) is the covariance of coordinates i and j, where i or j are a coordinate's dispersion about its mean
* value. If any entry is zero, then there's no correlation between the two coordinates defining that entry. If the
* returned matrix is diagonal, then all three coordinates are uncorrelated, and the specified point is
* distributed evenly about its mean point.
* @param {Float32Array | Float64Array | Number[]} points The points to consider.
* @returns {Matrix} This matrix set to the covariance matrix for the specified list of points.
* @throws {ArgumentError} If the specified array of points is null, undefined or empty.
*/
Matrix.prototype.setToCovarianceOfPoints = function (points) {
if (!points || points.length < 1) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToCovarianceOfPoints", "missingArray"));
}
var mean,
dx,
dy,
dz,
count = 0,
c11 = 0,
c22 = 0,
c33 = 0,
c12 = 0,
c13 = 0,
c23 = 0,
vec = new Vec3(0, 0, 0);
mean = Vec3.averageOfBuffer(points, new Vec3(0, 0, 0));
for (var i = 0, len = points.length / 3; i < len; i++) {
vec[0] = points[i * 3];
vec[1] = points[i * 3 + 1];
vec[2] = points[i * 3 + 2];
dx = vec[0] - mean[0];
dy = vec[1] - mean[1];
dz = vec[2] - mean[2];
++count;
c11 += dx * dx;
c22 += dy * dy;
c33 += dz * dz;
c12 += dx * dy; // c12 = c21
c13 += dx * dz; // c13 = c31
c23 += dy * dz; // c23 = c32
}
// Row 1
this[0] = c11 / count;
this[1] = c12 / count;
this[2] = c13 / count;
this[3] = 0;
// Row 2
this[4] = c12 / count;
this[5] = c22 / count;
this[6] = c23 / count;
this[7] = 0;
// Row 3
this[8] = c13 / count;
this[9] = c23 / count;
this[10] = c33 / count;
this[11] = 0;
// Row 4
this[12] = 0;
this[13] = 0;
this[14] = 0;
this[15] = 0;
return this;
};
/**
* Multiplies this matrix by a translation matrix with specified translation values.
* @param {Number} x The X translation component.
* @param {Number} y The Y translation component.
* @param {Number} z The Z translation component.
* @returns {Matrix} This matrix multiplied by the translation matrix implied by the specified values.
*/
Matrix.prototype.multiplyByTranslation = function (x, y, z) {
this.multiply(
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1);
return this;
};
/**
* Multiplies this matrix by a rotation matrix about a specified axis and angle.
* @param {Number} x The X component of the rotation axis.
* @param {Number} y The Y component of the rotation axis.
* @param {Number} z The Z component of the rotation axis.
* @param {Number} angleDegrees The angle to rotate, in degrees.
* @returns {Matrix} This matrix multiplied by the rotation matrix implied by the specified values.
*/
Matrix.prototype.multiplyByRotation = function (x, y, z, angleDegrees) {
var c = Math.cos(angleDegrees * Angle.DEGREES_TO_RADIANS),
s = Math.sin(angleDegrees * Angle.DEGREES_TO_RADIANS);
this.multiply(
c + (1 - c) * x * x, (1 - c) * x * y - s * z, (1 - c) * x * z + s * y, 0,
(1 - c) * x * y + s * z, c + (1 - c) * y * y, (1 - c) * y * z - s * x, 0,
(1 - c) * x * z - s * y, (1 - c) * y * z + s * x, c + (1 - c) * z * z, 0,
0, 0, 0, 1);
return this;
};
/**
* Multiplies this matrix by a scale matrix with specified values.
* @param {Number} xScale The X scale component.
* @param {Number} yScale The Y scale component.
* @param {Number} zScale The Z scale component.
* @returns {Matrix} This matrix multiplied by the scale matrix implied by the specified values.
*/
Matrix.prototype.multiplyByScale = function (xScale, yScale, zScale) {
this.multiply(
xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, zScale, 0,
0, 0, 0, 1);
return this;
};
/**
* Sets this matrix to one that flips and shifts the y-axis.
* <p>
* The resultant matrix maps Y=0 to Y=1 and Y=1 to Y=0. All existing values are overwritten. This matrix is
* usually used to change the coordinate origin from an upper left coordinate origin to a lower left coordinate
* origin. This is typically necessary to align the coordinate system of images (top-left origin) with that of
* OpenGL (bottom-left origin).
* @returns {Matrix} This matrix set to values described above.
*/
Matrix.prototype.setToUnitYFlip = function () {
this[0] = 1;
this[1] = 0;
this[2] = 0;
this[3] = 0;
this[4] = 0;
this[5] = -1;
this[6] = 0;
this[7] = 1;
this[8] = 0;
this[9] = 0;
this[10] = 1;
this[11] = 0;
this[12] = 0;
this[13] = 0;
this[14] = 0;
this[15] = 1;
return this;
};
/**
* Multiplies this matrix by a local coordinate system transform for the specified globe.
* <p>
* The local coordinate system is defined such that the local origin (0, 0, 0) maps to the specified origin
* point, the z axis maps to the globe's surface normal at the point, the y-axis maps to the north pointing
* tangent, and the x-axis maps to the east pointing tangent.
*
* @param {Vec3} origin The local coordinate system origin, in model coordinates.
* @param {Globe} globe The globe the coordinate system is relative to.
*
* @throws {ArgumentError} If either argument is null or undefined.
*/
Matrix.prototype.multiplyByLocalCoordinateTransform = function (origin, globe) {
if (!origin) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByLocalCoordinateTransform",
"Origin vector is null or undefined"));
}
if (!globe) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByLocalCoordinateTransform",
"missingGlobe"));
}
var xAxis = new Vec3(0, 0, 0),
yAxis = new Vec3(0, 0, 0),
zAxis = new Vec3(0, 0, 0);
WWMath.localCoordinateAxesAtPoint(origin, globe, xAxis, yAxis, zAxis);
this.multiply(
xAxis[0], yAxis[0], zAxis[0], origin[0],
xAxis[1], yAxis[1], zAxis[1], origin[1],
xAxis[2], yAxis[2], zAxis[2], origin[2],
0, 0, 0, 1);
return this;
};
/**
* Multiplies this matrix by a texture transform for the specified texture.
* <p>
* A texture image transform maps the bottom-left corner of the texture's image data to coordinate [0,0] and maps the
* top-right of the texture's image data to coordinate [1,1]. This correctly handles textures whose image data has
* non-power-of-two dimensions, and correctly orients textures whose image data has its origin in the upper-left corner.
*
* @param {Texture} texture The texture to multiply a transform for.
*
* @throws {ArgumentError} If the texture is null or undefined.
*/
Matrix.prototype.multiplyByTextureTransform = function (texture) {
if (!texture) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByTextureTransform",
"missingTexture"));
}
// Compute the scale necessary to map the edge of the image data to the range [0,1]. When the texture contains
// power-of-two image data the scale is 1 and has no effect. Otherwise, the scale is computed such that the portion
// of the texture containing image data maps to the range [0,1].
var sx = texture.originalImageWidth / texture.imageWidth,
sy = texture.originalImageHeight / texture.imageHeight;
// Multiply this by a scaling matrix that maps the texture's image data to the range [0,1] and inverts the y axis.
// We have precomputed the result here in order to avoid an unnecessary matrix multiplication.
this.multiply(
sx, 0, 0, 0,
0, -sy, 0, sy,
0, 0, 1, 0,
0, 0, 0, 1);
return this;
};
/**
* Returns the translation components of this matrix.
* @param {Vec3} result A pre-allocated {@link Vec3} in which to return the translation components.
* @returns {Vec3} The specified result argument set to the translation components of this matrix.
* @throws {ArgumentError} If the specified result argument is null or undefined.
*/
Matrix.prototype.extractTranslation = function (result) {
if (!result) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "extractTranslation", "missingResult"));
}
result[0] = this[3];
result[1] = this[7];
result[2] = this[11];
return result;
};
/**
* Returns the rotation angles of this matrix.
* @param {Vec3} result A pre-allocated {@link Vec3} in which to return the rotation angles.
* @returns {Vec3} The specified result argument set to the rotation angles of this matrix. The angles are in
* degrees.
* @throws {ArgumentError} If the specified result argument is null or undefined.
*/
Matrix.prototype.extractRotationAngles = function (result) {
if (!result) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "extractRotationAngles", "missingResult"));
}
// Taken from Extracting Euler Angles from a Rotation Matrix by Mike Day, Insomniac Games.
// http://www.insomniacgames.com/mike-day-extracting-euler-angles-from-a-rotation-matrix/
var x = Math.atan2(this[6], this[10]),
y = Math.atan2(-this[2], Math.sqrt(this[0] * this[0] + this[1] * this[1])),
cx = Math.cos(x),
sx = Math.sin(x),
z = Math.atan2(sx * this[8] - cx * this[4], cx * this[5] - sx * this[9]);
result[0] = x * Angle.RADIANS_TO_DEGREES;
result[1] = y * Angle.RADIANS_TO_DEGREES;
result[2] = z * Angle.RADIANS_TO_DEGREES;
return result;
};
/**
* Multiplies this matrix by a first person viewing matrix for the specified globe.
* <p>
* A first person viewing matrix places the viewer's eye at the specified eyePosition. By default the viewer is looking
* straight down at the globe's surface from the eye position, with the globe's normal vector coming out of the screen
* and north pointing toward the top of the screen.
* <p>
* Heading specifies the viewer's azimuth, or its angle relative to North. Heading values range from -180 degrees to 180
* degrees. A heading of 0 degrees looks North, 90 degrees looks East, +-180 degrees looks South, and -90 degrees looks
* West.
* <p>
* Tilt specifies the viewer's angle relative to the surface. Tilt values range from -180 degrees to 180 degrees. A tilt
* of 0 degrees looks straight down at the globe's surface, 90 degrees looks at the horizon, and 180 degrees looks
* straight up. Tilt values greater than 180 degrees cause the viewer to turn upside down, and are therefore rarely used.
* <p>
* Roll specifies the viewer's angle relative to the horizon. Roll values range from -180 degrees to 180 degrees. A roll
* of 0 degrees orients the viewer so that up is pointing to the top of the screen, at 90 degrees up is pointing to the
* right, at +-180 degrees up is pointing to the bottom, and at -90 up is pointing to the left.
*
* @param {Position} eyePosition The viewer's geographic eye position relative to the specified globe.
* @param {Number} heading The viewer's angle relative to north, in degrees.
* @param {Number} tilt The viewer's angle relative to the surface, in degrees.
* @param {Number} roll The viewer's angle relative to the horizon, in degrees.
* @param {Globe} globe The globe the viewer is looking at.
*
* @throws {ArgumentError} If the specified position or globe is null or undefined.
*/
Matrix.prototype.multiplyByFirstPersonModelview = function (eyePosition, heading, tilt, roll, globe) {
if (!eyePosition) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByFirstPersonModelview", "missingPosition"));
}
if (!globe) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByFirstPersonModelview", "missingGlobe"));
}
var c,
s,
ex, ey, ez,
xx, xy, xz,
yx, yy, yz,
zx, zy, zz,
eyePoint = new Vec3(0, 0, 0),
xAxis = new Vec3(0, 0, 0),
yAxis = new Vec3(0, 0, 0),
zAxis = new Vec3(0, 0, 0);
// Roll. Rotate the eye point in a counter-clockwise direction about the z axis. Note that we invert the sines used
// in the rotation matrix in order to produce the counter-clockwise rotation. We invert only the cosines since
// sin(-a) = -sin(a) and cos(-a) = cos(a).
c = Math.cos(roll * Angle.DEGREES_TO_RADIANS);
s = Math.sin(roll * Angle.DEGREES_TO_RADIANS);
this.multiply(
c, s, 0, 0,
-s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Tilt. Rotate the eye point in a counter-clockwise direction about the x axis. Note that we invert the sines used
// in the rotation matrix in order to produce the counter-clockwise rotation. We invert only the cosines since
// sin(-a) = -sin(a) and cos(-a) = cos(a).
c = Math.cos(tilt * Angle.DEGREES_TO_RADIANS);
s = Math.sin(tilt * Angle.DEGREES_TO_RADIANS);
this.multiply(1, 0, 0, 0,
0, c, s, 0,
0, -s, c, 0,
0, 0, 0, 1);
// Heading. Rotate the eye point in a clockwise direction about the z axis again. This has a different effect than
// roll when tilt is non-zero because the viewer is no longer looking down the z axis.
c = Math.cos(heading * Angle.DEGREES_TO_RADIANS);
s = Math.sin(heading * Angle.DEGREES_TO_RADIANS);
this.multiply(c, -s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Compute the eye point in model coordinates. This point is mapped to the origin in the look at transform below.
globe.computePointFromPosition(eyePosition.latitude, eyePosition.longitude, eyePosition.altitude, eyePoint);
ex = eyePoint[0];
ey = eyePoint[1];
ez = eyePoint[2];
// Transform the origin to the local coordinate system at the eye point.
WWMath.localCoordinateAxesAtPoint(eyePoint, globe, xAxis, yAxis, zAxis);
xx = xAxis[0];
xy = xAxis[1];
xz = xAxis[2];
yx = yAxis[0];
yy = yAxis[1];
yz = yAxis[2];
zx = zAxis[0];
zy = zAxis[1];
zz = zAxis[2];
this.multiply(xx, xy, xz, -xx * ex - xy * ey - xz * ez,
yx, yy, yz, -yx * ex - yy * ey - yz * ez,
zx, zy, zz, -zx * ex - zy * ey - zz * ez,
0, 0, 0, 1);
return this;
};
/**
* Multiplies this matrix by a look at viewing matrix for the specified globe.
* <p>
* A look at viewing matrix places the center of the screen at the specified lookAtPosition. By default the viewer is
* looking straight down at the look at position from the specified range, with the globe's normal vector coming out of
* the screen and north pointing toward the top of the screen.
* <p>
* Range specifies the distance between the look at position and the viewer's eye point. Range values may be any positive
* real number. A range of 0 places the eye point at the look at point, while a positive range moves the eye point away
* from but still looking at the look at point.
* <p>
* Heading specifies the viewer's azimuth, or its angle relative to North. Heading values range from -180 degrees to 180
* degrees. A heading of 0 degrees looks North, 90 degrees looks East, +-180 degrees looks South, and -90 degrees looks
* West.
* <p>
* Tilt specifies the viewer's angle relative to the surface. Tilt values range from -180 degrees to 180 degrees. A tilt
* of 0 degrees looks straight down at the globe's surface, 90 degrees looks at the horizon, and 180 degrees looks
* straight up. Tilt values greater than 180 degrees cause the viewer to turn upside down, and are therefore rarely used.
* <p>
* Roll specifies the viewer's angle relative to the horizon. Roll values range from -180 degrees to 180 degrees. A roll
* of 0 degrees orients the viewer so that up is pointing to the top of the screen, at 90 degrees up is pointing to the
* right, at +-180 degrees up is pointing to the bottom, and at -90 up is pointing to the left.
*
* @param {Position} lookAtPosition The viewer's geographic look at position relative to the specified globe.
* @param {Number} range The distance between the eye point and the look at point, in model coordinates.
* @param {Number} heading The viewer's angle relative to north, in degrees.
* @param {Number} tilt The viewer's angle relative to the surface, in degrees.
* @param {Number} roll The viewer's angle relative to the horizon, in degrees.
* @param {Globe} globe The globe the viewer is looking at.
*
* @throws {ArgumentError} If either the specified look-at position or globe is null or undefined, or the
* specified range is less than zero.
*/
Matrix.prototype.multiplyByLookAtModelview = function (lookAtPosition, range, heading, tilt, roll, globe) {
if (!lookAtPosition) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByLookAtModelview", "missingPosition"));
}
if (range < 0) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByLookAtModelview",
"Range is less than zero"));
}
if (!globe) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "multiplyByLookAtModelview", "missingGlobe"));
}
// Translate the eye point along the positive z axis while keeping the look at point in the center of the viewport.
this.multiplyByTranslation(0, 0, -range);
// Transform the origin to the local coordinate system at the look at position, and rotate the viewer by the
// specified heading, tilt and roll.
this.multiplyByFirstPersonModelview(lookAtPosition, heading, tilt, roll, globe);
return this;
};
/**
* Sets this matrix to a perspective projection matrix for the specified viewport dimensions and clip distances.
* <p>
* A perspective projection matrix maps points in eye coordinates into clip coordinates in a way that causes
* distant objects to appear smaller, and preserves the appropriate depth information for each point. In model
* coordinates, a perspective projection is defined by frustum originating at the eye position and extending
* outward in the viewer's direction. The near distance and the far distance identify the minimum and maximum
* distance, respectively, at which an object in the scene is visible. Near and far distances must be positive
* and may not be equal.
*
* @param {Number} viewportWidth The viewport width, in screen coordinates.
* @param {Number} viewportHeight The viewport height, in screen coordinates.
* @param {Number} nearDistance The near clip plane distance, in model coordinates.
* @param {Number} farDistance The far clip plane distance, in model coordinates.
* @throws {ArgumentError} If the specified width or height is less than or equal to zero, if the near and far
* distances are equal, or if either the near or far distance are less than or equal to zero.
*/
Matrix.prototype.setToPerspectiveProjection = function (viewportWidth, viewportHeight, nearDistance, farDistance) {
if (viewportWidth <= 0) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToPerspectiveProjection",
"invalidWidth"));
}
if (viewportHeight <= 0) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToPerspectiveProjection",
"invalidHeight"));
}
if (nearDistance === farDistance) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToPerspectiveProjection",
"Near and far distance are the same."));
}
if (nearDistance <= 0 || farDistance <= 0) {
throw new ArgumentError(Logger.logMessage(Logger.LEVEL_SEVERE, "Matrix", "setToPerspectiveProjection",
"Near or far distance is less than or equal to zero."));
}
// Compute the dimensions of the viewport rectangle at the near distance.
var nearRect = WWMath.perspectiveFrustumRectangle(viewportWidth, viewportHeight, nearDistance),
left = nearRect.getMinX(),
right = nearRect.getMaxX(),
bottom = nearRect.getMinY(),
top = nearRect.getMaxY();
// Taken from Mathematics for 3D Game Programming and Computer Graphics, Second Edition, equation 4.52.
// Row 1
this[0] = 2 * nearDistance / (right - left);
this[1] = 0;
this[2] = (right + left) / (right - left);
this[3] = 0;
// Row 2
this[4] = 0;
this[5] = 2 * nearDistance / (top - bottom);
this[6] = (top + bottom) / (top - bottom);