/** @see com.infomatiq.jsi.SpatialIndex#nearest(Point, IntProcedure, float) */ public void nearest(Point p, IntProcedure v, float furthestDistance) { Node rootNode = getNode(rootNodeId); nearest(p, rootNode, furthestDistance); visitProc.setProcedure(v); nearestIds.forEach(visitProc); nearestIds.clear(); }
/** * Recursively searches the tree for the nearest entry. Other queries call execute() on an * IntProcedure when a matching entry is found; however nearest() must store the entry Ids as it * searches the tree, in case a nearer entry is found. Uses the member variable nearestIds to * store the nearest entry IDs. * * <p>[x] TODO rewrite this to be non-recursive? */ private float nearest(Point p, Node n, float nearestDistance) { for (int i = 0; i < n.entryCount; i++) { float tempDistance = n.entries[i].distance(p); if (n.isLeaf()) { // for leaves, the distance is an actual nearest distance if (tempDistance < nearestDistance) { nearestDistance = tempDistance; nearestIds.clear(); } if (tempDistance <= nearestDistance) { nearestIds.add(n.ids[i]); } } else { // for index nodes, only go into them if they potentially could have // a rectangle nearer than actualNearest if (tempDistance <= nearestDistance) { // search the child node nearestDistance = nearest(p, getNode(n.ids[i]), nearestDistance); } } } return nearestDistance; }