Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Matrix.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*!
  2. * \file test/Matrix.cpp
  3. * \brief Matrix Unit Test
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <strings.h>
  11. #include <Matrix.h>
  12. #include "greatest.h"
  13. #ifdef COLUMN_ORDER
  14. unsigned char i0 = 0, i1 = 4, i2 = 8; // i3 = 12
  15. unsigned char i4 = 1, i6 = 9; // i5 = 5, i7 = 13
  16. unsigned char i8 = 2, i9 = 6, i10 = 10; // i11 = 14
  17. unsigned char i12 = 3, i13 = 7, i14 = 11, i15 = 15;
  18. #else
  19. unsigned char i0 = 0, i1 = 1, i2 = 2; // i3 = 3
  20. unsigned char i4 = 4, i6 = 6; // i5 = 5, i7 = 7
  21. unsigned char i8 = 8, i9 = 9, i10 = 10; // i11 = 11
  22. unsigned char i12 = 12, i13 = 13, i14 = 14, i15 = 15;
  23. #endif
  24. vec_t initialValues[][4] = {
  25. { 45.0f, 90.0f, 180.0f, 270.0f },
  26. { 10.0, 20.0, 30.0, 40.0 }
  27. };
  28. TEST identity(vec_t init[4], int mode) {
  29. Matrix a, b, c;
  30. matrix_t m;
  31. // Fill A matrix with garbage
  32. m[ 0] = m[ 1] = m[ 2] = m[ 3] = init[0];
  33. m[ 4] = m[ 5] = m[ 6] = m[ 7] = init[1];
  34. m[ 8] = m[ 9] = m[10] = m[11] = init[2];
  35. m[12] = m[13] = m[14] = m[15] = init[3];
  36. a.setMatrix(m);
  37. switch (mode) {
  38. case 0:
  39. a.setIdentity();
  40. break;
  41. case 1:
  42. c.setIdentity();
  43. b.setIdentity();
  44. a = c * b;
  45. break;
  46. case 2:
  47. a.setIdentity();
  48. b.setIdentity();
  49. a = a * b;
  50. break;
  51. }
  52. ASSERT(a.isIdentity());
  53. PASS();
  54. }
  55. TEST translation() {
  56. Matrix a;
  57. a.setIdentity();
  58. a.translate(10, 20, 30);
  59. ASSERT(a.mMatrix[i12] == 10);
  60. ASSERT(a.mMatrix[i13] == 20);
  61. ASSERT(a.mMatrix[i14] == 30);
  62. PASS();
  63. }
  64. TEST rotation(int axis) {
  65. Matrix a;
  66. a.setIdentity();
  67. vec_t rot = 90 * 0.01745329251994329f;
  68. a.rotate((axis == 0) ? rot : 0, (axis == 1) ? rot : 0, (axis == 2) ? rot : 0);
  69. if (axis == 0) {
  70. ASSERT(a.mMatrix[i0] == 1);
  71. ASSERT(a.mMatrix[i15] == 1);
  72. ASSERT(a.mMatrix[i9] == -1);
  73. ASSERT(a.mMatrix[i6] == 1);
  74. } else if (axis == 1) {
  75. ASSERT(a.mMatrix[i8] == 1);
  76. ASSERT(a.mMatrix[i2] == -1);
  77. ASSERT(a.mMatrix[i15] == 1);
  78. } else if (axis == 2) {
  79. ASSERT(a.mMatrix[i4] == -1);
  80. ASSERT(a.mMatrix[i15] == 1);
  81. ASSERT(a.mMatrix[i1] == 1);
  82. ASSERT(a.mMatrix[i10] == 1);
  83. } else {
  84. FAIL();
  85. }
  86. PASS();
  87. }
  88. TEST precision() {
  89. Matrix a;
  90. printf("Prescision test...\n");
  91. a.setIdentity();
  92. printf(" -> Rotate (0, 0, 90 degrees)\n");
  93. a.rotate(0, 0, 90*0.01745329251994329f);
  94. printf(" -> Translate (10, 20, 30)\n");
  95. a.translate(10, 20, 30);
  96. printf(" -> scale (10, 10, 10)\n");
  97. a.scale(10, 10, 10);
  98. a.print();
  99. printf("\n -> scale (0.1, 0.1, 0.1)\n");
  100. a.scale(0.1, 0.1, 0.1);
  101. printf(" -> Translate (-10, -20, -30)\n");
  102. a.translate(-10, -20, -30);
  103. printf(" -> Rotate (0, 0, -90 degrees)\n");
  104. a.rotate(0, 0, -90*0.01745329251994329f);
  105. a.print();
  106. PASS();
  107. }
  108. SUITE(matrixSuite) {
  109. for (unsigned int i = 0; i < sizeof(initialValues) / sizeof(initialValues[0]); i++) {
  110. for (int mode = 0; mode < 3; mode++) {
  111. RUN_TESTp(identity, initialValues[i], mode);
  112. }
  113. }
  114. //! \fixme These tests are weak and only spot check some of the matrix -- Mongoose, 2003.06.18
  115. RUN_TEST(translation);
  116. RUN_TESTp(rotation, 0);
  117. RUN_TESTp(rotation, 1);
  118. RUN_TESTp(rotation, 2);
  119. RUN_TEST(precision);
  120. }
  121. GREATEST_MAIN_DEFS();
  122. int main(int argc, char *argv[]) {
  123. GREATEST_MAIN_BEGIN();
  124. RUN_SUITE(matrixSuite);
  125. GREATEST_MAIN_END();
  126. }