checkpoint
authoradam <adam@megacz.com>
Wed, 5 Dec 2007 07:01:51 +0000 (23:01 -0800)
committeradam <adam@megacz.com>
Wed, 5 Dec 2007 07:01:51 +0000 (23:01 -0800)
darcs-hash:20071205070151-5007d-808c85315331e436407357f95f2ea7c2eaf9a43d.gz

src/edu/berkeley/qfat/Main.java
src/edu/berkeley/qfat/Mesh.java
src/edu/berkeley/qfat/geom/Matrix.java

index b774066..000fd94 100644 (file)
@@ -264,10 +264,21 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         r1 = r1 - (float)Math.floor(r1);
         r1 = r1 * (float)0.01;
         r1 = r1 - (float)0.005;
+        /*
+        public Vert partner() { return quadric==null ? this : quadric; }
+        public Point quadric() { return quadric_count==0 ? partner().p :
+                new Point(quadric_x/quadric_count, quadric_y/quadric_count, quadric_z/quadric_count); }
 
         Vec v = p.nearest_vert_in_other_mesh().minus(p.p).norm().times(r1);
-
+        */
         //v = p.norm().times(v.dot(p.norm()));
+        /*
+        Vec v = new Vec((random.nextFloat() - (float)0.5) / 1000,
+                        (random.nextFloat() - (float)0.5) / 1000,
+                        (random.nextFloat() - (float)0.5) / 1000);
+        */
+        Matrix inv = p.errorQuadric();
+        Vec v = new Vec(inv.d, inv.h, inv.l).norm().times(1/(float)1000);
 
         boolean aspect = false;//(Math.abs(random.nextInt()) % 100) <= 2;
         Matrix old_tile_aspect = null;//goal.aspect;
@@ -412,7 +423,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
                 for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
                     p.p.glVertex(gl);
                     //p.plus(p.norm().times(p.score()*10)).glVertex(gl);
-                    p.partner().p.glVertex(gl);
+                    //p.partner().p.glVertex(gl);
                     //tile.nearest(p).centroid().glVertex(gl);
                 }
 
