1 #ifndef GIM_QUANTIZED_SET_H_INCLUDED
2 #define GIM_QUANTIZED_SET_H_INCLUDED
4 /*! \file btGImpactQuantizedBvh.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 "btGImpactBvh.h"
28 #include "btQuantization.h"
29 #include "btGImpactQuantizedBvhStructs.h"
31 class GIM_QUANTIZED_BVH_NODE_ARRAY : public btAlignedObjectArray<BT_QUANTIZED_BVH_NODE>
35 //! Basic Box tree structure
36 class btQuantizedBvhTree
40 GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array;
41 btAABB m_global_bound;
42 btVector3 m_bvhQuantization;
45 void calc_quantization(GIM_BVH_DATA_ARRAY& primitive_boxes, btScalar boundMargin = btScalar(1.0));
47 int _sort_and_calc_splitting_index(
48 GIM_BVH_DATA_ARRAY& primitive_boxes,
49 int startIndex, int endIndex, int splitAxis);
51 int _calc_splitting_axis(GIM_BVH_DATA_ARRAY& primitive_boxes, int startIndex, int endIndex);
53 void _build_sub_tree(GIM_BVH_DATA_ARRAY& primitive_boxes, int startIndex, int endIndex);
61 //! prototype functions for box tree management
63 void build_tree(GIM_BVH_DATA_ARRAY& primitive_boxes);
65 SIMD_FORCE_INLINE void quantizePoint(
66 unsigned short* quantizedpoint, const btVector3& point) const
68 bt_quantize_clamp(quantizedpoint, point, m_global_bound.m_min, m_global_bound.m_max, m_bvhQuantization);
71 SIMD_FORCE_INLINE bool testQuantizedBoxOverlapp(
73 unsigned short* quantizedMin, unsigned short* quantizedMax) const
75 return m_node_array[node_index].testQuantizedBoxOverlapp(quantizedMin, quantizedMax);
78 SIMD_FORCE_INLINE void clearNodes()
85 SIMD_FORCE_INLINE int getNodeCount() const
90 //! tells if the node is a leaf
91 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
93 return m_node_array[nodeindex].isLeafNode();
96 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
98 return m_node_array[nodeindex].getDataIndex();
101 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB& bound) const
103 bound.m_min = bt_unquantize(
104 m_node_array[nodeindex].m_quantizedAabbMin,
105 m_global_bound.m_min, m_bvhQuantization);
107 bound.m_max = bt_unquantize(
108 m_node_array[nodeindex].m_quantizedAabbMax,
109 m_global_bound.m_min, m_bvhQuantization);
112 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB& bound)
114 bt_quantize_clamp(m_node_array[nodeindex].m_quantizedAabbMin,
116 m_global_bound.m_min,
117 m_global_bound.m_max,
120 bt_quantize_clamp(m_node_array[nodeindex].m_quantizedAabbMax,
122 m_global_bound.m_min,
123 m_global_bound.m_max,
127 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
129 return nodeindex + 1;
132 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
134 if (m_node_array[nodeindex + 1].isLeafNode()) return nodeindex + 2;
135 return nodeindex + 1 + m_node_array[nodeindex + 1].getEscapeIndex();
138 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
140 return m_node_array[nodeindex].getEscapeIndex();
143 SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE* get_node_pointer(int index = 0) const
145 return &m_node_array[index];
151 //! Structure for containing Boxes
153 This class offers an structure for managing a box tree of primitives.
154 Requires a Primitive prototype (like btPrimitiveManagerBase )
156 class btGImpactQuantizedBvh
159 btQuantizedBvhTree m_box_tree;
160 btPrimitiveManagerBase* m_primitive_manager;
167 //! this constructor doesn't build the tree. you must call buildSet
168 btGImpactQuantizedBvh()
170 m_primitive_manager = NULL;
173 //! this constructor doesn't build the tree. you must call buildSet
174 btGImpactQuantizedBvh(btPrimitiveManagerBase* primitive_manager)
176 m_primitive_manager = primitive_manager;
179 SIMD_FORCE_INLINE btAABB getGlobalBox() const
182 getNodeBound(0, totalbox);
186 SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase* primitive_manager)
188 m_primitive_manager = primitive_manager;
191 SIMD_FORCE_INLINE btPrimitiveManagerBase* getPrimitiveManager() const
193 return m_primitive_manager;
196 //! node manager prototype functions
199 //! this attemps to refit the box set.
200 SIMD_FORCE_INLINE void update()
205 //! this rebuild the entire set
208 //! returns the indices of the primitives in the m_primitive_manager
209 bool boxQuery(const btAABB& box, btAlignedObjectArray<int>& collided_results) const;
211 //! returns the indices of the primitives in the m_primitive_manager
212 SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB& box,
213 const btTransform& transform, btAlignedObjectArray<int>& collided_results) const
215 btAABB transbox = box;
216 transbox.appy_transform(transform);
217 return boxQuery(transbox, collided_results);
220 //! returns the indices of the primitives in the m_primitive_manager
222 const btVector3& ray_dir, const btVector3& ray_origin,
223 btAlignedObjectArray<int>& collided_results) const;
225 //! tells if this set has hierarcht
226 SIMD_FORCE_INLINE bool hasHierarchy() const
231 //! tells if this set is a trimesh
232 SIMD_FORCE_INLINE bool isTrimesh() const
234 return m_primitive_manager->is_trimesh();
238 SIMD_FORCE_INLINE int getNodeCount() const
240 return m_box_tree.getNodeCount();
243 //! tells if the node is a leaf
244 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
246 return m_box_tree.isLeafNode(nodeindex);
249 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
251 return m_box_tree.getNodeData(nodeindex);
254 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB& bound) const
256 m_box_tree.getNodeBound(nodeindex, bound);
259 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB& bound)
261 m_box_tree.setNodeBound(nodeindex, bound);
264 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
266 return m_box_tree.getLeftNode(nodeindex);
269 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
271 return m_box_tree.getRightNode(nodeindex);
274 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
276 return m_box_tree.getEscapeNodeIndex(nodeindex);
279 SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex, btPrimitiveTriangle& triangle) const
281 m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex), triangle);
284 SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE* get_node_pointer(int index = 0) const
286 return m_box_tree.get_node_pointer(index);
289 #ifdef TRI_COLLISION_PROFILING
290 static float getAverageTreeCollisionTime();
291 #endif //TRI_COLLISION_PROFILING
293 static void find_collision(const btGImpactQuantizedBvh* boxset1, const btTransform& trans1,
294 const btGImpactQuantizedBvh* boxset2, const btTransform& trans2,
295 btPairSet& collision_pairs);
298 #endif // GIM_BOXPRUNING_H_INCLUDED