newParent.mBoundingBox = leafNode.mBoundingBox.union_with(leafSibling.mBoundingBox); // the new parents aabb is the leaf aabb combined with it's siblings aabb
newParent.mLeftNodeIndex = leafSiblingIndex;
newParent.mRightNodeIndex = leafNodeIndex;
leafNode.mParentNodeIndex = newParentIndex;
leafSibling.mParentNodeIndex = newParentIndex;
if (oldParentIndex == NULL_TREE_NODE)
{
// the old parent was the root and so this is now the root
mRootNodeIndex = newParentIndex;
}
else
{
// the old parent was not the root and so we need to patch the left or right index to
// point to the new node
TreeNode& oldParent = mTreeNodes[oldParentIndex];
if (oldParent.mLeftNodeIndex == leafSiblingIndex)
{
oldParent.mLeftNodeIndex = newParentIndex;
}
else
{
oldParent.mRightNodeIndex = newParentIndex;
}
}
// finally we need to walk back up the tree fixing heights and areas