#include "aabb.h"
#include "constants.h"
#include "core.h"
#include "box2d/collision.h"
#include "box2d/math_functions.h"
#include <float.h>
#include <string.h>
#define B2_TREE_STACK_SIZE 1024
typedef struct b2TreeNode
{
b2AABB aabb;
uint64_t categoryBits;
union
{
struct
{
int32_t child1, child2;
} children;
uint64_t userData;
};
union
{
int32_t parent;
int32_t next;
};
uint16_t height; uint16_t flags; } b2TreeNode;
static b2TreeNode b2_defaultTreeNode = {
.aabb = { { 0.0f, 0.0f }, { 0.0f, 0.0f } },
.categoryBits = B2_DEFAULT_CATEGORY_BITS,
.children =
{
.child1 = B2_NULL_INDEX,
.child2 = B2_NULL_INDEX,
},
.parent = B2_NULL_INDEX,
.height = 0,
.flags = b2_allocatedNode,
};
static bool b2IsLeaf( const b2TreeNode* node )
{
return node->flags & b2_leafNode;
}
static bool b2IsAllocated( const b2TreeNode* node )
{
return node->flags & b2_allocatedNode;
}
static uint16_t b2MaxUInt16( uint16_t a, uint16_t b )
{
return a > b ? a : b;
}
b2DynamicTree b2DynamicTree_Create( void )
{
b2DynamicTree tree;
tree.root = B2_NULL_INDEX;
tree.nodeCapacity = 16;
tree.nodeCount = 0;
tree.nodes = (b2TreeNode*)b2Alloc( tree.nodeCapacity * sizeof( b2TreeNode ) );
memset( tree.nodes, 0, tree.nodeCapacity * sizeof( b2TreeNode ) );
for ( int i = 0; i < tree.nodeCapacity - 1; ++i )
{
tree.nodes[i].next = i + 1;
}
tree.nodes[tree.nodeCapacity - 1].next = B2_NULL_INDEX;
tree.freeList = 0;
tree.proxyCount = 0;
tree.leafIndices = NULL;
tree.leafBoxes = NULL;
tree.leafCenters = NULL;
tree.binIndices = NULL;
tree.rebuildCapacity = 0;
return tree;
}
void b2DynamicTree_Destroy( b2DynamicTree* tree )
{
b2Free( tree->nodes, tree->nodeCapacity * sizeof( b2TreeNode ) );
b2Free( tree->leafIndices, tree->rebuildCapacity * sizeof( int32_t ) );
b2Free( tree->leafBoxes, tree->rebuildCapacity * sizeof( b2AABB ) );
b2Free( tree->leafCenters, tree->rebuildCapacity * sizeof( b2Vec2 ) );
b2Free( tree->binIndices, tree->rebuildCapacity * sizeof( int32_t ) );
memset( tree, 0, sizeof( b2DynamicTree ) );
}
static int b2AllocateNode( b2DynamicTree* tree )
{
if ( tree->freeList == B2_NULL_INDEX )
{
B2_ASSERT( tree->nodeCount == tree->nodeCapacity );
b2TreeNode* oldNodes = tree->nodes;
int oldCapacity = tree->nodeCapacity;
tree->nodeCapacity += oldCapacity >> 1;
tree->nodes = (b2TreeNode*)b2Alloc( tree->nodeCapacity * sizeof( b2TreeNode ) );
B2_ASSERT( oldNodes != NULL );
memcpy( tree->nodes, oldNodes, tree->nodeCount * sizeof( b2TreeNode ) );
memset( tree->nodes + tree->nodeCount, 0, ( tree->nodeCapacity - tree->nodeCount ) * sizeof( b2TreeNode ) );
b2Free( oldNodes, oldCapacity * sizeof( b2TreeNode ) );
for ( int i = tree->nodeCount; i < tree->nodeCapacity - 1; ++i )
{
tree->nodes[i].next = i + 1;
}
tree->nodes[tree->nodeCapacity - 1].next = B2_NULL_INDEX;
tree->freeList = tree->nodeCount;
}
int nodeIndex = tree->freeList;
b2TreeNode* node = tree->nodes + nodeIndex;
tree->freeList = node->next;
*node = b2_defaultTreeNode;
++tree->nodeCount;
return nodeIndex;
}
static void b2FreeNode( b2DynamicTree* tree, int nodeId )
{
B2_ASSERT( 0 <= nodeId && nodeId < tree->nodeCapacity );
B2_ASSERT( 0 < tree->nodeCount );
tree->nodes[nodeId].next = tree->freeList;
tree->nodes[nodeId].flags = 0;
tree->freeList = nodeId;
--tree->nodeCount;
}
static int b2FindBestSibling( const b2DynamicTree* tree, b2AABB boxD )
{
b2Vec2 centerD = b2AABB_Center( boxD );
float areaD = b2Perimeter( boxD );
const b2TreeNode* nodes = tree->nodes;
int rootIndex = tree->root;
b2AABB rootBox = nodes[rootIndex].aabb;
float areaBase = b2Perimeter( rootBox );
float directCost = b2Perimeter( b2AABB_Union( rootBox, boxD ) );
float inheritedCost = 0.0f;
int bestSibling = rootIndex;
float bestCost = directCost;
int index = rootIndex;
while ( nodes[index].height > 0 )
{
int child1 = nodes[index].children.child1;
int child2 = nodes[index].children.child2;
float cost = directCost + inheritedCost;
if ( cost < bestCost )
{
bestSibling = index;
bestCost = cost;
}
inheritedCost += directCost - areaBase;
bool leaf1 = nodes[child1].height == 0;
bool leaf2 = nodes[child2].height == 0;
float lowerCost1 = FLT_MAX;
b2AABB box1 = nodes[child1].aabb;
float directCost1 = b2Perimeter( b2AABB_Union( box1, boxD ) );
float area1 = 0.0f;
if ( leaf1 )
{
float cost1 = directCost1 + inheritedCost;
if ( cost1 < bestCost )
{
bestSibling = child1;
bestCost = cost1;
}
}
else
{
area1 = b2Perimeter( box1 );
lowerCost1 = inheritedCost + directCost1 + b2MinFloat( areaD - area1, 0.0f );
}
float lowerCost2 = FLT_MAX;
b2AABB box2 = nodes[child2].aabb;
float directCost2 = b2Perimeter( b2AABB_Union( box2, boxD ) );
float area2 = 0.0f;
if ( leaf2 )
{
float cost2 = directCost2 + inheritedCost;
if ( cost2 < bestCost )
{
bestSibling = child2;
bestCost = cost2;
}
}
else
{
area2 = b2Perimeter( box2 );
lowerCost2 = inheritedCost + directCost2 + b2MinFloat( areaD - area2, 0.0f );
}
if ( leaf1 && leaf2 )
{
break;
}
if ( bestCost <= lowerCost1 && bestCost <= lowerCost2 )
{
break;
}
if ( lowerCost1 == lowerCost2 && leaf1 == false )
{
B2_ASSERT( lowerCost1 < FLT_MAX );
B2_ASSERT( lowerCost2 < FLT_MAX );
b2Vec2 d1 = b2Sub( b2AABB_Center( box1 ), centerD );
b2Vec2 d2 = b2Sub( b2AABB_Center( box2 ), centerD );
lowerCost1 = b2LengthSquared( d1 );
lowerCost2 = b2LengthSquared( d2 );
}
if ( lowerCost1 < lowerCost2 && leaf1 == false )
{
index = child1;
areaBase = area1;
directCost = directCost1;
}
else
{
index = child2;
areaBase = area2;
directCost = directCost2;
}
B2_ASSERT( nodes[index].height > 0 );
}
return bestSibling;
}
enum b2RotateType
{
b2_rotateNone,
b2_rotateBF,
b2_rotateBG,
b2_rotateCD,
b2_rotateCE
};
static void b2RotateNodes( b2DynamicTree* tree, int iA )
{
B2_ASSERT( iA != B2_NULL_INDEX );
b2TreeNode* nodes = tree->nodes;
b2TreeNode* A = nodes + iA;
if ( A->height < 2 )
{
return;
}
int iB = A->children.child1;
int iC = A->children.child2;
B2_ASSERT( 0 <= iB && iB < tree->nodeCapacity );
B2_ASSERT( 0 <= iC && iC < tree->nodeCapacity );
b2TreeNode* B = nodes + iB;
b2TreeNode* C = nodes + iC;
if ( B->height == 0 )
{
B2_ASSERT( C->height > 0 );
int iF = C->children.child1;
int iG = C->children.child2;
b2TreeNode* F = nodes + iF;
b2TreeNode* G = nodes + iG;
B2_ASSERT( 0 <= iF && iF < tree->nodeCapacity );
B2_ASSERT( 0 <= iG && iG < tree->nodeCapacity );
float costBase = b2Perimeter( C->aabb );
b2AABB aabbBG = b2AABB_Union( B->aabb, G->aabb );
float costBF = b2Perimeter( aabbBG );
b2AABB aabbBF = b2AABB_Union( B->aabb, F->aabb );
float costBG = b2Perimeter( aabbBF );
if ( costBase < costBF && costBase < costBG )
{
return;
}
if ( costBF < costBG )
{
A->children.child1 = iF;
C->children.child1 = iB;
B->parent = iC;
F->parent = iA;
C->aabb = aabbBG;
C->height = 1 + b2MaxUInt16( B->height, G->height );
A->height = 1 + b2MaxUInt16( C->height, F->height );
C->categoryBits = B->categoryBits | G->categoryBits;
A->categoryBits = C->categoryBits | F->categoryBits;
C->flags |= ( B->flags | G->flags ) & b2_enlargedNode;
A->flags |= ( C->flags | F->flags ) & b2_enlargedNode;
}
else
{
A->children.child1 = iG;
C->children.child2 = iB;
B->parent = iC;
G->parent = iA;
C->aabb = aabbBF;
C->height = 1 + b2MaxUInt16( B->height, F->height );
A->height = 1 + b2MaxUInt16( C->height, G->height );
C->categoryBits = B->categoryBits | F->categoryBits;
A->categoryBits = C->categoryBits | G->categoryBits;
C->flags |= ( B->flags | F->flags ) & b2_enlargedNode;
A->flags |= ( C->flags | G->flags ) & b2_enlargedNode;
}
}
else if ( C->height == 0 )
{
B2_ASSERT( B->height > 0 );
int iD = B->children.child1;
int iE = B->children.child2;
b2TreeNode* D = nodes + iD;
b2TreeNode* E = nodes + iE;
B2_ASSERT( 0 <= iD && iD < tree->nodeCapacity );
B2_ASSERT( 0 <= iE && iE < tree->nodeCapacity );
float costBase = b2Perimeter( B->aabb );
b2AABB aabbCE = b2AABB_Union( C->aabb, E->aabb );
float costCD = b2Perimeter( aabbCE );
b2AABB aabbCD = b2AABB_Union( C->aabb, D->aabb );
float costCE = b2Perimeter( aabbCD );
if ( costBase < costCD && costBase < costCE )
{
return;
}
if ( costCD < costCE )
{
A->children.child2 = iD;
B->children.child1 = iC;
C->parent = iB;
D->parent = iA;
B->aabb = aabbCE;
B->height = 1 + b2MaxUInt16( C->height, E->height );
A->height = 1 + b2MaxUInt16( B->height, D->height );
B->categoryBits = C->categoryBits | E->categoryBits;
A->categoryBits = B->categoryBits | D->categoryBits;
B->flags |= ( C->flags | E->flags ) & b2_enlargedNode;
A->flags |= ( B->flags | D->flags ) & b2_enlargedNode;
}
else
{
A->children.child2 = iE;
B->children.child2 = iC;
C->parent = iB;
E->parent = iA;
B->aabb = aabbCD;
B->height = 1 + b2MaxUInt16( C->height, D->height );
A->height = 1 + b2MaxUInt16( B->height, E->height );
B->categoryBits = C->categoryBits | D->categoryBits;
A->categoryBits = B->categoryBits | E->categoryBits;
B->flags |= ( C->flags | D->flags ) & b2_enlargedNode;
A->flags |= ( B->flags | E->flags ) & b2_enlargedNode;
}
}
else
{
int iD = B->children.child1;
int iE = B->children.child2;
int iF = C->children.child1;
int iG = C->children.child2;
B2_ASSERT( 0 <= iD && iD < tree->nodeCapacity );
B2_ASSERT( 0 <= iE && iE < tree->nodeCapacity );
B2_ASSERT( 0 <= iF && iF < tree->nodeCapacity );
B2_ASSERT( 0 <= iG && iG < tree->nodeCapacity );
b2TreeNode* D = nodes + iD;
b2TreeNode* E = nodes + iE;
b2TreeNode* F = nodes + iF;
b2TreeNode* G = nodes + iG;
float areaB = b2Perimeter( B->aabb );
float areaC = b2Perimeter( C->aabb );
float costBase = areaB + areaC;
enum b2RotateType bestRotation = b2_rotateNone;
float bestCost = costBase;
b2AABB aabbBG = b2AABB_Union( B->aabb, G->aabb );
float costBF = areaB + b2Perimeter( aabbBG );
if ( costBF < bestCost )
{
bestRotation = b2_rotateBF;
bestCost = costBF;
}
b2AABB aabbBF = b2AABB_Union( B->aabb, F->aabb );
float costBG = areaB + b2Perimeter( aabbBF );
if ( costBG < bestCost )
{
bestRotation = b2_rotateBG;
bestCost = costBG;
}
b2AABB aabbCE = b2AABB_Union( C->aabb, E->aabb );
float costCD = areaC + b2Perimeter( aabbCE );
if ( costCD < bestCost )
{
bestRotation = b2_rotateCD;
bestCost = costCD;
}
b2AABB aabbCD = b2AABB_Union( C->aabb, D->aabb );
float costCE = areaC + b2Perimeter( aabbCD );
if ( costCE < bestCost )
{
bestRotation = b2_rotateCE;
}
switch ( bestRotation )
{
case b2_rotateNone:
break;
case b2_rotateBF:
A->children.child1 = iF;
C->children.child1 = iB;
B->parent = iC;
F->parent = iA;
C->aabb = aabbBG;
C->height = 1 + b2MaxUInt16( B->height, G->height );
A->height = 1 + b2MaxUInt16( C->height, F->height );
C->categoryBits = B->categoryBits | G->categoryBits;
A->categoryBits = C->categoryBits | F->categoryBits;
C->flags |= ( B->flags | G->flags ) & b2_enlargedNode;
A->flags |= ( C->flags | F->flags ) & b2_enlargedNode;
break;
case b2_rotateBG:
A->children.child1 = iG;
C->children.child2 = iB;
B->parent = iC;
G->parent = iA;
C->aabb = aabbBF;
C->height = 1 + b2MaxUInt16( B->height, F->height );
A->height = 1 + b2MaxUInt16( C->height, G->height );
C->categoryBits = B->categoryBits | F->categoryBits;
A->categoryBits = C->categoryBits | G->categoryBits;
C->flags |= ( B->flags | F->flags ) & b2_enlargedNode;
A->flags |= ( C->flags | G->flags ) & b2_enlargedNode;
break;
case b2_rotateCD:
A->children.child2 = iD;
B->children.child1 = iC;
C->parent = iB;
D->parent = iA;
B->aabb = aabbCE;
B->height = 1 + b2MaxUInt16( C->height, E->height );
A->height = 1 + b2MaxUInt16( B->height, D->height );
B->categoryBits = C->categoryBits | E->categoryBits;
A->categoryBits = B->categoryBits | D->categoryBits;
B->flags |= ( C->flags | E->flags ) & b2_enlargedNode;
A->flags |= ( B->flags | D->flags ) & b2_enlargedNode;
break;
case b2_rotateCE:
A->children.child2 = iE;
B->children.child2 = iC;
C->parent = iB;
E->parent = iA;
B->aabb = aabbCD;
B->height = 1 + b2MaxUInt16( C->height, D->height );
A->height = 1 + b2MaxUInt16( B->height, E->height );
B->categoryBits = C->categoryBits | D->categoryBits;
A->categoryBits = B->categoryBits | E->categoryBits;
B->flags |= ( C->flags | D->flags ) & b2_enlargedNode;
A->flags |= ( B->flags | E->flags ) & b2_enlargedNode;
break;
default:
B2_ASSERT( false );
break;
}
}
}
static void b2InsertLeaf( b2DynamicTree* tree, int leaf, bool shouldRotate )
{
if ( tree->root == B2_NULL_INDEX )
{
tree->root = leaf;
tree->nodes[tree->root].parent = B2_NULL_INDEX;
return;
}
b2AABB leafAABB = tree->nodes[leaf].aabb;
int sibling = b2FindBestSibling( tree, leafAABB );
int oldParent = tree->nodes[sibling].parent;
int newParent = b2AllocateNode( tree );
b2TreeNode* nodes = tree->nodes;
nodes[newParent].parent = oldParent;
nodes[newParent].userData = UINT64_MAX;
nodes[newParent].aabb = b2AABB_Union( leafAABB, nodes[sibling].aabb );
nodes[newParent].categoryBits = nodes[leaf].categoryBits | nodes[sibling].categoryBits;
nodes[newParent].height = nodes[sibling].height + 1;
nodes[newParent].children.child1 = sibling;
nodes[newParent].children.child2 = leaf;
nodes[sibling].parent = newParent;
nodes[leaf].parent = newParent;
if ( oldParent != B2_NULL_INDEX )
{
if ( nodes[oldParent].children.child1 == sibling )
{
nodes[oldParent].children.child1 = newParent;
}
else
{
B2_ASSERT( nodes[oldParent].children.child2 == sibling );
nodes[oldParent].children.child2 = newParent;
}
}
else
{
tree->root = newParent;
}
int index = nodes[leaf].parent;
while ( index != B2_NULL_INDEX )
{
int child1 = nodes[index].children.child1;
int child2 = nodes[index].children.child2;
B2_ASSERT( child1 != B2_NULL_INDEX );
B2_ASSERT( child2 != B2_NULL_INDEX );
nodes[index].aabb = b2AABB_Union( nodes[child1].aabb, nodes[child2].aabb );
nodes[index].categoryBits = nodes[child1].categoryBits | nodes[child2].categoryBits;
nodes[index].height = 1 + b2MaxUInt16( nodes[child1].height, nodes[child2].height );
nodes[index].flags |= ( nodes[child1].flags | nodes[child2].flags ) & b2_enlargedNode;
if ( shouldRotate )
{
b2RotateNodes( tree, index );
}
index = nodes[index].parent;
}
}
static void b2RemoveLeaf( b2DynamicTree* tree, int leaf )
{
if ( leaf == tree->root )
{
tree->root = B2_NULL_INDEX;
return;
}
b2TreeNode* nodes = tree->nodes;
int parent = nodes[leaf].parent;
int grandParent = nodes[parent].parent;
int sibling;
if ( nodes[parent].children.child1 == leaf )
{
sibling = nodes[parent].children.child2;
}
else
{
sibling = nodes[parent].children.child1;
}
if ( grandParent != B2_NULL_INDEX )
{
if ( nodes[grandParent].children.child1 == parent )
{
nodes[grandParent].children.child1 = sibling;
}
else
{
nodes[grandParent].children.child2 = sibling;
}
nodes[sibling].parent = grandParent;
b2FreeNode( tree, parent );
int index = grandParent;
while ( index != B2_NULL_INDEX )
{
b2TreeNode* node = nodes + index;
b2TreeNode* child1 = nodes + node->children.child1;
b2TreeNode* child2 = nodes + node->children.child2;
node->aabb = b2AABB_Union( child1->aabb, child2->aabb );
node->categoryBits = child1->categoryBits | child2->categoryBits;
node->height = 1 + b2MaxUInt16( child1->height, child2->height );
index = node->parent;
}
}
else
{
tree->root = sibling;
tree->nodes[sibling].parent = B2_NULL_INDEX;
b2FreeNode( tree, parent );
}
}
int b2DynamicTree_CreateProxy( b2DynamicTree* tree, b2AABB aabb, uint64_t categoryBits, uint64_t userData )
{
B2_ASSERT( -B2_HUGE < aabb.lowerBound.x && aabb.lowerBound.x < B2_HUGE );
B2_ASSERT( -B2_HUGE < aabb.lowerBound.y && aabb.lowerBound.y < B2_HUGE );
B2_ASSERT( -B2_HUGE < aabb.upperBound.x && aabb.upperBound.x < B2_HUGE );
B2_ASSERT( -B2_HUGE < aabb.upperBound.y && aabb.upperBound.y < B2_HUGE );
int proxyId = b2AllocateNode( tree );
b2TreeNode* node = tree->nodes + proxyId;
node->aabb = aabb;
node->userData = userData;
node->categoryBits = categoryBits;
node->height = 0;
node->flags = b2_allocatedNode | b2_leafNode;
bool shouldRotate = true;
b2InsertLeaf( tree, proxyId, shouldRotate );
tree->proxyCount += 1;
return proxyId;
}
void b2DynamicTree_DestroyProxy( b2DynamicTree* tree, int proxyId )
{
B2_ASSERT( 0 <= proxyId && proxyId < tree->nodeCapacity );
B2_ASSERT( b2IsLeaf( tree->nodes + proxyId ) );
b2RemoveLeaf( tree, proxyId );
b2FreeNode( tree, proxyId );
B2_ASSERT( tree->proxyCount > 0 );
tree->proxyCount -= 1;
}
int b2DynamicTree_GetProxyCount( const b2DynamicTree* tree )
{
return tree->proxyCount;
}
void b2DynamicTree_MoveProxy( b2DynamicTree* tree, int proxyId, b2AABB aabb )
{
B2_ASSERT( b2IsValidAABB( aabb ) );
B2_ASSERT( aabb.upperBound.x - aabb.lowerBound.x < B2_HUGE );
B2_ASSERT( aabb.upperBound.y - aabb.lowerBound.y < B2_HUGE );
B2_ASSERT( 0 <= proxyId && proxyId < tree->nodeCapacity );
B2_ASSERT( b2IsLeaf( tree->nodes + proxyId ) );
b2RemoveLeaf( tree, proxyId );
tree->nodes[proxyId].aabb = aabb;
bool shouldRotate = false;
b2InsertLeaf( tree, proxyId, shouldRotate );
}
void b2DynamicTree_EnlargeProxy( b2DynamicTree* tree, int proxyId, b2AABB aabb )
{
b2TreeNode* nodes = tree->nodes;
B2_ASSERT( b2IsValidAABB( aabb ) );
B2_ASSERT( aabb.upperBound.x - aabb.lowerBound.x < B2_HUGE );
B2_ASSERT( aabb.upperBound.y - aabb.lowerBound.y < B2_HUGE );
B2_ASSERT( 0 <= proxyId && proxyId < tree->nodeCapacity );
B2_ASSERT( b2IsLeaf( tree->nodes + proxyId ) );
B2_ASSERT( b2AABB_Contains( nodes[proxyId].aabb, aabb ) == false );
nodes[proxyId].aabb = aabb;
int parentIndex = nodes[proxyId].parent;
while ( parentIndex != B2_NULL_INDEX )
{
bool changed = b2EnlargeAABB( &nodes[parentIndex].aabb, aabb );
nodes[parentIndex].flags |= b2_enlargedNode;
parentIndex = nodes[parentIndex].parent;
if ( changed == false )
{
break;
}
}
while ( parentIndex != B2_NULL_INDEX )
{
if ( nodes[parentIndex].flags & b2_enlargedNode )
{
break;
}
nodes[parentIndex].flags |= b2_enlargedNode;
parentIndex = nodes[parentIndex].parent;
}
}
void b2DynamicTree_SetCategoryBits( b2DynamicTree* tree, int proxyId, uint64_t categoryBits )
{
b2TreeNode* nodes = tree->nodes;
B2_ASSERT( nodes[proxyId].children.child1 == B2_NULL_INDEX );
B2_ASSERT( nodes[proxyId].children.child2 == B2_NULL_INDEX );
B2_ASSERT( ( nodes[proxyId].flags & b2_leafNode ) == b2_leafNode );
nodes[proxyId].categoryBits = categoryBits;
int nodeIndex = nodes[proxyId].parent;
while ( nodeIndex != B2_NULL_INDEX )
{
b2TreeNode* node = nodes + nodeIndex;
int child1 = node->children.child1;
B2_ASSERT( child1 != B2_NULL_INDEX );
int child2 = node->children.child2;
B2_ASSERT( child2 != B2_NULL_INDEX );
node->categoryBits = nodes[child1].categoryBits | nodes[child2].categoryBits;
nodeIndex = node->parent;
}
}
uint64_t b2DynamicTree_GetCategoryBits( b2DynamicTree* tree, int proxyId )
{
B2_ASSERT( 0 <= proxyId && proxyId < tree->nodeCapacity );
return tree->nodes[proxyId].categoryBits;
}
int b2DynamicTree_GetHeight( const b2DynamicTree* tree )
{
if ( tree->root == B2_NULL_INDEX )
{
return 0;
}
return tree->nodes[tree->root].height;
}
float b2DynamicTree_GetAreaRatio( const b2DynamicTree* tree )
{
if ( tree->root == B2_NULL_INDEX )
{
return 0.0f;
}
const b2TreeNode* root = tree->nodes + tree->root;
float rootArea = b2Perimeter( root->aabb );
float totalArea = 0.0f;
for ( int i = 0; i < tree->nodeCapacity; ++i )
{
const b2TreeNode* node = tree->nodes + i;
if ( b2IsAllocated( node ) == false || b2IsLeaf( node ) || i == tree->root )
{
continue;
}
totalArea += b2Perimeter( node->aabb );
}
return totalArea / rootArea;
}
b2AABB b2DynamicTree_GetRootBounds( const b2DynamicTree* tree )
{
if ( tree->root != B2_NULL_INDEX )
{
return tree->nodes[tree->root].aabb;
}
b2AABB empty = { b2Vec2_zero, b2Vec2_zero };
return empty;
}
#if B2_VALIDATE
static int b2ComputeHeight( const b2DynamicTree* tree, int nodeId )
{
B2_ASSERT( 0 <= nodeId && nodeId < tree->nodeCapacity );
b2TreeNode* node = tree->nodes + nodeId;
if ( b2IsLeaf( node ) )
{
return 0;
}
int height1 = b2ComputeHeight( tree, node->children.child1 );
int height2 = b2ComputeHeight( tree, node->children.child2 );
return 1 + b2MaxInt( height1, height2 );
}
static void b2ValidateStructure( const b2DynamicTree* tree, int index )
{
if ( index == B2_NULL_INDEX )
{
return;
}
if ( index == tree->root )
{
B2_ASSERT( tree->nodes[index].parent == B2_NULL_INDEX );
}
const b2TreeNode* node = tree->nodes + index;
B2_ASSERT( node->flags == 0 || ( node->flags & b2_allocatedNode ) != 0 );
if ( b2IsLeaf( node ) )
{
B2_ASSERT( node->height == 0 );
return;
}
int child1 = node->children.child1;
int child2 = node->children.child2;
B2_ASSERT( 0 <= child1 && child1 < tree->nodeCapacity );
B2_ASSERT( 0 <= child2 && child2 < tree->nodeCapacity );
B2_ASSERT( tree->nodes[child1].parent == index );
B2_ASSERT( tree->nodes[child2].parent == index );
if ( ( tree->nodes[child1].flags | tree->nodes[child2].flags ) & b2_enlargedNode )
{
B2_ASSERT( node->flags & b2_enlargedNode );
}
b2ValidateStructure( tree, child1 );
b2ValidateStructure( tree, child2 );
}
static void b2ValidateMetrics( const b2DynamicTree* tree, int index )
{
if ( index == B2_NULL_INDEX )
{
return;
}
const b2TreeNode* node = tree->nodes + index;
if ( b2IsLeaf( node ) )
{
B2_ASSERT( node->height == 0 );
return;
}
int child1 = node->children.child1;
int child2 = node->children.child2;
B2_ASSERT( 0 <= child1 && child1 < tree->nodeCapacity );
B2_ASSERT( 0 <= child2 && child2 < tree->nodeCapacity );
int height1 = tree->nodes[child1].height;
int height2 = tree->nodes[child2].height;
int height = 1 + b2MaxInt( height1, height2 );
B2_ASSERT( node->height == height );
B2_ASSERT( b2AABB_Contains( node->aabb, tree->nodes[child1].aabb ) );
B2_ASSERT( b2AABB_Contains( node->aabb, tree->nodes[child2].aabb ) );
uint64_t categoryBits = tree->nodes[child1].categoryBits | tree->nodes[child2].categoryBits;
B2_ASSERT( node->categoryBits == categoryBits );
b2ValidateMetrics( tree, child1 );
b2ValidateMetrics( tree, child2 );
}
#endif
void b2DynamicTree_Validate( const b2DynamicTree* tree )
{
#if B2_VALIDATE
if ( tree->root == B2_NULL_INDEX )
{
return;
}
b2ValidateStructure( tree, tree->root );
b2ValidateMetrics( tree, tree->root );
int freeCount = 0;
int freeIndex = tree->freeList;
while ( freeIndex != B2_NULL_INDEX )
{
B2_ASSERT( 0 <= freeIndex && freeIndex < tree->nodeCapacity );
freeIndex = tree->nodes[freeIndex].next;
++freeCount;
}
int height = b2DynamicTree_GetHeight( tree );
int computedHeight = b2ComputeHeight( tree, tree->root );
B2_ASSERT( height == computedHeight );
B2_ASSERT( tree->nodeCount + freeCount == tree->nodeCapacity );
#else
B2_UNUSED( tree );
#endif
}
void b2DynamicTree_ValidateNoEnlarged( const b2DynamicTree* tree )
{
#if B2_VALIDATE == 1
int capacity = tree->nodeCapacity;
const b2TreeNode* nodes = tree->nodes;
for ( int i = 0; i < capacity; ++i )
{
const b2TreeNode* node = nodes + i;
if ( node->flags & b2_allocatedNode )
{
B2_ASSERT( ( node->flags & b2_enlargedNode ) == 0 );
}
}
#else
B2_UNUSED( tree );
#endif
}
int b2DynamicTree_GetByteCount( const b2DynamicTree* tree )
{
size_t size = sizeof( b2DynamicTree ) + sizeof( b2TreeNode ) * tree->nodeCapacity +
tree->rebuildCapacity * ( sizeof( int ) + sizeof( b2AABB ) + sizeof( b2Vec2 ) + sizeof( int ) );
return (int)size;
}
uint64_t b2DynamicTree_GetUserData( const b2DynamicTree* tree, int proxyId )
{
B2_ASSERT( 0 <= proxyId && proxyId < tree->nodeCapacity );
return tree->nodes[proxyId].userData;
}
b2AABB b2DynamicTree_GetAABB( const b2DynamicTree* tree, int proxyId )
{
B2_ASSERT( 0 <= proxyId && proxyId < tree->nodeCapacity );
return tree->nodes[proxyId].aabb;
}
b2TreeStats b2DynamicTree_Query( const b2DynamicTree* tree, b2AABB aabb, uint64_t maskBits, b2TreeQueryCallbackFcn* callback,
void* context )
{
b2TreeStats result = { 0 };
if ( tree->nodeCount == 0 )
{
return result;
}
int stack[B2_TREE_STACK_SIZE];
int stackCount = 0;
stack[stackCount++] = tree->root;
while ( stackCount > 0 )
{
int nodeId = stack[--stackCount];
const b2TreeNode* node = tree->nodes + nodeId;
result.nodeVisits += 1;
if ( b2AABB_Overlaps( node->aabb, aabb ) && ( node->categoryBits & maskBits ) != 0 )
{
if ( b2IsLeaf( node ) )
{
bool proceed = callback( nodeId, node->userData, context );
result.leafVisits += 1;
if ( proceed == false )
{
return result;
}
}
else
{
if ( stackCount < B2_TREE_STACK_SIZE - 1 )
{
stack[stackCount++] = node->children.child1;
stack[stackCount++] = node->children.child2;
}
else
{
B2_ASSERT( stackCount < B2_TREE_STACK_SIZE - 1 );
}
}
}
}
return result;
}
b2TreeStats b2DynamicTree_QueryAll( const b2DynamicTree* tree, b2AABB aabb, b2TreeQueryCallbackFcn* callback, void* context )
{
b2TreeStats result = { 0 };
if ( tree->nodeCount == 0 )
{
return result;
}
int stack[B2_TREE_STACK_SIZE];
int stackCount = 0;
stack[stackCount++] = tree->root;
while ( stackCount > 0 )
{
int nodeId = stack[--stackCount];
const b2TreeNode* node = tree->nodes + nodeId;
result.nodeVisits += 1;
if ( b2AABB_Overlaps( node->aabb, aabb ) )
{
if ( b2IsLeaf( node ) )
{
bool proceed = callback( nodeId, node->userData, context );
result.leafVisits += 1;
if ( proceed == false )
{
return result;
}
}
else
{
if ( stackCount < B2_TREE_STACK_SIZE - 1 )
{
stack[stackCount++] = node->children.child1;
stack[stackCount++] = node->children.child2;
}
else
{
B2_ASSERT( stackCount < B2_TREE_STACK_SIZE - 1 );
}
}
}
}
return result;
}
b2TreeStats b2DynamicTree_RayCast( const b2DynamicTree* tree, const b2RayCastInput* input, uint64_t maskBits,
b2TreeRayCastCallbackFcn* callback, void* context )
{
b2TreeStats result = { 0 };
if ( tree->nodeCount == 0 )
{
return result;
}
b2Vec2 p1 = input->origin;
b2Vec2 d = input->translation;
b2Vec2 r = b2Normalize( d );
b2Vec2 v = b2CrossSV( 1.0f, r );
b2Vec2 abs_v = b2Abs( v );
float maxFraction = input->maxFraction;
b2Vec2 p2 = b2MulAdd( p1, maxFraction, d );
b2AABB segmentAABB = { b2Min( p1, p2 ), b2Max( p1, p2 ) };
int stack[B2_TREE_STACK_SIZE];
int stackCount = 0;
stack[stackCount++] = tree->root;
const b2TreeNode* nodes = tree->nodes;
b2RayCastInput subInput = *input;
while ( stackCount > 0 )
{
int nodeId = stack[--stackCount];
if ( nodeId == B2_NULL_INDEX )
{
B2_ASSERT( false );
continue;
}
const b2TreeNode* node = nodes + nodeId;
result.nodeVisits += 1;
b2AABB nodeAABB = node->aabb;
if ( ( node->categoryBits & maskBits ) == 0 || b2AABB_Overlaps( nodeAABB, segmentAABB ) == false )
{
continue;
}
b2Vec2 c = b2AABB_Center( nodeAABB );
b2Vec2 h = b2AABB_Extents( nodeAABB );
float term1 = b2AbsFloat( b2Dot( v, b2Sub( p1, c ) ) );
float term2 = b2Dot( abs_v, h );
if ( term2 < term1 )
{
continue;
}
if ( b2IsLeaf( node ) )
{
subInput.maxFraction = maxFraction;
float value = callback( &subInput, nodeId, node->userData, context );
result.leafVisits += 1;
if ( value == 0.0f )
{
return result;
}
if ( 0.0f < value && value <= maxFraction )
{
maxFraction = value;
p2 = b2MulAdd( p1, maxFraction, d );
segmentAABB.lowerBound = b2Min( p1, p2 );
segmentAABB.upperBound = b2Max( p1, p2 );
}
}
else
{
if ( stackCount < B2_TREE_STACK_SIZE - 1 )
{
b2Vec2 c1 = b2AABB_Center( nodes[node->children.child1].aabb );
b2Vec2 c2 = b2AABB_Center( nodes[node->children.child2].aabb );
if ( b2DistanceSquared( c1, p1 ) < b2DistanceSquared( c2, p1 ) )
{
stack[stackCount++] = node->children.child2;
stack[stackCount++] = node->children.child1;
}
else
{
stack[stackCount++] = node->children.child1;
stack[stackCount++] = node->children.child2;
}
}
else
{
B2_ASSERT( stackCount < B2_TREE_STACK_SIZE - 1 );
}
}
}
return result;
}
b2TreeStats b2DynamicTree_ShapeCast( const b2DynamicTree* tree, const b2ShapeCastInput* input, uint64_t maskBits,
b2TreeShapeCastCallbackFcn* callback, void* context )
{
b2TreeStats stats = { 0 };
if ( tree->nodeCount == 0 || input->proxy.count == 0 )
{
return stats;
}
b2AABB originAABB = { input->proxy.points[0], input->proxy.points[0] };
for ( int i = 1; i < input->proxy.count; ++i )
{
originAABB.lowerBound = b2Min( originAABB.lowerBound, input->proxy.points[i] );
originAABB.upperBound = b2Max( originAABB.upperBound, input->proxy.points[i] );
}
b2Vec2 radius = { input->proxy.radius, input->proxy.radius };
originAABB.lowerBound = b2Sub( originAABB.lowerBound, radius );
originAABB.upperBound = b2Add( originAABB.upperBound, radius );
b2Vec2 p1 = b2AABB_Center( originAABB );
b2Vec2 extension = b2AABB_Extents( originAABB );
b2Vec2 r = input->translation;
b2Vec2 v = b2CrossSV( 1.0f, r );
b2Vec2 abs_v = b2Abs( v );
float maxFraction = input->maxFraction;
b2Vec2 t = b2MulSV( maxFraction, input->translation );
b2AABB totalAABB = {
b2Min( originAABB.lowerBound, b2Add( originAABB.lowerBound, t ) ),
b2Max( originAABB.upperBound, b2Add( originAABB.upperBound, t ) ),
};
b2ShapeCastInput subInput = *input;
const b2TreeNode* nodes = tree->nodes;
int stack[B2_TREE_STACK_SIZE];
int stackCount = 0;
stack[stackCount++] = tree->root;
while ( stackCount > 0 )
{
int nodeId = stack[--stackCount];
if ( nodeId == B2_NULL_INDEX )
{
B2_ASSERT( false );
continue;
}
const b2TreeNode* node = nodes + nodeId;
stats.nodeVisits += 1;
if ( ( node->categoryBits & maskBits ) == 0 || b2AABB_Overlaps( node->aabb, totalAABB ) == false )
{
continue;
}
b2Vec2 c = b2AABB_Center( node->aabb );
b2Vec2 h = b2Add( b2AABB_Extents( node->aabb ), extension );
float term1 = b2AbsFloat( b2Dot( v, b2Sub( p1, c ) ) );
float term2 = b2Dot( abs_v, h );
if ( term2 < term1 )
{
continue;
}
if ( b2IsLeaf( node ) )
{
subInput.maxFraction = maxFraction;
float value = callback( &subInput, nodeId, node->userData, context );
stats.leafVisits += 1;
if ( value == 0.0f )
{
return stats;
}
if ( 0.0f < value && value < maxFraction )
{
maxFraction = value;
t = b2MulSV( maxFraction, input->translation );
totalAABB.lowerBound = b2Min( originAABB.lowerBound, b2Add( originAABB.lowerBound, t ) );
totalAABB.upperBound = b2Max( originAABB.upperBound, b2Add( originAABB.upperBound, t ) );
}
}
else
{
if ( stackCount < B2_TREE_STACK_SIZE - 1 )
{
b2Vec2 c1 = b2AABB_Center( nodes[node->children.child1].aabb );
b2Vec2 c2 = b2AABB_Center( nodes[node->children.child2].aabb );
if ( b2DistanceSquared( c1, p1 ) < b2DistanceSquared( c2, p1 ) )
{
stack[stackCount++] = node->children.child2;
stack[stackCount++] = node->children.child1;
}
else
{
stack[stackCount++] = node->children.child1;
stack[stackCount++] = node->children.child2;
}
}
else
{
B2_ASSERT( stackCount < B2_TREE_STACK_SIZE - 1 );
}
}
}
return stats;
}
#define B2_TREE_HEURISTIC 0
#if B2_TREE_HEURISTIC == 0
static int b2PartitionMid( int* indices, b2Vec2* centers, int count )
{
if ( count <= 2 )
{
return count / 2;
}
b2Vec2 lowerBound = centers[0];
b2Vec2 upperBound = centers[0];
for ( int i = 1; i < count; ++i )
{
lowerBound = b2Min( lowerBound, centers[i] );
upperBound = b2Max( upperBound, centers[i] );
}
b2Vec2 d = b2Sub( upperBound, lowerBound );
b2Vec2 c = { 0.5f * ( lowerBound.x + upperBound.x ), 0.5f * ( lowerBound.y + upperBound.y ) };
int i1 = 0, i2 = count;
if ( d.x > d.y )
{
float pivot = c.x;
while ( i1 < i2 )
{
while ( i1 < i2 && centers[i1].x < pivot )
{
i1 += 1;
};
while ( i1 < i2 && centers[i2 - 1].x >= pivot )
{
i2 -= 1;
};
if ( i1 < i2 )
{
{
int temp = indices[i1];
indices[i1] = indices[i2 - 1];
indices[i2 - 1] = temp;
}
{
b2Vec2 temp = centers[i1];
centers[i1] = centers[i2 - 1];
centers[i2 - 1] = temp;
}
i1 += 1;
i2 -= 1;
}
}
}
else
{
float pivot = c.y;
while ( i1 < i2 )
{
while ( i1 < i2 && centers[i1].y < pivot )
{
i1 += 1;
};
while ( i1 < i2 && centers[i2 - 1].y >= pivot )
{
i2 -= 1;
};
if ( i1 < i2 )
{
{
int temp = indices[i1];
indices[i1] = indices[i2 - 1];
indices[i2 - 1] = temp;
}
{
b2Vec2 temp = centers[i1];
centers[i1] = centers[i2 - 1];
centers[i2 - 1] = temp;
}
i1 += 1;
i2 -= 1;
}
}
}
B2_ASSERT( i1 == i2 );
if ( i1 > 0 && i1 < count )
{
return i1;
}
return count / 2;
}
#else
#define B2_BIN_COUNT 8
typedef struct b2TreeBin
{
b2AABB aabb;
int count;
} b2TreeBin;
typedef struct b2TreePlane
{
b2AABB leftAABB;
b2AABB rightAABB;
int leftCount;
int rightCount;
} b2TreePlane;
static int b2PartitionSAH( int* indices, int* binIndices, b2AABB* boxes, int count )
{
B2_ASSERT( count > 0 );
b2TreeBin bins[B2_BIN_COUNT];
b2TreePlane planes[B2_BIN_COUNT - 1];
b2Vec2 center = b2AABB_Center( boxes[0] );
b2AABB centroidAABB;
centroidAABB.lowerBound = center;
centroidAABB.upperBound = center;
for ( int i = 1; i < count; ++i )
{
center = b2AABB_Center( boxes[i] );
centroidAABB.lowerBound = b2Min( centroidAABB.lowerBound, center );
centroidAABB.upperBound = b2Max( centroidAABB.upperBound, center );
}
b2Vec2 d = b2Sub( centroidAABB.upperBound, centroidAABB.lowerBound );
int axisIndex;
float invD;
if ( d.x > d.y )
{
axisIndex = 0;
invD = d.x;
}
else
{
axisIndex = 1;
invD = d.y;
}
invD = invD > 0.0f ? 1.0f / invD : 0.0f;
for ( int i = 0; i < B2_BIN_COUNT; ++i )
{
bins[i].aabb.lowerBound = (b2Vec2){ FLT_MAX, FLT_MAX };
bins[i].aabb.upperBound = (b2Vec2){ -FLT_MAX, -FLT_MAX };
bins[i].count = 0;
}
float binCount = B2_BIN_COUNT;
float lowerBoundArray[2] = { centroidAABB.lowerBound.x, centroidAABB.lowerBound.y };
float minC = lowerBoundArray[axisIndex];
for ( int i = 0; i < count; ++i )
{
b2Vec2 c = b2AABB_Center( boxes[i] );
float cArray[2] = { c.x, c.y };
int binIndex = (int)( binCount * ( cArray[axisIndex] - minC ) * invD );
binIndex = b2ClampInt( binIndex, 0, B2_BIN_COUNT - 1 );
binIndices[i] = binIndex;
bins[binIndex].count += 1;
bins[binIndex].aabb = b2AABB_Union( bins[binIndex].aabb, boxes[i] );
}
int planeCount = B2_BIN_COUNT - 1;
planes[0].leftCount = bins[0].count;
planes[0].leftAABB = bins[0].aabb;
for ( int i = 1; i < planeCount; ++i )
{
planes[i].leftCount = planes[i - 1].leftCount + bins[i].count;
planes[i].leftAABB = b2AABB_Union( planes[i - 1].leftAABB, bins[i].aabb );
}
planes[planeCount - 1].rightCount = bins[planeCount].count;
planes[planeCount - 1].rightAABB = bins[planeCount].aabb;
for ( int i = planeCount - 2; i >= 0; --i )
{
planes[i].rightCount = planes[i + 1].rightCount + bins[i + 1].count;
planes[i].rightAABB = b2AABB_Union( planes[i + 1].rightAABB, bins[i + 1].aabb );
}
float minCost = FLT_MAX;
int bestPlane = 0;
for ( int i = 0; i < planeCount; ++i )
{
float leftArea = b2Perimeter( planes[i].leftAABB );
float rightArea = b2Perimeter( planes[i].rightAABB );
int leftCount = planes[i].leftCount;
int rightCount = planes[i].rightCount;
float cost = leftCount * leftArea + rightCount * rightArea;
if ( cost < minCost )
{
bestPlane = i;
minCost = cost;
}
}
int i1 = 0, i2 = count;
while ( i1 < i2 )
{
while ( i1 < i2 && binIndices[i1] < bestPlane )
{
i1 += 1;
};
while ( i1 < i2 && binIndices[i2 - 1] >= bestPlane )
{
i2 -= 1;
};
if ( i1 < i2 )
{
{
int temp = indices[i1];
indices[i1] = indices[i2 - 1];
indices[i2 - 1] = temp;
}
{
b2AABB temp = boxes[i1];
boxes[i1] = boxes[i2 - 1];
boxes[i2 - 1] = temp;
}
i1 += 1;
i2 -= 1;
}
}
B2_ASSERT( i1 == i2 );
if ( i1 > 0 && i1 < count )
{
return i1;
}
else
{
return count / 2;
}
}
#endif
struct b2RebuildItem
{
int nodeIndex;
int childCount;
int startIndex;
int splitIndex;
int endIndex;
};
static int b2BuildTree( b2DynamicTree* tree, int leafCount )
{
b2TreeNode* nodes = tree->nodes;
int* leafIndices = tree->leafIndices;
if ( leafCount == 1 )
{
nodes[leafIndices[0]].parent = B2_NULL_INDEX;
return leafIndices[0];
}
#if B2_TREE_HEURISTIC == 0
b2Vec2* leafCenters = tree->leafCenters;
#else
b2AABB* leafBoxes = tree->leafBoxes;
int* binIndices = tree->binIndices;
#endif
struct b2RebuildItem stack[B2_TREE_STACK_SIZE];
int top = 0;
stack[0].nodeIndex = b2AllocateNode( tree );
stack[0].childCount = -1;
stack[0].startIndex = 0;
stack[0].endIndex = leafCount;
#if B2_TREE_HEURISTIC == 0
stack[0].splitIndex = b2PartitionMid( leafIndices, leafCenters, leafCount );
#else
stack[0].splitIndex = b2PartitionSAH( leafIndices, binIndices, leafBoxes, leafCount );
#endif
while ( true )
{
struct b2RebuildItem* item = stack + top;
item->childCount += 1;
if ( item->childCount == 2 )
{
if ( top == 0 )
{
break;
}
struct b2RebuildItem* parentItem = stack + ( top - 1 );
b2TreeNode* parentNode = nodes + parentItem->nodeIndex;
if ( parentItem->childCount == 0 )
{
B2_ASSERT( parentNode->children.child1 == B2_NULL_INDEX );
parentNode->children.child1 = item->nodeIndex;
}
else
{
B2_ASSERT( parentItem->childCount == 1 );
B2_ASSERT( parentNode->children.child2 == B2_NULL_INDEX );
parentNode->children.child2 = item->nodeIndex;
}
b2TreeNode* node = nodes + item->nodeIndex;
B2_ASSERT( node->parent == B2_NULL_INDEX );
node->parent = parentItem->nodeIndex;
B2_ASSERT( node->children.child1 != B2_NULL_INDEX );
B2_ASSERT( node->children.child2 != B2_NULL_INDEX );
b2TreeNode* child1 = nodes + node->children.child1;
b2TreeNode* child2 = nodes + node->children.child2;
node->aabb = b2AABB_Union( child1->aabb, child2->aabb );
node->height = 1 + b2MaxUInt16( child1->height, child2->height );
node->categoryBits = child1->categoryBits | child2->categoryBits;
top -= 1;
}
else
{
int startIndex, endIndex;
if ( item->childCount == 0 )
{
startIndex = item->startIndex;
endIndex = item->splitIndex;
}
else
{
B2_ASSERT( item->childCount == 1 );
startIndex = item->splitIndex;
endIndex = item->endIndex;
}
int count = endIndex - startIndex;
if ( count == 1 )
{
int childIndex = leafIndices[startIndex];
b2TreeNode* node = nodes + item->nodeIndex;
if ( item->childCount == 0 )
{
B2_ASSERT( node->children.child1 == B2_NULL_INDEX );
node->children.child1 = childIndex;
}
else
{
B2_ASSERT( item->childCount == 1 );
B2_ASSERT( node->children.child2 == B2_NULL_INDEX );
node->children.child2 = childIndex;
}
b2TreeNode* childNode = nodes + childIndex;
B2_ASSERT( childNode->parent == B2_NULL_INDEX );
childNode->parent = item->nodeIndex;
}
else
{
B2_ASSERT( count > 0 );
B2_ASSERT( top < B2_TREE_STACK_SIZE );
top += 1;
struct b2RebuildItem* newItem = stack + top;
newItem->nodeIndex = b2AllocateNode( tree );
newItem->childCount = -1;
newItem->startIndex = startIndex;
newItem->endIndex = endIndex;
#if B2_TREE_HEURISTIC == 0
newItem->splitIndex = b2PartitionMid( leafIndices + startIndex, leafCenters + startIndex, count );
#else
newItem->splitIndex =
b2PartitionSAH( leafIndices + startIndex, binIndices + startIndex, leafBoxes + startIndex, count );
#endif
newItem->splitIndex += startIndex;
}
}
}
b2TreeNode* rootNode = nodes + stack[0].nodeIndex;
B2_ASSERT( rootNode->parent == B2_NULL_INDEX );
B2_ASSERT( rootNode->children.child1 != B2_NULL_INDEX );
B2_ASSERT( rootNode->children.child2 != B2_NULL_INDEX );
b2TreeNode* child1 = nodes + rootNode->children.child1;
b2TreeNode* child2 = nodes + rootNode->children.child2;
rootNode->aabb = b2AABB_Union( child1->aabb, child2->aabb );
rootNode->height = 1 + b2MaxUInt16( child1->height, child2->height );
rootNode->categoryBits = child1->categoryBits | child2->categoryBits;
return stack[0].nodeIndex;
}
int b2DynamicTree_Rebuild( b2DynamicTree* tree, bool fullBuild )
{
int proxyCount = tree->proxyCount;
if ( proxyCount == 0 )
{
return 0;
}
if ( proxyCount > tree->rebuildCapacity )
{
int newCapacity = proxyCount + proxyCount / 2;
b2Free( tree->leafIndices, tree->rebuildCapacity * sizeof( int ) );
tree->leafIndices = b2Alloc( newCapacity * sizeof( int ) );
#if B2_TREE_HEURISTIC == 0
b2Free( tree->leafCenters, tree->rebuildCapacity * sizeof( b2Vec2 ) );
tree->leafCenters = b2Alloc( newCapacity * sizeof( b2Vec2 ) );
#else
b2Free( tree->leafBoxes, tree->rebuildCapacity * sizeof( b2AABB ) );
tree->leafBoxes = b2Alloc( newCapacity * sizeof( b2AABB ) );
b2Free( tree->binIndices, tree->rebuildCapacity * sizeof( int ) );
tree->binIndices = b2Alloc( newCapacity * sizeof( int ) );
#endif
tree->rebuildCapacity = newCapacity;
}
int leafCount = 0;
int stack[B2_TREE_STACK_SIZE];
int stackCount = 0;
int nodeIndex = tree->root;
b2TreeNode* nodes = tree->nodes;
b2TreeNode* node = nodes + nodeIndex;
int* leafIndices = tree->leafIndices;
#if B2_TREE_HEURISTIC == 0
b2Vec2* leafCenters = tree->leafCenters;
#else
b2AABB* leafBoxes = tree->leafBoxes;
#endif
while ( true )
{
if ( node->height == 0 || ( ( node->flags & b2_enlargedNode ) == 0 && fullBuild == false ) )
{
leafIndices[leafCount] = nodeIndex;
#if B2_TREE_HEURISTIC == 0
leafCenters[leafCount] = b2AABB_Center( node->aabb );
#else
leafBoxes[leafCount] = node->aabb;
#endif
leafCount += 1;
node->parent = B2_NULL_INDEX;
}
else
{
int doomedNodeIndex = nodeIndex;
nodeIndex = node->children.child1;
if ( stackCount < B2_TREE_STACK_SIZE )
{
stack[stackCount++] = node->children.child2;
}
else
{
B2_ASSERT( stackCount < B2_TREE_STACK_SIZE );
}
node = nodes + nodeIndex;
b2FreeNode( tree, doomedNodeIndex );
continue;
}
if ( stackCount == 0 )
{
break;
}
nodeIndex = stack[--stackCount];
node = nodes + nodeIndex;
}
#if B2_VALIDATE == 1
int capacity = tree->nodeCapacity;
for ( int i = 0; i < capacity; ++i )
{
if ( nodes[i].flags & b2_allocatedNode )
{
B2_ASSERT( ( nodes[i].flags & b2_enlargedNode ) == 0 );
}
}
#endif
B2_ASSERT( leafCount <= proxyCount );
tree->root = b2BuildTree( tree, leafCount );
b2DynamicTree_Validate( tree );
return leafCount;
}