Matrix3.tests.js 13 KB

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