Matrix3.tests.js 15 KB

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