1 /* Copyright (c) 2013, Brandon Jones, Colin MacKenzie IV. All rights reserved.
3 Redistribution and use in source and binary forms, with or without modification,
4 are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright notice, this
7 list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright notice,
9 this list of conditions and the following disclaimer in the documentation
10 and/or other materials provided with the distribution.
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
30 * Creates a new identity mat3
32 * @returns {mat3} a new 3x3 matrix
34 mat3.create = function() {
35 var out = new GLMAT_ARRAY_TYPE(9);
49 * Copies the upper-left 3x3 values into the given mat3.
51 * @param {mat3} out the receiving 3x3 matrix
52 * @param {mat4} a the source 4x4 matrix
55 mat3.fromMat4 = function(out, a) {
69 * Creates a new mat3 initialized with values from an existing matrix
71 * @param {mat3} a matrix to clone
72 * @returns {mat3} a new 3x3 matrix
74 mat3.clone = function(a) {
75 var out = new GLMAT_ARRAY_TYPE(9);
89 * Copy the values from one mat3 to another
91 * @param {mat3} out the receiving matrix
92 * @param {mat3} a the source matrix
95 mat3.copy = function(out, a) {
109 * Set a mat3 to the identity matrix
111 * @param {mat3} out the receiving matrix
112 * @returns {mat3} out
114 mat3.identity = function(out) {
128 * Transpose the values of a mat3
130 * @param {mat3} out the receiving matrix
131 * @param {mat3} a the source matrix
132 * @returns {mat3} out
134 mat3.transpose = function(out, a) {
135 // If we are transposing ourselves we can skip a few steps but have to cache some values
137 var a01 = a[1], a02 = a[2], a12 = a[5];
162 * @param {mat3} out the receiving matrix
163 * @param {mat3} a the source matrix
164 * @returns {mat3} out
166 mat3.invert = function(out, a) {
167 var a00 = a[0], a01 = a[1], a02 = a[2],
168 a10 = a[3], a11 = a[4], a12 = a[5],
169 a20 = a[6], a21 = a[7], a22 = a[8],
171 b01 = a22 * a11 - a12 * a21,
172 b11 = -a22 * a10 + a12 * a20,
173 b21 = a21 * a10 - a11 * a20,
175 // Calculate the determinant
176 det = a00 * b01 + a01 * b11 + a02 * b21;
184 out[1] = (-a22 * a01 + a02 * a21) * det;
185 out[2] = (a12 * a01 - a02 * a11) * det;
187 out[4] = (a22 * a00 - a02 * a20) * det;
188 out[5] = (-a12 * a00 + a02 * a10) * det;
190 out[7] = (-a21 * a00 + a01 * a20) * det;
191 out[8] = (a11 * a00 - a01 * a10) * det;
196 * Calculates the adjugate of a mat3
198 * @param {mat3} out the receiving matrix
199 * @param {mat3} a the source matrix
200 * @returns {mat3} out
202 mat3.adjoint = function(out, a) {
203 var a00 = a[0], a01 = a[1], a02 = a[2],
204 a10 = a[3], a11 = a[4], a12 = a[5],
205 a20 = a[6], a21 = a[7], a22 = a[8];
207 out[0] = (a11 * a22 - a12 * a21);
208 out[1] = (a02 * a21 - a01 * a22);
209 out[2] = (a01 * a12 - a02 * a11);
210 out[3] = (a12 * a20 - a10 * a22);
211 out[4] = (a00 * a22 - a02 * a20);
212 out[5] = (a02 * a10 - a00 * a12);
213 out[6] = (a10 * a21 - a11 * a20);
214 out[7] = (a01 * a20 - a00 * a21);
215 out[8] = (a00 * a11 - a01 * a10);
220 * Calculates the determinant of a mat3
222 * @param {mat3} a the source matrix
223 * @returns {Number} determinant of a
225 mat3.determinant = function (a) {
226 var a00 = a[0], a01 = a[1], a02 = a[2],
227 a10 = a[3], a11 = a[4], a12 = a[5],
228 a20 = a[6], a21 = a[7], a22 = a[8];
230 return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
234 * Multiplies two mat3's
236 * @param {mat3} out the receiving matrix
237 * @param {mat3} a the first operand
238 * @param {mat3} b the second operand
239 * @returns {mat3} out
241 mat3.multiply = function (out, a, b) {
242 var a00 = a[0], a01 = a[1], a02 = a[2],
243 a10 = a[3], a11 = a[4], a12 = a[5],
244 a20 = a[6], a21 = a[7], a22 = a[8],
246 b00 = b[0], b01 = b[1], b02 = b[2],
247 b10 = b[3], b11 = b[4], b12 = b[5],
248 b20 = b[6], b21 = b[7], b22 = b[8];
250 out[0] = b00 * a00 + b01 * a10 + b02 * a20;
251 out[1] = b00 * a01 + b01 * a11 + b02 * a21;
252 out[2] = b00 * a02 + b01 * a12 + b02 * a22;
254 out[3] = b10 * a00 + b11 * a10 + b12 * a20;
255 out[4] = b10 * a01 + b11 * a11 + b12 * a21;
256 out[5] = b10 * a02 + b11 * a12 + b12 * a22;
258 out[6] = b20 * a00 + b21 * a10 + b22 * a20;
259 out[7] = b20 * a01 + b21 * a11 + b22 * a21;
260 out[8] = b20 * a02 + b21 * a12 + b22 * a22;
265 * Alias for {@link mat3.multiply}
268 mat3.mul = mat3.multiply;
271 * Translate a mat3 by the given vector
273 * @param {mat3} out the receiving matrix
274 * @param {mat3} a the matrix to translate
275 * @param {vec2} v vector to translate by
276 * @returns {mat3} out
278 mat3.translate = function(out, a, v) {
279 var a00 = a[0], a01 = a[1], a02 = a[2],
280 a10 = a[3], a11 = a[4], a12 = a[5],
281 a20 = a[6], a21 = a[7], a22 = a[8],
292 out[6] = x * a00 + y * a10 + a20;
293 out[7] = x * a01 + y * a11 + a21;
294 out[8] = x * a02 + y * a12 + a22;
299 * Rotates a mat3 by the given angle
301 * @param {mat3} out the receiving matrix
302 * @param {mat3} a the matrix to rotate
303 * @param {Number} rad the angle to rotate the matrix by
304 * @returns {mat3} out
306 mat3.rotate = function (out, a, rad) {
307 var a00 = a[0], a01 = a[1], a02 = a[2],
308 a10 = a[3], a11 = a[4], a12 = a[5],
309 a20 = a[6], a21 = a[7], a22 = a[8],
314 out[0] = c * a00 + s * a10;
315 out[1] = c * a01 + s * a11;
316 out[2] = c * a02 + s * a12;
318 out[3] = c * a10 - s * a00;
319 out[4] = c * a11 - s * a01;
320 out[5] = c * a12 - s * a02;
329 * Scales the mat3 by the dimensions in the given vec2
331 * @param {mat3} out the receiving matrix
332 * @param {mat3} a the matrix to rotate
333 * @param {vec2} v the vec2 to scale the matrix by
334 * @returns {mat3} out
336 mat3.scale = function(out, a, v) {
337 var x = v[0], y = v[2];
354 * Copies the values from a mat2d into a mat3
356 * @param {mat3} out the receiving matrix
357 * @param {mat3} a the matrix to rotate
358 * @param {vec2} v the vec2 to scale the matrix by
359 * @returns {mat3} out
361 mat3.fromMat2d = function(out, a) {
377 * Calculates a 3x3 matrix from the given quaternion
379 * @param {mat3} out mat3 receiving operation result
380 * @param {quat} q Quaternion to create matrix from
382 * @returns {mat3} out
384 mat3.fromQuat = function (out, q) {
385 var x = q[0], y = q[1], z = q[2], w = q[3],
400 out[0] = 1 - (yy + zz);
405 out[4] = 1 - (xx + zz);
410 out[8] = 1 - (xx + yy);
416 * Returns a string representation of a mat3
418 * @param {mat3} mat matrix to represent as a string
419 * @returns {String} string representation of the matrix
421 mat3.str = function (a) {
422 return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
423 a[3] + ', ' + a[4] + ', ' + a[5] + ', ' +
424 a[6] + ', ' + a[7] + ', ' + a[8] + ')';
427 if(typeof(exports) !== 'undefined') {