mat3f_test.js 14 KB

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