X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2Fgeom%2FRTree.java;h=57f20c894faf0d434c820cbf6e91f32781f4d3f4;hp=48fade20639e1448d0353044e5c2fa28a9c62084;hb=eabe4f7acd947415f183290dc3269b2502a25a1c;hpb=0f9ce20a060db6537a47b549cbf24fd268699ac6 diff --git a/src/edu/berkeley/qfat/geom/RTree.java b/src/edu/berkeley/qfat/geom/RTree.java index 48fade2..57f20c8 100644 --- a/src/edu/berkeley/qfat/geom/RTree.java +++ b/src/edu/berkeley/qfat/geom/RTree.java @@ -4,54 +4,93 @@ 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(); + private HashMap vToRect = new HashMap(); - public RTree() { - Properties props = new Properties(); + public Iterator iterator() { return vToId.keySet().iterator(); } + + private final MyIntProcedure myIntProcedure = new MyIntProcedure(); + private final Rectangle rect = new 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(); + vToRect.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); + Rectangle rect = new Rectangle(v.getMinX(), v.getMinY(), v.getMinZ(), v.getMaxX(), v.getMaxY(), v.getMaxZ()); + rtree.add(rect, id); + vToRect.put(v, rect); } 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); + rtree.delete(vToRect.get(v), id); + vToRect.remove(v); + } + + public V nearest(Point p) { return nearest(p, null); } + public V nearest(Point p, Visitor ip) { + point.set(p.x, p.y, p.z); + this.visitor = ip; + rtree.nearest(point, myIntProcedure, Float.POSITIVE_INFINITY); + this.visitor = null; + V ret = found; + found = null; + return ret; } + public void range(HasBoundingBox v, Visitor vis) { + visitor = vis; + rect.set(v.getMinX(), v.getMinY(), v.getMinZ(), v.getMaxX(), v.getMaxY(), v.getMaxZ()); + rtree.intersects(rect, myIntProcedure); + visitor = null; + } - V found = null; + public void range(Point p1, Point p2, Visitor vis) { + visitor = vis; + rect.set(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z); + rtree.intersects(rect, myIntProcedure); + visitor = null; + } - private IntProcedure finder = new IntProcedure() { - public boolean execute(int id) { - found = idToV.get(id); + private class MyIntProcedure implements IntProcedure { + public boolean execute(int id) { + found = idToV.get(id); + if (visitor != null) { + return visitor.visit(found); + } else { 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; + } } }