checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index 5ba7404..55052b9 100644 (file)
@@ -65,13 +65,13 @@ public class Mesh implements Iterable<Mesh.T> {
         }
     }
 
-    public void unscore() {
+    public void unApplyQuadricToNeighborAll() {
         HashSet<Vert> done = new HashSet<Vert>();
         for(T t : this)
             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
                 if (done.contains(p)) continue;
                 done.add(p);
-                p.unscore();
+                p.unApplyQuadricToNeighbor();
             }
     }
     public void recomputeAllFundamentalQuadrics() {
@@ -83,7 +83,7 @@ public class Mesh implements Iterable<Mesh.T> {
                 p.recomputeFundamentalQuadric();
             }
     }
-    public float rescore() {
+    public float applyQuadricToNeighborAll() {
         int num = 0;
         double dist = 0;
         HashSet<Vert> done = new HashSet<Vert>();
@@ -91,7 +91,7 @@ public class Mesh implements Iterable<Mesh.T> {
             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
                 if (done.contains(p)) continue;
                 done.add(p);
-                p.rescore();
+                p.applyQuadricToNeighbor();
                 
             }
         return (float)(dist/num);
@@ -171,7 +171,7 @@ public class Mesh implements Iterable<Mesh.T> {
         }
 
         public void recomputeFundamentalQuadric() {
-            unscore();
+            unApplyQuadricToNeighbor();
             Matrix m = Matrix.ZERO;
             E e = this.e;
             do {
@@ -180,26 +180,29 @@ public class Mesh implements Iterable<Mesh.T> {
                 e = e.pair.next;
             } while(e != this.e);
             fundamentalQuadric = m;
-            rescore();
+            applyQuadricToNeighbor();
         }
 
-        public void unscore() {
+        public void unApplyQuadricToNeighbor() {
             if (nearest_in_other_mesh == null) return;
             if (fundamentalQuadric == null) return;
+            nearest_in_other_mesh.unsc();
             nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.minus(fundamentalQuadric);
             nearest_in_other_mesh.quadric_count--;
             if (nearest_in_other_mesh.quadric_count==0)
                 nearest_in_other_mesh.quadric = Matrix.ZERO;
+            nearest_in_other_mesh.resc();
             nearest_in_other_mesh = null;
         }
 
-        public void rescore() {
-            if (score_against == null) return;
-
+        public void unsc() {
             score -= oldscore;
             oldscore = 0;
+        }
+        public void applyQuadricToNeighbor() {
+            if (score_against == null) return;
 
-            if (nearest_in_other_mesh != null) unscore();
+            if (nearest_in_other_mesh != null) unApplyQuadricToNeighbor();
             if (nearest_in_other_mesh == null) {
                 nearest_in_other_mesh = score_against.nearest(p);
 
@@ -207,18 +210,23 @@ public class Mesh implements Iterable<Mesh.T> {
                 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.unsc();
                     nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
                     nearest_in_other_mesh.quadric_count++;
+                    nearest_in_other_mesh.resc();
                 }
             }
-
+            unsc();
+            resc();
+        }
+        public void resc() {
             oldscore = quadric_count == 0 ? 0 : (quadric.preAndPostMultiply(p) / quadric_count);
             score += oldscore;
         }
 
         /** does NOT update bound pairs! */
         public boolean transform(Matrix m) {
-            unscore();
+            unApplyQuadricToNeighbor();
             try {
                 if (pointset.get(this.p)==null) throw new Error();
                 pointset.remove(this);
@@ -230,7 +238,7 @@ public class Mesh implements Iterable<Mesh.T> {
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
-            rescore();
+            applyQuadricToNeighbor();
 
             // should recompute fundamental quadrics of all vertices sharing a face, but we defer...
             // FIXME: intersection test needed?