问题
I have an octree, that stores a voxel-based fluid. When I simulate the fluid, I need to access the leaves around the current node, how can I implement such a search?
You can assume the node stores a pointer to its parent node.(Perhaps other data is needed?)
回答1:
Assuming each octree node also holds its 3D index[1] in the octree (and its depth).
- Generate the 3D indices for the neighbor nodes of the query node (simply increment/decrement the 3D index of the query node in each dimension).
- For each potential neighbor, traverse the octree from the root, using the generated 3D indices to choose which path to take at each node with children.
In case the current node has neighbors at higher depth only (the octree is not complete/perfect), the traversal done in 2. will stop at the maximum reachable depth in the octree that is less or equal than the depth of the query node.
If the nodes hold a pointer to their parents, 2. can be improved by first finding the lowest common ancestor of the current node and each of its neighbors (this is done by finding the longest common binary prefix in their 3D indices) and starting the traversal to reach the neighbor only from this ancestor node.
[1]: The 3D index is simply the x/y/z coordinates of the node in the octree. For instance, the eight children of the root have 3D indices with 1 binary digit (these node are at located depth 1 in the octree): 0/0/0, 1/0/0, 0/1/0, 1/1/0, ... The sixty-four grand-children of the root have 3D indices with 2 binary digits (these nodes are located at depth 2 in the octree): 00/00/00, 01/00/00, 10/00/00, 11/00/00, 00/01/00, 01/00/00...
来源:https://stackoverflow.com/questions/21502914/octree-neighbour-search