aaboxtest.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath Test Program *
  23. * *
  24. * $Archive:: /Commando/Code/Tests/mathtest/aaboxtest.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 3/13/00 4:33p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "aaboxtest.h"
  36. #include "output.h"
  37. #include "vector3.h"
  38. #include "tri.h"
  39. #include "aabox.h"
  40. #include "wwmath.h"
  41. #include "colmath.h"
  42. #include "p_timer.h"
  43. #include <stdio.h>
  44. class AABoxTriTestClass
  45. {
  46. public:
  47. AABoxClass Box;
  48. Vector3 BoxMove;
  49. Vector3 V0;
  50. Vector3 V1;
  51. Vector3 V2;
  52. Vector3 N;
  53. TriClass Tri;
  54. float Fraction;
  55. bool StartBad;
  56. AABoxTriTestClass
  57. (
  58. const Vector3 c, // center of box
  59. const Vector3 e, // extent of box
  60. const Vector3 m, // move for the box
  61. const Vector3 v0, // v0 of triangle
  62. const Vector3 v1, // v1 of triangle
  63. const Vector3 v2, // v2 of triangle
  64. float frac, // expected fraction
  65. bool sol // expected start solid
  66. )
  67. {
  68. BoxMove = m;
  69. V0 = v0;
  70. V1 = v1;
  71. V2 = v2;
  72. Fraction = frac;
  73. StartBad = sol;
  74. /*
  75. ** Initialize the triangle
  76. */
  77. Tri.V[0] = &V0;
  78. Tri.V[1] = &V1;
  79. Tri.V[2] = &V2;
  80. Tri.N = & N;
  81. Tri.Compute_Normal();
  82. /*
  83. ** Initialize the box
  84. */
  85. Box.Center = c;
  86. Box.Extent = e;
  87. }
  88. };
  89. AABoxTriTestClass Test0
  90. (
  91. Vector3(0,0,0), // box starting at origing
  92. Vector3(2,1,1), // 2 units along x, 1 y, 1 z
  93. Vector3(1,0,0), // moving 5 along x axis
  94. Vector3(6,-3,-1), // triangle crossing x and y extent but not colliding
  95. Vector3(8,-1,2),
  96. Vector3(9,0,-1),
  97. 1.0f,
  98. false
  99. );
  100. AABoxTriTestClass Test1
  101. (
  102. Vector3(3,0,0),
  103. Vector3(1,2,1),
  104. Vector3(0,-2,0),
  105. Vector3(1,-3,0),
  106. Vector3(2,-3,5),
  107. Vector3(6,-3,1),
  108. 0.5f,
  109. false
  110. );
  111. AABoxTriTestClass Test2
  112. (
  113. Vector3(-3.5,-1.5,0), // sweeping a 3x3 box along pos x and neg y
  114. Vector3(1.5,1.5,1.5),
  115. Vector3(4,-4,0),
  116. Vector3(-4,-4,-1), // into a polygon in y-z plane
  117. Vector3(-2,-4,5),
  118. Vector3(0,-4,1),
  119. 0.25f, // should only move 25%
  120. false
  121. );
  122. AABoxTriTestClass Test3
  123. (
  124. Vector3(-3.5,-1.5,0) + 0.25f * Vector3(4,-4,0),
  125. Vector3(1.5,1.5,1.5), // starting at end of test2's move, should be 0.0 but not StartBad!
  126. Vector3(4,-4,0),
  127. Vector3(-4,-4,-1), // into a polygon in y-z plane
  128. Vector3(-2,-4,5),
  129. Vector3(0,-4,1),
  130. 0.0f,
  131. false
  132. );
  133. AABoxTriTestClass Test4
  134. (
  135. Vector3(-3.5f,-1.5f,0), // Same as test 2
  136. Vector3(1.5f,1.5f,1.5f),
  137. Vector3(4,-4,0),
  138. Vector3(-9,-4,-1), // into a polygon in y-z plane *but* just barely not in the way
  139. Vector3(-8,-4,5),
  140. Vector3(-4.001f,-4,0),
  141. 1.0f, // should move 100%
  142. false
  143. );
  144. AABoxTriTestClass Test5
  145. (
  146. Vector3(-3.5,-1.5,0), // Same as test 3 with box brushing polygon vertex.
  147. Vector3(1.5,1.5,1.5),
  148. Vector3(4,-4,0),
  149. Vector3(-9,-4,-1), // into a polygon in y-z plane just touching path of box
  150. Vector3(-8,-4,5),
  151. Vector3(-4,-4,0),
  152. 1.0f, // should move 100%
  153. false
  154. );
  155. AABoxTriTestClass Test6
  156. (
  157. Vector3(-3.5,-1.5,0), // Same as test 3 with box brushing polygon vertex.
  158. Vector3(1.5,1.5,1.5),
  159. Vector3(4,-4,0),
  160. Vector3(-9,-4,-1), // into a polygon in y-z plane just touching path of box
  161. Vector3(-8,-4,5),
  162. Vector3(-4,-4,0),
  163. 1.0f, // should move 100%
  164. false
  165. );
  166. AABoxTriTestClass Test7
  167. (
  168. Vector3(0,0,0), // This is a case where the box starts out intersecting
  169. Vector3(5,5,5),
  170. Vector3(4,4,0),
  171. Vector3(1,4,-1),
  172. Vector3(2,4,5),
  173. Vector3(5,4,0),
  174. 0.0f,
  175. true
  176. );
  177. AABoxTriTestClass Test8
  178. (
  179. Vector3(-2.5,2,0),
  180. Vector3(1.5,1,1),
  181. Vector3(3,0,0),
  182. Vector3(1,2,0),
  183. Vector3(3,4,5),
  184. Vector3(4,5,-1),
  185. 0.66666667f,
  186. true
  187. );
  188. AABoxTriTestClass * AABoxTriTestCases[] =
  189. {
  190. &Test0,
  191. &Test1,
  192. &Test2,
  193. &Test3,
  194. &Test4,
  195. &Test5,
  196. &Test6,
  197. &Test7,
  198. &Test8
  199. };
  200. void Test_AABoxes(void)
  201. {
  202. int i;
  203. Print_Title("Testing AABox volume sweeping.");
  204. /*
  205. ** Test the sweep function with a bunch of boxes and triangles
  206. */
  207. CastResultStruct result;
  208. int numtests = sizeof(AABoxTriTestCases)/sizeof(AABoxTriTestClass *);
  209. unsigned cycles;
  210. // prime the cache
  211. AABoxTriTestClass * testcase = AABoxTriTestCases[4];
  212. cycles = Get_CPU_Clock();
  213. CollisionMath::Collide(testcase->Box,testcase->BoxMove,testcase->Tri,&result);
  214. cycles = Get_CPU_Clock() - cycles;
  215. // Now time and test the routine. Repeating and averaging time for each test
  216. float * cycle_counts = new float[numtests];
  217. for (i=0; i<numtests;i++) {
  218. cycle_counts[i] = 0.0f;
  219. }
  220. int num_repetitions = 50;
  221. for (int repeat = 0; repeat < num_repetitions; repeat++) {
  222. for (i=0; i<numtests; i++) {
  223. testcase = AABoxTriTestCases[i];
  224. result.Fraction = 1.0;
  225. result.StartBad = false;
  226. result.Normal.Set(0,0,0);
  227. cycles = Get_CPU_Clock();
  228. CollisionMath::Collide(testcase->Box,testcase->BoxMove,testcase->Tri,&result);
  229. cycles = Get_CPU_Clock() - cycles;
  230. printf("AABox -> Tri cycles: %d\n",cycles);
  231. cycle_counts[i] += cycles;
  232. if ((WWMath::Fabs(testcase->Fraction - result.Fraction) > WWMATH_EPSILON) ||
  233. (testcase->StartBad != result.StartBad))
  234. {
  235. printf("test %d failed!\n",i);
  236. } else {
  237. printf("test %d passed...\n",i);
  238. }
  239. }
  240. }
  241. printf("\n");
  242. for (i=0; i<numtests;i++) {
  243. cycle_counts[i] /= (float)num_repetitions;
  244. printf("Test%d Average Cycles: %f\r\n",i,cycle_counts[i]);
  245. }
  246. /*
  247. ** Test a box moving down the z-axis to a polygon, then moving along
  248. ** the x-axis along the surface of the polygon.
  249. */
  250. AABoxClass testbox;
  251. Vector3 move;
  252. Vector3 v0,v1,v2,n;
  253. TriClass testtri;
  254. v0.Set(0,1,0);
  255. v1.Set(-1,-1,0);
  256. v2.Set(1,1,0);
  257. testtri.V[0] = &v0;
  258. testtri.V[1] = &v1;
  259. testtri.V[2] = &v2;
  260. testtri.N = &n;
  261. testtri.Compute_Normal();
  262. testbox.Center.Set(0,0,2.363f);
  263. testbox.Extent.Set(1,1,1);
  264. move.Set(0,0,-5.0f);
  265. CastResultStruct cres;
  266. cres.StartBad = 0;
  267. cres.Fraction = 1.0f;
  268. CollisionMath::Collide(testbox,move,testtri,&cres);
  269. printf("fraction = %f\n",cres.Fraction);
  270. testbox.Center += cres.Fraction * move;
  271. move.Set(1.0f,1.0f,0.0f);
  272. cres.StartBad = 0;
  273. cres.Fraction = 1.0f;
  274. CollisionMath::Collide(testbox,move,testtri,&cres);
  275. printf("fraction = %f\n",cres.Fraction);
  276. testbox.Center += cres.Fraction * move;
  277. }