mat4_test.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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.Mat4Test');
  4. goog.setTestOnly('goog.vec.Mat4Test');
  5. goog.require('goog.vec.Mat4');
  6. goog.require('goog.testing.jsunit');
  7. var randomMat4 = goog.vec.Mat4.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. 0.5310639142990112,
  18. 0.8962187170982361,
  19. 0.280601441860199,
  20. 0.594650387763977,
  21. 0.4134795069694519,
  22. 0.06632178276777267,
  23. 0.8837796449661255);
  24. function testDeprecatedConstructor() {
  25. var m0 = goog.vec.Mat4.create();
  26. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], m0);
  27. var m1 = goog.vec.Mat4.createFromArray(
  28. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  29. assertElementsEquals(
  30. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m1);
  31. var m2 = goog.vec.Mat4.clone(m1);
  32. assertElementsEquals(
  33. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m1);
  34. assertElementsEquals(
  35. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m2);
  36. var m3 = goog.vec.Mat4.createFromValues(
  37. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  38. assertElementsEquals(
  39. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m3);
  40. var m4 = goog.vec.Mat4.createIdentity();
  41. assertElementsEquals([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], m4);
  42. }
  43. function testConstructor() {
  44. var m0 = goog.vec.Mat4.createFloat32();
  45. assertElementsEquals([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], m0);
  46. var m1 = goog.vec.Mat4.createFloat32FromArray(
  47. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  48. assertElementsEquals(
  49. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m1);
  50. var m2 = goog.vec.Mat4.clone(m1);
  51. assertElementsEquals(
  52. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m1);
  53. assertElementsEquals(
  54. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m2);
  55. var m3 = goog.vec.Mat4.createFloat32FromValues(
  56. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  57. assertElementsEquals(
  58. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m3);
  59. var m4 = goog.vec.Mat4.createIdentity();
  60. assertElementsEquals([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], m4);
  61. }
  62. function testSet() {
  63. var m0 = goog.vec.Mat4.createFloat32();
  64. var m1 = goog.vec.Mat4.createFloat32FromArray(
  65. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  66. goog.vec.Mat4.setFromArray(m0, m1);
  67. assertElementsEquals(
  68. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  69. goog.vec.Mat4.setFromValues(
  70. m0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17);
  71. assertElementsEquals(
  72. [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], m0);
  73. }
  74. function testSetDiagonal() {
  75. var m0 = goog.vec.Mat4.createFloat32();
  76. goog.vec.Mat4.setDiagonalValues(m0, 1, 2, 3, 4);
  77. assertElementsEquals(
  78. [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], m0);
  79. goog.vec.Mat4.setDiagonal(m0, [4, 5, 6, 7]);
  80. assertElementsEquals(
  81. [4, 0, 0, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 0, 0, 7], m0);
  82. }
  83. function testGetDiagonal() {
  84. var v0 = goog.vec.Vec4.create();
  85. var m0 = goog.vec.Mat4.createFloat32();
  86. goog.vec.Mat4.setFromArray(
  87. m0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
  88. goog.vec.Mat4.getDiagonal(m0, v0);
  89. assertElementsEquals([0, 5, 10, 15], v0);
  90. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  91. goog.vec.Mat4.getDiagonal(m0, v0, 1);
  92. assertElementsEquals([4, 9, 14, 0], v0);
  93. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  94. goog.vec.Mat4.getDiagonal(m0, v0, 2);
  95. assertElementsEquals([8, 13, 0, 0], v0);
  96. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  97. goog.vec.Mat4.getDiagonal(m0, v0, 3);
  98. assertElementsEquals([12, 0, 0, 0], v0);
  99. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  100. goog.vec.Mat4.getDiagonal(m0, v0, 4);
  101. assertElementsEquals([0, 0, 0, 0], v0);
  102. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  103. goog.vec.Mat4.getDiagonal(m0, v0, -1);
  104. assertElementsEquals([1, 6, 11, 0], v0);
  105. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  106. goog.vec.Mat4.getDiagonal(m0, v0, -2);
  107. assertElementsEquals([2, 7, 0, 0], v0);
  108. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  109. goog.vec.Mat4.getDiagonal(m0, v0, -3);
  110. assertElementsEquals([3, 0, 0, 0], v0);
  111. goog.vec.Vec4.setFromArray(v0, [0, 0, 0, 0]);
  112. goog.vec.Mat4.getDiagonal(m0, v0, -4);
  113. assertElementsEquals([0, 0, 0, 0], v0);
  114. }
  115. function testSetGetColumn() {
  116. var m0 = goog.vec.Mat4.createFloat32();
  117. goog.vec.Mat4.setColumn(m0, 0, [1, 2, 3, 4]);
  118. goog.vec.Mat4.setColumn(m0, 1, [5, 6, 7, 8]);
  119. goog.vec.Mat4.setColumn(m0, 2, [9, 10, 11, 12]);
  120. goog.vec.Mat4.setColumn(m0, 3, [13, 14, 15, 16]);
  121. assertElementsEquals(
  122. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  123. var v0 = [0, 0, 0, 0];
  124. goog.vec.Mat4.getColumn(m0, 0, v0);
  125. assertElementsEquals([1, 2, 3, 4], v0);
  126. goog.vec.Mat4.getColumn(m0, 1, v0);
  127. assertElementsEquals([5, 6, 7, 8], v0);
  128. goog.vec.Mat4.getColumn(m0, 2, v0);
  129. assertElementsEquals([9, 10, 11, 12], v0);
  130. goog.vec.Mat4.getColumn(m0, 3, v0);
  131. assertElementsEquals([13, 14, 15, 16], v0);
  132. }
  133. function testSetGetColumns() {
  134. var m0 = goog.vec.Mat4.createFloat32();
  135. goog.vec.Mat4.setColumns(
  136. m0, [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]);
  137. assertElementsEquals(
  138. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  139. var v0 = [0, 0, 0, 0], v1 = [0, 0, 0, 0];
  140. var v2 = [0, 0, 0, 0], v3 = [0, 0, 0, 0];
  141. goog.vec.Mat4.getColumns(m0, v0, v1, v2, v3);
  142. assertElementsEquals([1, 2, 3, 4], v0);
  143. assertElementsEquals([5, 6, 7, 8], v1);
  144. assertElementsEquals([9, 10, 11, 12], v2);
  145. assertElementsEquals([13, 14, 15, 16], v3);
  146. }
  147. function testSetGetRow() {
  148. var m0 = goog.vec.Mat4.createFloat32();
  149. goog.vec.Mat4.setRow(m0, 0, [1, 2, 3, 4]);
  150. goog.vec.Mat4.setRow(m0, 1, [5, 6, 7, 8]);
  151. goog.vec.Mat4.setRow(m0, 2, [9, 10, 11, 12]);
  152. goog.vec.Mat4.setRow(m0, 3, [13, 14, 15, 16]);
  153. assertElementsEquals(
  154. [1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16], m0);
  155. var v0 = [0, 0, 0, 0];
  156. goog.vec.Mat4.getRow(m0, 0, v0);
  157. assertElementsEquals([1, 2, 3, 4], v0);
  158. goog.vec.Mat4.getRow(m0, 1, v0);
  159. assertElementsEquals([5, 6, 7, 8], v0);
  160. goog.vec.Mat4.getRow(m0, 2, v0);
  161. assertElementsEquals([9, 10, 11, 12], v0);
  162. goog.vec.Mat4.getRow(m0, 3, v0);
  163. assertElementsEquals([13, 14, 15, 16], v0);
  164. }
  165. function testSetGetRows() {
  166. var m0 = goog.vec.Mat4.createFloat32();
  167. goog.vec.Mat4.setRows(
  168. m0, [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]);
  169. assertElementsEquals(
  170. [1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16], m0);
  171. var v0 = [0, 0, 0, 0], v1 = [0, 0, 0, 0];
  172. var v2 = [0, 0, 0, 0], v3 = [0, 0, 0, 0];
  173. goog.vec.Mat4.getRows(m0, v0, v1, v2, v3);
  174. assertElementsEquals([1, 2, 3, 4], v0);
  175. assertElementsEquals([5, 6, 7, 8], v1);
  176. assertElementsEquals([9, 10, 11, 12], v2);
  177. assertElementsEquals([13, 14, 15, 16], v3);
  178. }
  179. function testSetRowMajorArray() {
  180. var m0 = goog.vec.Mat4.createFloat32();
  181. goog.vec.Mat4.setFromRowMajorArray(
  182. m0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  183. assertElementsEquals(
  184. [1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16], m0);
  185. }
  186. function testMakeZero() {
  187. var m0 = goog.vec.Mat4.createFloat32FromArray(
  188. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  189. assertElementsEquals(
  190. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  191. goog.vec.Mat4.makeZero(m0);
  192. assertElementsEquals(
  193. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], m0);
  194. }
  195. function testMakeIdentity() {
  196. var m0 = goog.vec.Mat4.createFloat32();
  197. goog.vec.Mat4.makeIdentity(m0);
  198. assertElementsEquals(
  199. [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], m0);
  200. }
  201. function testSetGetElement() {
  202. var m0 = goog.vec.Mat4.createFloat32();
  203. for (var r = 0; r < 4; r++) {
  204. for (var c = 0; c < 4; c++) {
  205. var value = c * 4 + r + 1;
  206. goog.vec.Mat4.setElement(m0, r, c, value);
  207. assertEquals(value, goog.vec.Mat4.getElement(m0, r, c));
  208. }
  209. }
  210. assertElementsEquals(
  211. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  212. }
  213. function testAddMat() {
  214. var m0 = goog.vec.Mat4.createFloat32FromValues(
  215. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  216. var m1 = goog.vec.Mat4.createFloat32FromValues(
  217. 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8);
  218. var m2 = goog.vec.Mat4.createFloat32();
  219. goog.vec.Mat4.addMat(m0, m1, m2);
  220. assertElementsEquals(
  221. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  222. assertElementsEquals(
  223. [9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8], m1);
  224. assertElementsEquals(
  225. [10, 12, 14, 16, 18, 20, 22, 24, 10, 12, 14, 16, 18, 20, 22, 24], m2);
  226. goog.vec.Mat4.addMat(m0, m1, m0);
  227. assertElementsEquals(
  228. [9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8], m1);
  229. assertElementsEquals(
  230. [10, 12, 14, 16, 18, 20, 22, 24, 10, 12, 14, 16, 18, 20, 22, 24], m0);
  231. }
  232. function testSubMat() {
  233. var m0 = goog.vec.Mat4.createFloat32FromValues(
  234. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  235. var m1 = goog.vec.Mat4.createFloat32FromValues(
  236. 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8);
  237. var m2 = goog.vec.Mat4.createFloat32();
  238. goog.vec.Mat4.subMat(m0, m1, m2);
  239. assertElementsEquals(
  240. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  241. assertElementsEquals(
  242. [9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8], m1);
  243. assertElementsEquals(
  244. [-8, -8, -8, -8, -8, -8, -8, -8, 8, 8, 8, 8, 8, 8, 8, 8], m2);
  245. goog.vec.Mat4.subMat(m1, m0, m1);
  246. assertElementsEquals(
  247. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  248. assertElementsEquals(
  249. [8, 8, 8, 8, 8, 8, 8, 8, -8, -8, -8, -8, -8, -8, -8, -8], m1);
  250. }
  251. function testMultScalar() {
  252. var m0 = goog.vec.Mat4.createFloat32FromValues(
  253. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  254. var m1 = goog.vec.Mat4.createFloat32();
  255. goog.vec.Mat4.multScalar(m0, 2, m1);
  256. assertElementsEquals(
  257. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m0);
  258. assertElementsEquals(
  259. [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32], m1);
  260. goog.vec.Mat4.multScalar(m0, 5, m0);
  261. assertElementsEquals(
  262. [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80], m0);
  263. }
  264. function testMultMat() {
  265. var m0 = goog.vec.Mat4.createFloat32FromValues(
  266. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  267. var m1 = goog.vec.Mat4.createFloat32FromValues(
  268. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  269. var m2 = goog.vec.Mat4.createFloat32();
  270. goog.vec.Mat4.multMat(m0, m1, m2);
  271. assertElementsEquals(
  272. [90, 100, 110, 120, 202, 228, 254, 280,
  273. 314, 356, 398, 440, 426, 484, 542, 600], m2);
  274. goog.vec.Mat4.multScalar(m1, 2, m1);
  275. goog.vec.Mat4.multMat(m1, m0, m1);
  276. assertElementsEquals(
  277. [180, 200, 220, 240, 404, 456, 508, 560,
  278. 628, 712, 796, 880, 852, 968, 1084, 1200], m1);
  279. }
  280. function testTranspose() {
  281. var m0 = goog.vec.Mat4.createFloat32FromValues(
  282. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  283. var m1 = goog.vec.Mat4.createFloat32();
  284. goog.vec.Mat4.transpose(m0, m1);
  285. assertElementsEquals(
  286. [1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16], m1);
  287. goog.vec.Mat4.transpose(m1, m1);
  288. assertElementsEquals(
  289. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], m1);
  290. }
  291. function testDeterminant() {
  292. var m0 = goog.vec.Mat4.createFloat32FromValues(
  293. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  294. assertEquals(0, goog.vec.Mat4.determinant(m0));
  295. assertElementsEquals(
  296. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], m0);
  297. goog.vec.Mat4.setFromValues(
  298. m0, 1, 2, 3, 4, 2, 3, 4, 1, 3, 4, 1, 2, 4, 1, 2, 3);
  299. assertEquals(160, goog.vec.Mat4.determinant(m0));
  300. assertElementsEquals(
  301. [1, 2, 3, 4, 2, 3, 4, 1, 3, 4, 1, 2, 4, 1, 2, 3], m0);
  302. }
  303. function testInvert() {
  304. var m0 = goog.vec.Mat4.createFloat32FromValues(
  305. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
  306. assertFalse(goog.vec.Mat4.invert(m0, m0));
  307. assertElementsEquals(
  308. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], m0);
  309. goog.vec.Mat4.setFromValues(
  310. m0, 1, 2, 3, 4, 2, 3, 4, 1, 3, 4, 1, 2, 4, 1, 2, 3);
  311. assertTrue(goog.vec.Mat4.invert(m0, m0));
  312. assertElementsRoughlyEqual(
  313. [-0.225, 0.025, 0.025, 0.275, 0.025, 0.025, 0.275, -0.225,
  314. 0.025, 0.275, -0.225, 0.025, 0.275, -0.225, 0.025, 0.025], m0,
  315. goog.vec.EPSILON);
  316. goog.vec.Mat4.makeScale(m0, .01, .01, .01);
  317. assertTrue(goog.vec.Mat4.invert(m0, m0));
  318. var m1 = goog.vec.Mat4.createFloat32();
  319. goog.vec.Mat4.makeScale(m1, 100, 100, 100);
  320. assertElementsEquals(m1, m0);
  321. }
  322. function testEquals() {
  323. var m0 = goog.vec.Mat4.createFloat32FromValues(
  324. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  325. var m1 = goog.vec.Mat4.clone(m0);
  326. assertTrue(goog.vec.Mat4.equals(m0, m1));
  327. assertTrue(goog.vec.Mat4.equals(m1, m0));
  328. for (var i = 0; i < 16; i++) {
  329. m1[i] = 18;
  330. assertFalse(goog.vec.Mat4.equals(m0, m1));
  331. assertFalse(goog.vec.Mat4.equals(m1, m0));
  332. m1[i] = i + 1;
  333. }
  334. }
  335. function testMultVec3() {
  336. var m0 = goog.vec.Mat4.createFloat32FromValues(
  337. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  338. var v0 = [1, 2, 3];
  339. var v1 = [0, 0, 0];
  340. goog.vec.Mat4.multVec3(m0, v0, v1);
  341. assertElementsEquals([1, 2, 3], v0);
  342. assertElementsEquals([51, 58, 65], v1);
  343. goog.vec.Mat4.multVec3(m0, v0, v0);
  344. assertElementsEquals([51, 58, 65], v0);
  345. }
  346. function testMultVec3NoTranslate() {
  347. var m0 = goog.vec.Mat4.createFloat32FromValues(
  348. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  349. var v0 = [1, 2, 3];
  350. var v1 = [0, 0, 0];
  351. goog.vec.Mat4.multVec3NoTranslate(m0, v0, v1);
  352. assertElementsEquals([1, 2, 3], v0);
  353. assertElementsEquals([38, 44, 50], v1);
  354. goog.vec.Mat4.multVec3NoTranslate(m0, v0, v0);
  355. assertElementsEquals([38, 44, 50], v0);
  356. }
  357. function testMultVec3Projective() {
  358. var m0 = goog.vec.Mat4.createFloat32FromValues(
  359. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  360. var v0 = [1, 2, 3];
  361. var v1 = [0, 0, 0];
  362. var invw = 1 / 72;
  363. goog.vec.Mat4.multVec3Projective(m0, v0, v1);
  364. assertElementsEquals([1, 2, 3], v0);
  365. assertElementsEquals(
  366. [51 * invw, 58 * invw, 65 * invw], v1);
  367. goog.vec.Mat4.multVec3Projective(m0, v0, v0);
  368. assertElementsEquals(
  369. [51 * invw, 58 * invw, 65 * invw], v0);
  370. }
  371. function testMultVec4() {
  372. var m0 = goog.vec.Mat4.createFloat32FromValues(
  373. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  374. var v0 = [1, 2, 3, 4];
  375. var v1 = [0, 0, 0, 0];
  376. goog.vec.Mat4.multVec4(m0, v0, v1);
  377. assertElementsEquals([90, 100, 110, 120], v1);
  378. goog.vec.Mat4.multVec4(m0, v0, v0);
  379. assertElementsEquals([90, 100, 110, 120], v0);
  380. }
  381. function testSetValues() {
  382. var a0 = goog.vec.Mat4.createFloat32();
  383. assertElementsEquals(
  384. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], a0);
  385. a0 = goog.vec.Mat4.createFloat32FromArray(
  386. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
  387. assertElementsEquals(
  388. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], a0);
  389. var a1 = goog.vec.Mat4.createFloat32();
  390. goog.vec.Mat4.setDiagonalValues(a1, 1, 2, 3, 4);
  391. assertElementsEquals(
  392. [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 4], a1);
  393. goog.vec.Mat4.setColumnValues(a1, 0, 2, 3, 4, 5);
  394. goog.vec.Mat4.setColumnValues(a1, 1, 6, 7, 8, 9);
  395. goog.vec.Mat4.setColumnValues(a1, 2, 10, 11, 12, 13);
  396. goog.vec.Mat4.setColumnValues(a1, 3, 14, 15, 16, 1);
  397. assertElementsEquals(
  398. [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1], a1);
  399. goog.vec.Mat4.setRowValues(a1, 0, 1, 5, 9, 13);
  400. goog.vec.Mat4.setRowValues(a1, 1, 2, 6, 10, 14);
  401. goog.vec.Mat4.setRowValues(a1, 2, 3, 7, 11, 15);
  402. goog.vec.Mat4.setRowValues(a1, 3, 4, 8, 12, 16);
  403. assertElementsEquals(
  404. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], a1);
  405. }
  406. function testMakeTranslate() {
  407. var m0 = goog.vec.Mat4.createFloat32();
  408. goog.vec.Mat4.makeTranslate(m0, 3, 4, 5);
  409. assertElementsEquals(
  410. [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3, 4, 5, 1], m0);
  411. }
  412. function testMakeScale() {
  413. var m0 = goog.vec.Mat4.createFloat32();
  414. goog.vec.Mat4.makeScale(m0, 3, 4, 5);
  415. assertElementsEquals(
  416. [3, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1], m0);
  417. }
  418. function testMakeRotate() {
  419. var m0 = goog.vec.Mat4.createFloat32();
  420. goog.vec.Mat4.makeRotate(m0, Math.PI / 2, 0, 0, 1);
  421. assertElementsRoughlyEqual(
  422. [0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
  423. m0, goog.vec.EPSILON);
  424. var m1 = goog.vec.Mat4.createFloat32();
  425. goog.vec.Mat4.makeRotate(m1, -Math.PI / 4, 0, 0, 1);
  426. goog.vec.Mat4.multMat(m0, m1, m1);
  427. assertElementsRoughlyEqual(
  428. [0.7071068, 0.7071068, 0, 0,
  429. -0.7071068, 0.7071068, 0, 0,
  430. 0, 0, 1, 0,
  431. 0, 0, 0, 1],
  432. m1, goog.vec.EPSILON);
  433. }
  434. function testMakeRotateX() {
  435. var m0 = goog.vec.Mat4.createFloat32();
  436. var m1 = goog.vec.Mat4.createFloat32()
  437. goog.vec.Mat4.makeRotateX(m0, Math.PI / 7);
  438. goog.vec.Mat4.makeRotate(m1, Math.PI / 7, 1, 0, 0);
  439. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  440. }
  441. function testMakeRotateY() {
  442. var m0 = goog.vec.Mat4.createFloat32();
  443. var m1 = goog.vec.Mat4.createFloat32()
  444. goog.vec.Mat4.makeRotateY(m0, Math.PI / 7);
  445. goog.vec.Mat4.makeRotate(m1, Math.PI / 7, 0, 1, 0);
  446. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  447. }
  448. function testMakeRotateZ() {
  449. var m0 = goog.vec.Mat4.createFloat32();
  450. var m1 = goog.vec.Mat4.createFloat32()
  451. goog.vec.Mat4.makeRotateZ(m0, Math.PI / 7);
  452. goog.vec.Mat4.makeRotate(m1, Math.PI / 7, 0, 0, 1);
  453. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  454. }
  455. function testTranslate() {
  456. var m0 = goog.vec.Mat4.createIdentity();
  457. goog.vec.Mat4.translate(m0, 3, 4, 5);
  458. assertElementsEquals(
  459. [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3, 4, 5, 1], m0);
  460. goog.vec.Mat4.setFromValues(
  461. m0, 1, 2, 3, 4, 2, 3, 4, 1, 3, 4, 1, 2, 4, 1, 2, 3);
  462. var m1 = goog.vec.Mat4.createFloat32();
  463. goog.vec.Mat4.makeTranslate(m1, 5, 6, 7);
  464. var m2 = goog.vec.Mat4.createFloat32();
  465. goog.vec.Mat4.multMat(m0, m1, m2);
  466. goog.vec.Mat4.translate(m0, 5, 6, 7);
  467. assertElementsEquals(m2, m0);
  468. }
  469. function testScale() {
  470. var m0 = goog.vec.Mat4.createIdentity();
  471. goog.vec.Mat4.scale(m0, 3, 4, 5);
  472. assertElementsEquals([3, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1], m0);
  473. }
  474. function testRotate() {
  475. var m0 = goog.vec.Mat4.createIdentity();
  476. goog.vec.Mat4.rotate(m0, Math.PI / 2, 0, 0, 1);
  477. assertElementsRoughlyEqual(
  478. [0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
  479. m0, goog.vec.EPSILON);
  480. goog.vec.Mat4.rotate(m0, -Math.PI / 4, 0, 0, 1);
  481. assertElementsRoughlyEqual(
  482. [0.7071068, 0.7071068, 0, 0,
  483. -0.7071068, 0.7071068, 0, 0,
  484. 0, 0, 1, 0,
  485. 0, 0, 0, 1],
  486. m0, goog.vec.EPSILON);
  487. }
  488. function testRotateX() {
  489. var m0 = goog.vec.Mat4.createFloat32();
  490. var m1 = goog.vec.Mat4.createFloat32FromArray(randomMat4)
  491. goog.vec.Mat4.makeRotateX(m0, Math.PI / 7);
  492. goog.vec.Mat4.multMat(m1, m0, m0);
  493. goog.vec.Mat4.rotateX(m1, Math.PI / 7);
  494. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  495. }
  496. function testRotateY() {
  497. var m0 = goog.vec.Mat4.createFloat32();
  498. var m1 = goog.vec.Mat4.createFloat32FromArray(randomMat4)
  499. goog.vec.Mat4.makeRotateY(m0, Math.PI / 7);
  500. goog.vec.Mat4.multMat(m1, m0, m0);
  501. goog.vec.Mat4.rotateY(m1, Math.PI / 7);
  502. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  503. }
  504. function testRotateZ() {
  505. var m0 = goog.vec.Mat4.createFloat32();
  506. var m1 = goog.vec.Mat4.createFloat32FromArray(randomMat4)
  507. goog.vec.Mat4.makeRotateZ(m0, Math.PI / 7);
  508. goog.vec.Mat4.multMat(m1, m0, m0);
  509. goog.vec.Mat4.rotateZ(m1, Math.PI / 7);
  510. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  511. }
  512. function testGetTranslation() {
  513. var mat = goog.vec.Mat4.createFloat32FromArray(randomMat4);
  514. var translation = goog.vec.Vec3.createFloat32();
  515. goog.vec.Mat4.getTranslation(mat, translation);
  516. assertElementsRoughlyEqual(
  517. [0.59465038776, 0.413479506969, 0.0663217827677],
  518. translation, goog.vec.EPSILON);
  519. }
  520. function testMakeFrustum() {
  521. var m0 = goog.vec.Mat4.createFloat32();
  522. goog.vec.Mat4.makeFrustum(m0, -1, 2, -2, 1, .1, 1.1);
  523. assertElementsRoughlyEqual(
  524. [0.06666666, 0, 0, 0,
  525. 0, 0.06666666, 0, 0,
  526. 0.33333333, -0.33333333, -1.2, -1,
  527. 0, 0, -0.22, 0], m0, goog.vec.EPSILON);
  528. }
  529. function testMakePerspective() {
  530. var m0 = goog.vec.Mat4.createFloat32();
  531. goog.vec.Mat4.makePerspective(m0, 90 * Math.PI / 180, 2, 0.1, 1.1);
  532. assertElementsRoughlyEqual(
  533. [0.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1.2, -1, 0, 0, -0.22, 0],
  534. m0, goog.vec.EPSILON);
  535. }
  536. function testMakeOrtho() {
  537. var m0 = goog.vec.Mat4.createFloat32();
  538. goog.vec.Mat4.makeOrtho(m0, -1, 2, -2, 1, 0.1, 1.1);
  539. assertElementsRoughlyEqual(
  540. [0.6666666, 0, 0, 0,
  541. 0, 0.6666666, 0, 0,
  542. 0, 0, -2, 0,
  543. -0.333333, 0.3333333, -1.2, 1], m0, goog.vec.EPSILON);
  544. }
  545. function testMakeEulerZXZ() {
  546. var m0 = goog.vec.Mat4.createFloat32();
  547. var roll = 0.200982 * 2 * Math.PI;
  548. var tilt = 0.915833 * Math.PI;
  549. var yaw = 0.839392 * 2 * Math.PI;
  550. goog.vec.Mat4.makeRotate(m0, roll, 0, 0, 1);
  551. goog.vec.Mat4.rotate(m0, tilt, 1, 0, 0);
  552. goog.vec.Mat4.rotate(m0, yaw, 0, 0, 1);
  553. var m1 = goog.vec.Mat4.createFloat32();
  554. goog.vec.Mat4.makeEulerZXZ(m1, roll, tilt, yaw);
  555. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  556. var euler = [0, 0, 0];
  557. goog.vec.Mat4.toEulerZXZ(m0, euler);
  558. assertRoughlyEquals(roll, euler[0], goog.vec.EPSILON);
  559. assertRoughlyEquals(tilt, euler[1], goog.vec.EPSILON);
  560. assertRoughlyEquals(yaw, euler[2], goog.vec.EPSILON);
  561. // Test negative tilt now.
  562. goog.vec.Mat4.makeRotate(m0, roll, 0, 0, 1);
  563. goog.vec.Mat4.rotate(m0, -tilt, 1, 0, 0);
  564. goog.vec.Mat4.rotate(m0, yaw, 0, 0, 1);
  565. goog.vec.Mat4.makeEulerZXZ(m1, roll, -tilt, yaw);
  566. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  567. var euler = [0, 0, 0];
  568. goog.vec.Mat4.toEulerZXZ(m0, euler, true);
  569. assertRoughlyEquals(roll, euler[0], goog.vec.EPSILON);
  570. assertRoughlyEquals(-tilt, euler[1], goog.vec.EPSILON);
  571. assertRoughlyEquals(yaw, euler[2], goog.vec.EPSILON);
  572. }
  573. function testEulerZXZExtrema() {
  574. var m0 = goog.vec.Mat4.createFloat32FromArray(
  575. [1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
  576. var m1 = goog.vec.Mat4.createFloat32FromArray(
  577. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
  578. var euler = [0, 0, 0];
  579. goog.vec.Mat4.toEulerZXZ(m0, euler);
  580. assertElementsRoughlyEqual(
  581. [Math.PI, Math.PI / 2, Math.PI], euler, goog.vec.EPSILON);
  582. goog.vec.Mat4.makeEulerZXZ(m1, euler[0], euler[1], euler[2]);
  583. assertElementsRoughlyEqual(m0, m1, goog.vec.EPSILON);
  584. }
  585. function testLookAt() {
  586. var viewMatrix = goog.vec.Mat4.createFloat32();
  587. goog.vec.Mat4.makeLookAt(
  588. viewMatrix, [0, 0, 0], [1, 0, 0], [0, 1, 0]);
  589. assertElementsRoughlyEqual(
  590. [0, 0, -1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1], viewMatrix,
  591. goog.vec.EPSILON);
  592. }
  593. function testToLookAt() {
  594. // This test does not use the default precision goog.vec.EPSILON due to
  595. // precision issues in some browsers leading to flaky tests.
  596. var EPSILON = 1e-4;
  597. var eyeExp = [0, 0, 0];
  598. var fwdExp = [1, 0, 0];
  599. var upExp = [0, 1, 0];
  600. var centerExp = [0, 0, 0];
  601. goog.vec.Vec3.add(eyeExp, fwdExp, centerExp);
  602. var view = goog.vec.Mat4.createFloat32();
  603. goog.vec.Mat4.makeLookAt(view, eyeExp, centerExp, upExp);
  604. var eyeRes = [0, 0, 0];
  605. var fwdRes = [0, 0, 0];
  606. var upRes = [0, 0, 0];
  607. goog.vec.Mat4.toLookAt(view, eyeRes, fwdRes, upRes);
  608. assertElementsRoughlyEqual(eyeExp, eyeRes, EPSILON);
  609. assertElementsRoughlyEqual(fwdExp, fwdRes, EPSILON);
  610. assertElementsRoughlyEqual(upExp, upRes, EPSILON);
  611. }
  612. function testLookAtDecomposition() {
  613. // This test does not use the default precision goog.vec.EPSILON due to
  614. // precision issues in some browsers leading to flaky tests.
  615. var EPSILON = 1e-4;
  616. var viewExp = goog.vec.Mat4.createFloat32();
  617. var viewRes = goog.vec.Mat4.createFloat32();
  618. // Get a valid set of random vectors eye, forward, up by decomposing
  619. // a random matrix into a set of lookAt vectors.
  620. var tmp = goog.vec.Mat4.createFloat32FromArray(randomMat4);
  621. var eyeExp = [0, 0, 0];
  622. var fwdExp = [0, 0, 0];
  623. var upExp = [0, 0, 0];
  624. var centerExp = [0, 0, 0];
  625. // Project the random matrix into a real modelview matrix.
  626. goog.vec.Mat4.toLookAt(tmp, eyeExp, fwdExp, upExp);
  627. goog.vec.Vec3.add(eyeExp, fwdExp, centerExp);
  628. // Compute the expected modelview matrix from a set of valid random vectors.
  629. goog.vec.Mat4.makeLookAt(viewExp, eyeExp, centerExp, upExp);
  630. var eyeRes = [0, 0, 0];
  631. var fwdRes = [0, 0, 0];
  632. var upRes = [0, 0, 0];
  633. var centerRes = [0, 0, 0];
  634. goog.vec.Mat4.toLookAt(viewExp, eyeRes, fwdRes, upRes);
  635. goog.vec.Vec3.add(eyeRes, fwdRes, centerRes);
  636. goog.vec.Mat4.makeLookAt(viewRes, eyeRes, centerRes, upRes);
  637. assertElementsRoughlyEqual(eyeExp, eyeRes, EPSILON);
  638. assertElementsRoughlyEqual(fwdExp, fwdRes, EPSILON);
  639. assertElementsRoughlyEqual(upExp, upRes, EPSILON);
  640. assertElementsRoughlyEqual(viewExp, viewRes, EPSILON);
  641. }