mat4_test.js 25 KB

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