MatrixTests.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "tests/MatrixTests.h"
  2. #include "math/Matrix.h"
  3. #include "test/Test.h"
  4. using V3 = Core::Vector3;
  5. static void testInit() {
  6. Core::Matrix m;
  7. const float* data = m.getValues();
  8. for(int i = 0; i < 16; i++) {
  9. int x = i % 4;
  10. int y = i / 4;
  11. CORE_TEST_FLOAT(x == y, data[i], 0.0f);
  12. }
  13. }
  14. static void testTranspose() {
  15. Core::Matrix m;
  16. m.set(0, Core::Vector4(1.0f, 2.0f, 3.0f, 4.0f));
  17. m.set(1, Core::Vector4(5.0f, 6.0f, 7.0f, 8.0f));
  18. m.set(2, Core::Vector4(9.0f, 10.0f, 11.0f, 12.0f));
  19. m.set(3, Core::Vector4(13.0f, 14.0f, 15.0f, 16.0f));
  20. Core::Matrix t = m.transpose();
  21. Core::Matrix m2 = t.transpose();
  22. const float* mp = m.getValues();
  23. const float* tp = t.getValues();
  24. for(int x = 0; x < 4; x++) {
  25. for(int y = 0; y < 4; y++) {
  26. CORE_TEST_FLOAT(mp[y * 4 + x], tp[x * 4 + y], 0.0f);
  27. }
  28. }
  29. const float* mp2 = m2.getValues();
  30. for(int i = 0; i < 16; i++) {
  31. CORE_TEST_FLOAT(mp[i], mp2[i], 0.0f);
  32. }
  33. }
  34. static void testScale() {
  35. Core::Matrix m;
  36. m.scale(V3(2.0f, 3.0f, 4.0f));
  37. CORE_TEST_VECTOR(V3(-8.0f, 18.0f, 28.0f), m * V3(-4.0f, 6.0f, 7.0f));
  38. }
  39. static void testUniformScale() {
  40. Core::Matrix m;
  41. m.scale(2.0f);
  42. CORE_TEST_VECTOR(V3(-8.0f, 12.0f, 14.0f), m * V3(-4.0f, 6.0f, 7.0f));
  43. }
  44. static void testTranslateX() {
  45. Core::Matrix m;
  46. m.translateX(5.0f);
  47. CORE_TEST_VECTOR(V3(1.0f, 6.0f, 7.0f), m * V3(-4.0f, 6.0f, 7.0f));
  48. }
  49. static void testTranslateY() {
  50. Core::Matrix m;
  51. m.translateY(6.0f);
  52. CORE_TEST_VECTOR(V3(-4.0f, 12.0f, 7.0f), m * V3(-4.0f, 6.0f, 7.0f));
  53. }
  54. static void testTranslateZ() {
  55. Core::Matrix m;
  56. m.translateZ(7.0f);
  57. CORE_TEST_VECTOR(V3(-4.0f, 6.0f, 14.0f), m * V3(-4.0f, 6.0f, 7.0f));
  58. }
  59. static void testTranslate() {
  60. Core::Matrix m;
  61. m.translate(V3(1.0f, 2.0f, 3.0f));
  62. CORE_TEST_VECTOR(V3(-3.0f, 8.0f, 10.0f), m * V3(-4.0f, 6.0f, 7.0f));
  63. }
  64. static void testCombination() {
  65. Core::Matrix m;
  66. m.scale(2.0f);
  67. m.translateX(1.0f);
  68. m.translateY(2.0f);
  69. m.translateZ(3.0f);
  70. m.translate(V3(-4.0f, 2.0f, 3.0f));
  71. m.scale(V3(2.0f, 3.0f, 4.0f));
  72. m.scale(0.5f);
  73. CORE_TEST_VECTOR(V3(-1.0f, 9.0f, 16.0f), m * V3(1.0f, 1.0f, 1.0f));
  74. }
  75. static void testMatrixCombination() {
  76. Core::Matrix a;
  77. a.scale(2.0f);
  78. a.translate(V3(1.0f, 2.0f, 3.0f));
  79. Core::Matrix b;
  80. b.scale(3.0f);
  81. b.translate(V3(1.0f, 1.0f, 1.0f));
  82. Core::Matrix c;
  83. c.translate(V3(-1.0f, -2.0f, -3.0f));
  84. c *= b * a;
  85. CORE_TEST_VECTOR(V3(9.0f, 11.0f, 13.0f), c * V3(1.0f, 1.0f, 1.0f));
  86. }
  87. static void testRotateX() {
  88. Core::Matrix m;
  89. m.rotateX(90);
  90. CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), m * V3(1.0f, 0.0f, 0.0f));
  91. CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), m * V3(0.0f, 1.0f, 0.0f));
  92. CORE_TEST_VECTOR(V3(0.0f, -1.0f, 0.0f), m * V3(0.0f, 0.0f, 1.0f));
  93. }
  94. static void testRotateY() {
  95. Core::Matrix m;
  96. m.rotateY(90);
  97. CORE_TEST_VECTOR(V3(0.0f, 0.0f, -1.0f), m * V3(1.0f, 0.0f, 0.0f));
  98. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), m * V3(0.0f, 1.0f, 0.0f));
  99. CORE_TEST_VECTOR(V3(1.0f, 0.0f, 0.0f), m * V3(0.0f, 0.0f, 1.0f));
  100. }
  101. static void testRotateZ() {
  102. Core::Matrix m;
  103. m.rotateZ(90);
  104. CORE_TEST_VECTOR(V3(0.0f, 1.0f, 0.0f), m * V3(1.0f, 0.0f, 0.0f));
  105. CORE_TEST_VECTOR(V3(-1.0f, 0.0f, 0.0f), m * V3(0.0f, 1.0f, 0.0f));
  106. CORE_TEST_VECTOR(V3(0.0f, 0.0f, 1.0f), m * V3(0.0f, 0.0f, 1.0f));
  107. }
  108. static void testToString() {
  109. Core::String32<1024> s;
  110. Core::Matrix m;
  111. m.set(0, Core::Vector4(1.0f, 2.0f, 3.0f, 4.0f));
  112. m.set(1, Core::Vector4(5.0f, 6.0f, 7.0f, 8.0f));
  113. m.set(2, Core::Vector4(9.0f, 10.0f, 11.0f, 12.0f));
  114. m.set(3, Core::Vector4(13.0f, 14.0f, 15.0f, 16.0f));
  115. CORE_TEST_ERROR(s.append(m));
  116. CORE_TEST_STRING(
  117. "[[1.00, 2.00, 3.00, 4.00], [5.00, 6.00, 7.00, 8.00], "
  118. "[9.00, 10.00, 11.00, 12.00], [13.00, 14.00, 15.00, 16.00]]",
  119. s);
  120. }
  121. static void testQuaternionMatrix() {
  122. Core::Quaternion q1(V3(1.0f, 0.0f, 0.0f), 48.0f);
  123. Core::Quaternion q2(V3(0.0f, 1.0f, 0.0f), 52.0f);
  124. Core::Quaternion q3(V3(0.0f, 0.0f, 1.0f), 60.0f);
  125. Core::Matrix m;
  126. m.translate(V3(1.0f, 2.0f, 3.0f));
  127. m.rotate(q1).rotate(q2).rotate(q3);
  128. m.translate(V3(1.0f, 2.0f, 3.0f));
  129. Core::Matrix check;
  130. check.translate(V3(1.0f, 2.0f, 3.0f));
  131. check.rotateX(48.0f).rotateY(52.0f).rotateZ(60.0f);
  132. check.translate(V3(1.0f, 2.0f, 3.0f));
  133. for(int i = 0; i < 16; i++) {
  134. CORE_TEST_FLOAT(check.getValues()[i], m.getValues()[i], 0.0001f);
  135. }
  136. }
  137. void Core::MatrixTests::test() {
  138. testInit();
  139. testScale();
  140. testUniformScale();
  141. testTranspose();
  142. testTranslateX();
  143. testTranslateY();
  144. testTranslateZ();
  145. testTranslate();
  146. testCombination();
  147. testMatrixCombination();
  148. testRotateX();
  149. testRotateY();
  150. testRotateZ();
  151. testToString();
  152. testQuaternionMatrix();
  153. }