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
34{
36}
37
39{
40 g_accum_tree_collision_time += g_tree_clock.getTimeMicroseconds();
42}
43
45float btGImpactBvh::getAverageTreeCollisionTime()
46{
47 if (g_count_traversing == 0) return 0;
48
51
54 return avgtime;
55
56 // float avgtime = g_count_traversing;
57 // g_count_traversing = 0;
58 // return avgtime;
59}
60
61#endif //TRI_COLLISION_PROFILING
62
64
67{
68 int i;
69
72 int numIndices = endIndex - startIndex;
73
74 for (i = startIndex; i < endIndex; i++)
75 {
76 btVector3 center = btScalar(0.5) * (primitive_boxes[i].m_bound.m_max +
77 primitive_boxes[i].m_bound.m_min);
78 means += center;
79 }
80 means *= (btScalar(1.) / (btScalar)numIndices);
81
82 for (i = startIndex; i < endIndex; i++)
83 {
84 btVector3 center = btScalar(0.5) * (primitive_boxes[i].m_bound.m_max +
85 primitive_boxes[i].m_bound.m_min);
86 btVector3 diff2 = center - means;
87 diff2 = diff2 * diff2;
88 variance += diff2;
89 }
90 variance *= (btScalar(1.) / ((btScalar)numIndices - 1));
91
92 return variance.maxAxis();
93}
94
97 int endIndex, int splitAxis)
98{
99 int i;
101 int numIndices = endIndex - startIndex;
102
103 // average of centers
104 btScalar splitValue = 0.0f;
105
107 for (i = startIndex; i < endIndex; i++)
108 {
109 btVector3 center = btScalar(0.5) * (primitive_boxes[i].m_bound.m_max +
110 primitive_boxes[i].m_bound.m_min);
111 means += center;
112 }
113 means *= (btScalar(1.) / (btScalar)numIndices);
114
116
117 //sort leafNodes so all values larger then splitValue comes first, and smaller values start from 'splitIndex'.
118 for (i = startIndex; i < endIndex; i++)
119 {
120 btVector3 center = btScalar(0.5) * (primitive_boxes[i].m_bound.m_max +
121 primitive_boxes[i].m_bound.m_min);
122 if (center[splitAxis] > splitValue)
123 {
124 //swap
126 //swapLeafNodes(i,splitIndex);
127 splitIndex++;
128 }
129 }
130
131 //if the splitIndex causes unbalanced trees, fix this by using the center in between startIndex and endIndex
132 //otherwise the tree-building might fail due to stack-overflows in certain cases.
133 //unbalanced1 is unsafe: it can cause stack overflows
134 //bool unbalanced1 = ((splitIndex==startIndex) || (splitIndex == (endIndex-1)));
135
136 //unbalanced2 should work too: always use center (perfect balanced trees)
137 //bool unbalanced2 = true;
138
139 //this should be safe too:
140 int rangeBalancedIndices = numIndices / 3;
142
143 if (unbalanced)
144 {
145 splitIndex = startIndex + (numIndices >> 1);
146 }
147
149
150 return splitIndex;
151}
152
154{
155 int curIndex = m_num_nodes;
156 m_num_nodes++;
157
159
160 if ((endIndex - startIndex) == 1)
161 {
162 //We have a leaf node
164 m_node_array[curIndex].setDataIndex(primitive_boxes[startIndex].m_data);
165
166 return;
167 }
168 //calculate Best Splitting Axis and where to split it. Sort the incoming 'leafNodes' array within range 'startIndex/endIndex'.
169
170 //split axis
172
175 splitIndex //split axis
176 );
177
178 //calc this node bounding box
179
182
183 for (int i = startIndex; i < endIndex; i++)
184 {
185 node_bound.merge(primitive_boxes[i].m_bound);
186 }
187
189
190 //build left branch
192
193 //build right branch
195
196 m_node_array[curIndex].setEscapeIndex(m_num_nodes - curIndex);
197}
198
202{
203 // initialize node count to 0
204 m_num_nodes = 0;
205 // allocate nodes
207
209}
210
212
214{
215 int nodecount = getNodeCount();
216 while (nodecount--)
217 {
219 {
223 }
224 else
225 {
226 //const GIM_BVH_TREE_NODE * nodepointer = get_node_pointer(nodecount);
227 //get left bound
230
232
234 if (child_node)
235 {
237 bound.merge(temp_box);
238 }
239
241 if (child_node)
242 {
244 bound.merge(temp_box);
245 }
246
248 }
249 }
250}
251
254{
255 //obtain primitive boxes
258
259 for (int i = 0; i < primitive_boxes.size(); i++)
260 {
262 primitive_boxes[i].m_data = i;
263 }
264
266}
267
270{
271 int curIndex = 0;
272 int numNodes = getNodeCount();
273
274 while (curIndex < numNodes)
275 {
278
279 //catch bugs in tree data
280
281 bool aabbOverlap = bound.has_collision(box);
283
284 if (isleafnode && aabbOverlap)
285 {
287 }
288
289 if (aabbOverlap || isleafnode)
290 {
291 //next subnode
292 curIndex++;
293 }
294 else
295 {
296 //skip node
298 }
299 }
300 if (collided_results.size() > 0) return true;
301 return false;
302}
303
306 const btVector3& ray_dir, const btVector3& ray_origin,
308{
309 int curIndex = 0;
310 int numNodes = getNodeCount();
311
312 while (curIndex < numNodes)
313 {
316
317 //catch bugs in tree data
318
319 bool aabbOverlap = bound.collide_ray(ray_origin, ray_dir);
321
322 if (isleafnode && aabbOverlap)
323 {
325 }
326
327 if (aabbOverlap || isleafnode)
328 {
329 //next subnode
330 curIndex++;
331 }
332 else
333 {
334 //skip node
336 }
337 }
338 if (collided_results.size() > 0) return true;
339 return false;
340}
341
344 const BT_BOX_BOX_TRANSFORM_CACHE& trans_cache_1to0,
345 int node0, int node1, bool complete_primitive_tests)
346{
347 btAABB box0;
348 boxset0->getNodeBound(node0, box0);
349 btAABB box1;
350 boxset1->getNodeBound(node1, box1);
351
353 // box1.appy_transform_trans_cache(trans_cache_1to0);
354 // return box0.has_collision(box1);
355}
356
357//stackless recursive collision routine
361 const BT_BOX_BOX_TRANSFORM_CACHE& trans_cache_1to0,
362 int node0, int node1, bool complete_primitive_tests)
363{
364 if (_node_collision(
365 boxset0, boxset1, trans_cache_1to0,
366 node0, node1, complete_primitive_tests) == false) return; //avoid colliding internal nodes
367
368 if (boxset0->isLeafNode(node0))
369 {
370 if (boxset1->isLeafNode(node1))
371 {
372 // collision result
373 collision_pairs->push_pair(
374 boxset0->getNodeData(node0), boxset1->getNodeData(node1));
375 return;
376 }
377 else
378 {
379 //collide left recursive
380
383 collision_pairs, trans_cache_1to0,
384 node0, boxset1->getLeftNode(node1), false);
385
386 //collide right recursive
389 collision_pairs, trans_cache_1to0,
390 node0, boxset1->getRightNode(node1), false);
391 }
392 }
393 else
394 {
395 if (boxset1->isLeafNode(node1))
396 {
397 //collide left recursive
400 collision_pairs, trans_cache_1to0,
401 boxset0->getLeftNode(node0), node1, false);
402
403 //collide right recursive
404
407 collision_pairs, trans_cache_1to0,
408 boxset0->getRightNode(node0), node1, false);
409 }
410 else
411 {
412 //collide left0 left1
413
416 collision_pairs, trans_cache_1to0,
417 boxset0->getLeftNode(node0), boxset1->getLeftNode(node1), false);
418
419 //collide left0 right1
420
423 collision_pairs, trans_cache_1to0,
424 boxset0->getLeftNode(node0), boxset1->getRightNode(node1), false);
425
426 //collide right0 left1
427
430 collision_pairs, trans_cache_1to0,
431 boxset0->getRightNode(node0), boxset1->getLeftNode(node1), false);
432
433 //collide right0 right1
434
437 collision_pairs, trans_cache_1to0,
438 boxset0->getRightNode(node0), boxset1->getRightNode(node1), false);
439
440 } // else if node1 is not a leaf
441 } // else if node0 is not a leaf
442}
443
447{
448 if (boxset0->getNodeCount() == 0 || boxset1->getNodeCount() == 0) return;
449
450 BT_BOX_BOX_TRANSFORM_CACHE trans_cache_1to0;
451
452 trans_cache_1to0.calc_from_homogenic(trans0, trans1);
453
454#ifdef TRI_COLLISION_PROFILING
456#endif //TRI_COLLISION_PROFILING
457
460 &collision_pairs, trans_cache_1to0, 0, 0, true);
461#ifdef TRI_COLLISION_PROFILING
463#endif //TRI_COLLISION_PROFILING
464}
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:27
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
#define SIMD_FORCE_INLINE
Definition btScalar.h:98
#define btAssert(x)
Definition btScalar.h:153
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:23
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:30
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
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:477