Debian Codegraph @

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

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