Matrix4.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. QUnit.module( "Matrix4" );
  5. var matrixEquals4 = function( a, b, tolerance ) {
  6. tolerance = tolerance || 0.0001;
  7. if( a.elements.length != b.elements.length ) {
  8. return false;
  9. }
  10. for( var i = 0, il = a.elements.length; i < il; i ++ ) {
  11. var delta = a.elements[i] - b.elements[i];
  12. if( delta > tolerance ) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. };
  18. QUnit.test( "constructor" , function( assert ) {
  19. var a = new THREE.Matrix4();
  20. assert.ok( a.determinant() == 1, "Passed!" );
  21. var b = new THREE.Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  22. assert.ok( b.elements[0] == 0 );
  23. assert.ok( b.elements[1] == 4 );
  24. assert.ok( b.elements[2] == 8 );
  25. assert.ok( b.elements[3] == 12 );
  26. assert.ok( b.elements[4] == 1 );
  27. assert.ok( b.elements[5] == 5 );
  28. assert.ok( b.elements[6] == 9 );
  29. assert.ok( b.elements[7] == 13 );
  30. assert.ok( b.elements[8] == 2 );
  31. assert.ok( b.elements[9] == 6 );
  32. assert.ok( b.elements[10] == 10 );
  33. assert.ok( b.elements[11] == 14 );
  34. assert.ok( b.elements[12] == 3 );
  35. assert.ok( b.elements[13] == 7 );
  36. assert.ok( b.elements[14] == 11 );
  37. assert.ok( b.elements[15] == 15 );
  38. assert.ok( ! matrixEquals4( a, b ), "Passed!" );
  39. });
  40. QUnit.test( "copy" , function( assert ) {
  41. var a = new THREE.Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  42. var b = new THREE.Matrix4().copy( a );
  43. assert.ok( matrixEquals4( a, b ), "Passed!" );
  44. // ensure that it is a true copy
  45. a.elements[0] = 2;
  46. assert.ok( ! matrixEquals4( a, b ), "Passed!" );
  47. });
  48. QUnit.test( "set" , function( assert ) {
  49. var b = new THREE.Matrix4();
  50. assert.ok( b.determinant() == 1, "Passed!" );
  51. b.set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  52. assert.ok( b.elements[0] == 0 );
  53. assert.ok( b.elements[1] == 4 );
  54. assert.ok( b.elements[2] == 8 );
  55. assert.ok( b.elements[3] == 12 );
  56. assert.ok( b.elements[4] == 1 );
  57. assert.ok( b.elements[5] == 5 );
  58. assert.ok( b.elements[6] == 9 );
  59. assert.ok( b.elements[7] == 13 );
  60. assert.ok( b.elements[8] == 2 );
  61. assert.ok( b.elements[9] == 6 );
  62. assert.ok( b.elements[10] == 10 );
  63. assert.ok( b.elements[11] == 14 );
  64. assert.ok( b.elements[12] == 3 );
  65. assert.ok( b.elements[13] == 7 );
  66. assert.ok( b.elements[14] == 11 );
  67. assert.ok( b.elements[15] == 15 );
  68. });
  69. QUnit.test( "identity" , function( assert ) {
  70. var b = new THREE.Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  71. assert.ok( b.elements[0] == 0 );
  72. assert.ok( b.elements[1] == 4 );
  73. assert.ok( b.elements[2] == 8 );
  74. assert.ok( b.elements[3] == 12 );
  75. assert.ok( b.elements[4] == 1 );
  76. assert.ok( b.elements[5] == 5 );
  77. assert.ok( b.elements[6] == 9 );
  78. assert.ok( b.elements[7] == 13 );
  79. assert.ok( b.elements[8] == 2 );
  80. assert.ok( b.elements[9] == 6 );
  81. assert.ok( b.elements[10] == 10 );
  82. assert.ok( b.elements[11] == 14 );
  83. assert.ok( b.elements[12] == 3 );
  84. assert.ok( b.elements[13] == 7 );
  85. assert.ok( b.elements[14] == 11 );
  86. assert.ok( b.elements[15] == 15 );
  87. var a = new THREE.Matrix4();
  88. assert.ok( ! matrixEquals4( a, b ), "Passed!" );
  89. b.identity();
  90. assert.ok( matrixEquals4( a, b ), "Passed!" );
  91. });
  92. QUnit.test( "multiplyMatrices" , function ( assert ) {
  93. // Reference:
  94. //
  95. // #!/usr/bin/env python
  96. // from __future__ import print_function
  97. // import numpy as np
  98. // print(
  99. // np.dot(
  100. // np.reshape([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53], (4, 4)),
  101. // np.reshape([59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131], (4, 4))
  102. // )
  103. // )
  104. //
  105. // [[ 1585 1655 1787 1861]
  106. // [ 5318 5562 5980 6246]
  107. // [10514 11006 11840 12378]
  108. // [15894 16634 17888 18710]]
  109. var lhs = new THREE.Matrix4().set( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 );
  110. var rhs = new THREE.Matrix4().set( 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131 );
  111. var ans = new THREE.Matrix4();
  112. ans.multiplyMatrices(lhs, rhs);
  113. assert.ok( ans.elements[0] == 1585 );
  114. assert.ok( ans.elements[1] == 5318 );
  115. assert.ok( ans.elements[2] == 10514 );
  116. assert.ok( ans.elements[3] == 15894 );
  117. assert.ok( ans.elements[4] == 1655 );
  118. assert.ok( ans.elements[5] == 5562 );
  119. assert.ok( ans.elements[6] == 11006 );
  120. assert.ok( ans.elements[7] == 16634 );
  121. assert.ok( ans.elements[8] == 1787 );
  122. assert.ok( ans.elements[9] == 5980 );
  123. assert.ok( ans.elements[10] == 11840 );
  124. assert.ok( ans.elements[11] == 17888 );
  125. assert.ok( ans.elements[12] == 1861 );
  126. assert.ok( ans.elements[13] == 6246 );
  127. assert.ok( ans.elements[14] == 12378 );
  128. assert.ok( ans.elements[15] == 18710 );
  129. });
  130. QUnit.test( "multiplyScalar" , function( assert ) {
  131. var b = new THREE.Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  132. assert.ok( b.elements[0] == 0 );
  133. assert.ok( b.elements[1] == 4 );
  134. assert.ok( b.elements[2] == 8 );
  135. assert.ok( b.elements[3] == 12 );
  136. assert.ok( b.elements[4] == 1 );
  137. assert.ok( b.elements[5] == 5 );
  138. assert.ok( b.elements[6] == 9 );
  139. assert.ok( b.elements[7] == 13 );
  140. assert.ok( b.elements[8] == 2 );
  141. assert.ok( b.elements[9] == 6 );
  142. assert.ok( b.elements[10] == 10 );
  143. assert.ok( b.elements[11] == 14 );
  144. assert.ok( b.elements[12] == 3 );
  145. assert.ok( b.elements[13] == 7 );
  146. assert.ok( b.elements[14] == 11 );
  147. assert.ok( b.elements[15] == 15 );
  148. b.multiplyScalar( 2 );
  149. assert.ok( b.elements[0] == 0*2 );
  150. assert.ok( b.elements[1] == 4*2 );
  151. assert.ok( b.elements[2] == 8*2 );
  152. assert.ok( b.elements[3] == 12*2 );
  153. assert.ok( b.elements[4] == 1*2 );
  154. assert.ok( b.elements[5] == 5*2 );
  155. assert.ok( b.elements[6] == 9*2 );
  156. assert.ok( b.elements[7] == 13*2 );
  157. assert.ok( b.elements[8] == 2*2 );
  158. assert.ok( b.elements[9] == 6*2 );
  159. assert.ok( b.elements[10] == 10*2 );
  160. assert.ok( b.elements[11] == 14*2 );
  161. assert.ok( b.elements[12] == 3*2 );
  162. assert.ok( b.elements[13] == 7*2 );
  163. assert.ok( b.elements[14] == 11*2 );
  164. assert.ok( b.elements[15] == 15*2 );
  165. });
  166. QUnit.test( "determinant" , function( assert ) {
  167. var a = new THREE.Matrix4();
  168. assert.ok( a.determinant() == 1, "Passed!" );
  169. a.elements[0] = 2;
  170. assert.ok( a.determinant() == 2, "Passed!" );
  171. a.elements[0] = 0;
  172. assert.ok( a.determinant() == 0, "Passed!" );
  173. // calculated via http://www.euclideanspace.com/maths/algebra/matrix/functions/determinant/fourD/index.htm
  174. a.set( 2, 3, 4, 5, -1, -21, -3, -4, 6, 7, 8, 10, -8, -9, -10, -12 );
  175. assert.ok( a.determinant() == 76, "Passed!" );
  176. });
  177. QUnit.test( "getInverse" , function( assert ) {
  178. var identity = new THREE.Matrix4();
  179. var a = new THREE.Matrix4();
  180. var b = new THREE.Matrix4().set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  181. var c = new THREE.Matrix4().set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  182. assert.ok( ! matrixEquals4( a, b ), "Passed!" );
  183. b.getInverse( a, false );
  184. assert.ok( matrixEquals4( b, new THREE.Matrix4() ), "Passed!" );
  185. try {
  186. b.getInverse( c, true );
  187. assert.ok( false, "Passed!" ); // should never get here.
  188. }
  189. catch( err ) {
  190. assert.ok( true, "Passed!" );
  191. }
  192. var testMatrices = [
  193. new THREE.Matrix4().makeRotationX( 0.3 ),
  194. new THREE.Matrix4().makeRotationX( -0.3 ),
  195. new THREE.Matrix4().makeRotationY( 0.3 ),
  196. new THREE.Matrix4().makeRotationY( -0.3 ),
  197. new THREE.Matrix4().makeRotationZ( 0.3 ),
  198. new THREE.Matrix4().makeRotationZ( -0.3 ),
  199. new THREE.Matrix4().makeScale( 1, 2, 3 ),
  200. new THREE.Matrix4().makeScale( 1/8, 1/2, 1/3 ),
  201. new THREE.Matrix4().makePerspective( -1, 1, 1, -1, 1, 1000 ),
  202. new THREE.Matrix4().makePerspective( -16, 16, 9, -9, 0.1, 10000 ),
  203. new THREE.Matrix4().makeTranslation( 1, 2, 3 )
  204. ];
  205. for( var i = 0, il = testMatrices.length; i < il; i ++ ) {
  206. var m = testMatrices[i];
  207. var mInverse = new THREE.Matrix4().getInverse( m );
  208. var mSelfInverse = m.clone();
  209. mSelfInverse.getInverse( mSelfInverse );
  210. // self-inverse should the same as inverse
  211. assert.ok( matrixEquals4( mSelfInverse, mInverse ), "Passed!" );
  212. // the determinant of the inverse should be the reciprocal
  213. assert.ok( Math.abs( m.determinant() * mInverse.determinant() - 1 ) < 0.0001, "Passed!" );
  214. var mProduct = new THREE.Matrix4().multiplyMatrices( m, mInverse );
  215. // the determinant of the identity matrix is 1
  216. assert.ok( Math.abs( mProduct.determinant() - 1 ) < 0.0001, "Passed!" );
  217. assert.ok( matrixEquals4( mProduct, identity ), "Passed!" );
  218. }
  219. });
  220. QUnit.test( "makeBasis/extractBasis", function( assert ) {
  221. var identityBasis = [ new THREE.Vector3( 1, 0, 0 ), new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 1 ) ];
  222. var a = new THREE.Matrix4().makeBasis( identityBasis[0], identityBasis[1], identityBasis[2] );
  223. var identity = new THREE.Matrix4();
  224. assert.ok( matrixEquals4( a, identity ), "Passed!" );
  225. var testBases = [ [ new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( -1, 0, 0 ), new THREE.Vector3( 0, 0, 1 ) ] ];
  226. for( var i = 0; i < testBases.length; i ++ ) {
  227. var testBasis = testBases[i];
  228. var b = new THREE.Matrix4().makeBasis( testBasis[0], testBasis[1], testBasis[2] );
  229. var outBasis = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  230. b.extractBasis( outBasis[0], outBasis[1], outBasis[2] );
  231. // check what goes in, is what comes out.
  232. for( var j = 0; j < outBasis.length; j ++ ) {
  233. assert.ok( outBasis[j].equals( testBasis[j] ), "Passed!" );
  234. }
  235. // get the basis out the hard war
  236. for( var j = 0; j < identityBasis.length; j ++ ) {
  237. outBasis[j].copy( identityBasis[j] );
  238. outBasis[j].applyMatrix4( b );
  239. }
  240. // did the multiply method of basis extraction work?
  241. for( var j = 0; j < outBasis.length; j ++ ) {
  242. assert.ok( outBasis[j].equals( testBasis[j] ), "Passed!" );
  243. }
  244. }
  245. });
  246. QUnit.test( "transpose" , function( assert ) {
  247. var a = new THREE.Matrix4();
  248. var b = a.clone().transpose();
  249. assert.ok( matrixEquals4( a, b ), "Passed!" );
  250. b = new THREE.Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  251. var c = b.clone().transpose();
  252. assert.ok( ! matrixEquals4( b, c ), "Passed!" );
  253. c.transpose();
  254. assert.ok( matrixEquals4( b, c ), "Passed!" );
  255. });
  256. QUnit.test( "clone" , function( assert ) {
  257. var a = new THREE.Matrix4().set( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
  258. var b = a.clone();
  259. assert.ok( matrixEquals4( a, b ), "Passed!" );
  260. // ensure that it is a true copy
  261. a.elements[0] = 2;
  262. assert.ok( ! matrixEquals4( a, b ), "Passed!" );
  263. });
  264. QUnit.test( "compose/decompose", function( assert ) {
  265. var tValues = [
  266. new THREE.Vector3(),
  267. new THREE.Vector3( 3, 0, 0 ),
  268. new THREE.Vector3( 0, 4, 0 ),
  269. new THREE.Vector3( 0, 0, 5 ),
  270. new THREE.Vector3( -6, 0, 0 ),
  271. new THREE.Vector3( 0, -7, 0 ),
  272. new THREE.Vector3( 0, 0, -8 ),
  273. new THREE.Vector3( -2, 5, -9 ),
  274. new THREE.Vector3( -2, -5, -9 )
  275. ];
  276. var sValues = [
  277. new THREE.Vector3( 1, 1, 1 ),
  278. new THREE.Vector3( 2, 2, 2 ),
  279. new THREE.Vector3( 1, -1, 1 ),
  280. new THREE.Vector3( -1, 1, 1 ),
  281. new THREE.Vector3( 1, 1, -1 ),
  282. new THREE.Vector3( 2, -2, 1 ),
  283. new THREE.Vector3( -1, 2, -2 ),
  284. new THREE.Vector3( -1, -1, -1 ),
  285. new THREE.Vector3( -2, -2, -2 )
  286. ];
  287. var rValues = [
  288. new THREE.Quaternion(),
  289. new THREE.Quaternion().setFromEuler( new THREE.Euler( 1, 1, 0 ) ),
  290. new THREE.Quaternion().setFromEuler( new THREE.Euler( 1, -1, 1 ) ),
  291. new THREE.Quaternion( 0, 0.9238795292366128, 0, 0.38268342717215614 )
  292. ];
  293. for( var ti = 0; ti < tValues.length; ti ++ ) {
  294. for( var si = 0; si < sValues.length; si ++ ) {
  295. for( var ri = 0; ri < rValues.length; ri ++ ) {
  296. var t = tValues[ti];
  297. var s = sValues[si];
  298. var r = rValues[ri];
  299. var m = new THREE.Matrix4().compose( t, r, s );
  300. var t2 = new THREE.Vector3();
  301. var r2 = new THREE.Quaternion();
  302. var s2 = new THREE.Vector3();
  303. m.decompose( t2, r2, s2 );
  304. var m2 = new THREE.Matrix4().compose( t2, r2, s2 );
  305. var matrixIsSame = matrixEquals4( m, m2 );
  306. /* debug code
  307. if( ! matrixIsSame ) {
  308. console.log( t, s, r );
  309. console.log( t2, s2, r2 );
  310. console.log( m, m2 );
  311. }*/
  312. assert.ok( matrixEquals4( m, m2 ), "Passed!" );
  313. }
  314. }
  315. }
  316. });