checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index 74ccd4b..f4f0837 100644 (file)
@@ -135,6 +135,7 @@ public class Mesh implements Iterable<Mesh.T> {
         }
     }
 
+    public void rebuildPointSet() { pointset.rebuild(); }
     public Vec diagonal() { return pointset.diagonal(); }
     public Point centroid() { return pointset.centroid(); }
     public Vert nearest(Point p) { return pointset.nearest(p); }
@@ -153,6 +154,7 @@ public class Mesh implements Iterable<Mesh.T> {
         Vert bound_to = this;
         Matrix binding = new Matrix();
         float oldscore = 0;
+        boolean quadricStale = false;
 
         public Matrix errorQuadric() { return quadric; }
         public Point getPoint() { return p; }
@@ -171,6 +173,8 @@ public class Mesh implements Iterable<Mesh.T> {
         }
 
         public void recomputeFundamentalQuadric() {
+            if (!quadricStale && fundamentalQuadric != null) return;
+            quadricStale = false;
             unApplyQuadricToNeighbor();
             Matrix m = Matrix.ZERO;
             E e = this.e;
@@ -198,19 +202,22 @@ public class Mesh implements Iterable<Mesh.T> {
         public void applyQuadricToNeighbor() {
             if (score_against == null) return;
 
+            Vert new_nearest = score_against.nearest(p);
+            if (nearest_in_other_mesh != null && new_nearest == nearest_in_other_mesh) return;
+
             if (nearest_in_other_mesh != null) unApplyQuadricToNeighbor();
-            if (nearest_in_other_mesh == null) {
-                nearest_in_other_mesh = score_against.nearest(p);
-
-                // don't attract to vertices that face the other way
-                if (nearest_in_other_mesh.e == null || nearest_in_other_mesh.norm().dot(norm()) < 0) {
-                    nearest_in_other_mesh = null;
-                } else {
-                    nearest_in_other_mesh.unComputeError();
-                    nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
-                    nearest_in_other_mesh.quadric_count++;
-                    nearest_in_other_mesh.computeError();
-                }
+            if (nearest_in_other_mesh != null) throw new Error();
+
+            nearest_in_other_mesh = new_nearest;
+                
+            // don't attract to vertices that face the other way
+            if (nearest_in_other_mesh.e == null || nearest_in_other_mesh.norm().dot(norm()) < 0) {
+                nearest_in_other_mesh = null;
+            } else {
+                nearest_in_other_mesh.unComputeError();
+                nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
+                nearest_in_other_mesh.quadric_count++;
+                nearest_in_other_mesh.computeError();
             }
             reComputeError();
         }
@@ -245,6 +252,13 @@ public class Mesh implements Iterable<Mesh.T> {
             applyQuadricToNeighbor();
 
             // should recompute fundamental quadrics of all vertices sharing a face, but we defer...
+            E e = this.e;
+            do {
+                e.p2.quadricStale = true;
+                e = e.pair.next;
+            } while(e != this.e);
+
+
             // FIXME: intersection test needed?
             return true;
         }