checkpoint
[anneal.git] / src / edu / berkeley / qfat / Main.java
index 95cd989..e7ee361 100644 (file)
@@ -6,11 +6,37 @@ import javax.swing.*;
 import javax.media.opengl.*;
 import javax.media.opengl.glu.*;
 import java.util.*;
+import edu.berkeley.qfat.bind.*;
 import edu.berkeley.qfat.geom.*;
 import edu.berkeley.qfat.geom.Point;
 import edu.berkeley.qfat.geom.Polygon;
 
 // TO DO:
+//
+// - Implement "real" constraints (plane, line, point)
+//
+// - Constrained surface subdivision
+//     - Edge.flip() gets us the triforce subdivision
+//     - Edge.delete() gets us the catmull-clark subdivision
+//     - Catmull-Clark: just don't move points if we can't.  Need to average the influence of the points on a binding group.
+//
+// - Ability to snap three views to orthgonal
+// - SLIDE UI
+//     - left button -> crystal ball
+//     - translate
+//     - rightbutton/mousewheel zoom
+// - v+click to select vertex
+// - show: constraints, bindings, faces
+//
+// Editing:
+//  - fracture edge, face
+//  - change aspect ratio
+//  - ability to select a point, rotate the model, then move the point
+//  - when moving a vertex in one window, show that window's axes in all other windows
+//
+
+
+// TO DO:
 // - real anneal
 // - solve self-intersection problem
 // - get a better test model?
@@ -50,7 +76,9 @@ import edu.berkeley.qfat.geom.Polygon;
 
 // FIXME: re-orient goal (how?)
 
