mat3.js 36 KB

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