Derive rotor angles from bodies.
[featherfall2.git] / src / ext / gl-matrix.js
1 /**
2 * @fileoverview gl-matrix - High performance matrix and vector operations
3 * @author Brandon Jones
4 * @author Colin MacKenzie IV
5 * @version 2.2.1
6 */
7
8 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without modification,
11 are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29
30
31 (function(_global) {
32 "use strict";
33
34 var shim = {};
35 if (typeof(exports) === 'undefined') {
36 if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
37 shim.exports = {};
38 define(function() {
39 return shim.exports;
40 });
41 } else {
42 // gl-matrix lives in a browser, define its namespaces in global
43 shim.exports = typeof(window) !== 'undefined' ? window : _global;
44 }
45 }
46 else {
47 // gl-matrix lives in commonjs, define its namespaces in exports
48 shim.exports = exports;
49 }
50
51 (function(exports) {
52 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
53
54 Redistribution and use in source and binary forms, with or without modification,
55 are permitted provided that the following conditions are met:
56
57 * Redistributions of source code must retain the above copyright notice, this
58 list of conditions and the following disclaimer.
59 * Redistributions in binary form must reproduce the above copyright notice,
60 this list of conditions and the following disclaimer in the documentation
61 and/or other materials provided with the distribution.
62
63 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
64 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
67 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
70 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
73
74
75 if(!GLMAT_EPSILON) {
76 var GLMAT_EPSILON = 0.000001;
77 }
78
79 if(!GLMAT_ARRAY_TYPE) {
80 var GLMAT_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array;
81 }
82
83 if(!GLMAT_RANDOM) {
84 var GLMAT_RANDOM = Math.random;
85 }
86
87 /**
88 * @class Common utilities
89 * @name glMatrix
90 */
91 var glMatrix = {};
92
93 /**
94 * Sets the type of array used when creating new vectors and matricies
95 *
96 * @param {Type} type Array type, such as Float32Array or Array
97 */
98 glMatrix.setMatrixArrayType = function(type) {
99 GLMAT_ARRAY_TYPE = type;
100 }
101
102 if(typeof(exports) !== 'undefined') {
103 exports.glMatrix = glMatrix;
104 }
105
106 var degree = Math.PI / 180;
107
108 /**
109 * Convert Degree To Radian
110 *
111 * @param {Number} Angle in Degrees
112 */
113 glMatrix.toRadian = function(a){
114 return a * degree;
115 }
116 ;
117 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
118
119 Redistribution and use in source and binary forms, with or without modification,
120 are permitted provided that the following conditions are met:
121
122 * Redistributions of source code must retain the above copyright notice, this
123 list of conditions and the following disclaimer.
124 * Redistributions in binary form must reproduce the above copyright notice,
125 this list of conditions and the following disclaimer in the documentation
126 and/or other materials provided with the distribution.
127
128 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
129 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
130 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
131 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
132 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
133 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
134 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
135 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
136 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
137 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
138
139 /**
140 * @class 2 Dimensional Vector
141 * @name vec2
142 */
143
144 var vec2 = {};
145
146 /**
147 * Creates a new, empty vec2
148 *
149 * @returns {vec2} a new 2D vector
150 */
151 vec2.create = function() {
152 var out = new GLMAT_ARRAY_TYPE(2);
153 out[0] = 0;
154 out[1] = 0;
155 return out;
156 };
157
158 /**
159 * Creates a new vec2 initialized with values from an existing vector
160 *
161 * @param {vec2} a vector to clone
162 * @returns {vec2} a new 2D vector
163 */
164 vec2.clone = function(a) {
165 var out = new GLMAT_ARRAY_TYPE(2);
166 out[0] = a[0];
167 out[1] = a[1];
168 return out;
169 };
170
171 /**
172 * Creates a new vec2 initialized with the given values
173 *
174 * @param {Number} x X component
175 * @param {Number} y Y component
176 * @returns {vec2} a new 2D vector
177 */
178 vec2.fromValues = function(x, y) {
179 var out = new GLMAT_ARRAY_TYPE(2);
180 out[0] = x;
181 out[1] = y;
182 return out;
183 };
184
185 /**
186 * Copy the values from one vec2 to another
187 *
188 * @param {vec2} out the receiving vector
189 * @param {vec2} a the source vector
190 * @returns {vec2} out
191 */
192 vec2.copy = function(out, a) {
193 out[0] = a[0];
194 out[1] = a[1];
195 return out;
196 };
197
198 /**
199 * Set the components of a vec2 to the given values
200 *
201 * @param {vec2} out the receiving vector
202 * @param {Number} x X component
203 * @param {Number} y Y component
204 * @returns {vec2} out
205 */
206 vec2.set = function(out, x, y) {
207 out[0] = x;
208 out[1] = y;
209 return out;
210 };
211
212 /**
213 * Adds two vec2's
214 *
215 * @param {vec2} out the receiving vector
216 * @param {vec2} a the first operand
217 * @param {vec2} b the second operand
218 * @returns {vec2} out
219 */
220 vec2.add = function(out, a, b) {
221 out[0] = a[0] + b[0];
222 out[1] = a[1] + b[1];
223 return out;
224 };
225
226 /**
227 * Subtracts vector b from vector a
228 *
229 * @param {vec2} out the receiving vector
230 * @param {vec2} a the first operand
231 * @param {vec2} b the second operand
232 * @returns {vec2} out
233 */
234 vec2.subtract = function(out, a, b) {
235 out[0] = a[0] - b[0];
236 out[1] = a[1] - b[1];
237 return out;
238 };
239
240 /**
241 * Alias for {@link vec2.subtract}
242 * @function
243 */
244 vec2.sub = vec2.subtract;
245
246 /**
247 * Multiplies two vec2's
248 *
249 * @param {vec2} out the receiving vector
250 * @param {vec2} a the first operand
251 * @param {vec2} b the second operand
252 * @returns {vec2} out
253 */
254 vec2.multiply = function(out, a, b) {
255 out[0] = a[0] * b[0];
256 out[1] = a[1] * b[1];
257 return out;
258 };
259
260 /**
261 * Alias for {@link vec2.multiply}
262 * @function
263 */
264 vec2.mul = vec2.multiply;
265
266 /**
267 * Divides two vec2's
268 *
269 * @param {vec2} out the receiving vector
270 * @param {vec2} a the first operand
271 * @param {vec2} b the second operand
272 * @returns {vec2} out
273 */
274 vec2.divide = function(out, a, b) {
275 out[0] = a[0] / b[0];
276 out[1] = a[1] / b[1];
277 return out;
278 };
279
280 /**
281 * Alias for {@link vec2.divide}
282 * @function
283 */
284 vec2.div = vec2.divide;
285
286 /**
287 * Returns the minimum of two vec2's
288 *
289 * @param {vec2} out the receiving vector
290 * @param {vec2} a the first operand
291 * @param {vec2} b the second operand
292 * @returns {vec2} out
293 */
294 vec2.min = function(out, a, b) {
295 out[0] = Math.min(a[0], b[0]);
296 out[1] = Math.min(a[1], b[1]);
297 return out;
298 };
299
300 /**
301 * Returns the maximum of two vec2's
302 *
303 * @param {vec2} out the receiving vector
304 * @param {vec2} a the first operand
305 * @param {vec2} b the second operand
306 * @returns {vec2} out
307 */
308 vec2.max = function(out, a, b) {
309 out[0] = Math.max(a[0], b[0]);
310 out[1] = Math.max(a[1], b[1]);
311 return out;
312 };
313
314 /**
315 * Scales a vec2 by a scalar number
316 *
317 * @param {vec2} out the receiving vector
318 * @param {vec2} a the vector to scale
319 * @param {Number} b amount to scale the vector by
320 * @returns {vec2} out
321 */
322 vec2.scale = function(out, a, b) {
323 out[0] = a[0] * b;
324 out[1] = a[1] * b;
325 return out;
326 };
327
328 /**
329 * Adds two vec2's after scaling the second operand by a scalar value
330 *
331 * @param {vec2} out the receiving vector
332 * @param {vec2} a the first operand
333 * @param {vec2} b the second operand
334 * @param {Number} scale the amount to scale b by before adding
335 * @returns {vec2} out
336 */
337 vec2.scaleAndAdd = function(out, a, b, scale) {
338 out[0] = a[0] + (b[0] * scale);
339 out[1] = a[1] + (b[1] * scale);
340 return out;
341 };
342
343 /**
344 * Calculates the euclidian distance between two vec2's
345 *
346 * @param {vec2} a the first operand
347 * @param {vec2} b the second operand
348 * @returns {Number} distance between a and b
349 */
350 vec2.distance = function(a, b) {
351 var x = b[0] - a[0],
352 y = b[1] - a[1];
353 return Math.sqrt(x*x + y*y);
354 };
355
356 /**
357 * Alias for {@link vec2.distance}
358 * @function
359 */
360 vec2.dist = vec2.distance;
361
362 /**
363 * Calculates the squared euclidian distance between two vec2's
364 *
365 * @param {vec2} a the first operand
366 * @param {vec2} b the second operand
367 * @returns {Number} squared distance between a and b
368 */
369 vec2.squaredDistance = function(a, b) {
370 var x = b[0] - a[0],
371 y = b[1] - a[1];
372 return x*x + y*y;
373 };
374
375 /**
376 * Alias for {@link vec2.squaredDistance}
377 * @function
378 */
379 vec2.sqrDist = vec2.squaredDistance;
380
381 /**
382 * Calculates the length of a vec2
383 *
384 * @param {vec2} a vector to calculate length of
385 * @returns {Number} length of a
386 */
387 vec2.length = function (a) {
388 var x = a[0],
389 y = a[1];
390 return Math.sqrt(x*x + y*y);
391 };
392
393 /**
394 * Alias for {@link vec2.length}
395 * @function
396 */
397 vec2.len = vec2.length;
398
399 /**
400 * Calculates the squared length of a vec2
401 *
402 * @param {vec2} a vector to calculate squared length of
403 * @returns {Number} squared length of a
404 */
405 vec2.squaredLength = function (a) {
406 var x = a[0],
407 y = a[1];
408 return x*x + y*y;
409 };
410
411 /**
412 * Alias for {@link vec2.squaredLength}
413 * @function
414 */
415 vec2.sqrLen = vec2.squaredLength;
416
417 /**
418 * Negates the components of a vec2
419 *
420 * @param {vec2} out the receiving vector
421 * @param {vec2} a vector to negate
422 * @returns {vec2} out
423 */
424 vec2.negate = function(out, a) {
425 out[0] = -a[0];
426 out[1] = -a[1];
427 return out;
428 };
429
430 /**
431 * Normalize a vec2
432 *
433 * @param {vec2} out the receiving vector
434 * @param {vec2} a vector to normalize
435 * @returns {vec2} out
436 */
437 vec2.normalize = function(out, a) {
438 var x = a[0],
439 y = a[1];
440 var len = x*x + y*y;
441 if (len > 0) {
442 //TODO: evaluate use of glm_invsqrt here?
443 len = 1 / Math.sqrt(len);
444 out[0] = a[0] * len;
445 out[1] = a[1] * len;
446 }
447 return out;
448 };
449
450 /**
451 * Calculates the dot product of two vec2's
452 *
453 * @param {vec2} a the first operand
454 * @param {vec2} b the second operand
455 * @returns {Number} dot product of a and b
456 */
457 vec2.dot = function (a, b) {
458 return a[0] * b[0] + a[1] * b[1];
459 };
460
461 /**
462 * Computes the cross product of two vec2's
463 * Note that the cross product must by definition produce a 3D vector
464 *
465 * @param {vec3} out the receiving vector
466 * @param {vec2} a the first operand
467 * @param {vec2} b the second operand
468 * @returns {vec3} out
469 */
470 vec2.cross = function(out, a, b) {
471 var z = a[0] * b[1] - a[1] * b[0];
472 out[0] = out[1] = 0;
473 out[2] = z;
474 return out;
475 };
476
477 /**
478 * Performs a linear interpolation between two vec2's
479 *
480 * @param {vec2} out the receiving vector
481 * @param {vec2} a the first operand
482 * @param {vec2} b the second operand
483 * @param {Number} t interpolation amount between the two inputs
484 * @returns {vec2} out
485 */
486 vec2.lerp = function (out, a, b, t) {
487 var ax = a[0],
488 ay = a[1];
489 out[0] = ax + t * (b[0] - ax);
490 out[1] = ay + t * (b[1] - ay);
491 return out;
492 };
493
494 /**
495 * Generates a random vector with the given scale
496 *
497 * @param {vec2} out the receiving vector
498 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
499 * @returns {vec2} out
500 */
501 vec2.random = function (out, scale) {
502 scale = scale || 1.0;
503 var r = GLMAT_RANDOM() * 2.0 * Math.PI;
504 out[0] = Math.cos(r) * scale;
505 out[1] = Math.sin(r) * scale;
506 return out;
507 };
508
509 /**
510 * Transforms the vec2 with a mat2
511 *
512 * @param {vec2} out the receiving vector
513 * @param {vec2} a the vector to transform
514 * @param {mat2} m matrix to transform with
515 * @returns {vec2} out
516 */
517 vec2.transformMat2 = function(out, a, m) {
518 var x = a[0],
519 y = a[1];
520 out[0] = m[0] * x + m[2] * y;
521 out[1] = m[1] * x + m[3] * y;
522 return out;
523 };
524
525 /**
526 * Transforms the vec2 with a mat2d
527 *
528 * @param {vec2} out the receiving vector
529 * @param {vec2} a the vector to transform
530 * @param {mat2d} m matrix to transform with
531 * @returns {vec2} out
532 */
533 vec2.transformMat2d = function(out, a, m) {
534 var x = a[0],
535 y = a[1];
536 out[0] = m[0] * x + m[2] * y + m[4];
537 out[1] = m[1] * x + m[3] * y + m[5];
538 return out;
539 };
540
541 /**
542 * Transforms the vec2 with a mat3
543 * 3rd vector component is implicitly '1'
544 *
545 * @param {vec2} out the receiving vector
546 * @param {vec2} a the vector to transform
547 * @param {mat3} m matrix to transform with
548 * @returns {vec2} out
549 */
550 vec2.transformMat3 = function(out, a, m) {
551 var x = a[0],
552 y = a[1];
553 out[0] = m[0] * x + m[3] * y + m[6];
554 out[1] = m[1] * x + m[4] * y + m[7];
555 return out;
556 };
557
558 /**
559 * Transforms the vec2 with a mat4
560 * 3rd vector component is implicitly '0'
561 * 4th vector component is implicitly '1'
562 *
563 * @param {vec2} out the receiving vector
564 * @param {vec2} a the vector to transform
565 * @param {mat4} m matrix to transform with
566 * @returns {vec2} out
567 */
568 vec2.transformMat4 = function(out, a, m) {
569 var x = a[0],
570 y = a[1];
571 out[0] = m[0] * x + m[4] * y + m[12];
572 out[1] = m[1] * x + m[5] * y + m[13];
573 return out;
574 };
575
576 /**
577 * Perform some operation over an array of vec2s.
578 *
579 * @param {Array} a the array of vectors to iterate over
580 * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
581 * @param {Number} offset Number of elements to skip at the beginning of the array
582 * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
583 * @param {Function} fn Function to call for each vector in the array
584 * @param {Object} [arg] additional argument to pass to fn
585 * @returns {Array} a
586 * @function
587 */
588 vec2.forEach = (function() {
589 var vec = vec2.create();
590
591 return function(a, stride, offset, count, fn, arg) {
592 var i, l;
593 if(!stride) {
594 stride = 2;
595 }
596
597 if(!offset) {
598 offset = 0;
599 }
600
601 if(count) {
602 l = Math.min((count * stride) + offset, a.length);
603 } else {
604 l = a.length;
605 }
606
607 for(i = offset; i < l; i += stride) {
608 vec[0] = a[i]; vec[1] = a[i+1];
609 fn(vec, vec, arg);
610 a[i] = vec[0]; a[i+1] = vec[1];
611 }
612
613 return a;
614 };
615 })();
616
617 /**
618 * Returns a string representation of a vector
619 *
620 * @param {vec2} vec vector to represent as a string
621 * @returns {String} string representation of the vector
622 */
623 vec2.str = function (a) {
624 return 'vec2(' + a[0] + ', ' + a[1] + ')';
625 };
626
627 if(typeof(exports) !== 'undefined') {
628 exports.vec2 = vec2;
629 }
630 ;
631 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
632
633 Redistribution and use in source and binary forms, with or without modification,
634 are permitted provided that the following conditions are met:
635
636 * Redistributions of source code must retain the above copyright notice, this
637 list of conditions and the following disclaimer.
638 * Redistributions in binary form must reproduce the above copyright notice,
639 this list of conditions and the following disclaimer in the documentation
640 and/or other materials provided with the distribution.
641
642 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
643 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
644 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
645 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
646 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
647 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
648 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
649 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
650 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
651 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
652
653 /**
654 * @class 3 Dimensional Vector
655 * @name vec3
656 */
657
658 var vec3 = {};
659
660 /**
661 * Creates a new, empty vec3
662 *
663 * @returns {vec3} a new 3D vector
664 */
665 vec3.create = function() {
666 var out = new GLMAT_ARRAY_TYPE(3);
667 out[0] = 0;
668 out[1] = 0;
669 out[2] = 0;
670 return out;
671 };
672
673 /**
674 * Creates a new vec3 initialized with values from an existing vector
675 *
676 * @param {vec3} a vector to clone
677 * @returns {vec3} a new 3D vector
678 */
679 vec3.clone = function(a) {
680 var out = new GLMAT_ARRAY_TYPE(3);
681 out[0] = a[0];
682 out[1] = a[1];
683 out[2] = a[2];
684 return out;
685 };
686
687 /**
688 * Creates a new vec3 initialized with the given values
689 *
690 * @param {Number} x X component
691 * @param {Number} y Y component
692 * @param {Number} z Z component
693 * @returns {vec3} a new 3D vector
694 */
695 vec3.fromValues = function(x, y, z) {
696 var out = new GLMAT_ARRAY_TYPE(3);
697 out[0] = x;
698 out[1] = y;
699 out[2] = z;
700 return out;
701 };
702
703 /**
704 * Copy the values from one vec3 to another
705 *
706 * @param {vec3} out the receiving vector
707 * @param {vec3} a the source vector
708 * @returns {vec3} out
709 */
710 vec3.copy = function(out, a) {
711 out[0] = a[0];
712 out[1] = a[1];
713 out[2] = a[2];
714 return out;
715 };
716
717 /**
718 * Set the components of a vec3 to the given values
719 *
720 * @param {vec3} out the receiving vector
721 * @param {Number} x X component
722 * @param {Number} y Y component
723 * @param {Number} z Z component
724 * @returns {vec3} out
725 */
726 vec3.set = function(out, x, y, z) {
727 out[0] = x;
728 out[1] = y;
729 out[2] = z;
730 return out;
731 };
732
733 /**
734 * Adds two vec3's
735 *
736 * @param {vec3} out the receiving vector
737 * @param {vec3} a the first operand
738 * @param {vec3} b the second operand
739 * @returns {vec3} out
740 */
741 vec3.add = function(out, a, b) {
742 out[0] = a[0] + b[0];
743 out[1] = a[1] + b[1];
744 out[2] = a[2] + b[2];
745 return out;
746 };
747
748 /**
749 * Subtracts vector b from vector a
750 *
751 * @param {vec3} out the receiving vector
752 * @param {vec3} a the first operand
753 * @param {vec3} b the second operand
754 * @returns {vec3} out
755 */
756 vec3.subtract = function(out, a, b) {
757 out[0] = a[0] - b[0];
758 out[1] = a[1] - b[1];
759 out[2] = a[2] - b[2];
760 return out;
761 };
762
763 /**
764 * Alias for {@link vec3.subtract}
765 * @function
766 */
767 vec3.sub = vec3.subtract;
768
769 /**
770 * Multiplies two vec3's
771 *
772 * @param {vec3} out the receiving vector
773 * @param {vec3} a the first operand
774 * @param {vec3} b the second operand
775 * @returns {vec3} out
776 */
777 vec3.multiply = function(out, a, b) {
778 out[0] = a[0] * b[0];
779 out[1] = a[1] * b[1];
780 out[2] = a[2] * b[2];
781 return out;
782 };
783
784 /**
785 * Alias for {@link vec3.multiply}
786 * @function
787 */
788 vec3.mul = vec3.multiply;
789
790 /**
791 * Divides two vec3's
792 *
793 * @param {vec3} out the receiving vector
794 * @param {vec3} a the first operand
795 * @param {vec3} b the second operand
796 * @returns {vec3} out
797 */
798 vec3.divide = function(out, a, b) {
799 out[0] = a[0] / b[0];
800 out[1] = a[1] / b[1];
801 out[2] = a[2] / b[2];
802 return out;
803 };
804
805 /**
806 * Alias for {@link vec3.divide}
807 * @function
808 */
809 vec3.div = vec3.divide;
810
811 /**
812 * Returns the minimum of two vec3's
813 *
814 * @param {vec3} out the receiving vector
815 * @param {vec3} a the first operand
816 * @param {vec3} b the second operand
817 * @returns {vec3} out
818 */
819 vec3.min = function(out, a, b) {
820 out[0] = Math.min(a[0], b[0]);
821 out[1] = Math.min(a[1], b[1]);
822 out[2] = Math.min(a[2], b[2]);
823 return out;
824 };
825
826 /**
827 * Returns the maximum of two vec3's
828 *
829 * @param {vec3} out the receiving vector
830 * @param {vec3} a the first operand
831 * @param {vec3} b the second operand
832 * @returns {vec3} out
833 */
834 vec3.max = function(out, a, b) {
835 out[0] = Math.max(a[0], b[0]);
836 out[1] = Math.max(a[1], b[1]);
837 out[2] = Math.max(a[2], b[2]);
838 return out;
839 };
840
841 /**
842 * Scales a vec3 by a scalar number
843 *
844 * @param {vec3} out the receiving vector
845 * @param {vec3} a the vector to scale
846 * @param {Number} b amount to scale the vector by
847 * @returns {vec3} out
848 */
849 vec3.scale = function(out, a, b) {
850 out[0] = a[0] * b;
851 out[1] = a[1] * b;
852 out[2] = a[2] * b;
853 return out;
854 };
855
856 /**
857 * Adds two vec3's after scaling the second operand by a scalar value
858 *
859 * @param {vec3} out the receiving vector
860 * @param {vec3} a the first operand
861 * @param {vec3} b the second operand
862 * @param {Number} scale the amount to scale b by before adding
863 * @returns {vec3} out
864 */
865 vec3.scaleAndAdd = function(out, a, b, scale) {
866 out[0] = a[0] + (b[0] * scale);
867 out[1] = a[1] + (b[1] * scale);
868 out[2] = a[2] + (b[2] * scale);
869 return out;
870 };
871
872 /**
873 * Calculates the euclidian distance between two vec3's
874 *
875 * @param {vec3} a the first operand
876 * @param {vec3} b the second operand
877 * @returns {Number} distance between a and b
878 */
879 vec3.distance = function(a, b) {
880 var x = b[0] - a[0],
881 y = b[1] - a[1],
882 z = b[2] - a[2];
883 return Math.sqrt(x*x + y*y + z*z);
884 };
885
886 /**
887 * Alias for {@link vec3.distance}
888 * @function
889 */
890 vec3.dist = vec3.distance;
891
892 /**
893 * Calculates the squared euclidian distance between two vec3's
894 *
895 * @param {vec3} a the first operand
896 * @param {vec3} b the second operand
897 * @returns {Number} squared distance between a and b
898 */
899 vec3.squaredDistance = function(a, b) {
900 var x = b[0] - a[0],
901 y = b[1] - a[1],
902 z = b[2] - a[2];
903 return x*x + y*y + z*z;
904 };
905
906 /**
907 * Alias for {@link vec3.squaredDistance}
908 * @function
909 */
910 vec3.sqrDist = vec3.squaredDistance;
911
912 /**
913 * Calculates the length of a vec3
914 *
915 * @param {vec3} a vector to calculate length of
916 * @returns {Number} length of a
917 */
918 vec3.length = function (a) {
919 var x = a[0],
920 y = a[1],
921 z = a[2];
922 return Math.sqrt(x*x + y*y + z*z);
923 };
924
925 /**
926 * Alias for {@link vec3.length}
927 * @function
928 */
929 vec3.len = vec3.length;
930
931 /**
932 * Calculates the squared length of a vec3
933 *
934 * @param {vec3} a vector to calculate squared length of
935 * @returns {Number} squared length of a
936 */
937 vec3.squaredLength = function (a) {
938 var x = a[0],
939 y = a[1],
940 z = a[2];
941 return x*x + y*y + z*z;
942 };
943
944 /**
945 * Alias for {@link vec3.squaredLength}
946 * @function
947 */
948 vec3.sqrLen = vec3.squaredLength;
949
950 /**
951 * Negates the components of a vec3
952 *
953 * @param {vec3} out the receiving vector
954 * @param {vec3} a vector to negate
955 * @returns {vec3} out
956 */
957 vec3.negate = function(out, a) {
958 out[0] = -a[0];
959 out[1] = -a[1];
960 out[2] = -a[2];
961 return out;
962 };
963
964 /**
965 * Normalize a vec3
966 *
967 * @param {vec3} out the receiving vector
968 * @param {vec3} a vector to normalize
969 * @returns {vec3} out
970 */
971 vec3.normalize = function(out, a) {
972 var x = a[0],
973 y = a[1],
974 z = a[2];
975 var len = x*x + y*y + z*z;
976 if (len > 0) {
977 //TODO: evaluate use of glm_invsqrt here?
978 len = 1 / Math.sqrt(len);
979 out[0] = a[0] * len;
980 out[1] = a[1] * len;
981 out[2] = a[2] * len;
982 }
983 return out;
984 };
985
986 /**
987 * Calculates the dot product of two vec3's
988 *
989 * @param {vec3} a the first operand
990 * @param {vec3} b the second operand
991 * @returns {Number} dot product of a and b
992 */
993 vec3.dot = function (a, b) {
994 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
995 };
996
997 /**
998 * Computes the cross product of two vec3's
999 *
1000 * @param {vec3} out the receiving vector
1001 * @param {vec3} a the first operand
1002 * @param {vec3} b the second operand
1003 * @returns {vec3} out
1004 */
1005 vec3.cross = function(out, a, b) {
1006 var ax = a[0], ay = a[1], az = a[2],
1007 bx = b[0], by = b[1], bz = b[2];
1008
1009 out[0] = ay * bz - az * by;
1010 out[1] = az * bx - ax * bz;
1011 out[2] = ax * by - ay * bx;
1012 return out;
1013 };
1014
1015 /**
1016 * Performs a linear interpolation between two vec3's
1017 *
1018 * @param {vec3} out the receiving vector
1019 * @param {vec3} a the first operand
1020 * @param {vec3} b the second operand
1021 * @param {Number} t interpolation amount between the two inputs
1022 * @returns {vec3} out
1023 */
1024 vec3.lerp = function (out, a, b, t) {
1025 var ax = a[0],
1026 ay = a[1],
1027 az = a[2];
1028 out[0] = ax + t * (b[0] - ax);
1029 out[1] = ay + t * (b[1] - ay);
1030 out[2] = az + t * (b[2] - az);
1031 return out;
1032 };
1033
1034 /**
1035 * Generates a random vector with the given scale
1036 *
1037 * @param {vec3} out the receiving vector
1038 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
1039 * @returns {vec3} out
1040 */
1041 vec3.random = function (out, scale) {
1042 scale = scale || 1.0;
1043
1044 var r = GLMAT_RANDOM() * 2.0 * Math.PI;
1045 var z = (GLMAT_RANDOM() * 2.0) - 1.0;
1046 var zScale = Math.sqrt(1.0-z*z) * scale;
1047
1048 out[0] = Math.cos(r) * zScale;
1049 out[1] = Math.sin(r) * zScale;
1050 out[2] = z * scale;
1051 return out;
1052 };
1053
1054 /**
1055 * Transforms the vec3 with a mat4.
1056 * 4th vector component is implicitly '1'
1057 *
1058 * @param {vec3} out the receiving vector
1059 * @param {vec3} a the vector to transform
1060 * @param {mat4} m matrix to transform with
1061 * @returns {vec3} out
1062 */
1063 vec3.transformMat4 = function(out, a, m) {
1064 var x = a[0], y = a[1], z = a[2];
1065 out[0] = m[0] * x + m[4] * y + m[8] * z + m[12];
1066 out[1] = m[1] * x + m[5] * y + m[9] * z + m[13];
1067 out[2] = m[2] * x + m[6] * y + m[10] * z + m[14];
1068 return out;
1069 };
1070
1071 /**
1072 * Transforms the vec3 with a mat3.
1073 *
1074 * @param {vec3} out the receiving vector
1075 * @param {vec3} a the vector to transform
1076 * @param {mat4} m the 3x3 matrix to transform with
1077 * @returns {vec3} out
1078 */
1079 vec3.transformMat3 = function(out, a, m) {
1080 var x = a[0], y = a[1], z = a[2];
1081 out[0] = x * m[0] + y * m[3] + z * m[6];
1082 out[1] = x * m[1] + y * m[4] + z * m[7];
1083 out[2] = x * m[2] + y * m[5] + z * m[8];
1084 return out;
1085 };
1086
1087 /**
1088 * Transforms the vec3 with a quat
1089 *
1090 * @param {vec3} out the receiving vector
1091 * @param {vec3} a the vector to transform
1092 * @param {quat} q quaternion to transform with
1093 * @returns {vec3} out
1094 */
1095 vec3.transformQuat = function(out, a, q) {
1096 // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations
1097
1098 var x = a[0], y = a[1], z = a[2],
1099 qx = q[0], qy = q[1], qz = q[2], qw = q[3],
1100
1101 // calculate quat * vec
1102 ix = qw * x + qy * z - qz * y,
1103 iy = qw * y + qz * x - qx * z,
1104 iz = qw * z + qx * y - qy * x,
1105 iw = -qx * x - qy * y - qz * z;
1106
1107 // calculate result * inverse quat
1108 out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
1109 out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
1110 out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
1111 return out;
1112 };
1113
1114 /*
1115 * Rotate a 3D vector around the x-axis
1116 * @param {vec3} out The receiving vec3
1117 * @param {vec3} a The vec3 point to rotate
1118 * @param {vec3} b The origin of the rotation
1119 * @param {Number} c The angle of rotation
1120 * @returns {vec3} out
1121 */
1122 vec3.rotateX = function(out, a, b, c){
1123 var p = [], r=[];
1124 //Translate point to the origin
1125 p[0] = a[0] - b[0];
1126 p[1] = a[1] - b[1];
1127 p[2] = a[2] - b[2];
1128
1129 //perform rotation
1130 r[0] = p[0];
1131 r[1] = p[1]*Math.cos(c) - p[2]*Math.sin(c);
1132 r[2] = p[1]*Math.sin(c) + p[2]*Math.cos(c);
1133
1134 //translate to correct position
1135 out[0] = r[0] + b[0];
1136 out[1] = r[1] + b[1];
1137 out[2] = r[2] + b[2];
1138
1139 return out;
1140 };
1141
1142 /*
1143 * Rotate a 3D vector around the y-axis
1144 * @param {vec3} out The receiving vec3
1145 * @param {vec3} a The vec3 point to rotate
1146 * @param {vec3} b The origin of the rotation
1147 * @param {Number} c The angle of rotation
1148 * @returns {vec3} out
1149 */
1150 vec3.rotateY = function(out, a, b, c){
1151 var p = [], r=[];
1152 //Translate point to the origin
1153 p[0] = a[0] - b[0];
1154 p[1] = a[1] - b[1];
1155 p[2] = a[2] - b[2];
1156
1157 //perform rotation
1158 r[0] = p[2]*Math.sin(c) + p[0]*Math.cos(c);
1159 r[1] = p[1];
1160 r[2] = p[2]*Math.cos(c) - p[0]*Math.sin(c);
1161
1162 //translate to correct position
1163 out[0] = r[0] + b[0];
1164 out[1] = r[1] + b[1];
1165 out[2] = r[2] + b[2];
1166
1167 return out;
1168 };
1169
1170 /*
1171 * Rotate a 3D vector around the z-axis
1172 * @param {vec3} out The receiving vec3
1173 * @param {vec3} a The vec3 point to rotate
1174 * @param {vec3} b The origin of the rotation
1175 * @param {Number} c The angle of rotation
1176 * @returns {vec3} out
1177 */
1178 vec3.rotateZ = function(out, a, b, c){
1179 var p = [], r=[];
1180 //Translate point to the origin
1181 p[0] = a[0] - b[0];
1182 p[1] = a[1] - b[1];
1183 p[2] = a[2] - b[2];
1184
1185 //perform rotation
1186 r[0] = p[0]*Math.cos(c) - p[1]*Math.sin(c);
1187 r[1] = p[0]*Math.sin(c) + p[1]*Math.cos(c);
1188 r[2] = p[2];
1189
1190 //translate to correct position
1191 out[0] = r[0] + b[0];
1192 out[1] = r[1] + b[1];
1193 out[2] = r[2] + b[2];
1194
1195 return out;
1196 };
1197
1198 /**
1199 * Perform some operation over an array of vec3s.
1200 *
1201 * @param {Array} a the array of vectors to iterate over
1202 * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
1203 * @param {Number} offset Number of elements to skip at the beginning of the array
1204 * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
1205 * @param {Function} fn Function to call for each vector in the array
1206 * @param {Object} [arg] additional argument to pass to fn
1207 * @returns {Array} a
1208 * @function
1209 */
1210 vec3.forEach = (function() {
1211 var vec = vec3.create();
1212
1213 return function(a, stride, offset, count, fn, arg) {
1214 var i, l;
1215 if(!stride) {
1216 stride = 3;
1217 }
1218
1219 if(!offset) {
1220 offset = 0;
1221 }
1222
1223 if(count) {
1224 l = Math.min((count * stride) + offset, a.length);
1225 } else {
1226 l = a.length;
1227 }
1228
1229 for(i = offset; i < l; i += stride) {
1230 vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2];
1231 fn(vec, vec, arg);
1232 a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2];
1233 }
1234
1235 return a;
1236 };
1237 })();
1238
1239 /**
1240 * Returns a string representation of a vector
1241 *
1242 * @param {vec3} vec vector to represent as a string
1243 * @returns {String} string representation of the vector
1244 */
1245 vec3.str = function (a) {
1246 return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')';
1247 };
1248
1249 if(typeof(exports) !== 'undefined') {
1250 exports.vec3 = vec3;
1251 }
1252 ;
1253 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
1254
1255 Redistribution and use in source and binary forms, with or without modification,
1256 are permitted provided that the following conditions are met:
1257
1258 * Redistributions of source code must retain the above copyright notice, this
1259 list of conditions and the following disclaimer.
1260 * Redistributions in binary form must reproduce the above copyright notice,
1261 this list of conditions and the following disclaimer in the documentation
1262 and/or other materials provided with the distribution.
1263
1264 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1265 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1266 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1267 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1268 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1269 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1270 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1271 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1272 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1273 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
1274
1275 /**
1276 * @class 4 Dimensional Vector
1277 * @name vec4
1278 */
1279
1280 var vec4 = {};
1281
1282 /**
1283 * Creates a new, empty vec4
1284 *
1285 * @returns {vec4} a new 4D vector
1286 */
1287 vec4.create = function() {
1288 var out = new GLMAT_ARRAY_TYPE(4);
1289 out[0] = 0;
1290 out[1] = 0;
1291 out[2] = 0;
1292 out[3] = 0;
1293 return out;
1294 };
1295
1296 /**
1297 * Creates a new vec4 initialized with values from an existing vector
1298 *
1299 * @param {vec4} a vector to clone
1300 * @returns {vec4} a new 4D vector
1301 */
1302 vec4.clone = function(a) {
1303 var out = new GLMAT_ARRAY_TYPE(4);
1304 out[0] = a[0];
1305 out[1] = a[1];
1306 out[2] = a[2];
1307 out[3] = a[3];
1308 return out;
1309 };
1310
1311 /**
1312 * Creates a new vec4 initialized with the given values
1313 *
1314 * @param {Number} x X component
1315 * @param {Number} y Y component
1316 * @param {Number} z Z component
1317 * @param {Number} w W component
1318 * @returns {vec4} a new 4D vector
1319 */
1320 vec4.fromValues = function(x, y, z, w) {
1321 var out = new GLMAT_ARRAY_TYPE(4);
1322 out[0] = x;
1323 out[1] = y;
1324 out[2] = z;
1325 out[3] = w;
1326 return out;
1327 };
1328
1329 /**
1330 * Copy the values from one vec4 to another
1331 *
1332 * @param {vec4} out the receiving vector
1333 * @param {vec4} a the source vector
1334 * @returns {vec4} out
1335 */
1336 vec4.copy = function(out, a) {
1337 out[0] = a[0];
1338 out[1] = a[1];
1339 out[2] = a[2];
1340 out[3] = a[3];
1341 return out;
1342 };
1343
1344 /**
1345 * Set the components of a vec4 to the given values
1346 *
1347 * @param {vec4} out the receiving vector
1348 * @param {Number} x X component
1349 * @param {Number} y Y component
1350 * @param {Number} z Z component
1351 * @param {Number} w W component
1352 * @returns {vec4} out
1353 */
1354 vec4.set = function(out, x, y, z, w) {
1355 out[0] = x;
1356 out[1] = y;
1357 out[2] = z;
1358 out[3] = w;
1359 return out;
1360 };
1361
1362 /**
1363 * Adds two vec4's
1364 *
1365 * @param {vec4} out the receiving vector
1366 * @param {vec4} a the first operand
1367 * @param {vec4} b the second operand
1368 * @returns {vec4} out
1369 */
1370 vec4.add = function(out, a, b) {
1371 out[0] = a[0] + b[0];
1372 out[1] = a[1] + b[1];
1373 out[2] = a[2] + b[2];
1374 out[3] = a[3] + b[3];
1375 return out;
1376 };
1377
1378 /**
1379 * Subtracts vector b from vector a
1380 *
1381 * @param {vec4} out the receiving vector
1382 * @param {vec4} a the first operand
1383 * @param {vec4} b the second operand
1384 * @returns {vec4} out
1385 */
1386 vec4.subtract = function(out, a, b) {
1387 out[0] = a[0] - b[0];
1388 out[1] = a[1] - b[1];
1389 out[2] = a[2] - b[2];
1390 out[3] = a[3] - b[3];
1391 return out;
1392 };
1393
1394 /**
1395 * Alias for {@link vec4.subtract}
1396 * @function
1397 */
1398 vec4.sub = vec4.subtract;
1399
1400 /**
1401 * Multiplies two vec4's
1402 *
1403 * @param {vec4} out the receiving vector
1404 * @param {vec4} a the first operand
1405 * @param {vec4} b the second operand
1406 * @returns {vec4} out
1407 */
1408 vec4.multiply = function(out, a, b) {
1409 out[0] = a[0] * b[0];
1410 out[1] = a[1] * b[1];
1411 out[2] = a[2] * b[2];
1412 out[3] = a[3] * b[3];
1413 return out;
1414 };
1415
1416 /**
1417 * Alias for {@link vec4.multiply}
1418 * @function
1419 */
1420 vec4.mul = vec4.multiply;
1421
1422 /**
1423 * Divides two vec4's
1424 *
1425 * @param {vec4} out the receiving vector
1426 * @param {vec4} a the first operand
1427 * @param {vec4} b the second operand
1428 * @returns {vec4} out
1429 */
1430 vec4.divide = function(out, a, b) {
1431 out[0] = a[0] / b[0];
1432 out[1] = a[1] / b[1];
1433 out[2] = a[2] / b[2];
1434 out[3] = a[3] / b[3];
1435 return out;
1436 };
1437
1438 /**
1439 * Alias for {@link vec4.divide}
1440 * @function
1441 */
1442 vec4.div = vec4.divide;
1443
1444 /**
1445 * Returns the minimum of two vec4's
1446 *
1447 * @param {vec4} out the receiving vector
1448 * @param {vec4} a the first operand
1449 * @param {vec4} b the second operand
1450 * @returns {vec4} out
1451 */
1452 vec4.min = function(out, a, b) {
1453 out[0] = Math.min(a[0], b[0]);
1454 out[1] = Math.min(a[1], b[1]);
1455 out[2] = Math.min(a[2], b[2]);
1456 out[3] = Math.min(a[3], b[3]);
1457 return out;
1458 };
1459
1460 /**
1461 * Returns the maximum of two vec4's
1462 *
1463 * @param {vec4} out the receiving vector
1464 * @param {vec4} a the first operand
1465 * @param {vec4} b the second operand
1466 * @returns {vec4} out
1467 */
1468 vec4.max = function(out, a, b) {
1469 out[0] = Math.max(a[0], b[0]);
1470 out[1] = Math.max(a[1], b[1]);
1471 out[2] = Math.max(a[2], b[2]);
1472 out[3] = Math.max(a[3], b[3]);
1473 return out;
1474 };
1475
1476 /**
1477 * Scales a vec4 by a scalar number
1478 *
1479 * @param {vec4} out the receiving vector
1480 * @param {vec4} a the vector to scale
1481 * @param {Number} b amount to scale the vector by
1482 * @returns {vec4} out
1483 */
1484 vec4.scale = function(out, a, b) {
1485 out[0] = a[0] * b;
1486 out[1] = a[1] * b;
1487 out[2] = a[2] * b;
1488 out[3] = a[3] * b;
1489 return out;
1490 };
1491
1492 /**
1493 * Adds two vec4's after scaling the second operand by a scalar value
1494 *
1495 * @param {vec4} out the receiving vector
1496 * @param {vec4} a the first operand
1497 * @param {vec4} b the second operand
1498 * @param {Number} scale the amount to scale b by before adding
1499 * @returns {vec4} out
1500 */
1501 vec4.scaleAndAdd = function(out, a, b, scale) {
1502 out[0] = a[0] + (b[0] * scale);
1503 out[1] = a[1] + (b[1] * scale);
1504 out[2] = a[2] + (b[2] * scale);
1505 out[3] = a[3] + (b[3] * scale);
1506 return out;
1507 };
1508
1509 /**
1510 * Calculates the euclidian distance between two vec4's
1511 *
1512 * @param {vec4} a the first operand
1513 * @param {vec4} b the second operand
1514 * @returns {Number} distance between a and b
1515 */
1516 vec4.distance = function(a, b) {
1517 var x = b[0] - a[0],
1518 y = b[1] - a[1],
1519 z = b[2] - a[2],
1520 w = b[3] - a[3];
1521 return Math.sqrt(x*x + y*y + z*z + w*w);
1522 };
1523
1524 /**
1525 * Alias for {@link vec4.distance}
1526 * @function
1527 */
1528 vec4.dist = vec4.distance;
1529
1530 /**
1531 * Calculates the squared euclidian distance between two vec4's
1532 *
1533 * @param {vec4} a the first operand
1534 * @param {vec4} b the second operand
1535 * @returns {Number} squared distance between a and b
1536 */
1537 vec4.squaredDistance = function(a, b) {
1538 var x = b[0] - a[0],
1539 y = b[1] - a[1],
1540 z = b[2] - a[2],
1541 w = b[3] - a[3];
1542 return x*x + y*y + z*z + w*w;
1543 };
1544
1545 /**
1546 * Alias for {@link vec4.squaredDistance}
1547 * @function
1548 */
1549 vec4.sqrDist = vec4.squaredDistance;
1550
1551 /**
1552 * Calculates the length of a vec4
1553 *
1554 * @param {vec4} a vector to calculate length of
1555 * @returns {Number} length of a
1556 */
1557 vec4.length = function (a) {
1558 var x = a[0],
1559 y = a[1],
1560 z = a[2],
1561 w = a[3];
1562 return Math.sqrt(x*x + y*y + z*z + w*w);
1563 };
1564
1565 /**
1566 * Alias for {@link vec4.length}
1567 * @function
1568 */
1569 vec4.len = vec4.length;
1570
1571 /**
1572 * Calculates the squared length of a vec4
1573 *
1574 * @param {vec4} a vector to calculate squared length of
1575 * @returns {Number} squared length of a
1576 */
1577 vec4.squaredLength = function (a) {
1578 var x = a[0],
1579 y = a[1],
1580 z = a[2],
1581 w = a[3];
1582 return x*x + y*y + z*z + w*w;
1583 };
1584
1585 /**
1586 * Alias for {@link vec4.squaredLength}
1587 * @function
1588 */
1589 vec4.sqrLen = vec4.squaredLength;
1590
1591 /**
1592 * Negates the components of a vec4
1593 *
1594 * @param {vec4} out the receiving vector
1595 * @param {vec4} a vector to negate
1596 * @returns {vec4} out
1597 */
1598 vec4.negate = function(out, a) {
1599 out[0] = -a[0];
1600 out[1] = -a[1];
1601 out[2] = -a[2];
1602 out[3] = -a[3];
1603 return out;
1604 };
1605
1606 /**
1607 * Normalize a vec4
1608 *
1609 * @param {vec4} out the receiving vector
1610 * @param {vec4} a vector to normalize
1611 * @returns {vec4} out
1612 */
1613 vec4.normalize = function(out, a) {
1614 var x = a[0],
1615 y = a[1],
1616 z = a[2],
1617 w = a[3];
1618 var len = x*x + y*y + z*z + w*w;
1619 if (len > 0) {
1620 len = 1 / Math.sqrt(len);
1621 out[0] = a[0] * len;
1622 out[1] = a[1] * len;
1623 out[2] = a[2] * len;
1624 out[3] = a[3] * len;
1625 }
1626 return out;
1627 };
1628
1629 /**
1630 * Calculates the dot product of two vec4's
1631 *
1632 * @param {vec4} a the first operand
1633 * @param {vec4} b the second operand
1634 * @returns {Number} dot product of a and b
1635 */
1636 vec4.dot = function (a, b) {
1637 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
1638 };
1639
1640 /**
1641 * Performs a linear interpolation between two vec4's
1642 *
1643 * @param {vec4} out the receiving vector
1644 * @param {vec4} a the first operand
1645 * @param {vec4} b the second operand
1646 * @param {Number} t interpolation amount between the two inputs
1647 * @returns {vec4} out
1648 */
1649 vec4.lerp = function (out, a, b, t) {
1650 var ax = a[0],
1651 ay = a[1],
1652 az = a[2],
1653 aw = a[3];
1654 out[0] = ax + t * (b[0] - ax);
1655 out[1] = ay + t * (b[1] - ay);
1656 out[2] = az + t * (b[2] - az);
1657 out[3] = aw + t * (b[3] - aw);
1658 return out;
1659 };
1660
1661 /**
1662 * Generates a random vector with the given scale
1663 *
1664 * @param {vec4} out the receiving vector
1665 * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
1666 * @returns {vec4} out
1667 */
1668 vec4.random = function (out, scale) {
1669 scale = scale || 1.0;
1670
1671 //TODO: This is a pretty awful way of doing this. Find something better.
1672 out[0] = GLMAT_RANDOM();
1673 out[1] = GLMAT_RANDOM();
1674 out[2] = GLMAT_RANDOM();
1675 out[3] = GLMAT_RANDOM();
1676 vec4.normalize(out, out);
1677 vec4.scale(out, out, scale);
1678 return out;
1679 };
1680
1681 /**
1682 * Transforms the vec4 with a mat4.
1683 *
1684 * @param {vec4} out the receiving vector
1685 * @param {vec4} a the vector to transform
1686 * @param {mat4} m matrix to transform with
1687 * @returns {vec4} out
1688 */
1689 vec4.transformMat4 = function(out, a, m) {
1690 var x = a[0], y = a[1], z = a[2], w = a[3];
1691 out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
1692 out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
1693 out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
1694 out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
1695 return out;
1696 };
1697
1698 /**
1699 * Transforms the vec4 with a quat
1700 *
1701 * @param {vec4} out the receiving vector
1702 * @param {vec4} a the vector to transform
1703 * @param {quat} q quaternion to transform with
1704 * @returns {vec4} out
1705 */
1706 vec4.transformQuat = function(out, a, q) {
1707 var x = a[0], y = a[1], z = a[2],
1708 qx = q[0], qy = q[1], qz = q[2], qw = q[3],
1709
1710 // calculate quat * vec
1711 ix = qw * x + qy * z - qz * y,
1712 iy = qw * y + qz * x - qx * z,
1713 iz = qw * z + qx * y - qy * x,
1714 iw = -qx * x - qy * y - qz * z;
1715
1716 // calculate result * inverse quat
1717 out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
1718 out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
1719 out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
1720 return out;
1721 };
1722
1723 /**
1724 * Perform some operation over an array of vec4s.
1725 *
1726 * @param {Array} a the array of vectors to iterate over
1727 * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
1728 * @param {Number} offset Number of elements to skip at the beginning of the array
1729 * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
1730 * @param {Function} fn Function to call for each vector in the array
1731 * @param {Object} [arg] additional argument to pass to fn
1732 * @returns {Array} a
1733 * @function
1734 */
1735 vec4.forEach = (function() {
1736 var vec = vec4.create();
1737
1738 return function(a, stride, offset, count, fn, arg) {
1739 var i, l;
1740 if(!stride) {
1741 stride = 4;
1742 }
1743
1744 if(!offset) {
1745 offset = 0;
1746 }
1747
1748 if(count) {
1749 l = Math.min((count * stride) + offset, a.length);
1750 } else {
1751 l = a.length;
1752 }
1753
1754 for(i = offset; i < l; i += stride) {
1755 vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3];
1756 fn(vec, vec, arg);
1757 a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3];
1758 }
1759
1760 return a;
1761 };
1762 })();
1763
1764 /**
1765 * Returns a string representation of a vector
1766 *
1767 * @param {vec4} vec vector to represent as a string
1768 * @returns {String} string representation of the vector
1769 */
1770 vec4.str = function (a) {
1771 return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
1772 };
1773
1774 if(typeof(exports) !== 'undefined') {
1775 exports.vec4 = vec4;
1776 }
1777 ;
1778 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
1779
1780 Redistribution and use in source and binary forms, with or without modification,
1781 are permitted provided that the following conditions are met:
1782
1783 * Redistributions of source code must retain the above copyright notice, this
1784 list of conditions and the following disclaimer.
1785 * Redistributions in binary form must reproduce the above copyright notice,
1786 this list of conditions and the following disclaimer in the documentation
1787 and/or other materials provided with the distribution.
1788
1789 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1790 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1791 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1792 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
1793 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1794 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1795 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
1796 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1797 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1798 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
1799
1800 /**
1801 * @class 2x2 Matrix
1802 * @name mat2
1803 */
1804
1805 var mat2 = {};
1806
1807 /**
1808 * Creates a new identity mat2
1809 *
1810 * @returns {mat2} a new 2x2 matrix
1811 */
1812 mat2.create = function() {
1813 var out = new GLMAT_ARRAY_TYPE(4);
1814 out[0] = 1;
1815 out[1] = 0;
1816 out[2] = 0;
1817 out[3] = 1;
1818 return out;
1819 };
1820
1821 /**
1822 * Creates a new mat2 initialized with values from an existing matrix
1823 *
1824 * @param {mat2} a matrix to clone
1825 * @returns {mat2} a new 2x2 matrix
1826 */
1827 mat2.clone = function(a) {
1828 var out = new GLMAT_ARRAY_TYPE(4);
1829 out[0] = a[0];
1830 out[1] = a[1];
1831 out[2] = a[2];
1832 out[3] = a[3];
1833 return out;
1834 };
1835
1836 /**
1837 * Copy the values from one mat2 to another
1838 *
1839 * @param {mat2} out the receiving matrix
1840 * @param {mat2} a the source matrix
1841 * @returns {mat2} out
1842 */
1843 mat2.copy = function(out, a) {
1844 out[0] = a[0];
1845 out[1] = a[1];
1846 out[2] = a[2];
1847 out[3] = a[3];
1848 return out;
1849 };
1850
1851 /**
1852 * Set a mat2 to the identity matrix
1853 *
1854 * @param {mat2} out the receiving matrix
1855 * @returns {mat2} out
1856 */
1857 mat2.identity = function(out) {
1858 out[0] = 1;
1859 out[1] = 0;
1860 out[2] = 0;
1861 out[3] = 1;
1862 return out;
1863 };
1864
1865 /**
1866 * Transpose the values of a mat2
1867 *
1868 * @param {mat2} out the receiving matrix
1869 * @param {mat2} a the source matrix
1870 * @returns {mat2} out
1871 */
1872 mat2.transpose = function(out, a) {
1873 // If we are transposing ourselves we can skip a few steps but have to cache some values
1874 if (out === a) {
1875 var a1 = a[1];
1876 out[1] = a[2];
1877 out[2] = a1;
1878 } else {
1879 out[0] = a[0];
1880 out[1] = a[2];
1881 out[2] = a[1];
1882 out[3] = a[3];
1883 }
1884
1885 return out;
1886 };
1887
1888 /**
1889 * Inverts a mat2
1890 *
1891 * @param {mat2} out the receiving matrix
1892 * @param {mat2} a the source matrix
1893 * @returns {mat2} out
1894 */
1895 mat2.invert = function(out, a) {
1896 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
1897
1898 // Calculate the determinant
1899 det = a0 * a3 - a2 * a1;
1900
1901 if (!det) {
1902 return null;
1903 }
1904 det = 1.0 / det;
1905
1906 out[0] = a3 * det;
1907 out[1] = -a1 * det;
1908 out[2] = -a2 * det;
1909 out[3] = a0 * det;
1910
1911 return out;
1912 };
1913
1914 /**
1915 * Calculates the adjugate of a mat2
1916 *
1917 * @param {mat2} out the receiving matrix
1918 * @param {mat2} a the source matrix
1919 * @returns {mat2} out
1920 */
1921 mat2.adjoint = function(out, a) {
1922 // Caching this value is nessecary if out == a
1923 var a0 = a[0];
1924 out[0] = a[3];
1925 out[1] = -a[1];
1926 out[2] = -a[2];
1927 out[3] = a0;
1928
1929 return out;
1930 };
1931
1932 /**
1933 * Calculates the determinant of a mat2
1934 *
1935 * @param {mat2} a the source matrix
1936 * @returns {Number} determinant of a
1937 */
1938 mat2.determinant = function (a) {
1939 return a[0] * a[3] - a[2] * a[1];
1940 };
1941
1942 /**
1943 * Multiplies two mat2's
1944 *
1945 * @param {mat2} out the receiving matrix
1946 * @param {mat2} a the first operand
1947 * @param {mat2} b the second operand
1948 * @returns {mat2} out
1949 */
1950 mat2.multiply = function (out, a, b) {
1951 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
1952 var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
1953 out[0] = a0 * b0 + a2 * b1;
1954 out[1] = a1 * b0 + a3 * b1;
1955 out[2] = a0 * b2 + a2 * b3;
1956 out[3] = a1 * b2 + a3 * b3;
1957 return out;
1958 };
1959
1960 /**
1961 * Alias for {@link mat2.multiply}
1962 * @function
1963 */
1964 mat2.mul = mat2.multiply;
1965
1966 /**
1967 * Rotates a mat2 by the given angle
1968 *
1969 * @param {mat2} out the receiving matrix
1970 * @param {mat2} a the matrix to rotate
1971 * @param {Number} rad the angle to rotate the matrix by
1972 * @returns {mat2} out
1973 */
1974 mat2.rotate = function (out, a, rad) {
1975 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
1976 s = Math.sin(rad),
1977 c = Math.cos(rad);
1978 out[0] = a0 * c + a2 * s;
1979 out[1] = a1 * c + a3 * s;
1980 out[2] = a0 * -s + a2 * c;
1981 out[3] = a1 * -s + a3 * c;
1982 return out;
1983 };
1984
1985 /**
1986 * Scales the mat2 by the dimensions in the given vec2
1987 *
1988 * @param {mat2} out the receiving matrix
1989 * @param {mat2} a the matrix to rotate
1990 * @param {vec2} v the vec2 to scale the matrix by
1991 * @returns {mat2} out
1992 **/
1993 mat2.scale = function(out, a, v) {
1994 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
1995 v0 = v[0], v1 = v[1];
1996 out[0] = a0 * v0;
1997 out[1] = a1 * v0;
1998 out[2] = a2 * v1;
1999 out[3] = a3 * v1;
2000 return out;
2001 };
2002
2003 /**
2004 * Returns a string representation of a mat2
2005 *
2006 * @param {mat2} mat matrix to represent as a string
2007 * @returns {String} string representation of the matrix
2008 */
2009 mat2.str = function (a) {
2010 return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
2011 };
2012
2013 /**
2014 * Returns Frobenius norm of a mat2
2015 *
2016 * @param {mat2} a the matrix to calculate Frobenius norm of
2017 * @returns {Number} Frobenius norm
2018 */
2019 mat2.frob = function (a) {
2020 return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2)))
2021 };
2022
2023 /**
2024 * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
2025 * @param {mat2} L the lower triangular matrix
2026 * @param {mat2} D the diagonal matrix
2027 * @param {mat2} U the upper triangular matrix
2028 * @param {mat2} a the input matrix to factorize
2029 */
2030
2031 mat2.LDU = function (L, D, U, a) {
2032 L[2] = a[2]/a[0];
2033 U[0] = a[0];
2034 U[1] = a[1];
2035 U[3] = a[3] - L[2] * U[1];
2036 return [L, D, U];
2037 };
2038
2039 if(typeof(exports) !== 'undefined') {
2040 exports.mat2 = mat2;
2041 }
2042 ;
2043 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2044
2045 Redistribution and use in source and binary forms, with or without modification,
2046 are permitted provided that the following conditions are met:
2047
2048 * Redistributions of source code must retain the above copyright notice, this
2049 list of conditions and the following disclaimer.
2050 * Redistributions in binary form must reproduce the above copyright notice,
2051 this list of conditions and the following disclaimer in the documentation
2052 and/or other materials provided with the distribution.
2053
2054 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2055 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2056 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2057 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2058 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2059 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2060 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2061 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2062 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2063 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2064
2065 /**
2066 * @class 2x3 Matrix
2067 * @name mat2d
2068 *
2069 * @description
2070 * A mat2d contains six elements defined as:
2071 * <pre>
2072 * [a, c, tx,
2073 * b, d, ty]
2074 * </pre>
2075 * This is a short form for the 3x3 matrix:
2076 * <pre>
2077 * [a, c, tx,
2078 * b, d, ty,
2079 * 0, 0, 1]
2080 * </pre>
2081 * The last row is ignored so the array is shorter and operations are faster.
2082 */
2083
2084 var mat2d = {};
2085
2086 /**
2087 * Creates a new identity mat2d
2088 *
2089 * @returns {mat2d} a new 2x3 matrix
2090 */
2091 mat2d.create = function() {
2092 var out = new GLMAT_ARRAY_TYPE(6);
2093 out[0] = 1;
2094 out[1] = 0;
2095 out[2] = 0;
2096 out[3] = 1;
2097 out[4] = 0;
2098 out[5] = 0;
2099 return out;
2100 };
2101
2102 /**
2103 * Creates a new mat2d initialized with values from an existing matrix
2104 *
2105 * @param {mat2d} a matrix to clone
2106 * @returns {mat2d} a new 2x3 matrix
2107 */
2108 mat2d.clone = function(a) {
2109 var out = new GLMAT_ARRAY_TYPE(6);
2110 out[0] = a[0];
2111 out[1] = a[1];
2112 out[2] = a[2];
2113 out[3] = a[3];
2114 out[4] = a[4];
2115 out[5] = a[5];
2116 return out;
2117 };
2118
2119 /**
2120 * Copy the values from one mat2d to another
2121 *
2122 * @param {mat2d} out the receiving matrix
2123 * @param {mat2d} a the source matrix
2124 * @returns {mat2d} out
2125 */
2126 mat2d.copy = function(out, a) {
2127 out[0] = a[0];
2128 out[1] = a[1];
2129 out[2] = a[2];
2130 out[3] = a[3];
2131 out[4] = a[4];
2132 out[5] = a[5];
2133 return out;
2134 };
2135
2136 /**
2137 * Set a mat2d to the identity matrix
2138 *
2139 * @param {mat2d} out the receiving matrix
2140 * @returns {mat2d} out
2141 */
2142 mat2d.identity = function(out) {
2143 out[0] = 1;
2144 out[1] = 0;
2145 out[2] = 0;
2146 out[3] = 1;
2147 out[4] = 0;
2148 out[5] = 0;
2149 return out;
2150 };
2151
2152 /**
2153 * Inverts a mat2d
2154 *
2155 * @param {mat2d} out the receiving matrix
2156 * @param {mat2d} a the source matrix
2157 * @returns {mat2d} out
2158 */
2159 mat2d.invert = function(out, a) {
2160 var aa = a[0], ab = a[1], ac = a[2], ad = a[3],
2161 atx = a[4], aty = a[5];
2162
2163 var det = aa * ad - ab * ac;
2164 if(!det){
2165 return null;
2166 }
2167 det = 1.0 / det;
2168
2169 out[0] = ad * det;
2170 out[1] = -ab * det;
2171 out[2] = -ac * det;
2172 out[3] = aa * det;
2173 out[4] = (ac * aty - ad * atx) * det;
2174 out[5] = (ab * atx - aa * aty) * det;
2175 return out;
2176 };
2177
2178 /**
2179 * Calculates the determinant of a mat2d
2180 *
2181 * @param {mat2d} a the source matrix
2182 * @returns {Number} determinant of a
2183 */
2184 mat2d.determinant = function (a) {
2185 return a[0] * a[3] - a[1] * a[2];
2186 };
2187
2188 /**
2189 * Multiplies two mat2d's
2190 *
2191 * @param {mat2d} out the receiving matrix
2192 * @param {mat2d} a the first operand
2193 * @param {mat2d} b the second operand
2194 * @returns {mat2d} out
2195 */
2196 mat2d.multiply = function (out, a, b) {
2197 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
2198 b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5];
2199 out[0] = a0 * b0 + a2 * b1;
2200 out[1] = a1 * b0 + a3 * b1;
2201 out[2] = a0 * b2 + a2 * b3;
2202 out[3] = a1 * b2 + a3 * b3;
2203 out[4] = a0 * b4 + a2 * b5 + a4;
2204 out[5] = a1 * b4 + a3 * b5 + a5;
2205 return out;
2206 };
2207
2208 /**
2209 * Alias for {@link mat2d.multiply}
2210 * @function
2211 */
2212 mat2d.mul = mat2d.multiply;
2213
2214
2215 /**
2216 * Rotates a mat2d by the given angle
2217 *
2218 * @param {mat2d} out the receiving matrix
2219 * @param {mat2d} a the matrix to rotate
2220 * @param {Number} rad the angle to rotate the matrix by
2221 * @returns {mat2d} out
2222 */
2223 mat2d.rotate = function (out, a, rad) {
2224 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
2225 s = Math.sin(rad),
2226 c = Math.cos(rad);
2227 out[0] = a0 * c + a2 * s;
2228 out[1] = a1 * c + a3 * s;
2229 out[2] = a0 * -s + a2 * c;
2230 out[3] = a1 * -s + a3 * c;
2231 out[4] = a4;
2232 out[5] = a5;
2233 return out;
2234 };
2235
2236 /**
2237 * Scales the mat2d by the dimensions in the given vec2
2238 *
2239 * @param {mat2d} out the receiving matrix
2240 * @param {mat2d} a the matrix to translate
2241 * @param {vec2} v the vec2 to scale the matrix by
2242 * @returns {mat2d} out
2243 **/
2244 mat2d.scale = function(out, a, v) {
2245 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
2246 v0 = v[0], v1 = v[1];
2247 out[0] = a0 * v0;
2248 out[1] = a1 * v0;
2249 out[2] = a2 * v1;
2250 out[3] = a3 * v1;
2251 out[4] = a4;
2252 out[5] = a5;
2253 return out;
2254 };
2255
2256 /**
2257 * Translates the mat2d by the dimensions in the given vec2
2258 *
2259 * @param {mat2d} out the receiving matrix
2260 * @param {mat2d} a the matrix to translate
2261 * @param {vec2} v the vec2 to translate the matrix by
2262 * @returns {mat2d} out
2263 **/
2264 mat2d.translate = function(out, a, v) {
2265 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3], a4 = a[4], a5 = a[5],
2266 v0 = v[0], v1 = v[1];
2267 out[0] = a0;
2268 out[1] = a1;
2269 out[2] = a2;
2270 out[3] = a3;
2271 out[4] = a0 * v0 + a2 * v1 + a4;
2272 out[5] = a1 * v0 + a3 * v1 + a5;
2273 return out;
2274 };
2275
2276 /**
2277 * Returns a string representation of a mat2d
2278 *
2279 * @param {mat2d} a matrix to represent as a string
2280 * @returns {String} string representation of the matrix
2281 */
2282 mat2d.str = function (a) {
2283 return 'mat2d(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
2284 a[3] + ', ' + a[4] + ', ' + a[5] + ')';
2285 };
2286
2287 /**
2288 * Returns Frobenius norm of a mat2d
2289 *
2290 * @param {mat2d} a the matrix to calculate Frobenius norm of
2291 * @returns {Number} Frobenius norm
2292 */
2293 mat2d.frob = function (a) {
2294 return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + 1))
2295 };
2296
2297 if(typeof(exports) !== 'undefined') {
2298 exports.mat2d = mat2d;
2299 }
2300 ;
2301 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2302
2303 Redistribution and use in source and binary forms, with or without modification,
2304 are permitted provided that the following conditions are met:
2305
2306 * Redistributions of source code must retain the above copyright notice, this
2307 list of conditions and the following disclaimer.
2308 * Redistributions in binary form must reproduce the above copyright notice,
2309 this list of conditions and the following disclaimer in the documentation
2310 and/or other materials provided with the distribution.
2311
2312 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2313 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2314 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2315 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2316 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2317 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2318 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2319 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2320 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2321 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2322
2323 /**
2324 * @class 3x3 Matrix
2325 * @name mat3
2326 */
2327
2328 var mat3 = {};
2329
2330 /**
2331 * Creates a new identity mat3
2332 *
2333 * @returns {mat3} a new 3x3 matrix
2334 */
2335 mat3.create = function() {
2336 var out = new GLMAT_ARRAY_TYPE(9);
2337 out[0] = 1;
2338 out[1] = 0;
2339 out[2] = 0;
2340 out[3] = 0;
2341 out[4] = 1;
2342 out[5] = 0;
2343 out[6] = 0;
2344 out[7] = 0;
2345 out[8] = 1;
2346 return out;
2347 };
2348
2349 /**
2350 * Copies the upper-left 3x3 values into the given mat3.
2351 *
2352 * @param {mat3} out the receiving 3x3 matrix
2353 * @param {mat4} a the source 4x4 matrix
2354 * @returns {mat3} out
2355 */
2356 mat3.fromMat4 = function(out, a) {
2357 out[0] = a[0];
2358 out[1] = a[1];
2359 out[2] = a[2];
2360 out[3] = a[4];
2361 out[4] = a[5];
2362 out[5] = a[6];
2363 out[6] = a[8];
2364 out[7] = a[9];
2365 out[8] = a[10];
2366 return out;
2367 };
2368
2369 /**
2370 * Creates a new mat3 initialized with values from an existing matrix
2371 *
2372 * @param {mat3} a matrix to clone
2373 * @returns {mat3} a new 3x3 matrix
2374 */
2375 mat3.clone = function(a) {
2376 var out = new GLMAT_ARRAY_TYPE(9);
2377 out[0] = a[0];
2378 out[1] = a[1];
2379 out[2] = a[2];
2380 out[3] = a[3];
2381 out[4] = a[4];
2382 out[5] = a[5];
2383 out[6] = a[6];
2384 out[7] = a[7];
2385 out[8] = a[8];
2386 return out;
2387 };
2388
2389 /**
2390 * Copy the values from one mat3 to another
2391 *
2392 * @param {mat3} out the receiving matrix
2393 * @param {mat3} a the source matrix
2394 * @returns {mat3} out
2395 */
2396 mat3.copy = function(out, a) {
2397 out[0] = a[0];
2398 out[1] = a[1];
2399 out[2] = a[2];
2400 out[3] = a[3];
2401 out[4] = a[4];
2402 out[5] = a[5];
2403 out[6] = a[6];
2404 out[7] = a[7];
2405 out[8] = a[8];
2406 return out;
2407 };
2408
2409 /**
2410 * Set a mat3 to the identity matrix
2411 *
2412 * @param {mat3} out the receiving matrix
2413 * @returns {mat3} out
2414 */
2415 mat3.identity = function(out) {
2416 out[0] = 1;
2417 out[1] = 0;
2418 out[2] = 0;
2419 out[3] = 0;
2420 out[4] = 1;
2421 out[5] = 0;
2422 out[6] = 0;
2423 out[7] = 0;
2424 out[8] = 1;
2425 return out;
2426 };
2427
2428 /**
2429 * Transpose the values of a mat3
2430 *
2431 * @param {mat3} out the receiving matrix
2432 * @param {mat3} a the source matrix
2433 * @returns {mat3} out
2434 */
2435 mat3.transpose = function(out, a) {
2436 // If we are transposing ourselves we can skip a few steps but have to cache some values
2437 if (out === a) {
2438 var a01 = a[1], a02 = a[2], a12 = a[5];
2439 out[1] = a[3];
2440 out[2] = a[6];
2441 out[3] = a01;
2442 out[5] = a[7];
2443 out[6] = a02;
2444 out[7] = a12;
2445 } else {
2446 out[0] = a[0];
2447 out[1] = a[3];
2448 out[2] = a[6];
2449 out[3] = a[1];
2450 out[4] = a[4];
2451 out[5] = a[7];
2452 out[6] = a[2];
2453 out[7] = a[5];
2454 out[8] = a[8];
2455 }
2456
2457 return out;
2458 };
2459
2460 /**
2461 * Inverts a mat3
2462 *
2463 * @param {mat3} out the receiving matrix
2464 * @param {mat3} a the source matrix
2465 * @returns {mat3} out
2466 */
2467 mat3.invert = function(out, a) {
2468 var a00 = a[0], a01 = a[1], a02 = a[2],
2469 a10 = a[3], a11 = a[4], a12 = a[5],
2470 a20 = a[6], a21 = a[7], a22 = a[8],
2471
2472 b01 = a22 * a11 - a12 * a21,
2473 b11 = -a22 * a10 + a12 * a20,
2474 b21 = a21 * a10 - a11 * a20,
2475
2476 // Calculate the determinant
2477 det = a00 * b01 + a01 * b11 + a02 * b21;
2478
2479 if (!det) {
2480 return null;
2481 }
2482 det = 1.0 / det;
2483
2484 out[0] = b01 * det;
2485 out[1] = (-a22 * a01 + a02 * a21) * det;
2486 out[2] = (a12 * a01 - a02 * a11) * det;
2487 out[3] = b11 * det;
2488 out[4] = (a22 * a00 - a02 * a20) * det;
2489 out[5] = (-a12 * a00 + a02 * a10) * det;
2490 out[6] = b21 * det;
2491 out[7] = (-a21 * a00 + a01 * a20) * det;
2492 out[8] = (a11 * a00 - a01 * a10) * det;
2493 return out;
2494 };
2495
2496 /**
2497 * Calculates the adjugate of a mat3
2498 *
2499 * @param {mat3} out the receiving matrix
2500 * @param {mat3} a the source matrix
2501 * @returns {mat3} out
2502 */
2503 mat3.adjoint = function(out, a) {
2504 var a00 = a[0], a01 = a[1], a02 = a[2],
2505 a10 = a[3], a11 = a[4], a12 = a[5],
2506 a20 = a[6], a21 = a[7], a22 = a[8];
2507
2508 out[0] = (a11 * a22 - a12 * a21);
2509 out[1] = (a02 * a21 - a01 * a22);
2510 out[2] = (a01 * a12 - a02 * a11);
2511 out[3] = (a12 * a20 - a10 * a22);
2512 out[4] = (a00 * a22 - a02 * a20);
2513 out[5] = (a02 * a10 - a00 * a12);
2514 out[6] = (a10 * a21 - a11 * a20);
2515 out[7] = (a01 * a20 - a00 * a21);
2516 out[8] = (a00 * a11 - a01 * a10);
2517 return out;
2518 };
2519
2520 /**
2521 * Calculates the determinant of a mat3
2522 *
2523 * @param {mat3} a the source matrix
2524 * @returns {Number} determinant of a
2525 */
2526 mat3.determinant = function (a) {
2527 var a00 = a[0], a01 = a[1], a02 = a[2],
2528 a10 = a[3], a11 = a[4], a12 = a[5],
2529 a20 = a[6], a21 = a[7], a22 = a[8];
2530
2531 return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
2532 };
2533
2534 /**
2535 * Multiplies two mat3's
2536 *
2537 * @param {mat3} out the receiving matrix
2538 * @param {mat3} a the first operand
2539 * @param {mat3} b the second operand
2540 * @returns {mat3} out
2541 */
2542 mat3.multiply = function (out, a, b) {
2543 var a00 = a[0], a01 = a[1], a02 = a[2],
2544 a10 = a[3], a11 = a[4], a12 = a[5],
2545 a20 = a[6], a21 = a[7], a22 = a[8],
2546
2547 b00 = b[0], b01 = b[1], b02 = b[2],
2548 b10 = b[3], b11 = b[4], b12 = b[5],
2549 b20 = b[6], b21 = b[7], b22 = b[8];
2550
2551 out[0] = b00 * a00 + b01 * a10 + b02 * a20;
2552 out[1] = b00 * a01 + b01 * a11 + b02 * a21;
2553 out[2] = b00 * a02 + b01 * a12 + b02 * a22;
2554
2555 out[3] = b10 * a00 + b11 * a10 + b12 * a20;
2556 out[4] = b10 * a01 + b11 * a11 + b12 * a21;
2557 out[5] = b10 * a02 + b11 * a12 + b12 * a22;
2558
2559 out[6] = b20 * a00 + b21 * a10 + b22 * a20;
2560 out[7] = b20 * a01 + b21 * a11 + b22 * a21;
2561 out[8] = b20 * a02 + b21 * a12 + b22 * a22;
2562 return out;
2563 };
2564
2565 /**
2566 * Alias for {@link mat3.multiply}
2567 * @function
2568 */
2569 mat3.mul = mat3.multiply;
2570
2571 /**
2572 * Translate a mat3 by the given vector
2573 *
2574 * @param {mat3} out the receiving matrix
2575 * @param {mat3} a the matrix to translate
2576 * @param {vec2} v vector to translate by
2577 * @returns {mat3} out
2578 */
2579 mat3.translate = function(out, a, v) {
2580 var a00 = a[0], a01 = a[1], a02 = a[2],
2581 a10 = a[3], a11 = a[4], a12 = a[5],
2582 a20 = a[6], a21 = a[7], a22 = a[8],
2583 x = v[0], y = v[1];
2584
2585 out[0] = a00;
2586 out[1] = a01;
2587 out[2] = a02;
2588
2589 out[3] = a10;
2590 out[4] = a11;
2591 out[5] = a12;
2592
2593 out[6] = x * a00 + y * a10 + a20;
2594 out[7] = x * a01 + y * a11 + a21;
2595 out[8] = x * a02 + y * a12 + a22;
2596 return out;
2597 };
2598
2599 /**
2600 * Rotates a mat3 by the given angle
2601 *
2602 * @param {mat3} out the receiving matrix
2603 * @param {mat3} a the matrix to rotate
2604 * @param {Number} rad the angle to rotate the matrix by
2605 * @returns {mat3} out
2606 */
2607 mat3.rotate = function (out, a, rad) {
2608 var a00 = a[0], a01 = a[1], a02 = a[2],
2609 a10 = a[3], a11 = a[4], a12 = a[5],
2610 a20 = a[6], a21 = a[7], a22 = a[8],
2611
2612 s = Math.sin(rad),
2613 c = Math.cos(rad);
2614
2615 out[0] = c * a00 + s * a10;
2616 out[1] = c * a01 + s * a11;
2617 out[2] = c * a02 + s * a12;
2618
2619 out[3] = c * a10 - s * a00;
2620 out[4] = c * a11 - s * a01;
2621 out[5] = c * a12 - s * a02;
2622
2623 out[6] = a20;
2624 out[7] = a21;
2625 out[8] = a22;
2626 return out;
2627 };
2628
2629 /**
2630 * Scales the mat3 by the dimensions in the given vec2
2631 *
2632 * @param {mat3} out the receiving matrix
2633 * @param {mat3} a the matrix to rotate
2634 * @param {vec2} v the vec2 to scale the matrix by
2635 * @returns {mat3} out
2636 **/
2637 mat3.scale = function(out, a, v) {
2638 var x = v[0], y = v[1];
2639
2640 out[0] = x * a[0];
2641 out[1] = x * a[1];
2642 out[2] = x * a[2];
2643
2644 out[3] = y * a[3];
2645 out[4] = y * a[4];
2646 out[5] = y * a[5];
2647
2648 out[6] = a[6];
2649 out[7] = a[7];
2650 out[8] = a[8];
2651 return out;
2652 };
2653
2654 /**
2655 * Copies the values from a mat2d into a mat3
2656 *
2657 * @param {mat3} out the receiving matrix
2658 * @param {mat2d} a the matrix to copy
2659 * @returns {mat3} out
2660 **/
2661 mat3.fromMat2d = function(out, a) {
2662 out[0] = a[0];
2663 out[1] = a[1];
2664 out[2] = 0;
2665
2666 out[3] = a[2];
2667 out[4] = a[3];
2668 out[5] = 0;
2669
2670 out[6] = a[4];
2671 out[7] = a[5];
2672 out[8] = 1;
2673 return out;
2674 };
2675
2676 /**
2677 * Calculates a 3x3 matrix from the given quaternion
2678 *
2679 * @param {mat3} out mat3 receiving operation result
2680 * @param {quat} q Quaternion to create matrix from
2681 *
2682 * @returns {mat3} out
2683 */
2684 mat3.fromQuat = function (out, q) {
2685 var x = q[0], y = q[1], z = q[2], w = q[3],
2686 x2 = x + x,
2687 y2 = y + y,
2688 z2 = z + z,
2689
2690 xx = x * x2,
2691 yx = y * x2,
2692 yy = y * y2,
2693 zx = z * x2,
2694 zy = z * y2,
2695 zz = z * z2,
2696 wx = w * x2,
2697 wy = w * y2,
2698 wz = w * z2;
2699
2700 out[0] = 1 - yy - zz;
2701 out[3] = yx - wz;
2702 out[6] = zx + wy;
2703
2704 out[1] = yx + wz;
2705 out[4] = 1 - xx - zz;
2706 out[7] = zy - wx;
2707
2708 out[2] = zx - wy;
2709 out[5] = zy + wx;
2710 out[8] = 1 - xx - yy;
2711
2712 return out;
2713 };
2714
2715 /**
2716 * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
2717 *
2718 * @param {mat3} out mat3 receiving operation result
2719 * @param {mat4} a Mat4 to derive the normal matrix from
2720 *
2721 * @returns {mat3} out
2722 */
2723 mat3.normalFromMat4 = function (out, a) {
2724 var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
2725 a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
2726 a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
2727 a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
2728
2729 b00 = a00 * a11 - a01 * a10,
2730 b01 = a00 * a12 - a02 * a10,
2731 b02 = a00 * a13 - a03 * a10,
2732 b03 = a01 * a12 - a02 * a11,
2733 b04 = a01 * a13 - a03 * a11,
2734 b05 = a02 * a13 - a03 * a12,
2735 b06 = a20 * a31 - a21 * a30,
2736 b07 = a20 * a32 - a22 * a30,
2737 b08 = a20 * a33 - a23 * a30,
2738 b09 = a21 * a32 - a22 * a31,
2739 b10 = a21 * a33 - a23 * a31,
2740 b11 = a22 * a33 - a23 * a32,
2741
2742 // Calculate the determinant
2743 det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
2744
2745 if (!det) {
2746 return null;
2747 }
2748 det = 1.0 / det;
2749
2750 out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
2751 out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
2752 out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
2753
2754 out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
2755 out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
2756 out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
2757
2758 out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
2759 out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
2760 out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
2761
2762 return out;
2763 };
2764
2765 /**
2766 * Returns a string representation of a mat3
2767 *
2768 * @param {mat3} mat matrix to represent as a string
2769 * @returns {String} string representation of the matrix
2770 */
2771 mat3.str = function (a) {
2772 return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
2773 a[3] + ', ' + a[4] + ', ' + a[5] + ', ' +
2774 a[6] + ', ' + a[7] + ', ' + a[8] + ')';
2775 };
2776
2777 /**
2778 * Returns Frobenius norm of a mat3
2779 *
2780 * @param {mat3} a the matrix to calculate Frobenius norm of
2781 * @returns {Number} Frobenius norm
2782 */
2783 mat3.frob = function (a) {
2784 return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2)))
2785 };
2786
2787
2788 if(typeof(exports) !== 'undefined') {
2789 exports.mat3 = mat3;
2790 }
2791 ;
2792 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
2793
2794 Redistribution and use in source and binary forms, with or without modification,
2795 are permitted provided that the following conditions are met:
2796
2797 * Redistributions of source code must retain the above copyright notice, this
2798 list of conditions and the following disclaimer.
2799 * Redistributions in binary form must reproduce the above copyright notice,
2800 this list of conditions and the following disclaimer in the documentation
2801 and/or other materials provided with the distribution.
2802
2803 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2804 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2805 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2806 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
2807 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2808 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2809 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2810 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2811 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2812 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
2813
2814 /**
2815 * @class 4x4 Matrix
2816 * @name mat4
2817 */
2818
2819 var mat4 = {};
2820
2821 /**
2822 * Creates a new identity mat4
2823 *
2824 * @returns {mat4} a new 4x4 matrix
2825 */
2826 mat4.create = function() {
2827 var out = new GLMAT_ARRAY_TYPE(16);
2828 out[0] = 1;
2829 out[1] = 0;
2830 out[2] = 0;
2831 out[3] = 0;
2832 out[4] = 0;
2833 out[5] = 1;
2834 out[6] = 0;
2835 out[7] = 0;
2836 out[8] = 0;
2837 out[9] = 0;
2838 out[10] = 1;
2839 out[11] = 0;
2840 out[12] = 0;
2841 out[13] = 0;
2842 out[14] = 0;
2843 out[15] = 1;
2844 return out;
2845 };
2846
2847 /**
2848 * Creates a new mat4 initialized with values from an existing matrix
2849 *
2850 * @param {mat4} a matrix to clone
2851 * @returns {mat4} a new 4x4 matrix
2852 */
2853 mat4.clone = function(a) {
2854 var out = new GLMAT_ARRAY_TYPE(16);
2855 out[0] = a[0];
2856 out[1] = a[1];
2857 out[2] = a[2];
2858 out[3] = a[3];
2859 out[4] = a[4];
2860 out[5] = a[5];
2861 out[6] = a[6];
2862 out[7] = a[7];
2863 out[8] = a[8];
2864 out[9] = a[9];
2865 out[10] = a[10];
2866 out[11] = a[11];
2867 out[12] = a[12];
2868 out[13] = a[13];
2869 out[14] = a[14];
2870 out[15] = a[15];
2871 return out;
2872 };
2873
2874 /**
2875 * Copy the values from one mat4 to another
2876 *
2877 * @param {mat4} out the receiving matrix
2878 * @param {mat4} a the source matrix
2879 * @returns {mat4} out
2880 */
2881 mat4.copy = function(out, a) {
2882 out[0] = a[0];
2883 out[1] = a[1];
2884 out[2] = a[2];
2885 out[3] = a[3];
2886 out[4] = a[4];
2887 out[5] = a[5];
2888 out[6] = a[6];
2889 out[7] = a[7];
2890 out[8] = a[8];
2891 out[9] = a[9];
2892 out[10] = a[10];
2893 out[11] = a[11];
2894 out[12] = a[12];
2895 out[13] = a[13];
2896 out[14] = a[14];
2897 out[15] = a[15];
2898 return out;
2899 };
2900
2901 /**
2902 * Set a mat4 to the identity matrix
2903 *
2904 * @param {mat4} out the receiving matrix
2905 * @returns {mat4} out
2906 */
2907 mat4.identity = function(out) {
2908 out[0] = 1;
2909 out[1] = 0;
2910 out[2] = 0;
2911 out[3] = 0;
2912 out[4] = 0;
2913 out[5] = 1;
2914 out[6] = 0;
2915 out[7] = 0;
2916 out[8] = 0;
2917 out[9] = 0;
2918 out[10] = 1;
2919 out[11] = 0;
2920 out[12] = 0;
2921 out[13] = 0;
2922 out[14] = 0;
2923 out[15] = 1;
2924 return out;
2925 };
2926
2927 /**
2928 * Transpose the values of a mat4
2929 *
2930 * @param {mat4} out the receiving matrix
2931 * @param {mat4} a the source matrix
2932 * @returns {mat4} out
2933 */
2934 mat4.transpose = function(out, a) {
2935 // If we are transposing ourselves we can skip a few steps but have to cache some values
2936 if (out === a) {
2937 var a01 = a[1], a02 = a[2], a03 = a[3],
2938 a12 = a[6], a13 = a[7],
2939 a23 = a[11];
2940
2941 out[1] = a[4];
2942 out[2] = a[8];
2943 out[3] = a[12];
2944 out[4] = a01;
2945 out[6] = a[9];
2946 out[7] = a[13];
2947 out[8] = a02;
2948 out[9] = a12;
2949 out[11] = a[14];
2950 out[12] = a03;
2951 out[13] = a13;
2952 out[14] = a23;
2953 } else {
2954 out[0] = a[0];
2955 out[1] = a[4];
2956 out[2] = a[8];
2957 out[3] = a[12];
2958 out[4] = a[1];
2959 out[5] = a[5];
2960 out[6] = a[9];
2961 out[7] = a[13];
2962 out[8] = a[2];
2963 out[9] = a[6];
2964 out[10] = a[10];
2965 out[11] = a[14];
2966 out[12] = a[3];
2967 out[13] = a[7];
2968 out[14] = a[11];
2969 out[15] = a[15];
2970 }
2971
2972 return out;
2973 };
2974
2975 /**
2976 * Inverts a mat4
2977 *
2978 * @param {mat4} out the receiving matrix
2979 * @param {mat4} a the source matrix
2980 * @returns {mat4} out
2981 */
2982 mat4.invert = function(out, a) {
2983 var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
2984 a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
2985 a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
2986 a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
2987
2988 b00 = a00 * a11 - a01 * a10,
2989 b01 = a00 * a12 - a02 * a10,
2990 b02 = a00 * a13 - a03 * a10,
2991 b03 = a01 * a12 - a02 * a11,
2992 b04 = a01 * a13 - a03 * a11,
2993 b05 = a02 * a13 - a03 * a12,
2994 b06 = a20 * a31 - a21 * a30,
2995 b07 = a20 * a32 - a22 * a30,
2996 b08 = a20 * a33 - a23 * a30,
2997 b09 = a21 * a32 - a22 * a31,
2998 b10 = a21 * a33 - a23 * a31,
2999 b11 = a22 * a33 - a23 * a32,
3000
3001 // Calculate the determinant
3002 det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
3003
3004 if (!det) {
3005 return null;
3006 }
3007 det = 1.0 / det;
3008
3009 out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
3010 out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
3011 out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
3012 out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
3013 out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
3014 out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
3015 out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
3016 out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
3017 out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
3018 out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
3019 out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
3020 out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
3021 out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
3022 out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
3023 out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
3024 out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
3025
3026 return out;
3027 };
3028
3029 /**
3030 * Calculates the adjugate of a mat4
3031 *
3032 * @param {mat4} out the receiving matrix
3033 * @param {mat4} a the source matrix
3034 * @returns {mat4} out
3035 */
3036 mat4.adjoint = function(out, a) {
3037 var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
3038 a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
3039 a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
3040 a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
3041
3042 out[0] = (a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22));
3043 out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22));
3044 out[2] = (a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12));
3045 out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12));
3046 out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22));
3047 out[5] = (a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22));
3048 out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12));
3049 out[7] = (a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12));
3050 out[8] = (a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21));
3051 out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21));
3052 out[10] = (a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11));
3053 out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11));
3054 out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21));
3055 out[13] = (a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21));
3056 out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11));
3057 out[15] = (a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11));
3058 return out;
3059 };
3060
3061 /**
3062 * Calculates the determinant of a mat4
3063 *
3064 * @param {mat4} a the source matrix
3065 * @returns {Number} determinant of a
3066 */
3067 mat4.determinant = function (a) {
3068 var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
3069 a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
3070 a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
3071 a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
3072
3073 b00 = a00 * a11 - a01 * a10,
3074 b01 = a00 * a12 - a02 * a10,
3075 b02 = a00 * a13 - a03 * a10,
3076 b03 = a01 * a12 - a02 * a11,
3077 b04 = a01 * a13 - a03 * a11,
3078 b05 = a02 * a13 - a03 * a12,
3079 b06 = a20 * a31 - a21 * a30,
3080 b07 = a20 * a32 - a22 * a30,
3081 b08 = a20 * a33 - a23 * a30,
3082 b09 = a21 * a32 - a22 * a31,
3083 b10 = a21 * a33 - a23 * a31,
3084 b11 = a22 * a33 - a23 * a32;
3085
3086 // Calculate the determinant
3087 return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
3088 };
3089
3090 /**
3091 * Multiplies two mat4's
3092 *
3093 * @param {mat4} out the receiving matrix
3094 * @param {mat4} a the first operand
3095 * @param {mat4} b the second operand
3096 * @returns {mat4} out
3097 */
3098 mat4.multiply = function (out, a, b) {
3099 var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
3100 a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
3101 a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
3102 a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
3103
3104 // Cache only the current line of the second matrix
3105 var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
3106 out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
3107 out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
3108 out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
3109 out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
3110
3111 b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
3112 out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
3113 out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
3114 out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
3115 out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
3116
3117 b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
3118 out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
3119 out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
3120 out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
3121 out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
3122
3123 b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
3124 out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
3125 out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
3126 out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
3127 out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
3128 return out;
3129 };
3130
3131 /**
3132 * Alias for {@link mat4.multiply}
3133 * @function
3134 */
3135 mat4.mul = mat4.multiply;
3136
3137 /**
3138 * Translate a mat4 by the given vector
3139 *
3140 * @param {mat4} out the receiving matrix
3141 * @param {mat4} a the matrix to translate
3142 * @param {vec3} v vector to translate by
3143 * @returns {mat4} out
3144 */
3145 mat4.translate = function (out, a, v) {
3146 var x = v[0], y = v[1], z = v[2],
3147 a00, a01, a02, a03,
3148 a10, a11, a12, a13,
3149 a20, a21, a22, a23;
3150
3151 if (a === out) {
3152 out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
3153 out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
3154 out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
3155 out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
3156 } else {
3157 a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
3158 a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
3159 a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
3160
3161 out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
3162 out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
3163 out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
3164
3165 out[12] = a00 * x + a10 * y + a20 * z + a[12];
3166 out[13] = a01 * x + a11 * y + a21 * z + a[13];
3167 out[14] = a02 * x + a12 * y + a22 * z + a[14];
3168 out[15] = a03 * x + a13 * y + a23 * z + a[15];
3169 }
3170
3171 return out;
3172 };
3173
3174 /**
3175 * Scales the mat4 by the dimensions in the given vec3
3176 *
3177 * @param {mat4} out the receiving matrix
3178 * @param {mat4} a the matrix to scale
3179 * @param {vec3} v the vec3 to scale the matrix by
3180 * @returns {mat4} out
3181 **/
3182 mat4.scale = function(out, a, v) {
3183 var x = v[0], y = v[1], z = v[2];
3184
3185 out[0] = a[0] * x;
3186 out[1] = a[1] * x;
3187 out[2] = a[2] * x;
3188 out[3] = a[3] * x;
3189 out[4] = a[4] * y;
3190 out[5] = a[5] * y;
3191 out[6] = a[6] * y;
3192 out[7] = a[7] * y;
3193 out[8] = a[8] * z;
3194 out[9] = a[9] * z;
3195 out[10] = a[10] * z;
3196 out[11] = a[11] * z;
3197 out[12] = a[12];
3198 out[13] = a[13];
3199 out[14] = a[14];
3200 out[15] = a[15];
3201 return out;
3202 };
3203
3204 /**
3205 * Rotates a mat4 by the given angle
3206 *
3207 * @param {mat4} out the receiving matrix
3208 * @param {mat4} a the matrix to rotate
3209 * @param {Number} rad the angle to rotate the matrix by
3210 * @param {vec3} axis the axis to rotate around
3211 * @returns {mat4} out
3212 */
3213 mat4.rotate = function (out, a, rad, axis) {
3214 var x = axis[0], y = axis[1], z = axis[2],
3215 len = Math.sqrt(x * x + y * y + z * z),
3216 s, c, t,
3217 a00, a01, a02, a03,
3218 a10, a11, a12, a13,
3219 a20, a21, a22, a23,
3220 b00, b01, b02,
3221 b10, b11, b12,
3222 b20, b21, b22;
3223
3224 if (Math.abs(len) < GLMAT_EPSILON) { return null; }
3225
3226 len = 1 / len;
3227 x *= len;
3228 y *= len;
3229 z *= len;
3230
3231 s = Math.sin(rad);
3232 c = Math.cos(rad);
3233 t = 1 - c;
3234
3235 a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
3236 a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
3237 a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
3238
3239 // Construct the elements of the rotation matrix
3240 b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
3241 b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
3242 b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
3243
3244 // Perform rotation-specific matrix multiplication
3245 out[0] = a00 * b00 + a10 * b01 + a20 * b02;
3246 out[1] = a01 * b00 + a11 * b01 + a21 * b02;
3247 out[2] = a02 * b00 + a12 * b01 + a22 * b02;
3248 out[3] = a03 * b00 + a13 * b01 + a23 * b02;
3249 out[4] = a00 * b10 + a10 * b11 + a20 * b12;
3250 out[5] = a01 * b10 + a11 * b11 + a21 * b12;
3251 out[6] = a02 * b10 + a12 * b11 + a22 * b12;
3252 out[7] = a03 * b10 + a13 * b11 + a23 * b12;
3253 out[8] = a00 * b20 + a10 * b21 + a20 * b22;
3254 out[9] = a01 * b20 + a11 * b21 + a21 * b22;
3255 out[10] = a02 * b20 + a12 * b21 + a22 * b22;
3256 out[11] = a03 * b20 + a13 * b21 + a23 * b22;
3257
3258 if (a !== out) { // If the source and destination differ, copy the unchanged last row
3259 out[12] = a[12];
3260 out[13] = a[13];
3261 out[14] = a[14];
3262 out[15] = a[15];
3263 }
3264 return out;
3265 };
3266
3267 /**
3268 * Rotates a matrix by the given angle around the X axis
3269 *
3270 * @param {mat4} out the receiving matrix
3271 * @param {mat4} a the matrix to rotate
3272 * @param {Number} rad the angle to rotate the matrix by
3273 * @returns {mat4} out
3274 */
3275 mat4.rotateX = function (out, a, rad) {
3276 var s = Math.sin(rad),
3277 c = Math.cos(rad),
3278 a10 = a[4],
3279 a11 = a[5],
3280 a12 = a[6],
3281 a13 = a[7],
3282 a20 = a[8],
3283 a21 = a[9],
3284 a22 = a[10],
3285 a23 = a[11];
3286
3287 if (a !== out) { // If the source and destination differ, copy the unchanged rows
3288 out[0] = a[0];
3289 out[1] = a[1];
3290 out[2] = a[2];
3291 out[3] = a[3];
3292 out[12] = a[12];
3293 out[13] = a[13];
3294 out[14] = a[14];
3295 out[15] = a[15];
3296 }
3297
3298 // Perform axis-specific matrix multiplication
3299 out[4] = a10 * c + a20 * s;
3300 out[5] = a11 * c + a21 * s;
3301 out[6] = a12 * c + a22 * s;
3302 out[7] = a13 * c + a23 * s;
3303 out[8] = a20 * c - a10 * s;
3304 out[9] = a21 * c - a11 * s;
3305 out[10] = a22 * c - a12 * s;
3306 out[11] = a23 * c - a13 * s;
3307 return out;
3308 };
3309
3310 /**
3311 * Rotates a matrix by the given angle around the Y axis
3312 *
3313 * @param {mat4} out the receiving matrix
3314 * @param {mat4} a the matrix to rotate
3315 * @param {Number} rad the angle to rotate the matrix by
3316 * @returns {mat4} out
3317 */
3318 mat4.rotateY = function (out, a, rad) {
3319 var s = Math.sin(rad),
3320 c = Math.cos(rad),
3321 a00 = a[0],
3322 a01 = a[1],
3323 a02 = a[2],
3324 a03 = a[3],
3325 a20 = a[8],
3326 a21 = a[9],
3327 a22 = a[10],
3328 a23 = a[11];
3329
3330 if (a !== out) { // If the source and destination differ, copy the unchanged rows
3331 out[4] = a[4];
3332 out[5] = a[5];
3333 out[6] = a[6];
3334 out[7] = a[7];
3335 out[12] = a[12];
3336 out[13] = a[13];
3337 out[14] = a[14];
3338 out[15] = a[15];
3339 }
3340
3341 // Perform axis-specific matrix multiplication
3342 out[0] = a00 * c - a20 * s;
3343 out[1] = a01 * c - a21 * s;
3344 out[2] = a02 * c - a22 * s;
3345 out[3] = a03 * c - a23 * s;
3346 out[8] = a00 * s + a20 * c;
3347 out[9] = a01 * s + a21 * c;
3348 out[10] = a02 * s + a22 * c;
3349 out[11] = a03 * s + a23 * c;
3350 return out;
3351 };
3352
3353 /**
3354 * Rotates a matrix by the given angle around the Z axis
3355 *
3356 * @param {mat4} out the receiving matrix
3357 * @param {mat4} a the matrix to rotate
3358 * @param {Number} rad the angle to rotate the matrix by
3359 * @returns {mat4} out
3360 */
3361 mat4.rotateZ = function (out, a, rad) {
3362 var s = Math.sin(rad),
3363 c = Math.cos(rad),
3364 a00 = a[0],
3365 a01 = a[1],
3366 a02 = a[2],
3367 a03 = a[3],
3368 a10 = a[4],
3369 a11 = a[5],
3370 a12 = a[6],
3371 a13 = a[7];
3372
3373 if (a !== out) { // If the source and destination differ, copy the unchanged last row
3374 out[8] = a[8];
3375 out[9] = a[9];
3376 out[10] = a[10];
3377 out[11] = a[11];
3378 out[12] = a[12];
3379 out[13] = a[13];
3380 out[14] = a[14];
3381 out[15] = a[15];
3382 }
3383
3384 // Perform axis-specific matrix multiplication
3385 out[0] = a00 * c + a10 * s;
3386 out[1] = a01 * c + a11 * s;
3387 out[2] = a02 * c + a12 * s;
3388 out[3] = a03 * c + a13 * s;
3389 out[4] = a10 * c - a00 * s;
3390 out[5] = a11 * c - a01 * s;
3391 out[6] = a12 * c - a02 * s;
3392 out[7] = a13 * c - a03 * s;
3393 return out;
3394 };
3395
3396 /**
3397 * Creates a matrix from a quaternion rotation and vector translation
3398 * This is equivalent to (but much faster than):
3399 *
3400 * mat4.identity(dest);
3401 * mat4.translate(dest, vec);
3402 * var quatMat = mat4.create();
3403 * quat4.toMat4(quat, quatMat);
3404 * mat4.multiply(dest, quatMat);
3405 *
3406 * @param {mat4} out mat4 receiving operation result
3407 * @param {quat4} q Rotation quaternion
3408 * @param {vec3} v Translation vector
3409 * @returns {mat4} out
3410 */
3411 mat4.fromRotationTranslation = function (out, q, v) {
3412 // Quaternion math
3413 var x = q[0], y = q[1], z = q[2], w = q[3],
3414 x2 = x + x,
3415 y2 = y + y,
3416 z2 = z + z,
3417
3418 xx = x * x2,
3419 xy = x * y2,
3420 xz = x * z2,
3421 yy = y * y2,
3422 yz = y * z2,
3423 zz = z * z2,
3424 wx = w * x2,
3425 wy = w * y2,
3426 wz = w * z2;
3427
3428 out[0] = 1 - (yy + zz);
3429 out[1] = xy + wz;
3430 out[2] = xz - wy;
3431 out[3] = 0;
3432 out[4] = xy - wz;
3433 out[5] = 1 - (xx + zz);
3434 out[6] = yz + wx;
3435 out[7] = 0;
3436 out[8] = xz + wy;
3437 out[9] = yz - wx;
3438 out[10] = 1 - (xx + yy);
3439 out[11] = 0;
3440 out[12] = v[0];
3441 out[13] = v[1];
3442 out[14] = v[2];
3443 out[15] = 1;
3444
3445 return out;
3446 };
3447
3448 mat4.fromQuat = function (out, q) {
3449 var x = q[0], y = q[1], z = q[2], w = q[3],
3450 x2 = x + x,
3451 y2 = y + y,
3452 z2 = z + z,
3453
3454 xx = x * x2,
3455 yx = y * x2,
3456 yy = y * y2,
3457 zx = z * x2,
3458 zy = z * y2,
3459 zz = z * z2,
3460 wx = w * x2,
3461 wy = w * y2,
3462 wz = w * z2;
3463
3464 out[0] = 1 - yy - zz;
3465 out[1] = yx + wz;
3466 out[2] = zx - wy;
3467 out[3] = 0;
3468
3469 out[4] = yx - wz;
3470 out[5] = 1 - xx - zz;
3471 out[6] = zy + wx;
3472 out[7] = 0;
3473
3474 out[8] = zx + wy;
3475 out[9] = zy - wx;
3476 out[10] = 1 - xx - yy;
3477 out[11] = 0;
3478
3479 out[12] = 0;
3480 out[13] = 0;
3481 out[14] = 0;
3482 out[15] = 1;
3483
3484 return out;
3485 };
3486
3487 /**
3488 * Generates a frustum matrix with the given bounds
3489 *
3490 * @param {mat4} out mat4 frustum matrix will be written into
3491 * @param {Number} left Left bound of the frustum
3492 * @param {Number} right Right bound of the frustum
3493 * @param {Number} bottom Bottom bound of the frustum
3494 * @param {Number} top Top bound of the frustum
3495 * @param {Number} near Near bound of the frustum
3496 * @param {Number} far Far bound of the frustum
3497 * @returns {mat4} out
3498 */
3499 mat4.frustum = function (out, left, right, bottom, top, near, far) {
3500 var rl = 1 / (right - left),
3501 tb = 1 / (top - bottom),
3502 nf = 1 / (near - far);
3503 out[0] = (near * 2) * rl;
3504 out[1] = 0;
3505 out[2] = 0;
3506 out[3] = 0;
3507 out[4] = 0;
3508 out[5] = (near * 2) * tb;
3509 out[6] = 0;
3510 out[7] = 0;
3511 out[8] = (right + left) * rl;
3512 out[9] = (top + bottom) * tb;
3513 out[10] = (far + near) * nf;
3514 out[11] = -1;
3515 out[12] = 0;
3516 out[13] = 0;
3517 out[14] = (far * near * 2) * nf;
3518 out[15] = 0;
3519 return out;
3520 };
3521
3522 /**
3523 * Generates a perspective projection matrix with the given bounds
3524 *
3525 * @param {mat4} out mat4 frustum matrix will be written into
3526 * @param {number} fovy Vertical field of view in radians
3527 * @param {number} aspect Aspect ratio. typically viewport width/height
3528 * @param {number} near Near bound of the frustum
3529 * @param {number} far Far bound of the frustum
3530 * @returns {mat4} out
3531 */
3532 mat4.perspective = function (out, fovy, aspect, near, far) {
3533 var f = 1.0 / Math.tan(fovy / 2),
3534 nf = 1 / (near - far);
3535 out[0] = f / aspect;
3536 out[1] = 0;
3537 out[2] = 0;
3538 out[3] = 0;
3539 out[4] = 0;
3540 out[5] = f;
3541 out[6] = 0;
3542 out[7] = 0;
3543 out[8] = 0;
3544 out[9] = 0;
3545 out[10] = (far + near) * nf;
3546 out[11] = -1;
3547 out[12] = 0;
3548 out[13] = 0;
3549 out[14] = (2 * far * near) * nf;
3550 out[15] = 0;
3551 return out;
3552 };
3553
3554 /**
3555 * Generates a orthogonal projection matrix with the given bounds
3556 *
3557 * @param {mat4} out mat4 frustum matrix will be written into
3558 * @param {number} left Left bound of the frustum
3559 * @param {number} right Right bound of the frustum
3560 * @param {number} bottom Bottom bound of the frustum
3561 * @param {number} top Top bound of the frustum
3562 * @param {number} near Near bound of the frustum
3563 * @param {number} far Far bound of the frustum
3564 * @returns {mat4} out
3565 */
3566 mat4.ortho = function (out, left, right, bottom, top, near, far) {
3567 var lr = 1 / (left - right),
3568 bt = 1 / (bottom - top),
3569 nf = 1 / (near - far);
3570 out[0] = -2 * lr;
3571 out[1] = 0;
3572 out[2] = 0;
3573 out[3] = 0;
3574 out[4] = 0;
3575 out[5] = -2 * bt;
3576 out[6] = 0;
3577 out[7] = 0;
3578 out[8] = 0;
3579 out[9] = 0;
3580 out[10] = 2 * nf;
3581 out[11] = 0;
3582 out[12] = (left + right) * lr;
3583 out[13] = (top + bottom) * bt;
3584 out[14] = (far + near) * nf;
3585 out[15] = 1;
3586 return out;
3587 };
3588
3589 /**
3590 * Generates a look-at matrix with the given eye position, focal point, and up axis
3591 *
3592 * @param {mat4} out mat4 frustum matrix will be written into
3593 * @param {vec3} eye Position of the viewer
3594 * @param {vec3} center Point the viewer is looking at
3595 * @param {vec3} up vec3 pointing up
3596 * @returns {mat4} out
3597 */
3598 mat4.lookAt = function (out, eye, center, up) {
3599 var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
3600 eyex = eye[0],
3601 eyey = eye[1],
3602 eyez = eye[2],
3603 upx = up[0],
3604 upy = up[1],
3605 upz = up[2],
3606 centerx = center[0],
3607 centery = center[1],
3608 centerz = center[2];
3609
3610 if (Math.abs(eyex - centerx) < GLMAT_EPSILON &&
3611 Math.abs(eyey - centery) < GLMAT_EPSILON &&
3612 Math.abs(eyez - centerz) < GLMAT_EPSILON) {
3613 return mat4.identity(out);
3614 }
3615
3616 z0 = eyex - centerx;
3617 z1 = eyey - centery;
3618 z2 = eyez - centerz;
3619
3620 len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
3621 z0 *= len;
3622 z1 *= len;
3623 z2 *= len;
3624
3625 x0 = upy * z2 - upz * z1;
3626 x1 = upz * z0 - upx * z2;
3627 x2 = upx * z1 - upy * z0;
3628 len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
3629 if (!len) {
3630 x0 = 0;
3631 x1 = 0;
3632 x2 = 0;
3633 } else {
3634 len = 1 / len;
3635 x0 *= len;
3636 x1 *= len;
3637 x2 *= len;
3638 }
3639
3640 y0 = z1 * x2 - z2 * x1;
3641 y1 = z2 * x0 - z0 * x2;
3642 y2 = z0 * x1 - z1 * x0;
3643
3644 len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
3645 if (!len) {
3646 y0 = 0;
3647 y1 = 0;
3648 y2 = 0;
3649 } else {
3650 len = 1 / len;
3651 y0 *= len;
3652 y1 *= len;
3653 y2 *= len;
3654 }
3655
3656 out[0] = x0;
3657 out[1] = y0;
3658 out[2] = z0;
3659 out[3] = 0;
3660 out[4] = x1;
3661 out[5] = y1;
3662 out[6] = z1;
3663 out[7] = 0;
3664 out[8] = x2;
3665 out[9] = y2;
3666 out[10] = z2;
3667 out[11] = 0;
3668 out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
3669 out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
3670 out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
3671 out[15] = 1;
3672
3673 return out;
3674 };
3675
3676 /**
3677 * Returns a string representation of a mat4
3678 *
3679 * @param {mat4} mat matrix to represent as a string
3680 * @returns {String} string representation of the matrix
3681 */
3682 mat4.str = function (a) {
3683 return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' +
3684 a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' +
3685 a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' +
3686 a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')';
3687 };
3688
3689 /**
3690 * Returns Frobenius norm of a mat4
3691 *
3692 * @param {mat4} a the matrix to calculate Frobenius norm of
3693 * @returns {Number} Frobenius norm
3694 */
3695 mat4.frob = function (a) {
3696 return(Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2) + Math.pow(a[9], 2) + Math.pow(a[10], 2) + Math.pow(a[11], 2) + Math.pow(a[12], 2) + Math.pow(a[13], 2) + Math.pow(a[14], 2) + Math.pow(a[15], 2) ))
3697 };
3698
3699
3700 if(typeof(exports) !== 'undefined') {
3701 exports.mat4 = mat4;
3702 }
3703 ;
3704 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3705
3706 Redistribution and use in source and binary forms, with or without modification,
3707 are permitted provided that the following conditions are met:
3708
3709 * Redistributions of source code must retain the above copyright notice, this
3710 list of conditions and the following disclaimer.
3711 * Redistributions in binary form must reproduce the above copyright notice,
3712 this list of conditions and the following disclaimer in the documentation
3713 and/or other materials provided with the distribution.
3714
3715 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
3716 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
3717 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3718 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
3719 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
3720 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3721 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
3722 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3723 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3724 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
3725
3726 /**
3727 * @class Quaternion
3728 * @name quat
3729 */
3730
3731 var quat = {};
3732
3733 /**
3734 * Creates a new identity quat
3735 *
3736 * @returns {quat} a new quaternion
3737 */
3738 quat.create = function() {
3739 var out = new GLMAT_ARRAY_TYPE(4);
3740 out[0] = 0;
3741 out[1] = 0;
3742 out[2] = 0;
3743 out[3] = 1;
3744 return out;
3745 };
3746
3747 /**
3748 * Sets a quaternion to represent the shortest rotation from one
3749 * vector to another.
3750 *
3751 * Both vectors are assumed to be unit length.
3752 *
3753 * @param {quat} out the receiving quaternion.
3754 * @param {vec3} a the initial vector
3755 * @param {vec3} b the destination vector
3756 * @returns {quat} out
3757 */
3758 quat.rotationTo = (function() {
3759 var tmpvec3 = vec3.create();
3760 var xUnitVec3 = vec3.fromValues(1,0,0);
3761 var yUnitVec3 = vec3.fromValues(0,1,0);
3762
3763 return function(out, a, b) {
3764 var dot = vec3.dot(a, b);
3765 if (dot < -0.999999) {
3766 vec3.cross(tmpvec3, xUnitVec3, a);
3767 if (vec3.length(tmpvec3) < 0.000001)
3768 vec3.cross(tmpvec3, yUnitVec3, a);
3769 vec3.normalize(tmpvec3, tmpvec3);
3770 quat.setAxisAngle(out, tmpvec3, Math.PI);
3771 return out;
3772 } else if (dot > 0.999999) {
3773 out[0] = 0;
3774 out[1] = 0;
3775 out[2] = 0;
3776 out[3] = 1;
3777 return out;
3778 } else {
3779 vec3.cross(tmpvec3, a, b);
3780 out[0] = tmpvec3[0];
3781 out[1] = tmpvec3[1];
3782 out[2] = tmpvec3[2];
3783 out[3] = 1 + dot;
3784 return quat.normalize(out, out);
3785 }
3786 };
3787 })();
3788
3789 /**
3790 * Sets the specified quaternion with values corresponding to the given
3791 * axes. Each axis is a vec3 and is expected to be unit length and
3792 * perpendicular to all other specified axes.
3793 *
3794 * @param {vec3} view the vector representing the viewing direction
3795 * @param {vec3} right the vector representing the local "right" direction
3796 * @param {vec3} up the vector representing the local "up" direction
3797 * @returns {quat} out
3798 */
3799 quat.setAxes = (function() {
3800 var matr = mat3.create();
3801
3802 return function(out, view, right, up) {
3803 matr[0] = right[0];
3804 matr[3] = right[1];
3805 matr[6] = right[2];
3806
3807 matr[1] = up[0];
3808 matr[4] = up[1];
3809 matr[7] = up[2];
3810
3811 matr[2] = -view[0];
3812 matr[5] = -view[1];
3813 matr[8] = -view[2];
3814
3815 return quat.normalize(out, quat.fromMat3(out, matr));
3816 };
3817 })();
3818
3819 /**
3820 * Creates a new quat initialized with values from an existing quaternion
3821 *
3822 * @param {quat} a quaternion to clone
3823 * @returns {quat} a new quaternion
3824 * @function
3825 */
3826 quat.clone = vec4.clone;
3827
3828 /**
3829 * Creates a new quat initialized with the given values
3830 *
3831 * @param {Number} x X component
3832 * @param {Number} y Y component
3833 * @param {Number} z Z component
3834 * @param {Number} w W component
3835 * @returns {quat} a new quaternion
3836 * @function
3837 */
3838 quat.fromValues = vec4.fromValues;
3839
3840 /**
3841 * Copy the values from one quat to another
3842 *
3843 * @param {quat} out the receiving quaternion
3844 * @param {quat} a the source quaternion
3845 * @returns {quat} out
3846 * @function
3847 */
3848 quat.copy = vec4.copy;
3849
3850 /**
3851 * Set the components of a quat to the given values
3852 *
3853 * @param {quat} out the receiving quaternion
3854 * @param {Number} x X component
3855 * @param {Number} y Y component
3856 * @param {Number} z Z component
3857 * @param {Number} w W component
3858 * @returns {quat} out
3859 * @function
3860 */
3861 quat.set = vec4.set;
3862
3863 /**
3864 * Set a quat to the identity quaternion
3865 *
3866 * @param {quat} out the receiving quaternion
3867 * @returns {quat} out
3868 */
3869 quat.identity = function(out) {
3870 out[0] = 0;
3871 out[1] = 0;
3872 out[2] = 0;
3873 out[3] = 1;
3874 return out;
3875 };
3876
3877 /**
3878 * Sets a quat from the given angle and rotation axis,
3879 * then returns it.
3880 *
3881 * @param {quat} out the receiving quaternion
3882 * @param {vec3} axis the axis around which to rotate
3883 * @param {Number} rad the angle in radians
3884 * @returns {quat} out
3885 **/
3886 quat.setAxisAngle = function(out, axis, rad) {
3887 rad = rad * 0.5;
3888 var s = Math.sin(rad);
3889 out[0] = s * axis[0];
3890 out[1] = s * axis[1];
3891 out[2] = s * axis[2];
3892 out[3] = Math.cos(rad);
3893 return out;
3894 };
3895
3896 /**
3897 * Adds two quat's
3898 *
3899 * @param {quat} out the receiving quaternion
3900 * @param {quat} a the first operand
3901 * @param {quat} b the second operand
3902 * @returns {quat} out
3903 * @function
3904 */
3905 quat.add = vec4.add;
3906
3907 /**
3908 * Multiplies two quat's
3909 *
3910 * @param {quat} out the receiving quaternion
3911 * @param {quat} a the first operand
3912 * @param {quat} b the second operand
3913 * @returns {quat} out
3914 */
3915 quat.multiply = function(out, a, b) {
3916 var ax = a[0], ay = a[1], az = a[2], aw = a[3],
3917 bx = b[0], by = b[1], bz = b[2], bw = b[3];
3918
3919 out[0] = ax * bw + aw * bx + ay * bz - az * by;
3920 out[1] = ay * bw + aw * by + az * bx - ax * bz;
3921 out[2] = az * bw + aw * bz + ax * by - ay * bx;
3922 out[3] = aw * bw - ax * bx - ay * by - az * bz;
3923 return out;
3924 };
3925
3926 /**
3927 * Alias for {@link quat.multiply}
3928 * @function
3929 */
3930 quat.mul = quat.multiply;
3931
3932 /**
3933 * Scales a quat by a scalar number
3934 *
3935 * @param {quat} out the receiving vector
3936 * @param {quat} a the vector to scale
3937 * @param {Number} b amount to scale the vector by
3938 * @returns {quat} out
3939 * @function
3940 */
3941 quat.scale = vec4.scale;
3942
3943 /**
3944 * Rotates a quaternion by the given angle about the X axis
3945 *
3946 * @param {quat} out quat receiving operation result
3947 * @param {quat} a quat to rotate
3948 * @param {number} rad angle (in radians) to rotate
3949 * @returns {quat} out
3950 */
3951 quat.rotateX = function (out, a, rad) {
3952 rad *= 0.5;
3953
3954 var ax = a[0], ay = a[1], az = a[2], aw = a[3],
3955 bx = Math.sin(rad), bw = Math.cos(rad);
3956
3957 out[0] = ax * bw + aw * bx;
3958 out[1] = ay * bw + az * bx;
3959 out[2] = az * bw - ay * bx;
3960 out[3] = aw * bw - ax * bx;
3961 return out;
3962 };
3963
3964 /**
3965 * Rotates a quaternion by the given angle about the Y axis
3966 *
3967 * @param {quat} out quat receiving operation result
3968 * @param {quat} a quat to rotate
3969 * @param {number} rad angle (in radians) to rotate
3970 * @returns {quat} out
3971 */
3972 quat.rotateY = function (out, a, rad) {
3973 rad *= 0.5;
3974
3975 var ax = a[0], ay = a[1], az = a[2], aw = a[3],
3976 by = Math.sin(rad), bw = Math.cos(rad);
3977
3978 out[0] = ax * bw - az * by;
3979 out[1] = ay * bw + aw * by;
3980 out[2] = az * bw + ax * by;
3981 out[3] = aw * bw - ay * by;
3982 return out;
3983 };
3984
3985 /**
3986 * Rotates a quaternion by the given angle about the Z axis
3987 *
3988 * @param {quat} out quat receiving operation result
3989 * @param {quat} a quat to rotate
3990 * @param {number} rad angle (in radians) to rotate
3991 * @returns {quat} out
3992 */
3993 quat.rotateZ = function (out, a, rad) {
3994 rad *= 0.5;
3995
3996 var ax = a[0], ay = a[1], az = a[2], aw = a[3],
3997 bz = Math.sin(rad), bw = Math.cos(rad);
3998
3999 out[0] = ax * bw + ay * bz;
4000 out[1] = ay * bw - ax * bz;
4001 out[2] = az * bw + aw * bz;
4002 out[3] = aw * bw - az * bz;
4003 return out;
4004 };
4005
4006 /**
4007 * Calculates the W component of a quat from the X, Y, and Z components.
4008 * Assumes that quaternion is 1 unit in length.
4009 * Any existing W component will be ignored.
4010 *
4011 * @param {quat} out the receiving quaternion
4012 * @param {quat} a quat to calculate W component of
4013 * @returns {quat} out
4014 */
4015 quat.calculateW = function (out, a) {
4016 var x = a[0], y = a[1], z = a[2];
4017
4018 out[0] = x;
4019 out[1] = y;
4020 out[2] = z;
4021 out[3] = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
4022 return out;
4023 };
4024
4025 /**
4026 * Calculates the dot product of two quat's
4027 *
4028 * @param {quat} a the first operand
4029 * @param {quat} b the second operand
4030 * @returns {Number} dot product of a and b
4031 * @function
4032 */
4033 quat.dot = vec4.dot;
4034
4035 /**
4036 * Performs a linear interpolation between two quat's
4037 *
4038 * @param {quat} out the receiving quaternion
4039 * @param {quat} a the first operand
4040 * @param {quat} b the second operand
4041 * @param {Number} t interpolation amount between the two inputs
4042 * @returns {quat} out
4043 * @function
4044 */
4045 quat.lerp = vec4.lerp;
4046
4047 /**
4048 * Performs a spherical linear interpolation between two quat
4049 *
4050 * @param {quat} out the receiving quaternion
4051 * @param {quat} a the first operand
4052 * @param {quat} b the second operand
4053 * @param {Number} t interpolation amount between the two inputs
4054 * @returns {quat} out
4055 */
4056 quat.slerp = function (out, a, b, t) {
4057 // benchmarks:
4058 // http://jsperf.com/quaternion-slerp-implementations
4059
4060 var ax = a[0], ay = a[1], az = a[2], aw = a[3],
4061 bx = b[0], by = b[1], bz = b[2], bw = b[3];
4062
4063 var omega, cosom, sinom, scale0, scale1;
4064
4065 // calc cosine
4066 cosom = ax * bx + ay * by + az * bz + aw * bw;
4067 // adjust signs (if necessary)
4068 if ( cosom < 0.0 ) {
4069 cosom = -cosom;
4070 bx = - bx;
4071 by = - by;
4072 bz = - bz;
4073 bw = - bw;
4074 }
4075 // calculate coefficients
4076 if ( (1.0 - cosom) > 0.000001 ) {
4077 // standard case (slerp)
4078 omega = Math.acos(cosom);
4079 sinom = Math.sin(omega);
4080 scale0 = Math.sin((1.0 - t) * omega) / sinom;
4081 scale1 = Math.sin(t * omega) / sinom;
4082 } else {
4083 // "from" and "to" quaternions are very close
4084 // ... so we can do a linear interpolation
4085 scale0 = 1.0 - t;
4086 scale1 = t;
4087 }
4088 // calculate final values
4089 out[0] = scale0 * ax + scale1 * bx;
4090 out[1] = scale0 * ay + scale1 * by;
4091 out[2] = scale0 * az + scale1 * bz;
4092 out[3] = scale0 * aw + scale1 * bw;
4093
4094 return out;
4095 };
4096
4097 /**
4098 * Calculates the inverse of a quat
4099 *
4100 * @param {quat} out the receiving quaternion
4101 * @param {quat} a quat to calculate inverse of
4102 * @returns {quat} out
4103 */
4104 quat.invert = function(out, a) {
4105 var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
4106 dot = a0*a0 + a1*a1 + a2*a2 + a3*a3,
4107 invDot = dot ? 1.0/dot : 0;
4108
4109 // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
4110
4111 out[0] = -a0*invDot;
4112 out[1] = -a1*invDot;
4113 out[2] = -a2*invDot;
4114 out[3] = a3*invDot;
4115 return out;
4116 };
4117
4118 /**
4119 * Calculates the conjugate of a quat
4120 * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
4121 *
4122 * @param {quat} out the receiving quaternion
4123 * @param {quat} a quat to calculate conjugate of
4124 * @returns {quat} out
4125 */
4126 quat.conjugate = function (out, a) {
4127 out[0] = -a[0];
4128 out[1] = -a[1];
4129 out[2] = -a[2];
4130 out[3] = a[3];
4131 return out;
4132 };
4133
4134 /**
4135 * Calculates the length of a quat
4136 *
4137 * @param {quat} a vector to calculate length of
4138 * @returns {Number} length of a
4139 * @function
4140 */
4141 quat.length = vec4.length;
4142
4143 /**
4144 * Alias for {@link quat.length}
4145 * @function
4146 */
4147 quat.len = quat.length;
4148
4149 /**
4150 * Calculates the squared length of a quat
4151 *
4152 * @param {quat} a vector to calculate squared length of
4153 * @returns {Number} squared length of a
4154 * @function
4155 */
4156 quat.squaredLength = vec4.squaredLength;
4157
4158 /**
4159 * Alias for {@link quat.squaredLength}
4160 * @function
4161 */
4162 quat.sqrLen = quat.squaredLength;
4163
4164 /**
4165 * Normalize a quat
4166 *
4167 * @param {quat} out the receiving quaternion
4168 * @param {quat} a quaternion to normalize
4169 * @returns {quat} out
4170 * @function
4171 */
4172 quat.normalize = vec4.normalize;
4173
4174 /**
4175 * Creates a quaternion from the given 3x3 rotation matrix.
4176 *
4177 * NOTE: The resultant quaternion is not normalized, so you should be sure
4178 * to renormalize the quaternion yourself where necessary.
4179 *
4180 * @param {quat} out the receiving quaternion
4181 * @param {mat3} m rotation matrix
4182 * @returns {quat} out
4183 * @function
4184 */
4185 quat.fromMat3 = function(out, m) {
4186 // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
4187 // article "Quaternion Calculus and Fast Animation".
4188 var fTrace = m[0] + m[4] + m[8];
4189 var fRoot;
4190
4191 if ( fTrace > 0.0 ) {
4192 // |w| > 1/2, may as well choose w > 1/2
4193 fRoot = Math.sqrt(fTrace + 1.0); // 2w
4194 out[3] = 0.5 * fRoot;
4195 fRoot = 0.5/fRoot; // 1/(4w)
4196 out[0] = (m[7]-m[5])*fRoot;
4197 out[1] = (m[2]-m[6])*fRoot;
4198 out[2] = (m[3]-m[1])*fRoot;
4199 } else {
4200 // |w| <= 1/2
4201 var i = 0;
4202 if ( m[4] > m[0] )
4203 i = 1;
4204 if ( m[8] > m[i*3+i] )
4205 i = 2;
4206 var j = (i+1)%3;
4207 var k = (i+2)%3;
4208
4209 fRoot = Math.sqrt(m[i*3+i]-m[j*3+j]-m[k*3+k] + 1.0);
4210 out[i] = 0.5 * fRoot;
4211 fRoot = 0.5 / fRoot;
4212 out[3] = (m[k*3+j] - m[j*3+k]) * fRoot;
4213 out[j] = (m[j*3+i] + m[i*3+j]) * fRoot;
4214 out[k] = (m[k*3+i] + m[i*3+k]) * fRoot;
4215 }
4216
4217 return out;
4218 };
4219
4220 /**
4221 * Returns a string representation of a quatenion
4222 *
4223 * @param {quat} vec vector to represent as a string
4224 * @returns {String} string representation of the vector
4225 */
4226 quat.str = function (a) {
4227 return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
4228 };
4229
4230 if(typeof(exports) !== 'undefined') {
4231 exports.quat = quat;
4232 }
4233 ;
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247 })(shim.exports);
4248 })(this);