Bullet Collision Detection & Physics Library
btGImpactQuantizedBvh.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
26
27#ifdef TRI_COLLISION_PROFILING
29
30
33
34
36{
38}
39
41{
42 g_q_accum_tree_collision_time += g_q_tree_clock.getTimeMicroseconds();
44}
45
46
48float btGImpactQuantizedBvh::getAverageTreeCollisionTime()
49{
50 if(g_q_count_traversing == 0) return 0;
51
54
57 return avgtime;
58
59// float avgtime = g_q_count_traversing;
60// g_q_count_traversing = 0;
61// return avgtime;
62
63}
64
65#endif //TRI_COLLISION_PROFILING
66
68
71{
72 //calc globa box
75
76 for (int i=0;i<primitive_boxes.size() ;i++ )
77 {
78 global_bound.merge(primitive_boxes[i].m_bound);
79 }
80
83
84}
85
86
87
90{
91
92 int i;
93
96 int numIndices = endIndex-startIndex;
97
98 for (i=startIndex;i<endIndex;i++)
99 {
100 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
101 primitive_boxes[i].m_bound.m_min);
102 means+=center;
103 }
104 means *= (btScalar(1.)/(btScalar)numIndices);
105
106 for (i=startIndex;i<endIndex;i++)
107 {
108 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
109 primitive_boxes[i].m_bound.m_min);
110 btVector3 diff2 = center-means;
111 diff2 = diff2 * diff2;
112 variance += diff2;
113 }
114 variance *= (btScalar(1.)/ ((btScalar)numIndices-1) );
115
116 return variance.maxAxis();
117}
118
119
122 int endIndex, int splitAxis)
123{
124 int i;
126 int numIndices = endIndex - startIndex;
127
128 // average of centers
129 btScalar splitValue = 0.0f;
130
132 for (i=startIndex;i<endIndex;i++)
133 {
134 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
135 primitive_boxes[i].m_bound.m_min);
136 means+=center;
137 }
138 means *= (btScalar(1.)/(btScalar)numIndices);
139
141
142
143 //sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
144 for (i=startIndex;i<endIndex;i++)
145 {
146 btVector3 center = btScalar(0.5)*(primitive_boxes[i].m_bound.m_max +
147 primitive_boxes[i].m_bound.m_min);
148 if (center[splitAxis] > splitValue)
149 {
150 //swap
152 //swapLeafNodes(i,splitIndex);
153 splitIndex++;
154 }
155 }
156
157 //if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
158 //otherwise the tree-building might fail due to stack-overflows in certain cases.
159 //unbalanced1 is unsafe: it can cause stack overflows
160 //bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
161
162 //unbalanced2 should work too: always use center (perfect balanced trees)
163 //bool unbalanced2 = true;
164
165 //this should be safe too:
166 int rangeBalancedIndices = numIndices/3;
168
169 if (unbalanced)
170 {
171 splitIndex = startIndex+ (numIndices>>1);
172 }
173
175
176 return splitIndex;
177
178}
179
180
182{
183 int curIndex = m_num_nodes;
184 m_num_nodes++;
185
187
188 if ((endIndex-startIndex)==1)
189 {
190 //We have a leaf node
192 m_node_array[curIndex].setDataIndex(primitive_boxes[startIndex].m_data);
193
194 return;
195 }
196 //calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
197
198 //split axis
200
203 splitIndex//split axis
204 );
205
206
207 //calc this node bounding box
208
211
212 for (int i=startIndex;i<endIndex;i++)
213 {
214 node_bound.merge(primitive_boxes[i].m_bound);
215 }
216
218
219
220 //build left branch
222
223
224 //build right branch
226
227 m_node_array[curIndex].setEscapeIndex(m_num_nodes - curIndex);
228
229
230}
231
235{
237 // initialize node count to 0
238 m_num_nodes = 0;
239 // allocate nodes
241
243}
244
246
248{
249 int nodecount = getNodeCount();
250 while(nodecount--)
251 {
253 {
257 }
258 else
259 {
260 //const GIM_BVH_TREE_NODE * nodepointer = get_node_pointer(nodecount);
261 //get left bound
264
266
268 if(child_node)
269 {
271 bound.merge(temp_box);
272 }
273
275 if(child_node)
276 {
278 bound.merge(temp_box);
279 }
280
282 }
283 }
284}
285
288{
289 //obtain primitive boxes
292
293 for (int i = 0;i<primitive_boxes.size() ;i++ )
294 {
296 primitive_boxes[i].m_data = i;
297 }
298
300}
301
304{
305 int curIndex = 0;
306 int numNodes = getNodeCount();
307
308 //quantize box
309
310 unsigned short quantizedMin[3];
311 unsigned short quantizedMax[3];
312
315
316
317 while (curIndex < numNodes)
318 {
319
320 //catch bugs in tree data
321
324
325 if (isleafnode && aabbOverlap)
326 {
328 }
329
330 if (aabbOverlap || isleafnode)
331 {
332 //next subnode
333 curIndex++;
334 }
335 else
336 {
337 //skip node
339 }
340 }
341 if(collided_results.size()>0) return true;
342 return false;
343}
344
345
346
349 const btVector3 & ray_dir,const btVector3 & ray_origin ,
351{
352 int curIndex = 0;
353 int numNodes = getNodeCount();
354
355 while (curIndex < numNodes)
356 {
359
360 //catch bugs in tree data
361
362 bool aabbOverlap = bound.collide_ray(ray_origin,ray_dir);
364
365 if (isleafnode && aabbOverlap)
366 {
368 }
369
370 if (aabbOverlap || isleafnode)
371 {
372 //next subnode
373 curIndex++;
374 }
375 else
376 {
377 //skip node
379 }
380 }
381 if(collided_results.size()>0) return true;
382 return false;
383}
384
385
388 const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
389 int node0 ,int node1, bool complete_primitive_tests)
390{
391 btAABB box0;
392 boxset0->getNodeBound(node0,box0);
393 btAABB box1;
394 boxset1->getNodeBound(node1,box1);
395
397// box1.appy_transform_trans_cache(trans_cache_1to0);
398// return box0.has_collision(box1);
399
400}
401
402
403//stackless recursive collision routine
407 const BT_BOX_BOX_TRANSFORM_CACHE & trans_cache_1to0,
408 int node0, int node1, bool complete_primitive_tests)
409{
410
411
412
414 boxset0,boxset1,trans_cache_1to0,
415 node0,node1,complete_primitive_tests) ==false) return;//avoid colliding internal nodes
416
417 if(boxset0->isLeafNode(node0))
418 {
419 if(boxset1->isLeafNode(node1))
420 {
421 // collision result
422 collision_pairs->push_pair(
423 boxset0->getNodeData(node0),boxset1->getNodeData(node1));
424 return;
425 }
426 else
427 {
428
429 //collide left recursive
430
433 collision_pairs,trans_cache_1to0,
434 node0,boxset1->getLeftNode(node1),false);
435
436 //collide right recursive
439 collision_pairs,trans_cache_1to0,
440 node0,boxset1->getRightNode(node1),false);
441
442
443 }
444 }
445 else
446 {
447 if(boxset1->isLeafNode(node1))
448 {
449
450 //collide left recursive
453 collision_pairs,trans_cache_1to0,
454 boxset0->getLeftNode(node0),node1,false);
455
456
457 //collide right recursive
458
461 collision_pairs,trans_cache_1to0,
462 boxset0->getRightNode(node0),node1,false);
463
464
465 }
466 else
467 {
468 //collide left0 left1
469
470
471
474 collision_pairs,trans_cache_1to0,
475 boxset0->getLeftNode(node0),boxset1->getLeftNode(node1),false);
476
477 //collide left0 right1
478
481 collision_pairs,trans_cache_1to0,
482 boxset0->getLeftNode(node0),boxset1->getRightNode(node1),false);
483
484
485 //collide right0 left1
486
489 collision_pairs,trans_cache_1to0,
490 boxset0->getRightNode(node0),boxset1->getLeftNode(node1),false);
491
492 //collide right0 right1
493
496 collision_pairs,trans_cache_1to0,
497 boxset0->getRightNode(node0),boxset1->getRightNode(node1),false);
498
499 }// else if node1 is not a leaf
500 }// else if node0 is not a leaf
501}
502
503
507{
508
509 if(boxset0->getNodeCount()==0 || boxset1->getNodeCount()==0 ) return;
510
511 BT_BOX_BOX_TRANSFORM_CACHE trans_cache_1to0;
512
513 trans_cache_1to0.calc_from_homogenic(trans0,trans1);
514
515#ifdef TRI_COLLISION_PROFILING
517#endif //TRI_COLLISION_PROFILING
518
521 &collision_pairs,trans_cache_1to0,0,0,true);
522#ifdef TRI_COLLISION_PROFILING
524#endif //TRI_COLLISION_PROFILING
525
526}
527
528
bool _quantized_node_collision(const btGImpactQuantizedBvh *boxset0, const btGImpactQuantizedBvh *boxset1, const BT_BOX_BOX_TRANSFORM_CACHE &trans_cache_1to0, int node0, int node1, bool complete_primitive_tests)
static void _find_quantized_collision_pairs_recursive(const btGImpactQuantizedBvh *boxset0, const btGImpactQuantizedBvh *boxset1, btPairSet *collision_pairs, 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
void bt_calc_quantization_parameters(btVector3 &outMinBound, btVector3 &outMaxBound, btVector3 &bvhQuantization, const btVector3 &srcMinBound, const btVector3 &srcMaxBound, btScalar quantizationMargin)
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
btVector3 m_min
btVector3 m_max
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())
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.
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
btPrimitiveManagerBase * m_primitive_manager
void buildSet()
this rebuild the entire set
bool boxQuery(const btAABB &box, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
btQuantizedBvhTree m_box_tree
void setNodeBound(int nodeindex, const btAABB &bound)
int getNodeCount() const
node count
int getRightNode(int nodeindex) const
int getEscapeNodeIndex(int nodeindex) const
void getNodeBound(int nodeindex, btAABB &bound) const
int getLeftNode(int nodeindex) const
static void find_collision(const btGImpactQuantizedBvh *boxset1, const btTransform &trans1, const btGImpactQuantizedBvh *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
void setNodeBound(int nodeindex, const btAABB &bound)
void _build_sub_tree(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
void quantizePoint(unsigned short *quantizedpoint, const btVector3 &point) const
void calc_quantization(GIM_BVH_DATA_ARRAY &primitive_boxes, btScalar boundMargin=btScalar(1.0))
void build_tree(GIM_BVH_DATA_ARRAY &primitive_boxes)
prototype functions for box tree management
GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array
int _sort_and_calc_splitting_index(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex, int splitAxis)
int _calc_splitting_axis(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
bool testQuantizedBoxOverlapp(int node_index, unsigned short *quantizedMin, unsigned short *quantizedMax) const
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