X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2Fgeom%2FPointSet.java;h=11e48a224834d9f6afb0aa7fbe2ca71b20565dc5;hp=fe44993abf5e712fe2011f3f63b3fdd0c58c780f;hb=HEAD;hpb=0f9ce20a060db6537a47b549cbf24fd268699ac6 diff --git a/src/edu/berkeley/qfat/geom/PointSet.java b/src/edu/berkeley/qfat/geom/PointSet.java index fe44993..11e48a2 100644 --- a/src/edu/berkeley/qfat/geom/PointSet.java +++ b/src/edu/berkeley/qfat/geom/PointSet.java @@ -1,91 +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 implements Iterable { - private final RTree rtree = new RTree(); + private RTree rtree = new RTree(); - private /*final*/ KDTree kd = new KDTree(3); - private final double[] doubles = new double[3]; + private HashMap exact = new HashMap(); public int size() { return exact.size(); } - private HashMap exact = new HashMap(); - public Iterator iterator() { - return exact.values().iterator(); - } + public Iterator iterator() { return rtree.iterator(); } + public void clear() { - kd = new KDTree(3); - exact = new HashMap(); + exact.clear(); + rtree = new RTree(); } public V get(Point p) { return exact.get(p); } - public void rebuild() { - /* - HashMap old_exact = exact; - exact = new HashMap(); - 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(V v) { Point p = v.getPoint(); - /* - doubles[0] = p.x; - doubles[1] = p.y; - doubles[2] = p.z; - try { - kd.delete(doubles); - } catch (Exception e) { } - */ 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); - } - V kd_says = (V)results[0]; - */ - V rt_says = rtree.nearest(p); - //if (kd_says != rt_says) System.err.println("disagree: " + p + " " + kd_says + " " + rt_says); - return rt_says; + return rtree.nearest(p, vis); } - public Vec diagonal() { float min_x = Float.MAX_VALUE; float min_y = Float.MAX_VALUE; @@ -93,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; @@ -104,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; @@ -111,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;