Original Post
bool COctreeNode::Iterator::SeekNextNode()
{
// Make sure we have a root node and that this node is not the root node
if (!m_pRootNode || m_pCurrentNode == m_pRootNode)
return false;
// Go to higher levels of the octree
while (true)
{
// Loop through our neighbour nodes
for (int i = m_pCurrentNode->m_NodeIndex + 1; i < 8; i++)
{
// Get a pointer to this child node
COctreeNode* pChildNode = m_pCurrentNode->m_pParent->m_pChildren[i];
// If this child node does not exist, skip it
if (!pChildNode)
continue;
// Verify if this child node intersects with the search area
if (pChildNode->SphereIntersect(m_vSphereOrigin, m_fSphereRadius))
{
// Set the current node as this child node
m_pCurrentNode = pChildNode;
// Find the lowest level node within our search area
for (int l = 0; m_pCurrentNode->m_NumChildren > 0 && l < OCTREE_MAX_SPLIT_LEVEL; l++)
{
// Loop through the children nodes
for (int i = 0; i < 8; i++)
{
// If this child node does not exist, skip it
if (!m_pCurrentNode->m_pChildren[i])
continue;
// Get a pointer to this child node
COctreeNode* pChildNode = m_pCurrentNode->m_pChildren[i];
// Verify if this child node intersects with the search area
if (pChildNode->SphereIntersect(m_vSphereOrigin, m_fSphereRadius))
{
// Set the current node as this child node
m_pCurrentNode = pChildNode;
// Break out of this loop
break;
}
}
}
// If this node has polygons in it
if (m_pCurrentNode->m_NumPolygons > 0)
{
// We have found the next node
return true;
}
}
}
// Verify if our parent node is the root node
if (m_pCurrentNode->m_pParent != m_pRootNode)
{
// Go one level higher
m_pCurrentNode = m_pCurrentNode->m_pParent;
}
else
{
// Impossible to find next node
return false;
}
}
}