checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index 5240413..45ddf31 100644 (file)
@@ -47,12 +47,14 @@ public class Mesh implements Iterable<Mesh.T> {
                          (float)(0.25+(0.05*t.color)),
                          (float)(0.75+(0.05*t.color)),
                          (float)0.3); 
+            /*
             if (t.red) {
             gl.glColor4f((float)(0.75+(0.05*t.color)),
                          (float)(0.25+(0.05*t.color)),
                          (float)(0.25+(0.05*t.color)),
                          (float)0.3); 
             }
+            */
             t.glTriangle(gl, m);
         }
         if (option_errorNormals)
@@ -151,43 +153,62 @@ public class Mesh implements Iterable<Mesh.T> {
         }
 
         int i=0;
+
         for(E e : flip) {
             e.flip();
             System.out.println("flip!");
             i++;
-            //if (i>2) break;
         }
+
         System.out.println("count="+count);
-        /*
-        for (E e : flip) {
-            if (e.p1.original && !e.p2.face && !e.p2.original)      e.flip();
-            else if (e.p2.original && !e.p1.face && !e.p1.original) e.flip();
-        }
+
+        rebindPoints();
         HashSet<Vertex> verts = new HashSet<Vertex>();
         for(Vertex v : vertices()) verts.add(v);
-        for(Vertex v : verts) {
-            if (!v.face) continue;
-            //v.move(v.recenter().minus(v.getPoint()), false);
-        }
-        */
-
-        /*
-        Queue<T> q = new LinkedList<T>();
-        OUTER: while(true) {
-            for (T t : this) {
-                if (t.old) { t.shatter(); continue OUTER; }
+        for (Vertex v : verts)
+            v.clearWish();
+        for (Vertex v : verts) {
+            if (v.edge) {
+                for(E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
+                    if (e.p2.original) {
+                        v.wish(e.p2);
+                        v.wish(e.p2);
+                        v.wish(e.p2);
+                        v.wish(e.p2);
+                    }
+                }
+                for(E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
+                    for(E e2 = e.p2.e; e2!=null; e2=e2.pair.next==e.p2.e?null:e2.pair.next) {
+                        if (e2.p2.original) {
+                            v.wish(e.p2);
+                        }
+                    }
+                }
             }
-            break;
         }
-        */
+        for (Vertex v : verts)
+            v.grantWish();
+        System.out.println("-------------------------------------------------------------------");
         /*
-        for (Vertex v : vertices())
-            clearWish();
-        for (Vertex v : vertices()) {
-            
+        for (Vertex v : verts) {
+            if (v.original) {
+                int n=0;
+                for(E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
+                    n++;
+                    v.wish(e.midpoint());
+                    v.wish(e.midpoint());
+                    v.wish(e.next.pair.t.centroid());
+                }
+                v.avgWish();
+                v.wishes = 3;
+                for(int j=0; j<n-3; j++)
+                    v.wish(v.getPoint());
+            }
         }
-        for (Vertex v : vertices())
-            grantWish();
+        for (Vertex v : verts)
+            v.avgWish();
+        for (Vertex v : verts)
+            v.grantWish();
         */
     }
 
