0

I am trying to recursively subdivide an octree and I am struggling with the logic of how to allow each node to be visited, but preventing an infinite recursion.

It only seems to be visiting the first node, and then stopping and I understand why it is doing this, but I'm not sure how to fix it.

I am defining a maxDepth of 2, and a currentDepth which is supposed to increment once all child nodes have been subdivided. I initially pass in the root node and it should recursively go through until the max depth is reached.

This is what I have so far, where depth 1 works fine and it gets subdivided, but when I want a maxDepth of 2, it will only visit the first node and leave the rest.

Here is the code I am using to subdivide.

private void subdivideNode(OctreeNode node)
{
    if (currentDepth >= maxDepth) return;

    node.subdivide();
    currentDepth++;
    for (int i = 0; i < node.children.Length; i++)
    {
        subdivideNode(node.children[i]);
    }
}

enter image description here

The subdivide method in OctreeNode creates the 8 octants of a given node

jjmcc
  • 795
  • 2
  • 11
  • 27

1 Answers1

0

Figured it out by using

  private void subdivideNode(OctreeNode node, int depth)
    {
        if (depth == maxDepth) return;

        node.subdivide();

        for (int i = 0; i < node.children.Length; i++)
        {
            subdivideNode(node.children[i], depth + 1);
        }
    }
jjmcc
  • 795
  • 2
  • 11
  • 27