checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index b4f282a..87209a7 100644 (file)
@@ -50,7 +50,7 @@ public class Mesh implements Iterable<Mesh.T> {
     public void transform(Matrix m) {
         ArrayList<Vertex> set = new ArrayList<Vertex>();
         for(Vertex v : vertices) set.add(v);
-        for(Vertex v : set) v.transform(m);
+        for(Vertex v : set) v.transform(m.times(v.p));
     }
 
     public void rebuild() { /*vertices.rebuild();*/ }
@@ -81,6 +81,7 @@ public class Mesh implements Iterable<Mesh.T> {
 
         Matrix binding = Matrix.ONE;
         Vertex bound_to = this;
+        private boolean good;
 
         public Point getPoint() { return p; }
         public float error() { return olderror; }
@@ -108,11 +109,7 @@ public class Mesh implements Iterable<Mesh.T> {
             return m.times(1/(float)count);
         }
 
-        public HasQuadric nearest() {
-            if (error_against==null) return null;
-            return error_against.vertices.nearest(p, this);
-        }
-        public void unComputeError() { setError(0); }
+        public HasQuadric nearest() { return error_against==null ? null : error_against.vertices.nearest(p, this); }
         public void computeError() {
             float nerror =
                 quadric_count != 0
@@ -131,17 +128,8 @@ public class Mesh implements Iterable<Mesh.T> {
             setError(nerror);
         }
 
-        private void removeTrianglesFromRTree() {
-            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
-                if (e.t != null) e.t.removeFromRTree();
-        }
-        private void addTrianglesToRTree() {
-            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
-                if (e.t != null) e.t.addToRTree();
-        }
-
         /** does NOT update bound pairs! */
-        public boolean transform(Matrix m) {
+        public boolean transform(Point newp) {
             if (immutableVertices) throw new Error();
 
             unApplyQuadricToNeighbor();
@@ -149,12 +137,11 @@ public class Mesh implements Iterable<Mesh.T> {
 
             if (vertices.get(this.p)==null) throw new Error();
             vertices.remove(this);
-            removeTrianglesFromRTree();
-            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);
-            addTrianglesToRTree();
+            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
+                if (e.t != null) e.t.removeFromRTree();
+            this.p = newp;
+            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
+                if (e.t != null) e.t.addToRTree();
             vertices.add(this);
 
             applyQuadricToNeighbor();
@@ -169,8 +156,6 @@ public class Mesh implements Iterable<Mesh.T> {
 
             if (!ignorecollision && good) triangles.range(oldp, this.p, (Visitor<T>)this);
 
-            reComputeErrorAround();
-
             return good;
         }
 
@@ -201,16 +186,20 @@ public class Mesh implements Iterable<Mesh.T> {
                 return true;
             }
         }
-        private boolean good;
 
+        Point oldp;
         public boolean move(Vec v) {
             Matrix m = Matrix.translate(v);
-            Vertex p = this;
             boolean good = true;
-            do {
-                good &= p.transform(m);
-                p = p.bound_to;
-            } while (p != this);
+            for(Vertex p = this; p != null; p = (p.bound_to==this)?null:p.bound_to) {
+                p.oldp = p.p;
+                good &= p.transform(m.times(p.p));
+            }
+            for(Vertex p = this; p != null; p = (p.bound_to==this)?null:p.bound_to)
+                if (good)
+                    p.reComputeErrorAround();
+                else 
+                    p.transform(p.oldp);
             return good;
         }