MatrixTests.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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(
  74. "[[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, degreeToRadian(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, degreeToRadian(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, degreeToRadian(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), degreeToRadian(48.0f));
  125. Quaternion q2 = UNIT_QUATERNION;
  126. axisAngleQ(&q2, &V(0.0f, 1.0f, 0.0f), degreeToRadian(52.0f));
  127. Quaternion q3 = UNIT_QUATERNION;
  128. axisAngleQ(&q3, &V(0.0f, 0.0f, 1.0f), degreeToRadian(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, degreeToRadian(48.0f));
  138. rotateMatrixY(&check, degreeToRadian(52.0f));
  139. rotateMatrixZ(&check, degreeToRadian(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. }