checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / RTree.java
index c2fd9b4..b9078e1 100644 (file)
@@ -4,19 +4,28 @@ import java.util.*;
 import com.infomatiq.jsi.*;
 import com.infomatiq.jsi.rtree.*;
 
-public class RTree<V extends HasBoundingBox> {
+public class RTree<V extends HasBoundingBox> implements Iterable<V> {
 
     private com.infomatiq.jsi.rtree.RTree rtree =
         new com.infomatiq.jsi.rtree.RTree();
 
-    int lowid = 0;
-    HashMap<Integer, V> idToV = new HashMap<Integer, V>();
-    HashMap<V, Integer> vToId = new HashMap<V, Integer>();
+    private int lowid = 0;
+    private HashMap<Integer, V> idToV = new HashMap<Integer, V>();
+    private HashMap<V, Integer> vToId = new HashMap<V, Integer>();
 
-    public RTree() {
-        Properties props = new Properties();
+    public Iterator<V> iterator() { return vToId.keySet().iterator(); }
+
+    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);
     }
 
@@ -25,20 +34,19 @@ public class RTree<V extends HasBoundingBox> {
         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);
     }
 
+    private com.infomatiq.jsi.Rectangle rect = new com.infomatiq.jsi.Rectangle(0,0,0,0,0,0);
     public void remove(V 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);
     }
 
     // gross...