checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index e530ad7..1579551 100644 (file)
@@ -16,7 +16,6 @@ public class Mesh implements Iterable<Mesh.T> {
 
     private PointSet<Vert> pointset = new PointSet<Vert>();
     public Vert nearest(Point p) { return pointset.nearest(p); }
-    private HashMap<Point,Vert> verts = new HashMap<Point,Vert>();
 
     public Iterable<E> edges() {
         return
@@ -37,7 +36,7 @@ public class Mesh implements Iterable<Mesh.T> {
     }
 
     public Iterator<T> iterator() {
-        for(Vert v : verts.values())
+        for(Vert v : pointset)
             if (v.e != null && v.e.t != null)
                 return new FaceIterator(v);
         return new FaceIterator();
@@ -78,6 +77,7 @@ public class Mesh implements Iterable<Mesh.T> {
                 done.add(p);
                 p.rescore();
             }
+        /*
         for(T t : this)
             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() })
                 p.kdremove();
@@ -85,52 +85,19 @@ public class Mesh implements Iterable<Mesh.T> {
         for(T t : this)
             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() })
                 p.kdinsert();
+        */
         return (float)(dist/num);
     }
 
     public void transform(Matrix m) {
         ArrayList<Vert> set = new ArrayList<Vert>();
-        set.addAll(verts.values());
+        for (Vert v : pointset)
+            set.add(v);
         for(Vert v : set) v.transform(m);
     }
 
-    public Vec diagonal() {
-        float min_x = Float.MAX_VALUE;
-        float min_y = Float.MAX_VALUE;
-        float min_z = Float.MAX_VALUE;
-        float max_x = Float.MIN_VALUE;
-        float max_y = Float.MIN_VALUE;
-        float max_z = Float.MIN_VALUE;
-        for(Point p : verts.keySet()) {
-            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;
-            if (p.x > max_x) max_x = p.x;
-            if (p.y > max_y) max_y = p.y;
-            if (p.z > max_z) max_z = p.z;
-        }
-        return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
-    }
-
-    public Point centroid() {
-        float min_x = Float.MAX_VALUE;
-        float min_y = Float.MAX_VALUE;
-        float min_z = Float.MAX_VALUE;
-        float max_x = Float.MIN_VALUE;
-        float max_y = Float.MIN_VALUE;
-        float max_z = Float.MIN_VALUE;
-        for(Point p : verts.keySet()) {
-            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;
-            if (p.x > max_x) max_x = p.x;
-            if (p.y > max_y) max_y = p.y;
-            if (p.z > max_z) max_z = p.z;
-        }
-        return new Point((float)(max_x + min_x)/2,
-                     (float)(max_y + min_y)/2,
-                     (float)(max_z + min_z)/2);
-    }
+    public Vec diagonal() { return pointset.diagonal(); }
+    public Point centroid() { return pointset.centroid(); }
 
     public float volume() {
         double total = 0;
@@ -164,14 +131,18 @@ public class Mesh implements Iterable<Mesh.T> {
         }
     }
 
-    public Vert register(Point p) { Vert v = verts.get(p); return v==null ? new Vert(p) : v; }
+    public Vert register(Point p) { Vert v = pointset.get(p); return v==null ? new Vert(p) : v; }
     public final class Vert extends HasPoint {
         public Point p;
         public Point getPoint() { return p; }
         private Vert(Point p) {
             this.p = p;
-            if (verts.get(p) != null) throw new Error();
-            verts.put(this.p, this);
+            if (pointset.get(p) != null) throw new Error();
+            pointset.add(this);
+        }
+        public void reinsert() {
+            pointset.remove(this);
+            pointset.add(this);
         }
         public void kdremove() {
             if (!inserted) return;
@@ -213,7 +184,7 @@ public class Mesh implements Iterable<Mesh.T> {
                 watch = score_against.nearest(po.p);
 
                 // don't attract to vertices that face the other way
-                if (watch.norm().dot(norm()) < 0) {
+                if (watch.e == null || watch.norm().dot(norm()) < 0) {
                     watch = null;
                 } else {
                     watch.watch_x += po.p.x;
@@ -237,14 +208,14 @@ public class Mesh implements Iterable<Mesh.T> {
             // FIXME: screws up hashmap
             unscore();
             try {
-                if (verts.get(this.p)==null) throw new Error();
-                verts.remove(this.p);
+                if (pointset.get(this.p)==null) throw new Error();
+                pointset.remove(this);
                 float newx = m.a*p.x + m.b*p.y + m.c*p.z + m.d;
                 float newy = m.e*p.x + m.f*p.y + m.g*p.z + m.h;
                 float newz = m.i*p.x + m.j*p.y + m.k*p.z + m.l;
                 this.p = new Point(newx, newy, newz);
                 // FIXME: what if we move onto exactly where another point is?
-                verts.put(this.p,(Vert)this);
+                pointset.add(this);
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }