mat4d_test.js 26 KB

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