checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / RTree.java
index 2fab5f5..5a29093 100644 (file)
@@ -4,6 +4,7 @@ import java.util.*;
 import com.infomatiq.jsi.*;
 import com.infomatiq.jsi.rtree.*;
 
+/** wrapper around the <tt>com.infomatiq.jsi.rtree.RTree</tt> class */
 public class RTree<V extends HasBoundingBox> implements Iterable<V> {
 
     private com.infomatiq.jsi.rtree.RTree rtree =
@@ -15,10 +16,10 @@ public class RTree<V extends HasBoundingBox> implements Iterable<V> {
 
     public Iterator<V> iterator() { return vToId.keySet().iterator(); }
 
-    private final MyIntProcedure myIntProcedure = new MyIntProcedure();
+    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 final com.infomatiq.jsi.Point point    = new com.infomatiq.jsi.Point(0,0,0);
+    private V          found   = null;
     private Visitor<V> visitor = null;
 
     private static final Properties props = new Properties();
@@ -54,31 +55,39 @@ public class RTree<V extends HasBoundingBox> implements Iterable<V> {
         rtree.delete(rect, 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) {
+    public V nearest(Point p) { return nearest(p, null); }
+    public V nearest(Point p, Visitor<V> 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) {
+    public void range(HasBoundingBox v, Visitor<V> vis) {
         visitor = vis;
+        rect.set(v.getMinX(), v.getMinY(), v.getMinZ(), v.getMaxX(), v.getMaxY(), v.getMaxZ());
         rtree.intersects(rect, myIntProcedure);
         visitor = null;
     }
 
+    public void range(Point p1, Point p2, Visitor<V> vis) {
+        visitor = vis;
+        rect.set(p1.x, p1.y, p1.z, p2.x, p2.y, p2.z);
+        rtree.intersects(rect, myIntProcedure);
+        visitor = null;
+    }
+
+    private class MyIntProcedure implements IntProcedure {
+        public boolean execute(int id) {
+            found = idToV.get(id);
+            if (visitor != null) {
+                return visitor.visit(found);
+            } else {
+                return false;
+            }
+        }
+    }
 }