Bullet Collision Detection & Physics Library: btGImpactBvh.h Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btGImpactBvh.h
Go to the documentation of this file.
1 #ifndef GIM_BOX_SET_H_INCLUDED
2 #define GIM_BOX_SET_H_INCLUDED
3
7 /*
8 This source file is part of GIMPACT Library.
9
10 For the latest info, see http://gimpact.sourceforge.net/
11
12 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
14
15
16 This software is provided 'as-is', without any express or implied warranty.
17 In no event will the authors be held liable for any damages arising from the use of this software.
18 Permission is granted to anyone to use this software for any purpose,
19 including commercial applications, and to alter it and redistribute it freely,
20 subject to the following restrictions:
21
22 1. 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.
23 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
25 */
26
27
28 #include "LinearMath/btAlignedObjectArray.h"
29
30 #include "btBoxCollision.h"
31 #include "btTriangleShapeEx.h"
32
33
34
35
36
38 struct GIM_PAIR
39 {
40 int m_index1;
41 int m_index2;
42 GIM_PAIR()
43 {}
44
45 GIM_PAIR(const GIM_PAIR & p)
46 {
47 m_index1 = p.m_index1;
48 m_index2 = p.m_index2;
49 }
50
51 GIM_PAIR(int index1, int index2)
52 {
53 m_index1 = index1;
54 m_index2 = index2;
55 }
56 };
57
59 class btPairSet: public btAlignedObjectArray<GIM_PAIR>
60 {
61 public:
62 btPairSet()
63 {
64 reserve(32);
65 }
66 inline void push_pair(int index1,int index2)
67 {
68 push_back(GIM_PAIR(index1,index2));
69 }
70
71 inline void push_pair_inv(int index1,int index2)
72 {
73 push_back(GIM_PAIR(index2,index1));
74 }
75 };
76
77
79 struct GIM_BVH_DATA
80 {
81 btAABB m_bound;
82 int m_data;
83 };
84
86 class GIM_BVH_TREE_NODE
87 {
88 public:
89 btAABB m_bound;
90 protected:
91 int m_escapeIndexOrDataIndex;
92 public:
93 GIM_BVH_TREE_NODE()
94 {
95 m_escapeIndexOrDataIndex = 0;
96 }
97
98 SIMD_FORCE_INLINE bool isLeafNode() const
99 {
100 //skipindex is negative (internal node), triangleindex >=0 (leafnode)
101 return (m_escapeIndexOrDataIndex>=0);
102 }
103
104 SIMD_FORCE_INLINE int getEscapeIndex() const
105 {
106 //btAssert(m_escapeIndexOrDataIndex < 0);
107 return -m_escapeIndexOrDataIndex;
108 }
109
110 SIMD_FORCE_INLINE void setEscapeIndex(int index)
111 {
112 m_escapeIndexOrDataIndex = -index;
113 }
114
115 SIMD_FORCE_INLINE int getDataIndex() const
116 {
117 //btAssert(m_escapeIndexOrDataIndex >= 0);
118
119 return m_escapeIndexOrDataIndex;
120 }
121
122 SIMD_FORCE_INLINE void setDataIndex(int index)
123 {
124 m_escapeIndexOrDataIndex = index;
125 }
126
127 };
128
129
130 class GIM_BVH_DATA_ARRAY:public btAlignedObjectArray<GIM_BVH_DATA>
131 {
132 };
133
134
135 class GIM_BVH_TREE_NODE_ARRAY:public btAlignedObjectArray<GIM_BVH_TREE_NODE>
136 {
137 };
138
139
140
141
143 class btBvhTree
144 {
145 protected:
146 int m_num_nodes;
147 GIM_BVH_TREE_NODE_ARRAY m_node_array;
148 protected:
149 int _sort_and_calc_splitting_index(
150 GIM_BVH_DATA_ARRAY & primitive_boxes,
151 int startIndex, int endIndex, int splitAxis);
152
153 int _calc_splitting_axis(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
154
155 void _build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
156 public:
157 btBvhTree()
158 {
159 m_num_nodes = 0;
160 }
161
164 void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
165
166 SIMD_FORCE_INLINE void clearNodes()
167 {
168 m_node_array.clear();
169 m_num_nodes = 0;
170 }
171
173 SIMD_FORCE_INLINE int getNodeCount() const
174 {
175 return m_num_nodes;
176 }
177
179 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
180 {
181 return m_node_array[nodeindex].isLeafNode();
182 }
183
184 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
185 {
186 return m_node_array[nodeindex].getDataIndex();
187 }
188
189 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
190 {
191 bound = m_node_array[nodeindex].m_bound;
192 }
193
194 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
195 {
196 m_node_array[nodeindex].m_bound = bound;
197 }
198
199 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
200 {
201 return nodeindex+1;
202 }
203
204 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
205 {
206 if(m_node_array[nodeindex+1].isLeafNode()) return nodeindex+2;
207 return nodeindex+1 + m_node_array[nodeindex+1].getEscapeIndex();
208 }
209
210 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
211 {
212 return m_node_array[nodeindex].getEscapeIndex();
213 }
214
215 SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE * get_node_pointer(int index = 0) const
216 {
217 return &m_node_array[index];
218 }
219
221 };
222
223
225
230 class btPrimitiveManagerBase
231 {
232 public:
233
234 virtual ~btPrimitiveManagerBase() {}
235
237 virtual bool is_trimesh() const = 0;
238 virtual int get_primitive_count() const = 0;
239 virtual void get_primitive_box(int prim_index ,btAABB & primbox) const = 0;
241 virtual void get_primitive_triangle(int prim_index,btPrimitiveTriangle & triangle) const= 0;
242 };
243
244
246
250 class btGImpactBvh
251 {
252 protected:
253 btBvhTree m_box_tree;
254 btPrimitiveManagerBase * m_primitive_manager;
255
256 protected:
257 //stackless refit
258 void refit();
259 public:
260
262 btGImpactBvh()
263 {
264 m_primitive_manager = NULL;
265 }
266
268 btGImpactBvh(btPrimitiveManagerBase * primitive_manager)
269 {
270 m_primitive_manager = primitive_manager;
271 }
272
273 SIMD_FORCE_INLINE btAABB getGlobalBox() const
274 {
275 btAABB totalbox;
276 getNodeBound(0, totalbox);
277 return totalbox;
278 }
279
280 SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase * primitive_manager)
281 {
282 m_primitive_manager = primitive_manager;
283 }
284
285 SIMD_FORCE_INLINE btPrimitiveManagerBase * getPrimitiveManager() const
286 {
287 return m_primitive_manager;
288 }
289
290
293
295 SIMD_FORCE_INLINE void update()
296 {
297 refit();
298 }
299
301 void buildSet();
302
304 bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
305
307 SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB & box,
308 const btTransform & transform, btAlignedObjectArray<int> & collided_results) const
309 {
310 btAABB transbox=box;
311 transbox.appy_transform(transform);
312 return boxQuery(transbox,collided_results);
313 }
314
316 bool rayQuery(
317 const btVector3 & ray_dir,const btVector3 & ray_origin ,
318 btAlignedObjectArray<int> & collided_results) const;
319
321 SIMD_FORCE_INLINE bool hasHierarchy() const
322 {
323 return true;
324 }
325
327 SIMD_FORCE_INLINE bool isTrimesh() const
328 {
329 return m_primitive_manager->is_trimesh();
330 }
331
333 SIMD_FORCE_INLINE int getNodeCount() const
334 {
335 return m_box_tree.getNodeCount();
336 }
337
339 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
340 {
341 return m_box_tree.isLeafNode(nodeindex);
342 }
343
344 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
345 {
346 return m_box_tree.getNodeData(nodeindex);
347 }
348
349 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
350 {
351 m_box_tree.getNodeBound(nodeindex, bound);
352 }
353
354 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
355 {
356 m_box_tree.setNodeBound(nodeindex, bound);
357 }
358
359
360 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
361 {
362 return m_box_tree.getLeftNode(nodeindex);
363 }
364
365 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
366 {
367 return m_box_tree.getRightNode(nodeindex);
368 }
369
370 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
371 {
372 return m_box_tree.getEscapeNodeIndex(nodeindex);
373 }
374
375 SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex,btPrimitiveTriangle & triangle) const
376 {
377 m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex),triangle);
378 }
379
380
381 SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE * get_node_pointer(int index = 0) const
382 {
383 return m_box_tree.get_node_pointer(index);
384 }
385
386 #ifdef TRI_COLLISION_PROFILING
387 static float getAverageTreeCollisionTime();
388 #endif //TRI_COLLISION_PROFILING
389
390 static void find_collision(btGImpactBvh * boxset1, const btTransform & trans1,
391 btGImpactBvh * boxset2, const btTransform & trans2,
392 btPairSet & collision_pairs);
393 };
394
395
396 #endif // GIM_BOXPRUNING_H_INCLUDED
btAlignedObjectArray< GIM_PAIR >::reserve
void reserve(int _Count)
Definition: btAlignedObjectArray.h:300
btGImpactBvh::update
void update()
node manager prototype functions
Definition: btGImpactBvh.h:295
btGImpactBvh::find_collision
static void find_collision(btGImpactBvh *boxset1, const btTransform &trans1, btGImpactBvh *boxset2, const btTransform &trans2, btPairSet &collision_pairs)
Definition: btGImpactBvh.cpp:475
btAlignedObjectArray< GIM_PAIR >::push_back
void push_back(const GIM_PAIR &_Val)
Definition: btAlignedObjectArray.h:276
GIM_PAIR::GIM_PAIR
GIM_PAIR(int index1, int index2)
Definition: btGImpactBvh.h:51
btBvhTree::m_node_array
GIM_BVH_TREE_NODE_ARRAY m_node_array
Definition: btGImpactBvh.h:147
btPrimitiveTriangle
Definition: btTriangleShapeEx.h:74
GIM_PAIR::GIM_PAIR
GIM_PAIR()
Definition: btGImpactBvh.h:42
btGImpactBvh::getPrimitiveManager
btPrimitiveManagerBase * getPrimitiveManager() const
Definition: btGImpactBvh.h:285
GIM_BVH_TREE_NODE::getDataIndex
int getDataIndex() const
Definition: btGImpactBvh.h:115
btPairSet::btPairSet
btPairSet()
Definition: btGImpactBvh.h:62
GIM_BVH_DATA
GIM_BVH_DATA is an internal GIMPACT collision structure to contain axis aligned bounding box...
Definition: btGImpactBvh.h:79
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:47
btBvhTree::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactBvh.h:194
btTriangleShapeEx.h
GIM_PAIR::m_index2
int m_index2
Definition: btGImpactBvh.h:41
btPrimitiveManagerBase::~btPrimitiveManagerBase
virtual ~btPrimitiveManagerBase()
Definition: btGImpactBvh.h:234
btPrimitiveManagerBase::get_primitive_count
virtual int get_primitive_count() const =0
btPairSet::push_pair_inv
void push_pair_inv(int index1, int index2)
Definition: btGImpactBvh.h:71
btGImpactBvh::refit
void refit()
Definition: btGImpactBvh.cpp:225
btBvhTree::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactBvh.h:210
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:63
GIM_BVH_TREE_NODE::getEscapeIndex
int getEscapeIndex() const
Definition: btGImpactBvh.h:104
btBvhTree::build_tree
void build_tree(GIM_BVH_DATA_ARRAY &primitive_boxes)
prototype functions for box tree management
Definition: btGImpactBvh.cpp:212
btBvhTree::m_num_nodes
int m_num_nodes
Definition: btGImpactBvh.h:146
btGImpactBvh::isTrimesh
bool isTrimesh() const
tells if this set is a trimesh
Definition: btGImpactBvh.h:327
btBvhTree::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactBvh.h:173
btBvhTree::_build_sub_tree
void _build_sub_tree(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
Definition: btGImpactBvh.cpp:160
btBvhTree::get_node_pointer
const GIM_BVH_TREE_NODE * get_node_pointer(int index=0) const
Definition: btGImpactBvh.h:215
btGImpactBvh::getGlobalBox
btAABB getGlobalBox() const
Definition: btGImpactBvh.h:273
btPrimitiveManagerBase
Prototype Base class for primitive classification.
Definition: btGImpactBvh.h:230
GIM_BVH_TREE_NODE::setEscapeIndex
void setEscapeIndex(int index)
Definition: btGImpactBvh.h:110
btBvhTree::btBvhTree
btBvhTree()
Definition: btGImpactBvh.h:157
btPairSet
A pairset array.
Definition: btGImpactBvh.h:59
btGImpactBvh::boxQueryTrans
bool boxQueryTrans(const btAABB &box, const btTransform &transform, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
Definition: btGImpactBvh.h:307
btPairSet::push_pair
void push_pair(int index1, int index2)
Definition: btGImpactBvh.h:66
btAlignedObjectArray::clear
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
Definition: btAlignedObjectArray.h:184
btGImpactBvh::get_node_pointer
const GIM_BVH_TREE_NODE * get_node_pointer(int index=0) const
Definition: btGImpactBvh.h:381
btBvhTree::_calc_splitting_axis
int _calc_splitting_axis(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
Definition: btGImpactBvh.cpp:67
btBvhTree::_sort_and_calc_splitting_index
int _sort_and_calc_splitting_index(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex, int splitAxis)
Definition: btGImpactBvh.cpp:99
btGImpactBvh::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactBvh.h:349
btGImpactBvh::hasHierarchy
bool hasHierarchy() const
tells if this set has hierarcht
Definition: btGImpactBvh.h:321
btPrimitiveManagerBase::get_primitive_box
virtual void get_primitive_box(int prim_index, btAABB &primbox) const =0
btAABB
Axis aligned box.
Definition: btBoxCollision.h:229
btBvhTree::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactBvh.h:179
btGImpactBvh::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactBvh.h:339
btGImpactBvh::btGImpactBvh
btGImpactBvh()
this constructor doesn't build the tree. you must call buildSet
Definition: btGImpactBvh.h:262
btBvhTree::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactBvh.h:204
btBvhTree
Basic Box tree structure.
Definition: btGImpactBvh.h:143
btAABB::appy_transform
void appy_transform(const btTransform &trans)
Apply a transform to an AABB.
Definition: btBoxCollision.h:358
btPrimitiveManagerBase::is_trimesh
virtual bool is_trimesh() const =0
determines if this manager consist on only triangles, which special case will be optimized ...
btGImpactBvh::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactBvh.h:354
GIM_BVH_TREE_NODE::m_escapeIndexOrDataIndex
int m_escapeIndexOrDataIndex
Definition: btGImpactBvh.h:91
btGImpactBvh::m_box_tree
btBvhTree m_box_tree
Definition: btGImpactBvh.h:253
GIM_BVH_TREE_NODE::isLeafNode
bool isLeafNode() const
Definition: btGImpactBvh.h:98
btGImpactBvh::buildSet
void buildSet()
this rebuild the entire set
Definition: btGImpactBvh.cpp:265
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btGImpactBvh::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactBvh.h:370
btPrimitiveManagerBase::get_primitive_triangle
virtual void get_primitive_triangle(int prim_index, btPrimitiveTriangle &triangle) const =0
retrieves only the points of the triangle, and the collision margin
GIM_BVH_TREE_NODE_ARRAY
Definition: btGImpactBvh.h:135
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btGImpactBvh::setPrimitiveManager
void setPrimitiveManager(btPrimitiveManagerBase *primitive_manager)
Definition: btGImpactBvh.h:280
GIM_BVH_DATA::m_bound
btAABB m_bound
Definition: btGImpactBvh.h:81
btGImpactBvh::m_primitive_manager
btPrimitiveManagerBase * m_primitive_manager
Definition: btGImpactBvh.h:254
btGImpactBvh::boxQuery
bool boxQuery(const btAABB &box, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
Definition: btGImpactBvh.cpp:281
GIM_PAIR::GIM_PAIR
GIM_PAIR(const GIM_PAIR &p)
Definition: btGImpactBvh.h:45
btBvhTree::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactBvh.h:184
btBvhTree::clearNodes
void clearNodes()
Definition: btGImpactBvh.h:166
btBvhTree::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactBvh.h:199
btGImpactBvh::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactBvh.h:344
GIM_BVH_TREE_NODE
Node Structure for trees.
Definition: btGImpactBvh.h:86
btGImpactBvh
Structure for containing Boxes.
Definition: btGImpactBvh.h:250
btBoxCollision.h
GIM_PAIR
Overlapping pair.
Definition: btGImpactBvh.h:38
btGImpactBvh::btGImpactBvh
btGImpactBvh(btPrimitiveManagerBase *primitive_manager)
this constructor doesn't build the tree. you must call buildSet
Definition: btGImpactBvh.h:268
btGImpactBvh::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactBvh.h:333
btGImpactBvh::getNodeTriangle
void getNodeTriangle(int nodeindex, btPrimitiveTriangle &triangle) const
Definition: btGImpactBvh.h:375
btAlignedObjectArray.h
GIM_BVH_DATA_ARRAY
Definition: btGImpactBvh.h:130
GIM_BVH_TREE_NODE::setDataIndex
void setDataIndex(int index)
Definition: btGImpactBvh.h:122
GIM_BVH_TREE_NODE::m_bound
btAABB m_bound
Definition: btGImpactBvh.h:89
btGImpactBvh::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactBvh.h:360
GIM_BVH_DATA::m_data
int m_data
Definition: btGImpactBvh.h:82
btGImpactBvh::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactBvh.h:365
btGImpactBvh::rayQuery
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
Definition: btGImpactBvh.cpp:319
btBvhTree::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactBvh.h:189
GIM_BVH_TREE_NODE::GIM_BVH_TREE_NODE
GIM_BVH_TREE_NODE()
Definition: btGImpactBvh.h:93
GIM_PAIR::m_index1
int m_index1
Definition: btGImpactBvh.h:40