mat3.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 Implements 3x3 matrices and their related functions which are
  16. * compatible with WebGL. The API is structured to avoid unnecessary memory
  17. * allocations. The last parameter will typically be the output vector and
  18. * an object can be both an input and output parameter to all methods except
  19. * where noted. Matrix operations follow the mathematical form when multiplying
  20. * vectors as follows: resultVec = matrix * vec.
  21. *
  22. */
  23. goog.provide('goog.vec.Mat3');
  24. goog.require('goog.vec');
  25. goog.require('goog.vec.Vec3');
  26. /** @typedef {goog.vec.Float32} */ goog.vec.Mat3.Float32;
  27. /** @typedef {goog.vec.Float64} */ goog.vec.Mat3.Float64;
  28. /** @typedef {goog.vec.Number} */ goog.vec.Mat3.Number;
  29. /** @typedef {goog.vec.AnyType} */ goog.vec.Mat3.AnyType;
  30. // The following two types are deprecated - use the above types instead.
  31. /** @typedef {Float32Array} */ goog.vec.Mat3.Type;
  32. /** @typedef {goog.vec.ArrayType} */ goog.vec.Mat3.Mat3Like;
  33. /**
  34. * Creates the array representation of a 3x3 matrix of Float32.
  35. * The use of the array directly instead of a class reduces overhead.
  36. * The returned matrix is cleared to all zeros.
  37. *
  38. * @return {!goog.vec.Mat3.Float32} The new matrix.
  39. */
  40. goog.vec.Mat3.createFloat32 = function() {
  41. return new Float32Array(9);
  42. };
  43. /**
  44. * Creates the array representation of a 3x3 matrix of Float64.
  45. * The returned matrix is cleared to all zeros.
  46. *
  47. * @return {!goog.vec.Mat3.Float64} The new matrix.
  48. */
  49. goog.vec.Mat3.createFloat64 = function() {
  50. return new Float64Array(9);
  51. };
  52. /**
  53. * Creates the array representation of a 3x3 matrix of Number.
  54. * The returned matrix is cleared to all zeros.
  55. *
  56. * @return {!goog.vec.Mat3.Number} The new matrix.
  57. */
  58. goog.vec.Mat3.createNumber = function() {
  59. var a = new Array(9);
  60. goog.vec.Mat3.setFromValues(a,
  61. 0, 0, 0,
  62. 0, 0, 0,
  63. 0, 0, 0);
  64. return a;
  65. };
  66. /**
  67. * Creates the array representation of a 3x3 matrix of Float32.
  68. * The returned matrix is cleared to all zeros.
  69. *
  70. * @deprecated Use createFloat32.
  71. * @return {!goog.vec.Mat3.Type} The new matrix.
  72. */
  73. goog.vec.Mat3.create = function() {
  74. return goog.vec.Mat3.createFloat32();
  75. };
  76. /**
  77. * Creates a 3x3 identity matrix of Float32.
  78. *
  79. * @return {!goog.vec.Mat3.Float32} The new 9 element array.
  80. */
  81. goog.vec.Mat3.createFloat32Identity = function() {
  82. var mat = goog.vec.Mat3.createFloat32();
  83. mat[0] = mat[4] = mat[8] = 1;
  84. return mat;
  85. };
  86. /**
  87. * Creates a 3x3 identity matrix of Float64.
  88. *
  89. * @return {!goog.vec.Mat3.Float64} The new 9 element array.
  90. */
  91. goog.vec.Mat3.createFloat64Identity = function() {
  92. var mat = goog.vec.Mat3.createFloat64();
  93. mat[0] = mat[4] = mat[8] = 1;
  94. return mat;
  95. };
  96. /**
  97. * Creates a 3x3 identity matrix of Number.
  98. * The returned matrix is cleared to all zeros.
  99. *
  100. * @return {!goog.vec.Mat3.Number} The new 9 element array.
  101. */
  102. goog.vec.Mat3.createNumberIdentity = function() {
  103. var a = new Array(9);
  104. goog.vec.Mat3.setFromValues(a,
  105. 1, 0, 0,
  106. 0, 1, 0,
  107. 0, 0, 1);
  108. return a;
  109. };
  110. /**
  111. * Creates the array representation of a 3x3 matrix of Float32.
  112. * The returned matrix is cleared to all zeros.
  113. *
  114. * @deprecated Use createFloat32Identity.
  115. * @return {!goog.vec.Mat3.Type} The new 9 element array.
  116. */
  117. goog.vec.Mat3.createIdentity = function() {
  118. return goog.vec.Mat3.createFloat32Identity();
  119. };
  120. /**
  121. * Creates a 3x3 matrix of Float32 initialized from the given array.
  122. *
  123. * @param {goog.vec.Mat3.AnyType} matrix The array containing the
  124. * matrix values in column major order.
  125. * @return {!goog.vec.Mat3.Float32} The new, nine element array.
  126. */
  127. goog.vec.Mat3.createFloat32FromArray = function(matrix) {
  128. var newMatrix = goog.vec.Mat3.createFloat32();
  129. goog.vec.Mat3.setFromArray(newMatrix, matrix);
  130. return newMatrix;
  131. };
  132. /**
  133. * Creates a 3x3 matrix of Float32 initialized from the given values.
  134. *
  135. * @param {number} v00 The values at (0, 0).
  136. * @param {number} v10 The values at (1, 0).
  137. * @param {number} v20 The values at (2, 0).
  138. * @param {number} v01 The values at (0, 1).
  139. * @param {number} v11 The values at (1, 1).
  140. * @param {number} v21 The values at (2, 1).
  141. * @param {number} v02 The values at (0, 2).
  142. * @param {number} v12 The values at (1, 2).
  143. * @param {number} v22 The values at (2, 2).
  144. * @return {!goog.vec.Mat3.Float32} The new, nine element array.
  145. */
  146. goog.vec.Mat3.createFloat32FromValues = function(
  147. v00, v10, v20, v01, v11, v21, v02, v12, v22) {
  148. var newMatrix = goog.vec.Mat3.createFloat32();
  149. goog.vec.Mat3.setFromValues(
  150. newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
  151. return newMatrix;
  152. };
  153. /**
  154. * Creates a clone of a 3x3 matrix of Float32.
  155. *
  156. * @param {goog.vec.Mat3.Float32} matrix The source 3x3 matrix.
  157. * @return {!goog.vec.Mat3.Float32} The new 3x3 element matrix.
  158. */
  159. goog.vec.Mat3.cloneFloat32 = goog.vec.Mat3.createFloat32FromArray;
  160. /**
  161. * Creates a 3x3 matrix of Float64 initialized from the given array.
  162. *
  163. * @param {goog.vec.Mat3.AnyType} matrix The array containing the
  164. * matrix values in column major order.
  165. * @return {!goog.vec.Mat3.Float64} The new, nine element array.
  166. */
  167. goog.vec.Mat3.createFloat64FromArray = function(matrix) {
  168. var newMatrix = goog.vec.Mat3.createFloat64();
  169. goog.vec.Mat3.setFromArray(newMatrix, matrix);
  170. return newMatrix;
  171. };
  172. /**
  173. * Creates a 3x3 matrix of Float64 initialized from the given values.
  174. *
  175. * @param {number} v00 The values at (0, 0).
  176. * @param {number} v10 The values at (1, 0).
  177. * @param {number} v20 The values at (2, 0).
  178. * @param {number} v01 The values at (0, 1).
  179. * @param {number} v11 The values at (1, 1).
  180. * @param {number} v21 The values at (2, 1).
  181. * @param {number} v02 The values at (0, 2).
  182. * @param {number} v12 The values at (1, 2).
  183. * @param {number} v22 The values at (2, 2).
  184. * @return {!goog.vec.Mat3.Float64} The new, nine element array.
  185. */
  186. goog.vec.Mat3.createFloat64FromValues = function(
  187. v00, v10, v20, v01, v11, v21, v02, v12, v22) {
  188. var newMatrix = goog.vec.Mat3.createFloat64();
  189. goog.vec.Mat3.setFromValues(
  190. newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
  191. return newMatrix;
  192. };
  193. /**
  194. * Creates a clone of a 3x3 matrix of Float64.
  195. *
  196. * @param {goog.vec.Mat3.Float64} matrix The source 3x3 matrix.
  197. * @return {!goog.vec.Mat3.Float64} The new 3x3 element matrix.
  198. */
  199. goog.vec.Mat3.cloneFloat64 = goog.vec.Mat3.createFloat64FromArray;
  200. /**
  201. * Creates a 3x3 matrix of Float32 initialized from the given array.
  202. *
  203. * @deprecated Use createFloat32FromArray.
  204. * @param {goog.vec.Mat3.Mat3Like} matrix The array containing the
  205. * matrix values in column major order.
  206. * @return {!goog.vec.Mat3.Type} The new, nine element array.
  207. */
  208. goog.vec.Mat3.createFromArray = function(matrix) {
  209. var newMatrix = goog.vec.Mat3.createFloat32();
  210. goog.vec.Mat3.setFromArray(newMatrix, matrix);
  211. return newMatrix;
  212. };
  213. /**
  214. * Creates a 3x3 matrix of Float32 initialized from the given values.
  215. *
  216. * @deprecated Use createFloat32FromValues.
  217. * @param {number} v00 The values at (0, 0).
  218. * @param {number} v10 The values at (1, 0).
  219. * @param {number} v20 The values at (2, 0).
  220. * @param {number} v01 The values at (0, 1).
  221. * @param {number} v11 The values at (1, 1).
  222. * @param {number} v21 The values at (2, 1).
  223. * @param {number} v02 The values at (0, 2).
  224. * @param {number} v12 The values at (1, 2).
  225. * @param {number} v22 The values at (2, 2).
  226. * @return {!goog.vec.Mat3.Type} The new, nine element array.
  227. */
  228. goog.vec.Mat3.createFromValues = function(
  229. v00, v10, v20, v01, v11, v21, v02, v12, v22) {
  230. var newMatrix = goog.vec.Mat3.create();
  231. goog.vec.Mat3.setFromValues(
  232. newMatrix, v00, v10, v20, v01, v11, v21, v02, v12, v22);
  233. return newMatrix;
  234. };
  235. /**
  236. * Creates a clone of a 3x3 matrix of Float32.
  237. *
  238. * @deprecated Use cloneFloat32.
  239. * @param {goog.vec.Mat3.Mat3Like} matrix The source 3x3 matrix.
  240. * @return {!goog.vec.Mat3.Type} The new 3x3 element matrix.
  241. */
  242. goog.vec.Mat3.clone = goog.vec.Mat3.createFromArray;
  243. /**
  244. * Retrieves the element at the requested row and column.
  245. *
  246. * @param {goog.vec.Mat3.AnyType} mat The matrix containing the value to
  247. * retrieve.
  248. * @param {number} row The row index.
  249. * @param {number} column The column index.
  250. * @return {number} The element value at the requested row, column indices.
  251. */
  252. goog.vec.Mat3.getElement = function(mat, row, column) {
  253. return mat[row + column * 3];
  254. };
  255. /**
  256. * Sets the element at the requested row and column.
  257. *
  258. * @param {goog.vec.Mat3.AnyType} mat The matrix containing the value to
  259. * retrieve.
  260. * @param {number} row The row index.
  261. * @param {number} column The column index.
  262. * @param {number} value The value to set at the requested row, column.
  263. */
  264. goog.vec.Mat3.setElement = function(mat, row, column, value) {
  265. mat[row + column * 3] = value;
  266. };
  267. /**
  268. * Initializes the matrix from the set of values. Note the values supplied are
  269. * in column major order.
  270. *
  271. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the
  272. * values.
  273. * @param {number} v00 The values at (0, 0).
  274. * @param {number} v10 The values at (1, 0).
  275. * @param {number} v20 The values at (2, 0).
  276. * @param {number} v01 The values at (0, 1).
  277. * @param {number} v11 The values at (1, 1).
  278. * @param {number} v21 The values at (2, 1).
  279. * @param {number} v02 The values at (0, 2).
  280. * @param {number} v12 The values at (1, 2).
  281. * @param {number} v22 The values at (2, 2).
  282. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  283. * chained together.
  284. */
  285. goog.vec.Mat3.setFromValues = function(
  286. mat, v00, v10, v20, v01, v11, v21, v02, v12, v22) {
  287. mat[0] = v00;
  288. mat[1] = v10;
  289. mat[2] = v20;
  290. mat[3] = v01;
  291. mat[4] = v11;
  292. mat[5] = v21;
  293. mat[6] = v02;
  294. mat[7] = v12;
  295. mat[8] = v22;
  296. return mat;
  297. };
  298. /**
  299. * Sets the matrix from the array of values stored in column major order.
  300. *
  301. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  302. * @param {goog.vec.Mat3.AnyType} values The column major ordered
  303. * array of values to store in the matrix.
  304. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  305. * chained together.
  306. */
  307. goog.vec.Mat3.setFromArray = function(mat, values) {
  308. mat[0] = values[0];
  309. mat[1] = values[1];
  310. mat[2] = values[2];
  311. mat[3] = values[3];
  312. mat[4] = values[4];
  313. mat[5] = values[5];
  314. mat[6] = values[6];
  315. mat[7] = values[7];
  316. mat[8] = values[8];
  317. return mat;
  318. };
  319. /**
  320. * Sets the matrix from the array of values stored in row major order.
  321. *
  322. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  323. * @param {goog.vec.Mat3.AnyType} values The row major ordered array
  324. * of values to store in the matrix.
  325. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  326. * chained together.
  327. */
  328. goog.vec.Mat3.setFromRowMajorArray = function(mat, values) {
  329. mat[0] = values[0];
  330. mat[1] = values[3];
  331. mat[2] = values[6];
  332. mat[3] = values[1];
  333. mat[4] = values[4];
  334. mat[5] = values[7];
  335. mat[6] = values[2];
  336. mat[7] = values[5];
  337. mat[8] = values[8];
  338. return mat;
  339. };
  340. /**
  341. * Sets the diagonal values of the matrix from the given values.
  342. *
  343. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  344. * @param {number} v00 The values for (0, 0).
  345. * @param {number} v11 The values for (1, 1).
  346. * @param {number} v22 The values for (2, 2).
  347. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  348. * chained together.
  349. */
  350. goog.vec.Mat3.setDiagonalValues = function(mat, v00, v11, v22) {
  351. mat[0] = v00;
  352. mat[4] = v11;
  353. mat[8] = v22;
  354. return mat;
  355. };
  356. /**
  357. * Sets the diagonal values of the matrix from the given vector.
  358. *
  359. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  360. * @param {goog.vec.Vec3.AnyType} vec The vector containing the values.
  361. */
  362. goog.vec.Mat3.setDiagonal = function(mat, vec) {
  363. mat[0] = vec[0];
  364. mat[4] = vec[1];
  365. mat[8] = vec[2];
  366. };
  367. /**
  368. * Sets the specified column with the supplied values.
  369. *
  370. * @param {goog.vec.Mat3.AnyType} mat The matrix to recieve the values.
  371. * @param {number} column The column index to set the values on.
  372. * @param {number} v0 The value for row 0.
  373. * @param {number} v1 The value for row 1.
  374. * @param {number} v2 The value for row 2.
  375. */
  376. goog.vec.Mat3.setColumnValues = function(mat, column, v0, v1, v2) {
  377. var i = column * 3;
  378. mat[i] = v0;
  379. mat[i + 1] = v1;
  380. mat[i + 2] = v2;
  381. };
  382. /**
  383. * Sets the specified column with the value from the supplied array.
  384. *
  385. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  386. * @param {number} column The column index to set the values on.
  387. * @param {goog.vec.Vec3.AnyType} vec The vector elements for the column.
  388. */
  389. goog.vec.Mat3.setColumn = function(mat, column, vec) {
  390. var i = column * 3;
  391. mat[i] = vec[0];
  392. mat[i + 1] = vec[1];
  393. mat[i + 2] = vec[2];
  394. };
  395. /**
  396. * Retrieves the specified column from the matrix into the given vector
  397. * array.
  398. *
  399. * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the values.
  400. * @param {number} column The column to get the values from.
  401. * @param {goog.vec.Vec3.AnyType} vec The vector elements to receive the
  402. * column.
  403. */
  404. goog.vec.Mat3.getColumn = function(mat, column, vec) {
  405. var i = column * 3;
  406. vec[0] = mat[i];
  407. vec[1] = mat[i + 1];
  408. vec[2] = mat[i + 2];
  409. };
  410. /**
  411. * Sets the columns of the matrix from the set of vector elements.
  412. *
  413. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  414. * @param {goog.vec.Vec3.AnyType} vec0 The values for column 0.
  415. * @param {goog.vec.Vec3.AnyType} vec1 The values for column 1.
  416. * @param {goog.vec.Vec3.AnyType} vec2 The values for column 2.
  417. */
  418. goog.vec.Mat3.setColumns = function(mat, vec0, vec1, vec2) {
  419. goog.vec.Mat3.setColumn(mat, 0, vec0);
  420. goog.vec.Mat3.setColumn(mat, 1, vec1);
  421. goog.vec.Mat3.setColumn(mat, 2, vec2);
  422. };
  423. /**
  424. * Retrieves the column values from the given matrix into the given vector
  425. * elements.
  426. *
  427. * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the columns.
  428. * @param {goog.vec.Vec3.AnyType} vec0 The vector to receive column 0.
  429. * @param {goog.vec.Vec3.AnyType} vec1 The vector to receive column 1.
  430. * @param {goog.vec.Vec3.AnyType} vec2 The vector to receive column 2.
  431. */
  432. goog.vec.Mat3.getColumns = function(mat, vec0, vec1, vec2) {
  433. goog.vec.Mat3.getColumn(mat, 0, vec0);
  434. goog.vec.Mat3.getColumn(mat, 1, vec1);
  435. goog.vec.Mat3.getColumn(mat, 2, vec2);
  436. };
  437. /**
  438. * Sets the row values from the supplied values.
  439. *
  440. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  441. * @param {number} row The index of the row to receive the values.
  442. * @param {number} v0 The value for column 0.
  443. * @param {number} v1 The value for column 1.
  444. * @param {number} v2 The value for column 2.
  445. */
  446. goog.vec.Mat3.setRowValues = function(mat, row, v0, v1, v2) {
  447. mat[row] = v0;
  448. mat[row + 3] = v1;
  449. mat[row + 6] = v2;
  450. };
  451. /**
  452. * Sets the row values from the supplied vector.
  453. *
  454. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the row values.
  455. * @param {number} row The index of the row.
  456. * @param {goog.vec.Vec3.AnyType} vec The vector containing the values.
  457. */
  458. goog.vec.Mat3.setRow = function(mat, row, vec) {
  459. mat[row] = vec[0];
  460. mat[row + 3] = vec[1];
  461. mat[row + 6] = vec[2];
  462. };
  463. /**
  464. * Retrieves the row values into the given vector.
  465. *
  466. * @param {goog.vec.Mat3.AnyType} mat The matrix supplying the values.
  467. * @param {number} row The index of the row supplying the values.
  468. * @param {goog.vec.Vec3.AnyType} vec The vector to receive the row.
  469. */
  470. goog.vec.Mat3.getRow = function(mat, row, vec) {
  471. vec[0] = mat[row];
  472. vec[1] = mat[row + 3];
  473. vec[2] = mat[row + 6];
  474. };
  475. /**
  476. * Sets the rows of the matrix from the supplied vectors.
  477. *
  478. * @param {goog.vec.Mat3.AnyType} mat The matrix to receive the values.
  479. * @param {goog.vec.Vec3.AnyType} vec0 The values for row 0.
  480. * @param {goog.vec.Vec3.AnyType} vec1 The values for row 1.
  481. * @param {goog.vec.Vec3.AnyType} vec2 The values for row 2.
  482. */
  483. goog.vec.Mat3.setRows = function(mat, vec0, vec1, vec2) {
  484. goog.vec.Mat3.setRow(mat, 0, vec0);
  485. goog.vec.Mat3.setRow(mat, 1, vec1);
  486. goog.vec.Mat3.setRow(mat, 2, vec2);
  487. };
  488. /**
  489. * Retrieves the rows of the matrix into the supplied vectors.
  490. *
  491. * @param {goog.vec.Mat3.AnyType} mat The matrix to supplying the values.
  492. * @param {goog.vec.Vec3.AnyType} vec0 The vector to receive row 0.
  493. * @param {goog.vec.Vec3.AnyType} vec1 The vector to receive row 1.
  494. * @param {goog.vec.Vec3.AnyType} vec2 The vector to receive row 2.
  495. */
  496. goog.vec.Mat3.getRows = function(mat, vec0, vec1, vec2) {
  497. goog.vec.Mat3.getRow(mat, 0, vec0);
  498. goog.vec.Mat3.getRow(mat, 1, vec1);
  499. goog.vec.Mat3.getRow(mat, 2, vec2);
  500. };
  501. /**
  502. * Makes the given 3x3 matrix the zero matrix.
  503. *
  504. * @param {goog.vec.Mat3.AnyType} mat The matrix.
  505. * @return {!goog.vec.Mat3.AnyType} return mat so operations can be chained.
  506. */
  507. goog.vec.Mat3.makeZero = function(mat) {
  508. mat[0] = 0;
  509. mat[1] = 0;
  510. mat[2] = 0;
  511. mat[3] = 0;
  512. mat[4] = 0;
  513. mat[5] = 0;
  514. mat[6] = 0;
  515. mat[7] = 0;
  516. mat[8] = 0;
  517. return mat;
  518. };
  519. /**
  520. * Makes the given 3x3 matrix the identity matrix.
  521. *
  522. * @param {goog.vec.Mat3.AnyType} mat The matrix.
  523. * @return {!goog.vec.Mat3.AnyType} return mat so operations can be chained.
  524. */
  525. goog.vec.Mat3.makeIdentity = function(mat) {
  526. mat[0] = 1;
  527. mat[1] = 0;
  528. mat[2] = 0;
  529. mat[3] = 0;
  530. mat[4] = 1;
  531. mat[5] = 0;
  532. mat[6] = 0;
  533. mat[7] = 0;
  534. mat[8] = 1;
  535. return mat;
  536. };
  537. /**
  538. * Performs a per-component addition of the matrices mat0 and mat1, storing
  539. * the result into resultMat.
  540. *
  541. * @param {goog.vec.Mat3.AnyType} mat0 The first addend.
  542. * @param {goog.vec.Mat3.AnyType} mat1 The second addend.
  543. * @param {goog.vec.Mat3.AnyType} resultMat The matrix to
  544. * receive the results (may be either mat0 or mat1).
  545. * @return {!goog.vec.Mat3.AnyType} return resultMat so that operations can be
  546. * chained together.
  547. */
  548. goog.vec.Mat3.addMat = function(mat0, mat1, resultMat) {
  549. resultMat[0] = mat0[0] + mat1[0];
  550. resultMat[1] = mat0[1] + mat1[1];
  551. resultMat[2] = mat0[2] + mat1[2];
  552. resultMat[3] = mat0[3] + mat1[3];
  553. resultMat[4] = mat0[4] + mat1[4];
  554. resultMat[5] = mat0[5] + mat1[5];
  555. resultMat[6] = mat0[6] + mat1[6];
  556. resultMat[7] = mat0[7] + mat1[7];
  557. resultMat[8] = mat0[8] + mat1[8];
  558. return resultMat;
  559. };
  560. /**
  561. * Performs a per-component subtraction of the matrices mat0 and mat1,
  562. * storing the result into resultMat.
  563. *
  564. * @param {goog.vec.Mat3.AnyType} mat0 The minuend.
  565. * @param {goog.vec.Mat3.AnyType} mat1 The subtrahend.
  566. * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
  567. * the results (may be either mat0 or mat1).
  568. * @return {!goog.vec.Mat3.AnyType} return resultMat so that operations can be
  569. * chained together.
  570. */
  571. goog.vec.Mat3.subMat = function(mat0, mat1, resultMat) {
  572. resultMat[0] = mat0[0] - mat1[0];
  573. resultMat[1] = mat0[1] - mat1[1];
  574. resultMat[2] = mat0[2] - mat1[2];
  575. resultMat[3] = mat0[3] - mat1[3];
  576. resultMat[4] = mat0[4] - mat1[4];
  577. resultMat[5] = mat0[5] - mat1[5];
  578. resultMat[6] = mat0[6] - mat1[6];
  579. resultMat[7] = mat0[7] - mat1[7];
  580. resultMat[8] = mat0[8] - mat1[8];
  581. return resultMat;
  582. };
  583. /**
  584. * Multiplies matrix mat0 with the given scalar, storing the result
  585. * into resultMat.
  586. *
  587. * @param {goog.vec.Mat3.AnyType} mat The matrix.
  588. * @param {number} scalar The scalar value to multiple to each element of mat.
  589. * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
  590. * the results (may be mat).
  591. * @return {!goog.vec.Mat3.AnyType} return resultMat so that operations can be
  592. * chained together.
  593. */
  594. goog.vec.Mat3.multScalar = function(mat, scalar, resultMat) {
  595. resultMat[0] = mat[0] * scalar;
  596. resultMat[1] = mat[1] * scalar;
  597. resultMat[2] = mat[2] * scalar;
  598. resultMat[3] = mat[3] * scalar;
  599. resultMat[4] = mat[4] * scalar;
  600. resultMat[5] = mat[5] * scalar;
  601. resultMat[6] = mat[6] * scalar;
  602. resultMat[7] = mat[7] * scalar;
  603. resultMat[8] = mat[8] * scalar;
  604. return resultMat;
  605. };
  606. /**
  607. * Multiplies the two matrices mat0 and mat1 using matrix multiplication,
  608. * storing the result into resultMat.
  609. *
  610. * @param {goog.vec.Mat3.AnyType} mat0 The first (left hand) matrix.
  611. * @param {goog.vec.Mat3.AnyType} mat1 The second (right hand) matrix.
  612. * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
  613. * the results (may be either mat0 or mat1).
  614. * @return {!goog.vec.Mat3.AnyType} return resultMat so that operations can be
  615. * chained together.
  616. */
  617. goog.vec.Mat3.multMat = function(mat0, mat1, resultMat) {
  618. var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
  619. var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
  620. var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
  621. var b00 = mat1[0], b10 = mat1[1], b20 = mat1[2];
  622. var b01 = mat1[3], b11 = mat1[4], b21 = mat1[5];
  623. var b02 = mat1[6], b12 = mat1[7], b22 = mat1[8];
  624. resultMat[0] = a00 * b00 + a01 * b10 + a02 * b20;
  625. resultMat[1] = a10 * b00 + a11 * b10 + a12 * b20;
  626. resultMat[2] = a20 * b00 + a21 * b10 + a22 * b20;
  627. resultMat[3] = a00 * b01 + a01 * b11 + a02 * b21;
  628. resultMat[4] = a10 * b01 + a11 * b11 + a12 * b21;
  629. resultMat[5] = a20 * b01 + a21 * b11 + a22 * b21;
  630. resultMat[6] = a00 * b02 + a01 * b12 + a02 * b22;
  631. resultMat[7] = a10 * b02 + a11 * b12 + a12 * b22;
  632. resultMat[8] = a20 * b02 + a21 * b12 + a22 * b22;
  633. return resultMat;
  634. };
  635. /**
  636. * Transposes the given matrix mat storing the result into resultMat.
  637. *
  638. * @param {goog.vec.Mat3.AnyType} mat The matrix to transpose.
  639. * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
  640. * the results (may be mat).
  641. * @return {!goog.vec.Mat3.AnyType} return resultMat so that operations can be
  642. * chained together.
  643. */
  644. goog.vec.Mat3.transpose = function(mat, resultMat) {
  645. if (resultMat == mat) {
  646. var a10 = mat[1], a20 = mat[2], a21 = mat[5];
  647. resultMat[1] = mat[3];
  648. resultMat[2] = mat[6];
  649. resultMat[3] = a10;
  650. resultMat[5] = mat[7];
  651. resultMat[6] = a20;
  652. resultMat[7] = a21;
  653. } else {
  654. resultMat[0] = mat[0];
  655. resultMat[1] = mat[3];
  656. resultMat[2] = mat[6];
  657. resultMat[3] = mat[1];
  658. resultMat[4] = mat[4];
  659. resultMat[5] = mat[7];
  660. resultMat[6] = mat[2];
  661. resultMat[7] = mat[5];
  662. resultMat[8] = mat[8];
  663. }
  664. return resultMat;
  665. };
  666. /**
  667. * Computes the inverse of mat0 storing the result into resultMat. If the
  668. * inverse is defined, this function returns true, false otherwise.
  669. *
  670. * @param {goog.vec.Mat3.AnyType} mat0 The matrix to invert.
  671. * @param {goog.vec.Mat3.AnyType} resultMat The matrix to receive
  672. * the result (may be mat0).
  673. * @return {boolean} True if the inverse is defined. If false is returned,
  674. * resultMat is not modified.
  675. */
  676. goog.vec.Mat3.invert = function(mat0, resultMat) {
  677. var a00 = mat0[0], a10 = mat0[1], a20 = mat0[2];
  678. var a01 = mat0[3], a11 = mat0[4], a21 = mat0[5];
  679. var a02 = mat0[6], a12 = mat0[7], a22 = mat0[8];
  680. var t00 = a11 * a22 - a12 * a21;
  681. var t10 = a12 * a20 - a10 * a22;
  682. var t20 = a10 * a21 - a11 * a20;
  683. var det = a00 * t00 + a01 * t10 + a02 * t20;
  684. if (det == 0) {
  685. return false;
  686. }
  687. var idet = 1 / det;
  688. resultMat[0] = t00 * idet;
  689. resultMat[3] = (a02 * a21 - a01 * a22) * idet;
  690. resultMat[6] = (a01 * a12 - a02 * a11) * idet;
  691. resultMat[1] = t10 * idet;
  692. resultMat[4] = (a00 * a22 - a02 * a20) * idet;
  693. resultMat[7] = (a02 * a10 - a00 * a12) * idet;
  694. resultMat[2] = t20 * idet;
  695. resultMat[5] = (a01 * a20 - a00 * a21) * idet;
  696. resultMat[8] = (a00 * a11 - a01 * a10) * idet;
  697. return true;
  698. };
  699. /**
  700. * Returns true if the components of mat0 are equal to the components of mat1.
  701. *
  702. * @param {goog.vec.Mat3.AnyType} mat0 The first matrix.
  703. * @param {goog.vec.Mat3.AnyType} mat1 The second matrix.
  704. * @return {boolean} True if the the two matrices are equivalent.
  705. */
  706. goog.vec.Mat3.equals = function(mat0, mat1) {
  707. return mat0.length == mat1.length &&
  708. mat0[0] == mat1[0] && mat0[1] == mat1[1] && mat0[2] == mat1[2] &&
  709. mat0[3] == mat1[3] && mat0[4] == mat1[4] && mat0[5] == mat1[5] &&
  710. mat0[6] == mat1[6] && mat0[7] == mat1[7] && mat0[8] == mat1[8];
  711. };
  712. /**
  713. * Transforms the given vector with the given matrix storing the resulting,
  714. * transformed matrix into resultVec.
  715. *
  716. * @param {!goog.vec.Mat3.AnyType} mat The matrix supplying the transformation.
  717. * @param {goog.vec.Vec3.AnyType} vec The vector to transform.
  718. * @param {goog.vec.Vec3.AnyType} resultVec The vector to
  719. * receive the results (may be vec).
  720. * @return {!goog.vec.Vec3.AnyType} return resultVec so that operations can be
  721. * chained together.
  722. */
  723. goog.vec.Mat3.multVec3 = function(mat, vec, resultVec) {
  724. var x = vec[0], y = vec[1], z = vec[2];
  725. resultVec[0] = x * mat[0] + y * mat[3] + z * mat[6];
  726. resultVec[1] = x * mat[1] + y * mat[4] + z * mat[7];
  727. resultVec[2] = x * mat[2] + y * mat[5] + z * mat[8];
  728. return resultVec;
  729. };
  730. /**
  731. * Makes the given 3x3 matrix a translation matrix with x and y
  732. * translation values.
  733. *
  734. * @param {!goog.vec.Mat3.AnyType} mat The matrix.
  735. * @param {number} x The translation along the x axis.
  736. * @param {number} y The translation along the y axis.
  737. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  738. * chained.
  739. */
  740. goog.vec.Mat3.makeTranslate = function(mat, x, y) {
  741. goog.vec.Mat3.makeIdentity(mat);
  742. goog.vec.Mat3.setColumnValues(mat, 2, x, y, 1);
  743. return mat;
  744. };
  745. /**
  746. * Makes the given 3x3 matrix a scale matrix with x, y, and z scale factors.
  747. *
  748. * @param {!goog.vec.Mat3.AnyType} mat The 3x3 (9-element) matrix
  749. * array to receive the new scale matrix.
  750. * @param {number} x The scale along the x axis.
  751. * @param {number} y The scale along the y axis.
  752. * @param {number} z The scale along the z axis.
  753. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  754. * chained.
  755. */
  756. goog.vec.Mat3.makeScale = function(mat, x, y, z) {
  757. goog.vec.Mat3.makeIdentity(mat);
  758. goog.vec.Mat3.setDiagonalValues(mat, x, y, z);
  759. return mat;
  760. };
  761. /**
  762. * Makes the given 3x3 matrix a rotation matrix with the given rotation
  763. * angle about the axis defined by the vector (ax, ay, az).
  764. *
  765. * @param {!goog.vec.Mat3.AnyType} mat The matrix.
  766. * @param {number} angle The rotation angle in radians.
  767. * @param {number} ax The x component of the rotation axis.
  768. * @param {number} ay The y component of the rotation axis.
  769. * @param {number} az The z component of the rotation axis.
  770. * @return {!goog.vec.Mat3.AnyType} return mat so that operations can be
  771. * chained.
  772. */
  773. goog.vec.Mat3.makeRotate = function(mat, angle, ax, ay, az) {
  774. var c = Math.cos(angle);
  775. var d = 1 - c;
  776. var s = Math.sin(angle);
  777. goog.vec.Mat3.setFromValues(mat,
  778. ax * ax * d + c,
  779. ax * ay * d + az * s,
  780. ax * az * d - ay * s,
  781. ax * ay * d - az * s,
  782. ay * ay * d + c,
  783. ay * az * d + ax * s,
  784. ax * az * d + ay * s,
  785. ay * az * d - ax * s,
  786. az * az * d + c);
  787. return mat;
  788. };