Matrix3.tests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* global QUnit */
  2. import { Matrix3 } from '../../../../src/math/Matrix3.js';
  3. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  4. function matrixEquals3( a, b, tolerance ) {
  5. tolerance = tolerance || 0.0001;
  6. if ( a.elements.length != b.elements.length ) {
  7. return false;
  8. }
  9. for ( let i = 0, il = a.elements.length; i < il; i ++ ) {
  10. const delta = a.elements[ i ] - b.elements[ i ];
  11. if ( delta > tolerance ) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. function toMatrix4( m3 ) {
  18. const result = new Matrix4();
  19. const re = result.elements;
  20. const me = m3.elements;
  21. re[ 0 ] = me[ 0 ];
  22. re[ 1 ] = me[ 1 ];
  23. re[ 2 ] = me[ 2 ];
  24. re[ 4 ] = me[ 3 ];
  25. re[ 5 ] = me[ 4 ];
  26. re[ 6 ] = me[ 5 ];
  27. re[ 8 ] = me[ 6 ];
  28. re[ 9 ] = me[ 7 ];
  29. re[ 10 ] = me[ 8 ];
  30. return result;
  31. }
  32. export default QUnit.module( 'Maths', () => {
  33. QUnit.module( 'Matrix3', () => {
  34. // INSTANCING
  35. QUnit.test( 'Instancing', ( assert ) => {
  36. const a = new Matrix3();
  37. assert.ok( a.determinant() == 1, 'Passed!' );
  38. const b = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  39. assert.ok( b.elements[ 0 ] == 0 );
  40. assert.ok( b.elements[ 1 ] == 3 );
  41. assert.ok( b.elements[ 2 ] == 6 );
  42. assert.ok( b.elements[ 3 ] == 1 );
  43. assert.ok( b.elements[ 4 ] == 4 );
  44. assert.ok( b.elements[ 5 ] == 7 );
  45. assert.ok( b.elements[ 6 ] == 2 );
  46. assert.ok( b.elements[ 7 ] == 5 );
  47. assert.ok( b.elements[ 8 ] == 8 );
  48. assert.ok( ! matrixEquals3( a, b ), 'Passed!' );
  49. const c = new Matrix3( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  50. assert.ok( c.elements[ 0 ] == 0 );
  51. assert.ok( c.elements[ 1 ] == 3 );
  52. assert.ok( c.elements[ 2 ] == 6 );
  53. assert.ok( c.elements[ 3 ] == 1 );
  54. assert.ok( c.elements[ 4 ] == 4 );
  55. assert.ok( c.elements[ 5 ] == 7 );
  56. assert.ok( c.elements[ 6 ] == 2 );
  57. assert.ok( c.elements[ 7 ] == 5 );
  58. assert.ok( c.elements[ 8 ] == 8 );
  59. assert.ok( ! matrixEquals3( a, c ), 'Passed!' );
  60. } );
  61. // PUBLIC STUFF
  62. QUnit.test( 'isMatrix3', ( assert ) => {
  63. const a = new Matrix3();
  64. assert.ok( a.isMatrix3 === true, 'Passed!' );
  65. const b = new Matrix4();
  66. assert.ok( ! b.isMatrix3, 'Passed!' );
  67. } );
  68. QUnit.test( 'set', ( assert ) => {
  69. const b = new Matrix3();
  70. assert.ok( b.determinant() == 1, 'Passed!' );
  71. b.set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  72. assert.ok( b.elements[ 0 ] == 0 );
  73. assert.ok( b.elements[ 1 ] == 3 );
  74. assert.ok( b.elements[ 2 ] == 6 );
  75. assert.ok( b.elements[ 3 ] == 1 );
  76. assert.ok( b.elements[ 4 ] == 4 );
  77. assert.ok( b.elements[ 5 ] == 7 );
  78. assert.ok( b.elements[ 6 ] == 2 );
  79. assert.ok( b.elements[ 7 ] == 5 );
  80. assert.ok( b.elements[ 8 ] == 8 );
  81. } );
  82. QUnit.test( 'identity', ( assert ) => {
  83. const b = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  84. assert.ok( b.elements[ 0 ] == 0 );
  85. assert.ok( b.elements[ 1 ] == 3 );
  86. assert.ok( b.elements[ 2 ] == 6 );
  87. assert.ok( b.elements[ 3 ] == 1 );
  88. assert.ok( b.elements[ 4 ] == 4 );
  89. assert.ok( b.elements[ 5 ] == 7 );
  90. assert.ok( b.elements[ 6 ] == 2 );
  91. assert.ok( b.elements[ 7 ] == 5 );
  92. assert.ok( b.elements[ 8 ] == 8 );
  93. const a = new Matrix3();
  94. assert.ok( ! matrixEquals3( a, b ), 'Passed!' );
  95. b.identity();
  96. assert.ok( matrixEquals3( a, b ), 'Passed!' );
  97. } );
  98. QUnit.test( 'clone', ( assert ) => {
  99. const a = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  100. const b = a.clone();
  101. assert.ok( matrixEquals3( a, b ), 'Passed!' );
  102. // ensure that it is a true copy
  103. a.elements[ 0 ] = 2;
  104. assert.ok( ! matrixEquals3( a, b ), 'Passed!' );
  105. } );
  106. QUnit.test( 'copy', ( assert ) => {
  107. const a = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  108. const b = new Matrix3().copy( a );
  109. assert.ok( matrixEquals3( a, b ), 'Passed!' );
  110. // ensure that it is a true copy
  111. a.elements[ 0 ] = 2;
  112. assert.ok( ! matrixEquals3( a, b ), 'Passed!' );
  113. } );
  114. QUnit.todo( 'extractBasis', ( assert ) => {
  115. // extractBasis( xAxis, yAxis, zAxis )
  116. assert.ok( false, 'everything\'s gonna be alright' );
  117. } );
  118. QUnit.test( 'setFromMatrix4', ( assert ) => {
  119. const a = new Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  120. const b = new Matrix3();
  121. const c = new Matrix3().set( 0, 1, 2, 4, 5, 6, 8, 9, 10 );
  122. b.setFromMatrix4( a );
  123. assert.ok( b.equals( c ) );
  124. } );
  125. QUnit.test( 'multiply/premultiply', ( assert ) => {
  126. // both simply just wrap multiplyMatrices
  127. const a = new Matrix3().set( 2, 3, 5, 7, 11, 13, 17, 19, 23 );
  128. const b = new Matrix3().set( 29, 31, 37, 41, 43, 47, 53, 59, 61 );
  129. const expectedMultiply = [ 446, 1343, 2491, 486, 1457, 2701, 520, 1569, 2925 ];
  130. const expectedPremultiply = [ 904, 1182, 1556, 1131, 1489, 1967, 1399, 1845, 2435 ];
  131. a.multiply( b );
  132. assert.deepEqual( a.elements, expectedMultiply, 'multiply: check result' );
  133. a.set( 2, 3, 5, 7, 11, 13, 17, 19, 23 );
  134. a.premultiply( b );
  135. assert.deepEqual( a.elements, expectedPremultiply, 'premultiply: check result' );
  136. } );
  137. QUnit.test( 'multiplyMatrices', ( assert ) => {
  138. // Reference:
  139. //
  140. // #!/usr/bin/env python
  141. // from __future__ import print_function
  142. // import numpy as np
  143. // print(
  144. // np.dot(
  145. // np.reshape([2, 3, 5, 7, 11, 13, 17, 19, 23], (3, 3)),
  146. // np.reshape([29, 31, 37, 41, 43, 47, 53, 59, 61], (3, 3))
  147. // )
  148. // )
  149. //
  150. // [[ 446 486 520]
  151. // [1343 1457 1569]
  152. // [2491 2701 2925]]
  153. const lhs = new Matrix3().set( 2, 3, 5, 7, 11, 13, 17, 19, 23 );
  154. const rhs = new Matrix3().set( 29, 31, 37, 41, 43, 47, 53, 59, 61 );
  155. const ans = new Matrix3();
  156. ans.multiplyMatrices( lhs, rhs );
  157. assert.ok( ans.elements[ 0 ] == 446 );
  158. assert.ok( ans.elements[ 1 ] == 1343 );
  159. assert.ok( ans.elements[ 2 ] == 2491 );
  160. assert.ok( ans.elements[ 3 ] == 486 );
  161. assert.ok( ans.elements[ 4 ] == 1457 );
  162. assert.ok( ans.elements[ 5 ] == 2701 );
  163. assert.ok( ans.elements[ 6 ] == 520 );
  164. assert.ok( ans.elements[ 7 ] == 1569 );
  165. assert.ok( ans.elements[ 8 ] == 2925 );
  166. } );
  167. QUnit.test( 'multiplyScalar', ( assert ) => {
  168. const b = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  169. assert.ok( b.elements[ 0 ] == 0 );
  170. assert.ok( b.elements[ 1 ] == 3 );
  171. assert.ok( b.elements[ 2 ] == 6 );
  172. assert.ok( b.elements[ 3 ] == 1 );
  173. assert.ok( b.elements[ 4 ] == 4 );
  174. assert.ok( b.elements[ 5 ] == 7 );
  175. assert.ok( b.elements[ 6 ] == 2 );
  176. assert.ok( b.elements[ 7 ] == 5 );
  177. assert.ok( b.elements[ 8 ] == 8 );
  178. b.multiplyScalar( 2 );
  179. assert.ok( b.elements[ 0 ] == 0 * 2 );
  180. assert.ok( b.elements[ 1 ] == 3 * 2 );
  181. assert.ok( b.elements[ 2 ] == 6 * 2 );
  182. assert.ok( b.elements[ 3 ] == 1 * 2 );
  183. assert.ok( b.elements[ 4 ] == 4 * 2 );
  184. assert.ok( b.elements[ 5 ] == 7 * 2 );
  185. assert.ok( b.elements[ 6 ] == 2 * 2 );
  186. assert.ok( b.elements[ 7 ] == 5 * 2 );
  187. assert.ok( b.elements[ 8 ] == 8 * 2 );
  188. } );
  189. QUnit.test( 'determinant', ( assert ) => {
  190. const a = new Matrix3();
  191. assert.ok( a.determinant() == 1, 'Passed!' );
  192. a.elements[ 0 ] = 2;
  193. assert.ok( a.determinant() == 2, 'Passed!' );
  194. a.elements[ 0 ] = 0;
  195. assert.ok( a.determinant() == 0, 'Passed!' );
  196. // calculated via http://www.euclideanspace.com/maths/algebra/matrix/functions/determinant/threeD/index.htm
  197. a.set( 2, 3, 4, 5, 13, 7, 8, 9, 11 );
  198. assert.ok( a.determinant() == - 73, 'Passed!' );
  199. } );
  200. QUnit.test( 'invert', ( assert ) => {
  201. const zero = new Matrix3().set( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  202. const identity4 = new Matrix4();
  203. const a = new Matrix3().set( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  204. const b = new Matrix3();
  205. b.copy( a ).invert();
  206. assert.ok( matrixEquals3( b, zero ), 'Matrix a is zero matrix' );
  207. const testMatrices = [
  208. new Matrix4().makeRotationX( 0.3 ),
  209. new Matrix4().makeRotationX( - 0.3 ),
  210. new Matrix4().makeRotationY( 0.3 ),
  211. new Matrix4().makeRotationY( - 0.3 ),
  212. new Matrix4().makeRotationZ( 0.3 ),
  213. new Matrix4().makeRotationZ( - 0.3 ),
  214. new Matrix4().makeScale( 1, 2, 3 ),
  215. new Matrix4().makeScale( 1 / 8, 1 / 2, 1 / 3 )
  216. ];
  217. for ( let i = 0, il = testMatrices.length; i < il; i ++ ) {
  218. const m = testMatrices[ i ];
  219. a.setFromMatrix4( m );
  220. const mInverse3 = b.copy( a ).invert();
  221. const mInverse = toMatrix4( mInverse3 );
  222. // the determinant of the inverse should be the reciprocal
  223. assert.ok( Math.abs( a.determinant() * mInverse3.determinant() - 1 ) < 0.0001, 'Passed!' );
  224. assert.ok( Math.abs( m.determinant() * mInverse.determinant() - 1 ) < 0.0001, 'Passed!' );
  225. const mProduct = new Matrix4().multiplyMatrices( m, mInverse );
  226. assert.ok( Math.abs( mProduct.determinant() - 1 ) < 0.0001, 'Passed!' );
  227. assert.ok( matrixEquals3( mProduct, identity4 ), 'Passed!' );
  228. }
  229. } );
  230. QUnit.test( 'transpose', ( assert ) => {
  231. const a = new Matrix3();
  232. let b = a.clone().transpose();
  233. assert.ok( matrixEquals3( a, b ), 'Passed!' );
  234. b = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  235. const c = b.clone().transpose();
  236. assert.ok( ! matrixEquals3( b, c ), 'Passed!' );
  237. c.transpose();
  238. assert.ok( matrixEquals3( b, c ), 'Passed!' );
  239. } );
  240. QUnit.test( 'getNormalMatrix', ( assert ) => {
  241. const a = new Matrix3();
  242. const b = new Matrix4().set(
  243. 2, 3, 5, 7,
  244. 11, 13, 17, 19,
  245. 23, 29, 31, 37,
  246. 41, 43, 47, 57
  247. );
  248. const expected = new Matrix3().set(
  249. - 1.2857142857142856, 0.7142857142857143, 0.2857142857142857,
  250. 0.7428571428571429, - 0.7571428571428571, 0.15714285714285714,
  251. - 0.19999999999999998, 0.3, - 0.09999999999999999
  252. );
  253. a.getNormalMatrix( b );
  254. assert.ok( matrixEquals3( a, expected ), 'Check resulting Matrix3' );
  255. } );
  256. QUnit.test( 'transposeIntoArray', ( assert ) => {
  257. const a = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  258. const b = [];
  259. a.transposeIntoArray( b );
  260. assert.ok( b[ 0 ] == 0 );
  261. assert.ok( b[ 1 ] == 1 );
  262. assert.ok( b[ 2 ] == 2 );
  263. assert.ok( b[ 3 ] == 3 );
  264. assert.ok( b[ 4 ] == 4 );
  265. assert.ok( b[ 5 ] == 5 );
  266. assert.ok( b[ 5 ] == 5 );
  267. assert.ok( b[ 6 ] == 6 );
  268. assert.ok( b[ 7 ] == 7 );
  269. assert.ok( b[ 8 ] == 8 );
  270. } );
  271. QUnit.test( 'setUvTransform', ( assert ) => {
  272. const a = new Matrix3().set(
  273. 0.1767766952966369, 0.17677669529663687, 0.32322330470336313,
  274. - 0.17677669529663687, 0.1767766952966369, 0.5,
  275. 0, 0, 1
  276. );
  277. const b = new Matrix3();
  278. const params = {
  279. centerX: 0.5,
  280. centerY: 0.5,
  281. offsetX: 0,
  282. offsetY: 0,
  283. repeatX: 0.25,
  284. repeatY: 0.25,
  285. rotation: 0.7753981633974483
  286. };
  287. const expected = new Matrix3().set(
  288. 0.1785355940258599, 0.17500011904519763, 0.32323214346447127,
  289. - 0.17500011904519763, 0.1785355940258599, 0.4982322625096689,
  290. 0, 0, 1
  291. );
  292. a.setUvTransform(
  293. params.offsetX, params.offsetY,
  294. params.repeatX, params.repeatY,
  295. params.rotation,
  296. params.centerX, params.centerY
  297. );
  298. b.identity()
  299. .translate( - params.centerX, - params.centerY )
  300. .rotate( params.rotation )
  301. .scale( params.repeatX, params.repeatY )
  302. .translate( params.centerX, params.centerY )
  303. .translate( params.offsetX, params.offsetY );
  304. assert.ok( matrixEquals3( a, expected ), 'Check direct method' );
  305. assert.ok( matrixEquals3( b, expected ), 'Check indirect method' );
  306. } );
  307. QUnit.test( 'scale', ( assert ) => {
  308. const a = new Matrix3().set( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  309. const expected = new Matrix3().set(
  310. 0.25, 0.5, 0.75,
  311. 1, 1.25, 1.5,
  312. 7, 8, 9
  313. );
  314. a.scale( 0.25, 0.25 );
  315. assert.ok( matrixEquals3( a, expected ), 'Check scaling result' );
  316. } );
  317. QUnit.test( 'rotate', ( assert ) => {
  318. const a = new Matrix3().set( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  319. const expected = new Matrix3().set(
  320. 3.5355339059327373, 4.949747468305833, 6.363961030678928,
  321. 2.121320343559643, 2.121320343559643, 2.1213203435596433,
  322. 7, 8, 9
  323. );
  324. a.rotate( Math.PI / 4 );
  325. assert.ok( matrixEquals3( a, expected ), 'Check rotated result' );
  326. } );
  327. QUnit.test( 'translate', ( assert ) => {
  328. const a = new Matrix3().set( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  329. const expected = new Matrix3().set( 22, 26, 30, 53, 61, 69, 7, 8, 9 );
  330. a.translate( 3, 7 );
  331. assert.ok( matrixEquals3( a, expected ), 'Check translation result' );
  332. } );
  333. QUnit.todo( 'makeTranslation', ( assert ) => {
  334. const a = new Matrix3();
  335. const b = new Vector2( 1, 2 );
  336. const c = new Matrix3().set( 1, 0, 1, 0, 1, 2, 0, 0, 1 );
  337. a.makeTranslation( b.x, b.y );
  338. assert.ok( matrixEquals3( a, c ), 'Check translation result' );
  339. a.makeTranslation( b );
  340. assert.ok( matrixEquals3( a, c ), 'Check translation result' );
  341. } );
  342. QUnit.todo( 'makeRotation', ( assert ) => {
  343. // makeRotation( theta ) // counterclockwise
  344. assert.ok( false, 'everything\'s gonna be alright' );
  345. } );
  346. QUnit.todo( 'makeScale', ( assert ) => {
  347. // makeScale( x, y )
  348. assert.ok( false, 'everything\'s gonna be alright' );
  349. } );
  350. QUnit.test( 'equals', ( assert ) => {
  351. const a = new Matrix3().set( 0, 1, 2, 3, 4, 5, 6, 7, 8 );
  352. const b = new Matrix3().set( 0, - 1, 2, 3, 4, 5, 6, 7, 8 );
  353. assert.notOk( a.equals( b ), 'Check that a does not equal b' );
  354. assert.notOk( b.equals( a ), 'Check that b does not equal a' );
  355. a.copy( b );
  356. assert.ok( a.equals( b ), 'Check that a equals b after copy()' );
  357. assert.ok( b.equals( a ), 'Check that b equals a after copy()' );
  358. } );
  359. QUnit.test( 'fromArray', ( assert ) => {
  360. let b = new Matrix3();
  361. b.fromArray( [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] );
  362. assert.ok( b.elements[ 0 ] == 0 );
  363. assert.ok( b.elements[ 1 ] == 1 );
  364. assert.ok( b.elements[ 2 ] == 2 );
  365. assert.ok( b.elements[ 3 ] == 3 );
  366. assert.ok( b.elements[ 4 ] == 4 );
  367. assert.ok( b.elements[ 5 ] == 5 );
  368. assert.ok( b.elements[ 6 ] == 6 );
  369. assert.ok( b.elements[ 7 ] == 7 );
  370. assert.ok( b.elements[ 8 ] == 8 );
  371. b = new Matrix3();
  372. b.fromArray( [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ], 10 );
  373. assert.ok( b.elements[ 0 ] == 10 );
  374. assert.ok( b.elements[ 1 ] == 11 );
  375. assert.ok( b.elements[ 2 ] == 12 );
  376. assert.ok( b.elements[ 3 ] == 13 );
  377. assert.ok( b.elements[ 4 ] == 14 );
  378. assert.ok( b.elements[ 5 ] == 15 );
  379. assert.ok( b.elements[ 6 ] == 16 );
  380. assert.ok( b.elements[ 7 ] == 17 );
  381. assert.ok( b.elements[ 8 ] == 18 );
  382. } );
  383. QUnit.test( 'toArray', ( assert ) => {
  384. const a = new Matrix3().set( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  385. const noOffset = [ 1, 4, 7, 2, 5, 8, 3, 6, 9 ];
  386. const withOffset = [ undefined, 1, 4, 7, 2, 5, 8, 3, 6, 9 ];
  387. let array = a.toArray();
  388. assert.deepEqual( array, noOffset, 'No array, no offset' );
  389. array = [];
  390. a.toArray( array );
  391. assert.deepEqual( array, noOffset, 'With array, no offset' );
  392. array = [];
  393. a.toArray( array, 1 );
  394. assert.deepEqual( array, withOffset, 'With array, with offset' );
  395. } );
  396. } );
  397. } );