checkpoint
[anneal.git] / src / edu / berkeley / qfat / Main.java
index 0ff0dcf..44a8312 100644 (file)
@@ -26,19 +26,19 @@ import edu.berkeley.qfat.geom.Point;
 //    http://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c
 
 /*
-blender keys
-- middle mouse = option+click
-- right mouse = command+click
+  blender keys
+  - middle mouse = option+click
+  - right mouse = command+click
 
-3,7,1 = view along axes (control for opp direction)
-4, 8, 7, 2 = rotate in discrete increments (+control to translate)
-middle trag: rotate space
-shift+middle drag: translate space
-wheel: zoom
-home: home view: take current angle, zoom to whole scnee
-5 = ortho vs non-ortho
+  3,7,1 = view along axes (control for opp direction)
+  4, 8, 7, 2 = rotate in discrete increments (+control to translate)
+  middle trag: rotate space
+  shift+middle drag: translate space
+  wheel: zoom
+  home: home view: take current angle, zoom to whole scnee
+  5 = ortho vs non-ortho
 
- */
+*/
 
 
 // FIXME: re-orient goal (how?)
@@ -73,9 +73,9 @@ public class Main extends MeshViewer {
         float goal_depth  = goal.diagonal().dot(new Vec(0, 0, 1));
 
         /*
-        float width  = (float)0.6;
-        float height = (float)0.08;
-        float depth  = (float)0.3;
+          float width  = (float)0.6;
+          float height = (float)0.08;
+          float depth  = (float)0.3;
         */
         float width  = (float)0.7;
         float depth  = (float)0.08;
@@ -95,8 +95,8 @@ public class Main extends MeshViewer {
             Matrix.translate(new Vec(rshift, -depth,    halfup)),
 
             /*
-            Matrix.translate(new Vec(0,  depth,    halfup)),
-            Matrix.translate(new Vec(0, -depth,    halfup)),
+              Matrix.translate(new Vec(0,  depth,    halfup)),
+              Matrix.translate(new Vec(0, -depth,    halfup)),
             */
 
             Matrix.translate(new Vec(lshift,       0,  height)),
@@ -239,7 +239,7 @@ public class Main extends MeshViewer {
             }
         }
 
-        //xMesh.Vert mid = lbf.getE(mbn).shatter();
+        //xMesh.Vertex mid = lbf.getE(mbn).shatter();
 
         // rescale to match volume
         float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
@@ -247,6 +247,7 @@ public class Main extends MeshViewer {
 
         // translate to match centroid
         goal.transform(Matrix.translate(tile.centroid().minus(goal.centroid())));
+        goal.makeVerticesImmutable();
 
         //tx.e2.shatter();
         //tx.e3.shatter();
@@ -265,14 +266,17 @@ public class Main extends MeshViewer {
 
         tile.score_against = goal;
         goal.score_against = tile;
-        tile.tilemesh = true;
     }
 
     public synchronized void breakit() {
         int oldverts = verts;
         System.out.println("doubling vertices.");
         PriorityQueue<Mesh.E> es = new PriorityQueue<Mesh.E>();
-        for(Mesh.E e : tile.edges()) es.add(e);
+        for(Mesh.T t : tile) {
+            es.add(t.e1());
+            es.add(t.e2());
+            es.add(t.e3());
+        }
         for(int i=0; i<oldverts; i++) {
             Mesh.E e = es.poll();
             verts++;
@@ -282,7 +286,7 @@ public class Main extends MeshViewer {
         tile.rebindPoints();
     }
 
-    public synchronized void rand(float temp, Mesh.Vert p) {
+    public synchronized void rand(float temp, Mesh.Vertex p) {
 
         //p.reComputeError();
         p.reComputeErrorAround();
@@ -291,15 +295,15 @@ public class Main extends MeshViewer {
 
         Vec v;
         /*
-        Matrix inv = p.errorQuadric();
-        v = new Vec(inv.d, inv.h, inv.l).norm().times(1/(float)300);
-        if (p.quadric_count == 0) {
-            v = goal.nearest(p.p).p.minus(p.p).norm().times(1/(float)300);
-        }
+          Matrix inv = p.errorQuadric();
+          v = new Vec(inv.d, inv.h, inv.l).norm().times(1/(float)300);
+          if (p.quadric_count == 0) {
+          v = goal.nearest(p.p).p.minus(p.p).norm().times(1/(float)300);
+          }
         */
         Vec v2 = new Vec((random.nextFloat() - (float)0.5) / 500,
-                        (random.nextFloat() - (float)0.5)  / 500,
-                        (random.nextFloat() - (float)0.5)  / 500);
+                         (random.nextFloat() - (float)0.5)  / 500,
+                         (random.nextFloat() - (float)0.5)  / 500);
         //v = v.plus(v2.norm().times(1/(float)300));
         v = v2.norm().times(1/(float)300);
 
@@ -335,78 +339,79 @@ public class Main extends MeshViewer {
         float last = 10;
         while(true) {
             synchronized(this) {
-            double ratio = (hits+misses==0) ? 1 : (hits / (hits+misses));
-            hits = 0;
-            misses = 0;
-            float gamma = 0;
-            double acceptance = ratio;
-            accepts = (int)(Math.ceil(ratio*100));
-            temps = (int)(Math.ceil(temp*1000));
-            vertss = tile.size();
-            if (breaks > 0) { while (breaks>0) {
-                breaks--;
-                    breakit();
-                    //gamma = 1;
-                    gamma = 1;
-                    //temp = last * 0.8f;
-                    //last = temp;
-                    //temp = hightemp;
-                } } else
-            if (acceptance > 0.96) gamma = 0.4f;
-            else if (acceptance > 0.9) gamma = 0.5f;
-            else if (acceptance > 0.8) gamma = 0.65f;
-            else if (acceptance > 0.6) gamma = 0.7f;
-            else {
-                if (acceptance > 0.3) {
-                    gamma = 0.9f;
-                } else if (acceptance > 0.15) {
-                    gamma = 0.95f;
-                } else if (acceptance > 0.10) {
-                    gamma = 0.98f;
-                } else {
-                    breakit();
-                    //gamma = 1;
-                    gamma = 0.99f;
-                    //gamma = 1;
-                    //temp = last * 0.8f;
-                    //last = temp;
-                    //temp = hightemp;
+                double ratio = (hits+misses==0) ? 1 : (hits / (hits+misses));
+                hits = 0;
+                misses = 0;
+                float gamma = 0;
+                double acceptance = ratio;
+                accepts = (int)(Math.ceil(ratio*100));
+                temps = (int)(Math.ceil(temp*1000));
+                vertss = tile.size();
+                if (breaks > 0) { while (breaks>0) {
+                        breaks--;
+                        breakit();
+                        //gamma = 1;
+                        gamma = 1;
+                        //temp = last * 0.8f;
+                        //last = temp;
+                        //temp = hightemp;
+                    } } else
+                    if (acceptance > 0.96) gamma = 0.4f;
+                    else if (acceptance > 0.9) gamma = 0.5f;
+                    else if (acceptance > 0.8) gamma = 0.65f;
+                    else if (acceptance > 0.6) gamma = 0.7f;
+                    else {
+                        if (acceptance > 0.3) {
+                            gamma = 0.9f;
+                        } else if (acceptance > 0.15) {
+                            gamma = 0.95f;
+                        } else if (acceptance > 0.10) {
+                            gamma = 0.98f;
+                        } else {
+                            breakit();
+                            //gamma = 1;
+                            gamma = 0.99f;
+                            //gamma = 1;
+                            //temp = last * 0.8f;
+                            //last = temp;
+                            //temp = hightemp;
+                        }
+                    }
+                temp = temp * gamma;
+
+
+                HashSet<Mesh.Vertex> hs = new HashSet<Mesh.Vertex>();
+                for(Mesh.Vertex p : tile.vertices()) hs.add(p);
+                Mesh.Vertex[] pts = (Mesh.Vertex[])hs.toArray(new Mesh.Vertex[0]);
+
+                int count = 0;
+                long then = System.currentTimeMillis();
+                for(int i=0; i<40; i++) {
+                    if (anneal) {
+                        count++;
+                        Mesh.Vertex v = pts[Math.abs(random.nextInt()) % pts.length];
+                        rand(temp,v);
+                        v.recomputeFundamentalQuadricIfStale();
+                        v.recomputeFundamentalQuadricIfNeighborChanged();
+                    }
+                    Thread.yield();
+                    repaint();
                 }
-            }
-            temp = temp * gamma;
+                System.out.println("temp="+temp + " ratio="+(Math.ceil(ratio*100)) + " " +
+                                   "points_per_second=" +
+                                   (count*1000)/((double)(System.currentTimeMillis()-then)));
 
+                for(Mesh.Vertex p : goal.vertices()) p.recomputeFundamentalQuadricIfNeighborChanged();
 
-            HashSet<Mesh.Vert> hs = new HashSet<Mesh.Vert>();
-            for(Mesh.Vert p : tile.vertices()) hs.add(p);
-            Mesh.Vert[] pts = (Mesh.Vert[])hs.toArray(new Mesh.Vert[0]);
-
-            int count = 0;
-            long then = System.currentTimeMillis();
-            for(int i=0; i<100; i++) {
-                if (anneal) {
-                    count++;
-                    Mesh.Vert v = pts[Math.abs(random.nextInt()) % pts.length];
-                    rand(temp,v);
+                synchronized(safeTriangles) {
+                    safeTriangles.clear();
+                    for(Mesh.T t : tile) if (t.shouldBeDrawn()) safeTriangles.add(t);
                 }
-                Thread.yield();
-                repaint();
-            }
-            System.out.println("temp="+temp + " ratio="+(Math.ceil(ratio*100)) + " " +
-                               "points_per_second=" +
-                               (count*1000)/((double)(System.currentTimeMillis()-then)));
-            tile.rebuildPointSet();
-            repaint();
-            //breakit();
-            repaint();
-            goal.unApplyQuadricToNeighborAll();
-            repaint();
-            tile.recomputeAllFundamentalQuadrics();
-            repaint();
-            goal.applyQuadricToNeighborAll();
             }
-       }
+        }
     }
 
+
     public static void main(String[] s) throws Exception {
         StlFile stlf = new StlFile();
         stlf.load("fish.stl");