1 #ifndef BT_GIMPACT_BVH_H_INCLUDED
2 #define BT_GIMPACT_BVH_H_INCLUDED
4 /*! \file gim_box_set.h
5 \author Francisco Leon Najera
8 This source file is part of GIMPACT Library.
10 For the latest info, see http://gimpact.sourceforge.net/
12 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13 email: projectileman@yahoo.com
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:
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.
27 #include "LinearMath/btAlignedObjectArray.h"
29 #include "btBoxCollision.h"
30 #include "btTriangleShapeEx.h"
31 #include "btGImpactBvhStructs.h"
34 class btPairSet : public btAlignedObjectArray<GIM_PAIR>
41 inline void push_pair(int index1, int index2)
43 push_back(GIM_PAIR(index1, index2));
46 inline void push_pair_inv(int index1, int index2)
48 push_back(GIM_PAIR(index2, index1));
52 class GIM_BVH_DATA_ARRAY : public btAlignedObjectArray<GIM_BVH_DATA>
56 class GIM_BVH_TREE_NODE_ARRAY : public btAlignedObjectArray<GIM_BVH_TREE_NODE>
60 //! Basic Box tree structure
65 GIM_BVH_TREE_NODE_ARRAY m_node_array;
68 int _sort_and_calc_splitting_index(
69 GIM_BVH_DATA_ARRAY& primitive_boxes,
70 int startIndex, int endIndex, int splitAxis);
72 int _calc_splitting_axis(GIM_BVH_DATA_ARRAY& primitive_boxes, int startIndex, int endIndex);
74 void _build_sub_tree(GIM_BVH_DATA_ARRAY& primitive_boxes, int startIndex, int endIndex);
82 //! prototype functions for box tree management
84 void build_tree(GIM_BVH_DATA_ARRAY& primitive_boxes);
86 SIMD_FORCE_INLINE void clearNodes()
93 SIMD_FORCE_INLINE int getNodeCount() const
98 //! tells if the node is a leaf
99 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
101 return m_node_array[nodeindex].isLeafNode();
104 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
106 return m_node_array[nodeindex].getDataIndex();
109 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB& bound) const
111 bound = m_node_array[nodeindex].m_bound;
114 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB& bound)
116 m_node_array[nodeindex].m_bound = bound;
119 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
121 return nodeindex + 1;
124 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
126 if (m_node_array[nodeindex + 1].isLeafNode()) return nodeindex + 2;
127 return nodeindex + 1 + m_node_array[nodeindex + 1].getEscapeIndex();
130 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
132 return m_node_array[nodeindex].getEscapeIndex();
135 SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE* get_node_pointer(int index = 0) const
137 return &m_node_array[index];
143 //! Prototype Base class for primitive classification
145 This class is a wrapper for primitive collections.
146 This tells relevant info for the Bounding Box set classes, which take care of space classification.
147 This class can manage Compound shapes and trimeshes, and if it is managing trimesh then the Hierarchy Bounding Box classes will take advantage of primitive Vs Box overlapping tests for getting optimal results and less Per Box compairisons.
149 class btPrimitiveManagerBase
152 virtual ~btPrimitiveManagerBase() {}
154 //! determines if this manager consist on only triangles, which special case will be optimized
155 virtual bool is_trimesh() const = 0;
156 virtual int get_primitive_count() const = 0;
157 virtual void get_primitive_box(int prim_index, btAABB& primbox) const = 0;
158 //! retrieves only the points of the triangle, and the collision margin
159 virtual void get_primitive_triangle(int prim_index, btPrimitiveTriangle& triangle) const = 0;
162 //! Structure for containing Boxes
164 This class offers an structure for managing a box tree of primitives.
165 Requires a Primitive prototype (like btPrimitiveManagerBase )
170 btBvhTree m_box_tree;
171 btPrimitiveManagerBase* m_primitive_manager;
178 //! this constructor doesn't build the tree. you must call buildSet
181 m_primitive_manager = NULL;
184 //! this constructor doesn't build the tree. you must call buildSet
185 btGImpactBvh(btPrimitiveManagerBase* primitive_manager)
187 m_primitive_manager = primitive_manager;
190 SIMD_FORCE_INLINE btAABB getGlobalBox() const
193 getNodeBound(0, totalbox);
197 SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase* primitive_manager)
199 m_primitive_manager = primitive_manager;
202 SIMD_FORCE_INLINE btPrimitiveManagerBase* getPrimitiveManager() const
204 return m_primitive_manager;
207 //! node manager prototype functions
210 //! this attemps to refit the box set.
211 SIMD_FORCE_INLINE void update()
216 //! this rebuild the entire set
219 //! returns the indices of the primitives in the m_primitive_manager
220 bool boxQuery(const btAABB& box, btAlignedObjectArray<int>& collided_results) const;
222 //! returns the indices of the primitives in the m_primitive_manager
223 SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB& box,
224 const btTransform& transform, btAlignedObjectArray<int>& collided_results) const
226 btAABB transbox = box;
227 transbox.appy_transform(transform);
228 return boxQuery(transbox, collided_results);
231 //! returns the indices of the primitives in the m_primitive_manager
233 const btVector3& ray_dir, const btVector3& ray_origin,
234 btAlignedObjectArray<int>& collided_results) const;
236 //! tells if this set has hierarcht
237 SIMD_FORCE_INLINE bool hasHierarchy() const
242 //! tells if this set is a trimesh
243 SIMD_FORCE_INLINE bool isTrimesh() const
245 return m_primitive_manager->is_trimesh();
249 SIMD_FORCE_INLINE int getNodeCount() const
251 return m_box_tree.getNodeCount();
254 //! tells if the node is a leaf
255 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
257 return m_box_tree.isLeafNode(nodeindex);
260 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
262 return m_box_tree.getNodeData(nodeindex);
265 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB& bound) const
267 m_box_tree.getNodeBound(nodeindex, bound);
270 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB& bound)
272 m_box_tree.setNodeBound(nodeindex, bound);
275 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
277 return m_box_tree.getLeftNode(nodeindex);
280 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
282 return m_box_tree.getRightNode(nodeindex);
285 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
287 return m_box_tree.getEscapeNodeIndex(nodeindex);
290 SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex, btPrimitiveTriangle& triangle) const
292 m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex), triangle);
295 SIMD_FORCE_INLINE const GIM_BVH_TREE_NODE* get_node_pointer(int index = 0) const
297 return m_box_tree.get_node_pointer(index);
300 #ifdef TRI_COLLISION_PROFILING
301 static float getAverageTreeCollisionTime();
302 #endif //TRI_COLLISION_PROFILING
304 static void find_collision(btGImpactBvh* boxset1, const btTransform& trans1,
305 btGImpactBvh* boxset2, const btTransform& trans2,
306 btPairSet& collision_pairs);
309 #endif // BT_GIMPACT_BVH_H_INCLUDED