@@ -203,6 +224,34 @@ public class Mesh implements Iterable<Mesh.T> {
         public boolean edge = false;
         public boolean face = false;
 
+        private int wishes = 0;
+        private Point wish = Point.ZERO;
+        public void clearWish() { wishes = 0; wish = Point.ZERO; }
+        public void wish(HasPoint hp) {
+            Point p = hp.getPoint();
+            wishes++;
+            wish = new Point(wish.x+p.x, wish.y+p.y, wish.z+p.z);
+        }
+        public void grantWish() {
+            for(Vertex v : (Iterable<Vertex>)getBoundPeers()) {
+                if (v==this) continue;
+                if (v.wishes==0) continue;
+                Point p = this.getBindingMatrix(v).times(v.wish.minus(Point.ZERO).div(v.wishes).plus(Point.ZERO));
+                wish = p.minus(Point.ZERO).times(v.wishes).plus(wish);
+                wishes += v.wishes;
+                v.clearWish();
+            }
+            if (wishes==0) return;
+            Vec d = wish.minus(Point.ZERO).div(wishes).plus(Point.ZERO).minus(getPoint());
+            move(d, false);
+            clearWish();
+        }
+        public void avgWish() {
+            if (wishes==0) return;
+            wish = wish.minus(Point.ZERO).div(wishes).plus(Point.ZERO);
+            wishes = 1;
+        }
+
         private boolean illegal = false;
 
         public boolean visible = false;
@@ -360,8 +409,8 @@ public class Mesh implements Iterable<Mesh.T> {
             if (bindingGroup != null) {
                 /*
                 for(int i=0; i<20 ; i++) {
-                    Point p2 = getConstraint().times(pp);
-                    pp = pp.midpoint(p2);
+                    Point v2 = getConstraint().times(pp);
+                    pp = pp.midpoint(v2);
                     //System.out.println(m.minus(m2));
                 }
             */
@@ -489,7 +538,7 @@ public class Mesh implements Iterable<Mesh.T> {
                 if (e.p1 == this && e.p2 == p2) return e;
                 i++;
                 e.sanity();
-                if (e.destroyed) throw new RuntimeException();
+                if (e.destroyed) throw new RuntimeException("fark " + i + " " + e.prev + " " + (e.prev.next==e) + " " + e.prev.destroyed);
             }
             return null;
         }
@@ -541,9 +590,17 @@ public class Mesh implements Iterable<Mesh.T> {
             Vertex v2 = pair.t.getOtherVertex(pair);
             destroy();
             pair.destroy();
-            newT(v1, v2, p2).red = true;
-            newT(v2, v1, p1).red = true;
+            T t1 = newT(v1, v2, p2);
+            T t2 = newT(v2, v1, p1);
+            t1.e1().sanity();
+            t1.e2().sanity();
+            t1.e3().sanity();
+            t2.e1().sanity();
+            t2.e2().sanity();
+            t2.e3().sanity();
+
             for (E e : (Iterable<E>)getBoundPeers()) {
+                if (e==this) continue;
                 if (e.destroyed) continue;
                 Vertex v1e = e.t.getOtherVertex(e);
                 Vertex v2e = e.pair.t.getOtherVertex(e.pair);
@@ -552,12 +609,10 @@ public class Mesh implements Iterable<Mesh.T> {
                 if (v1e.getE(v2e)!=null) throw new RuntimeException();
                 newT(v1e, v2e, e.p2).red = true;
                 newT(v2e, v1e, e.p1).red = true;
-                makeE(v1.getPoint(),
-                      v2.getPoint()).bindTo(this.getBindingMatrix(e), makeE(v1e.getPoint(), v2e.getPoint()));
-                makeE(v2.getPoint(),
-                      v1.getPoint()).bindTo(pair.getBindingMatrix(e.pair), makeE(v2e.getPoint(), v1e.getPoint()));
-
+                v2e.getE(v1e).bindTo(e.getBindingMatrix(this), v1.getE(v2));
+                v1e.getE(v2e).bindTo(e.pair.getBindingMatrix(this.pair), v2.getE(v1));
             }
+
         }
 
         public void bindingGroupChanged(edu.berkeley.qfat.geom.BindingGroup newBindingGroup_) {
@@ -713,22 +768,37 @@ public class Mesh implements Iterable<Mesh.T> {
             pair.next.t = null;
             pair.prev.t = null;
 
-            if (next.destroyed) throw new RuntimeException();
-            if (prev.destroyed) throw new RuntimeException();
             pair.prev.next = next;
             next.prev = pair.prev;
             prev.next = pair.next;
-            pair.next = prev;
+            pair.next.prev = prev;
+
+            if (p1.e == this) p1.e = pair.next;
+            if (pair.p1.e == pair) pair.p1.e = next;
 
-            if (p1.e == this) p1.e = prev.next;
-            if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
             if (p2.e == this) throw new RuntimeException();
             if (pair.p2.e == pair) throw new RuntimeException();
 
+            /*
+            next = pair;
+            prev = pair;
+            pair.next = this;
+            pair.prev = this;
+            */
+
+            /*
+            pair.prev = null;
+            pair.next = null;
+            next = null;
+            prev = null;
+            */
+
+            /*
             sanity();
             next.sanity();
             prev.sanity();
             pair.sanity();
+            */
         }
 
         private void sync() {