c++ - QuadTrees and recursive constructor stack overflow -
i'm creating chunked quadtree terrain , i'm trying ensure there never more 1 level of detail difference between 2 nodes. current code constructor this:
quadnode::quadnode(rect area, quadnode * p, quadroot * r) { root = r; parent = p; level = p->level + 1; initializegeometry(area); for(int iii = 0; iii < 4; iii++) children[iii] = 0; for(int iii = 0; iii < 4; iii++) { if(quadrantneedschild(iii)) children[iii] = new quadnode(getrect(iii), this, root); } }
and works fine. in order force no more 1 level of difference between 2 neighbour nodes i'm trying add end of constructor:
quadnode * neighbour; for(int direction = 0; direction < 4; direction++) { neighbour = findneighbour(direction); if(neighbour) { while(level - neighbour->level > 1) { neighbour->subdivide(); neighbour = findneighbour(direction); //neighbour should child of previous neighbour } } }
but stack overflows. 1 reason think assignment portion of children[iii] = new quadnode(getrect(iii), this, root);
statement never executes , findneighbour()
requires children set find proper neighbour. don't think that's problem code never reaches second neighbour = findneighbour(direction);
line , have no idea what's causing that.
if run second code snippet in new function after base creation of tree more-or-less works requires multiple passes ensure newly created nodes don't create level difference > 1. i'd rather have code in constructor if @ possible. can think of way of achieving this?
a few notes on class in case helps quadrantneedschild(int quadrant)
ensures level never exceeds 8 know i'm not going deep. subdivide()
runs children[iii] = new quadnode(getrect(iii), this, root);
on quadrants. findneighbour(int direction)
can return parent or ancestor. example if d looking north neighbour grandparent(the entire diagram) if b never subdivided in following:
- - - - - - | | | | | b | | | | |- - - - - -| | | d| | | c |-----| | | | | - - - - - -
the subdivide function subdivides given quadrant or quadrants if supplied out of range quadrant.
void quadnode::subdivide(int quadrant) { if(quadrant > -1 && quadrant < 4) { if(!children[quadrant]) children[quadrant] = new quadnode(getrect(quadrant), this, root); } else for(int iii = 0; iii < 4; iii++) if(children[iii] == 0) children[iii] = new quadnode(getrect(iii), this, root); }
when first child of node constructed, find it's own parent neighbor, since parent has no children yet (the first 1 created)?
that cause node cal subdivide()
on own parent, construct new child again call subdivide()
,...
and without this, constructing first node on new level subdivide()
it's neighbors in turn subdivide neighbors of neighbors, recursively. how it's supposed work? deepest level cause 48 levels of recursion.
Comments
Post a Comment