checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index c2d2988..5c913c4 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), true);
     }
 
     public void rebuild() { /*vertices.rebuild();*/ }
@@ -76,7 +76,7 @@ public class Mesh implements Iterable<Mesh.T> {
 
     /** a vertex in the mesh */
     public final class Vertex extends HasQuadric implements Visitor {
-        public Point p;
+        public Point p, oldp;
         E e;                // some edge *leaving* this point
 
         Matrix binding = Matrix.ONE;
@@ -129,17 +129,17 @@ public class Mesh implements Iterable<Mesh.T> {
         }
 
         /** does NOT update bound pairs! */
-        public boolean transform(Matrix m) {
+        private boolean transform(Point newp, boolean ignoreProblems) {
+            this.oldp = this.p;
             if (immutableVertices) throw new Error();
 
             unApplyQuadricToNeighbor();
-            Point oldp = this.p;
 
             if (vertices.get(this.p)==null) throw new Error();
             vertices.remove(this);
             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 = m.times(this.p);
+            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);
@@ -148,13 +148,15 @@ public class Mesh implements Iterable<Mesh.T> {
 
             good = true;
 
-            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
-                if (Math.abs(e.crossAngle()) > (Math.PI * 0.9) || Math.abs(e.next.crossAngle()) > (Math.PI * 0.9)) good = false;
-                if (e.t.aspect() < 0.1) good = false;
-                e.p2.quadricStale = true;
-            }
+            if (!ignoreProblems)
+                for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
+                    if (Math.abs(e.crossAngle()) > (Math.PI * 0.9) || Math.abs(e.next.crossAngle()) > (Math.PI * 0.9)) good = false;
+                    if (e.t.aspect() < 0.1) good = false;
+                    e.p2.quadricStale = true;
+                }
 
-            if (!ignorecollision && good) triangles.range(oldp, this.p, (Visitor<T>)this);
+            if (!ignorecollision && !ignoreProblems && good)
+                triangles.range(oldp, this.p, (Visitor<T>)this);
 
             return good;
         }
@@ -167,36 +169,28 @@ public class Mesh implements Iterable<Mesh.T> {
         }
 
         public boolean visit(Object o) {
-            if (o instanceof T) {
-                T t = (T)o;
-                if (!good) return false;
-                for(E e = Vertex.this.e; e!=null; e=e.pair.next==Vertex.this.e?null:e.pair.next) {
-                    if (!t.has(e.p1) && !t.has(e.p2) && e.intersects(t)) { good = false; }
-                    if (e.t != null) {
-                        if (!e.t.has(t.e1().p1) && !e.t.has(t.e1().p2) && t.e1().intersects(e.t)) { good = false; }
-                        if (!e.t.has(t.e2().p1) && !e.t.has(t.e2().p2) && t.e2().intersects(e.t)) { good = false; }
-                        if (!e.t.has(t.e3().p1) && !e.t.has(t.e3().p2) && t.e3().intersects(e.t)) { good = false; }
-                    }
+            if (o instanceof Vertex)
+                return ((Vertex)o).e != null && ((Vertex)o).norm().dot(Vertex.this.norm()) >= 0;
+            T t = (T)o;
+            if (!good) return false;
+            for(E e = Vertex.this.e; e!=null; e=e.pair.next==Vertex.this.e?null:e.pair.next) {
+                if (!t.has(e.p1) && !t.has(e.p2) && e.intersects(t)) { good = false; }
+                if (e.t != null) {
+                    if (!e.t.has(t.e1().p1) && !e.t.has(t.e1().p2) && t.e1().intersects(e.t)) { good = false; }
+                    if (!e.t.has(t.e2().p1) && !e.t.has(t.e2().p2) && t.e2().intersects(e.t)) { good = false; }
+                    if (!e.t.has(t.e3().p1) && !e.t.has(t.e3().p2) && t.e3().intersects(e.t)) { good = false; }
                 }
-                return good;
-            } else {
-                Vertex v = (Vertex)o;
-                if (v.e==null || v.norm().dot(Vertex.this.norm()) < 0)
-                    return false;
-                return true;
             }
+            return good;
         }
 
-        public boolean move(Vec v) {
-            Matrix m = Matrix.translate(v);
+        public boolean move(Matrix m, boolean ignoreProblems) {
             boolean good = true;
             for(Vertex p = this; p != null; p = (p.bound_to==this)?null:p.bound_to)
-                good &= p.transform(m);
+                good &= p.transform(m.times(p.p), ignoreProblems);
             for(Vertex p = this; p != null; p = (p.bound_to==this)?null:p.bound_to)
-                if (good)
-                    p.reComputeErrorAround();
-                else 
-                    p.transform(Matrix.translate(v.times(-1)));
+                if (good)  p.reComputeErrorAround();
+                else       p.transform(p.oldp, true);
             return good;
         }