index f635754..efeb89b 100644 (file)
@@ -126,24 +126,24 @@ public class Mesh implements Iterable<Mesh.T> {
         E e;                // some edge *leaving* this point
 
         Vert bound_to = this;
-        int nearest_vert_in_other_mesh_count;
-        float nearest_vert_in_other_mesh_x;
-        float nearest_vert_in_other_mesh_y;
-        float nearest_vert_in_other_mesh_z;
-        Vert nearest_vert_in_other_mesh;
+
+        /** the nearest vertex in the "score_against" mesh */
+        Vert   nearest_in_other_mesh;
+        /** the number of vertices in the other mesh for which this is the nearest_in_other_mesh */
+        int    quadric_count;
+        /** the total error quadric (contributions from all vertices in other mesh for which this is nearest) */
+        Matrix quadric = Matrix.ZERO;
+
         Matrix binding = new Matrix();
         float oldscore = 0;
         boolean inserted = false;
 
-        public Matrix quadric() {
-            Matrix m = new Matrix(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
-            E e = this.e;
-            do {
-                T t = e.t;
-                m = m.plus(t.norm().fundamentalQuadric(t.centroid()));
-                e = e.pair.next;
-            } while(e != this.e);
-            return m;
+        public Matrix errorQuadric() { return quadric; }
+
+        private Matrix fundamentalQuadric = null;
+        public Matrix fundamentalQuadric() {
+            if (fundamentalQuadric == null) recomputeFundamentalQuadric();
+            return fundamentalQuadric;
         }
 
         public Point getPoint() { return p; }
@@ -153,49 +153,58 @@ public class Mesh implements Iterable<Mesh.T> {
             pointset.add(this);
         }
         public float score() { return oldscore; }
+
+        public void recomputeFundamentalQuadric() {
+            unscore();
+            Matrix m = Matrix.ZERO;
+            E e = this.e;
+            do {
+                T t = e.t;
+                m = m.plus(t.norm().fundamentalQuadric(t.centroid()));
+                e = e.pair.next;
+            } while(e != this.e);
+            fundamentalQuadric = m;
+            rescore();
+        }
+
         public void unscore() {
-            if (nearest_vert_in_other_mesh == null) return;
-            nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_x -= p.x;
-            nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_y -= p.y;
-            nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_z -= p.z;
-            nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_count--;
-            if (nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_count==0) {
-                nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_x = 0;
-                nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_y = 0;
-                nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_z = 0;
-            }
-            nearest_vert_in_other_mesh = null;
+            if (nearest_in_other_mesh == null) return;
+            if (fundamentalQuadric == null) return;
+            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 = null;
         }
-        public Vert partner() { return nearest_vert_in_other_mesh==null ? this : nearest_vert_in_other_mesh; }
-        public Point nearest_vert_in_other_mesh() { return nearest_vert_in_other_mesh_count==0 ? partner().p :
-                new Point(nearest_vert_in_other_mesh_x/nearest_vert_in_other_mesh_count, nearest_vert_in_other_mesh_y/nearest_vert_in_other_mesh_count, nearest_vert_in_other_mesh_z/nearest_vert_in_other_mesh_count); }
+
         public void rescore() {
             if (score_against == null) return;
 
             score -= oldscore;
             oldscore = 0;
 
-            if (nearest_vert_in_other_mesh != null) unscore();
-            Vert po = this;
-            if (nearest_vert_in_other_mesh == null) {
-                nearest_vert_in_other_mesh = score_against.nearest(po.p);
+            if (nearest_in_other_mesh != null) unscore();
+            if (nearest_in_other_mesh == null) {
+                nearest_in_other_mesh = score_against.nearest(p);
 
                 // don't attract to vertices that face the other way
-                if (nearest_vert_in_other_mesh.e == null || nearest_vert_in_other_mesh.norm().dot(norm()) < 0) {
-                    nearest_vert_in_other_mesh = null;
+                if (nearest_in_other_mesh.e == null || nearest_in_other_mesh.norm().dot(norm()) < 0) {
+                    nearest_in_other_mesh = null;
                 } else {
-                    nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_x += po.p.x;
-                    nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_y += po.p.y;
-                    nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_z += po.p.z;
-                    nearest_vert_in_other_mesh.nearest_vert_in_other_mesh_count++;
+                    nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
+                    nearest_in_other_mesh.quadric_count++;
                 }
             }
 
+            /*
             double s1, s2;
-            if (nearest_vert_in_other_mesh_count==0) s1 = 0;
-            else                s1 = p.distance(nearest_vert_in_other_mesh_x/nearest_vert_in_other_mesh_count, nearest_vert_in_other_mesh_y/nearest_vert_in_other_mesh_count, nearest_vert_in_other_mesh_z/nearest_vert_in_other_mesh_count);
-            s2 = nearest_vert_in_other_mesh==null ? 0 : po.p.distance(nearest_vert_in_other_mesh.p);
+            if (quadric_count==0) s1 = 0;
+            else                  s1 = p.distance(quadric_x/quadric_count, quadric_y/quadric_count, quadric_z/quadric_count);
+            s2 = quadric==null ? 0 : po.p.distance(quadric.p);
             oldscore = (float)(s1 + s2);
+            */
+            oldscore = quadric.preAndPostMultiply(p);
+
             score += oldscore;
         }
 
@@ -213,7 +222,18 @@ public class Mesh implements Iterable<Mesh.T> {
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
+            fundamentalQuadric = fundamentalQuadric();
             rescore();
+
+            // recompute fundamental quadrics of all vertices sharing a face
+            E e = this.e;
+            do {
+                e.t.v1().recomputeFundamentalQuadric();
+                e.t.v2().recomputeFundamentalQuadric();
+                e.t.v3().recomputeFundamentalQuadric();
+                e = e.pair.next;
+            } while(e != this.e);
+            
             boolean good = true;
             /*
             for(T t : this) {
index 7d14a71..346f53e 100644 (file)
@@ -2,6 +2,9 @@ package edu.berkeley.qfat.geom;
 
 /** affine matrix; immutable */
 public class Matrix {
+
+    public static final Matrix ZERO = new Matrix(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
+
     //
     //  [ a b c d ]   [ x ]
     //  [ e f g h ]   [ y ]
@@ -95,4 +98,68 @@ public class Matrix {
     public Vec apply(Vec v) { return v; }
     public Matrix invert() { return this; }
     public Matrix times(Matrix m) { return this; }
+
+
+    public float determinant() {
+        float m00 = a;
+        float m01 = b;
+        float m02 = c;
+        float m03 = d;
+        float m10 = e;
+        float m11 = f;
+        float m12 = g;
+        float m13 = h;
+        float m20 = i;
+        float m21 = j;
+        float m22 = k;
+        float m23 = l;
+        float m30 = m;
+        float m31 = n;
+        float m32 = o;
+        float m33 = p;
+        return
+            m03 * m12 * m21 * m30-m02 * m13 * m21 * m30-m03 * m11 * m22 * m30+m01 * m13    * m22 * m30+
+            m02 * m11 * m23 * m30-m01 * m12 * m23 * m30-m03 * m12 * m20 * m31+m02 * m13    * m20 * m31+
+            m03 * m10 * m22 * m31-m00 * m13 * m22 * m31-m02 * m10 * m23 * m31+m00 * m12    * m23 * m31+
+            m03 * m11 * m20 * m32-m01 * m13 * m20 * m32-m03 * m10 * m21 * m32+m00 * m13    * m21 * m32+
+            m01 * m10 * m23 * m32-m00 * m11 * m23 * m32-m02 * m11 * m20 * m33+m01 * m12    * m20 * m33+
+            m02 * m10 * m21 * m33-m00 * m12 * m21 * m33-m01 * m10 * m22 * m33+m00 * m11    * m22 * m33;
+    }
+
+    public Matrix inverse() {
+        float m00 = a;
+        float m01 = b;
+        float m02 = c;
+        float m03 = d;
+        float m10 = e;
+        float m11 = f;
+        float m12 = g;
+        float m13 = h;
+        float m20 = i;
+        float m21 = j;
+        float m22 = k;
+        float m23 = l;
+        float m30 = m;
+        float m31 = n;
+        float m32 = o;
+        float m33 = p;
+        return
+            new Matrix(m12*m23*m31 - m13*m22*m31 + m13*m21*m32 - m11*m23*m32 - m12*m21*m33 + m11*m22*m33,
+                       m03*m22*m31 - m02*m23*m31 - m03*m21*m32 + m01*m23*m32 + m02*m21*m33 - m01*m22*m33,
+                       m02*m13*m31 - m03*m12*m31 + m03*m11*m32 - m01*m13*m32 - m02*m11*m33 + m01*m12*m33,
+                       m03*m12*m21 - m02*m13*m21 - m03*m11*m22 + m01*m13*m22 + m02*m11*m23 - m01*m12*m23,
+                       m13*m22*m30 - m12*m23*m30 - m13*m20*m32 + m10*m23*m32 + m12*m20*m33 - m10*m22*m33,
+                       m02*m23*m30 - m03*m22*m30 + m03*m20*m32 - m00*m23*m32 - m02*m20*m33 + m00*m22*m33,
+                       m03*m12*m30 - m02*m13*m30 - m03*m10*m32 + m00*m13*m32 + m02*m10*m33 - m00*m12*m33,
+                       m02*m13*m20 - m03*m12*m20 + m03*m10*m22 - m00*m13*m22 - m02*m10*m23 + m00*m12*m23,
+                       m11*m23*m30 - m13*m21*m30 + m13*m20*m31 - m10*m23*m31 - m11*m20*m33 + m10*m21*m33,
+                       m03*m21*m30 - m01*m23*m30 - m03*m20*m31 + m00*m23*m31 + m01*m20*m33 - m00*m21*m33,
+                       m01*m13*m30 - m03*m11*m30 + m03*m10*m31 - m00*m13*m31 - m01*m10*m33 + m00*m11*m33,
+                       m03*m11*m20 - m01*m13*m20 - m03*m10*m21 + m00*m13*m21 + m01*m10*m23 - m00*m11*m23,
+                       m12*m21*m30 - m11*m22*m30 - m12*m20*m31 + m10*m22*m31 + m11*m20*m32 - m10*m21*m32,
+                       m01*m22*m30 - m02*m21*m30 + m02*m20*m31 - m00*m22*m31 - m01*m20*m32 + m00*m21*m32,
+                       m02*m11*m30 - m01*m12*m30 - m02*m10*m31 + m00*m12*m31 + m01*m10*m32 - m00*m11*m32,
+                       m01*m12*m20 - m02*m11*m20 + m02*m10*m21 - m00*m12*m21 - m01*m10*m22 + m00*m11*m22)
+            .times(1/determinant());
+    }
 }