-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmath.js
3168 lines (2921 loc) · 93.7 KB
/
math.js
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 2009, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileoverview This file contains matrix/vector math functions.
* It adds them to the "math" module on the o3djs object.
*
* o3djs.math supports a row-major and a column-major mode. In both
* modes, vectors are stored as arrays of numbers, and matrices are also.
*
* In row-major mode:
*
* - Rows of a matrix are neighboring <dimension> elements in the array.
* - Entries of a matrix M get accessed in M[row*dimension+column] fashion.
* - Tuples of coordinates adjacent are interpreted as row-vectors.
* - A vector v gets transformed by a matrix M by multiplying in the order v*M.
*
* In column-major mode:
*
* - Columns of a matrix are neighboring <dimension> elements in the array.
* - Entries of a matrix M get accessed in M[column*dimension+row] fashion.
* - Tuples of coordinates adjacent are interpreted as column-vectors.
* - A matrix M transforms a vector v by multiplying in the order M*v.
*
* When a function in o3djs.math requires separate row-major and
* column-major versions, a function with the same name gets added to each of
* the namespaces o3djs.math.rowMajor and o3djs.math.columnMajor. The
* function installRowMajorFunctions() or the function
* installColumnMajorFunctions() should get called during initialization to
* establish the mode. installRowMajorFunctions() works by iterating through
* the o3djs.math.rowMajor namespace and for each function foo, setting
* o3djs.math.foo equal to o3djs.math.rowMajor.foo.
* installRowMajorFunctions() works the same way, iterating over the columnMajor
* namespace. At the end of this file, we call installRowMajorFunctions().
*
* Switching modes changes two things. It changes how a matrix is encoded as an
* array, and it changes how the entries of a matrix get interpreted. Because
* those two things change together, the matrix representing a given
* transformation of space is the same JavaScript object in either mode.
* One consequence of this is that very few functions require separate row-major
* and column-major versions. Typically, a function requires separate versions
* only if it makes matrix multiplication order explicit, like
* mulMatrixMatrix(), mulMatrixVector(), or mulVectorMatrix(). Functions which
* create a new matrix, like scaling(), rotationZYX(), and translation() return
* the same JavaScript object in either mode, and functions which implicitly
* multiply like scale(), rotateZYX() and translate() modify the matrix in the
* same way in either mode.
*
* The convention choice made for math functions in this library is independent
* of the convention choice for how matrices get loaded into shaders. That
* convention is determined on a per-shader basis.
*
* Other utilities in o3djs should avoid making calls to functions that make
* multiplication order explicit. Instead they should appeal to functions like:
*
* o3djs.math.matrix4.transformPoint
* o3djs.math.matrix4.transformDirection
* o3djs.math.matrix4.transformNormal
* o3djs.math.matrix4.transformVector4
* o3djs.math.matrix4.composition
* o3djs.math.matrix4.compose
*
* These functions multiply matrices implicitly and internally choose the
* multiplication order to get the right result. That way, utilities which use
* o3djs.math work in either major mode. Note that this does not necessarily
* mean all sample code will work even if a line is added which switches major
* modes, but it does mean that calls to o3djs still do what they are supposed
* to.
*/
var use_plugin_math = false;
if ((typeof o3d != 'undefined') && o3d && o3d.Transform &&
o3d.Transform.makeIdentityMatrix4_) {
o3djs.provide('o3djs.math');
} else {
use_plugin_math = true;
o3djs.require('o3djs.plugin_math');
}
o3djs.provide("o3djs.flat_math");
/**
* A module for math for o3djs.math.
* @namespace
*/
o3djs.math = o3djs.math || {};
/**
* Functions which deal with 4-by-4 transformation matrices are kept in their
* own namespsace.
* @namespace
*/
o3djs.math.matrix4 = o3djs.math.matrix4 || {};
/**
* Functions that are specifically row major are kept in their own namespace.
* @namespace
*/
o3djs.math.rowMajor = o3djs.math.rowMajor || {};
/**
* Functions that are specifically column major are kept in their own namespace.
* @namespace
*/
o3djs.math.columnMajor = o3djs.math.columnMajor || {};
/**
* Functions that do error checking are stored in their own namespace.
* @namespace
*/
o3djs.math.errorCheck = o3djs.math.errorCheck || {};
/**
* Functions that do no error checking and have a separate version that does in
* o3djs.math.errorCheck are stored in their own namespace.
* @namespace
*/
o3djs.math.errorCheckFree = o3djs.math.errorCheckFree || {};
/**
* An Array of 2 floats
* @type {(!Array.<number>|!o3d.Float2)}
*/
o3djs.math.Vector2 = goog.typedef;
/**
* An Array of 3 floats
* @type {(!Array.<number>|!o3d.Float3)}
*/
o3djs.math.Vector3 = goog.typedef;
/**
* An Array of 4 floats
* @type {(!Array.<number>|!o3d.Float4)}
*/
o3djs.math.Vector4 = goog.typedef;
/**
* An Array of floats.
* @type {!Array.<number>}
*/
o3djs.math.Vector = goog.typedef;
/**
* A 1x1 Matrix of floats
* @type {!Array.<!Array.<number>>}
*/
o3djs.math.Matrix1 = goog.typedef;
/**
* A 2x2 Matrix of floats
* @type {!Array.<!Array.<number>>}
*/
o3djs.math.Matrix2 = goog.typedef;
/**
* A 3x3 Matrix of floats
* @type {!Array.<!Array.<number>>}
*/
o3djs.math.Matrix3 = goog.typedef;
/**
* A 4x4 Matrix of floats
* @type {(!Array.<!Array.<number>>|!o3d.Matrix4)}
*/
o3djs.math.Matrix4 = goog.typedef;
/**
* A arbitrary size Matrix of floats
* @type {(!Array.<!Array.<number>>|!o3d.Matrix4)}
*/
o3djs.math.Matrix = goog.typedef;
/**
* A module for math functions where a matrix is represented as a flat
* (1-dimensional) array.
* @namespace
*/
o3djs.flat_math = o3djs.flat_math || {};
/**
* A random seed for the pseudoRandom function.
* @private
* @type {number}
*/
o3djs.flat_math.randomSeed_ = 0;
/**
* A constant for the pseudoRandom function
* @private
* @type {number}
*/
o3djs.flat_math.RANDOM_RANGE_ = Math.pow(2, 32);
/**
* Functions which deal with 4-by-4 transformation matrices are kept in their
* own namespsace.
* @namespace
*/
o3djs.flat_math.matrix4 = o3djs.flat_math.matrix4 || {};
/**
* Functions that are specifically row major are kept in their own namespace.
* @namespace
*/
o3djs.flat_math.rowMajor = o3djs.flat_math.rowMajor || {};
/**
* Functions that are specifically column major are kept in their own namespace.
* @namespace
*/
o3djs.flat_math.columnMajor = o3djs.flat_math.columnMajor || {};
/**
* Functions that do error checking are stored in their own namespace.
* @namespace
*/
o3djs.flat_math.errorCheck = o3djs.flat_math.errorCheck || {};
/**
* Functions that do no error checking and have a separate version that does in
* o3djs.flat_math.errorCheck are stored in their own namespace.
* @namespace
*/
o3djs.flat_math.errorCheckFree = o3djs.flat_math.errorCheckFree || {};
/**
* An Float32Array of 2 floats
* @type {!Array.<number>}
*/
o3djs.flat_math.Vector2 = goog.typedef;
/**
* An Float32Array of 3 floats
* @type {!Array.<number>}
*/
o3djs.flat_math.Vector3 = goog.typedef;
/**
* An Float32Array of 4 floats
* @type {!Array.<number>}
*/
o3djs.flat_math.Vector4 = goog.typedef;
/**
* A 1x1 Matrix of floats
* @type {(!Array.<number>|Float32Array)}
*/
o3djs.flat_math.Matrix1 = goog.typedef;
/**
* A 2x2 Matrix of floats
* @type {(!Array.<number>|Float32Array)}
*/
o3djs.flat_math.Matrix2 = goog.typedef;
/**
* A 3x3 Matrix of floats
* @type {(!Array.<number>|Float32Array)}
*/
o3djs.flat_math.Matrix3 = goog.typedef;
/**
* A 4x4 Matrix of floats
* @type {(!Array.<number>|Float32Array)}
*/
o3djs.flat_math.Matrix4 = goog.typedef;
o3djs.flat_math.useFloat32Array_ = false;
/**
* A arbitrary size Matrix of floats
* @type {(!Array.<number>|Float32Array|o3djs.flat_math.Matrix1|
* o3djs.flat_math.Matrix2|o3djs.flat_math.Matrix3|o3djs.flat_math.Matrix4)}
*/
o3djs.flat_math.Matrix = goog.typedef;
/**
* A arbitrary size Matrix of floats
* @type {!Array.<number>}
*/
o3djs.flat_math.Vector = goog.typedef;
/**
* Namespace for Float32Array specific math functions.
* @namespace
*/
o3djs.flat_math.Float32Array = {};
/**
* A arbitrary size Matrix of floats
* @constructor
*/
o3djs.flat_math.Float32Array.Matrix = Float32Array;
/**
* A Float32Array.
* @constructor
*/
o3djs.flat_math.Float32Array.Vector = Float32Array;
/**
* If 16 arguments, this returns a 4x4 matrix
* with values set to the passed in arguments
* If 9 arguments returns a 3x3 matrix, if 4 arguments returns a 2x2 matrix.
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [0][2] element
* @param {number} d [0][3] element
* @param {number} e [1][0] element
* @param {number} f [1][1] element
* @param {number} g [1][2] element
* @param {number} h [1][3] element
* @param {number} i [2][0] element
* @param {number} j [2][1] element
* @param {number} k [2][2] element
* @param {number} l [2][3] element
* @param {number} m [3][0] element
* @param {number} n [3][1] element
* @param {number} o [3][2] element
* @param {number} p [3][3] element
* @returns {!o3djs.flat_math.Matrix}
*/
o3djs.flat_math.Float32Array.makeMatrix = function(
a, b, c, d,
e, f, g, h,
i, j, k, l,
m, n, o, p) {
if (p === undefined) {
if (i === undefined) {
var retval = new Float32Array(4);
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
return retval;
}
var retval = new Float32Array(9);
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
retval[4] = e;
retval[5] = f;
retval[6] = g;
retval[7] = h;
retval[8] = i;
return retval;
}
var retval = new Float32Array(16);
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
retval[4] = e;
retval[5] = f;
retval[6] = g;
retval[7] = h;
retval[8] = i;
retval[9] = j;
retval[10] = k;
retval[11] = l;
retval[12] = m;
retval[13] = n;
retval[14] = o;
retval[15] = p;
return retval;
};
/**
* returns a 2x2 matrix
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [1][0] element
* @param {number} d [1][1] element
* @returns {!o3djs.flat_math.Matrix}
*/
o3djs.flat_math.Float32Array.makeMatrix2 = function(a,b,
c,d) {
var retval = new Float32Array(4);
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
return retval;
};
/**
* If returns a 3x3 matrix
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [0][2] element
* @param {number} d [1][0] element
* @param {number} e [1][1] element
* @param {number} f [1][2] element
* @param {number} g [2][0] element
* @param {number} h [2][1] element
* @param {number} i [2][2] element
* @return {!o3djs.flat_math.Matrix} the matrix of the above elements
*/
o3djs.flat_math.Float32Array.makeMatrix3 = function(
a, b, c,
d, e, f,
g, h, i) {
var retval = new Float32Array(9);
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
retval[4] = e;
retval[5] = f;
retval[6] = g;
retval[7] = h;
retval[8] = i;
return retval;
};
/**
* returns a 4x4 matrix
* with values set to the passed in arguments
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [0][2] element
* @param {number} d [0][3] element
* @param {number} e [1][0] element
* @param {number} f [1][1] element
* @param {number} g [1][2] element
* @param {number} h [1][3] element
* @param {number} i [2][0] element
* @param {number} j [2][1] element
* @param {number} k [2][2] element
* @param {number} l [2][3] element
* @param {number} m [3][0] element
* @param {number} n [3][1] element
* @param {number} o [3][2] element
* @param {number} p [3][3] element
* @returns {o3djs.flat_math.Matrix} comprised of the above elements
*/
o3djs.flat_math.Float32Array.makeMatrix4 = function(
a, b, c, d,
e, f, g, h,
i, j, k, l,
m, n, o, p) {
var retval = new Float32Array(16);
retval[0] = a;
retval[1] = b;
retval[2] = c;
retval[3] = d;
retval[4] = e;
retval[5] = f;
retval[6] = g;
retval[7] = h;
retval[8] = i;
retval[9] = j;
retval[10] = k;
retval[11] = l;
retval[12] = m;
retval[13] = n;
retval[14] = o;
retval[15] = p;
return retval;
};
/**
* Namespace for Array specific math functions
*/
o3djs.flat_math.Array={};
/**
* A arbitrary size Matrix of floats
* @constructor
*/
o3djs.flat_math.Array.Matrix = Array;
/**
* An Float32Array of floats.
* @constructor
*/
o3djs.flat_math.Array.Vector = Array;
/**
* If 16 arguments, this returns a 4x4 matrix with values set to the passed
* in arguments. If 9 arguments returns a 3x3 matrix, if 4 arguments returns
* a 2x2 matrix
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [0][2] element
* @param {number} d [0][3] element
* @param {number} e [1][0] element
* @param {number} f [1][1] element
* @param {number} g [1][2] element
* @param {number} h [1][3] element
* @param {number} i [2][0] element
* @param {number} j [2][1] element
* @param {number} k [2][2] element
* @param {number} l [2][3] element
* @param {number} m [3][0] element
* @param {number} n [3][1] element
* @param {number} o [3][2] element
* @param {number} p [3][3] element
*/
o3djs.flat_math.Array.makeMatrix = function(
a, b, c, d,
e, f, g, h,
i, j, k, l,
m, n, o, p) {
if (p === undefined) {
if (i === undefined) {
return [a,b,c,d];
}
return [a, b, c, d, e, f, g, h, i];
}
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p];
};
/**
* returns a 2x2 matrix
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [1][0] element
* @param {number} d [1][1] element
* @returns {!o3djs.flat_math.Matrix}
*/
o3djs.flat_math.Array.makeMatrix2 = function(a, b, c, d) {
return [a, b, c, d];
};
/**
* If returns a 3x3 matrix
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [0][2] element
* @param {number} d [1][0] element
* @param {number} e [1][1] element
* @param {number} f [1][2] element
* @param {number} g [2][0] element
* @param {number} h [2][1] element
* @param {number} i [2][2] element
* @return {!o3djs.flat_math.Matrix} the matrix of the above elements
*/
o3djs.flat_math.Array.makeMatrix3 = function(
a, b, c,
d, e, f,
g, h, i) {
return [a, b, c, d, e, f, g, h, i];
};
/**
* returns a 4x4 matrix
* with values set to the passed in arguments
* @param {number} a [0][0] element
* @param {number} b [0][1] element
* @param {number} c [0][2] element
* @param {number} d [0][3] element
* @param {number} e [1][0] element
* @param {number} f [1][1] element
* @param {number} g [1][2] element
* @param {number} h [1][3] element
* @param {number} i [2][0] element
* @param {number} j [2][1] element
* @param {number} k [2][2] element
* @param {number} l [2][3] element
* @param {number} m [3][0] element
* @param {number} n [3][1] element
* @param {number} o [3][2] element
* @param {number} p [3][3] element
* @returns {o3djs.flat_math.Matrix} comprised of the above elements
*/
o3djs.flat_math.Array.makeMatrix4 = function(
a, b, c, d,
e, f, g, h,
i, j, k, l,
m, n, o, p) {
return [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p];
};
/**
* Helper function to copy functions from one namespace into another.
* @param {Object} source The source namespace.
* @param {Object} target The target namespace.
*/
o3djs.copyFunctions_ = function(source, target) {
for (var i in source) {
var value = source[i];
// If it's a function, copy it.
if (value.call) {
target[i] = value;
}
}
};
if (o3djs.flat_math.useFloat32Array_) {
o3djs.flat_math.Matrix = o3djs.flat_math.Float32Array.Matrix;
o3djs.flat_math.Vector = o3djs.flat_math.Float32Array.Vector;
o3djs.copyFunctions_(o3djs.flat_math.Float32Array, o3djs.flat_math);
} else {
o3djs.flat_math.Matrix = o3djs.flat_math.Array.Matrix;
o3djs.flat_math.Vector = o3djs.flat_math.Array.Vector;
o3djs.copyFunctions_(o3djs.flat_math.Array, o3djs.flat_math);
}
/**
* Returns a deterministic pseudorandom number between 0 and 1
* @return {number} a random number between 0 and 1
*/
o3djs.flat_math.pseudoRandom = function() {
var math = o3djs.flat_math;
return (math.randomSeed_ =
(134775813 * math.randomSeed_ + 1) %
math.RANDOM_RANGE_) / math.RANDOM_RANGE_;
};
/**
* Resets the pseudoRandom function sequence.
*/
o3djs.flat_math.resetPseudoRandom = function() {
o3djs.flat_math.randomSeed_ = 0;
};
/**
* Converts degrees to radians.
* @param {number} degrees A value in degrees.
* @return {number} the value in radians.
*/
o3djs.flat_math.degToRad = function(degrees) {
return degrees * Math.PI / 180;
};
/**
* Converts radians to degrees.
* @param {number} radians A value in radians.
* @return {number} the value in degrees.
*/
o3djs.flat_math.radToDeg = function(radians) {
return radians * 180 / Math.PI;
};
/**
* Performs linear interpolation on two scalars.
* Given scalars a and b and interpolation coefficient t, returns
* (1 - t) * a + t * b.
* @param {number} a Operand scalar.
* @param {number} b Operand scalar.
* @param {number} t Interpolation coefficient.
* @return {number} The weighted sum of a and b.
*/
o3djs.flat_math.lerpScalar = function(a, b, t) {
return (1 - t) * a + t * b;
};
/**
* Adds two vectors; assumes a and b have the same dimension.
* @param {!o3djs.flat_math.Vector} a Operand vector.
* @param {!o3djs.flat_math.Vector} b Operand vector.
* @return {!o3djs.flat_math.Vector} The sum of a and b.
*/
o3djs.flat_math.addVector = function(a, b) {
var aLength = a.length;
var r = new o3djs.flat_math.Vector(aLength);
for (var i = 0; i < aLength; ++i)
r[i] = a[i] + b[i];
return r;
};
/**
* Subtracts two vectors.
* @param {!o3djs.flat_math.Vector} a Operand vector.
* @param {!o3djs.flat_math.Vector} b Operand vector.
* @return {!o3djs.flat_math.Vector} The difference of a and b.
*/
o3djs.flat_math.subVector = function(a, b) {
var aLength = a.length;
var r = new o3djs.flat_math.Vector(aLength);
for (var i = 0; i < aLength; ++i)
r[i] = a[i] - b[i];
return r;
};
/**
* Subtracts two 3d vectors.
* @param {!o3djs.flat_math.Vector3} a Operand vector.
* @param {!o3djs.flat_math.Vector3} b Operand vector.
* @return {!o3djs.flat_math.Vector3} The difference of a and b.
*/
o3djs.flat_math.subVector3 = function(a, b) {
var r = new o3djs.flat_math.Vector(3);
for (var i = 0; i < 3; ++i)
r[i] = a[i] - b[i];
return r;
};
/**
* Performs linear interpolation on two vectors.
* Given vectors a and b and interpolation coefficient t, returns
* (1 - t) * a + t * b.
* @param {!o3djs.flat_math.Vector} a Operand vector.
* @param {!o3djs.flat_math.Vector} b Operand vector.
* @param {number} t Interpolation coefficient.
* @return {!o3djs.flat_math.Vector} The weighted sum of a and b.
*/
o3djs.flat_math.lerpVector = function(a, b, t) {
var aLength = a.length;
var r = new o3djs.flat_math.Vector(aLength);
for (var i = 0; i < aLength; ++i)
r[i] = (1 - t) * a[i] + t * b[i];
return r;
};
/**
* Clamps a value between 0 and range using a modulo.
* @param {number} v Value to clamp mod.
* @param {number} range Range to clamp to.
* @param {number} opt_rangeStart start of range. Default = 0.
* @return {number} Clamp modded value.
*/
o3djs.flat_math.modClamp = function(v, range, opt_rangeStart) {
var start = opt_rangeStart || 0;
if (range < 0.00001) {
return start;
}
v -= start;
if (v < 0) {
v -= Math.floor(v / range) * range;
} else {
v = v % range;
}
return v + start;
};
/**
* Lerps in a circle.
* Does a lerp between a and b but inside range so for example if
* range is 100, a is 95 and b is 5 lerping will go in the positive direction.
* @param {number} a Start value.
* @param {number} b Target value.
* @param {number} t Amount to lerp (0 to 1).
* @param {number} range Range of circle.
* @return {number} lerped result.
*/
o3djs.flat_math.lerpCircular = function(a, b, t, range) {
a = o3djs.flat_math.modClamp(a, range);
b = o3djs.flat_math.modClamp(b, range);
var delta = b - a;
if (Math.abs(delta) > range * 0.5) {
if (delta > 0) {
b -= range;
} else {
b += range;
}
}
return o3djs.flat_math.modClamp(o3djs.flat_math.lerpScalar(a, b, t), range);
};
/**
* Lerps radians.
* @param {number} a Start value.
* @param {number} b Target value.
* @param {number} t Amount to lerp (0 to 1).
* @return {number} lerped result.
*/
o3djs.flat_math.lerpRadian = function(a, b, t) {
return o3djs.flat_math.lerpCircular(a, b, t, Math.PI * 2);
};
/**
* Divides a vector by a scalar.
* @param {!o3djs.flat_math.Vector} v The vector.
* @param {number} k The scalar.
* @return {!o3djs.flat_math.Vector} v The vector v divided by k.
*/
o3djs.flat_math.divVectorScalar = function(v, k) {
var r = [];
var vLength = v.length;
for (var i = 0; i < vLength; ++i)
r[i] = v[i] / k;
return r;
};
/**
* Computes the dot product of two vectors; assumes that a and b have
* the same dimension.
* @param {!o3djs.flat_math.Vector} a Operand vector.
* @param {!o3djs.flat_math.Vector} b Operand vector.
* @return {number} The dot product of a and b.
*/
o3djs.flat_math.dot = function(a, b) {
var r = 0.0;
var aLength = a.length;
for (var i = 0; i < aLength; ++i)
r += a[i] * b[i];
return r;
};
/**
* Computes the cross product of two vectors; assumes both vectors have
* three entries.
* @param {!o3djs.flat_math.Vector} a Operand vector.
* @param {!o3djs.flat_math.Vector} b Operand vector.
* @return {!o3djs.flat_math.Vector} The vector a cross b.
*/
o3djs.flat_math.cross = function(a, b) {
var r = new o3djs.flat_math.Vector(3);
r[0] = a[1] * b[2] - a[2] * b[1];
r[1] = a[2] * b[0] - a[0] * b[2];
r[2] = a[0] * b[1] - a[1] * b[0];
return r;
};
/**
* Computes the Euclidean length of a vector, i.e. the square root of the
* sum of the squares of the entries.
* @param {!o3djs.flat_math.Vector} a The vector.
* @return {number} The length of a.
*/
o3djs.flat_math.length = function(a) {
var r = 0.0;
var aLength = a.length;
for (var i = 0; i < aLength; ++i)
r += a[i] * a[i];
return Math.sqrt(r);
};
/**
* Computes the square of the Euclidean length of a vector, i.e. the sum
* of the squares of the entries.
* @param {!o3djs.flat_math.Vector} a The vector.
* @return {number} The square of the length of a.
*/
o3djs.flat_math.lengthSquared = function(a) {
var r = 0.0;
var aLength = a.length;
for (var i = 0; i < aLength; ++i)
r += a[i] * a[i];
return r;
};
/**
* Computes the Euclidean distance between two vectors.
* @param {!o3djs.flat_math.Vector} a A vector.
* @param {!o3djs.flat_math.Vector} b A vector.
* @return {number} The distance between a and b.
*/
o3djs.flat_math.distance = function(a, b) {
var r = 0.0;
var aLength = a.length;
for (var i = 0; i < aLength; ++i) {
var t = a[i] - b[i];
r += t * t;
}
return Math.sqrt(r);
};
/**
* Computes the square of the Euclidean distance between two vectors.
* @param {!o3djs.flat_math.Vector} a A vector.
* @param {!o3djs.flat_math.Vector} b A vector.
* @return {number} The distance between a and b.
*/
o3djs.flat_math.distanceSquared = function(a, b) {
var r = 0.0;
var aLength = a.length;
for (var i = 0; i < aLength; ++i) {
var t = a[i] - b[i];
r += t * t;
}
return r;
};
/**
* Divides a vector by its Euclidean length and returns the quotient.
* @param {!o3djs.flat_math.Vector} a The vector.
* @return {!o3djs.flat_math.Vector} The normalized vector.
*/
o3djs.flat_math.normalize = function(a) {
var aLength = a.length;
var r = new o3djs.flat_math.Vector(aLength);
var n = 0.0;
var i;
for (i = 0; i < aLength; ++i)
n += a[i] * a[i];
n = Math.sqrt(n);
for (i = 0; i < aLength; ++i)
r[i] = a[i] / n;
return r;
};
/**
* Adds two matrices; assumes a and b are the same size.
* @param {!o3djs.flat_math.Matrix} a Operand matrix.
* @param {!o3djs.flat_math.Matrix} b Operand matrix.
* @return {!o3djs.flat_math.Matrix} The sum of a and b.
*/
o3djs.flat_math.addMatrix = function(a, b) {
var aLength = a.length;
var r = new o3djs.flat_math.Matrix(aLength);
for (var i = 0; i < aLength; ++i) {
r[i] = a[i] + b[i];
}
return r;
};
/**
* Subtracts two matrices; assumes a and b are the same size.
* @param {!o3djs.flat_math.Matrix} a Operand matrix.
* @param {!o3djs.flat_math.Matrix} b Operand matrix.
* @return {!o3djs.flat_math.Matrix} The sum of a and b.
*/
o3djs.flat_math.subMatrix = function(a, b) {
var aLength = a.length;
var r = new o3djs.flat_math.Matrix(aLength);
for (var i = 0; i < aLength; ++i) {
r[i] = a[i] - b[i];
}
return r;
};
/**
* Performs linear interpolation on two matrices.
* Given matrices a and b and interpolation coefficient t, returns
* (1 - t) * a + t * b.
* @param {!o3djs.flat_math.Matrix} a Operand matrix.
* @param {!o3djs.flat_math.Matrix} b Operand matrix.
* @param {number} t Interpolation coefficient.
* @return {!o3djs.flat_math.Matrix} Interpolated a and b.
*/
o3djs.flat_math.lerpMatrix = function(a, b, t) {
var aLength = a.length;
var r = new o3djs.flat_math.Matrix(aLength);
for (var i = 0; i < aLength; ++i) {
r[i] = (1 - t) * a[i] + t * b[i];
}
return r;
};
/**
* Divides a matrix by a scalar; assumes a and b are the same size.
* @param {!o3djs.flat_math.Matrix} a Operand matrix.
* @param {number} b scalar
* @return {!o3djs.flat_math.Matrix} The division of a by b.
*/
o3djs.flat_math.divMatrixScalar = function(a, b) {
var aLength = a.length;
var r = new o3djs.flat_math.Matrix(aLength);
for (var i = 0; i < aLength; ++i) {
r[i] = a[i] / b;
}
return r;
};
/**
* Negates a scalar.
* @param {number} a The scalar.
* @return {number} -a.
*/
o3djs.flat_math.negativeScalar = function(a) {
return -a;
};
/**
* Negates a vector.
* @param {!o3djs.flat_math.Vector} v The vector.
* @return {!o3djs.flat_math.Vector} -v.