matrixOps.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. using namespace scene;
  7. using namespace video;
  8. using namespace io;
  9. using namespace gui;
  10. namespace
  11. {
  12. // Basic tests for identity matrix
  13. bool identity(void)
  14. {
  15. bool result = true;
  16. matrix4 m;
  17. // Check default init
  18. result &= (m==core::IdentityMatrix);
  19. result &= (core::IdentityMatrix==m);
  20. assert_log(result);
  21. // Since the last test can be made with isDefinitelyIdentityMatrix we set it to false here
  22. m.setDefinitelyIdentityMatrix(false);
  23. result &= (m==core::IdentityMatrix);
  24. result &= (core::IdentityMatrix==m);
  25. assert_log(result);
  26. // also equals should see this
  27. result &= m.equals(core::IdentityMatrix);
  28. result &= core::IdentityMatrix.equals(m);
  29. assert_log(result);
  30. // Check inequality
  31. m[12]=5.f;
  32. result &= (m!=core::IdentityMatrix);
  33. result &= (core::IdentityMatrix!=m);
  34. result &= !m.equals(core::IdentityMatrix);
  35. result &= !core::IdentityMatrix.equals(m);
  36. assert_log(result);
  37. // Test multiplication
  38. result &= (m==(core::IdentityMatrix*m));
  39. result &= m.equals(core::IdentityMatrix*m);
  40. result &= (m==(m*core::IdentityMatrix));
  41. result &= m.equals(m*core::IdentityMatrix);
  42. assert_log(result);
  43. return result;
  44. }
  45. // Test rotations
  46. bool transformations(void)
  47. {
  48. bool result = true;
  49. matrix4 m, s;
  50. m.setRotationDegrees(core::vector3df(30,40,50));
  51. s.setScale(core::vector3df(2,3,4));
  52. m *= s;
  53. m.setTranslation(core::vector3df(5,6,7));
  54. result &= (core::vector3df(5,6,7).equals(m.getTranslation()));
  55. assert_log(result);
  56. result &= (core::vector3df(2,3,4).equals(m.getScale()));
  57. assert_log(result);
  58. core::vector3df newRotation = m.getRotationDegrees();
  59. result &= (core::vector3df(30,40,50).equals(newRotation, 0.000004f));
  60. assert_log(result);
  61. m.setRotationDegrees(vector3df(90.0001f, 270.85f, 180.0f));
  62. s.setRotationDegrees(vector3df(0,0, 0.860866f));
  63. m *= s;
  64. newRotation = m.getRotationDegrees();
  65. result &= (core::vector3df(0,270,270).equals(newRotation, 0.0001f));
  66. assert_log(result);
  67. m.setRotationDegrees(vector3df(270.0f, 89.8264f, 0.000100879f));
  68. s.setRotationDegrees(vector3df(0,0, 0.189398f));
  69. m *= s;
  70. newRotation = m.getRotationDegrees();
  71. result &= (core::vector3df(0,90,90).equals(newRotation, 0.0001f));
  72. assert_log(result);
  73. m.setRotationDegrees(vector3df(270.0f, 89.0602f, 359.999f));
  74. s.setRotationDegrees(vector3df(0,0, 0.949104f));
  75. m *= s;
  76. newRotation = m.getRotationDegrees();
  77. result &= (core::vector3df(0,90,89.999f).equals(newRotation));
  78. assert_log(result);
  79. return result;
  80. }
  81. // Test rotations
  82. bool rotations(void)
  83. {
  84. bool result = true;
  85. matrix4 rot1,rot2,rot3,rot4,rot5;
  86. core::vector3df vec1(1,2,3),vec12(1,2,3);
  87. core::vector3df vec2(-5,0,0),vec22(-5,0,0);
  88. core::vector3df vec3(20,0,-20), vec32(20,0,-20);
  89. // Make sure the matrix multiplication and rotation application give same results
  90. rot1.setRotationDegrees(core::vector3df(90,0,0));
  91. rot2.setRotationDegrees(core::vector3df(0,90,0));
  92. rot3.setRotationDegrees(core::vector3df(0,0,90));
  93. rot4.setRotationDegrees(core::vector3df(90,90,90));
  94. rot5 = rot3*rot2*rot1;
  95. result &= (rot4.equals(rot5, ROUNDING_ERROR_f32));
  96. assert_log(result);
  97. rot4.transformVect(vec1);rot5.transformVect(vec12);
  98. rot4.transformVect(vec2);rot5.transformVect(vec22);
  99. rot4.transformVect(vec3);rot5.transformVect(vec32);
  100. result &= (vec1.equals(vec12));
  101. result &= (vec2.equals(vec22));
  102. result &= (vec3.equals(vec32));
  103. assert_log(result);
  104. vec1.set(1,2,3);vec12.set(1,2,3);
  105. vec2.set(-5,0,0);vec22.set(-5,0,0);
  106. vec3.set(20,0,-20);vec32.set(20,0,-20);
  107. rot1.setRotationDegrees(core::vector3df(45,0,0));
  108. rot2.setRotationDegrees(core::vector3df(0,45,0));
  109. rot3.setRotationDegrees(core::vector3df(0,0,45));
  110. rot4.setRotationDegrees(core::vector3df(45,45,45));
  111. rot5 = rot3*rot2*rot1;
  112. result &= (rot4.equals(rot5, ROUNDING_ERROR_f32));
  113. assert_log(result);
  114. rot4.transformVect(vec1);rot5.transformVect(vec12);
  115. rot4.transformVect(vec2);rot5.transformVect(vec22);
  116. rot4.transformVect(vec3);rot5.transformVect(vec32);
  117. result &= (vec1.equals(vec12));
  118. result &= (vec2.equals(vec22));
  119. result &= (vec3.equals(vec32, 2*ROUNDING_ERROR_f32));
  120. assert_log(result);
  121. vec1.set(1,2,3);vec12.set(1,2,3);
  122. vec2.set(-5,0,0);vec22.set(-5,0,0);
  123. vec3.set(20,0,-20);vec32.set(20,0,-20);
  124. rot1.setRotationDegrees(core::vector3df(-60,0,0));
  125. rot2.setRotationDegrees(core::vector3df(0,-60,0));
  126. rot3.setRotationDegrees(core::vector3df(0,0,-60));
  127. rot4.setRotationDegrees(core::vector3df(-60,-60,-60));
  128. rot5 = rot3*rot2*rot1;
  129. result &= (rot4.equals(rot5, ROUNDING_ERROR_f32));
  130. assert_log(result);
  131. rot4.transformVect(vec1);rot5.transformVect(vec12);
  132. rot4.transformVect(vec2);rot5.transformVect(vec22);
  133. rot4.transformVect(vec3);rot5.transformVect(vec32);
  134. result &= (vec1.equals(vec12));
  135. result &= (vec2.equals(vec22));
  136. // this one needs higher tolerance due to rounding issues
  137. result &= (vec3.equals(vec32, 0.000002f));
  138. assert_log(result);
  139. vec1.set(1,2,3);vec12.set(1,2,3);
  140. vec2.set(-5,0,0);vec22.set(-5,0,0);
  141. vec3.set(20,0,-20);vec32.set(20,0,-20);
  142. rot1.setRotationDegrees(core::vector3df(113,0,0));
  143. rot2.setRotationDegrees(core::vector3df(0,-27,0));
  144. rot3.setRotationDegrees(core::vector3df(0,0,193));
  145. rot4.setRotationDegrees(core::vector3df(113,-27,193));
  146. rot5 = rot3*rot2*rot1;
  147. result &= (rot4.equals(rot5, ROUNDING_ERROR_f32));
  148. assert_log(result);
  149. rot4.transformVect(vec1);rot5.transformVect(vec12);
  150. rot4.transformVect(vec2);rot5.transformVect(vec22);
  151. rot4.transformVect(vec3);rot5.transformVect(vec32);
  152. // these ones need higher tolerance due to rounding issues
  153. result &= (vec1.equals(vec12, 0.000002f));
  154. assert_log(result);
  155. result &= (vec2.equals(vec22));
  156. assert_log(result);
  157. result &= (vec3.equals(vec32, 0.000002f));
  158. assert_log(result);
  159. rot1.setRotationDegrees(core::vector3df(0,0,34));
  160. rot2.setRotationDegrees(core::vector3df(0,43,0));
  161. vec1=(rot2*rot1).getRotationDegrees();
  162. result &= (vec1.equals(core::vector3df(27.5400505f, 34.4302292f, 42.6845398f), 0.000002f));
  163. assert_log(result);
  164. // corner cases
  165. rot1.setRotationDegrees(irr::core::vector3df(180.0f, 0.f, 0.f));
  166. vec1=rot1.getRotationDegrees();
  167. result &= (vec1.equals(core::vector3df(180.0f, 0.f, 0.f), 0.000002f));
  168. assert_log(result);
  169. rot1.setRotationDegrees(irr::core::vector3df(0.f, 180.0f, 0.f));
  170. vec1=rot1.getRotationDegrees();
  171. result &= (vec1.equals(core::vector3df(180.0f, 360, 180.0f), 0.000002f));
  172. assert_log(result);
  173. rot1.setRotationDegrees(irr::core::vector3df(0.f, 0.f, 180.0f));
  174. vec1=rot1.getRotationDegrees();
  175. result &= (vec1.equals(core::vector3df(0.f, 0.f, 180.0f), 0.000002f));
  176. assert_log(result);
  177. rot1.makeIdentity();
  178. rot1.setRotationDegrees(core::vector3df(270.f,0,0));
  179. rot2.makeIdentity();
  180. rot2.setRotationDegrees(core::vector3df(-90.f,0,0));
  181. vec1=(rot1*rot2).getRotationDegrees();
  182. result &= (vec1.equals(core::vector3df(180.f, 0.f, 0.0f)));
  183. assert_log(result);
  184. return result;
  185. }
  186. // Test isOrthogonal
  187. bool isOrthogonal(void)
  188. {
  189. matrix4 rotationMatrix;
  190. if (!rotationMatrix.isOrthogonal())
  191. {
  192. logTestString("irr::core::matrix4::isOrthogonal() failed with Identity.\n");
  193. return false;
  194. }
  195. rotationMatrix.setRotationDegrees(vector3df(90, 0, 0));
  196. if (!rotationMatrix.isOrthogonal())
  197. {
  198. logTestString("irr::core::matrix4::isOrthogonal() failed with rotation.\n");
  199. return false;
  200. }
  201. matrix4 translationMatrix;
  202. translationMatrix.setTranslation(vector3df(0, 3, 0));
  203. if (translationMatrix.isOrthogonal())
  204. {
  205. logTestString("irr::core::matrix4::isOrthogonal() failed with translation.\n");
  206. return false;
  207. }
  208. matrix4 scaleMatrix;
  209. scaleMatrix.setScale(vector3df(1, 2, 3));
  210. if (!scaleMatrix.isOrthogonal())
  211. {
  212. logTestString("irr::core::matrix4::isOrthogonal() failed with scale.\n");
  213. return false;
  214. }
  215. return true;
  216. }
  217. bool checkMatrixRotation(irr::core::matrix4& m, const vector3df& vector, const vector3df& expectedResult)
  218. {
  219. vector3df v(vector);
  220. m.rotateVect(v);
  221. if ( expectedResult.equals(v) )
  222. return true;
  223. logTestString("checkMatrixRotation failed for vector %f %f %f. Expected %f %f %f, got %f %f %f \n"
  224. , vector.X, vector.Y, vector.Z, expectedResult.X, expectedResult.Y, expectedResult.Z, v.X, v.Y, v.Z);
  225. logTestString("matrix: ");
  226. for ( int i=0; i<16; ++i )
  227. logTestString("%.2f ", m[i]);
  228. logTestString("\n");
  229. return false;
  230. }
  231. bool setRotationAxis()
  232. {
  233. matrix4 m;
  234. vector3df v;
  235. // y up, x right, z depth (as usual)
  236. // y rotated around x-axis
  237. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(1,0,0)), vector3df(0,1,0), vector3df(0, 0, 1)) )
  238. {
  239. logTestString("%s:%d", __FILE__, __LINE__);
  240. return false;
  241. }
  242. if ( !checkMatrixRotation( m.setRotationAxisRadians(180.f*DEGTORAD, vector3df(1,0,0)), vector3df(0,1,0), vector3df(0, -1, 0)) )
  243. {
  244. logTestString("%s:%d", __FILE__, __LINE__);
  245. return false;
  246. }
  247. // y rotated around negative x-axis
  248. m.makeIdentity();
  249. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(-1,0,0)), vector3df(0,1,0), vector3df(0, 0, -1)) )
  250. {
  251. logTestString("%s:%d", __FILE__, __LINE__);
  252. return false;
  253. }
  254. // x rotated around x-axis
  255. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(1,0,0)), vector3df(1,0,0), vector3df(1, 0, 0)) )
  256. {
  257. logTestString("%s:%d", __FILE__, __LINE__);
  258. return false;
  259. }
  260. // x rotated around y-axis
  261. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,1,0)), vector3df(1,0,0), vector3df(0, 0, -1)) )
  262. {
  263. logTestString("%s:%d", __FILE__, __LINE__);
  264. return false;
  265. }
  266. if ( !checkMatrixRotation( m.setRotationAxisRadians(180.f*DEGTORAD, vector3df(0,1,0)), vector3df(1,0,0), vector3df(-1, 0, 0)) )
  267. {
  268. logTestString("%s:%d", __FILE__, __LINE__);
  269. return false;
  270. }
  271. // x rotated around negative y-axis
  272. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,-1,0)), vector3df(1,0,0), vector3df(0, 0, 1)) )
  273. {
  274. logTestString("%s:%d", __FILE__, __LINE__);
  275. return false;
  276. }
  277. // y rotated around y-axis
  278. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,1,0)), vector3df(0,1,0), vector3df(0, 1, 0)) )
  279. {
  280. logTestString("%s:%d", __FILE__, __LINE__);
  281. return false;
  282. }
  283. // x rotated around z-axis
  284. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,0,1)), vector3df(1,0,0), vector3df(0, 1, 0)) )
  285. {
  286. logTestString("%s:%d", __FILE__, __LINE__);
  287. return false;
  288. }
  289. if ( !checkMatrixRotation( m.setRotationAxisRadians(180.f*DEGTORAD, vector3df(0,0,1)), vector3df(1,0,0), vector3df(-1, 0, 0)) )
  290. {
  291. logTestString("%s:%d", __FILE__, __LINE__);
  292. return false;
  293. }
  294. // x rotated around negative z-axis
  295. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,0,-1)), vector3df(1,0,0), vector3df(0, -1, 0)) )
  296. {
  297. logTestString("%s:%d", __FILE__, __LINE__);
  298. return false;
  299. }
  300. // y rotated around z-axis
  301. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,0,1)), vector3df(0,1,0), vector3df(-1, 0, 0)) )
  302. {
  303. logTestString("%s:%d", __FILE__, __LINE__);
  304. return false;
  305. }
  306. if ( !checkMatrixRotation( m.setRotationAxisRadians(180.f*DEGTORAD, vector3df(0,0,1)), vector3df(0,1,0), vector3df(0, -1, 0)) )
  307. {
  308. logTestString("%s:%d", __FILE__, __LINE__);
  309. return false;
  310. }
  311. // z rotated around z-axis
  312. if ( !checkMatrixRotation( m.setRotationAxisRadians(90.f*DEGTORAD, vector3df(0,0,1)), vector3df(0,0,1), vector3df(0, 0, 1)) )
  313. {
  314. logTestString("%s:%d", __FILE__, __LINE__);
  315. return false;
  316. }
  317. return true;
  318. }
  319. // just calling each function once to find compile problems
  320. void calltest()
  321. {
  322. matrix4 mat;
  323. matrix4 mat2(mat);
  324. f32& f1 = mat(0,0);
  325. const f32& f2 = mat(0,0);
  326. f32& f3 = mat[0];
  327. const f32& f4 = mat[0];
  328. mat = mat;
  329. mat = 1.f;
  330. const f32 * pf1 = mat.pointer();
  331. f32 * pf2 = mat.pointer();
  332. bool b = mat == mat2;
  333. b = mat != mat2;
  334. mat = mat + mat2;
  335. mat += mat2;
  336. mat = mat - mat2;
  337. mat -= mat2;
  338. mat.setbyproduct(mat, mat2);
  339. mat.setbyproduct_nocheck(mat, mat2);
  340. mat = mat * mat2;
  341. mat *= mat2;
  342. mat = mat * 10.f;
  343. mat *= 10.f;
  344. mat.makeIdentity();
  345. b = mat.isIdentity();
  346. b = mat.isOrthogonal();
  347. b = mat.isIdentity_integer_base ();
  348. mat.setTranslation(vector3df(1.f, 1.f, 1.f) );
  349. vector3df v1 = mat.getTranslation();
  350. mat.setInverseTranslation(vector3df(1.f, 1.f, 1.f) );
  351. mat.setRotationRadians(vector3df(1.f, 1.f, 1.f) );
  352. mat.setRotationDegrees(vector3df(1.f, 1.f, 1.f) );
  353. vector3df v2 = mat.getRotationDegrees();
  354. mat.setInverseRotationRadians(vector3df(1.f, 1.f, 1.f) );
  355. mat.setInverseRotationDegrees(vector3df(1.f, 1.f, 1.f) );
  356. mat.setRotationAxisRadians(1.f, vector3df(1.f, 1.f, 1.f) );
  357. mat.setScale(vector3df(1.f, 1.f, 1.f) );
  358. mat.setScale(1.f);
  359. vector3df v3 = mat.getScale();
  360. mat.inverseTranslateVect(v1);
  361. mat.inverseRotateVect(v1);
  362. mat.rotateVect(v1);
  363. mat.rotateVect(v1, v2);
  364. f32 fv3[3];
  365. mat.rotateVect(fv3, v1);
  366. mat.transformVect(v1);
  367. mat.transformVect(v1, v1);
  368. f32 fv4[4];
  369. mat.transformVect(fv4, v1);
  370. mat.transformVec3(fv3, fv3);
  371. mat.translateVect(v1);
  372. plane3df p1;
  373. mat.transformPlane(p1);
  374. mat.transformPlane(p1, p1);
  375. aabbox3df bb1;
  376. mat.transformBox(bb1);
  377. mat.transformBoxEx(bb1);
  378. mat.multiplyWith1x4Matrix(fv4);
  379. mat.makeInverse();
  380. b = mat.getInversePrimitive(mat2);
  381. b = mat.getInverse(mat2);
  382. mat.buildProjectionMatrixPerspectiveFovRH(1.f, 1.f, 1.f, 1000.f);
  383. mat.buildProjectionMatrixPerspectiveFovLH(1.f, 1.f, 1.f, 1000.f);
  384. mat.buildProjectionMatrixPerspectiveFovInfinityLH(1.f, 1.f, 1.f);
  385. mat.buildProjectionMatrixPerspectiveRH(100.f, 100.f, 1.f, 1000.f);
  386. mat.buildProjectionMatrixPerspectiveLH(10000.f, 10000.f, 1.f, 1000.f);
  387. mat.buildProjectionMatrixOrthoLH(10000.f, 10000.f, 1.f, 1000.f);
  388. mat.buildProjectionMatrixOrthoRH(10000.f, 10000.f, 1.f, 1000.f);
  389. mat.buildCameraLookAtMatrixLH(vector3df(1.f, 1.f, 1.f), vector3df(0.f, 0.f, 0.f), vector3df(0.f, 1.f, 0.f) );
  390. mat.buildCameraLookAtMatrixRH(vector3df(1.f, 1.f, 1.f), vector3df(0.f, 0.f, 0.f), vector3df(0.f, 1.f, 0.f) );
  391. mat.buildShadowMatrix(vector3df(1.f, 1.f, 1.f), p1);
  392. core::rect<s32> a1(0,0,100,100);
  393. mat.buildNDCToDCMatrix(a1, 1.f);
  394. mat.interpolate(mat2, 1.f);
  395. mat = mat.getTransposed();
  396. mat.getTransposed(mat2);
  397. mat.buildRotateFromTo(vector3df(1.f, 1.f, 1.f), vector3df(1.f, 1.f, 1.f));
  398. mat.setRotationCenter(vector3df(1.f, 1.f, 1.f), vector3df(1.f, 1.f, 1.f));
  399. mat.buildAxisAlignedBillboard(vector3df(1.f, 1.f, 1.f), vector3df(1.f, 1.f, 1.f), vector3df(1.f, 1.f, 1.f), vector3df(1.f, 1.f, 1.f), vector3df(1.f, 1.f, 1.f));
  400. mat.buildTextureTransform( 1.f,vector2df(1.f, 1.f), vector2df(1.f, 1.f), vector2df(1.f, 1.f));
  401. mat.setTextureRotationCenter( 1.f );
  402. mat.setTextureTranslate( 1.f, 1.f );
  403. mat.setTextureTranslateTransposed(1.f, 1.f);
  404. mat.setTextureScale( 1.f, 1.f );
  405. mat.setTextureScaleCenter( 1.f, 1.f );
  406. f32 fv16[16];
  407. mat.setM(fv16);
  408. mat.setDefinitelyIdentityMatrix(false);
  409. b = mat.getDefinitelyIdentityMatrix();
  410. b = mat.equals(mat2);
  411. f1 = f1+f2+f3+f4+*pf1+*pf2; // getting rid of unused variable warnings.
  412. }
  413. }
  414. bool matrixOps(void)
  415. {
  416. bool result = true;
  417. calltest();
  418. result &= identity();
  419. result &= rotations();
  420. result &= isOrthogonal();
  421. result &= transformations();
  422. result &= setRotationAxis();
  423. return result;
  424. }