X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2Fgeom%2FRTree.java;h=35b6f9b03d89e902c4695a33e5b7f2edf0b940ce;hb=37516ec826e2017925130f192176632240f67b17;hp=48fade20639e1448d0353044e5c2fa28a9c62084;hpb=0f9ce20a060db6537a47b549cbf24fd268699ac6;p=anneal.git diff --git a/src/edu/berkeley/qfat/geom/RTree.java b/src/edu/berkeley/qfat/geom/RTree.java index 48fade2..35b6f9b 100644 --- a/src/edu/berkeley/qfat/geom/RTree.java +++ b/src/edu/berkeley/qfat/geom/RTree.java @@ -4,54 +4,81 @@ import java.util.*; import com.infomatiq.jsi.*; import com.infomatiq.jsi.rtree.*; -public class RTree { +/** wrapper around the com.infomatiq.jsi.rtree.RTree class */ +public class RTree implements Iterable { private com.infomatiq.jsi.rtree.RTree rtree = new com.infomatiq.jsi.rtree.RTree(); - int lowid = 0; - HashMap idToV = new HashMap(); - HashMap vToId = new HashMap(); + private int lowid = 0; + private HashMap idToV = new HashMap(); + private HashMap vToId = new HashMap(); - public RTree() { - Properties props = new Properties(); + public Iterator iterator() { return vToId.keySet().iterator(); } + + private final MyIntProcedure myIntProcedure = new MyIntProcedure(); + private final com.infomatiq.jsi.Rectangle rect = new com.infomatiq.jsi.Rectangle(0,0,0,0,0,0); + private final com.infomatiq.jsi.Point point = new com.infomatiq.jsi.Point(0,0,0); + private V found = null; + private Visitor visitor = null; + + private static final Properties props = new Properties(); + static { props.put("MinNodeEntries", "1"); props.put("MaxNodeEntries", "5"); + } + + public RTree() { clear(); } + + public void clear() { + idToV.clear(); + vToId.clear(); rtree.init(props); } + public void add(V v) { insert(v); } public void insert(V v) { int id = lowid++; idToV.put(id, v); vToId.put(v, id); - rtree.add(new com.infomatiq.jsi.Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(), - v.getMaxX(), v.getMaxY(), v.getMaxZ()), - id); + rect.set(v.getMinX(), v.getMinY(), v.getMinZ(), v.getMaxX(), v.getMaxY(), v.getMaxZ()); + rtree.add(rect, id); } public void remove(V v) { - int id = vToId.get(v); + Integer idi = vToId.get(v); + if (idi==null) return; + int id = idi; idToV.remove(id); vToId.remove(v); - rtree.delete(new com.infomatiq.jsi.Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(), - v.getMaxX(), v.getMaxY(), v.getMaxZ()), - id); + rect.set(v.getMinX(), v.getMinY(), v.getMinZ(), v.getMaxX(), v.getMaxY(), v.getMaxZ()); + rtree.delete(rect, id); } + public V nearest(Point p) { + point.set(p.x, p.y, p.z); + rtree.nearest(point, myIntProcedure, Float.POSITIVE_INFINITY); + V ret = found; + found = null; + return ret; + } - V found = null; + public void range(HasBoundingBox v, Visitor vis) { + visitor = vis; + rtree.intersects(rect, myIntProcedure); + visitor = null; + } - private IntProcedure finder = new IntProcedure() { - public boolean execute(int id) { + private class MyIntProcedure implements IntProcedure { + public boolean execute(int id) { + if (visitor != null) { + V v = idToV.get(id); + visitor.visit(v); + return true; + } else { found = idToV.get(id); return false; } - }; - - public V nearest(Point p) { - rtree.nearest(new com.infomatiq.jsi.Point(p.x, p.y, p.z), finder, Float.POSITIVE_INFINITY); - V ret = found; - found = null; - return ret; + } } }