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