MatrixTests.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "../Tests.h"
  2. #include "core/Generic.h"
  3. #include "core/Utility.h"
  4. static void testInit() {
  5. Matrix m = UNIT_MATRIX;
  6. const float* data = (float*)&m;
  7. for(int i = 0; i < 16; i++) {
  8. int x = i % 4;
  9. int y = i / 4;
  10. TEST_FLOAT(x == y, data[i], 0.0f);
  11. }
  12. }
  13. static void testTranspose() {
  14. Matrix m;
  15. float* data = (float*)&m;
  16. for(int i = 0; i < 16; i++) {
  17. data[i] = (float)(i + 1);
  18. }
  19. Matrix t = m;
  20. transposeMatrix(&t);
  21. Matrix m2 = t;
  22. transposeMatrix(&m2);
  23. const float* mp = (float*)&m;
  24. const float* tp = (float*)&t;
  25. for(int x = 0; x < 4; x++) {
  26. for(int y = 0; y < 4; y++) {
  27. TEST_FLOAT(mp[y * 4 + x], tp[x * 4 + y], 0.0f);
  28. }
  29. }
  30. const float* mp2 = (float*)&m2;
  31. for(int i = 0; i < 16; i++) {
  32. TEST_FLOAT(mp[i], mp2[i], 0.0f);
  33. }
  34. }
  35. static void testScale() {
  36. Matrix m = UNIT_MATRIX;
  37. scaleMatrix(&m, &V(2.0f, 3.0f, 4.0f));
  38. TEST_V3(&V(-8.0f, 18.0f, 28.0f), mul(&m, &V(-4.0f, 6.0f, 7.0f)));
  39. }
  40. static void testUniformScale() {
  41. Matrix m = UNIT_MATRIX;
  42. scaleMatrixF(&m, 2.0f);
  43. TEST_V3(&V(-8.0f, 12.0f, 14.0f), mul(&m, &V(-4.0f, 6.0f, 7.0f)));
  44. }
  45. static void testTranslateX() {
  46. Matrix m = UNIT_MATRIX;
  47. translateMatrixX(&m, 5.0f);
  48. TEST_V3(&V(1.0f, 6.0f, 7.0f), mul(&m, &V(-4.0f, 6.0f, 7.0f)));
  49. }
  50. static void testTranslateY() {
  51. Matrix m = UNIT_MATRIX;
  52. translateMatrixY(&m, 6.0f);
  53. TEST_V3(&V(-4.0f, 12.0f, 7.0f), mul(&m, &V(-4.0f, 6.0f, 7.0f)));
  54. }
  55. static void testTranslateZ() {
  56. Matrix m = UNIT_MATRIX;
  57. translateMatrixZ(&m, 7.0f);
  58. TEST_V3(&V(-4.0f, 6.0f, 14.0f), mul(&m, &V(-4.0f, 6.0f, 7.0f)));
  59. }
  60. static void testTranslate() {
  61. Matrix m = UNIT_MATRIX;
  62. translateMatrix(&m, &V(1.0f, 2.0f, 3.0f));
  63. TEST_V3(&V(-3.0f, 8.0f, 10.0f), mul(&m, &V(-4.0f, 6.0f, 7.0f)));
  64. }
  65. static void testTranslateTo() {
  66. char buffer[1024];
  67. Matrix m;
  68. for(int i = 0; i < 16; i++) {
  69. ((float*)&m)[i] = (float)i + 1.0f;
  70. }
  71. translateMatrixTo(&m, &V(6.0f, 8.0f, 9.0f));
  72. toStringMatrix(&m, buffer, sizeof(buffer));
  73. TEST_STRING("[[1.000, 0.000, 0.000, 6.000], [0.000, 1.000, 0.000, 8.000], "
  74. "[0.000, 0.000, 1.000, 9.000], [0.000, 0.000, 0.000, 1.000]]",
  75. buffer);
  76. }
  77. static void testCombination() {
  78. Matrix m = UNIT_MATRIX;
  79. scaleMatrixF(&m, 2.0f);
  80. translateMatrixX(&m, 1.0f);
  81. translateMatrixY(&m, 2.0f);
  82. translateMatrixZ(&m, 3.0f);
  83. translateMatrix(&m, &V(-4.0f, 2.0f, 3.0f));
  84. scaleMatrix(&m, &V(2.0f, 3.0f, 4.0f));
  85. scaleMatrixF(&m, 0.5f);
  86. TEST_V3(&V(-1.0f, 9.0f, 16.0f), mul(&m, &V(1.0f, 1.0f, 1.0f)));
  87. }
  88. static void testMatrixCombination() {
  89. Matrix a = UNIT_MATRIX;
  90. scaleMatrixF(&a, 2.0f);
  91. translateMatrix(&a, &V(1.0f, 2.0f, 3.0f));
  92. Matrix b = UNIT_MATRIX;
  93. scaleMatrixF(&b, 3.0f);
  94. translateMatrix(&b, &V(1.0f, 1.0f, 1.0f));
  95. Matrix c = UNIT_MATRIX;
  96. translateMatrix(&c, &V(-1.0f, -2.0f, -3.0f));
  97. mulSet(&c, mul(&ZERO_MATRIX, &b, &a));
  98. TEST_V3(&V(9.0f, 11.0f, 13.0f), mul(&c, &V(1.0f, 1.0f, 1.0f)));
  99. }
  100. static void testRotateX() {
  101. Matrix m = UNIT_MATRIX;
  102. rotateMatrixX(&m, degreeToRadian(90.0f));
  103. TEST_V3(&V(1.0f, 0.0f, 0.0f), mul(&m, &V(1.0f, 0.0f, 0.0f)));
  104. TEST_V3(&V(0.0f, 0.0f, 1.0f), mul(&m, &V(0.0f, 1.0f, 0.0f)));
  105. TEST_V3(&V(0.0f, -1.0f, 0.0f), mul(&m, &V(0.0f, 0.0f, 1.0f)));
  106. }
  107. static void testRotateY() {
  108. Matrix m = UNIT_MATRIX;
  109. rotateMatrixY(&m, degreeToRadian(90.0f));
  110. TEST_V3(&V(0.0f, 0.0f, -1.0f), mul(&m, &V(1.0f, 0.0f, 0.0f)));
  111. TEST_V3(&V(0.0f, 1.0f, 0.0f), mul(&m, &V(0.0f, 1.0f, 0.0f)));
  112. TEST_V3(&V(1.0f, 0.0f, 0.0f), mul(&m, &V(0.0f, 0.0f, 1.0f)));
  113. }
  114. static void testRotateZ() {
  115. Matrix m = UNIT_MATRIX;
  116. rotateMatrixZ(&m, degreeToRadian(90.0f));
  117. TEST_V3(&V(0.0f, 1.0f, 0.0f), mul(&m, &V(1.0f, 0.0f, 0.0f)));
  118. TEST_V3(&V(-1.0f, 0.0f, 0.0f), mul(&m, &V(0.0f, 1.0f, 0.0f)));
  119. TEST_V3(&V(0.0f, 0.0f, 1.0f), mul(&m, &V(0.0f, 0.0f, 1.0f)));
  120. }
  121. static void testQuaternionMatrix() {
  122. Quaternion q1 = UNIT_QUATERNION;
  123. axisAngleQ(&q1, &V(1.0f, 0.0f, 0.0f), degreeToRadian(48.0f));
  124. Quaternion q2 = UNIT_QUATERNION;
  125. axisAngleQ(&q2, &V(0.0f, 1.0f, 0.0f), degreeToRadian(52.0f));
  126. Quaternion q3 = UNIT_QUATERNION;
  127. axisAngleQ(&q3, &V(0.0f, 0.0f, 1.0f), degreeToRadian(60.0f));
  128. Matrix m = UNIT_MATRIX;
  129. translateMatrix(&m, &V(1.0f, 2.0f, 3.0f));
  130. rotateMatrix(&m, &q1);
  131. rotateMatrix(&m, &q2);
  132. rotateMatrix(&m, &q3);
  133. translateMatrix(&m, &V(1.0f, 2.0f, 3.0f));
  134. Matrix check = UNIT_MATRIX;
  135. translateMatrix(&check, &V(1.0f, 2.0f, 3.0f));
  136. rotateMatrixX(&check, degreeToRadian(48.0f));
  137. rotateMatrixY(&check, degreeToRadian(52.0f));
  138. rotateMatrixZ(&check, degreeToRadian(60.0f));
  139. translateMatrix(&check, &V(1.0f, 2.0f, 3.0f));
  140. for(int i = 0; i < 16; i++) {
  141. TEST_FLOAT(((float*)&check)[i], ((float*)&m)[i], 0.0001f);
  142. }
  143. }
  144. static void testToString() {
  145. Matrix m;
  146. for(int i = 0; i < 16; i++) {
  147. ((float*)&m)[i] = (float)i + 1.0f;
  148. }
  149. char buffer[1024];
  150. size_t n = toStringMatrix(&m, buffer, sizeof(buffer));
  151. TEST_SIZE(127, n);
  152. TEST_STRING(
  153. "[[1.000, 2.000, 3.000, 4.000], [5.000, 6.000, 7.000, 8.000], "
  154. "[9.000, 10.000, 11.000, 12.000], [13.000, 14.000, 15.000, 16.000]]",
  155. buffer);
  156. n = toStringMatrix(&m, buffer, 20);
  157. TEST_SIZE(127, n);
  158. TEST_STRING("[[1.000, 2.000, 3.0", buffer);
  159. }
  160. void testMatrix() {
  161. testInit();
  162. testTranspose();
  163. testScale();
  164. testUniformScale();
  165. testTranslateX();
  166. testTranslateY();
  167. testTranslateZ();
  168. testTranslate();
  169. testTranslateTo();
  170. testCombination();
  171. testMatrixCombination();
  172. testRotateX();
  173. testRotateY();
  174. testRotateZ();
  175. testQuaternionMatrix();
  176. testToString();
  177. }