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 
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;
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:
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 
80 {
82  int m_data;
83 };
84 
87 {
88 public:
90 protected:
92 public:
94  {
96  }
97 
99  {
100  //skipindex is negative (internal node), triangleindex >=0 (leafnode)
101  return (m_escapeIndexOrDataIndex>=0);
102  }
103 
105  {
106  //btAssert(m_escapeIndexOrDataIndex < 0);
107  return -m_escapeIndexOrDataIndex;
108  }
109 
111  {
112  m_escapeIndexOrDataIndex = -index;
113  }
114 
116  {
117  //btAssert(m_escapeIndexOrDataIndex >= 0);
118 
120  }
121 
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 
144 {
145 protected:
148 protected:
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:
158  {
159  m_num_nodes = 0;
160  }
161 
164  void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
165 
167  {
169  m_num_nodes = 0;
170  }
171 
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 
216  {
217  return &m_node_array[index];
218  }
219 
221 };
222 
223 
225 
231 {
232 public:
233 
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 
251 {
252 protected:
255 
256 protected:
257  //stackless refit
258  void refit();
259 public:
260 
263  {
264  m_primitive_manager = NULL;
265  }
266 
269  {
270  m_primitive_manager = primitive_manager;
271  }
272 
274  {
275  btAABB totalbox;
276  getNodeBound(0, totalbox);
277  return totalbox;
278  }
279 
281  {
282  m_primitive_manager = primitive_manager;
283  }
284 
286  {
287  return m_primitive_manager;
288  }
289 
290 
293 
296  {
297  refit();
298  }
299 
301  void buildSet();
302 
304  bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
305 
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 
322  {
323  return true;
324  }
325 
328  {
330  }
331 
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  {
378  }
379 
380 
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
void update()
node manager prototype functions
Definition: btGImpactBvh.h:295
static void find_collision(btGImpactBvh *boxset1, const btTransform &trans1, btGImpactBvh *boxset2, const btTransform &trans2, btPairSet &collision_pairs)
void push_back(const GIM_PAIR &_Val)
GIM_PAIR(int index1, int index2)
Definition: btGImpactBvh.h:51
GIM_BVH_TREE_NODE_ARRAY m_node_array
Definition: btGImpactBvh.h:147
btPrimitiveManagerBase * getPrimitiveManager() const
Definition: btGImpactBvh.h:285
int getDataIndex() const
Definition: btGImpactBvh.h:115
GIM_BVH_DATA is an internal GIMPACT collision structure to contain axis aligned bounding box...
Definition: btGImpactBvh.h:79
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactBvh.h:194
int m_index2
Definition: btGImpactBvh.h:41
virtual ~btPrimitiveManagerBase()
Definition: btGImpactBvh.h:234
virtual int get_primitive_count() const =0
void push_pair_inv(int index1, int index2)
Definition: btGImpactBvh.h:71
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactBvh.h:210
#define SIMD_FORCE_INLINE
Definition: btScalar.h:58
int getEscapeIndex() const
Definition: btGImpactBvh.h:104
void build_tree(GIM_BVH_DATA_ARRAY &primitive_boxes)
prototype functions for box tree management
int m_num_nodes
Definition: btGImpactBvh.h:146
bool isTrimesh() const
tells if this set is a trimesh
Definition: btGImpactBvh.h:327
int getNodeCount() const
node count
Definition: btGImpactBvh.h:173
void _build_sub_tree(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
const GIM_BVH_TREE_NODE * get_node_pointer(int index=0) const
Definition: btGImpactBvh.h:215
btAABB getGlobalBox() const
Definition: btGImpactBvh.h:273
Prototype Base class for primitive classification.
Definition: btGImpactBvh.h:230
void setEscapeIndex(int index)
Definition: btGImpactBvh.h:110
A pairset array.
Definition: btGImpactBvh.h:59
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
void push_pair(int index1, int index2)
Definition: btGImpactBvh.h:66
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.
const GIM_BVH_TREE_NODE * get_node_pointer(int index=0) const
Definition: btGImpactBvh.h:381
int _calc_splitting_axis(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex)
int _sort_and_calc_splitting_index(GIM_BVH_DATA_ARRAY &primitive_boxes, int startIndex, int endIndex, int splitAxis)
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactBvh.h:349
bool hasHierarchy() const
tells if this set has hierarcht
Definition: btGImpactBvh.h:321
virtual void get_primitive_box(int prim_index, btAABB &primbox) const =0
Axis aligned box.
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactBvh.h:179
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactBvh.h:339
btGImpactBvh()
this constructor doesn't build the tree. you must call buildSet
Definition: btGImpactBvh.h:262
int getRightNode(int nodeindex) const
Definition: btGImpactBvh.h:204
Basic Box tree structure.
Definition: btGImpactBvh.h:143
void appy_transform(const btTransform &trans)
Apply a transform to an AABB.
virtual bool is_trimesh() const =0
determines if this manager consist on only triangles, which special case will be optimized ...
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactBvh.h:354
int m_escapeIndexOrDataIndex
Definition: btGImpactBvh.h:91
btBvhTree m_box_tree
Definition: btGImpactBvh.h:253
bool isLeafNode() const
Definition: btGImpactBvh.h:98
void buildSet()
this rebuild the entire set
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactBvh.h:370
virtual void get_primitive_triangle(int prim_index, btPrimitiveTriangle &triangle) const =0
retrieves only the points of the triangle, and the collision margin
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
void setPrimitiveManager(btPrimitiveManagerBase *primitive_manager)
Definition: btGImpactBvh.h:280
btAABB m_bound
Definition: btGImpactBvh.h:81
btPrimitiveManagerBase * m_primitive_manager
Definition: btGImpactBvh.h:254
bool boxQuery(const btAABB &box, btAlignedObjectArray< int > &collided_results) const
returns the indices of the primitives in the m_primitive_manager
GIM_PAIR(const GIM_PAIR &p)
Definition: btGImpactBvh.h:45
int getNodeData(int nodeindex) const
Definition: btGImpactBvh.h:184
void clearNodes()
Definition: btGImpactBvh.h:166
int getLeftNode(int nodeindex) const
Definition: btGImpactBvh.h:199
int getNodeData(int nodeindex) const
Definition: btGImpactBvh.h:344
Node Structure for trees.
Definition: btGImpactBvh.h:86
Structure for containing Boxes.
Definition: btGImpactBvh.h:250
Overlapping pair.
Definition: btGImpactBvh.h:38
btGImpactBvh(btPrimitiveManagerBase *primitive_manager)
this constructor doesn't build the tree. you must call buildSet
Definition: btGImpactBvh.h:268
int getNodeCount() const
node count
Definition: btGImpactBvh.h:333
void getNodeTriangle(int nodeindex, btPrimitiveTriangle &triangle) const
Definition: btGImpactBvh.h:375
void setDataIndex(int index)
Definition: btGImpactBvh.h:122
int getLeftNode(int nodeindex) const
Definition: btGImpactBvh.h:360
int getRightNode(int nodeindex) const
Definition: btGImpactBvh.h:365
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
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactBvh.h:189
int m_index1
Definition: btGImpactBvh.h:40