MatrixTests.cpp 4.8 KB

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