m_parent[m_root] = NULL_NODE;
return;
}
// find the best sibling
AABB leafAABB = m_aabb[leaf];
int index = m_root;
while (m_child1[index] != NULL_NODE) {
final int node = index;
int child1 = m_child1[node];
int child2 = m_child2[node];
final AABB nodeAABB = m_aabb[node];
float area = nodeAABB.getPerimeter();
combinedAABB.combine(nodeAABB, leafAABB);
float combinedArea = combinedAABB.getPerimeter();
// Cost of creating a new parent for this node and the new leaf
float cost = 2.0f * combinedArea;
// Minimum cost of pushing the leaf further down the tree
float inheritanceCost = 2.0f * (combinedArea - area);
// Cost of descending into child1
float cost1;
AABB child1AABB = m_aabb[child1];
if (m_child1[child1] == NULL_NODE) {
combinedAABB.combine(leafAABB, child1AABB);
cost1 = combinedAABB.getPerimeter() + inheritanceCost;
} else {
combinedAABB.combine(leafAABB, child1AABB);
float oldArea = child1AABB.getPerimeter();
float newArea = combinedAABB.getPerimeter();
cost1 = (newArea - oldArea) + inheritanceCost;
}
// Cost of descending into child2
float cost2;
AABB child2AABB = m_aabb[child2];
if (m_child1[child2] == NULL_NODE) {
combinedAABB.combine(leafAABB, child2AABB);
cost2 = combinedAABB.getPerimeter() + inheritanceCost;
} else {
combinedAABB.combine(leafAABB, child2AABB);
float oldArea = child2AABB.getPerimeter();
float newArea = combinedAABB.getPerimeter();
cost2 = newArea - oldArea + inheritanceCost;
}
// Descend according to the minimum cost.