问题
I am having a difficult time grasping how to iterate an octree or quad. And it may be because I am not experienced with different mythologies of iterating. But let’s suppose that I produced a quad tree that holds float x,y,z; dword color. Now, Let’s also say that this node can only produce 4 children at a time (and those children can both produced 4 children, etc, etc) up until: 7 levels are reached (so that child can’t create anymore children, but its brothers/sisters can), all 4 children created are the same dword color (again, if that happens, its brothers/sisters can still produce), or total nodes created are equal to 87380. When the above happens, it is placed into a container. And the process continues.
Now this container that holds the nodes is (for example) 7 levels deep, all children of children of children all different x,y,zs, and colors. The problem I am having is how I iterate this container, how can I go through all the children, sisters? Since the root leads to 4 children, and those 4 children have 4 children, etc, etc, etc: 4^1+4^2....+4^7. How can I find the node I want, without writing complex if statements, and iterating over the whole node (starting from root)? Does the container (the one that produces the node) need additional code that allows this process to be simple?
Sorry if the question is to general.
回答1:
Iterating over the whole tree is easy, you can do it recursively.
void iterate(node *n) {
// do something with n
if (n->child1 != NULL) iterate(n->child1);
if (n->child2 != NULL) iterate(n->child2);
if (n->child3 != NULL) iterate(n->child3);
if (n->child4 != NULL) iterate(n->child4);
}
Then call iterate(root)
and the do something
will happen on every node in the quadtree.
I suspect this isn't really what you're asking, though. There'd be no point in keeping your data in a quadtree if that's all you were doing. If you want to find a particular node in the quadtree, then you need something else. Let's say you want to find a point x,y
in a quadtree. Then you do something like:
void find(node *n, float x, float y) {
if (x == n->x && y == n->y) // you've found it!
if (x < n->x) {
if (y < n->y) {
if (n->child1 != NULL) {
find(n->child1, x, y);
} else {
// point not in quadtree
}
} else {
...same, but child2
}
} else {
...same, child3 & 4
}
}
Note that quadtrees aren't normally split on the points they store themselves, they are usually split by storing the splitting coordinates separately from the points (which are only stored at the leaves of the quadtree). See the wikipedia picture for an example.
来源:https://stackoverflow.com/questions/11488413/how-to-iterating-a-quad-oct-tree