Open Source Tomb Raider Engine
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Matrix.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Freyja
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : Matrix
  9. * License : No use w/o permission (C) 2002 Mongoose
  10. * Comments: 3d Matrix class
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2002.05.11:
  19. * Mongoose - Created
  20. =================================================================*/
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <strings.h>
  24. #include <Matrix.h>
  25. /* <Order> is (r)ow or (c)ol */
  26. void generateMatrixSourceTest(char order)
  27. {
  28. int i, j, k;
  29. if (order == 'r')
  30. {
  31. printf("/* Row order */\n");
  32. }
  33. else
  34. {
  35. printf("/* Column order */\n");
  36. }
  37. for (i = 0; i < 4; ++i)
  38. {
  39. for (j = 0; j < 4; ++j)
  40. {
  41. if (order == 'r')
  42. {
  43. printf("result[%2i] = ", j+i*4);
  44. }
  45. else
  46. {
  47. printf("result[%2i] = ", j+i*4);
  48. }
  49. for (k = 0; k < 4; ++k)
  50. {
  51. if (order == 'r')
  52. {
  53. printf("a[%2i] * b[%2i]%s",
  54. k+i*4, j+k*4, (k == 3) ? ";\n" : " + ");
  55. }
  56. else
  57. {
  58. printf("a[%2i] * b[%2i]%s",
  59. i+k*4, k+j*4, (k == 3) ? ";\n" : " + ");
  60. }
  61. //sum+=(elements[i+k*4]*m.elements[k+j*4]);
  62. }
  63. //result.elements[i+j*4]=sum;
  64. }
  65. printf("\n");
  66. }
  67. printf("\n");
  68. printf("/* Transpose */\n");
  69. for(i = 0; i < 4; ++i)
  70. {
  71. for (j = 0; j < 4; ++j)
  72. {
  73. printf("a[%2i] = b[%2i]%s",
  74. j+i*4, i+j*4, (j == 3) ? ";\n" : "; ");
  75. }
  76. }
  77. }
  78. int runMatrixUnitTest()
  79. {
  80. unsigned int i, errs;
  81. Matrix a, b, c;
  82. matrix_t m;
  83. // Test 3 cases of identity use
  84. for (errs = 0, i = 0; i < 3; ++i)
  85. {
  86. // Fill A matrix with garbage
  87. m[ 0] = m[ 1] = m[ 2] = m[ 3] = 45.0f;
  88. m[ 4] = m[ 5] = m[ 6] = m[ 7] = 90.0f;
  89. m[ 8] = m[ 9] = m[10] = m[11] = 180.0f;
  90. m[12] = m[13] = m[14] = m[15] = 270.0f;
  91. a.setMatrix(m);
  92. switch (i)
  93. {
  94. case 0:
  95. printf("Set to Identity");
  96. a.setIdentity();
  97. break;
  98. case 1:
  99. printf("Identity * Identity");
  100. c.setIdentity();
  101. b.setIdentity();
  102. a = c * b;
  103. break;
  104. case 2:
  105. printf("Identity *= Identity");
  106. a.setIdentity();
  107. b.setIdentity();
  108. a = a * b;
  109. break;
  110. }
  111. if (a.isIdentity())
  112. {
  113. printf(" \t[ Passed ]\n");
  114. }
  115. else
  116. {
  117. ++errs;
  118. printf(" \t[ Failed ]\a\n"); // beep
  119. a.print();
  120. }
  121. printf("\n");
  122. }
  123. /* 2003.06.18, Mongoose - These tests are weak and
  124. only spot check some of the matrix */
  125. /* Cheap translation test */
  126. a.setIdentity();
  127. printf("I -> Translate (10, 20, 30)\n");
  128. a.translate(10, 20, 30);
  129. #ifdef COLUMN_ORDER
  130. unsigned char i0 = 0, i1 = 4, i2 = 8, i3 = 12;
  131. unsigned char i4 = 1, i5 = 5, i6 = 9, i7 = 13;
  132. unsigned char i8 = 2, i9 = 6, i10 = 10, i11 = 14;
  133. unsigned char i12 = 3, i13 = 7, i14 = 11, i15 = 15;
  134. #else
  135. unsigned char i0 = 0, i1 = 1, i2 = 2; // i3 = 3
  136. unsigned char i4 = 4, i6 = 6; // i5 = 5, i7 = 7
  137. unsigned char i8 = 8, i9 = 9, i10 = 10; // i11 = 11
  138. unsigned char i12 = 12, i13 = 13, i14 = 14, i15 = 15;
  139. #endif
  140. if (a.mMatrix[i12] != 10 ||
  141. a.mMatrix[i13] != 20 ||
  142. a.mMatrix[i14] != 30)
  143. {
  144. ++errs;
  145. printf(" \t[ Failed ]\a\n"); // beep
  146. a.print();
  147. }
  148. /* Cheap X rotation test */
  149. a.setIdentity();
  150. printf("I -> Rotate (90 degrees, 0, 0)\n");
  151. a.rotate(90*0.01745329251994329f, 0, 0);
  152. if (a.mMatrix[i0] != 1 || a.mMatrix[i15] != 1 ||
  153. a.mMatrix[i9] != -1 || a.mMatrix[i6] != 1)
  154. {
  155. ++errs;
  156. printf(" \t[ Failed ]\a\n"); // beep
  157. a.print();
  158. }
  159. /* Cheap Y rotation test */
  160. a.setIdentity();
  161. printf("I -> Rotate (0, 90 degrees, 0)\n");
  162. a.rotate(0, 90*0.01745329251994329f, 0);
  163. if (a.mMatrix[i8] != 1 || a.mMatrix[i2] != -1 ||
  164. a.mMatrix[i15] != 1)
  165. {
  166. ++errs;
  167. printf(" \t[ Failed ]\a\n"); // beep
  168. a.print();
  169. }
  170. /* Cheap Z rotation test */
  171. a.setIdentity();
  172. printf("I -> Rotate (0, 0, 90 degrees)\n");
  173. a.rotate(0, 0, 90*0.01745329251994329f);
  174. if (a.mMatrix[i4] != -1 || a.mMatrix[i15] != 1 ||
  175. a.mMatrix[i1] != 1 || a.mMatrix[i10] != 1)
  176. {
  177. ++errs;
  178. printf(" \t[ Failed ]\a\n"); // beep
  179. a.print();
  180. }
  181. printf("\n%i errors\n", errs);
  182. printf("\n");
  183. printf("Prescision test...\n");
  184. printf("I ->\n");
  185. a.setIdentity();
  186. printf(" -> Rotate (0, 0, 90 degrees)\n");
  187. a.rotate(0, 0, 90*0.01745329251994329f);
  188. printf(" -> Translate (10, 20, 30)\n");
  189. a.translate(10, 20, 30);
  190. printf(" -> scale (10, 10, 10)\n");
  191. a.scale(10, 10, 10);
  192. a.print();
  193. printf("\n");
  194. printf(" -> scale (0.1, 0.1, 0.1)\n");
  195. a.scale(0.1, 0.1, 0.1);
  196. printf(" -> Translate (-10, -20, -30)\n");
  197. a.translate(-10, -20, -30);
  198. printf(" -> Rotate (0, 0, -90 degrees)\n");
  199. a.rotate(0, 0, -90*0.01745329251994329f);
  200. a.print();
  201. printf("\n%i errors\n", errs);
  202. return errs;
  203. }
  204. int main(int argc, char *argv[])
  205. {
  206. if (argc > 2)
  207. {
  208. if (strcmp(argv[1], "-src") == 0)
  209. {
  210. generateMatrixSourceTest(argv[2][0]);
  211. return 0;
  212. }
  213. }
  214. printf("[Matrix class test]\n");
  215. runMatrixUnitTest();
  216. return 0;
  217. }