X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2Fgeom%2FPointSet.java;h=11e48a224834d9f6afb0aa7fbe2ca71b20565dc5;hp=3b701180b0e6150fc25ae5d3ec50879bd2206d13;hb=HEAD;hpb=a3a659d0128908676e5793c7e59884b565c367cf diff --git a/src/edu/berkeley/qfat/geom/PointSet.java b/src/edu/berkeley/qfat/geom/PointSet.java index 3b70118..11e48a2 100644 --- a/src/edu/berkeley/qfat/geom/PointSet.java +++ b/src/edu/berkeley/qfat/geom/PointSet.java @@ -1,19 +1,20 @@ 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 implements Iterable { - private /*final*/ KDTree kd = new KDTree(3); - private final double[] doubles = new double[3]; + private RTree rtree = new RTree(); private HashMap exact = new HashMap(); - public Iterator iterator() { - return exact.values().iterator(); - } + public int size() { return exact.size(); } + + public Iterator iterator() { return rtree.iterator(); } + public void clear() { - kd = new KDTree(3); + exact.clear(); + rtree = new RTree(); } public V get(Point p) { @@ -23,44 +24,24 @@ public class PointSet implements Iterable { 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 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; @@ -68,7 +49,8 @@ public class PointSet implements Iterable { 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; @@ -79,6 +61,7 @@ public class PointSet implements Iterable { 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; @@ -86,7 +69,8 @@ public class PointSet implements Iterable { 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;