16#ifndef BT_QUANTIZED_BVH_H
17#define BT_QUANTIZED_BVH_H
22#ifdef DEBUG_CHECK_DEQUANTIZATION
24#define printf spu_printf
34#ifdef BT_USE_DOUBLE_PRECISION
35#define btQuantizedBvhData btQuantizedBvhDoubleData
36#define btOptimizedBvhNodeData btOptimizedBvhNodeDoubleData
37#define btQuantizedBvhDataName "btQuantizedBvhDoubleData"
39#define btQuantizedBvhData btQuantizedBvhFloatData
40#define btOptimizedBvhNodeData btOptimizedBvhNodeFloatData
41#define btQuantizedBvhDataName "btQuantizedBvhFloatData"
47#define MAX_SUBTREE_SIZE_IN_BYTES 2048
51#define MAX_NUM_PARTS_IN_BITS 10
61 unsigned short int m_quantizedAabbMin[3];
62 unsigned short int m_quantizedAabbMax[3];
69 return (m_escapeIndexOrTriangleIndex >= 0);
74 return -m_escapeIndexOrTriangleIndex;
82 return (m_escapeIndexOrTriangleIndex & ~(y));
123 unsigned short int m_quantizedAabbMin[3];
124 unsigned short int m_quantizedAabbMax[3];
172 TRAVERSAL_STACKLESS = 0,
203 if (m_useQuantization)
205 quantize(&m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[0], aabbMin, 0);
209 m_contiguousNodes[nodeIndex].m_aabbMinOrg = aabbMin;
214 if (m_useQuantization)
216 quantize(&m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[0], aabbMax, 1);
220 m_contiguousNodes[nodeIndex].m_aabbMaxOrg = aabbMax;
226 if (m_useQuantization)
228 return unQuantize(&m_quantizedLeafNodes[nodeIndex].m_quantizedAabbMin[0]);
231 return m_leafNodes[nodeIndex].m_aabbMinOrg;
235 if (m_useQuantization)
237 return unQuantize(&m_quantizedLeafNodes[nodeIndex].m_quantizedAabbMax[0]);
240 return m_leafNodes[nodeIndex].m_aabbMaxOrg;
245 if (m_useQuantization)
247 m_quantizedContiguousNodes[nodeIndex].m_escapeIndexOrTriangleIndex = -escapeIndex;
251 m_contiguousNodes[nodeIndex].m_escapeIndex = escapeIndex;
257 if (m_useQuantization)
259 unsigned short int quantizedAabbMin[3];
260 unsigned short int quantizedAabbMax[3];
261 quantize(quantizedAabbMin, newAabbMin, 0);
262 quantize(quantizedAabbMax, newAabbMax, 1);
263 for (
int i = 0; i < 3; i++)
265 if (m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[i] > quantizedAabbMin[i])
266 m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMin[i] = quantizedAabbMin[i];
268 if (m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[i] < quantizedAabbMax[i])
269 m_quantizedContiguousNodes[nodeIndex].m_quantizedAabbMax[i] = quantizedAabbMax[i];
275 m_contiguousNodes[nodeIndex].m_aabbMinOrg.setMin(newAabbMin);
276 m_contiguousNodes[nodeIndex].m_aabbMaxOrg.setMax(newAabbMax);
280 void swapLeafNodes(
int firstIndex,
int secondIndex);
282 void assignInternalNodeFromLeafNode(
int internalNode,
int leafNodeIndex);
285 void buildTree(
int startIndex,
int endIndex);
287 int calcSplittingAxis(
int startIndex,
int endIndex);
289 int sortAndCalcSplittingIndex(
int startIndex,
int endIndex,
int splitAxis);
294 void walkStacklessQuantizedTree(
btNodeOverlapCallback * nodeCallback,
unsigned short int* quantizedQueryAabbMin,
unsigned short int* quantizedQueryAabbMax,
int startNodeIndex,
int endNodeIndex)
const;
298 void walkStacklessQuantizedTreeCacheFriendly(
btNodeOverlapCallback * nodeCallback,
unsigned short int* quantizedQueryAabbMin,
unsigned short int* quantizedQueryAabbMax)
const;
301 void walkRecursiveQuantizedTreeAgainstQueryAabb(
const btQuantizedBvhNode* currentNode,
btNodeOverlapCallback* nodeCallback,
unsigned short int* quantizedQueryAabbMin,
unsigned short int* quantizedQueryAabbMax)
const;
306 void updateSubtreeHeaders(
int leftChildNodexIndex,
int rightChildNodexIndex);
319 void buildInternal();
338 btVector3 v = (point - m_bvhAabbMin) * m_bvhQuantization;
344 out[0] = (
unsigned short)(((
unsigned short)(v.
getX() +
btScalar(1.)) | 1));
345 out[1] = (
unsigned short)(((
unsigned short)(v.
getY() +
btScalar(1.)) | 1));
346 out[2] = (
unsigned short)(((
unsigned short)(v.
getZ() +
btScalar(1.)) | 1));
350 out[0] = (
unsigned short)(((
unsigned short)(v.
getX()) & 0xfffe));
351 out[1] = (
unsigned short)(((
unsigned short)(v.
getY()) & 0xfffe));
352 out[2] = (
unsigned short)(((
unsigned short)(v.
getZ()) & 0xfffe));
355#ifdef DEBUG_CHECK_DEQUANTIZATION
361 printf(
"unconservative X, diffX = %f, oldX=%f,newX=%f\n", newPoint.
getX() - point.
getX(), newPoint.
getX(), point.
getX());
365 printf(
"unconservative Y, diffY = %f, oldY=%f,newY=%f\n", newPoint.
getY() - point.
getY(), newPoint.
getY(), point.
getY());
369 printf(
"unconservative Z, diffZ = %f, oldZ=%f,newZ=%f\n", newPoint.
getZ() - point.
getZ(), newPoint.
getZ(), point.
getZ());
376 printf(
"unconservative X, diffX = %f, oldX=%f,newX=%f\n", newPoint.
getX() - point.
getX(), newPoint.
getX(), point.
getX());
380 printf(
"unconservative Y, diffY = %f, oldY=%f,newY=%f\n", newPoint.
getY() - point.
getY(), newPoint.
getY(), point.
getY());
384 printf(
"unconservative Z, diffZ = %f, oldZ=%f,newZ=%f\n", newPoint.
getZ() - point.
getZ(), newPoint.
getZ(), point.
getZ());
395 clampedPoint.
setMax(m_bvhAabbMin);
396 clampedPoint.
setMin(m_bvhAabbMax);
398 quantize(out, clampedPoint, isMax);
408 vecOut += m_bvhAabbMin;
415 m_traversalMode = traversalMode;
420 return m_quantizedContiguousNodes;
425 return m_SubtreeHeaders;
431 unsigned calculateSerializeBufferSize()
const;
434 virtual bool serialize(
void* o_alignedDataBuffer,
unsigned i_dataBufferSize,
bool i_swapEndian)
const;
437 static btQuantizedBvh* deSerializeInPlace(
void* i_alignedDataBuffer,
unsigned int i_dataBufferSize,
bool i_swapEndian);
439 static unsigned int getAlignmentSerializationPadding();
442 virtual int calculateSerializeBufferSizeNew()
const;
445 virtual const char* serialize(
void* dataBuffer,
btSerializer* serializer)
const;
455 return m_useQuantization;
btAlignedObjectArray< btOptimizedBvhNode > NodeArray
for code readability:
btAlignedObjectArray< btBvhSubtreeInfo > BvhSubtreeInfoArray
#define MAX_NUM_PARTS_IN_BITS
btAlignedObjectArray< btQuantizedBvhNode > QuantizedNodeArray
#define btQuantizedBvhData
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
#define ATTRIBUTE_ALIGNED16(a)
#define SIMD_FORCE_INLINE
btBvhSubtreeInfo provides info to gather a subtree of limited size
BT_DECLARE_ALIGNED_ALLOCATOR()
void setAabbFromQuantizeNode(const btQuantizedBvhNode &quantizedNode)
virtual ~btNodeOverlapCallback()
virtual void processNode(int subPart, int triangleIndex)=0
The btQuantizedBvh class stores an AABB tree that can be quickly traversed on CPU and Cell SPU.
void setInternalNodeAabbMax(int nodeIndex, const btVector3 &aabbMax)
QuantizedNodeArray & getLeafNodeArray()
QuantizedNodeArray m_quantizedLeafNodes
btTraversalMode m_traversalMode
void quantize(unsigned short *out, const btVector3 &point, int isMax) const
BvhSubtreeInfoArray & getSubtreeInfoArray()
btVector3 m_bvhQuantization
void setInternalNodeEscapeIndex(int nodeIndex, int escapeIndex)
@ TRAVERSAL_STACKLESS_CACHE_FRIENDLY
BvhSubtreeInfoArray m_SubtreeHeaders
NodeArray m_contiguousNodes
QuantizedNodeArray & getQuantizedNodeArray()
void walkRecursiveQuantizedTreeAgainstQuantizedTree(const btQuantizedBvhNode *treeNodeA, const btQuantizedBvhNode *treeNodeB, btNodeOverlapCallback *nodeCallback) const
use the 16-byte stackless 'skipindex' node tree to do a recursive traversal
void setInternalNodeAabbMin(int nodeIndex, const btVector3 &aabbMin)
two versions, one for quantized and normal nodes.
void mergeInternalNodeAabb(int nodeIndex, const btVector3 &newAabbMin, const btVector3 &newAabbMax)
BT_DECLARE_ALIGNED_ALLOCATOR()
virtual int calculateSerializeBufferSizeNew() const
void quantizeWithClamp(unsigned short *out, const btVector3 &point2, int isMax) const
void setTraversalMode(btTraversalMode traversalMode)
setTraversalMode let's you choose between stackless, recursive or stackless cache friendly tree trave...
btVector3 getAabbMax(int nodeIndex) const
btVector3 getAabbMin(int nodeIndex) const
QuantizedNodeArray m_quantizedContiguousNodes
btVector3 unQuantize(const unsigned short *vecIn) const
btVector3 can be used to represent 3D points and vectors.
const btScalar & getZ() const
Return the z value.
void setMax(const btVector3 &other)
Set each element to the max of the current values and the values of another btVector3.
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
const btScalar & getY() const
Return the y value.
void setMin(const btVector3 &other)
Set each element to the min of the current values and the values of another btVector3.
const btScalar & getX() const
Return the x value.
unsigned short m_quantizedAabbMin[3]
unsigned short m_quantizedAabbMax[3]
btVector3DoubleData m_aabbMaxOrg
btVector3DoubleData m_aabbMinOrg
btVector3FloatData m_aabbMaxOrg
btVector3FloatData m_aabbMinOrg
btOptimizedBvhNode contains both internal and leaf node information.
BT_DECLARE_ALIGNED_ALLOCATOR()
btBvhSubtreeInfoData * m_subTreeInfoPtr
int m_numContiguousLeafNodes
btVector3DoubleData m_bvhAabbMin
btVector3DoubleData m_bvhAabbMax
int m_numQuantizedContiguousNodes
btVector3DoubleData m_bvhQuantization
btQuantizedBvhNodeData * m_quantizedContiguousNodesPtr
btOptimizedBvhNodeDoubleData * m_contiguousNodesPtr
btOptimizedBvhNodeFloatData * m_contiguousNodesPtr
btVector3FloatData m_bvhAabbMin
int m_numQuantizedContiguousNodes
btBvhSubtreeInfoData * m_subTreeInfoPtr
int m_numContiguousLeafNodes
btVector3FloatData m_bvhQuantization
btQuantizedBvhNodeData * m_quantizedContiguousNodesPtr
btVector3FloatData m_bvhAabbMax
int m_escapeIndexOrTriangleIndex
unsigned short m_quantizedAabbMax[3]
unsigned short m_quantizedAabbMin[3]
btQuantizedBvhNode is a compressed aabb node, 16 bytes.
unsigned short int m_quantizedAabbMin[3]
unsigned short int m_quantizedAabbMax[3]
int getEscapeIndex() const
int m_escapeIndexOrTriangleIndex
BT_DECLARE_ALIGNED_ALLOCATOR()
int getTriangleIndex() const