Debian Codegraph @

closure-library/closure/goog/vec/mat3.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Implements 3x3 matrices and their related functions which are
17 * compatible with WebGL. The API is structured to avoid unnecessary memory
18 * allocations. The last parameter will typically be the output vector and
19 * an object can be both an input and output parameter to all methods except
20 * where noted. Matrix operations follow the mathematical form when multiplying
21 * vectors as follows: resultVec = matrix * vec.
22 *
23 * The matrices are stored in column-major order.
24 *
25 */
26 goog.provide('goog.vec.Mat3');
27
28 goog.require('goog.vec');
29
30
31 /** @typedef {goog.vec.Float32} */ goog.vec.Mat3.Float32;
32 /** @typedef {goog.vec.Float64} */ goog.vec.Mat3.Float64;
33 /** @typedef {goog.vec.Number} */ goog.vec.Mat3.Number;
34 /** @typedef {goog.vec.AnyType} */ goog.vec.Mat3.AnyType;
35
36 // The following two types are deprecated - use the above types instead.
37 /** @typedef {Float32Array} */ goog.vec.Mat3.Type;
38 /** @typedef {goog.vec.ArrayType} */ goog.vec.Mat3.Mat3Like;
39
40
41 /**
42 * Creates the array representation of a 3x3 matrix of Float32.
43 * The use of the array directly instead of a class reduces overhead.
44 * The returned matrix is cleared to all zeros.
45 *
46 * @return {!goog.vec.Mat3.Float32} The new matrix.
47 */
48 goog.vec.Mat3.createFloat32 = function() {
49 return new Float32Array(9);
50 };
51
52
53 /**
54 * Creates the array representation of a 3x3 matrix of Float64.
55 * The returned matrix is cleared to all zeros.
56 *
57 * @return {!goog.vec.Mat3.Float64} The new matrix.
58 */
59 goog.vec.Mat3.createFloat64 = function() {
60 return new Float64Array(9);
61 };
62
63
64 /**
65 * Creates the array representation of a 3x3 matrix of Number.
66 * The returned matrix is cleared to all zeros.
67 *
68 * @return {!goog.vec.Mat3.Number} The new matrix.
69 */
70 goog.vec.Mat3.createNumber = function() {
71 var a = new Array(9);
72 goog.vec.Mat3.setFromValues(a, 0, 0, 0, 0, 0, 0, 0, 0, 0);
73 return a;
74 };
75
76
77 /**
78 * Creates the array representation of a 3x3 matrix of Float32.
79 * The returned matrix is cleared to all zeros.
80 *
81 * @deprecated Use createFloat32.
82 * @return {!goog.vec.Mat3.Type} The new matrix.
83 */
84 goog.vec.Mat3.create = function() {
85 return goog.vec.Mat3.createFloat32();
86 };
87
88
89 /**
90 * Creates a 3x3 identity matrix of Float32.
91 *
92 * @return {!goog.vec.Mat3.Float32} The new 9 element array.
93 */
94 goog.vec.Mat3.createFloat32Identity = function() {
95 var mat = goog.vec.Mat3.createFloat32();
96 mat[0] = mat[4] = mat[8] = 1;
97 return mat;
98 };
99
100
101 /**
102 * Creates a 3x3 identity matrix of Float64.
103 *
104 * @return {!goog.vec.Mat3.Float64} The new 9 element array.
105 */
106 goog.vec.Mat3.createFloat64Identity = function() {
107 var mat = goog.vec.Mat3.createFloat64();
108 mat[0] = mat[4] = mat[8] = 1;
109 return mat;
110 };
111
112
113 /**
114 * Creates a 3x3 identity matrix of Number.
115 * The returned matrix is cleared to all zeros.
116 *
117 * @return {!goog.vec.Mat3.Number} The new 9 element array.
118 */
119 goog.vec.Mat3.createNumberIdentity = function() {
120 var a = new Array(9);
121 goog.vec.Mat3.setFromValues(a, 1, 0, 0, 0, 1, 0, 0, 0, 1);
122 return a;
123 };
124
125
126 /**
127 * Creates the array representation of a 3x3 matrix of Float32.
128 * The returned matrix is cleared to all zeros.
129 *
130 * @deprecated Use createFloat32Identity.
131 * @return {!goog.vec.Mat3.Type} The new 9 element array.
132 */
133 goog.vec.Mat3.createIdentity = function() {
134 return goog.vec.Mat3.createFloat32Identity();
135 };
136
137
138 /**
139 * Creates a 3x3 matrix of Float32 initialized from the given array.
140 *
141 * @param {goog.vec.Mat3.AnyType} matrix The array containing the
142 * matrix values in column major order.
143 * @return {!goog.vec.Mat3.Float32} The new, nine element array.
144 */
145 goog.vec.Mat3.createFloat32FromArray = function(matrix) {
146 var newMatrix = goog.vec.Mat3.createFloat32();
147 goog.vec.Mat3.setFromArray(newMatrix, matrix);
148 return newMatrix;
149 };
150
151
152 /**
153 * Creates a 3x3 matrix of Float32 initialized from the given values.
154 *
155 * @param {number} v00 The values at (0, 0).
156 * @param {number} v10 The values at (1, 0).
157 * @param {number} v20 The values at (2, 0).
158 * @param {number} v01 The values at (0, 1).
159 * @param {number} v11 The values at (1, 1).
160 * @param {number} v21 The values at (2, 1).
161 * @param {number} v02 The values at (0, 2).
162 * @param {number} v12 The values at (1, 2).
163 * @param {number} v22 The values at (2, 2).
164 * @return {!goog.vec.Mat3.Float32} The new, nine element array.
165 */
166 goog.vec.Mat3.createFloat32FromValues = function(
167 v00, v10, v20, v01, v11, v21, v02, v12, v22) {
168 var newMatrix = goog.vec.Mat3.createFloat32();
169 goog.vec.Mat3.setFromValues(
170 newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
171 return newMatrix;
172 };
173
174
175 /**
176 * Creates a clone of a 3x3 matrix of Float32.
177 *
178 * @param {goog.vec.Mat3.Float32} matrix The source 3x3 matrix.
179 * @return {!goog.vec.Mat3.Float32} The new 3x3 element matrix.
180 */
181 goog.vec.Mat3.cloneFloat32 = goog.vec.Mat3.createFloat32FromArray;
182
183
184 /**
185 * Creates a 3x3 matrix of Float64 initialized from the given array.
186 *
187 * @param {goog.vec.Mat3.AnyType} matrix The array containing the
188 * matrix values in column major order.
189 * @return {!goog.vec.Mat3.Float64} The new, nine element array.
190 */
191 goog.vec.Mat3.createFloat64FromArray = function(matrix) {
192 var newMatrix = goog.vec.Mat3.createFloat64();
193 goog.vec.Mat3.setFromArray(newMatrix, matrix);
194 return newMatrix;
195 };
196
197
198 /**
199 * Creates a 3x3 matrix of Float64 initialized from the given values.
200 *
201 * @param {number} v00 The values at (0, 0).
202 * @param {number} v10 The values at (1, 0).
203 * @param {number} v20 The values at (2, 0).
204 * @param {number} v01 The values at (0, 1).
205 * @param {number} v11 The values at (1, 1).
206 * @param {number} v21 The values at (2, 1).
207 * @param {number} v02 The values at (0, 2).
208 * @param {number} v12 The values at (1, 2).
209 * @param {number} v22 The values at (2, 2).
210 * @return {!goog.vec.Mat3.Float64} The new, nine element array.
211 */
212 goog.vec.Mat3.createFloat64FromValues = function(
213 v00, v10, v20, v01, v11, v21, v02, v12, v22) {
214 var newMatrix = goog.vec.Mat3.createFloat64();
215 goog.vec.Mat3.setFromValues(
216 newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
217 return newMatrix;
218 };
219
220
221 /**
222 * Creates a clone of a 3x3 matrix of Float64.
223 *
224 * @param {goog.vec.Mat3.Float64} matrix The source 3x3 matrix.
225 * @return {!goog.vec.Mat3.Float64} The new 3x3 element matrix.
226 */
227 goog.vec.Mat3.cloneFloat64 = goog.vec.Mat3.createFloat64FromArray;
228
229
230 /**
231 * Creates a 3x3 matrix of Float32 initialized from the given array.
232 *
233 * @deprecated Use createFloat32FromArray.
234 * @param {goog.vec.Mat3.Mat3Like} matrix The array containing the
235 * matrix values in column major order.
236 * @return {!goog.vec.Mat3.Type} The new, nine element array.
237 */
238 goog.vec.Mat3.createFromArray = function(matrix) {
239 var newMatrix = goog.vec.Mat3.createFloat32();
240 goog.vec.Mat3.setFromArray(newMatrix, matrix);
241 return newMatrix;
242 };
243
244
245 /**
246 * Creates a 3x3 matrix of Float32 initialized from the given values.
247 *
248 * @deprecated Use createFloat32FromValues.
249 * @param {number} v00 The values at (0, 0).
250 * @param {number} v10 The values at (1, 0).
251 * @param {number} v20 The values at (2, 0).
252 * @param {number} v01 The values at (0, 1).
253 * @param {number} v11 The values at (1, 1).
254 * @param {number} v21 The values at (2, 1).
255 * @param {number} v02 The values at (0, 2).
256 * @param {number} v12 The values at (1, 2).
257 * @param {number} v22 The values at (2, 2).
258 * @return {!goog.vec.Mat3.Type} The new, nine element array.
259 */
260 goog.vec.Mat3.createFromValues = function(
261 v00, v10, v20, v01, v11, v21, v02, v12, v22) {
262 var newMatrix = goog.vec.Mat3.create();
263 goog.vec.Mat3.setFromValues(
264 newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
265 return newMatrix;
266 };
267
268
269 /**
270 * Creates a clone of a 3x3 matrix of Float32.
271 *
272 * @deprecated Use cloneFloat32.
273 * @param {goog.vec.Mat3.Mat3Like} matrix The source 3x3 matrix.
274 * @return {!goog.vec.Mat3.Type} The new 3x3 element matrix.
275 */
276 goog.vec.Mat3.clone = goog.vec.Mat3.createFromArray;
277
278
279 /**
280 * Retrieves the element at the requested row and column.
281 *
282 * @param {goog.vec.Mat3.AnyType} mat The matrix containing the value to
283 * retrieve.
284 * @param {number} row The row index.
285 * @param {number} column The column index.
286 * @return {number} The element value at the requested row, column indices.
287 */
288 goog.vec.Mat3.getElement = function(mat, row, column) {
289 return mat[row + column * 3];
290 };
291
292
293 /**
294 * Sets the element at the requested row and column.
295 *
296 * @param {goog.vec.Mat3.AnyType} mat The matrix containing the value to
297 * retrieve.
298 * @param {number} row The row index.
299 * @param {number} column The column index.
300 * @param {number} value The value to set at the requested row, column.
301 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
302 * chained together.
303 */
304 goog.vec.Mat3.setElement = function(mat, row, column, value) {
305 mat[row + column * 3] = value;
306 return mat;
307 };
308
309
310 /**
311 * Initializes the matrix from the set of values. Note the values supplied are
312 * in column major order.
313 *
314 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the
315 * values.
316 * @param {number} v00 The values at (0, 0).
317 * @param {number} v10 The values at (1, 0).
318 * @param {number} v20 The values at (2, 0).
319 * @param {number} v01 The values at (0, 1).
320 * @param {number} v11 The values at (1, 1).
321 * @param {number} v21 The values at (2, 1).
322 * @param {number} v02 The values at (0, 2).
323 * @param {number} v12 The values at (1, 2).
324 * @param {number} v22 The values at (2, 2).
325 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
326 * chained together.
327 */
328 goog.vec.Mat3.setFromValues = function(
329 mat, v00, v10, v20, v01, v11, v21, v02, v12, v22) {
330 mat[0] = v00;
331 mat[1] = v10;
332 mat[2] = v20;
333 mat[3] = v01;
334 mat[4] = v11;
335 mat[5] = v21;
336 mat[6] = v02;
337 mat[7] = v12;
338 mat[8] = v22;
339 return mat;
340 };
341
342
343 /**
344 * Sets the matrix from the array of values stored in column major order.
345 *
346 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
347 * @param {goog.vec.Mat3.AnyType} values The column major ordered
348 * array of values to store in the matrix.
349 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
350 * chained together.
351 */
352 goog.vec.Mat3.setFromArray = function(mat, values) {
353 mat[0] = values[0];
354 mat[1] = values[1];
355 mat[2] = values[2];
356 mat[3] = values[3];
357 mat[4] = values[4];
358 mat[5] = values[5];
359 mat[6] = values[6];
360 mat[7] = values[7];
361 mat[8] = values[8];
362 return mat;
363 };
364
365
366 /**
367 * Sets the matrix from the array of values stored in row major order.
368 *
369 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
370 * @param {goog.vec.Mat3.AnyType} values The row major ordered array
371 * of values to store in the matrix.
372 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
373 * chained together.
374 */
375 goog.vec.Mat3.setFromRowMajorArray = function(mat, values) {
376 mat[0] = values[0];
377 mat[1] = values[3];
378 mat[2] = values[6];
379 mat[3] = values[1];
380 mat[4] = values[4];
381 mat[5] = values[7];
382 mat[6] = values[2];
383 mat[7] = values[5];
384 mat[8] = values[8];
385 return mat;
386 };
387
388
389 /**
390 * Sets the diagonal values of the matrix from the given values.
391 *
392 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
393 * @param {number} v00 The values for (0, 0).
394 * @param {number} v11 The values for (1, 1).
395 * @param {number} v22 The values for (2, 2).
396 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
397 * chained together.
398 */
399 goog.vec.Mat3.setDiagonalValues = function(mat, v00, v11, v22) {
400 mat[0] = v00;
401 mat[4] = v11;
402 mat[8] = v22;
403 return mat;
404 };
405
406
407 /**
408 * Sets the diagonal values of the matrix from the given vector.
409 *
410 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
411 * @param {goog.vec.Vec3.AnyType} vec The vector containing the values.
412 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
413 * chained together.
414 */
415 goog.vec.Mat3.setDiagonal = function(mat, vec) {
416 mat[0] = vec[0];
417 mat[4] = vec[1];
418 mat[8] = vec[2];
419 return mat;
420 };
421
422
423 /**
424 * Sets the specified column with the supplied values.
425 *
426 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
427 * @param {number} column The column index to set the values on.
428 * @param {number} v0 The value for row 0.
429 * @param {number} v1 The value for row 1.
430 * @param {number} v2 The value for row 2.
431 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
432 * chained together.
433 */
434 goog.vec.Mat3.setColumnValues = function(mat, column, v0, v1, v2) {
435 var i = column * 3;
436 mat[i] = v0;
437 mat[i + 1] = v1;
438 mat[i + 2] = v2;
439 return mat;
440 };
441
442
443 /**
444 * Sets the specified column with the value from the supplied array.
445 *
446 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
447 * @param {number} column The column index to set the values on.
448 * @param {goog.vec.Vec3.AnyType} vec The vector elements for the column.
449 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
450 * chained together.
451 */
452 goog.vec.Mat3.setColumn = function(mat, column, vec) {
453 var i = column * 3;
454 mat[i] = vec[0];
455 mat[i + 1] = vec[1];
456 mat[i + 2] = vec[2];
457 return mat;
458 };
459
460
461 /**
462 * Retrieves the specified column from the matrix into the given vector
463 * array.
464 *
465 * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the values.
466 * @param {number} column The column to get the values from.
467 * @param {goog.vec.Vec3.AnyType} vec The vector elements to receive the
468 * column.
469 * @return {goog.vec.Vec3.AnyType} return vec so that operations can be
470 * chained together.
471 */
472 goog.vec.Mat3.getColumn = function(mat, column, vec) {
473 var i = column * 3;
474 vec[0] = mat[i];
475 vec[1] = mat[i + 1];
476 vec[2] = mat[i + 2];
477 return vec;
478 };
479
480
481 /**
482 * Sets the columns of the matrix from the set of vector elements.
483 *
484 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
485 * @param {goog.vec.Vec3.AnyType} vec0 The values for column 0.
486 * @param {goog.vec.Vec3.AnyType} vec1 The values for column 1.
487 * @param {goog.vec.Vec3.AnyType} vec2 The values for column 2.
488 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
489 * chained together.
490 */
491 goog.vec.Mat3.setColumns = function(mat, vec0, vec1, vec2) {
492 goog.vec.Mat3.setColumn(mat, 0, vec0);
493 goog.vec.Mat3.setColumn(mat, 1, vec1);
494 goog.vec.Mat3.setColumn(mat, 2, vec2);
495 return mat;
496 };
497
498
499 /**
500 * Retrieves the column values from the given matrix into the given vector
501 * elements.
502 *
503 * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the columns.
504 * @param {goog.vec.Vec3.AnyType} vec0 The vector to receive column 0.
505 * @param {goog.vec.Vec3.AnyType} vec1 The vector to receive column 1.
506 * @param {goog.vec.Vec3.AnyType} vec2 The vector to receive column 2.
507 */
508 goog.vec.Mat3.getColumns = function(mat, vec0, vec1, vec2) {
509 goog.vec.Mat3.getColumn(mat, 0, vec0);
510 goog.vec.Mat3.getColumn(mat, 1, vec1);
511 goog.vec.Mat3.getColumn(mat, 2, vec2);
512 };
513
514
515 /**
516 * Sets the row values from the supplied values.
517 *
518 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
519 * @param {number} row The index of the row to receive the values.
520 * @param {number} v0 The value for column 0.
521 * @param {number} v1 The value for column 1.
522 * @param {number} v2 The value for column 2.
523 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
524 * chained together.
525 */
526 goog.vec.Mat3.setRowValues = function(mat, row, v0, v1, v2) {
527 mat[row] = v0;
528 mat[row + 3] = v1;
529 mat[row + 6] = v2;
530 return mat;
531 };
532
533
534 /**
535 * Sets the row values from the supplied vector.
536 *
537 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the row values.
538 * @param {number} row The index of the row.
539 * @param {goog.vec.Vec3.AnyType} vec The vector containing the values.
540 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
541 * chained together.
542 */
543 goog.vec.Mat3.setRow = function(mat, row, vec) {
544 mat[row] = vec[0];
545 mat[row + 3] = vec[1];
546 mat[row + 6] = vec[2];
547 return mat;
548 };
549
550
551 /**
552 * Retrieves the row values into the given vector.
553 *
554 * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the values.
555 * @param {number} row The index of the row supplying the values.
556 * @param {goog.vec.Vec3.AnyType} vec The vector to receive the row.
557 * @return {goog.vec.Vec3.AnyType} return vec so that operations can be
558 * chained together.
559 */
560 goog.vec.Mat3.getRow = function(mat, row, vec) {
561 vec[0] = mat[row];
562 vec[1] = mat[row + 3];
563 vec[2] = mat[row + 6];
564 return vec;
565 };
566
567
568 /**
569 * Sets the rows of the matrix from the supplied vectors.
570 *
571 * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
572 * @param {goog.vec.Vec3.AnyType} vec0 The values for row 0.
573 * @param {goog.vec.Vec3.AnyType} vec1 The values for row 1.
574 * @param {goog.vec.Vec3.AnyType} vec2 The values for row 2.
575 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
576 * chained together.
577 */
578 goog.vec.Mat3.setRows = function(mat, vec0, vec1, vec2) {
579 goog.vec.Mat3.setRow(mat, 0, vec0);
580 goog.vec.Mat3.setRow(mat, 1, vec1);
581 goog.vec.Mat3.setRow(mat, 2, vec2);
582 return mat;
583 };
584
585
586 /**
587 * Retrieves the rows of the matrix into the supplied vectors.
588 *
589 * @param {goog.vec.Mat3.AnyType} mat The matrix to supplying the values.
590 * @param {goog.vec.Vec3.AnyType} vec0 The vector to receive row 0.
591 * @param {goog.vec.Vec3.AnyType} vec1 The vector to receive row 1.
592 * @param {goog.vec.Vec3.AnyType} vec2 The vector to receive row 2.
593 */
594 goog.vec.Mat3.getRows = function(mat, vec0, vec1, vec2) {
595 goog.vec.Mat3.getRow(mat, 0, vec0);
596 goog.vec.Mat3.getRow(mat, 1, vec1);
597 goog.vec.Mat3.getRow(mat, 2, vec2);
598 };
599
600
601 /**
602 * Makes the given 3x3 matrix the zero matrix.
603 *
604 * @param {goog.vec.Mat3.AnyType} mat The matrix.
605 * @return {goog.vec.Mat3.AnyType} return mat so operations can be chained.
606 */
607 goog.vec.Mat3.makeZero = function(mat) {
608 mat[0] = 0;
609 mat[1] = 0;
610 mat[2] = 0;
611 mat[3] = 0;
612 mat[4] = 0;
613 mat[5] = 0;
614 mat[6] = 0;
615 mat[7] = 0;
616 mat[8] = 0;
617 return mat;
618 };
619
620
621 /**
622 * Makes the given 3x3 matrix the identity matrix.
623 *
624 * @param {goog.vec.Mat3.AnyType} mat The matrix.
625 * @return {goog.vec.Mat3.AnyType} return mat so operations can be chained.
626 */
627 goog.vec.Mat3.makeIdentity = function(mat) {
628 mat[0] = 1;
629 mat[1] = 0;
630 mat[2] = 0;
631 mat[3] = 0;
632 mat[4] = 1;
633 mat[5] = 0;
634 mat[6] = 0;
635 mat[7] = 0;
636 mat[8] = 1;
637 return mat;
638 };
639
640
641 /**
642 * Performs a per-component addition of the matrices mat0 and mat1, storing
643 * the result into resultMat.
644 *
645 * @param {goog.vec.Mat3.AnyType} mat0 The first addend.
646 * @param {goog.vec.Mat3.AnyType} mat1 The second addend.
647 * @param {goog.vec.Mat3.AnyType} resultMat The matrix to
648 * receive the results (may be either mat0 or mat1).
649 * @return {goog.vec.Mat3.AnyType} return resultMat so that operations can be
650 * chained together.
651 */
652 goog.vec.Mat3.addMat = function(mat0, mat1, resultMat) {
653 resultMat[0] = mat0[0] + mat1[0];
654 resultMat[1] = mat0[1] + mat1[1];
655 resultMat[2] = mat0[2] + mat1[2];
656 resultMat[3] = mat0[3] + mat1[3];
657 resultMat[4] = mat0[4] + mat1[4];
658 resultMat[5] = mat0[5] + mat1[5];
659 resultMat[6] = mat0[6] + mat1[6];
660 resultMat[7] = mat0[7] + mat1[7];
661 resultMat[8] = mat0[8] + mat1[8];
662 return resultMat;
663 };
664
665
666 /**
667 * Performs a per-component subtraction of the matrices mat0 and mat1,
668 * storing the result into resultMat.
669 *
670 * @param {goog.vec.Mat3.AnyType} mat0 The minuend.
671 * @param {goog.vec.Mat3.AnyType} mat1 The subtrahend.
672 * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
673 * the results (may be either mat0 or mat1).
674 * @return {goog.vec.Mat3.AnyType} return resultMat so that operations can be
675 * chained together.
676 */
677 goog.vec.Mat3.subMat = function(mat0, mat1, resultMat) {
678 resultMat[0] = mat0[0] - mat1[0];
679 resultMat[1] = mat0[1] - mat1[1];
680 resultMat[2] = mat0[2] - mat1[2];
681 resultMat[3] = mat0[3] - mat1[3];
682 resultMat[4] = mat0[4] - mat1[4];
683 resultMat[5] = mat0[5] - mat1[5];
684 resultMat[6] = mat0[6] - mat1[6];
685 resultMat[7] = mat0[7] - mat1[7];
686 resultMat[8] = mat0[8] - mat1[8];
687 return resultMat;
688 };
689
690
691 /**
692 * Multiplies matrix mat0 with the given scalar, storing the result
693 * into resultMat.
694 *
695 * @param {goog.vec.Mat3.AnyType} mat The matrix.
696 * @param {number} scalar The scalar value to multiple to each element of mat.
697 * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
698 * the results (may be mat).
699 * @return {goog.vec.Mat3.AnyType} return resultMat so that operations can be
700 * chained together.
701 */
702 goog.vec.Mat3.multScalar = function(mat, scalar, resultMat) {
703 resultMat[0] = mat[0] * scalar;
704 resultMat[1] = mat[1] * scalar;
705 resultMat[2] = mat[2] * scalar;
706 resultMat[3] = mat[3] * scalar;
707 resultMat[4] = mat[4] * scalar;
708 resultMat[5] = mat[5] * scalar;
709 resultMat[6] = mat[6] * scalar;
710 resultMat[7] = mat[7] * scalar;
711 resultMat[8] = mat[8] * scalar;
712 return resultMat;
713 };
714
715
716 /**
717 * Multiplies the two matrices mat0 and mat1 using matrix multiplication,
718 * storing the result into resultMat.
719 *
720 * @param {goog.vec.Mat3.AnyType} mat0 The first (left hand) matrix.
721 * @param {goog.vec.Mat3.AnyType} mat1 The second (right hand) matrix.
722 * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
723 * the results (may be either mat0 or mat1).
724 * @return {goog.vec.Mat3.AnyType} return resultMat so that operations can be
725 * chained together.
726 */
727 goog.vec.Mat3.multMat = function(mat0, mat1, resultMat) {
728 var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
729 var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
730 var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
731
732 var b00 = mat1[0], b10 = mat1[1], b20 = mat1[2];
733 var b01 = mat1[3], b11 = mat1[4], b21 = mat1[5];
734 var b02 = mat1[6], b12 = mat1[7], b22 = mat1[8];
735
736 resultMat[0] = a00 * b00 + a01 * b10 + a02 * b20;
737 resultMat[1] = a10 * b00 + a11 * b10 + a12 * b20;
738 resultMat[2] = a20 * b00 + a21 * b10 + a22 * b20;
739 resultMat[3] = a00 * b01 + a01 * b11 + a02 * b21;
740 resultMat[4] = a10 * b01 + a11 * b11 + a12 * b21;
741 resultMat[5] = a20 * b01 + a21 * b11 + a22 * b21;
742 resultMat[6] = a00 * b02 + a01 * b12 + a02 * b22;
743 resultMat[7] = a10 * b02 + a11 * b12 + a12 * b22;
744 resultMat[8] = a20 * b02 + a21 * b12 + a22 * b22;
745 return resultMat;
746 };
747
748
749 /**
750 * Transposes the given matrix mat storing the result into resultMat.
751 *
752 * @param {goog.vec.Mat3.AnyType} mat The matrix to transpose.
753 * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
754 * the results (may be mat).
755 * @return {goog.vec.Mat3.AnyType} return resultMat so that operations can be
756 * chained together.
757 */
758 goog.vec.Mat3.transpose = function(mat, resultMat) {
759 if (resultMat == mat) {
760 var a10 = mat[1], a20 = mat[2], a21 = mat[5];
761 resultMat[1] = mat[3];
762 resultMat[2] = mat[6];
763 resultMat[3] = a10;
764 resultMat[5] = mat[7];
765 resultMat[6] = a20;
766 resultMat[7] = a21;
767 } else {
768 resultMat[0] = mat[0];
769 resultMat[1] = mat[3];
770 resultMat[2] = mat[6];
771 resultMat[3] = mat[1];
772 resultMat[4] = mat[4];
773 resultMat[5] = mat[7];
774 resultMat[6] = mat[2];
775 resultMat[7] = mat[5];
776 resultMat[8] = mat[8];
777 }
778 return resultMat;
779 };
780
781
782 /**
783 * Computes the inverse of mat0 storing the result into resultMat. If the
784 * inverse is defined, this function returns true, false otherwise.
785 *
786 * @param {goog.vec.Mat3.AnyType} mat0 The matrix to invert.
787 * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
788 * the result (may be mat0).
789 * @return {boolean} True if the inverse is defined. If false is returned,
790 * resultMat is not modified.
791 */
792 goog.vec.Mat3.invert = function(mat0, resultMat) {
793 var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
794 var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
795 var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
796
797 var t00 = a11 * a22 - a12 * a21;
798 var t10 = a12 * a20 - a10 * a22;
799 var t20 = a10 * a21 - a11 * a20;
800 var det = a00 * t00 + a01 * t10 + a02 * t20;
801 if (det == 0) {
802 return false;
803 }
804
805 var idet = 1 / det;
806 resultMat[0] = t00 * idet;
807 resultMat[3] = (a02 * a21 - a01 * a22) * idet;
808 resultMat[6] = (a01 * a12 - a02 * a11) * idet;
809
810 resultMat[1] = t10 * idet;
811 resultMat[4] = (a00 * a22 - a02 * a20) * idet;
812 resultMat[7] = (a02 * a10 - a00 * a12) * idet;
813
814 resultMat[2] = t20 * idet;
815 resultMat[5] = (a01 * a20 - a00 * a21) * idet;
816 resultMat[8] = (a00 * a11 - a01 * a10) * idet;
817 return true;
818 };
819
820
821 /**
822 * Returns true if the components of mat0 are equal to the components of mat1.
823 *
824 * @param {goog.vec.Mat3.AnyType} mat0 The first matrix.
825 * @param {goog.vec.Mat3.AnyType} mat1 The second matrix.
826 * @return {boolean} True if the the two matrices are equivalent.
827 */
828 goog.vec.Mat3.equals = function(mat0, mat1) {
829 return mat0.length == mat1.length && mat0[0] == mat1[0] &&
830 mat0[1] == mat1[1] && mat0[2] == mat1[2] && mat0[3] == mat1[3] &&
831 mat0[4] == mat1[4] && mat0[5] == mat1[5] && mat0[6] == mat1[6] &&
832 mat0[7] == mat1[7] && mat0[8] == mat1[8];
833 };
834
835
836 /**
837 * Transforms the given vector with the given matrix storing the resulting,
838 * transformed matrix into resultVec.
839 *
840 * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the transformation.
841 * @param {goog.vec.Vec3.AnyType} vec The vector to transform.
842 * @param {goog.vec.Vec3.AnyType} resultVec The vector to
843 * receive the results (may be vec).
844 * @return {goog.vec.Vec3.AnyType} return resultVec so that operations can be
845 * chained together.
846 */
847 goog.vec.Mat3.multVec3 = function(mat, vec, resultVec) {
848 var x = vec[0], y = vec[1], z = vec[2];
849 resultVec[0] = x * mat[0] + y * mat[3] + z * mat[6];
850 resultVec[1] = x * mat[1] + y * mat[4] + z * mat[7];
851 resultVec[2] = x * mat[2] + y * mat[5] + z * mat[8];
852 return resultVec;
853 };
854
855
856 /**
857 * Makes the given 3x3 matrix a translation matrix with x and y
858 * translation values.
859 *
860 * @param {goog.vec.Mat3.AnyType} mat The matrix.
861 * @param {number} x The translation along the x axis.
862 * @param {number} y The translation along the y axis.
863 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
864 * chained.
865 */
866 goog.vec.Mat3.makeTranslate = function(mat, x, y) {
867 goog.vec.Mat3.makeIdentity(mat);
868 return goog.vec.Mat3.setColumnValues(mat, 2, x, y, 1);
869 };
870
871
872 /**
873 * Makes the given 3x3 matrix a scale matrix with x, y, and z scale factors.
874 *
875 * @param {goog.vec.Mat3.AnyType} mat The 3x3 (9-element) matrix
876 * array to receive the new scale matrix.
877 * @param {number} x The scale along the x axis.
878 * @param {number} y The scale along the y axis.
879 * @param {number} z The scale along the z axis.
880 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
881 * chained.
882 */
883 goog.vec.Mat3.makeScale = function(mat, x, y, z) {
884 goog.vec.Mat3.makeIdentity(mat);
885 return goog.vec.Mat3.setDiagonalValues(mat, x, y, z);
886 };
887
888
889 /**
890 * Makes the given 3x3 matrix a rotation matrix with the given rotation
891 * angle about the axis defined by the vector (ax, ay, az).
892 *
893 * @param {goog.vec.Mat3.AnyType} mat The matrix.
894 * @param {number} angle The rotation angle in radians.
895 * @param {number} ax The x component of the rotation axis.
896 * @param {number} ay The y component of the rotation axis.
897 * @param {number} az The z component of the rotation axis.
898 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
899 * chained.
900 */
901 goog.vec.Mat3.makeRotate = function(mat, angle, ax, ay, az) {
902 var c = Math.cos(angle);
903 var d = 1 - c;
904 var s = Math.sin(angle);
905
906 return goog.vec.Mat3.setFromValues(
907 mat, ax * ax * d + c, ax * ay * d + az * s, ax * az * d - ay * s,
908
909 ax * ay * d - az * s, ay * ay * d + c, ay * az * d + ax * s,
910
911 ax * az * d + ay * s, ay * az * d - ax * s, az * az * d + c);
912 };
913
914
915 /**
916 * Makes the given 3x3 matrix a rotation matrix with the given rotation
917 * angle about the X axis.
918 *
919 * @param {goog.vec.Mat3.AnyType} mat The matrix.
920 * @param {number} angle The rotation angle in radians.
921 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
922 * chained.
923 */
924 goog.vec.Mat3.makeRotateX = function(mat, angle) {
925 var c = Math.cos(angle);
926 var s = Math.sin(angle);
927 return goog.vec.Mat3.setFromValues(mat, 1, 0, 0, 0, c, s, 0, -s, c);
928 };
929
930
931 /**
932 * Makes the given 3x3 matrix a rotation matrix with the given rotation
933 * angle about the Y axis.
934 *
935 * @param {goog.vec.Mat3.AnyType} mat The matrix.
936 * @param {number} angle The rotation angle in radians.
937 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
938 * chained.
939 */
940 goog.vec.Mat3.makeRotateY = function(mat, angle) {
941 var c = Math.cos(angle);
942 var s = Math.sin(angle);
943 return goog.vec.Mat3.setFromValues(mat, c, 0, -s, 0, 1, 0, s, 0, c);
944 };
945
946
947 /**
948 * Makes the given 3x3 matrix a rotation matrix with the given rotation
949 * angle about the Z axis.
950 *
951 * @param {goog.vec.Mat3.AnyType} mat The matrix.
952 * @param {number} angle The rotation angle in radians.
953 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
954 * chained.
955 */
956 goog.vec.Mat3.makeRotateZ = function(mat, angle) {
957 var c = Math.cos(angle);
958 var s = Math.sin(angle);
959 return goog.vec.Mat3.setFromValues(mat, c, s, 0, -s, c, 0, 0, 0, 1);
960 };
961
962
963 /**
964 * Rotate the given matrix by angle about the x,y,z axis. Equivalent to:
965 * goog.vec.Mat3.multMat(
966 * mat,
967 * goog.vec.Mat3.makeRotate(goog.vec.Mat3.create(), angle, x, y, z),
968 * mat);
969 *
970 * @param {goog.vec.Mat3.AnyType} mat The matrix.
971 * @param {number} angle The angle in radians.
972 * @param {number} x The x component of the rotation axis.
973 * @param {number} y The y component of the rotation axis.
974 * @param {number} z The z component of the rotation axis.
975 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
976 * chained.
977 */
978 goog.vec.Mat3.rotate = function(mat, angle, x, y, z) {
979 var m00 = mat[0], m10 = mat[1], m20 = mat[2];
980 var m01 = mat[3], m11 = mat[4], m21 = mat[5];
981 var m02 = mat[6], m12 = mat[7], m22 = mat[8];
982
983 var cosAngle = Math.cos(angle);
984 var sinAngle = Math.sin(angle);
985 var diffCosAngle = 1 - cosAngle;
986 var r00 = x * x * diffCosAngle + cosAngle;
987 var r10 = x * y * diffCosAngle + z * sinAngle;
988 var r20 = x * z * diffCosAngle - y * sinAngle;
989
990 var r01 = x * y * diffCosAngle - z * sinAngle;
991 var r11 = y * y * diffCosAngle + cosAngle;
992 var r21 = y * z * diffCosAngle + x * sinAngle;
993
994 var r02 = x * z * diffCosAngle + y * sinAngle;
995 var r12 = y * z * diffCosAngle - x * sinAngle;
996 var r22 = z * z * diffCosAngle + cosAngle;
997
998 return goog.vec.Mat3.setFromValues(
999 mat, m00 * r00 + m01 * r10 + m02 * r20, m10 * r00 + m11 * r10 + m12 * r20,
1000 m20 * r00 + m21 * r10 + m22 * r20,
1001
1002 m00 * r01 + m01 * r11 + m02 * r21, m10 * r01 + m11 * r11 + m12 * r21,
1003 m20 * r01 + m21 * r11 + m22 * r21,
1004
1005 m00 * r02 + m01 * r12 + m02 * r22, m10 * r02 + m11 * r12 + m12 * r22,
1006 m20 * r02 + m21 * r12 + m22 * r22);
1007 };
1008
1009
1010 /**
1011 * Rotate the given matrix by angle about the x axis. Equivalent to:
1012 * goog.vec.Mat3.multMat(
1013 * mat,
1014 * goog.vec.Mat3.makeRotateX(goog.vec.Mat3.create(), angle),
1015 * mat);
1016 *
1017 * @param {goog.vec.Mat3.AnyType} mat The matrix.
1018 * @param {number} angle The angle in radians.
1019 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
1020 * chained.
1021 */
1022 goog.vec.Mat3.rotateX = function(mat, angle) {
1023 var m01 = mat[3], m11 = mat[4], m21 = mat[5];
1024 var m02 = mat[6], m12 = mat[7], m22 = mat[8];
1025
1026 var c = Math.cos(angle);
1027 var s = Math.sin(angle);
1028
1029 mat[3] = m01 * c + m02 * s;
1030 mat[4] = m11 * c + m12 * s;
1031 mat[5] = m21 * c + m22 * s;
1032 mat[6] = m01 * -s + m02 * c;
1033 mat[7] = m11 * -s + m12 * c;
1034 mat[8] = m21 * -s + m22 * c;
1035
1036 return mat;
1037 };
1038
1039
1040 /**
1041 * Rotate the given matrix by angle about the y axis. Equivalent to:
1042 * goog.vec.Mat3.multMat(
1043 * mat,
1044 * goog.vec.Mat3.makeRotateY(goog.vec.Mat3.create(), angle),
1045 * mat);
1046 *
1047 * @param {goog.vec.Mat3.AnyType} mat The matrix.
1048 * @param {number} angle The angle in radians.
1049 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
1050 * chained.
1051 */
1052 goog.vec.Mat3.rotateY = function(mat, angle) {
1053 var m00 = mat[0], m10 = mat[1], m20 = mat[2];
1054 var m02 = mat[6], m12 = mat[7], m22 = mat[8];
1055
1056 var c = Math.cos(angle);
1057 var s = Math.sin(angle);
1058
1059 mat[0] = m00 * c + m02 * -s;
1060 mat[1] = m10 * c + m12 * -s;
1061 mat[2] = m20 * c + m22 * -s;
1062 mat[6] = m00 * s + m02 * c;
1063 mat[7] = m10 * s + m12 * c;
1064 mat[8] = m20 * s + m22 * c;
1065
1066 return mat;
1067 };
1068
1069
1070 /**
1071 * Rotate the given matrix by angle about the z axis. Equivalent to:
1072 * goog.vec.Mat3.multMat(
1073 * mat,
1074 * goog.vec.Mat3.makeRotateZ(goog.vec.Mat3.create(), angle),
1075 * mat);
1076 *
1077 * @param {goog.vec.Mat3.AnyType} mat The matrix.
1078 * @param {number} angle The angle in radians.
1079 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
1080 * chained.
1081 */
1082 goog.vec.Mat3.rotateZ = function(mat, angle) {
1083 var m00 = mat[0], m10 = mat[1], m20 = mat[2];
1084 var m01 = mat[3], m11 = mat[4], m21 = mat[5];
1085
1086 var c = Math.cos(angle);
1087 var s = Math.sin(angle);
1088
1089 mat[0] = m00 * c + m01 * s;
1090 mat[1] = m10 * c + m11 * s;
1091 mat[2] = m20 * c + m21 * s;
1092 mat[3] = m00 * -s + m01 * c;
1093 mat[4] = m10 * -s + m11 * c;
1094 mat[5] = m20 * -s + m21 * c;
1095
1096 return mat;
1097 };
1098
1099
1100 /**
1101 * Makes the given 3x3 matrix a rotation matrix given Euler angles using
1102 * the ZXZ convention.
1103 * Given the euler angles [theta1, theta2, theta3], the rotation is defined as
1104 * rotation = rotation_z(theta1) * rotation_x(theta2) * rotation_z(theta3),
1105 * with theta1 in [0, 2 * pi], theta2 in [0, pi] and theta3 in [0, 2 * pi].
1106 * rotation_x(theta) means rotation around the X axis of theta radians.
1107 *
1108 * @param {goog.vec.Mat3.AnyType} mat The matrix.
1109 * @param {number} theta1 The angle of rotation around the Z axis in radians.
1110 * @param {number} theta2 The angle of rotation around the X axis in radians.
1111 * @param {number} theta3 The angle of rotation around the Z axis in radians.
1112 * @return {goog.vec.Mat3.AnyType} return mat so that operations can be
1113 * chained.
1114 */
1115 goog.vec.Mat3.makeEulerZXZ = function(mat, theta1, theta2, theta3) {
1116 var c1 = Math.cos(theta1);
1117 var s1 = Math.sin(theta1);
1118
1119 var c2 = Math.cos(theta2);
1120 var s2 = Math.sin(theta2);
1121
1122 var c3 = Math.cos(theta3);
1123 var s3 = Math.sin(theta3);
1124
1125 mat[0] = c1 * c3 - c2 * s1 * s3;
1126 mat[1] = c2 * c1 * s3 + c3 * s1;
1127 mat[2] = s3 * s2;
1128
1129 mat[3] = -c1 * s3 - c3 * c2 * s1;
1130 mat[4] = c1 * c2 * c3 - s1 * s3;
1131 mat[5] = c3 * s2;
1132
1133 mat[6] = s2 * s1;
1134 mat[7] = -c1 * s2;
1135 mat[8] = c2;
1136
1137 return mat;
1138 };
1139
1140
1141 /**
1142 * Decomposes a rotation matrix into Euler angles using the ZXZ convention so
1143 * that rotation = rotation_z(theta1) * rotation_x(theta2) * rotation_z(theta3),
1144 * with theta1 in [0, 2 * pi], theta2 in [0, pi] and theta3 in [0, 2 * pi].
1145 * rotation_x(theta) means rotation around the X axis of theta radians.
1146 *
1147 * @param {goog.vec.Mat3.AnyType} mat The matrix.
1148 * @param {goog.vec.Vec3.AnyType} euler The ZXZ Euler angles in
1149 * radians as [theta1, theta2, theta3].
1150 * @param {boolean=} opt_theta2IsNegative Whether theta2 is in [-pi, 0] instead
1151 * of the default [0, pi].
1152 * @return {goog.vec.Vec3.AnyType} return euler so that operations can be
1153 * chained together.
1154 */
1155 goog.vec.Mat3.toEulerZXZ = function(mat, euler, opt_theta2IsNegative) {
1156 // There is an ambiguity in the sign of sinTheta2 because of the sqrt.
1157 var sinTheta2 = Math.sqrt(mat[2] * mat[2] + mat[5] * mat[5]);
1158
1159 // By default we explicitely constrain theta2 to be in [0, pi],
1160 // so sinTheta2 is always positive. We can change the behavior and specify
1161 // theta2 to be negative in [-pi, 0] with opt_Theta2IsNegative.
1162 var signTheta2 = opt_theta2IsNegative ? -1 : 1;
1163
1164 if (sinTheta2 > goog.vec.EPSILON) {
1165 euler[2] = Math.atan2(mat[2] * signTheta2, mat[5] * signTheta2);
1166 euler[1] = Math.atan2(sinTheta2 * signTheta2, mat[8]);
1167 euler[0] = Math.atan2(mat[6] * signTheta2, -mat[7] * signTheta2);
1168 } else {
1169 // There is also an arbitrary choice for theta1 = 0 or theta2 = 0 here.
1170 // We assume theta1 = 0 as some applications do not allow the camera to roll
1171 // (i.e. have theta1 != 0).
1172 euler[0] = 0;
1173 euler[1] = Math.atan2(sinTheta2 * signTheta2, mat[8]);
1174 euler[2] = Math.atan2(mat[1], mat[0]);
1175 }
1176
1177 // Atan2 outputs angles in [-pi, pi] so we bring them back to [0, 2 * pi].
1178 euler[0] = (euler[0] + Math.PI * 2) % (Math.PI * 2);
1179 euler[2] = (euler[2] + Math.PI * 2) % (Math.PI * 2);
1180 // For theta2 we want the angle to be in [0, pi] or [-pi, 0] depending on
1181 // signTheta2.
1182 euler[1] =
1183 ((euler[1] * signTheta2 + Math.PI * 2) % (Math.PI * 2)) * signTheta2;
1184
1185 return euler;
1186 };