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

Bullet Collision Detection & Physics Library

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 #include "btGImpactBvhStructs.h"
33
35 class btPairSet: public btAlignedObjectArray<GIM_PAIR>
36 {
37 public:
38 btPairSet()
39 {
40 reserve(32);
41 }
42 inline void push_pair(int index1,int index2)
43 {
44 push_back(GIM_PAIR(index1,index2));
45 }
46
47 inline void push_pair_inv(int index1,int index2)
48 {
49 push_back(GIM_PAIR(index2,index1));
50 }
51 };
52
53 class GIM_BVH_DATA_ARRAY:public btAlignedObjectArray<GIM_BVH_DATA>
54 {
55 };
56
57
58 class GIM_BVH_TREE_NODE_ARRAY:public btAlignedObjectArray<GIM_BVH_TREE_NODE>
59 {
60 };
61
62
63
64
66 class btBvhTree
67 {
68 protected:
69 int m_num_nodes;
70 GIM_BVH_TREE_NODE_ARRAY m_node_array;
71 protected:
72 int _sort_and_calc_splitting_index(
73 GIM_BVH_DATA_ARRAY & primitive_boxes,
74 int startIndex, int endIndex, int splitAxis);
75
76 int _calc_splitting_axis(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
77
78 void _build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
79 public:
80 btBvhTree()
81 {
82 m_num_nodes = 0;
83 }
84
87 void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
88
89 SIMD_FORCE_INLINE void clearNodes()
90 {
91 m_node_array.clear();
92 m_num_nodes = 0;
93 }
94
96 SIMD_FORCE_INLINE int getNodeCount() const
97 {
98 return m_num_nodes;
99 }
100
102 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
103 {
104 return m_node_array[nodeindex].isLeafNode();
105 }
106
107 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
108 {
109 return m_node_array[nodeindex].getDataIndex();
110 }
111
112 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
113 {
114 bound = m_node_array[nodeindex].m_bound;
115 }
116
117 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
118 {
119 m_node_array[nodeindex].m_bound = bound;
120 }
121
122 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
123 {
124 return nodeindex+1;
125 }
126
127 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
128 {
129 if(m_node_array[nodeindex+1].isLeafNode()) return nodeindex+2;
130 return nodeindex+1 + m_node_array[nodeindex+1].getEscapeIndex();
131 }
132
133 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
134 {
135 return m_node_array[nodeindex].getEscapeIndex();
136 }
137
138 SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE * get_node_pointer(int index = 0) const
139 {
140 return &m_node_array[index];
141 }
142
144 };
145
146
148
153 class btPrimitiveManagerBase
154 {
155 public:
156
157 virtual ~btPrimitiveManagerBase() {}
158
160 virtual bool is_trimesh() const = 0;
161 virtual int get_primitive_count() const = 0;
162 virtual void get_primitive_box(int prim_index ,btAABB & primbox) const = 0;
164 virtual void get_primitive_triangle(int prim_index,btPrimitiveTriangle & triangle) const= 0;
165 };
166
167
169
173 class btGImpactBvh
174 {
175 protected:
176 btBvhTree m_box_tree;
177 btPrimitiveManagerBase * m_primitive_manager;
178
179 protected:
180 //stackless refit
181 void refit();
182 public:
183
185 btGImpactBvh()
186 {
187 m_primitive_manager = NULL;
188 }
189
191 btGImpactBvh(btPrimitiveManagerBase * primitive_manager)
192 {
193 m_primitive_manager = primitive_manager;
194 }
195
196 SIMD_FORCE_INLINE btAABB getGlobalBox() const
197 {
198 btAABB totalbox;
199 getNodeBound(0, totalbox);
200 return totalbox;
201 }
202
203 SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase * primitive_manager)
204 {
205 m_primitive_manager = primitive_manager;
206 }
207
208 SIMD_FORCE_INLINE btPrimitiveManagerBase * getPrimitiveManager() const
209 {
210 return m_primitive_manager;
211 }
212
213
216
218 SIMD_FORCE_INLINE void update()
219 {
220 refit();
221 }
222
224 void buildSet();
225
227 bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
228
230 SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB & box,
231 const btTransform & transform, btAlignedObjectArray<int> & collided_results) const
232 {
233 btAABB transbox=box;
234 transbox.appy_transform(transform);
235 return boxQuery(transbox,collided_results);
236 }
237
239 bool rayQuery(
240 const btVector3 & ray_dir,const btVector3 & ray_origin ,
241 btAlignedObjectArray<int> & collided_results) const;
242
244 SIMD_FORCE_INLINE bool hasHierarchy() const
245 {
246 return true;
247 }
248
250 SIMD_FORCE_INLINE bool isTrimesh() const
251 {
252 return m_primitive_manager->is_trimesh();
253 }
254
256 SIMD_FORCE_INLINE int getNodeCount() const
257 {
258 return m_box_tree.getNodeCount();
259 }
260
262 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
263 {
264 return m_box_tree.isLeafNode(nodeindex);
265 }
266
267 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
268 {
269 return m_box_tree.getNodeData(nodeindex);
270 }
271
272 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
273 {
274 m_box_tree.getNodeBound(nodeindex, bound);
275 }
276
277 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
278 {
279 m_box_tree.setNodeBound(nodeindex, bound);
280 }
281
282
283 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
284 {
285 return m_box_tree.getLeftNode(nodeindex);
286 }
287
288 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
289 {
290 return m_box_tree.getRightNode(nodeindex);
291 }
292
293 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
294 {
295 return m_box_tree.getEscapeNodeIndex(nodeindex);
296 }
297
298 SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex,btPrimitiveTriangle & triangle) const
299 {
300 m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex),triangle);
301 }
302
303
304 SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE * get_node_pointer(int index = 0) const
305 {
306 return m_box_tree.get_node_pointer(index);
307 }
308
309 #ifdef TRI_COLLISION_PROFILING
310 static float getAverageTreeCollisionTime();
311 #endif //TRI_COLLISION_PROFILING
312
313 static void find_collision(btGImpactBvh * boxset1, const btTransform & trans1,
314 btGImpactBvh * boxset2, const btTransform & trans2,
315 btPairSet & collision_pairs);
316 };
317
318 #endif // GIM_BOXPRUNING_H_INCLUDED
btAlignedObjectArray< GIM_PAIR >::reserve
void reserve(int _Count)
Definition: btAlignedObjectArray.h:298
btGImpactBvh::update
void update()
node manager prototype functions
Definition: btGImpactBvh.h:218
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:274
btBvhTree::m_node_array
GIM_BVH_TREE_NODE_ARRAY m_node_array
Definition: btGImpactBvh.h:70
btPrimitiveTriangle
Definition: btTriangleShapeEx.h:74
btPairSet::btPairSet
btPairSet()
Definition: btGImpactBvh.h:38
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:53
btGImpactBvh::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactBvh.h:272
btBvhTree::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactBvh.h:117
btTriangleShapeEx.h
btPrimitiveManagerBase::~btPrimitiveManagerBase
virtual ~btPrimitiveManagerBase()
Definition: btGImpactBvh.h:157
btPrimitiveManagerBase::get_primitive_count
virtual int get_primitive_count() const =0
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
btPairSet::push_pair_inv
void push_pair_inv(int index1, int index2)
Definition: btGImpactBvh.h:47
btGImpactBvh::refit
void refit()
Definition: btGImpactBvh.cpp:225
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btBvhTree::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactBvh.h:107
btBvhTree::build_tree
void build_tree(GIM_BVH_DATA_ARRAY &primitive_boxes)
prototype functions for box tree management
Definition: btGImpactBvh.cpp:212
btBvhTree::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactBvh.h:112
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
btGImpactBvh::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactBvh.h:267
btBvhTree::m_num_nodes
int m_num_nodes
Definition: btGImpactBvh.h:69
btBvhTree::_build_sub_tree
void _build_sub_tree(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
Definition: btGImpactBvh.cpp:160
btPrimitiveManagerBase
Prototype Base class for primitive classification.
Definition: btGImpactBvh.h:153
btGImpactBvh::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactBvh.h:262
btBvhTree::btBvhTree
btBvhTree()
Definition: btGImpactBvh.h:80
btPairSet
A pairset array.
Definition: btGImpactBvh.h:35
btPairSet::push_pair
void push_pair(int index1, int index2)
Definition: btGImpactBvh.h:42
btGImpactBvh::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactBvh.h:283
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:190
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::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:230
btBvhTree::get_node_pointer
const GIM_BVH_TREE_NODE * get_node_pointer(int index=0) const
Definition: btGImpactBvh.h:138
btPrimitiveManagerBase::get_primitive_box
virtual void get_primitive_box(int prim_index, btAABB &primbox) const =0
btAABB
Axis aligned box.
Definition: btBoxCollision.h:229
btGImpactBvh::btGImpactBvh
btGImpactBvh()
this constructor doesn&#39;t build the tree. you must call buildSet
Definition: btGImpactBvh.h:185
btBvhTree
Basic Box tree structure.
Definition: btGImpactBvh.h:66
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:277
btGImpactBvh::m_box_tree
btBvhTree m_box_tree
Definition: btGImpactBvh.h:176
btGImpactBvh::getPrimitiveManager
btPrimitiveManagerBase * getPrimitiveManager() const
Definition: btGImpactBvh.h:208
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
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:58
btGImpactBvh::isTrimesh
bool isTrimesh() const
tells if this set is a trimesh
Definition: btGImpactBvh.h:250
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btBvhTree::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactBvh.h:122
btGImpactBvh::setPrimitiveManager
void setPrimitiveManager(btPrimitiveManagerBase *primitive_manager)
Definition: btGImpactBvh.h:203
btGImpactBvh::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactBvh.h:293
btGImpactBvh::m_primitive_manager
btPrimitiveManagerBase * m_primitive_manager
Definition: btGImpactBvh.h:177
btBvhTree::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactBvh.h:102
btBvhTree::clearNodes
void clearNodes()
Definition: btGImpactBvh.h:89
btGImpactBvh::hasHierarchy
bool hasHierarchy() const
tells if this set has hierarcht
Definition: btGImpactBvh.h:244
GIM_BVH_TREE_NODE
Node Structure for trees.
Definition: btGImpactBvhStructs.h:62
btBvhTree::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactBvh.h:96
btGImpactBvh
Structure for containing Boxes.
Definition: btGImpactBvh.h:173
btBoxCollision.h
GIM_PAIR
Overlapping pair.
Definition: btGImpactBvhStructs.h:34
btGImpactBvh::btGImpactBvh
btGImpactBvh(btPrimitiveManagerBase *primitive_manager)
this constructor doesn&#39;t build the tree. you must call buildSet
Definition: btGImpactBvh.h:191
btGImpactBvh::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactBvh.h:288
btAlignedObjectArray.h
GIM_BVH_DATA_ARRAY
Definition: btGImpactBvh.h:53
btGImpactBvh::get_node_pointer
const GIM_BVH_TREE_NODE * get_node_pointer(int index=0) const
Definition: btGImpactBvh.h:304
btBvhTree::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactBvh.h:133
btGImpactBvh::getNodeTriangle
void getNodeTriangle(int nodeindex, btPrimitiveTriangle &triangle) const
Definition: btGImpactBvh.h:298
btGImpactBvh::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactBvh.h:256
btGImpactBvhStructs.h
btGImpactBvh::getGlobalBox
btAABB getGlobalBox() const
Definition: btGImpactBvh.h:196
btBvhTree::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactBvh.h:127