X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fquadtree.cpp;h=8474a7ff7c2c02efc3955f3d7cde823e315b3ca4;hb=ea748154f1bc7dbc81cb52611a52865e63109439;hp=2aace7acc912dd8d723241ed9252e5f7ae2f9bf4;hpb=5f25f354c33142215147b1fa3d18445bd0d7a6ee;p=ipdf%2Fcode.git diff --git a/src/quadtree.cpp b/src/quadtree.cpp index 2aace7a..8474a7f 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -63,6 +63,140 @@ bool ContainedInQuadChild(const Rect& src, QuadTreeNodeChildren child_type) return true; } + + +QuadTreeIndex QuadTree::GetNeighbour(QuadTreeIndex start, int xdir, int ydir) const +{ + if (!xdir && !ydir) return start; + + QuadTreeIndex newNode; + // Try moving to the right if that's easy. + if (xdir > 0) + { + switch (nodes[start].child_type) + { + case QTC_TOP_LEFT: + case QTC_BOTTOM_LEFT: + { + if (nodes[start].child_type == QTC_TOP_LEFT) + newNode = nodes[nodes[start].parent].top_right; + else + newNode = nodes[nodes[start].parent].bottom_right; + + return GetNeighbour(newNode, xdir - 1, ydir); + } + case QTC_TOP_RIGHT: + case QTC_BOTTOM_RIGHT: + { + QuadTreeIndex right_parent = GetNeighbour(nodes[start].parent, 1, 0); + if (right_parent == -1) return -1; + if (nodes[start].child_type == QTC_TOP_RIGHT) + newNode = nodes[right_parent].top_left; + else + newNode = nodes[right_parent].bottom_left; + return GetNeighbour(newNode, xdir - 1, ydir); + } + default: + return -1; + + } + } + + // Try moving to the left. + if (xdir < 0) + { + switch (nodes[start].child_type) + { + case QTC_TOP_RIGHT: + case QTC_BOTTOM_RIGHT: + { + if (nodes[start].child_type == QTC_TOP_RIGHT) + newNode = nodes[nodes[start].parent].top_left; + else + newNode = nodes[nodes[start].parent].bottom_left; + + return GetNeighbour(newNode, xdir + 1, ydir); + } + case QTC_TOP_LEFT: + case QTC_BOTTOM_LEFT: + { + QuadTreeIndex left_parent = GetNeighbour(nodes[start].parent, -1, 0); + if (left_parent == -1) return -1; + if (nodes[start].child_type == QTC_TOP_LEFT) + newNode = nodes[left_parent].top_right; + else + newNode = nodes[left_parent].bottom_right; + return GetNeighbour(newNode, xdir + 1, ydir); + } + default: + return -1; + } + } + + // Try moving to the bottom. + if (ydir > 0) + { + switch (nodes[start].child_type) + { + case QTC_TOP_LEFT: + case QTC_TOP_RIGHT: + { + if (nodes[start].child_type == QTC_TOP_LEFT) + newNode = nodes[nodes[start].parent].bottom_left; + else + newNode = nodes[nodes[start].parent].bottom_right; + + return GetNeighbour(newNode, xdir, ydir - 1); + } + case QTC_BOTTOM_LEFT: + case QTC_BOTTOM_RIGHT: + { + QuadTreeIndex bottom_parent = GetNeighbour(nodes[start].parent, 0, 1); + if (bottom_parent == -1) return -1; + if (nodes[start].child_type == QTC_BOTTOM_LEFT) + newNode = nodes[bottom_parent].top_left; + else + newNode = nodes[bottom_parent].top_right; + return GetNeighbour(newNode, xdir, ydir - 1); + } + default: + return -1; + } + } + + // Try moving up, towards the sky. + if (ydir < 0) + { + switch (nodes[start].child_type) + { + case QTC_BOTTOM_LEFT: + case QTC_BOTTOM_RIGHT: + { + if (nodes[start].child_type == QTC_BOTTOM_LEFT) + newNode = nodes[nodes[start].parent].top_left; + else + newNode = nodes[nodes[start].parent].top_right; + + return GetNeighbour(newNode, xdir, ydir + 1); + } + case QTC_TOP_LEFT: + case QTC_TOP_RIGHT: + { + QuadTreeIndex top_parent = GetNeighbour(nodes[start].parent, 0, -1); + if (top_parent == -1) return -1; + if (nodes[start].child_type == QTC_TOP_LEFT) + newNode = nodes[top_parent].bottom_left; + else + newNode = nodes[top_parent].bottom_right; + return GetNeighbour(newNode, xdir, ydir + 1); + } + default: + return -1; + } + } + return -1; +} + } #endif