-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
2271 lines (1800 loc) · 56.3 KB
/
build.js
File metadata and controls
2271 lines (1800 loc) · 56.3 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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
/**
* Safer Object.hasOwnProperty
*/
function hasOwn(obj, prop){
return Object.prototype.hasOwnProperty.call(obj, prop);
}
var hasOwn_1 = hasOwn;
var _hasDontEnumBug;
var _dontEnums;
function checkDontEnum(){
_dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
];
_hasDontEnumBug = true;
for (var key in {'toString': null}) {
_hasDontEnumBug = false;
}
}
/**
* Similar to Array/forEach but works over object properties and fixes Don't
* Enum bug on IE.
* based on: http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
*/
function forIn(obj, fn, thisObj){
var key, i = 0;
// no need to check if argument is a real object that way we can use
// it for arrays, functions, date, etc.
//post-pone check till needed
if (_hasDontEnumBug == null) { checkDontEnum(); }
for (key in obj) {
if (exec(fn, obj, key, thisObj) === false) {
break;
}
}
if (_hasDontEnumBug) {
var ctor = obj.constructor,
isProto = !!ctor && obj === ctor.prototype;
while (key = _dontEnums[i++]) {
// For constructor, if it is a prototype object the constructor
// is always non-enumerable unless defined otherwise (and
// enumerated above). For non-prototype objects, it will have
// to be defined on this object, since it cannot be defined on
// any prototype objects.
//
// For other [[DontEnum]] properties, check if the value is
// different than Object prototype value.
if (
(key !== 'constructor' ||
(!isProto && hasOwn_1(obj, key))) &&
obj[key] !== Object.prototype[key]
) {
if (exec(fn, obj, key, thisObj) === false) {
break;
}
}
}
}
}
function exec(fn, obj, key, thisObj){
return fn.call(thisObj, obj[key], key, obj);
}
var forIn_1 = forIn;
/**
* Similar to Array/forEach but works over object properties and fixes Don't
* Enum bug on IE.
* based on: http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
*/
function forOwn(obj, fn, thisObj){
forIn_1(obj, function(val, key){
if (hasOwn_1(obj, key)) {
return fn.call(thisObj, obj[key], key, obj);
}
});
}
var forOwn_1 = forOwn;
/**
* Combine properties from all the objects into first one.
* - This method affects target object in place, if you want to create a new Object pass an empty object as first param.
* @param {object} target Target Object
* @param {...object} objects Objects to be combined (0...n objects).
* @return {object} Target Object.
*/
function mixIn(target, objects){
var arguments$1 = arguments;
var i = 0,
n = arguments.length,
obj;
while(++i < n){
obj = arguments$1[i];
if (obj != null) {
forOwn_1(obj, copyProp, target);
}
}
return target;
}
function copyProp(val, key){
this[key] = val;
}
var mixIn_1 = mixIn;
/**
* Create Object using prototypal inheritance and setting custom properties.
* - Mix between Douglas Crockford Prototypal Inheritance <http://javascript.crockford.com/prototypal.html> and the EcmaScript 5 `Object.create()` method.
* @param {object} parent Parent Object.
* @param {object} [props] Object properties.
* @return {object} Created object.
*/
function createObject(parent, props){
function F(){}
F.prototype = parent;
return mixIn_1(new F(), props);
}
var createObject_1 = createObject;
var _rKind = /^\[object (.*)\]$/;
var _toString = Object.prototype.toString;
var UNDEF;
/**
* Gets the "kind" of value. (e.g. "String", "Number", etc)
*/
function kindOf(val) {
if (val === null) {
return 'Null';
} else if (val === UNDEF) {
return 'Undefined';
} else {
return _rKind.exec( _toString.call(val) )[1];
}
}
var kindOf_1 = kindOf;
var hasDescriptors = true;
try {
Object.defineProperty({}, "~", {});
Object.getOwnPropertyDescriptor({}, "~");
} catch (e){
hasDescriptors = false;
}
// we only need to be able to implement "toString" and "valueOf" in IE < 9
var hasEnumBug = !({valueOf: 0}).propertyIsEnumerable("valueOf");
var buggy = ["toString", "valueOf"];
var verbs = /^constructor|inherits|mixin$/;
var implement = function(proto){
var prototype = this.prototype;
for (var key in proto){
if (key.match(verbs)) { continue }
if (hasDescriptors){
var descriptor = Object.getOwnPropertyDescriptor(proto, key);
if (descriptor){
Object.defineProperty(prototype, key, descriptor);
continue
}
}
prototype[key] = proto[key];
}
if (hasEnumBug) { for (var i = 0; (key = buggy[i]); i++){
var value = proto[key];
if (value !== Object.prototype[key]) { prototype[key] = value; }
} }
return this
};
var prime = function(proto){
if (kindOf_1(proto) === "Function") { proto = {constructor: proto}; }
var superprime = proto.inherits;
// if our nice proto object has no own constructor property
// then we proceed using a ghosting constructor that all it does is
// call the parent's constructor if it has a superprime, else an empty constructor
// proto.constructor becomes the effective constructor
var constructor = (hasOwn_1(proto, "constructor")) ? proto.constructor : (superprime) ? function(){
return superprime.apply(this, arguments)
} : function(){};
if (superprime){
mixIn_1(constructor, superprime);
var superproto = superprime.prototype;
// inherit from superprime
var cproto = constructor.prototype = createObject_1(superproto);
// setting constructor.parent to superprime.prototype
// because it's the shortest possible absolute reference
constructor.parent = superproto;
cproto.constructor = constructor;
}
if (!constructor.implement) { constructor.implement = implement; }
var mixins = proto.mixin;
if (mixins){
if (kindOf_1(mixins) !== "Array") { mixins = [mixins]; }
for (var i = 0; i < mixins.length; i++) { constructor.implement(createObject_1(mixins[i].prototype)); }
}
// implement proto and return constructor
return constructor.implement(proto)
};
var index$1 = prime;
var Vector3 = index$1({
constructor: function Vector3(x, y, z) {
this[0] = x || 0;
this[1] = y || 0;
this[2] = z || 0;
},
clone: function() {
return new Vector3(this[0], this[1], this[2]);
},
get x() {
return this[0];
},
get y() {
return this[1];
},
get z() {
return this[2];
},
equals: function(v3) {
return this[0] === v3[0] && this[1] === v3[1] && this[2] === v3[2];
},
// three.js
length: function() {
return Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]);
},
// three.js
normalize: function() {
var length = this.length();
if (length === 0) { return new Vector3(); }
return new Vector3(this[0] / length, this[1] / length, this[2] / length);
},
// three.js
dot: function(v3) {
return this[0] * v3[0] + this[1] * v3[1] + this[2] * v3[2];
},
// three.js
cross: function(v3) {
var x = this[0], y = this[1], z = this[2];
return new Vector3(
y * v3[2] - z * v3[1],
z * v3[0] - x * v3[2],
x * v3[1] - y * v3[0]
);
},
lerp: function (v3, delta) {
var scale1 = delta;
var scale2 = 1 - delta;
return v3.combine(this, scale1, scale2);
},
// trasform_util
combine: function(v3, scale1, scale2) {
var this$1 = this;
var result = new Vector3;
for (var i = 0; i < 3; i++) { result[i] = this$1[i] * scale1 + v3[i] * scale2; }
return result;
},
});
var Vector3_1 = Vector3;
var degToRad = function(degrees) {
return degrees * Math.PI / 180;
};
var radToDeg = function(radians) {
return radians * 180 / Math.PI;
};
var Vector4 = index$1({
constructor: function Vector4(x, y, z, w) {
this[0] = x || 0;
this[1] = y || 0;
this[2] = z || 0;
this[3] = w || 0;
},
clone: function() {
return new Vector4(this[0], this[1], this[2], this[3]);
},
get x() {
return this[0];
},
get y() {
return this[1];
},
get z() {
return this[2];
},
get w() {
return this[3];
},
equals: function(v3) {
return this[0] === v3[0] && this[1] === v3[1] && this[2] === v3[2] && this[3] === v3[3];
},
// three.js
length: function() {
return Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2] + this[3] * this[3]);
},
// three.js
dot: function(v4) {
return this[0] * v4[0] + this[1] * v4[1] + this[2] * v4[2] + this[3] * v4[3];
},
// three.js
normalize: function() {
var length = this.length();
if (length === 0) { return new Vector4(0, 0, 0, 1); }
var inv = 1 / length;
return new Vector4(this[0] * inv, this[1] * inv, this[2] * inv, this[3] * inv);
},
// three.js
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
// deg indicates you want the angle in degrees in the resulting Vector4
quaternionToAngle: function(deg) {
var v4 = this;
// if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
if (v4[3] > 1) { v4 = v4.normalize(); }
var w = 2 * Math.acos(v4[3]);
var s = Math.sqrt(1 - v4[3] * v4[3]); // assuming quaternion normalised then w is less than 1, so term always positive.
if (s < 1e-4) { // test to avoid divide by zero, s is always positive due to sqrt
// if s close to zero then direction of axis not important
return new Vector4(v4[0], v4[1], v4[2], w);
} else {
// normalise axis
return new Vector4(v4[0] / s, v4[1] / s, v4[2] / s, deg ? radToDeg(w) : w);
}
},
// three.js
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
// deg indicates the Vector4 contains the angle in degrees
angleToQuaternion: function(deg) {
var angle = deg ? degToRad(this[3]) : this[3];
var half = angle / 2, s = Math.sin(half);
return new Vector4(this[0] * s, this[1] * s, this[2] * s, Math.cos(half));
},
// transform_util
combine: function(v4, scale1, scale2) {
var this$1 = this;
var result = new Vector4;
for (var i = 0; i < 4; i++) { result[i] = this$1[i] * scale1 + v4[i] * scale2; }
return result;
},
lerp: function (v4, delta) {
var scale1 = delta;
var scale2 = 1 - delta;
return v4.combine(this, scale1, scale2);
},
// transform_util
slerp: function(v4q, delta) {
var this$1 = this;
var interpolated = new Vector4;
var product = this.dot(v4q);
// Clamp product to -1.0 <= product <= 1.0.
product = Math.min(Math.max(product, -1), 1);
// Interpolate angles along the shortest path. For example, to interpolate
// between a 175 degree angle and a 185 degree angle, interpolate along the
// 10 degree path from 175 to 185, rather than along the 350 degree path in
// the opposite direction. This matches WebKit's implementation but not
// the current W3C spec. Fixing the spec to match this approach is discussed
// at:
// http://lists.w3.org/Archives/Public/www-style/2013May/0131.html
var scale1 = 1;
if (product < 0) {
product = -product;
scale1 = -1.0;
}
var epsilon = 1e-5;
if (Math.abs(product - 1.0) < epsilon) {
for (var i = 0; i < 4; ++i) { interpolated[i] = this$1[i]; }
return interpolated;
}
var denom = Math.sqrt(1 - product * product);
var theta = Math.acos(product);
var w = Math.sin(delta * theta) * (1 / denom);
scale1 *= Math.cos(delta * theta) - product * w;
var scale2 = w;
return this.combine(v4q, scale1, scale2);
}
});
var Vector4_1 = Vector4;
var stringify = function(array, places) {
if (places == null || places > 20) { places = 20; }
var strings = [];
for (var i = 0; i < array.length; i++) { strings[i] = array[i].toFixed(10).replace(/\.?0+$/, ''); }
return strings;
};
var TypeMask = {
Identity: 0,
Translate: 0x01, //!< set if the matrix has translation
Scale: 0x02, //!< set if the matrix has any scale != 1
Affine: 0x04, //!< set if the matrix skews or rotates
Perspective: 0x08, //!< set if the matrix is in perspective
All: 0xF,
Unknown: 0x80
};
var Matrix3d = index$1({
constructor: function Matrix3d() {
var this$1 = this;
// m.m11, m.m12, m.m13, m.m14,
// m.m21, m.m22, m.m23, m.m24,
// m.m31, m.m32, m.m33, m.m34,
// m.m41, m.m42, m.m43, m.m44
var values = arguments;
if (values.length === 1) { values = values[0]; } // matrix as list
if (!values.length) { values = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]; }
var i = 0, j, k = 0;
if (values.length === 6) { // 2d matrix
var a = values[0];
var b = values[1];
var c = values[2];
var d = values[3];
var e = values[4];
var f = values[5];
values = [
a, b, 0, 0,
c, d, 0, 0,
0, 0, 1, 0,
e, f, 0, 1
];
}
if (values.length !== 16) { throw new Error('invalid matrix'); }
// always 16
for (i = 0; i < 4; i++) {
var col = this$1[i] = [];
for (j = 0; j < 4; j++) {
col[j] = values[k++];
}
}
},
// get 2x3
get a() { return this.m11; },
get b() { return this.m12; },
get c() { return this.m21; },
get d() { return this.m22; },
get e() { return this.m41; },
get f() { return this.m42; },
// set 2x3
set a(value) { this.m11 = value; },
set b(value) { this.m12 = value; },
set c(value) { this.m21 = value; },
set d(value) { this.m22 = value; },
set e(value) { this.m41 = value; },
set f(value) { this.m42 = value; },
// get 4x4
get m11() { return this[0][0]; },
get m12() { return this[0][1]; },
get m13() { return this[0][2]; },
get m14() { return this[0][3]; },
get m21() { return this[1][0]; },
get m22() { return this[1][1]; },
get m23() { return this[1][2]; },
get m24() { return this[1][3]; },
get m31() { return this[2][0]; },
get m32() { return this[2][1]; },
get m33() { return this[2][2]; },
get m34() { return this[2][3]; },
get m41() { return this[3][0]; },
get m42() { return this[3][1]; },
get m43() { return this[3][2]; },
get m44() { return this[3][3]; },
// set 4x4
set m11(value) { this[0][0] = value; },
set m12(value) { this[0][1] = value; },
set m13(value) { this[0][2] = value; },
set m14(value) { this[0][3] = value; },
set m21(value) { this[1][0] = value; },
set m22(value) { this[1][1] = value; },
set m23(value) { this[1][2] = value; },
set m24(value) { this[1][3] = value; },
set m31(value) { this[2][0] = value; },
set m32(value) { this[2][1] = value; },
set m33(value) { this[2][2] = value; },
set m34(value) { this[2][3] = value; },
set m41(value) { this[3][0] = value; },
set m42(value) { this[3][1] = value; },
set m43(value) { this[3][2] = value; },
set m44(value) { this[3][3] = value; },
// get shortcuts
get transX() { return this[3][0]; },
get transY() { return this[3][1]; },
get transZ() { return this[3][2]; },
get scaleX() { return this[0][0]; },
get scaleY() { return this[1][1]; },
get scaleZ() { return this[2][2]; },
get perspX() { return this[0][3]; },
get perspY() { return this[1][3]; },
get perspZ() { return this[2][3]; },
// set shortcuts
set transX(value) { this[3][0] = value; },
set transY(value) { this[3][1] = value; },
set transZ(value) { this[3][2] = value; },
set scaleX(value) { this[0][0] = value; },
set scaleY(value) { this[1][1] = value; },
set scaleZ(value) { this[2][2] = value; },
set perspX(value) { this[0][3] = value; },
set perspY(value) { this[1][3] = value; },
set perspZ(value) { this[2][3] = value; },
// type getter
get type() {
var m = this;
var mask = 0;
if (0 !== m.perspX || 0 !== m.perspY || 0 !== m.perspZ || 1 !== m[3][3]) {
return TypeMask.Translate | TypeMask.Scale | TypeMask.Affine | TypeMask.Perspective;
}
if (0 !== m.transX || 0 !== m.transY || 0 !== m.transZ) {
mask |= TypeMask.Translate;
}
if (1 !== m.scaleX || 1 !== m.scaleY || 1 !== m.scaleZ) {
mask |= TypeMask.Scale;
}
if (0 !== m[1][0] || 0 !== m[0][1] || 0 !== m[0][2] ||
0 !== m[2][0] || 0 !== m[1][2] || 0 !== m[2][1]) {
mask |= TypeMask.Affine;
}
return mask;
},
// W3C
is2d: function() {
var m = this;
return m.m31 === 0 && m.m32 === 0 &&
m.m13 === 0 && m.m14 === 0 &&
m.m23 === 0 && m.m24 === 0 &&
m.m33 === 1 && m.m34 === 0 &&
m.m43 === 0 && m.m44 === 1;
},
equals: function(m2) {
var m1 = this;
return
m1.m11 === m2.m11 && m1.m12 === m2.m12 && m1.m13 === m2.m13 && m1.m14 === m2.m14 &&
m1.m21 === m2.m21 && m1.m22 === m2.m22 && m1.m23 === m2.m23 && m1.m24 === m2.m24 &&
m1.m31 === m2.m31 && m1.m32 === m2.m32 && m1.m33 === m2.m33 && m1.m34 === m2.m34 &&
m1.m41 === m2.m41 && m1.m42 === m2.m42 && m1.m43 === m2.m43 && m1.m44 === m2.m44;
},
clone: function() {
var m = this;
return new Matrix3d(
m.m11, m.m12, m.m13, m.m14,
m.m21, m.m22, m.m23, m.m24,
m.m31, m.m32, m.m33, m.m34,
m.m41, m.m42, m.m43, m.m44
);
},
/**
* Return true if the matrix is identity.
*/
isIdentity: function() {
return this.type === TypeMask.Identity;
},
/**
* Return true if the matrix contains translate or is identity.
*/
isTranslate: function() {
return !(this.type & ~TypeMask.Translate);
},
/**
* Return true if the matrix only contains scale or translate or is identity.
*/
isScaleTranslate: function() {
return !(this.type & ~(TypeMask.Scale | TypeMask.Translate));
},
concat: function(m2) {
var this$1 = this;
if (this.isIdentity()) { return m2.clone(); }
if (m2.isIdentity()) { return this.clone(); }
var m = new Matrix3d;
for (var j = 0; j < 4; j++) {
for (var i = 0; i < 4; i++) {
var value = 0;
for (var k = 0; k < 4; k++) {
value += this$1[k][i] * m2[j][k];
}
m[j][i] = value;
}
}
return m;
},
translate: function(v3) {
var translationMatrix = new Matrix3d;
translationMatrix.m41 = v3[0];
translationMatrix.m42 = v3[1];
translationMatrix.m43 = v3[2];
return this.concat(translationMatrix);
},
scale: function(v3) {
var m = new Matrix3d;
m.m11 = v3[0];
m.m22 = v3[1];
m.m33 = v3[2];
return this.concat(m);
},
rotate: function(v4q) {
var rotationMatrix = new Matrix3d;
var x = v4q[0];
var y = v4q[1];
var z = v4q[2];
var w = v4q[3];
rotationMatrix.m11 = 1 - 2 * (y * y + z * z);
rotationMatrix.m21 = 2 * (x * y - z * w);
rotationMatrix.m31 = 2 * (x * z + y * w);
rotationMatrix.m12 = 2 * (x * y + z * w);
rotationMatrix.m22 = 1 - 2 * (x * x + z * z);
rotationMatrix.m32 = 2 * (y * z - x * w);
rotationMatrix.m13 = 2 * (x * z - y * w);
rotationMatrix.m23 = 2 * (y * z + x * w);
rotationMatrix.m33 = 1 - 2 * (x * x + y * y);
return this.concat(rotationMatrix);
},
skew: function(v3) {
var skewMatrix = new Matrix3d;
skewMatrix[1][0] = v3[0];
skewMatrix[2][0] = v3[1];
skewMatrix[2][1] = v3[2];
return this.concat(skewMatrix);
},
perspective: function(v4) {
var perspectiveMatrix = new Matrix3d;
perspectiveMatrix.m14 = v4[0];
perspectiveMatrix.m24 = v4[1];
perspectiveMatrix.m34 = v4[2];
perspectiveMatrix.m44 = v4[3];
return this.concat(perspectiveMatrix);
},
map: function(v4) {
var this$1 = this;
var result = new Vector4_1;
for (var i = 0; i < 4; i++) {
var value = 0;
for (var j = 0; j < 4; j++) {
value += this$1[j][i] * v4[j];
}
result[i] = value;
}
return result;
},
determinant: function() {
if (this.isIdentity()) { return 1; }
if (this.isScaleTranslate()) { return this[0][0] * this[1][1] * this[2][2] * this[3][3]; }
var a00 = this[0][0];
var a01 = this[0][1];
var a02 = this[0][2];
var a03 = this[0][3];
var a10 = this[1][0];
var a11 = this[1][1];
var a12 = this[1][2];
var a13 = this[1][3];
var a20 = this[2][0];
var a21 = this[2][1];
var a22 = this[2][2];
var a23 = this[2][3];
var a30 = this[3][0];
var a31 = this[3][1];
var a32 = this[3][2];
var a33 = this[3][3];
var b00 = a00 * a11 - a01 * a10;
var b01 = a00 * a12 - a02 * a10;
var b02 = a00 * a13 - a03 * a10;
var b03 = a01 * a12 - a02 * a11;
var b04 = a01 * a13 - a03 * a11;
var b05 = a02 * a13 - a03 * a12;
var b06 = a20 * a31 - a21 * a30;
var b07 = a20 * a32 - a22 * a30;
var b08 = a20 * a33 - a23 * a30;
var b09 = a21 * a32 - a22 * a31;
var b10 = a21 * a33 - a23 * a31;
var b11 = a22 * a33 - a23 * a32;
// Calculate the determinant
return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
},
normalize: function() {
var this$1 = this;
var m44 = this.m44;
// Cannot normalize.
if (m44 === 0) { return false; }
var normalizedMatrix = new Matrix3d;
var scale = 1 / m44;
for (var i = 0; i < 4; i++)
{ for (var j = 0; j < 4; j++)
{ normalizedMatrix[j][i] = this$1[j][i] * scale; } }
return normalizedMatrix;
},
decompose: function() {
// We'll operate on a copy of the matrix.
var matrix = this.normalize();
// If we cannot normalize the matrix, then bail early as we cannot decompose.
if (!matrix) { return false; }
var perspectiveMatrix = matrix.clone();
var i, j;
for (i = 0; i < 3; i++) { perspectiveMatrix[i][3] = 0; }
perspectiveMatrix[3][3] = 1;
// If the perspective matrix is not invertible, we are also unable to
// decompose, so we'll bail early. Constant taken from SkMatrix44::invert.
if (Math.abs(perspectiveMatrix.determinant()) < 1e-8) { return false; }
var perspective;
if (matrix[0][3] !== 0 || matrix[1][3] !== 0 || matrix[2][3] !== 0) {
// rhs is the right hand side of the equation.
var rightHandSide = new Vector4_1(
matrix[0][3],
matrix[1][3],
matrix[2][3],
matrix[3][3]
);
// Solve the equation by inverting perspectiveMatrix and multiplying
// rightHandSide by the inverse.
var inversePerspectiveMatrix = perspectiveMatrix.invert();
if (!inversePerspectiveMatrix) { return false; }
var transposedInversePerspectiveMatrix = inversePerspectiveMatrix.transpose();
perspective = transposedInversePerspectiveMatrix.map(rightHandSide);
} else {
// No perspective.
perspective = new Vector4_1(0, 0, 0, 1);
}
var translate = new Vector3_1;
for (i = 0; i < 3; i++) { translate[i] = matrix[3][i]; }
var row = [];
for (i = 0; i < 3; i++) {
var v3 = row[i] = new Vector3_1;
for (j = 0; j < 3; ++j)
{ v3[j] = matrix[i][j]; }
}
// Compute X scale factor and normalize first row.
var scale = new Vector3_1;
scale[0] = row[0].length();
row[0] = row[0].normalize();
// Compute XY shear factor and make 2nd row orthogonal to 1st.
var skew = new Vector3_1;
skew[0] = row[0].dot(row[1]);
row[1] = row[1].combine(row[0], 1.0, -skew[0]);
// Now, compute Y scale and normalize 2nd row.
scale[1] = row[1].length();
row[1] = row[1].normalize();
skew[0] /= scale[1];
// Compute XZ and YZ shears, orthogonalize 3rd row
skew[1] = row[0].dot(row[2]);
row[2] = row[2].combine(row[0], 1.0, -skew[1]);
skew[2] = row[1].dot(row[2]);
row[2] = row[2].combine(row[1], 1.0, -skew[2]);
// Next, get Z scale and normalize 3rd row.
scale[2] = row[2].length();
row[2] = row[2].normalize();
skew[1] /= scale[2];
skew[2] /= scale[2];
// At this point, the matrix (in rows) is orthonormal.
// Check for a coordinate system flip. If the determinant
// is -1, then negate the matrix and the scaling factors.
var pdum3 = row[1].cross(row[2]);
if (row[0].dot(pdum3) < 0) {
for (i = 0; i < 3; i++) {
scale[i] *= -1;
for (j = 0; j < 3; ++j)
{ row[i][j] *= -1; }
}
}
var quaternion = new Vector4_1(
0.5 * Math.sqrt(Math.max(1 + row[0][0] - row[1][1] - row[2][2], 0)),
0.5 * Math.sqrt(Math.max(1 - row[0][0] + row[1][1] - row[2][2], 0)),
0.5 * Math.sqrt(Math.max(1 - row[0][0] - row[1][1] + row[2][2], 0)),
0.5 * Math.sqrt(Math.max(1 + row[0][0] + row[1][1] + row[2][2], 0))
);
if (row[2][1] > row[1][2]) { quaternion[0] = -quaternion[0]; }
if (row[0][2] > row[2][0]) { quaternion[1] = -quaternion[1]; }
if (row[1][0] > row[0][1]) { quaternion[2] = -quaternion[2]; }
return new DecomposedMatrix(perspective, translate, quaternion, skew, scale);
},
invert: function() {
var a00 = this[0][0];
var a01 = this[0][1];
var a02 = this[0][2];
var a03 = this[0][3];
var a10 = this[1][0];
var a11 = this[1][1];
var a12 = this[1][2];
var a13 = this[1][3];
var a20 = this[2][0];
var a21 = this[2][1];
var a22 = this[2][2];
var a23 = this[2][3];
var a30 = this[3][0];
var a31 = this[3][1];
var a32 = this[3][2];
var a33 = this[3][3];
var b00 = a00 * a11 - a01 * a10;
var b01 = a00 * a12 - a02 * a10;
var b02 = a00 * a13 - a03 * a10;
var b03 = a01 * a12 - a02 * a11;
var b04 = a01 * a13 - a03 * a11;
var b05 = a02 * a13 - a03 * a12;
var b06 = a20 * a31 - a21 * a30;
var b07 = a20 * a32 - a22 * a30;
var b08 = a20 * a33 - a23 * a30;
var b09 = a21 * a32 - a22 * a31;
var b10 = a21 * a33 - a23 * a31;
var b11 = a22 * a33 - a23 * a32;
// Calculate the determinant
var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
// If det is zero, we want to return false. However, we also want to return false
// if 1/det overflows to infinity (i.e. det is denormalized). Both of these are
// handled by checking that 1/det is finite.
if (det === 0 || !isFinite(det)) { return false; }
var invdet = 1.0 / det;