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

Bullet Collision Detection & Physics Library

btGImpactQuantizedBvh.h
Go to the documentation of this file.
1 #ifndef GIM_QUANTIZED_SET_H_INCLUDED
2 #define GIM_QUANTIZED_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 #include "btGImpactBvh.h"
28 #include "btQuantization.h"
29 #include "btGImpactQuantizedBvhStructs.h"
30
31 class GIM_QUANTIZED_BVH_NODE_ARRAY:public btAlignedObjectArray<BT_QUANTIZED_BVH_NODE>
32 {
33 };
34
35
36
37
39 class btQuantizedBvhTree
40 {
41 protected:
42 int m_num_nodes;
43 GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array;
44 btAABB m_global_bound;
45 btVector3 m_bvhQuantization;
46 protected:
47 void calc_quantization(GIM_BVH_DATA_ARRAY & primitive_boxes, btScalar boundMargin = btScalar(1.0) );
48
49 int _sort_and_calc_splitting_index(
50 GIM_BVH_DATA_ARRAY & primitive_boxes,
51 int startIndex, int endIndex, int splitAxis);
52
53 int _calc_splitting_axis(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
54
55 void _build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
56 public:
57 btQuantizedBvhTree()
58 {
59 m_num_nodes = 0;
60 }
61
64 void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
65
66 SIMD_FORCE_INLINE void quantizePoint(
67 unsigned short * quantizedpoint, const btVector3 & point) const
68 {
69 bt_quantize_clamp(quantizedpoint,point,m_global_bound.m_min,m_global_bound.m_max,m_bvhQuantization);
70 }
71
72
73 SIMD_FORCE_INLINE bool testQuantizedBoxOverlapp(
74 int node_index,
75 unsigned short * quantizedMin,unsigned short * quantizedMax) const
76 {
77 return m_node_array[node_index].testQuantizedBoxOverlapp(quantizedMin,quantizedMax);
78 }
79
80 SIMD_FORCE_INLINE void clearNodes()
81 {
82 m_node_array.clear();
83 m_num_nodes = 0;
84 }
85
87 SIMD_FORCE_INLINE int getNodeCount() const
88 {
89 return m_num_nodes;
90 }
91
93 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
94 {
95 return m_node_array[nodeindex].isLeafNode();
96 }
97
98 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
99 {
100 return m_node_array[nodeindex].getDataIndex();
101 }
102
103 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
104 {
105 bound.m_min = bt_unquantize(
106 m_node_array[nodeindex].m_quantizedAabbMin,
107 m_global_bound.m_min,m_bvhQuantization);
108
109 bound.m_max = bt_unquantize(
110 m_node_array[nodeindex].m_quantizedAabbMax,
111 m_global_bound.m_min,m_bvhQuantization);
112 }
113
114 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
115 {
116 bt_quantize_clamp( m_node_array[nodeindex].m_quantizedAabbMin,
117 bound.m_min,
118 m_global_bound.m_min,
119 m_global_bound.m_max,
120 m_bvhQuantization);
121
122 bt_quantize_clamp( m_node_array[nodeindex].m_quantizedAabbMax,
123 bound.m_max,
124 m_global_bound.m_min,
125 m_global_bound.m_max,
126 m_bvhQuantization);
127 }
128
129 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
130 {
131 return nodeindex+1;
132 }
133
134 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
135 {
136 if(m_node_array[nodeindex+1].isLeafNode()) return nodeindex+2;
137 return nodeindex+1 + m_node_array[nodeindex+1].getEscapeIndex();
138 }
139
140 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
141 {
142 return m_node_array[nodeindex].getEscapeIndex();
143 }
144
145 SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index = 0) const
146 {
147 return &m_node_array[index];
148 }
149
151 };
152
153
154
156
160 class btGImpactQuantizedBvh
161 {
162 protected:
163 btQuantizedBvhTree m_box_tree;
164 btPrimitiveManagerBase * m_primitive_manager;
165
166 protected:
167 //stackless refit
168 void refit();
169 public:
170
172 btGImpactQuantizedBvh()
173 {
174 m_primitive_manager = NULL;
175 }
176
178 btGImpactQuantizedBvh(btPrimitiveManagerBase * primitive_manager)
179 {
180 m_primitive_manager = primitive_manager;
181 }
182
183 SIMD_FORCE_INLINE btAABB getGlobalBox() const
184 {
185 btAABB totalbox;
186 getNodeBound(0, totalbox);
187 return totalbox;
188 }
189
190 SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase * primitive_manager)
191 {
192 m_primitive_manager = primitive_manager;
193 }
194
195 SIMD_FORCE_INLINE btPrimitiveManagerBase * getPrimitiveManager() const
196 {
197 return m_primitive_manager;
198 }
199
200
203
205 SIMD_FORCE_INLINE void update()
206 {
207 refit();
208 }
209
211 void buildSet();
212
214 bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
215
217 SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB & box,
218 const btTransform & transform, btAlignedObjectArray<int> & collided_results) const
219 {
220 btAABB transbox=box;
221 transbox.appy_transform(transform);
222 return boxQuery(transbox,collided_results);
223 }
224
226 bool rayQuery(
227 const btVector3 & ray_dir,const btVector3 & ray_origin ,
228 btAlignedObjectArray<int> & collided_results) const;
229
231 SIMD_FORCE_INLINE bool hasHierarchy() const
232 {
233 return true;
234 }
235
237 SIMD_FORCE_INLINE bool isTrimesh() const
238 {
239 return m_primitive_manager->is_trimesh();
240 }
241
243 SIMD_FORCE_INLINE int getNodeCount() const
244 {
245 return m_box_tree.getNodeCount();
246 }
247
249 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
250 {
251 return m_box_tree.isLeafNode(nodeindex);
252 }
253
254 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
255 {
256 return m_box_tree.getNodeData(nodeindex);
257 }
258
259 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
260 {
261 m_box_tree.getNodeBound(nodeindex, bound);
262 }
263
264 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
265 {
266 m_box_tree.setNodeBound(nodeindex, bound);
267 }
268
269
270 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
271 {
272 return m_box_tree.getLeftNode(nodeindex);
273 }
274
275 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
276 {
277 return m_box_tree.getRightNode(nodeindex);
278 }
279
280 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
281 {
282 return m_box_tree.getEscapeNodeIndex(nodeindex);
283 }
284
285 SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex,btPrimitiveTriangle & triangle) const
286 {
287 m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex),triangle);
288 }
289
290
291 SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index = 0) const
292 {
293 return m_box_tree.get_node_pointer(index);
294 }
295
296 #ifdef TRI_COLLISION_PROFILING
297 static float getAverageTreeCollisionTime();
298 #endif //TRI_COLLISION_PROFILING
299
300 static void find_collision(const btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
301 const btGImpactQuantizedBvh * boxset2, const btTransform & trans2,
302 btPairSet & collision_pairs);
303 };
304
305 #endif // GIM_BOXPRUNING_H_INCLUDED
btGImpactBvh.h
btPrimitiveTriangle
Definition: btTriangleShapeEx.h:74
btGImpactQuantizedBvh::btGImpactQuantizedBvh
btGImpactQuantizedBvh()
this constructor doesn&#39;t build the tree. you must call buildSet
Definition: btGImpactQuantizedBvh.h:172
btQuantizedBvhTree::m_bvhQuantization
btVector3 m_bvhQuantization
Definition: btGImpactQuantizedBvh.h:45
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:53
btAABB::m_max
btVector3 m_max
Definition: btBoxCollision.h:233
btGImpactQuantizedBvh::getPrimitiveManager
btPrimitiveManagerBase * getPrimitiveManager() const
Definition: btGImpactQuantizedBvh.h:195
btGImpactQuantizedBvh::getGlobalBox
btAABB getGlobalBox() const
Definition: btGImpactQuantizedBvh.h:183
btGImpactQuantizedBvh::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:254
GIM_QUANTIZED_BVH_NODE_ARRAY
Definition: btGImpactQuantizedBvh.h:31
btQuantizedBvhTree::testQuantizedBoxOverlapp
bool testQuantizedBoxOverlapp(int node_index, unsigned short *quantizedMin, unsigned short *quantizedMax) const
Definition: btGImpactQuantizedBvh.h:73
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
bt_unquantize
btVector3 bt_unquantize(const unsigned short *vecIn, const btVector3 &offset, const btVector3 &bvhQuantization)
Definition: btQuantization.h:72
btAABB::m_min
btVector3 m_min
Definition: btBoxCollision.h:232
btGImpactQuantizedBvh::get_node_pointer
const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index=0) const
Definition: btGImpactQuantizedBvh.h:291
btGImpactQuantizedBvh::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:280
btGImpactQuantizedBvh::m_box_tree
btQuantizedBvhTree m_box_tree
Definition: btGImpactQuantizedBvh.h:163
btPrimitiveManagerBase
Prototype Base class for primitive classification.
Definition: btGImpactBvh.h:153
btPairSet
A pairset array.
Definition: btGImpactBvh.h:35
btQuantizedBvhTree::btQuantizedBvhTree
btQuantizedBvhTree()
Definition: btGImpactQuantizedBvh.h:57
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
bt_quantize_clamp
void bt_quantize_clamp(unsigned short *out, const btVector3 &point, const btVector3 &min_bound, const btVector3 &max_bound, const btVector3 &bvhQuantization)
Definition: btQuantization.h:53
btQuantizedBvhTree::get_node_pointer
const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index=0) const
Definition: btGImpactQuantizedBvh.h:145
btQuantization.h
btQuantizedBvhTree::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactQuantizedBvh.h:93
btGImpactQuantizedBvh::isTrimesh
bool isTrimesh() const
tells if this set is a trimesh
Definition: btGImpactQuantizedBvh.h:237
btGImpactQuantizedBvh::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactQuantizedBvh.h:264
btQuantizedBvhTree::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactQuantizedBvh.h:87
btGImpactQuantizedBvhStructs.h
btGImpactQuantizedBvh::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactQuantizedBvh.h:249
btGImpactQuantizedBvh::update
void update()
node manager prototype functions
Definition: btGImpactQuantizedBvh.h:205
btQuantizedBvhTree::quantizePoint
void quantizePoint(unsigned short *quantizedpoint, const btVector3 &point) const
Definition: btGImpactQuantizedBvh.h:66
btGImpactQuantizedBvh::setPrimitiveManager
void setPrimitiveManager(btPrimitiveManagerBase *primitive_manager)
Definition: btGImpactQuantizedBvh.h:190
btAABB
Axis aligned box.
Definition: btBoxCollision.h:229
btGImpactQuantizedBvh::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactQuantizedBvh.h:243
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 ...
btQuantizedBvhTree::m_global_bound
btAABB m_global_bound
Definition: btGImpactQuantizedBvh.h:44
btQuantizedBvhTree::m_node_array
GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array
Definition: btGImpactQuantizedBvh.h:43
btGImpactQuantizedBvh::getNodeTriangle
void getNodeTriangle(int nodeindex, btPrimitiveTriangle &triangle) const
Definition: btGImpactQuantizedBvh.h:285
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btQuantizedBvhTree::m_num_nodes
int m_num_nodes
Definition: btGImpactQuantizedBvh.h:42
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
btGImpactQuantizedBvh::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactQuantizedBvh.h:259
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btQuantizedBvhTree
Basic Box tree structure.
Definition: btGImpactQuantizedBvh.h:39
btGImpactQuantizedBvh::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:270
btGImpactQuantizedBvh::m_primitive_manager
btPrimitiveManagerBase * m_primitive_manager
Definition: btGImpactQuantizedBvh.h:164
btGImpactQuantizedBvh::hasHierarchy
bool hasHierarchy() const
tells if this set has hierarcht
Definition: btGImpactQuantizedBvh.h:231
btGImpactQuantizedBvh::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:275
btQuantizedBvhTree::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:129
btGImpactQuantizedBvh::btGImpactQuantizedBvh
btGImpactQuantizedBvh(btPrimitiveManagerBase *primitive_manager)
this constructor doesn&#39;t build the tree. you must call buildSet
Definition: btGImpactQuantizedBvh.h:178
GIM_BVH_DATA_ARRAY
Definition: btGImpactBvh.h:53
BT_QUANTIZED_BVH_NODE
btQuantizedBvhNode is a compressed aabb node, 16 bytes.
Definition: btGImpactQuantizedBvhStructs.h:32
btQuantizedBvhTree::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:98
btGImpactQuantizedBvh
Structure for containing Boxes.
Definition: btGImpactQuantizedBvh.h:160
btQuantizedBvhTree::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactQuantizedBvh.h:114
btQuantizedBvhTree::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:134
btQuantizedBvhTree::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:140
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btGImpactQuantizedBvh::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: btGImpactQuantizedBvh.h:217
btQuantizedBvhTree::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactQuantizedBvh.h:103
btQuantizedBvhTree::clearNodes
void clearNodes()
Definition: btGImpactQuantizedBvh.h:80