demo_space.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. /*
  23. testing procedure:
  24. * create a bunch of random boxes
  25. * test for intersections directly, put results in n^2 array
  26. * get space to report collisions:
  27. - all correct collisions reported
  28. - no pair reported more than once
  29. - no incorrect collisions reported
  30. */
  31. #include <ode/ode.h>
  32. #include <drawstuff/drawstuff.h>
  33. #include "texturepath.h"
  34. #ifdef _MSC_VER
  35. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  36. #endif
  37. // select correct drawing functions
  38. #ifdef dDOUBLE
  39. #define dsDrawBox dsDrawBoxD
  40. #define dsDrawSphere dsDrawSphereD
  41. #define dsDrawCylinder dsDrawCylinderD
  42. #define dsDrawCapsule dsDrawCapsuleD
  43. #endif
  44. // some constants
  45. #define NUM 20 // number of boxes to test
  46. // collision objects and globals
  47. static dSpaceID space;
  48. static dGeomID geom[NUM];
  49. static dReal bounds[NUM][6];
  50. static size_t good_matrix[NUM][NUM]; // correct collision matrix
  51. static size_t test_matrix[NUM][NUM]; // testing collision matrix
  52. static size_t hits[NUM]; // number of collisions a box has
  53. static unsigned long seed=37;
  54. static void init_test()
  55. {
  56. int i,j;
  57. const dReal scale = 0.5;
  58. // set random boxes
  59. dRandSetSeed (seed);
  60. for (i=0; i < NUM; i++) {
  61. bounds[i][0] = dRandReal()*2-1;
  62. bounds[i][1] = bounds[i][0] + dRandReal()*scale;
  63. bounds[i][2] = dRandReal()*2-1;
  64. bounds[i][3] = bounds[i][2] + dRandReal()*scale;
  65. bounds[i][4] = dRandReal()*2;
  66. bounds[i][5] = bounds[i][4] + dRandReal()*scale;
  67. if (geom[i]) dGeomDestroy (geom[i]);
  68. geom[i] = dCreateBox (space,
  69. bounds[i][1] - bounds[i][0],
  70. bounds[i][3] - bounds[i][2],
  71. bounds[i][5] - bounds[i][4]);
  72. dGeomSetPosition (geom[i],
  73. (bounds[i][0] + bounds[i][1])*0.5,
  74. (bounds[i][2] + bounds[i][3])*0.5,
  75. (bounds[i][4] + bounds[i][5])*0.5);
  76. dGeomSetData (geom[i],(void*)(size_t)(i));
  77. }
  78. // compute all intersections and put the results in "good_matrix"
  79. for (i=0; i < NUM; i++) {
  80. for (j=0; j < NUM; j++) good_matrix[i][j] = 0;
  81. }
  82. for (i=0; i < NUM; i++) hits[i] = 0;
  83. for (i=0; i < NUM; i++) {
  84. for (j=i+1; j < NUM; j++) {
  85. dReal *bounds1 = &bounds[i][0];
  86. dReal *bounds2 = &bounds[j][0];
  87. if (bounds1[0] > bounds2[1] ||
  88. bounds1[1] < bounds2[0] ||
  89. bounds1[2] > bounds2[3] ||
  90. bounds1[3] < bounds2[2] ||
  91. bounds1[4] > bounds2[5] ||
  92. bounds1[5] < bounds2[4]) continue;
  93. good_matrix[i][j] = 1;
  94. good_matrix[j][i] = 1;
  95. hits[i]++;
  96. hits[j]++;
  97. }
  98. }
  99. }
  100. // this is called by dSpaceCollide when two objects in space are
  101. // potentially colliding.
  102. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  103. {
  104. size_t i,j;
  105. i = (size_t) dGeomGetData (o1);
  106. j = (size_t) dGeomGetData (o2);
  107. if (i==j)
  108. printf ("collision (%d,%d) is between the same object\n",(int)i,(int)j);
  109. if (!good_matrix[i][j] || !good_matrix[j][i])
  110. printf ("collision (%d,%d) is incorrect\n",(int)i,(int)j);
  111. if (test_matrix[i][j] || test_matrix[j][i])
  112. printf ("collision (%d,%d) reported more than once\n",(int)i,(int)j);
  113. test_matrix[i][j] = 1;
  114. test_matrix[j][i] = 1;
  115. }
  116. // start simulation - set viewpoint
  117. static void start()
  118. {
  119. dAllocateODEDataForThread(dAllocateMaskAll);
  120. static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
  121. static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
  122. dsSetViewpoint (xyz,hpr);
  123. }
  124. static void command (int cmd)
  125. {
  126. if (cmd == ' ') {
  127. seed++;
  128. init_test();
  129. }
  130. }
  131. // simulation loop
  132. static void simLoop (int pause)
  133. {
  134. int i,j;
  135. for (i=0; i < NUM; i++) {
  136. for (j=0; j < NUM; j++) test_matrix[i][j] = 0;
  137. }
  138. dSpaceCollide (space,0,&nearCallback);
  139. for (i=0; i < NUM; i++) {
  140. for (j=i+1; j < NUM; j++) {
  141. if (good_matrix[i][j] && !test_matrix[i][j]) {
  142. printf ("failed to report collision (%d,%d) (seed=%ld)\n",i,j,seed);
  143. }
  144. }
  145. }
  146. seed++;
  147. init_test();
  148. for (i=0; i<NUM; i++) {
  149. dVector3 pos,side;
  150. dMatrix3 R;
  151. dRSetIdentity (R);
  152. for (j=0; j<3; j++) pos[j] = (bounds[i][j*2+1] + bounds[i][j*2]) * 0.5;
  153. for (j=0; j<3; j++) side[j] = bounds[i][j*2+1] - bounds[i][j*2];
  154. if (hits[i] > 0) dsSetColor (1,0,0);
  155. else dsSetColor (1,1,0);
  156. dsDrawBox (pos,R,side);
  157. }
  158. }
  159. int main (int argc, char **argv)
  160. {
  161. int i;
  162. // setup pointers to drawstuff callback functions
  163. dsFunctions fn;
  164. fn.version = DS_VERSION;
  165. fn.start = &start;
  166. fn.step = &simLoop;
  167. fn.command = &command;
  168. fn.stop = 0;
  169. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  170. dInitODE2(0);
  171. // test the simple space:
  172. // space = dSimpleSpaceCreate();
  173. // test the hash space:
  174. // space = dHashSpaceCreate (0);
  175. // dHashSpaceSetLevels (space,-10,10);
  176. // test the quadtree space
  177. dVector3 Center = {0, 0, 0, 0};
  178. dVector3 Extents = {10, 0, 10, 0};
  179. space = dQuadTreeSpaceCreate(0, Center, Extents, 7);
  180. for (i=0; i < NUM; i++) geom[i] = 0;
  181. init_test();
  182. // run simulation
  183. dsSimulationLoop (argc,argv,352,288,&fn);
  184. dSpaceDestroy (space);
  185. dCloseODE();
  186. return 0;
  187. }