mat3_test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. goog.provide('goog.vec.Mat3Test');
  4. goog.setTestOnly('goog.vec.Mat3Test');
  5. goog.require('goog.testing.jsunit');
  6. goog.require('goog.vec.Mat3');
  7. var randomMat3 = goog.vec.Mat3.createFloat32FromValues(
  8. 0.8025078773498535,
  9. 0.7559120655059814,
  10. 0.15274643898010254,
  11. 0.19196106493473053,
  12. 0.0890120416879654,
  13. 0.15422114729881287,
  14. 0.09754583984613419,
  15. 0.44862601161003113,
  16. 0.9196512699127197);
  17. function testDeprecatedConstructor() {
  18. var m0 = goog.vec.Mat3.create();
  19. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0], m0);
  20. var m1 = goog.vec.Mat3.createFromArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  21. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m1);
  22. var m2 = goog.vec.Mat3.createFromArray(m1);
  23. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m1);
  24. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m2);
  25. var m3 = goog.vec.Mat3.createFromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  26. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m3);
  27. var m4 = goog.vec.Mat3.createIdentity();
  28. assertElementsEquals([1, 0, 0, 0, 1, 0, 0, 0, 1], m4);
  29. }
  30. function testConstructor() {
  31. var m0 = goog.vec.Mat3.createFloat32();
  32. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0], m0);
  33. var m1 = goog.vec.Mat3.createFloat32FromArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  34. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m1);
  35. var m2 = goog.vec.Mat3.createFloat32FromArray(m1);
  36. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m1);
  37. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m2);
  38. var m3 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  39. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m3);
  40. var m4 = goog.vec.Mat3.createFloat32Identity();
  41. assertElementsEquals([1, 0, 0, 0, 1, 0, 0, 0, 1], m4);
  42. var n0 = goog.vec.Mat3.createFloat64();
  43. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0], n0);
  44. var n1 = goog.vec.Mat3.createFloat64FromArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  45. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], n1);
  46. var n2 = goog.vec.Mat3.createFloat64FromArray(n1);
  47. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], n1);
  48. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], n2);
  49. var n3 = goog.vec.Mat3.createFloat64FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  50. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], n3);
  51. var n4 = goog.vec.Mat3.createFloat64Identity();
  52. assertElementsEquals([1, 0, 0, 0, 1, 0, 0, 0, 1], n4);
  53. }
  54. function testSet() {
  55. var m0 = goog.vec.Mat3.createFloat32();
  56. var m1 = goog.vec.Mat3.createFromArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  57. goog.vec.Mat3.setFromArray(m0, m1);
  58. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  59. goog.vec.Mat3.setFromValues(m0, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  60. assertElementsEquals([2, 3, 4, 5, 6, 7, 8, 9, 10], m0);
  61. }
  62. function testSetDiagonal() {
  63. var m0 = goog.vec.Mat3.createFloat32();
  64. goog.vec.Mat3.setDiagonalValues(m0, 1, 2, 3);
  65. assertElementsEquals([1, 0, 0, 0, 2, 0, 0, 0, 3], m0);
  66. goog.vec.Mat3.setDiagonal(m0, [4, 5, 6]);
  67. assertElementsEquals([4, 0, 0, 0, 5, 0, 0, 0, 6], m0);
  68. }
  69. function testSetGetColumn() {
  70. var m0 = goog.vec.Mat3.createFloat32();
  71. goog.vec.Mat3.setColumn(m0, 0, [1, 2, 3]);
  72. goog.vec.Mat3.setColumn(m0, 1, [4, 5, 6]);
  73. goog.vec.Mat3.setColumn(m0, 2, [7, 8, 9]);
  74. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  75. var v0 = [0, 0, 0];
  76. goog.vec.Mat3.getColumn(m0, 0, v0);
  77. assertElementsEquals([1, 2, 3], v0);
  78. goog.vec.Mat3.getColumn(m0, 1, v0);
  79. assertElementsEquals([4, 5, 6], v0);
  80. goog.vec.Mat3.getColumn(m0, 2, v0);
  81. assertElementsEquals([7, 8, 9], v0);
  82. }
  83. function testSetGetColumns() {
  84. var m0 = goog.vec.Mat3.createFloat32();
  85. goog.vec.Mat3.setColumns(m0, [1, 2, 3], [4, 5, 6], [7, 8, 9]);
  86. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  87. var v0 = [0, 0, 0], v1 = [0, 0, 0], v2 = [0, 0, 0];
  88. goog.vec.Mat3.getColumns(m0, v0, v1, v2);
  89. assertElementsEquals([1, 2, 3], v0);
  90. assertElementsEquals([4, 5, 6], v1);
  91. assertElementsEquals([7, 8, 9], v2);
  92. }
  93. function testSetGetRow() {
  94. var m0 = goog.vec.Mat3.createFloat32();
  95. goog.vec.Mat3.setRow(m0, 0, [1, 2, 3]);
  96. goog.vec.Mat3.setRow(m0, 1, [4, 5, 6]);
  97. goog.vec.Mat3.setRow(m0, 2, [7, 8, 9]);
  98. assertElementsEquals([1, 4, 7, 2, 5, 8, 3, 6, 9], m0);
  99. var v0 = [0, 0, 0];
  100. goog.vec.Mat3.getRow(m0, 0, v0);
  101. assertElementsEquals([1, 2, 3], v0);
  102. goog.vec.Mat3.getRow(m0, 1, v0);
  103. assertElementsEquals([4, 5, 6], v0);
  104. goog.vec.Mat3.getRow(m0, 2, v0);
  105. assertElementsEquals([7, 8, 9], v0);
  106. }
  107. function testSetGetRows() {
  108. var m0 = goog.vec.Mat3.createFloat32();
  109. goog.vec.Mat3.setRows(m0, [1, 2, 3], [4, 5, 6], [7, 8, 9]);
  110. assertElementsEquals([1, 4, 7, 2, 5, 8, 3, 6, 9], m0);
  111. var v0 = [0, 0, 0], v1 = [0, 0, 0], v2 = [0, 0, 0];
  112. goog.vec.Mat3.getRows(m0, v0, v1, v2);
  113. assertElementsEquals([1, 2, 3], v0);
  114. assertElementsEquals([4, 5, 6], v1);
  115. assertElementsEquals([7, 8, 9], v2);
  116. }
  117. function testSetRowMajorArray() {
  118. var m0 = goog.vec.Mat3.createFloat32();
  119. goog.vec.Mat3.setFromRowMajorArray(
  120. m0, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
  121. assertElementsEquals([1, 4, 7, 2, 5, 8, 3, 6, 9], m0);
  122. }
  123. function testMakeZero() {
  124. var m0 = goog.vec.Mat3.createFromArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
  125. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  126. goog.vec.Mat3.makeZero(m0);
  127. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0], m0);
  128. }
  129. function testMakeIdentity() {
  130. var m0 = goog.vec.Mat3.createFloat32();
  131. goog.vec.Mat3.makeIdentity(m0);
  132. assertElementsEquals([1, 0, 0, 0, 1, 0, 0, 0, 1], m0);
  133. }
  134. function testSetGetElement() {
  135. var m0 = goog.vec.Mat3.createFloat32();
  136. for (var r = 0; r < 3; r++) {
  137. for (var c = 0; c < 3; c++) {
  138. var value = c * 3 + r + 1;
  139. goog.vec.Mat3.setElement(m0, r, c, value);
  140. assertEquals(value, goog.vec.Mat3.getElement(m0, r, c));
  141. }
  142. }
  143. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  144. }
  145. function testAddMat() {
  146. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  147. var m1 = goog.vec.Mat3.createFloat32FromValues(3, 4, 5, 6, 7, 8, 9, 1, 2);
  148. var m2 = goog.vec.Mat3.create();
  149. goog.vec.Mat3.addMat(m0, m1, m2);
  150. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  151. assertElementsEquals([3, 4, 5, 6, 7, 8, 9, 1, 2], m1);
  152. assertElementsEquals([4, 6, 8, 10, 12, 14, 16, 9, 11], m2);
  153. goog.vec.Mat3.addMat(m0, m1, m0);
  154. assertElementsEquals([3, 4, 5, 6, 7, 8, 9, 1, 2], m1);
  155. assertElementsEquals([4, 6, 8, 10, 12, 14, 16, 9, 11], m0);
  156. }
  157. function testSubMat() {
  158. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  159. var m1 = goog.vec.Mat3.createFloat32FromValues(3, 4, 5, 6, 7, 8, 9, 1, 2);
  160. var m2 = goog.vec.Mat3.create();
  161. goog.vec.Mat3.subMat(m0, m1, m2);
  162. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  163. assertElementsEquals([3, 4, 5, 6, 7, 8, 9, 1, 2], m1);
  164. assertElementsEquals([-2, -2, -2, -2, -2, -2, -2, 7, 7], m2);
  165. goog.vec.Mat3.subMat(m1, m0, m1);
  166. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  167. assertElementsEquals([2, 2, 2, 2, 2, 2, 2, -7, -7], m1);
  168. }
  169. function testMultScalar() {
  170. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  171. var m1 = goog.vec.Mat3.createFloat32();
  172. goog.vec.Mat3.multScalar(m0, 5, m1);
  173. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m0);
  174. assertElementsEquals([5, 10, 15, 20, 25, 30, 35, 40, 45], m1);
  175. goog.vec.Mat3.multScalar(m0, 5, m0);
  176. assertElementsEquals([5, 10, 15, 20, 25, 30, 35, 40, 45], m0);
  177. }
  178. function testMultMat() {
  179. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  180. var m1 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  181. var m2 = goog.vec.Mat3.create();
  182. goog.vec.Mat3.multMat(m0, m1, m2);
  183. assertElementsEquals([30, 36, 42, 66, 81, 96, 102, 126, 150], m2);
  184. goog.vec.Mat3.addMat(m0, m1, m1);
  185. goog.vec.Mat3.multMat(m0, m1, m1);
  186. assertElementsEquals([60, 72, 84, 132, 162, 192, 204, 252, 300], m1);
  187. }
  188. function testTranspose() {
  189. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  190. var m1 = goog.vec.Mat3.createFloat32();
  191. goog.vec.Mat3.transpose(m0, m1);
  192. assertElementsEquals([1, 4, 7, 2, 5, 8, 3, 6, 9], m1);
  193. goog.vec.Mat3.transpose(m1, m1);
  194. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], m1);
  195. }
  196. function testInvert() {
  197. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 1, 1, 1, 1, 1, 1, 1, 1);
  198. assertFalse(goog.vec.Mat3.invert(m0, m0));
  199. assertElementsEquals([1, 1, 1, 1, 1, 1, 1, 1, 1], m0);
  200. goog.vec.Mat3.setFromValues(m0, 1, 2, 3, 1, 3, 4, 3, 4, 5);
  201. assertTrue(goog.vec.Mat3.invert(m0, m0));
  202. assertElementsEquals([0.5, -1.0, 0.5, -3.5, 2.0, 0.5, 2.5, -1.0, -0.5], m0);
  203. goog.vec.Mat3.makeScale(m0, .01, .01, .01);
  204. assertTrue(goog.vec.Mat3.invert(m0, m0));
  205. var m1 = goog.vec.Mat3.create();
  206. goog.vec.Mat3.makeScale(m1, 100, 100, 100);
  207. assertElementsEquals(m1, m0);
  208. }
  209. function testEquals() {
  210. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  211. var m1 = goog.vec.Mat3.createFromArray(m0);
  212. assertTrue(goog.vec.Mat3.equals(m0, m1));
  213. assertTrue(goog.vec.Mat3.equals(m1, m0));
  214. for (var i = 0; i < 9; i++) {
  215. m1[i] = 15;
  216. assertFalse(goog.vec.Mat3.equals(m0, m1));
  217. assertFalse(goog.vec.Mat3.equals(m1, m0));
  218. m1[i] = i + 1;
  219. }
  220. }
  221. function testMultVec3() {
  222. var m0 = goog.vec.Mat3.createFloat32FromValues(1, 2, 3, 4, 5, 6, 7, 8, 9);
  223. var v0 = [1, 2, 3];
  224. var v1 = [0, 0, 0];
  225. goog.vec.Mat3.multVec3(m0, v0, v1);
  226. assertElementsEquals([30, 36, 42], v1);
  227. goog.vec.Mat3.multVec3(m0, v0, v0);
  228. assertElementsEquals([30, 36, 42], v0);
  229. }
  230. function testSetValues() {
  231. var a0 = goog.vec.Mat3.createFloat32();
  232. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0], a0);
  233. goog.vec.Mat3.setFromValues(a0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
  234. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a0);
  235. var a1 = goog.vec.Mat3.createFloat32();
  236. goog.vec.Mat3.setDiagonalValues(a1, 1, 2, 3);
  237. assertElementsEquals([1, 0, 0, 0, 2, 0, 0, 0, 3], a1);
  238. goog.vec.Mat3.setColumnValues(a1, 0, 2, 3, 4);
  239. goog.vec.Mat3.setColumnValues(a1, 1, 5, 6, 7);
  240. goog.vec.Mat3.setColumnValues(a1, 2, 8, 9, 1);
  241. assertElementsEquals([2, 3, 4, 5, 6, 7, 8, 9, 1], a1);
  242. goog.vec.Mat3.setRowValues(a1, 0, 1, 4, 7);
  243. goog.vec.Mat3.setRowValues(a1, 1, 2, 5, 8);
  244. goog.vec.Mat3.setRowValues(a1, 2, 3, 6, 9);
  245. assertElementsEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a1);
  246. }
  247. function testMakeTranslate() {
  248. var m0 = goog.vec.Mat3.createFloat32();
  249. goog.vec.Mat3.makeTranslate(m0, 3, 4);
  250. assertElementsEquals([1, 0, 0, 0, 1, 0, 3, 4, 1], m0);
  251. }
  252. function testMakeScale() {
  253. var m0 = goog.vec.Mat3.createFloat32();
  254. goog.vec.Mat3.makeScale(m0, 3, 4, 5);
  255. assertElementsEquals([3, 0, 0, 0, 4, 0, 0, 0, 5], m0);
  256. }
  257. function testMakeRotate() {
  258. var m0 = goog.vec.Mat3.createFloat32();
  259. goog.vec.Mat3.makeRotate(m0, Math.PI / 2, 0, 0, 1);
  260. var v0 = [0, 1, 0, -1, 0, 0, 0, 0, 1];
  261. assertElementsRoughlyEqual(m0, v0, goog.vec.EPSILON);
  262. var m1 = goog.vec.Mat3.createFloat32();
  263. goog.vec.Mat3.makeRotate(m1, -Math.PI / 4, 0, 0, 1);
  264. goog.vec.Mat3.multMat(m0, m1, m1);
  265. var v1 = [0.7071068, 0.7071068, 0, -0.7071068, 0.7071068, 0, 0, 0, 1];
  266. assertElementsRoughlyEqual(m1, v1, goog.vec.EPSILON);
  267. }
  268. function testMakeRotateX() {
  269. var m0 = goog.vec.Mat3.createFloat32();
  270. var m1 = goog.vec.Mat3.createFloat32();
  271. goog.vec.Mat3.makeRotateX(m0, Math.PI / 7);
  272. goog.vec.Mat3.makeRotate(m1, Math.PI / 7, 1, 0, 0);
  273. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  274. }
  275. function testMakeRotateY() {
  276. var m0 = goog.vec.Mat3.createFloat32();
  277. var m1 = goog.vec.Mat3.createFloat32();
  278. goog.vec.Mat3.makeRotateY(m0, Math.PI / 7);
  279. goog.vec.Mat3.makeRotate(m1, Math.PI / 7, 0, 1, 0);
  280. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  281. }
  282. function testMakeRotateZ() {
  283. var m0 = goog.vec.Mat3.createFloat32();
  284. var m1 = goog.vec.Mat3.createFloat32();
  285. goog.vec.Mat3.makeRotateZ(m0, Math.PI / 7);
  286. goog.vec.Mat3.makeRotate(m1, Math.PI / 7, 0, 0, 1);
  287. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  288. }
  289. function testRotate() {
  290. var m0 = goog.vec.Mat3.createIdentity();
  291. goog.vec.Mat3.rotate(m0, Math.PI / 2, 0, 0, 1);
  292. assertElementsRoughlyEqual(
  293. [0, 1, 0, -1, 0, 0, 0, 0, 1],
  294. m0, goog.vec.EPSILON);
  295. goog.vec.Mat3.rotate(m0, -Math.PI / 4, 0, 0, 1);
  296. assertElementsRoughlyEqual(
  297. [0.7071068, 0.7071068, 0,
  298. -0.7071068, 0.7071068, 0,
  299. 0, 0, 1],
  300. m0, goog.vec.EPSILON);
  301. }
  302. function testRotateX() {
  303. var m0 = goog.vec.Mat3.createFloat32();
  304. var m1 = goog.vec.Mat3.createFloat32FromArray(randomMat3);
  305. goog.vec.Mat3.makeRotateX(m0, Math.PI / 7);
  306. goog.vec.Mat3.multMat(m1, m0, m0);
  307. goog.vec.Mat3.rotateX(m1, Math.PI / 7);
  308. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  309. }
  310. function testRotateY() {
  311. var m0 = goog.vec.Mat3.createFloat32();
  312. var m1 = goog.vec.Mat3.createFloat32FromArray(randomMat3);
  313. goog.vec.Mat3.makeRotateY(m0, Math.PI / 7);
  314. goog.vec.Mat3.multMat(m1, m0, m0);
  315. goog.vec.Mat3.rotateY(m1, Math.PI / 7);
  316. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  317. }
  318. function testRotateZ() {
  319. var m0 = goog.vec.Mat3.createFloat32();
  320. var m1 = goog.vec.Mat3.createFloat32FromArray(randomMat3);
  321. goog.vec.Mat3.makeRotateZ(m0, Math.PI / 7);
  322. goog.vec.Mat3.multMat(m1, m0, m0);
  323. goog.vec.Mat3.rotateZ(m1, Math.PI / 7);
  324. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  325. }
  326. function testMakeEulerZXZ() {
  327. var m0 = goog.vec.Mat3.createFloat32();
  328. var roll = 0.200982 * 2 * Math.PI;
  329. var tilt = 0.915833 * Math.PI;
  330. var yaw = 0.839392 * 2 * Math.PI;
  331. goog.vec.Mat3.makeRotate(m0, roll, 0, 0, 1);
  332. goog.vec.Mat3.rotate(m0, tilt, 1, 0, 0);
  333. goog.vec.Mat3.rotate(m0, yaw, 0, 0, 1);
  334. var m1 = goog.vec.Mat3.createFloat32();
  335. goog.vec.Mat3.makeEulerZXZ(m1, roll, tilt, yaw);
  336. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  337. var euler = [0, 0, 0];
  338. goog.vec.Mat3.toEulerZXZ(m0, euler);
  339. assertRoughlyEquals(roll, euler[0], goog.vec.EPSILON);
  340. assertRoughlyEquals(tilt, euler[1], goog.vec.EPSILON);
  341. assertRoughlyEquals(yaw, euler[2], goog.vec.EPSILON);
  342. // Test negative tilt now.
  343. goog.vec.Mat3.makeRotate(m0, roll, 0, 0, 1);
  344. goog.vec.Mat3.rotate(m0, -tilt, 1, 0, 0);
  345. goog.vec.Mat3.rotate(m0, yaw, 0, 0, 1);
  346. goog.vec.Mat3.makeEulerZXZ(m1, roll, -tilt, yaw);
  347. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  348. var euler = [0, 0, 0];
  349. goog.vec.Mat3.toEulerZXZ(m0, euler, true);
  350. assertRoughlyEquals(roll, euler[0], goog.vec.EPSILON);
  351. assertRoughlyEquals(-tilt, euler[1], goog.vec.EPSILON);
  352. assertRoughlyEquals(yaw, euler[2], goog.vec.EPSILON);
  353. }
  354. function testEulerZXZExtrema() {
  355. var m0 = goog.vec.Mat3.createFloat32FromArray(
  356. [1, 0, 0, 0, 0, -1, 0, 1, 0]);
  357. var m1 = goog.vec.Mat3.createFloat32FromArray(
  358. [0, 0, 0, 0, 0, 0, 0, 0, 0]);
  359. var euler = [0, 0, 0];
  360. goog.vec.Mat3.toEulerZXZ(m0, euler);
  361. assertElementsRoughlyEqual(
  362. [Math.PI, Math.PI / 2, Math.PI], euler, goog.vec.EPSILON);
  363. goog.vec.Mat3.makeEulerZXZ(m1, euler[0], euler[1], euler[2]);
  364. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  365. }