26 m_useQuantization(false),
28 m_traversalMode(TRAVERSAL_STACKLESS)
30 ,m_subtreeHeaderCount(0)
79 #ifdef DEBUG_PATCH_COLORS
87 #endif //DEBUG_PATCH_COLORS
94 btVector3 clampValue(quantizationMargin,quantizationMargin,quantizationMargin);
103 unsigned short vecIn[3];
108 m_bvhAabbMin.setMin(v-clampValue);
127 #ifdef DEBUG_TREE_BUILDING
129 int gMaxStackDepth = 0;
130 #endif //DEBUG_TREE_BUILDING
134 #ifdef DEBUG_TREE_BUILDING
136 if (gStackDepth > gMaxStackDepth)
137 gMaxStackDepth = gStackDepth;
138 #endif //DEBUG_TREE_BUILDING
141 int splitAxis, splitIndex, i;
142 int numIndices =endIndex-startIndex;
149 #ifdef DEBUG_TREE_BUILDING
151 #endif //DEBUG_TREE_BUILDING
172 for (i=startIndex;i<endIndex;i++)
191 #ifdef DEBUG_TREE_BUILDING
193 #endif //DEBUG_TREE_BUILDING
201 const int treeSizeInBytes = escapeIndex * sizeQuantizedNode;
221 int leftSubTreeSizeInBytes = leftSubTreeSize *
static_cast<int>(
sizeof(
btQuantizedBvhNode));
225 int rightSubTreeSizeInBytes = rightSubTreeSize *
static_cast<int>(
sizeof(
btQuantizedBvhNode));
251 int splitIndex =startIndex;
252 int numIndices = endIndex - startIndex;
256 for (i=startIndex;i<endIndex;i++)
263 splitValue = means[splitAxis];
266 for (i=startIndex;i<endIndex;i++)
269 if (center[splitAxis] > splitValue)
286 int rangeBalancedIndices = numIndices/3;
287 bool unbalanced = ((splitIndex<=(startIndex+rangeBalancedIndices)) || (splitIndex >=(endIndex-1-rangeBalancedIndices)));
291 splitIndex = startIndex+ (numIndices>>1);
294 bool unbal = (splitIndex==startIndex) || (splitIndex == (endIndex));
308 int numIndices = endIndex-startIndex;
310 for (i=startIndex;i<endIndex;i++)
317 for (i=startIndex;i<endIndex;i++)
321 diff2 = diff2 * diff2;
338 unsigned short int quantizedQueryAabbMin[3];
339 unsigned short int quantizedQueryAabbMax[3];
376 int escapeIndex, curIndex = 0;
377 int walkIterations = 0;
380 unsigned aabbOverlap;
392 if (isLeafNode && (aabbOverlap != 0))
398 if ((aabbOverlap != 0) || isLeafNode)
405 rootNode += escapeIndex;
406 curIndex += escapeIndex;
441 unsigned aabbOverlap;
448 if (aabbOverlap != 0)
472 int escapeIndex, curIndex = 0;
473 int walkIterations = 0;
476 unsigned aabbOverlap=0;
477 unsigned rayBoxOverlap=0;
483 rayAabbMin.
setMin(rayTarget);
484 rayAabbMax.
setMax(rayTarget);
487 rayAabbMin += aabbMin;
488 rayAabbMax += aabbMax;
491 btVector3 rayDir = (rayTarget-raySource);
493 lambda_max = rayDir.
dot(rayTarget-raySource);
499 unsigned int sign[3] = { rayDirectionInverse[0] < 0.0, rayDirectionInverse[1] < 0.0, rayDirectionInverse[2] < 0.0};
515 bounds[0] -= aabbMax;
516 bounds[1] -= aabbMin;
525 rayBoxOverlap = aabbOverlap ?
btRayAabb2 (raySource, rayDirectionInverse, sign, bounds, param, 0.0f, lambda_max) :
false;
529 rayBoxOverlap =
btRayAabb(raySource, rayTarget,bounds[0],bounds[1],param, normal);
535 if (isLeafNode && (rayBoxOverlap != 0))
541 if ((rayBoxOverlap != 0) || isLeafNode)
548 rootNode += escapeIndex;
549 curIndex += escapeIndex;
563 int curIndex = startNodeIndex;
564 int walkIterations = 0;
565 int subTreeSize = endNodeIndex - startNodeIndex;
573 unsigned boxBoxOverlap = 0;
574 unsigned rayBoxOverlap = 0;
579 btVector3 rayDirection = (rayTarget-raySource);
581 lambda_max = rayDirection.
dot(rayTarget-raySource);
586 unsigned int sign[3] = { rayDirection[0] < 0.0, rayDirection[1] < 0.0, rayDirection[2] < 0.0};
592 rayAabbMin.
setMin(rayTarget);
593 rayAabbMax.
setMax(rayTarget);
596 rayAabbMin += aabbMin;
597 rayAabbMax += aabbMax;
599 unsigned short int quantizedQueryAabbMin[3];
600 unsigned short int quantizedQueryAabbMax[3];
604 while (curIndex < endNodeIndex)
608 #ifdef VISUALLY_ANALYZE_BVH
610 static int drawPatch = 0;
613 if (curIndex==drawPatch)
619 debugDrawerPtr->
drawAabb(aabbMin,aabbMax,color);
621 #endif//VISUALLY_ANALYZE_BVH
624 btAssert (walkIterations < subTreeSize);
639 bounds[0] -= aabbMax;
640 bounds[1] -= aabbMin;
643 bool ra2 =
btRayAabb2 (raySource, rayDirection, sign, bounds, param, 0.0, lambda_max);
644 bool ra =
btRayAabb (raySource, rayTarget, bounds[0], bounds[1], param, normal);
647 printf(
"functions don't match\n");
656 rayBoxOverlap =
btRayAabb2 (raySource, rayDirection, sign, bounds, param, 0.0f, lambda_max);
659 rayBoxOverlap =
true;
663 if (isLeafNode && rayBoxOverlap)
669 if ((rayBoxOverlap != 0) || isLeafNode)
676 rootNode += escapeIndex;
677 curIndex += escapeIndex;
689 int curIndex = startNodeIndex;
690 int walkIterations = 0;
691 int subTreeSize = endNodeIndex - startNodeIndex;
699 unsigned aabbOverlap;
701 while (curIndex < endNodeIndex)
705 #ifdef VISUALLY_ANALYZE_BVH
707 static int drawPatch = 0;
710 if (curIndex==drawPatch)
716 debugDrawerPtr->
drawAabb(aabbMin,aabbMax,color);
718 #endif//VISUALLY_ANALYZE_BVH
721 btAssert (walkIterations < subTreeSize);
728 if (isLeafNode && aabbOverlap)
734 if ((aabbOverlap != 0) || isLeafNode)
741 rootNode += escapeIndex;
742 curIndex += escapeIndex;
839 static const unsigned BVH_ALIGNMENT = 16;
840 static const unsigned BVH_ALIGNMENT_MASK = BVH_ALIGNMENT-1;
842 static const unsigned BVH_ALIGNMENT_BLOCKS = 2;
906 unsigned char *nodeData = (
unsigned char *)targetBvh;
909 unsigned sizeToAdd = 0;
910 nodeData += sizeToAdd;
920 for (
int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
935 for (
int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
964 for (
int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
976 for (
int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
995 nodeData += sizeToAdd;
1044 *((
void**)o_alignedDataBuffer) = NULL;
1052 if (i_alignedDataBuffer == NULL)
1071 btAssert(calculatedBufSize <= i_dataBufferSize);
1073 if (calculatedBufSize > i_dataBufferSize)
1078 unsigned char *nodeData = (
unsigned char *)bvh;
1081 unsigned sizeToAdd = 0;
1082 nodeData += sizeToAdd;
1096 for (
int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
1117 for (
int nodeIndex = 0; nodeIndex < nodeCount; nodeIndex++)
1131 nodeData += sizeToAdd;
1157 m_bvhAabbMin(self.m_bvhAabbMin),
1158 m_bvhAabbMax(self.m_bvhAabbMax),
1159 m_bvhQuantization(self.m_bvhQuantization),
1182 for (
int i=0;i<numElem;i++,memPtr++)
1200 for (
int i=0;i<numElem;i++,memPtr++)
1221 for (
int i=0;i<numElem;i++,memPtr++)
1253 for (
int i=0;i<numElem;i++,memPtr++)
1271 for (
int i=0;i<numElem;i++,memPtr++)
1292 for (
int i=0;i<numElem;i++,memPtr++)
1324 if (quantizedData->m_contiguousNodesPtr)
1330 for (
int i=0;i<numElem;i++,memPtr++)
1344 if (quantizedData->m_quantizedContiguousNodesPtr)
1350 for (
int i=0;i<numElem;i++,memPtr++)
1367 if (quantizedData->m_subTreeInfoPtr)
1373 for (
int i=0;i<numElem;i++,memPtr++)