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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
30
31
32
33
36 ATTRIBUTE_ALIGNED16 (struct) BT_QUANTIZED_BVH_NODE
37 {
38 //12 bytes
39 unsigned short int m_quantizedAabbMin[3];
40 unsigned short int m_quantizedAabbMax[3];
41 //4 bytes
42 int m_escapeIndexOrDataIndex;
43
44 BT_QUANTIZED_BVH_NODE()
45 {
46 m_escapeIndexOrDataIndex = 0;
47 }
48
49 SIMD_FORCE_INLINE bool isLeafNode() const
50 {
51 //skipindex is negative (internal node), triangleindex >=0 (leafnode)
52 return (m_escapeIndexOrDataIndex>=0);
53 }
54
55 SIMD_FORCE_INLINE int getEscapeIndex() const
56 {
57 //btAssert(m_escapeIndexOrDataIndex < 0);
58 return -m_escapeIndexOrDataIndex;
59 }
60
61 SIMD_FORCE_INLINE void setEscapeIndex(int index)
62 {
63 m_escapeIndexOrDataIndex = -index;
64 }
65
66 SIMD_FORCE_INLINE int getDataIndex() const
67 {
68 //btAssert(m_escapeIndexOrDataIndex >= 0);
69
70 return m_escapeIndexOrDataIndex;
71 }
72
73 SIMD_FORCE_INLINE void setDataIndex(int index)
74 {
75 m_escapeIndexOrDataIndex = index;
76 }
77
78 SIMD_FORCE_INLINE bool testQuantizedBoxOverlapp(
79 unsigned short * quantizedMin,unsigned short * quantizedMax) const
80 {
81 if(m_quantizedAabbMin[0] > quantizedMax[0] ||
82 m_quantizedAabbMax[0] < quantizedMin[0] ||
83 m_quantizedAabbMin[1] > quantizedMax[1] ||
84 m_quantizedAabbMax[1] < quantizedMin[1] ||
85 m_quantizedAabbMin[2] > quantizedMax[2] ||
86 m_quantizedAabbMax[2] < quantizedMin[2])
87 {
88 return false;
89 }
90 return true;
91 }
92
93 };
94
95
96
97 class GIM_QUANTIZED_BVH_NODE_ARRAY:public btAlignedObjectArray<BT_QUANTIZED_BVH_NODE>
98 {
99 };
100
101
102
103
105 class btQuantizedBvhTree
106 {
107 protected:
108 int m_num_nodes;
109 GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array;
110 btAABB m_global_bound;
111 btVector3 m_bvhQuantization;
112 protected:
113 void calc_quantization(GIM_BVH_DATA_ARRAY & primitive_boxes, btScalar boundMargin = btScalar(1.0) );
114
115 int _sort_and_calc_splitting_index(
116 GIM_BVH_DATA_ARRAY & primitive_boxes,
117 int startIndex, int endIndex, int splitAxis);
118
119 int _calc_splitting_axis(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
120
121 void _build_sub_tree(GIM_BVH_DATA_ARRAY & primitive_boxes, int startIndex, int endIndex);
122 public:
123 btQuantizedBvhTree()
124 {
125 m_num_nodes = 0;
126 }
127
130 void build_tree(GIM_BVH_DATA_ARRAY & primitive_boxes);
131
132 SIMD_FORCE_INLINE void quantizePoint(
133 unsigned short * quantizedpoint, const btVector3 & point) const
134 {
135 bt_quantize_clamp(quantizedpoint,point,m_global_bound.m_min,m_global_bound.m_max,m_bvhQuantization);
136 }
137
138
139 SIMD_FORCE_INLINE bool testQuantizedBoxOverlapp(
140 int node_index,
141 unsigned short * quantizedMin,unsigned short * quantizedMax) const
142 {
143 return m_node_array[node_index].testQuantizedBoxOverlapp(quantizedMin,quantizedMax);
144 }
145
146 SIMD_FORCE_INLINE void clearNodes()
147 {
148 m_node_array.clear();
149 m_num_nodes = 0;
150 }
151
153 SIMD_FORCE_INLINE int getNodeCount() const
154 {
155 return m_num_nodes;
156 }
157
159 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
160 {
161 return m_node_array[nodeindex].isLeafNode();
162 }
163
164 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
165 {
166 return m_node_array[nodeindex].getDataIndex();
167 }
168
169 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
170 {
171 bound.m_min = bt_unquantize(
172 m_node_array[nodeindex].m_quantizedAabbMin,
173 m_global_bound.m_min,m_bvhQuantization);
174
175 bound.m_max = bt_unquantize(
176 m_node_array[nodeindex].m_quantizedAabbMax,
177 m_global_bound.m_min,m_bvhQuantization);
178 }
179
180 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
181 {
182 bt_quantize_clamp( m_node_array[nodeindex].m_quantizedAabbMin,
183 bound.m_min,
184 m_global_bound.m_min,
185 m_global_bound.m_max,
186 m_bvhQuantization);
187
188 bt_quantize_clamp( m_node_array[nodeindex].m_quantizedAabbMax,
189 bound.m_max,
190 m_global_bound.m_min,
191 m_global_bound.m_max,
192 m_bvhQuantization);
193 }
194
195 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
196 {
197 return nodeindex+1;
198 }
199
200 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
201 {
202 if(m_node_array[nodeindex+1].isLeafNode()) return nodeindex+2;
203 return nodeindex+1 + m_node_array[nodeindex+1].getEscapeIndex();
204 }
205
206 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
207 {
208 return m_node_array[nodeindex].getEscapeIndex();
209 }
210
211 SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index = 0) const
212 {
213 return &m_node_array[index];
214 }
215
217 };
218
219
220
222
226 class btGImpactQuantizedBvh
227 {
228 protected:
229 btQuantizedBvhTree m_box_tree;
230 btPrimitiveManagerBase * m_primitive_manager;
231
232 protected:
233 //stackless refit
234 void refit();
235 public:
236
238 btGImpactQuantizedBvh()
239 {
240 m_primitive_manager = NULL;
241 }
242
244 btGImpactQuantizedBvh(btPrimitiveManagerBase * primitive_manager)
245 {
246 m_primitive_manager = primitive_manager;
247 }
248
249 SIMD_FORCE_INLINE btAABB getGlobalBox() const
250 {
251 btAABB totalbox;
252 getNodeBound(0, totalbox);
253 return totalbox;
254 }
255
256 SIMD_FORCE_INLINE void setPrimitiveManager(btPrimitiveManagerBase * primitive_manager)
257 {
258 m_primitive_manager = primitive_manager;
259 }
260
261 SIMD_FORCE_INLINE btPrimitiveManagerBase * getPrimitiveManager() const
262 {
263 return m_primitive_manager;
264 }
265
266
269
271 SIMD_FORCE_INLINE void update()
272 {
273 refit();
274 }
275
277 void buildSet();
278
280 bool boxQuery(const btAABB & box, btAlignedObjectArray<int> & collided_results) const;
281
283 SIMD_FORCE_INLINE bool boxQueryTrans(const btAABB & box,
284 const btTransform & transform, btAlignedObjectArray<int> & collided_results) const
285 {
286 btAABB transbox=box;
287 transbox.appy_transform(transform);
288 return boxQuery(transbox,collided_results);
289 }
290
292 bool rayQuery(
293 const btVector3 & ray_dir,const btVector3 & ray_origin ,
294 btAlignedObjectArray<int> & collided_results) const;
295
297 SIMD_FORCE_INLINE bool hasHierarchy() const
298 {
299 return true;
300 }
301
303 SIMD_FORCE_INLINE bool isTrimesh() const
304 {
305 return m_primitive_manager->is_trimesh();
306 }
307
309 SIMD_FORCE_INLINE int getNodeCount() const
310 {
311 return m_box_tree.getNodeCount();
312 }
313
315 SIMD_FORCE_INLINE bool isLeafNode(int nodeindex) const
316 {
317 return m_box_tree.isLeafNode(nodeindex);
318 }
319
320 SIMD_FORCE_INLINE int getNodeData(int nodeindex) const
321 {
322 return m_box_tree.getNodeData(nodeindex);
323 }
324
325 SIMD_FORCE_INLINE void getNodeBound(int nodeindex, btAABB & bound) const
326 {
327 m_box_tree.getNodeBound(nodeindex, bound);
328 }
329
330 SIMD_FORCE_INLINE void setNodeBound(int nodeindex, const btAABB & bound)
331 {
332 m_box_tree.setNodeBound(nodeindex, bound);
333 }
334
335
336 SIMD_FORCE_INLINE int getLeftNode(int nodeindex) const
337 {
338 return m_box_tree.getLeftNode(nodeindex);
339 }
340
341 SIMD_FORCE_INLINE int getRightNode(int nodeindex) const
342 {
343 return m_box_tree.getRightNode(nodeindex);
344 }
345
346 SIMD_FORCE_INLINE int getEscapeNodeIndex(int nodeindex) const
347 {
348 return m_box_tree.getEscapeNodeIndex(nodeindex);
349 }
350
351 SIMD_FORCE_INLINE void getNodeTriangle(int nodeindex,btPrimitiveTriangle & triangle) const
352 {
353 m_primitive_manager->get_primitive_triangle(getNodeData(nodeindex),triangle);
354 }
355
356
357 SIMD_FORCE_INLINE const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index = 0) const
358 {
359 return m_box_tree.get_node_pointer(index);
360 }
361
362 #ifdef TRI_COLLISION_PROFILING
363 static float getAverageTreeCollisionTime();
364 #endif //TRI_COLLISION_PROFILING
365
366 static void find_collision(const btGImpactQuantizedBvh * boxset1, const btTransform & trans1,
367 const btGImpactQuantizedBvh * boxset2, const btTransform & trans2,
368 btPairSet & collision_pairs);
369 };
370
371
372 #endif // GIM_BOXPRUNING_H_INCLUDED
btGImpactQuantizedBvh::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:341
btGImpactBvh.h
BT_QUANTIZED_BVH_NODE::BT_QUANTIZED_BVH_NODE
BT_QUANTIZED_BVH_NODE()
Definition: btGImpactQuantizedBvh.h:44
BT_QUANTIZED_BVH_NODE::testQuantizedBoxOverlapp
bool testQuantizedBoxOverlapp(unsigned short *quantizedMin, unsigned short *quantizedMax) const
Definition: btGImpactQuantizedBvh.h:78
BT_QUANTIZED_BVH_NODE::getEscapeIndex
int getEscapeIndex() const
Definition: btGImpactQuantizedBvh.h:55
btPrimitiveTriangle
Definition: btTriangleShapeEx.h:74
btGImpactQuantizedBvh::btGImpactQuantizedBvh
btGImpactQuantizedBvh()
this constructor doesn&#39;t build the tree. you must call buildSet
Definition: btGImpactQuantizedBvh.h:238
btQuantizedBvhTree::testQuantizedBoxOverlapp
bool testQuantizedBoxOverlapp(int node_index, unsigned short *quantizedMin, unsigned short *quantizedMax) const
Definition: btGImpactQuantizedBvh.h:139
btQuantizedBvhTree::m_bvhQuantization
btVector3 m_bvhQuantization
Definition: btGImpactQuantizedBvh.h:111
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition: btAlignedObjectArray.h:53
btQuantizedBvhTree::getRightNode
int getRightNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:200
btAABB::m_max
btVector3 m_max
Definition: btBoxCollision.h:233
BT_QUANTIZED_BVH_NODE::getDataIndex
int getDataIndex() const
Definition: btGImpactQuantizedBvh.h:66
btGImpactQuantizedBvh::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:320
GIM_QUANTIZED_BVH_NODE_ARRAY
Definition: btGImpactQuantizedBvh.h:97
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:63
bt_unquantize
btVector3 bt_unquantize(const unsigned short *vecIn, const btVector3 &offset, const btVector3 &bvhQuantization)
Definition: btQuantization.h:72
btQuantizedBvhTree::getNodeData
int getNodeData(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:164
btAABB::m_min
btVector3 m_min
Definition: btBoxCollision.h:232
btGImpactQuantizedBvh::isTrimesh
bool isTrimesh() const
tells if this set is a trimesh
Definition: btGImpactQuantizedBvh.h:303
BT_QUANTIZED_BVH_NODE::setEscapeIndex
void setEscapeIndex(int index)
Definition: btGImpactQuantizedBvh.h:61
btGImpactQuantizedBvh::m_box_tree
btQuantizedBvhTree m_box_tree
Definition: btGImpactQuantizedBvh.h:229
btPrimitiveManagerBase
Prototype Base class for primitive classification.
Definition: btGImpactBvh.h:230
btGImpactQuantizedBvh::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactQuantizedBvh.h:315
btPairSet
A pairset array.
Definition: btGImpactBvh.h:59
btQuantizedBvhTree::btQuantizedBvhTree
btQuantizedBvhTree()
Definition: btGImpactQuantizedBvh.h:123
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
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:283
btQuantization.h
btGImpactQuantizedBvh::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactQuantizedBvh.h:330
BT_QUANTIZED_BVH_NODE::isLeafNode
bool isLeafNode() const
Definition: btGImpactQuantizedBvh.h:49
btGImpactQuantizedBvh::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:336
btGImpactQuantizedBvh::update
void update()
node manager prototype functions
Definition: btGImpactQuantizedBvh.h:271
btGImpactQuantizedBvh::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactQuantizedBvh.h:309
btGImpactQuantizedBvh::setPrimitiveManager
void setPrimitiveManager(btPrimitiveManagerBase *primitive_manager)
Definition: btGImpactQuantizedBvh.h:256
btAABB
Axis aligned box.
Definition: btBoxCollision.h:229
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:110
btQuantizedBvhTree::m_node_array
GIM_QUANTIZED_BVH_NODE_ARRAY m_node_array
Definition: btGImpactQuantizedBvh.h:109
BT_QUANTIZED_BVH_NODE::m_escapeIndexOrDataIndex
int m_escapeIndexOrDataIndex
Definition: btGImpactQuantizedBvh.h:42
btGImpactQuantizedBvh::getPrimitiveManager
btPrimitiveManagerBase * getPrimitiveManager() const
Definition: btGImpactQuantizedBvh.h:261
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
ATTRIBUTE_ALIGNED16
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:64
btQuantizedBvhTree::m_num_nodes
int m_num_nodes
Definition: btGImpactQuantizedBvh.h:108
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
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:105
btQuantizedBvhTree::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactQuantizedBvh.h:169
btQuantizedBvhTree::getLeftNode
int getLeftNode(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:195
btGImpactQuantizedBvh::getNodeTriangle
void getNodeTriangle(int nodeindex, btPrimitiveTriangle &triangle) const
Definition: btGImpactQuantizedBvh.h:351
btGImpactQuantizedBvh::m_primitive_manager
btPrimitiveManagerBase * m_primitive_manager
Definition: btGImpactQuantizedBvh.h:230
btGImpactQuantizedBvh::hasHierarchy
bool hasHierarchy() const
tells if this set has hierarcht
Definition: btGImpactQuantizedBvh.h:297
BT_QUANTIZED_BVH_NODE::setDataIndex
void setDataIndex(int index)
Definition: btGImpactQuantizedBvh.h:73
btGImpactQuantizedBvh::btGImpactQuantizedBvh
btGImpactQuantizedBvh(btPrimitiveManagerBase *primitive_manager)
this constructor doesn&#39;t build the tree. you must call buildSet
Definition: btGImpactQuantizedBvh.h:244
btQuantizedBvhTree::getNodeCount
int getNodeCount() const
node count
Definition: btGImpactQuantizedBvh.h:153
GIM_BVH_DATA_ARRAY
Definition: btGImpactBvh.h:130
btQuantizedBvhTree::isLeafNode
bool isLeafNode(int nodeindex) const
tells if the node is a leaf
Definition: btGImpactQuantizedBvh.h:159
btGImpactQuantizedBvh::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:346
BT_QUANTIZED_BVH_NODE
btQuantizedBvhNode is a compressed aabb node, 16 bytes.
Definition: btGImpactQuantizedBvh.h:36
btQuantizedBvhTree::quantizePoint
void quantizePoint(unsigned short *quantizedpoint, const btVector3 &point) const
Definition: btGImpactQuantizedBvh.h:132
btGImpactQuantizedBvh::getNodeBound
void getNodeBound(int nodeindex, btAABB &bound) const
Definition: btGImpactQuantizedBvh.h:325
btGImpactQuantizedBvh::getGlobalBox
btAABB getGlobalBox() const
Definition: btGImpactQuantizedBvh.h:249
btQuantizedBvhTree::get_node_pointer
const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index=0) const
Definition: btGImpactQuantizedBvh.h:211
btGImpactQuantizedBvh
Structure for containing Boxes.
Definition: btGImpactQuantizedBvh.h:226
btQuantizedBvhTree::setNodeBound
void setNodeBound(int nodeindex, const btAABB &bound)
Definition: btGImpactQuantizedBvh.h:180
btQuantizedBvhTree::getEscapeNodeIndex
int getEscapeNodeIndex(int nodeindex) const
Definition: btGImpactQuantizedBvh.h:206
btGImpactQuantizedBvh::get_node_pointer
const BT_QUANTIZED_BVH_NODE * get_node_pointer(int index=0) const
Definition: btGImpactQuantizedBvh.h:357
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
btQuantizedBvhTree::clearNodes
void clearNodes()
Definition: btGImpactQuantizedBvh.h:146