Bullet Collision Detection & Physics Library
btGImpactBvh.cpp
Go to the documentation of this file.
1
4/*
5This source file is part of GIMPACT Library.
6
7For the latest info, see http://gimpact.sourceforge.net/
8
9Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
10email: projectileman@yahoo.com
11
12
13This software is provided 'as-is', without any express or implied warranty.
14In no event will the authors be held liable for any damages arising from the use of this software.
15Permission is granted to anyone to use this software for any purpose,
16including commercial applications, and to alter it and redistribute it freely,
17subject to the following restrictions:
18
191. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
202. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
213. This notice may not be removed or altered from any source distribution.
22*/
23#include "btGImpactBvh.h"
25
26#ifdef TRI_COLLISION_PROFILING
27
29
31int g_count_traversing = 0;
32
33
35{
37}
38
40{
41 g_accum_tree_collision_time += g_tree_clock.getTimeMicroseconds();
43}
44
46float btGImpactBvh::getAverageTreeCollisionTime()
47{
48 if(g_count_traversing == 0) return 0;
49
52
55 return avgtime;
56
57// float avgtime = g_count_traversing;
58// g_count_traversing = 0;
59// return avgtime;
60
61}
62
63#endif //TRI_COLLISION_PROFILING
64
66
69{
70
71 int i;
72
75 int numIndices = endIndex-startIndex;
76
77 for (i=startIndex;i<endIndex;i++)
78 {
79 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
80 primitive_boxes[i].m_bound.m_min);
81 means+=center;
82 }
83 means *= (btScalar(1.)/(btScalar)numIndices);
84
85 for (i=startIndex;i<endIndex;i++)
86 {
87 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
88 primitive_boxes[i].m_bound.m_min);
89 btVector3 diff2 = center-means;
90 diff2 = diff2 * diff2;
91 variance += diff2;
92 }
93 variance *= (btScalar(1.)/ ((btScalar)numIndices-1) );
94
95 return variance.maxAxis();
96}
97
98
101 int endIndex, int splitAxis)
102{
103 int i;
105 int numIndices = endIndex - startIndex;
106
107 // average of centers
108 btScalar splitValue = 0.0f;
109
111 for (i=startIndex;i<endIndex;i++)
112 {
113 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
114 primitive_boxes[i].m_bound.m_min);
115 means+=center;
116 }
117 means *= (btScalar(1.)/(btScalar)numIndices);
118
120
121
122 //sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
123 for (i=startIndex;i<endIndex;i++)
124 {
125 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
126 primitive_boxes[i].m_bound.m_min);
127 if (center[splitAxis] > splitValue)
128 {
129 //swap
131 //swapLeafNodes(i,splitIndex);
132 splitIndex++;
133 }
134 }
135
136 //if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
137 //otherwise the tree-building might fail due to stack-overflows in certain cases.
138 //unbalanced1 is unsafe: it can cause stack overflows
139 //bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
140
141 //unbalanced2 should work too: always use center (perfect balanced trees)
142 //bool unbalanced2 = true;
143
144 //this should be safe too:
145 int rangeBalancedIndices = numIndices/3;
147
148 if (unbalanced)
149 {
150 splitIndex = startIndex+ (numIndices>>1);
151 }
152
154
155 return splitIndex;
156
157}
158
159
161{
162 int curIndex = m_num_nodes;
163 m_num_nodes++;
164
166
167 if ((endIndex-startIndex)==1)
168 {
169 //We have a leaf node
171 m_node_array[curIndex].setDataIndex(primitive_boxes[startIndex].m_data);
172
173 return;
174 }
175 //calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
176
177 //split axis
179
182 splitIndex//split axis
183 );
184
185
186 //calc this node bounding box
187
190
191 for (int i=startIndex;i<endIndex;i++)
192 {
193 node_bound.merge(primitive_boxes[i].m_bound);
194 }
195
197
198
199 //build left branch
201
202
203 //build right branch
205
206 m_node_array[curIndex].setEscapeIndex(m_num_nodes - curIndex);
207
208
209}
210
214{
215 // initialize node count to 0
216 m_num_nodes = 0;
217 // allocate nodes
219
221}
222
224
226{
227 int nodecount = getNodeCount();
228 while(nodecount--)
229 {
231 {
235 }
236 else
237 {
238 //const GIM_BVH_TREE_NODE * nodepointer = get_node_pointer(nodecount);
239 //get left bound
242
244
246 if(child_node)
247 {
249 bound.merge(temp_box);
250 }
251
253 if(child_node)
254 {
256 bound.merge(temp_box);
257 }
258
260 }
261 }
262}
263
266{
267 //obtain primitive boxes
270
271 for (int i = 0;i<primitive_boxes.size() ;i++ )
272 {
274 primitive_boxes[i].m_data = i;
275 }
276
278}
279
282{
283 int curIndex = 0;
284 int numNodes = getNodeCount();
285
286 while (curIndex < numNodes)
287 {
290
291 //catch bugs in tree data
292
293 bool aabbOverlap = bound.has_collision(box);
295
296 if (isleafnode && aabbOverlap)
297 {
299 }
300
301 if (aabbOverlap || isleafnode)
302 {
303 //next subnode
304 curIndex++;
305 }
306 else
307 {
308 //skip node
310 }
311 }
312 if(collided_results.size()>0) return true;
313 return false;
314}
315
316
317
320 const btVector3 & ray_dir,const btVector3 & ray_origin ,
322{
323 int curIndex = 0;
324 int numNodes = getNodeCount();
325
326 while (curIndex < numNodes)
327 {
330
331 //catch bugs in tree data
332
333 bool aabbOverlap = bound.collide_ray(ray_origin,ray_dir);
335
336 if (isleafnode && aabbOverlap)
337 {
339 }
340
341 if (aabbOverlap || isleafnode)
342 {
343 //next subnode
344 curIndex++;
345 }
346 else
347 {
348 //skip node
350 }
351 }
352 if(collided_results.size()>0) return true;
353 return false;
354}
355
356
359 const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
360 int node0 ,int node1, bool complete_primitive_tests)
361{
362 btAABB box0;
363 boxset0->getNodeBound(node0,box0);
364 btAABB box1;
365 boxset1->getNodeBound(node1,box1);
366
368// box1.appy_transform_trans_cache(trans_cache_1to0);
369// return box0.has_collision(box1);
370
371}
372
373
374//stackless recursive collision routine
378 const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
379 int node0, int node1, bool complete_primitive_tests)
380{
381
382
383
384 if( _node_collision(
385 boxset0,boxset1,trans_cache_1to0,
386 node0,node1,complete_primitive_tests) ==false) return;//avoid colliding internal nodes
387
388 if(boxset0->isLeafNode(node0))
389 {
390 if(boxset1->isLeafNode(node1))
391 {
392 // collision result
393 collision_pairs->push_pair(
394 boxset0->getNodeData(node0),boxset1->getNodeData(node1));
395 return;
396 }
397 else
398 {
399
400 //collide left recursive
401
404 collision_pairs,trans_cache_1to0,
405 node0,boxset1->getLeftNode(node1),false);
406
407 //collide right recursive
410 collision_pairs,trans_cache_1to0,
411 node0,boxset1->getRightNode(node1),false);
412
413
414 }
415 }
416 else
417 {
418 if(boxset1->isLeafNode(node1))
419 {
420
421 //collide left recursive
424 collision_pairs,trans_cache_1to0,
425 boxset0->getLeftNode(node0),node1,false);
426
427
428 //collide right recursive
429
432 collision_pairs,trans_cache_1to0,
433 boxset0->getRightNode(node0),node1,false);
434
435
436 }
437 else
438 {
439 //collide left0 left1
440
441
442
445 collision_pairs,trans_cache_1to0,
446 boxset0->getLeftNode(node0),boxset1->getLeftNode(node1),false);
447
448 //collide left0 right1
449
452 collision_pairs,trans_cache_1to0,
453 boxset0->getLeftNode(node0),boxset1->getRightNode(node1),false);
454
455
456 //collide right0 left1
457
460 collision_pairs,trans_cache_1to0,
461 boxset0->getRightNode(node0),boxset1->getLeftNode(node1),false);
462
463 //collide right0 right1
464
467 collision_pairs,trans_cache_1to0,
468 boxset0->getRightNode(node0),boxset1->getRightNode(node1),false);
469
470 }// else if node1 is not a leaf
471 }// else if node0 is not a leaf
472}
473
474
478{
479
480 if(boxset0->getNodeCount()==0 || boxset1->getNodeCount()==0 ) return;
481
482 BT_BOX_BOX_TRANSFORM_CACHE trans_cache_1to0;
483
484 trans_cache_1to0.calc_from_homogenic(trans0,trans1);
485
486#ifdef TRI_COLLISION_PROFILING
488#endif //TRI_COLLISION_PROFILING
489
492 &collision_pairs,trans_cache_1to0,0,0,true);
493#ifdef TRI_COLLISION_PROFILING
495#endif //TRI_COLLISION_PROFILING
496
497}
498
static void _find_collision_pairs_recursive(btGImpactBvh *boxset0, btGImpactBvh *boxset1, btPairSet *collision_pairs, const BT_BOX_BOX_TRANSFORM_CACHE &trans_cache_1to0, int node0, int node1, bool complete_primitive_tests)
bool _node_collision(btGImpactBvh *boxset0, btGImpactBvh *boxset1, const BT_BOX_BOX_TRANSFORM_CACHE &trans_cache_1to0, int node0, int node1, bool complete_primitive_tests)
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:29
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:292
#define SIMD_FORCE_INLINE
Definition btScalar.h:81
#define btAssert(x)
Definition btScalar.h:131
Class for transforming a model1 to the space of model0.
void calc_from_homogenic(const btTransform &trans0, const btTransform &trans1)
Calc the transformation relative 1 to 0. Inverts matrics by transposing.
Axis aligned box.
bool overlapping_trans_cache(const btAABB &box, const BT_BOX_BOX_TRANSFORM_CACHE &transcache, bool fulltest) const
transcache is the transformation cache from box to this AABB
void invalidate()
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
void resize(int newsize, const T &fillData=T())
void _build_sub_tree(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
int _calc_splitting_axis(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
GIM_BVH_TREE_NODE_ARRAY m_node_array
int m_num_nodes
void setNodeBound(int nodeindex, const btAABB &bound)
int _sort_and_calc_splitting_index(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex, int splitAxis)
void build_tree(GIM_BVH_DATA_ARRAY &primitive_boxes)
prototype functions for box tree management
The btClock is a portable basic clock that measures accurate time in seconds, use for profiling.
Definition btQuickprof.h:25
void reset()
Resets the initial reference time.
Structure for containing Boxes.
void buildSet()
this rebuild the entire set
int getRightNode(int nodeindex) const
int getNodeCount() const
node count
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
int getLeftNode(int nodeindex) const
bool boxQuery(const btAABB &box, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
void getNodeBound(int nodeindex, btAABB &bound) const
int getEscapeNodeIndex(int nodeindex) const
int getNodeData(int nodeindex) const
bool rayQuery(const btVector3 &ray_dir, const btVector3 &ray_origin, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
btBvhTree m_box_tree
btPrimitiveManagerBase * m_primitive_manager
void setNodeBound(int nodeindex, const btAABB &bound)
static void find_collision(btGImpactBvh *boxset1, const btTransform &trans1, btGImpactBvh *boxset2, const btTransform &trans2, btPairSet &collision_pairs)
A pairset array.
virtual int get_primitive_count() const =0
virtual void get_primitive_box(int prim_index, btAABB &primbox) const =0
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition btTransform.h:34
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:84
int maxAxis() const
Return the axis with the largest value Note return values are 0,1,2 for x, y, or z.
Definition btVector3.h:487