checkpoint
authoradam <adam@megacz.com>
Sun, 16 Dec 2007 03:34:50 +0000 (19:34 -0800)
committeradam <adam@megacz.com>
Sun, 16 Dec 2007 03:34:50 +0000 (19:34 -0800)
darcs-hash:20071216033450-5007d-94d1ba089a772e21d30d97866035a05a30eb8f96.gz

src/edu/berkeley/qfat/Mesh.java
src/edu/berkeley/qfat/geom/HasPoint.java
src/edu/berkeley/qfat/geom/HasQuadric.java

index 42f03d2..f63bd1d 100644 (file)
@@ -76,7 +76,6 @@ public class Mesh implements Iterable<Mesh.T> {
 
     /** a vertex in the mesh */
     public final class Vertex extends HasQuadric implements Visitor {
-        public String toString() { return p.toString(); }
         public Point p;
         E e;                // some edge *leaving* this point
 
@@ -92,47 +91,32 @@ public class Mesh implements Iterable<Mesh.T> {
             vertices.add(this);
         }
 
-        private void glNormal(GL gl) {
-            Vec norm = norm();
-            gl.glNormal3f(norm.x, norm.y, norm.z);
+        public float oldscore = 0;
+        public void setScore(float nscore) {
+            score -= oldscore;
+            oldscore = nscore;
+            score += oldscore;
         }
 
-        public void _recomputeFundamentalQuadric() {
+        public Matrix _recomputeFundamentalQuadric() {
             Matrix m = Matrix.ZERO;
             int count = 0;
             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
-                T t = e.t;
-                m = m.plus(t.norm().fundamentalQuadric(t.centroid()));
+                m = m.plus(e.t.norm().fundamentalQuadric(e.t.centroid()));
                 count++;
             }
-            quadricStale = false;
-            fundamentalQuadric = m.times(1/(float)count);
+            return m.times(1/(float)count);
         }
 
-        public void reComputeErrorAround() {
-            reComputeError();
-            if (nearest_in_other_mesh != null) nearest_in_other_mesh.reComputeError();
-            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
-                e.p2.reComputeError();
-        }
-        public void reComputeError() {
-            unComputeError();
-            computeError();
-        }
-        public void unComputeError() {
-            score -= oldscore;
-            oldscore = 0;
-        }
         public HasQuadric nearest() {
             if (score_against==null) return null;
             return score_against.vertices.nearest(p, this);
         }
+        public void unComputeError() { setScore(0); }
         public void computeError() {
-            oldscore =
+            float nscore =
                 quadric_count != 0
                 ? (quadric.preAndPostMultiply(p) * 100) / quadric_count
-                : immutableVertices
-                ? oldscore
                 : nearest_in_other_mesh != null
                 ? nearest_in_other_mesh.fundamentalQuadric().preAndPostMultiply(p) * 100 * 10
                 : score_against != null
@@ -142,10 +126,9 @@ public class Mesh implements Iterable<Mesh.T> {
                 double ang = Math.abs(e.crossAngle());
                 if (ang > Math.PI) throw new Error();
                 float minangle = (float)(Math.PI * 0.8);
-                if (ang > minangle)
-                    oldscore += (ang - minangle);
+                if (ang > minangle) nscore += (ang - minangle);
             }
-            score += oldscore;
+            setScore(nscore);
         }
 
         private void removeTrianglesFromRTree() {
@@ -187,9 +170,17 @@ public class Mesh implements Iterable<Mesh.T> {
             if (!ignorecollision && good) triangles.range(oldp, this.p, (Visitor<T>)this);
 
             reComputeErrorAround();
+
             return good;
         }
 
+        public void reComputeErrorAround() {
+            reComputeError();
+            if (nearest_in_other_mesh != null) nearest_in_other_mesh.reComputeError();
+            for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
+                e.p2.reComputeError();
+        }
+
         public boolean visit(Object o) {
             if (o instanceof T) {
                 T t = (T)o;
@@ -249,6 +240,10 @@ public class Mesh implements Iterable<Mesh.T> {
             return null;
         }
 
+        private void glNormal(GL gl) {
+            Vec norm = norm();
+            gl.glNormal3f(norm.x, norm.y, norm.z);
+        }
         public Vec norm() {
             Vec norm = new Vec(0, 0, 0);
             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
index e4e6d9e..200dab8 100644 (file)
@@ -10,4 +10,5 @@ public abstract class HasPoint implements HasBoundingBox {
     public float getMinY() { return getPoint().getMinY(); }
     public float getMaxZ() { return getPoint().getMaxZ(); }
     public float getMinZ() { return getPoint().getMinZ(); }
+    public String toString() { return getPoint().toString(); }
 }
index 8addfc2..e358e69 100644 (file)
@@ -4,7 +4,6 @@ import javax.media.opengl.*;
 /** any object associated with a specific point in 3D space */
 public abstract class HasQuadric extends HasPoint {
 
-        public float oldscore = 0;
     public Matrix errorQuadric() { return quadric; }
     public boolean quadricStale = false;
     /** the nearest vertex in the "score_against" mesh */
@@ -18,61 +17,66 @@ public abstract class HasQuadric extends HasPoint {
 
     public Matrix fundamentalQuadric = null;
 
-        public void recomputeFundamentalQuadricIfNeighborChanged() {
-            HasQuadric oldv = nearest_in_other_mesh;
-            HasQuadric newv = nearest();
-            if (oldv==newv) return;
+    public void recomputeFundamentalQuadricIfNeighborChanged() {
+        HasQuadric oldv = nearest_in_other_mesh;
+        HasQuadric newv = nearest();
+        if (oldv==newv) return;
+        recomputeFundamentalQuadric();
+        if (oldv!=null) oldv.recomputeFundamentalQuadricIfNeighborChanged();
+        // for some reason this causes an infinite loop
+        //if (newv!=null) newv.recomputeFundamentalQuadricIfNeighborChanged();
+    }
+    public void recomputeFundamentalQuadricIfStale() {
+        if (quadricStale || fundamentalQuadric==null) 
             recomputeFundamentalQuadric();
-            if (oldv!=null) oldv.recomputeFundamentalQuadricIfNeighborChanged();
-            // for some reason this causes an infinite loop
-            //if (newv!=null) newv.recomputeFundamentalQuadricIfNeighborChanged();
+    }
+    public void recomputeFundamentalQuadric() {
+        unApplyQuadricToNeighbor();
+        if (quadricStale || fundamentalQuadric==null) {
+            fundamentalQuadric = _recomputeFundamentalQuadric();
+            quadricStale = false;
         }
-        public void recomputeFundamentalQuadricIfStale() {
-            if (quadricStale || fundamentalQuadric==null) 
-                recomputeFundamentalQuadric();
+        applyQuadricToNeighbor();
+    }
+    public abstract Matrix _recomputeFundamentalQuadric();
+        public void reComputeError() {
+            unComputeError();
+            computeError();
         }
-        public void recomputeFundamentalQuadric() {
-            unApplyQuadricToNeighbor();
-            if (quadricStale || fundamentalQuadric==null) _recomputeFundamentalQuadric();
-            applyQuadricToNeighbor();
-        }
-    public abstract void _recomputeFundamentalQuadric();
-    public abstract void reComputeErrorAround();
-    public abstract void reComputeError();
     public abstract void unComputeError();
     public abstract void computeError();
     public abstract HasQuadric nearest();
-        public void applyQuadricToNeighbor() {
-            HasQuadric new_nearest = nearest();
-            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) throw new Error();
+    public void applyQuadricToNeighbor() {
+        HasQuadric new_nearest = nearest();
+        if (nearest_in_other_mesh != null && new_nearest == nearest_in_other_mesh) return;
 
-            nearest_in_other_mesh = new_nearest;
+        if (nearest_in_other_mesh != null) unApplyQuadricToNeighbor();
+        if (nearest_in_other_mesh != null) throw new Error();
 
-            if (nearest_in_other_mesh!=null) {
-                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();
-            }
+        nearest_in_other_mesh = new_nearest;
 
-            reComputeError();
-        }
-        public Matrix fundamentalQuadric() {
-            if (fundamentalQuadric == null) recomputeFundamentalQuadric();
-            return fundamentalQuadric;
-        }
-        public void unApplyQuadricToNeighbor() {
-            if (nearest_in_other_mesh == null) return;
-            if (fundamentalQuadric == null) return;
+        if (nearest_in_other_mesh!=null) {
             nearest_in_other_mesh.unComputeError();
-            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.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
+            nearest_in_other_mesh.quadric_count++;
             nearest_in_other_mesh.computeError();
-            nearest_in_other_mesh = null;
         }
+
+        reComputeError();
+    }
+    public Matrix fundamentalQuadric() {
+        if (fundamentalQuadric == null) recomputeFundamentalQuadric();
+        return fundamentalQuadric;
+    }
+    public void unApplyQuadricToNeighbor() {
+        if (nearest_in_other_mesh == null) return;
+        if (fundamentalQuadric == null) return;
+        nearest_in_other_mesh.unComputeError();
+        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.computeError();
+        nearest_in_other_mesh = null;
+    }
 }