checkpoint
[anneal.git] / src / edu / berkeley / qfat / Main.java
index 65c6c2a..bdab101 100644 (file)
@@ -5,6 +5,8 @@ import javax.swing.*;
 import javax.media.opengl.*;
 import javax.media.opengl.glu.*;
 import java.util.*;
+import edu.berkeley.qfat.geom.*;
+import edu.berkeley.qfat.geom.Point;
 
 // FIXME: recenter goal to have centroid coincident with tile
 // FIXME: re-orient goal (how?)
@@ -17,7 +19,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
     boolean control = false;
 
     public void mouseWheelMoved(MouseWheelEvent e) {
-        tz -= e.getWheelRotation() ;
+        tz -= e.getWheelRotation();
     }
 
     public void keyTyped(KeyEvent e)  { }
@@ -66,70 +68,70 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         mousey = e.getY();
     }
 
-    private Geom tile = new Geom();
-    private Geom goal = new Geom();
+    private Mesh tile = new Mesh();
+    private Mesh goal = new Mesh();
 
     /** magnification factor */
     private static final float MAG = 1;
 
-    Geom.M[] translations;
-    Geom.V[] points;
+    Matrix[] translations;
+    Mesh.Vert[] points;
 
     public Main(StlFile stlf) {
 
         for(int i=0; i<stlf.coordArray.length; i+=3) {
-            Geom.V p0 = goal.register(new Geom.P(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG));
-            Geom.V p1 = goal.register(new Geom.P(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG));
-            Geom.V p2 = goal.register(new Geom.P(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG));
-            Geom.Vec n  = new Geom.Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
-            Geom.T t  = goal.newT(p0, p1, p2, n);
+            Mesh.Vert p0 = goal.register(new Point(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG));
+            Mesh.Vert p1 = goal.register(new Point(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG));
+            Mesh.Vert p2 = goal.register(new Point(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG));
+            Vec n  = new Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
+            Mesh.T t  = goal.newT(p0, p1, p2, n);
         }
 
         // rotate to align major axis -- this probably needs to be done by a human.
-        goal.transform(new Geom.M(new Geom.Vec(0, 0, 1), (float)(Math.PI/2)));
+        goal.transform(new Matrix(new Vec(0, 0, 1), (float)(Math.PI/2)));
 
 
-        float goal_width  = goal.diagonal().dot(new Geom.Vec(1, 0, 0));
-        float goal_height = goal.diagonal().dot(new Geom.Vec(0, 1, 0));
-        float goal_depth  = goal.diagonal().dot(new Geom.Vec(0, 0, 1));
+        float goal_width  = goal.diagonal().dot(new Vec(1, 0, 0));
+        float goal_height = goal.diagonal().dot(new Vec(0, 1, 0));
+        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;
-        translations = new Geom.M[] {
-
-            new Geom.M(new Geom.Vec(-(width/2),  height,    0)),
-            new Geom.M(new Geom.Vec( (width/2),  height,    0)),
-            new Geom.M(new Geom.Vec(-(width/2), -height,    0)),
-            new Geom.M(new Geom.Vec( (width/2), -height,    0)),
-            new Geom.M(new Geom.Vec(-(width/2),       0,  depth)),
-            new Geom.M(new Geom.Vec( (width/2),       0,  depth)),
-            new Geom.M(new Geom.Vec(-(width/2),       0, -depth)),
-            new Geom.M(new Geom.Vec( (width/2),       0, -depth)),
-
-            new Geom.M(new Geom.Vec( width,           0,    0)),
-            new Geom.M(new Geom.Vec(-width,           0,    0)),
+        translations = new Matrix[] {
+
+            new Matrix(new Vec(-(width/2),  height,    0)),
+            new Matrix(new Vec( (width/2),  height,    0)),
+            new Matrix(new Vec(-(width/2), -height,    0)),
+            new Matrix(new Vec( (width/2), -height,    0)),
+            new Matrix(new Vec(-(width/2),       0,  depth)),
+            new Matrix(new Vec( (width/2),       0,  depth)),
+            new Matrix(new Vec(-(width/2),       0, -depth)),
+            new Matrix(new Vec( (width/2),       0, -depth)),
+
+            new Matrix(new Vec( width,           0,    0)),
+            new Matrix(new Vec(-width,           0,    0)),
             /*
-            new Geom.M(new Geom.Vec(     0,           0,    depth)),
-            new Geom.M(new Geom.Vec(     0,           0,   -depth)),
+            new Matrix(new Vec(     0,           0,    depth)),
+            new Matrix(new Vec(     0,           0,   -depth)),
             */
         };
 
 
-        Geom.V ltf = tile.register(new Geom.P(-(width/2),  (height/2),  (depth/2)));
-        Geom.V mtf = tile.register(new Geom.P( 0.0,        (height/2),  (depth/2)));
-        Geom.V rtf = tile.register(new Geom.P( (width/2),  (height/2),  (depth/2)));
-        Geom.V ltn = tile.register(new Geom.P(-(width/2),  (height/2), -(depth/2)));
-        Geom.V mtn = tile.register(new Geom.P( 0.0,        (height/2), -(depth/2)));
-        Geom.V rtn = tile.register(new Geom.P( (width/2),  (height/2), -(depth/2)));
-        Geom.V lbf = tile.register(new Geom.P(-(width/2), -(height/2),  (depth/2)));
-        Geom.V mbf = tile.register(new Geom.P( 0.0,       -(height/2),  (depth/2)));
-        Geom.V rbf = tile.register(new Geom.P( (width/2), -(height/2),  (depth/2)));
-        Geom.V lbn = tile.register(new Geom.P(-(width/2), -(height/2), -(depth/2)));
-        Geom.V mbn = tile.register(new Geom.P( 0.0,       -(height/2), -(depth/2)));
-        Geom.V rbn = tile.register(new Geom.P( (width/2), -(height/2), -(depth/2)));
+        Mesh.Vert ltf = tile.register(new Point(-(width/2),  (height/2),  (depth/2)));
+        Mesh.Vert mtf = tile.register(new Point( 0.0,        (height/2),  (depth/2)));
+        Mesh.Vert rtf = tile.register(new Point( (width/2),  (height/2),  (depth/2)));
+        Mesh.Vert ltn = tile.register(new Point(-(width/2),  (height/2), -(depth/2)));
+        Mesh.Vert mtn = tile.register(new Point( 0.0,        (height/2), -(depth/2)));
+        Mesh.Vert rtn = tile.register(new Point( (width/2),  (height/2), -(depth/2)));
+        Mesh.Vert lbf = tile.register(new Point(-(width/2), -(height/2),  (depth/2)));
+        Mesh.Vert mbf = tile.register(new Point( 0.0,       -(height/2),  (depth/2)));
+        Mesh.Vert rbf = tile.register(new Point( (width/2), -(height/2),  (depth/2)));
+        Mesh.Vert lbn = tile.register(new Point(-(width/2), -(height/2), -(depth/2)));
+        Mesh.Vert mbn = tile.register(new Point( 0.0,       -(height/2), -(depth/2)));
+        Mesh.Vert rbn = tile.register(new Point( (width/2), -(height/2), -(depth/2)));
         
-        points = new Geom.V[] {
+        points = new Mesh.Vert[] {
             ltf,
             mtf,
             rtf,
@@ -146,59 +148,59 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
 
 
         // top
-        tile.newT(ltf, mtf, mtn);
-        tile.newT(mtn, ltn, ltf);
-        tile.newT(mtf, rtf, rtn);
-        tile.newT(rtn, mtn, mtf);
+        tile.newT(ltf, mtf, mtn, null);
+        tile.newT(mtn, ltn, ltf, null);
+        tile.newT(mtf, rtf, rtn, null);
+        tile.newT(rtn, mtn, mtf, null);
 
         // bottom (swap normals)
-        tile.newT(mbf, lbf, mbn);
-        tile.newT(lbn, mbn, lbf);
-        tile.newT(rbf, mbf, rbn);
-        tile.newT(mbn, rbn, mbf);
+        tile.newT(mbf, lbf, mbn, null);
+        tile.newT(lbn, mbn, lbf, null);
+        tile.newT(rbf, mbf, rbn, null);
+        tile.newT(mbn, rbn, mbf, null);
         
         // left
-        tile.newT(ltf, ltn, lbn);
-        tile.newT(lbn, lbf, ltf);
+        tile.newT(ltf, ltn, lbn, null);
+        tile.newT(lbn, lbf, ltf, null);
 
         // right (swap normals)
-        tile.newT(rtn, rtf, rbn);
-        tile.newT(rbf, rbn, rtf);
+        tile.newT(rtn, rtf, rbn, null);
+        tile.newT(rbf, rbn, rtf, null);
 
         // front
-        tile.newT(ltn, mtn, mbn);
-        tile.newT(ltn, mbn, lbn);
-        tile.newT(mtn, rtn, rbn);
-        tile.newT(mtn, rbn, mbn);
+        tile.newT(ltn, mtn, mbn, null);
+        tile.newT(ltn, mbn, lbn, null);
+        tile.newT(mtn, rtn, rbn, null);
+        tile.newT(mtn, rbn, mbn, null);
 
         // back
-        tile.newT(mtf, ltf, mbf);
-        tile.newT(mbf, ltf, lbf);
-        tile.newT(rtf, mtf, rbf);
-        tile.newT(rbf, mtf, mbf);
-
-        for(Geom.M m : translations) {
-            for(Geom.T t1 : tile) {
-                for(Geom.T t2 : tile) {
+        tile.newT(mtf, ltf, mbf, null);
+        tile.newT(mbf, ltf, lbf, null);
+        tile.newT(rtf, mtf, rbf, null);
+        tile.newT(rbf, mtf, mbf, null);
+
+        for(Matrix m : translations) {
+            for(Mesh.T t1 : tile) {
+                for(Mesh.T t2 : tile) {
                     if (t1==t2) continue;
 
-                    if ((t1.p1().p.times(m).minus(t2.p1().p).mag() < Geom.EPSILON) &&
-                        (t1.p2().p.times(m).minus(t2.p3().p).mag() < Geom.EPSILON) &&
-                        (t1.p3().p.times(m).minus(t2.p2().p).mag() < Geom.EPSILON)) {
+                    if ((t1.v1().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
+                        (t1.v2().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
+                        (t1.v3().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
                         t1.e1().bind(t2.e3().pair);
                         t1.e2().bind(t2.e2().pair);
                         t1.e3().bind(t2.e1().pair);
                     }
-                    if ((t1.p2().p.times(m).minus(t2.p1().p).mag() < Geom.EPSILON) &&
-                        (t1.p3().p.times(m).minus(t2.p3().p).mag() < Geom.EPSILON) &&
-                        (t1.p1().p.times(m).minus(t2.p2().p).mag() < Geom.EPSILON)) {
+                    if ((t1.v2().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
+                        (t1.v3().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
+                        (t1.v1().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
                         t1.e2().bind(t2.e3().pair);
                         t1.e3().bind(t2.e2().pair);
                         t1.e1().bind(t2.e1().pair);
                     }
-                    if ((t1.p3().p.times(m).minus(t2.p1().p).mag() < Geom.EPSILON) &&
-                        (t1.p1().p.times(m).minus(t2.p3().p).mag() < Geom.EPSILON) &&
-                        (t1.p2().p.times(m).minus(t2.p2().p).mag() < Geom.EPSILON)) {
+                    if ((t1.v3().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
+                        (t1.v1().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
+                        (t1.v2().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
                         t1.e3().bind(t2.e3().pair);
                         t1.e1().bind(t2.e2().pair);
                         t1.e2().bind(t2.e1().pair);
@@ -207,14 +209,14 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
             }
         }
 
-        //xGeom.V mid = lbf.getE(mbn).shatter();
+        //xMesh.Vert mid = lbf.getE(mbn).shatter();
 
         // rescale to match volume
         float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
-        goal.transform(new Geom.M(factor));
+        goal.transform(new Matrix(factor));
 
         // translate to match centroid
-        goal.transform(new Geom.M(tile.centroid().minus(goal.centroid())));
+        goal.transform(new Matrix(tile.centroid().minus(goal.centroid())));
 
         //tx.e2.shatter();
         //tx.e3.shatter();
@@ -222,10 +224,10 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
 
         tile.bind();
 
-        //mid.move(new Geom.Vec((float)0,0,(float)-0.05));
-        //ltn.move(new Geom.Vec((float)0,0,(float)-0.05));
+        //mid.move(new Vec((float)0,0,(float)-0.05));
+        //ltn.move(new Vec((float)0,0,(float)-0.05));
 
-        //mtf.move(new Geom.Vec(0, (float)-0.05, (float)0.05));
+        //mtf.move(new Vec(0, (float)-0.05, (float)0.05));
 
 
         System.out.println("tile volume: " + tile.volume());
@@ -240,10 +242,10 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         if (verts > 300) return;
         //double min = (tile.avgedge/tile.numedges)*(1+(4/(double)verts));
         //if (verts>0 && tile.es.peek().length() < min) return;
-        PriorityQueue<Geom.E> es = new PriorityQueue<Geom.E>();
-        for(Geom.E e : tile.es) es.add(e);
+        PriorityQueue<Mesh.E> es = new PriorityQueue<Mesh.E>();
+        for(Mesh.E e : tile.es) es.add(e);
         for(int i=0; i<10; i++) {
-            Geom.E e = es.poll();
+            Mesh.E e = es.poll();
             verts++;
             System.out.println("shatter " + e);
             e.shatter();
@@ -252,7 +254,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         }
     }
 
-    public synchronized void rand(double temperature, Geom.V p) {
+    public synchronized void rand(double temperature, Mesh.Vert p) {
         double tile_score = tile.score();
         double goal_score = goal.score();
         
@@ -262,18 +264,18 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         r1 = r1 - (float)Math.floor(r1);
         r1 = r1 * (float)0.01;
         r1 = r1 - (float)0.005;
-        Geom.Vec v = p.watchback().p.minus(p.p).norm().times(r1);
+        Vec v = p.watchback().p.minus(p.p).norm().times(r1);
 
         //v = p.norm().times(v.dot(p.norm()));
 
         boolean aspect = false;//(Math.abs(random.nextInt()) % 100) <= 2;
-        Geom.M old_tile_aspect = null;//goal.aspect;
+        Matrix old_tile_aspect = null;//goal.aspect;
         boolean good = true;
         if (aspect) {
             /*
             v = v.times(10);
-            tile.aspect = new Geom.M(tile.aspect.a / (v.x+1), tile.aspect.f / (v.y+1), tile.aspect.k / (v.z+1));
-            tile.invaspect = new Geom.M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
+            tile.aspect = new Matrix(tile.aspect.a / (v.x+1), tile.aspect.f / (v.y+1), tile.aspect.k / (v.z+1));
+            tile.invaspect = new Matrix(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
             goal.rescore();
             tile.rescore();
             */
@@ -296,7 +298,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         } else {
             if (aspect) {
                 //tile.aspect = old_tile_aspect;
-                //tile.invaspect = new Geom.M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
+                //tile.invaspect = new Matrix(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
                 goal.rescore();
                 tile.rescore();
             } else {
@@ -348,12 +350,12 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         int i = 0;
         //gl.glDisable(GL.GL_DEPTH_TEST);
         gl.glColor4f(1,1,1,1);
-        for(Geom.M m : translations) {
+        for(Matrix m : translations) {
             //if (v1.z==0 && v1.y==0) continue;
             i++;
             if (i != 1 /*&& i!=4*/) continue;
-            Geom.P p = new Geom.P(0, 0, 0).times(m);
-            Geom.Vec v = new Geom.Vec(p.x, p.y, p.z);
+            Point p = new Point(0, 0, 0).times(m);
+            Vec v = new Vec(p.x, p.y, p.z);
             v = v.times((float)1.04);
             gl.glTranslatef(v.x, v.y, v.z);
             draw(gl, false, tile);
@@ -362,11 +364,11 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
         //gl.glEnable(GL.GL_DEPTH_TEST);
     }
 
-    private synchronized void draw(GL gl, boolean triangles, Geom mesh) {
+    private synchronized void draw(GL gl, boolean triangles, Mesh mesh) {
         float red = 0.0f;
         float green = 0.0f;
         float blue = 0.0f;
-        for(Geom.T t : mesh) {
+        for(Mesh.T t : mesh) {
             if (red < 0.15) red = 1.0f;
             if (green < 0.15) green = 1.0f;
             if (blue < 0.15) blue = 1.0f;
@@ -397,7 +399,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
                 gl.glEnd();
             }
 
-            Geom.P centroid = t.centroid();
+            Point centroid = t.centroid();
             gl.glBegin(GL.GL_LINES);
             gl.glColor3f(1, 1, 1);
             /*
@@ -406,7 +408,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
             */
 
             if (mesh==goal)
-                for(Geom.V p : new Geom.V[] { t.p1(), t.p2(), t.p3() }) {
+                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);
@@ -447,8 +449,8 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener
             for(int i=0; i<1; i++) {
                 glcanvas.repaint();
                 //tile.ts.get(Math.abs(random.nextInt()) % tile.ts.size()).e1().p1
-                for(Geom.T t : tile)
-                    for(Geom.V p : new Geom.V[] { t.p1(), t.p2(), t.p3() }) {
+                for(Mesh.T t : tile)
+                    for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
                         rand(10,p);
                     }
                 goal.rescore();