add items to TODO list
[anneal.git] / src / edu / berkeley / qfat / geom / PointSet.java
index cd3edb7..11e48a2 100644 (file)
@@ -1,76 +1,47 @@
 package edu.berkeley.qfat.geom;
-import edu.wlu.cs.levy.CG.KDTree;
 import java.util.*;
 
+/** a set of points, plus many useful methods operating over the set */
 public class PointSet<V extends HasPoint> implements Iterable<V> {
 
-    private /*final*/ KDTree kd = new KDTree(3);
-    private final double[] doubles = new double[3];
+    private RTree<V> rtree = new RTree<V>();
 
-    public int size() { return exact.size(); }
     private HashMap<Point,V> exact = new HashMap<Point,V>();
 
-    public Iterator<V> iterator() {
-        return exact.values().iterator();
-    }
+    public int size() { return exact.size(); }
+
+    public Iterator<V> iterator() { return rtree.iterator(); }
+
     public void clear() {
-        kd = new KDTree(3);
-        exact = new HashMap<Point,V>();
+        exact.clear();
+        rtree = new RTree<V>();
     }
 
     public V get(Point p) {
         return exact.get(p);
     }
 
-    public void rebuild() {
-        HashMap<Point,V> old_exact = exact;
-        exact = new HashMap<Point,V>();
-        kd = new KDTree(3);
-        for(V v : old_exact.values()) add(v);
-    }
-
     public void add(V v) {
         V x = get(v.getPoint());
         if (x != null && x.equals(v)) return;
         if (x != null) throw new Error("duplicates!");
         Point p = v.getPoint();
-        doubles[0] = p.x;
-        doubles[1] = p.y;
-        doubles[2] = p.z;
-        try {
-            kd.insert(doubles, v);
-        } catch (Exception e) {
-            throw new Error(e);
-        }
+        rtree.insert(v);
         exact.put(p, v);
     }
 
-    public void remove(HasPoint v) { remove(v.getPoint()); }
-    public void remove(Point p) {
-        doubles[0] = p.x;
-        doubles[1] = p.y;
-        doubles[2] = p.z;
-        try {
-            kd.delete(doubles);
-        } catch (Exception e) { }
+    public void remove(V v) {
+        Point p = v.getPoint();
+        rtree.remove(v);
         exact.remove(p);
     }
 
-    public V nearest(Point p) {
+    public V nearest(Point p) { return nearest(p, null); }
+    public V nearest(Point p, Visitor<V> vis) {
         if (exact.size()==0) return null;
-        Object[] results;
-        try {
-            doubles[0] = p.x;
-            doubles[1] = p.y;
-            doubles[2] = p.z;
-            results = kd.nearest(doubles,1);
-        } catch (Exception e) {
-            throw new Error(e);
-        }
-        return (V)results[0];
+        return rtree.nearest(p, vis);
     }
 
-
     public Vec diagonal() {
         float min_x = Float.MAX_VALUE;
         float min_y = Float.MAX_VALUE;
@@ -78,7 +49,8 @@ public class PointSet<V extends HasPoint> implements Iterable<V> {
         float max_x = Float.MIN_VALUE;
         float max_y = Float.MIN_VALUE;
         float max_z = Float.MIN_VALUE;
-        for(Point p : exact.keySet()) {
+        for(V v : this) {
+            Point p = v.getPoint();
             if (p.x < min_x) min_x = p.x;
             if (p.y < min_y) min_y = p.y;
             if (p.z < min_z) min_z = p.z;
@@ -89,6 +61,7 @@ public class PointSet<V extends HasPoint> implements Iterable<V> {
         return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
     }
 
+    // FEATURE: compute incrementally?
     public Point centroid() {
         float min_x = Float.MAX_VALUE;
         float min_y = Float.MAX_VALUE;
@@ -96,7 +69,8 @@ public class PointSet<V extends HasPoint> implements Iterable<V> {
         float max_x = Float.MIN_VALUE;
         float max_y = Float.MIN_VALUE;
         float max_z = Float.MIN_VALUE;
-        for(Point p : exact.keySet()) {
+        for(V v : this) {
+            Point p = v.getPoint();
             if (p.x < min_x) min_x = p.x;
             if (p.y < min_y) min_y = p.y;
             if (p.z < min_z) min_z = p.z;