-public class Main extends MeshViewer {
+public class Main extends InteractiveMeshViewer {
+
+
 
     public static int verts = 1;
 
@@ -70,7 +98,7 @@ public class Main extends MeshViewer {
                 if (v.mag() < 0.0001) continue;
                 v = v.times(-1);
                 v = v.times(0.5f);
-                Point p = Point.ORIGIN.plus(v);
+                Point p = Point.ZERO.plus(v);
                 v = v.times(-1);
                 
                 //System.out.println(v);
@@ -79,7 +107,7 @@ public class Main extends MeshViewer {
                 polygons.add(new Polygon(hs));
         }
         for(Polygon p : polygons) {
-            System.out.println(p.plane.norm + " " + p.plane.dvalue);
+            System.out.println(p.plane.norm() + " " + p.plane.d);
             for(HalfSpace hs : halfSpaces) {
                 if (p.plane==hs) continue;
                 p = p.intersect(hs);
@@ -107,7 +135,7 @@ public class Main extends MeshViewer {
             StlFile stlf = new StlFile();
             InputStream res = this.getClass().getClassLoader().getResourceAsStream(file);
             stlf.readBinaryFile(file, res);
-            goal = new Mesh(false);
+            setGoal(new Mesh(false));
             for(int i=0; i<stlf.coordArray.length; i+=3) {
                 Point p0 = new Point(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
                 Point p1 = new Point(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
@@ -135,8 +163,7 @@ public class Main extends MeshViewer {
 
     public Main(JFrame f) { super(f); }
 
-    public void fixupTile() { 
-        synchronized(safeTriangles) {
+    public synchronized void fixupTile() { 
         for(Matrix m1 : transforms) {
             for(Matrix m2 : transforms) {
                 if (m1==m2) continue;
@@ -199,7 +226,6 @@ public class Main extends MeshViewer {
         tile.error_against = goal;
         goal.error_against = tile;
         fixupGoal();
-        }
     }
 
     public void breakit() {
@@ -236,8 +262,7 @@ public class Main extends MeshViewer {
         Vec v = new Vec(random.nextFloat(), random.nextFloat(), random.nextFloat());
         v = v.norm().times((random.nextFloat() - 0.5f) * max);
         Matrix m = Matrix.translate(v);
-
-        boolean good = p.move(m, false);
+        boolean good = p.move(v, false);
         if (!good) { return false; }
 
         double new_tile_error = tile.error();
@@ -263,7 +288,7 @@ public class Main extends MeshViewer {
             hits++;
             p.goodp = p.p;
         } else {
-            p.move(Matrix.translate(v.times(-1)), true);
+            p.move(v.times(-1), true);
             misses++;
         }
         p.reComputeErrorAround();
@@ -280,13 +305,7 @@ public class Main extends MeshViewer {
         double acceptance = 1;
         while(true) {
             synchronized(this) {
-            synchronized(safeTriangles) {
-                safeTriangles.clear();
-                for(Mesh.T t : tile) 
-                    if (t.shouldBeDrawn())
-                        safeTriangles.add(t);
-            }
-            if (!anneal) { repaint(); Thread.sleep(10); continue; }
+                if (!anneal) { repaint(); Thread.sleep(10); continue; }
 
             double ratio = (hits+misses==0) ? 1 : (hits / (hits+misses));
             hits = 0;
@@ -355,7 +374,9 @@ public class Main extends MeshViewer {
 
     public static void main(String[] s) throws Exception {
         JFrame f = new JFrame();
+        f.setLayout(new BorderLayout());
         Main main = new Main(f);
+        f.add(main, BorderLayout.CENTER);
         f.setJMenuBar(main.new MyMenuBar());
         f.pack();
         f.show();
@@ -371,16 +392,14 @@ public class Main extends MeshViewer {
         }
         public void actionPerformed(ActionEvent event) {
             synchronized(Main.this) {
-                synchronized(safeTriangles) {
                     hit();
-                }
             }
         }
         public void hit() {}
     }
 
     public void hexBrick(boolean offset, boolean rotated) {
-                tile = new Mesh(false);
+        setTile(new Mesh(false));
                 float width  = (float)0.8;
                 float depth  = (float)0.08;
                 float height = (float)0.4;
@@ -503,9 +522,9 @@ public class Main extends MeshViewer {
                         es.add(t.e3());
                     }
                     for(Mesh.E e : es) {
-                        if (e.p1.p.x == e.p2.p.x && e.p1.p.y == e.p2.p.y) continue;
-                        if (e.p1.p.z == e.p2.p.z && e.p1.p.y == e.p2.p.y) continue;
-                        if (e.p1.p.x == e.p2.p.x && e.p1.p.z == e.p2.p.z) continue;
+                        if (e.v1.p.x == e.v2.p.x && e.v1.p.y == e.v2.p.y) continue;
+                        if (e.v1.p.z == e.v2.p.z && e.v1.p.y == e.v2.p.y) continue;
+                        if (e.v1.p.x == e.v2.p.x && e.v1.p.z == e.v2.p.z) continue;
                         e.shatter();
                     }
                     transforms = new Matrix[] {
@@ -613,7 +632,7 @@ public class Main extends MeshViewer {
                 hexBrick(false, true);
             }});
             tileMenu.add(new MyMenuItem("Temp (do not use)") { public void hit() {
-                tile = new Mesh(false);
+                setTile(new Mesh(false));
                 float width  = (float)0.8;
                 float depth  = (float)0.08;
                 float height = (float)0.4;
@@ -674,7 +693,7 @@ public class Main extends MeshViewer {
                 fixupTile();
             } });
             tileMenu.add(new MyMenuItem("Dense Packing (hex)") { public void hit() {
-                tile = new Mesh(false);
+                setTile(new Mesh(false));
                 float width  = (float)3.2;
                 float depth  = (float)0.32;
                 float height = (float)1.6;
@@ -716,7 +735,7 @@ public class Main extends MeshViewer {
                 fixupTile();
             } });
             tileMenu.add(new MyMenuItem("Slim Dense Packing (Cubic)") { public void hit() {
-                tile = new Mesh(false);
+                setTile(new Mesh(false));
                 float unit = 0.4f;
                 float r = unit/2;
                 float sin = (float)(unit * Math.sin(Math.PI/3));
@@ -766,13 +785,13 @@ public class Main extends MeshViewer {
                 //Matrix m = Matrix.scale(1f, 2.1f, 1f);
                 tile.transform(m);
                 for(int i=0; i<transforms.length; i++)
-                    transforms[i] = transforms[i].preMultiplyTranslationalComponentBy(m);
+                    transforms[i] = preMultiplyTranslationalComponentBy(transforms[i], m);
                 fixupTile();
 
             } });
             tileMenu.add(new MyMenuItem("Genus-1") { public void hit() {
                 synchronized(this) {
-                    tile = new Mesh(false);
+                    setTile(new Mesh(false));
                     Matrix mm = Matrix.scale(0.1f);
                     float height = 4;
                     float width  = 4;
@@ -863,6 +882,7 @@ public class Main extends MeshViewer {
                         Matrix.translate(new Vec(-0.2f,0,0))
                         .times(Matrix.rotate(new Vec(1,0,0), (float)(-1*Math.PI/2))),
 
+
                         //Matrix.rotate(new Vec(0,0,1), (float)(1*Math.PI/2)),
 
                         /*
@@ -903,7 +923,7 @@ public class Main extends MeshViewer {
                 }}});
             tileMenu.add(new MyMenuItem("Hammerhead") { public void hit() {
                 synchronized(this) {
-                    tile = new Mesh(false);
+                    setTile(new Mesh(false));
                     Matrix mm = Matrix.ONE;
                     float height1 = .1f;
                     float height2 = .1f;
@@ -984,4 +1004,12 @@ public class Main extends MeshViewer {
 
     }
 
+    private static Matrix preMultiplyTranslationalComponentBy(Matrix mthis, Matrix mm) {
+        Vec v = mm.times(mthis.getTranslationalComponent());
+        return new Matrix(mthis.a, mthis.b, mthis.c, v.x,
+                          mthis.e, mthis.f, mthis.g, v.y,
+                          mthis.i, mthis.j, mthis.k, v.z,
+                          mthis.m, mthis.n, mthis.o, 1);
+    }
+
 }
\ No newline at end of file