void Octree::Update() { PROFILE(UpdateOctree); for (auto it = updateQueue.Begin(); it != updateQueue.End(); ++it) { OctreeNode* node = *it; // If node was removed before update could happen, a null pointer will be in its place if (node) { node->SetFlag(NF_OCTREE_UPDATE_QUEUED, false); // Do nothing if still fits the current octant const BoundingBox& box = node->WorldBoundingBox(); Vector3 boxSize = box.Size(); Octant* oldOctant = node->octant; if (oldOctant && oldOctant->cullingBox.IsInside(box) == INSIDE && oldOctant->FitBoundingBox(box, boxSize)) continue; // Begin reinsert process. Start from root and check what level child needs to be used Octant* newOctant = &root; Vector3 boxCenter = box.Center(); for (;;) { bool insertHere; // If node does not fit fully inside root octant, must remain in it if (newOctant == &root) insertHere = newOctant->cullingBox.IsInside(box) != INSIDE || newOctant->FitBoundingBox(box, boxSize); else insertHere = newOctant->FitBoundingBox(box, boxSize); if (insertHere) { if (newOctant != oldOctant) { // Add first, then remove, because node count going to zero deletes the octree branch in question AddNode(node, newOctant); if (oldOctant) RemoveNode(node, oldOctant); } break; } else newOctant = CreateChildOctant(newOctant, newOctant->ChildIndex(boxCenter)); } } } updateQueue.Clear(); }
void Octree::RemoveNode(OctreeNode* node, Octant* octant) { // Do not set the node's octant pointer to zero, as the node may already be added into another octant octant->nodes.Remove(node); // Decrement the node count in the whole parent branch and erase empty octants as necessary while (octant) { --octant->numNodes; Octant* next = octant->parent; if (!octant->numNodes && next) DeleteChildOctant(next, next->ChildIndex(octant->center)); octant = next; } }