Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ode/ode-0.9/ode/demo/demo_space.cpp @ 216

Last change on this file since 216 was 216, checked in by mathiask, 17 years ago

[Physik] add ode-0.9

File size: 6.4 KB
Line 
1/*************************************************************************
2 *                                                                       *
3 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith.       *
4 * All rights reserved.  Email: russ@q12.org   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/*
24
25testing procedure:
26  * create a bunch of random boxes
27  * test for intersections directly, put results in n^2 array
28  * get space to report collisions:
29    - all correct collisions reported
30    - no pair reported more than once
31    - no incorrect collisions reported
32
33*/
34
35
36#include <ode/ode.h>
37#include <drawstuff/drawstuff.h>
38
39#ifdef _MSC_VER
40#pragma warning(disable:4244 4305)  // for VC++, no precision loss complaints
41#endif
42
43// select correct drawing functions
44
45#ifdef dDOUBLE
46#define dsDrawBox dsDrawBoxD
47#define dsDrawSphere dsDrawSphereD
48#define dsDrawCylinder dsDrawCylinderD
49#define dsDrawCapsule dsDrawCapsuleD
50#endif
51
52
53// some constants
54
55#define NUM 20                  // number of boxes to test
56
57
58// collision objects and globals
59
60static dSpaceID space;
61static dGeomID geom[NUM];
62static dReal bounds[NUM][6];
63static size_t good_matrix[NUM][NUM];    // correct collision matrix
64static size_t test_matrix[NUM][NUM];    // testing collision matrix
65static size_t hits[NUM];                // number of collisions a box has
66static unsigned long seed=37;
67
68
69static void init_test()
70{
71  int i,j;
72  const dReal scale = 0.5;
73
74  // set random boxes
75  dRandSetSeed (seed);
76  for (i=0; i < NUM; i++) {
77    bounds[i][0] = dRandReal()*2-1;
78    bounds[i][1] = bounds[i][0] + dRandReal()*scale;
79    bounds[i][2] = dRandReal()*2-1;
80    bounds[i][3] = bounds[i][2] + dRandReal()*scale;
81    bounds[i][4] = dRandReal()*2;
82    bounds[i][5] = bounds[i][4] + dRandReal()*scale;
83
84    if (geom[i]) dGeomDestroy (geom[i]);
85    geom[i] = dCreateBox (space,
86                          bounds[i][1] - bounds[i][0],
87                          bounds[i][3] - bounds[i][2],
88                          bounds[i][5] - bounds[i][4]);
89    dGeomSetPosition (geom[i],
90                      (bounds[i][0] + bounds[i][1])*0.5,
91                      (bounds[i][2] + bounds[i][3])*0.5,
92                      (bounds[i][4] + bounds[i][5])*0.5);
93    dGeomSetData (geom[i],(void*)(size_t)(i));
94  }
95
96  // compute all intersections and put the results in "good_matrix"
97  for (i=0; i < NUM; i++) {
98    for (j=0; j < NUM; j++) good_matrix[i][j] = 0;
99  }
100  for (i=0; i < NUM; i++) hits[i] = 0;
101
102  for (i=0; i < NUM; i++) {
103    for (j=i+1; j < NUM; j++) {
104      dReal *bounds1 = &bounds[i][0];
105      dReal *bounds2 = &bounds[j][0];
106      if (bounds1[0] > bounds2[1] ||
107          bounds1[1] < bounds2[0] ||
108          bounds1[2] > bounds2[3] ||
109          bounds1[3] < bounds2[2] ||
110          bounds1[4] > bounds2[5] ||
111          bounds1[5] < bounds2[4]) continue;
112      good_matrix[i][j] = 1;
113      good_matrix[j][i] = 1;
114      hits[i]++;
115      hits[j]++;
116    }
117  }
118}
119
120
121// this is called by dSpaceCollide when two objects in space are
122// potentially colliding.
123
124static void nearCallback (void *data, dGeomID o1, dGeomID o2)
125{
126  size_t i,j;
127  i = (size_t) dGeomGetData (o1);
128  j = (size_t) dGeomGetData (o2);
129  if (i==j)
130    printf ("collision (%d,%d) is between the same object\n",i,j);
131  if (!good_matrix[i][j] || !good_matrix[j][i])
132    printf ("collision (%d,%d) is incorrect\n",i,j);
133  if (test_matrix[i][j] || test_matrix[j][i])
134    printf ("collision (%d,%d) reported more than once\n",i,j);
135  test_matrix[i][j] = 1;
136  test_matrix[j][i] = 1;
137}
138
139
140// start simulation - set viewpoint
141
142static void start()
143{
144  static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
145  static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
146  dsSetViewpoint (xyz,hpr);
147}
148
149
150static void command (int cmd)
151{
152  if (cmd == ' ') {
153    seed++;
154    init_test();
155  }
156}
157
158
159// simulation loop
160
161static void simLoop (int pause)
162{
163  int i,j;
164
165  for (i=0; i < NUM; i++) {
166    for (j=0; j < NUM; j++) test_matrix[i][j] = 0;
167  }
168  dSpaceCollide (space,0,&nearCallback);
169  for (i=0; i < NUM; i++) {
170    for (j=i+1; j < NUM; j++) {
171      if (good_matrix[i][j] && !test_matrix[i][j]) {
172        printf ("failed to report collision (%d,%d) (seed=%ld)\n",i,j,seed);
173      }
174    }
175  }
176
177  seed++;
178  init_test();
179
180  for (i=0; i<NUM; i++) {
181    dVector3 pos,side;
182    dMatrix3 R;
183    dRSetIdentity (R);
184    for (j=0; j<3; j++) pos[j] = (bounds[i][j*2+1] + bounds[i][j*2]) * 0.5;
185    for (j=0; j<3; j++) side[j] = bounds[i][j*2+1] - bounds[i][j*2];
186    if (hits[i] > 0) dsSetColor (1,0,0);
187    else dsSetColor (1,1,0);
188    dsDrawBox (pos,R,side);
189  }
190}
191
192
193int main (int argc, char **argv)
194{
195  int i;
196
197  // setup pointers to drawstuff callback functions
198  dsFunctions fn;
199  fn.version = DS_VERSION;
200  fn.start = &start;
201  fn.step = &simLoop;
202  fn.command = &command;
203  fn.stop = 0;
204  fn.path_to_textures = "../../drawstuff/textures";
205  if(argc==2)
206    {
207        fn.path_to_textures = argv[1];
208    }
209
210  dInitODE();
211
212  // test the simple space:
213  // space = dSimpleSpaceCreate();
214
215  // test the hash space:
216  // space = dHashSpaceCreate (0);
217  // dHashSpaceSetLevels (space,-10,10);
218
219  // test the quadtree space
220  dVector3 Center = {0, 0, 0, 0};
221  dVector3 Extents = {10, 0, 10, 0};
222  space = dQuadTreeSpaceCreate(0, Center, Extents, 7);
223
224  for (i=0; i < NUM; i++) geom[i] = 0;
225  init_test();
226
227  // run simulation
228  dsSimulationLoop (argc,argv,352,288,&fn);
229
230  dSpaceDestroy (space);
231  dCloseODE();
232  return 0;
233}
Note: See TracBrowser for help on using the repository browser.