mat3.js 36 KB

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