matrix3.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. * @fileoverview WARNING: DEPRECATED. Use Mat3 instead.
  16. * 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. */
  24. goog.provide('goog.vec.Matrix3');
  25. goog.require('goog.vec');
  26. /**
  27. * @typedef {goog.vec.ArrayType}
  28. */
  29. goog.vec.Matrix3.Type;
  30. /**
  31. * Creates the array representation of a 3x3 matrix. The use of the array
  32. * directly eliminates any overhead associated with the class representation
  33. * defined above. The returned matrix is cleared to all zeros.
  34. *
  35. * @return {goog.vec.Matrix3.Type} The new, nine element array.
  36. */
  37. goog.vec.Matrix3.create = function() {
  38. return new Float32Array(9);
  39. };
  40. /**
  41. * Creates the array representation of a 3x3 matrix. The use of the array
  42. * directly eliminates any overhead associated with the class representation
  43. * defined above. The returned matrix is initialized with the identity.
  44. *
  45. * @return {goog.vec.Matrix3.Type} The new, nine element array.
  46. */
  47. goog.vec.Matrix3.createIdentity = function() {
  48. var mat = goog.vec.Matrix3.create();
  49. mat[0] = mat[4] = mat[8] = 1;
  50. return mat;
  51. };
  52. /**
  53. * Creates a 3x3 matrix initialized from the given array.
  54. *
  55. * @param {goog.vec.ArrayType} matrix The array containing the
  56. * matrix values in column major order.
  57. * @return {goog.vec.Matrix3.Type} The new, nine element array.
  58. */
  59. goog.vec.Matrix3.createFromArray = function(matrix) {
  60. var newMatrix = goog.vec.Matrix3.create();
  61. goog.vec.Matrix3.setFromArray(newMatrix, matrix);
  62. return newMatrix;
  63. };
  64. /**
  65. * Creates a 3x3 matrix initialized from the given values.
  66. *
  67. * @param {number} v00 The values at (0, 0).
  68. * @param {number} v10 The values at (1, 0).
  69. * @param {number} v20 The values at (2, 0).
  70. * @param {number} v01 The values at (0, 1).
  71. * @param {number} v11 The values at (1, 1).
  72. * @param {number} v21 The values at (2, 1).
  73. * @param {number} v02 The values at (0, 2).
  74. * @param {number} v12 The values at (1, 2).
  75. * @param {number} v22 The values at (2, 2).
  76. * @return {goog.vec.Matrix3.Type} The new, nine element array.
  77. */
  78. goog.vec.Matrix3.createFromValues = function(
  79. v00, v10, v20, v01, v11, v21, v02, v12, v22) {
  80. var newMatrix = goog.vec.Matrix3.create();
  81. goog.vec.Matrix3.setFromValues(
  82. newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
  83. return newMatrix;
  84. };
  85. /**
  86. * Creates a clone of a 3x3 matrix.
  87. *
  88. * @param {goog.vec.Matrix3.Type} matrix The source 3x3 matrix.
  89. * @return {goog.vec.Matrix3.Type} The new 3x3 element matrix.
  90. */
  91. goog.vec.Matrix3.clone =
  92. goog.vec.Matrix3.createFromArray;
  93. /**
  94. * Retrieves the element at the requested row and column.
  95. *
  96. * @param {goog.vec.ArrayType} mat The matrix containing the
  97. * value to retrieve.
  98. * @param {number} row The row index.
  99. * @param {number} column The column index.
  100. * @return {number} The element value at the requested row, column indices.
  101. */
  102. goog.vec.Matrix3.getElement = function(mat, row, column) {
  103. return mat[row + column * 3];
  104. };
  105. /**
  106. * Sets the element at the requested row and column.
  107. *
  108. * @param {goog.vec.ArrayType} mat The matrix containing the
  109. * value to retrieve.
  110. * @param {number} row The row index.
  111. * @param {number} column The column index.
  112. * @param {number} value The value to set at the requested row, column.
  113. */
  114. goog.vec.Matrix3.setElement = function(mat, row, column, value) {
  115. mat[row + column * 3] = value;
  116. };
  117. /**
  118. * Initializes the matrix from the set of values. Note the values supplied are
  119. * in column major order.
  120. *
  121. * @param {goog.vec.ArrayType} mat The matrix to receive the
  122. * values.
  123. * @param {number} v00 The values at (0, 0).
  124. * @param {number} v10 The values at (1, 0).
  125. * @param {number} v20 The values at (2, 0).
  126. * @param {number} v01 The values at (0, 1).
  127. * @param {number} v11 The values at (1, 1).
  128. * @param {number} v21 The values at (2, 1).
  129. * @param {number} v02 The values at (0, 2).
  130. * @param {number} v12 The values at (1, 2).
  131. * @param {number} v22 The values at (2, 2).
  132. */
  133. goog.vec.Matrix3.setFromValues = function(
  134. mat, v00, v10, v20, v01, v11, v21, v02, v12, v22) {
  135. mat[0] = v00;
  136. mat[1] = v10;
  137. mat[2] = v20;
  138. mat[3] = v01;
  139. mat[4] = v11;
  140. mat[5] = v21;
  141. mat[6] = v02;
  142. mat[7] = v12;
  143. mat[8] = v22;
  144. };
  145. /**
  146. * Sets the matrix from the array of values stored in column major order.
  147. *
  148. * @param {goog.vec.ArrayType} mat The matrix to receive the
  149. * values.
  150. * @param {goog.vec.ArrayType} values The column major ordered
  151. * array of values to store in the matrix.
  152. */
  153. goog.vec.Matrix3.setFromArray = function(mat, values) {
  154. mat[0] = values[0];
  155. mat[1] = values[1];
  156. mat[2] = values[2];
  157. mat[3] = values[3];
  158. mat[4] = values[4];
  159. mat[5] = values[5];
  160. mat[6] = values[6];
  161. mat[7] = values[7];
  162. mat[8] = values[8];
  163. };
  164. /**
  165. * Sets the matrix from the array of values stored in row major order.
  166. *
  167. * @param {goog.vec.ArrayType} mat The matrix to receive the
  168. * values.
  169. * @param {goog.vec.ArrayType} values The row major ordered array
  170. * of values to store in the matrix.
  171. */
  172. goog.vec.Matrix3.setFromRowMajorArray = function(mat, values) {
  173. mat[0] = values[0];
  174. mat[1] = values[3];
  175. mat[2] = values[6];
  176. mat[3] = values[1];
  177. mat[4] = values[4];
  178. mat[5] = values[7];
  179. mat[6] = values[2];
  180. mat[7] = values[5];
  181. mat[8] = values[8];
  182. };
  183. /**
  184. * Sets the diagonal values of the matrix from the given values.
  185. *
  186. * @param {goog.vec.ArrayType} mat The matrix to receive the
  187. * values.
  188. * @param {number} v00 The values for (0, 0).
  189. * @param {number} v11 The values for (1, 1).
  190. * @param {number} v22 The values for (2, 2).
  191. */
  192. goog.vec.Matrix3.setDiagonalValues = function(mat, v00, v11, v22) {
  193. mat[0] = v00;
  194. mat[4] = v11;
  195. mat[8] = v22;
  196. };
  197. /**
  198. * Sets the diagonal values of the matrix from the given vector.
  199. *
  200. * @param {goog.vec.ArrayType} mat The matrix to receive the
  201. * values.
  202. * @param {goog.vec.ArrayType} vec The vector containing the
  203. * values.
  204. */
  205. goog.vec.Matrix3.setDiagonal = function(mat, vec) {
  206. mat[0] = vec[0];
  207. mat[4] = vec[1];
  208. mat[8] = vec[2];
  209. };
  210. /**
  211. * Sets the specified column with the supplied values.
  212. *
  213. * @param {goog.vec.ArrayType} mat The matrix to recieve the
  214. * values.
  215. * @param {number} column The column index to set the values on.
  216. * @param {number} v0 The value for row 0.
  217. * @param {number} v1 The value for row 1.
  218. * @param {number} v2 The value for row 2.
  219. */
  220. goog.vec.Matrix3.setColumnValues = function(
  221. mat, column, v0, v1, v2) {
  222. var i = column * 3;
  223. mat[i] = v0;
  224. mat[i + 1] = v1;
  225. mat[i + 2] = v2;
  226. };
  227. /**
  228. * Sets the specified column with the value from the supplied array.
  229. *
  230. * @param {goog.vec.ArrayType} mat The matrix to receive the
  231. * values.
  232. * @param {number} column The column index to set the values on.
  233. * @param {goog.vec.ArrayType} vec The vector elements for the
  234. * column.
  235. */
  236. goog.vec.Matrix3.setColumn = function(mat, column, vec) {
  237. var i = column * 3;
  238. mat[i] = vec[0];
  239. mat[i + 1] = vec[1];
  240. mat[i + 2] = vec[2];
  241. };
  242. /**
  243. * Retrieves the specified column from the matrix into the given vector
  244. * array.
  245. *
  246. * @param {goog.vec.ArrayType} mat The matrix supplying the
  247. * values.
  248. * @param {number} column The column to get the values from.
  249. * @param {goog.vec.ArrayType} vec The vector elements to receive
  250. * the column.
  251. */
  252. goog.vec.Matrix3.getColumn = function(mat, column, vec) {
  253. var i = column * 3;
  254. vec[0] = mat[i];
  255. vec[1] = mat[i + 1];
  256. vec[2] = mat[i + 2];
  257. };
  258. /**
  259. * Sets the columns of the matrix from the set of vector elements.
  260. *
  261. * @param {goog.vec.ArrayType} mat The matrix to receive the
  262. * values.
  263. * @param {goog.vec.ArrayType} vec0 The values for column 0.
  264. * @param {goog.vec.ArrayType} vec1 The values for column 1.
  265. * @param {goog.vec.ArrayType} vec2 The values for column 2.
  266. */
  267. goog.vec.Matrix3.setColumns = function(
  268. mat, vec0, vec1, vec2) {
  269. goog.vec.Matrix3.setColumn(mat, 0, vec0);
  270. goog.vec.Matrix3.setColumn(mat, 1, vec1);
  271. goog.vec.Matrix3.setColumn(mat, 2, vec2);
  272. };
  273. /**
  274. * Retrieves the column values from the given matrix into the given vector
  275. * elements.
  276. *
  277. * @param {goog.vec.ArrayType} mat The matrix containing the
  278. * columns to retrieve.
  279. * @param {goog.vec.ArrayType} vec0 The vector elements to receive
  280. * column 0.
  281. * @param {goog.vec.ArrayType} vec1 The vector elements to receive
  282. * column 1.
  283. * @param {goog.vec.ArrayType} vec2 The vector elements to receive
  284. * column 2.
  285. */
  286. goog.vec.Matrix3.getColumns = function(
  287. mat, vec0, vec1, vec2) {
  288. goog.vec.Matrix3.getColumn(mat, 0, vec0);
  289. goog.vec.Matrix3.getColumn(mat, 1, vec1);
  290. goog.vec.Matrix3.getColumn(mat, 2, vec2);
  291. };
  292. /**
  293. * Sets the row values from the supplied values.
  294. *
  295. * @param {goog.vec.ArrayType} mat The matrix to receive the
  296. * values.
  297. * @param {number} row The index of the row to receive the values.
  298. * @param {number} v0 The value for column 0.
  299. * @param {number} v1 The value for column 1.
  300. * @param {number} v2 The value for column 2.
  301. */
  302. goog.vec.Matrix3.setRowValues = function(mat, row, v0, v1, v2) {
  303. mat[row] = v0;
  304. mat[row + 3] = v1;
  305. mat[row + 6] = v2;
  306. };
  307. /**
  308. * Sets the row values from the supplied vector.
  309. *
  310. * @param {goog.vec.ArrayType} mat The matrix to receive the
  311. * row values.
  312. * @param {number} row The index of the row.
  313. * @param {goog.vec.ArrayType} vec The vector containing the values.
  314. */
  315. goog.vec.Matrix3.setRow = function(mat, row, vec) {
  316. mat[row] = vec[0];
  317. mat[row + 3] = vec[1];
  318. mat[row + 6] = vec[2];
  319. };
  320. /**
  321. * Retrieves the row values into the given vector.
  322. *
  323. * @param {goog.vec.ArrayType} mat The matrix supplying the
  324. * values.
  325. * @param {number} row The index of the row supplying the values.
  326. * @param {goog.vec.ArrayType} vec The vector to receive the row.
  327. */
  328. goog.vec.Matrix3.getRow = function(mat, row, vec) {
  329. vec[0] = mat[row];
  330. vec[1] = mat[row + 3];
  331. vec[2] = mat[row + 6];
  332. };
  333. /**
  334. * Sets the rows of the matrix from the supplied vectors.
  335. *
  336. * @param {goog.vec.ArrayType} mat The matrix to receive the
  337. * values.
  338. * @param {goog.vec.ArrayType} vec0 The values for row 0.
  339. * @param {goog.vec.ArrayType} vec1 The values for row 1.
  340. * @param {goog.vec.ArrayType} vec2 The values for row 2.
  341. */
  342. goog.vec.Matrix3.setRows = function(
  343. mat, vec0, vec1, vec2) {
  344. goog.vec.Matrix3.setRow(mat, 0, vec0);
  345. goog.vec.Matrix3.setRow(mat, 1, vec1);
  346. goog.vec.Matrix3.setRow(mat, 2, vec2);
  347. };
  348. /**
  349. * Retrieves the rows of the matrix into the supplied vectors.
  350. *
  351. * @param {goog.vec.ArrayType} mat The matrix to supplying
  352. * the values.
  353. * @param {goog.vec.ArrayType} vec0 The vector to receive row 0.
  354. * @param {goog.vec.ArrayType} vec1 The vector to receive row 1.
  355. * @param {goog.vec.ArrayType} vec2 The vector to receive row 2.
  356. */
  357. goog.vec.Matrix3.getRows = function(
  358. mat, vec0, vec1, vec2) {
  359. goog.vec.Matrix3.getRow(mat, 0, vec0);
  360. goog.vec.Matrix3.getRow(mat, 1, vec1);
  361. goog.vec.Matrix3.getRow(mat, 2, vec2);
  362. };
  363. /**
  364. * Clears the given matrix to zero.
  365. *
  366. * @param {goog.vec.ArrayType} mat The matrix to clear.
  367. */
  368. goog.vec.Matrix3.setZero = function(mat) {
  369. mat[0] = 0;
  370. mat[1] = 0;
  371. mat[2] = 0;
  372. mat[3] = 0;
  373. mat[4] = 0;
  374. mat[5] = 0;
  375. mat[6] = 0;
  376. mat[7] = 0;
  377. mat[8] = 0;
  378. };
  379. /**
  380. * Sets the given matrix to the identity matrix.
  381. *
  382. * @param {goog.vec.ArrayType} mat The matrix to set.
  383. */
  384. goog.vec.Matrix3.setIdentity = function(mat) {
  385. mat[0] = 1;
  386. mat[1] = 0;
  387. mat[2] = 0;
  388. mat[3] = 0;
  389. mat[4] = 1;
  390. mat[5] = 0;
  391. mat[6] = 0;
  392. mat[7] = 0;
  393. mat[8] = 1;
  394. };
  395. /**
  396. * Performs a per-component addition of the matrices mat0 and mat1, storing
  397. * the result into resultMat.
  398. *
  399. * @param {goog.vec.ArrayType} mat0 The first addend.
  400. * @param {goog.vec.ArrayType} mat1 The second addend.
  401. * @param {goog.vec.ArrayType} resultMat The matrix to
  402. * receive the results (may be either mat0 or mat1).
  403. * @return {goog.vec.ArrayType} return resultMat so that operations can be
  404. * chained together.
  405. */
  406. goog.vec.Matrix3.add = function(mat0, mat1, resultMat) {
  407. resultMat[0] = mat0[0] + mat1[0];
  408. resultMat[1] = mat0[1] + mat1[1];
  409. resultMat[2] = mat0[2] + mat1[2];
  410. resultMat[3] = mat0[3] + mat1[3];
  411. resultMat[4] = mat0[4] + mat1[4];
  412. resultMat[5] = mat0[5] + mat1[5];
  413. resultMat[6] = mat0[6] + mat1[6];
  414. resultMat[7] = mat0[7] + mat1[7];
  415. resultMat[8] = mat0[8] + mat1[8];
  416. return resultMat;
  417. };
  418. /**
  419. * Performs a per-component subtraction of the matrices mat0 and mat1,
  420. * storing the result into resultMat.
  421. *
  422. * @param {goog.vec.ArrayType} mat0 The minuend.
  423. * @param {goog.vec.ArrayType} mat1 The subtrahend.
  424. * @param {goog.vec.ArrayType} resultMat The matrix to receive
  425. * the results (may be either mat0 or mat1).
  426. * @return {goog.vec.ArrayType} return resultMat so that operations can be
  427. * chained together.
  428. */
  429. goog.vec.Matrix3.subtract = function(mat0, mat1, resultMat) {
  430. resultMat[0] = mat0[0] - mat1[0];
  431. resultMat[1] = mat0[1] - mat1[1];
  432. resultMat[2] = mat0[2] - mat1[2];
  433. resultMat[3] = mat0[3] - mat1[3];
  434. resultMat[4] = mat0[4] - mat1[4];
  435. resultMat[5] = mat0[5] - mat1[5];
  436. resultMat[6] = mat0[6] - mat1[6];
  437. resultMat[7] = mat0[7] - mat1[7];
  438. resultMat[8] = mat0[8] - mat1[8];
  439. return resultMat;
  440. };
  441. /**
  442. * Performs a component-wise multiplication of mat0 with the given scalar
  443. * storing the result into resultMat.
  444. *
  445. * @param {goog.vec.ArrayType} mat0 The matrix to scale.
  446. * @param {number} scalar The scalar value to multiple to each element of mat0.
  447. * @param {goog.vec.ArrayType} resultMat The matrix to receive
  448. * the results (may be mat0).
  449. * @return {goog.vec.ArrayType} return resultMat so that operations can be
  450. * chained together.
  451. */
  452. goog.vec.Matrix3.scale = function(mat0, scalar, resultMat) {
  453. resultMat[0] = mat0[0] * scalar;
  454. resultMat[1] = mat0[1] * scalar;
  455. resultMat[2] = mat0[2] * scalar;
  456. resultMat[3] = mat0[3] * scalar;
  457. resultMat[4] = mat0[4] * scalar;
  458. resultMat[5] = mat0[5] * scalar;
  459. resultMat[6] = mat0[6] * scalar;
  460. resultMat[7] = mat0[7] * scalar;
  461. resultMat[8] = mat0[8] * scalar;
  462. return resultMat;
  463. };
  464. /**
  465. * Multiplies the two matrices mat0 and mat1 using matrix multiplication,
  466. * storing the result into resultMat.
  467. *
  468. * @param {goog.vec.ArrayType} mat0 The first (left hand) matrix.
  469. * @param {goog.vec.ArrayType} mat1 The second (right hand)
  470. * matrix.
  471. * @param {goog.vec.ArrayType} resultMat The matrix to receive
  472. * the results (may be either mat0 or mat1).
  473. * @return {goog.vec.ArrayType} return resultMat so that operations can be
  474. * chained together.
  475. */
  476. goog.vec.Matrix3.multMat = function(mat0, mat1, resultMat) {
  477. var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
  478. var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
  479. var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
  480. var b00 = mat1[0], b10 = mat1[1], b20 = mat1[2];
  481. var b01 = mat1[3], b11 = mat1[4], b21 = mat1[5];
  482. var b02 = mat1[6], b12 = mat1[7], b22 = mat1[8];
  483. resultMat[0] = a00 * b00 + a01 * b10 + a02 * b20;
  484. resultMat[1] = a10 * b00 + a11 * b10 + a12 * b20;
  485. resultMat[2] = a20 * b00 + a21 * b10 + a22 * b20;
  486. resultMat[3] = a00 * b01 + a01 * b11 + a02 * b21;
  487. resultMat[4] = a10 * b01 + a11 * b11 + a12 * b21;
  488. resultMat[5] = a20 * b01 + a21 * b11 + a22 * b21;
  489. resultMat[6] = a00 * b02 + a01 * b12 + a02 * b22;
  490. resultMat[7] = a10 * b02 + a11 * b12 + a12 * b22;
  491. resultMat[8] = a20 * b02 + a21 * b12 + a22 * b22;
  492. return resultMat;
  493. };
  494. /**
  495. * Transposes the given matrix mat storing the result into resultMat.
  496. * @param {goog.vec.ArrayType} mat The matrix to transpose.
  497. * @param {goog.vec.ArrayType} resultMat The matrix to receive
  498. * the results (may be mat).
  499. * @return {goog.vec.ArrayType} return resultMat so that operations can be
  500. * chained together.
  501. */
  502. goog.vec.Matrix3.transpose = function(mat, resultMat) {
  503. if (resultMat == mat) {
  504. var a10 = mat[1], a20 = mat[2], a21 = mat[5];
  505. resultMat[1] = mat[3];
  506. resultMat[2] = mat[6];
  507. resultMat[3] = a10;
  508. resultMat[5] = mat[7];
  509. resultMat[6] = a20;
  510. resultMat[7] = a21;
  511. } else {
  512. resultMat[0] = mat[0];
  513. resultMat[1] = mat[3];
  514. resultMat[2] = mat[6];
  515. resultMat[3] = mat[1];
  516. resultMat[4] = mat[4];
  517. resultMat[5] = mat[7];
  518. resultMat[6] = mat[2];
  519. resultMat[7] = mat[5];
  520. resultMat[8] = mat[8];
  521. }
  522. return resultMat;
  523. };
  524. /**
  525. * Computes the inverse of mat0 storing the result into resultMat. If the
  526. * inverse is defined, this function returns true, false otherwise.
  527. * @param {goog.vec.ArrayType} mat0 The matrix to invert.
  528. * @param {goog.vec.ArrayType} resultMat The matrix to receive
  529. * the result (may be mat0).
  530. * @return {boolean} True if the inverse is defined. If false is returned,
  531. * resultMat is not modified.
  532. */
  533. goog.vec.Matrix3.invert = function(mat0, resultMat) {
  534. var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
  535. var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
  536. var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
  537. var t00 = a11 * a22 - a12 * a21;
  538. var t10 = a12 * a20 - a10 * a22;
  539. var t20 = a10 * a21 - a11 * a20;
  540. var det = a00 * t00 + a01 * t10 + a02 * t20;
  541. if (det == 0) {
  542. return false;
  543. }
  544. var idet = 1 / det;
  545. resultMat[0] = t00 * idet;
  546. resultMat[3] = (a02 * a21 - a01 * a22) * idet;
  547. resultMat[6] = (a01 * a12 - a02 * a11) * idet;
  548. resultMat[1] = t10 * idet;
  549. resultMat[4] = (a00 * a22 - a02 * a20) * idet;
  550. resultMat[7] = (a02 * a10 - a00 * a12) * idet;
  551. resultMat[2] = t20 * idet;
  552. resultMat[5] = (a01 * a20 - a00 * a21) * idet;
  553. resultMat[8] = (a00 * a11 - a01 * a10) * idet;
  554. return true;
  555. };
  556. /**
  557. * Returns true if the components of mat0 are equal to the components of mat1.
  558. *
  559. * @param {goog.vec.ArrayType} mat0 The first matrix.
  560. * @param {goog.vec.ArrayType} mat1 The second matrix.
  561. * @return {boolean} True if the the two matrices are equivalent.
  562. */
  563. goog.vec.Matrix3.equals = function(mat0, mat1) {
  564. return mat0.length == mat1.length &&
  565. mat0[0] == mat1[0] && mat0[1] == mat1[1] && mat0[2] == mat1[2] &&
  566. mat0[3] == mat1[3] && mat0[4] == mat1[4] && mat0[5] == mat1[5] &&
  567. mat0[6] == mat1[6] && mat0[7] == mat1[7] && mat0[8] == mat1[8];
  568. };
  569. /**
  570. * Transforms the given vector with the given matrix storing the resulting,
  571. * transformed matrix into resultVec.
  572. *
  573. * @param {goog.vec.ArrayType} mat The matrix supplying the
  574. * transformation.
  575. * @param {goog.vec.ArrayType} vec The vector to transform.
  576. * @param {goog.vec.ArrayType} resultVec The vector to
  577. * receive the results (may be vec).
  578. * @return {goog.vec.ArrayType} return resultVec so that operations can be
  579. * chained together.
  580. */
  581. goog.vec.Matrix3.multVec3 = function(mat, vec, resultVec) {
  582. var x = vec[0], y = vec[1], z = vec[2];
  583. resultVec[0] = x * mat[0] + y * mat[3] + z * mat[6];
  584. resultVec[1] = x * mat[1] + y * mat[4] + z * mat[7];
  585. resultVec[2] = x * mat[2] + y * mat[5] + z * mat[8];
  586. return resultVec;
  587. };
  588. /**
  589. * Initializes the given 3x3 matrix as a translation matrix with x and y
  590. * translation values.
  591. *
  592. * @param {goog.vec.ArrayType} mat The 3x3 (9-element) matrix
  593. * array to receive the new translation matrix.
  594. * @param {number} x The translation along the x axis.
  595. * @param {number} y The translation along the y axis.
  596. */
  597. goog.vec.Matrix3.makeTranslate = function(mat, x, y) {
  598. goog.vec.Matrix3.setIdentity(mat);
  599. goog.vec.Matrix3.setColumnValues(mat, 2, x, y, 1);
  600. };
  601. /**
  602. * Initializes the given 3x3 matrix as a scale matrix with x, y and z scale
  603. * factors.
  604. * @param {goog.vec.ArrayType} mat The 3x3 (9-element) matrix
  605. * array to receive the new scale matrix.
  606. * @param {number} x The scale along the x axis.
  607. * @param {number} y The scale along the y axis.
  608. * @param {number} z The scale along the z axis.
  609. */
  610. goog.vec.Matrix3.makeScale = function(mat, x, y, z) {
  611. goog.vec.Matrix3.setIdentity(mat);
  612. goog.vec.Matrix3.setDiagonalValues(mat, x, y, z);
  613. };
  614. /**
  615. * Initializes the given 3x3 matrix as a rotation matrix with the given rotation
  616. * angle about the axis defined by the vector (ax, ay, az).
  617. * @param {goog.vec.ArrayType} mat The 3x3 (9-element) matrix
  618. * array to receive the new scale matrix.
  619. * @param {number} angle The rotation angle in radians.
  620. * @param {number} ax The x component of the rotation axis.
  621. * @param {number} ay The y component of the rotation axis.
  622. * @param {number} az The z component of the rotation axis.
  623. */
  624. goog.vec.Matrix3.makeAxisAngleRotate = function(
  625. mat, angle, ax, ay, az) {
  626. var c = Math.cos(angle);
  627. var d = 1 - c;
  628. var s = Math.sin(angle);
  629. goog.vec.Matrix3.setFromValues(mat,
  630. ax * ax * d + c,
  631. ax * ay * d + az * s,
  632. ax * az * d - ay * s,
  633. ax * ay * d - az * s,
  634. ay * ay * d + c,
  635. ay * az * d + ax * s,
  636. ax * az * d + ay * s,
  637. ay * az * d - ax * s,
  638. az * az * d + c);
  639. };