MatrixTests.c 5.6 KB

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