checkpoint
[anneal.git] / src / edu / berkeley / qfat / Main.java
1 package edu.berkeley.qfat;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
7 import java.util.*;
8 import edu.berkeley.qfat.geom.*;
9 import edu.berkeley.qfat.geom.Point;
10
11 // FIXME: re-orient goal (how?)
12
13 public class Main extends MeshViewer {
14
15     public static int verts = 0;
16
17     public static final Random random = new Random();
18     
19     /** magnification factor */
20     private static final float MAG = 1;
21
22     public Main(StlFile stlf, Frame f) {
23         super(f);
24
25         for(int i=0; i<stlf.coordArray.length; i+=3) {
26             Point p0 = new Point(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
27             Point p1 = new Point(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
28             Point p2 = new Point(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
29             Vec n  = new Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
30             Mesh.T t  = goal.newT(p0, p1, p2, n);
31         }
32
33         // rotate to align major axis -- this probably needs to be done by a human.
34         goal.transform(new Matrix(new Vec(0, 0, 1), (float)(Math.PI/2)));
35
36         float goal_width  = goal.diagonal().dot(new Vec(1, 0, 0));
37         float goal_height = goal.diagonal().dot(new Vec(0, 1, 0));
38         float goal_depth  = goal.diagonal().dot(new Vec(0, 0, 1));
39
40         float width  = (float)0.6;
41         float height = (float)0.08;
42         float depth  = (float)0.3;
43         translations = new Matrix[] {
44
45             new Matrix(new Vec(-(width/2),  height,    0)),
46             new Matrix(new Vec( (width/2),  height,    0)),
47             new Matrix(new Vec(-(width/2), -height,    0)),
48             new Matrix(new Vec( (width/2), -height,    0)),
49             new Matrix(new Vec(-(width/2),       0,  depth)),
50             new Matrix(new Vec( (width/2),       0,  depth)),
51             new Matrix(new Vec(-(width/2),       0, -depth)),
52             new Matrix(new Vec( (width/2),       0, -depth)),
53
54             new Matrix(new Vec( width,           0,    0)),
55             new Matrix(new Vec(-width,           0,    0)),
56             /*
57             new Matrix(new Vec(     0,           0,    depth)),
58             new Matrix(new Vec(     0,           0,   -depth)),
59             */
60         };
61
62
63         Point ltf = new Point(-(width/2),  (height/2),  (depth/2));
64         Point mtf = new Point( 0.0,        (height/2),  (depth/2));
65         Point rtf = new Point( (width/2),  (height/2),  (depth/2));
66         Point ltn = new Point(-(width/2),  (height/2), -(depth/2));
67         Point mtn = new Point( 0.0,        (height/2), -(depth/2));
68         Point rtn = new Point( (width/2),  (height/2), -(depth/2));
69         Point lbf = new Point(-(width/2), -(height/2),  (depth/2));
70         Point mbf = new Point( 0.0,       -(height/2),  (depth/2));
71         Point rbf = new Point( (width/2), -(height/2),  (depth/2));
72         Point lbn = new Point(-(width/2), -(height/2), -(depth/2));
73         Point mbn = new Point( 0.0,       -(height/2), -(depth/2));
74         Point rbn = new Point( (width/2), -(height/2), -(depth/2));
75         
76         Point[] points = new Point[] {
77             ltf,
78             mtf,
79             rtf,
80             ltn,
81             mtn,
82             rtn,
83             lbf,
84             mbf,
85             rbf,
86             lbn,
87             mbn,
88             rbn
89         };
90
91
92         // top
93         tile.newT(ltf, mtf, mtn, null);
94         tile.newT(mtn, ltn, ltf, null);
95         tile.newT(mtf, rtf, rtn, null);
96         tile.newT(rtn, mtn, mtf, null);
97
98         // bottom (swap normals)
99         tile.newT(mbf, lbf, mbn, null);
100         tile.newT(lbn, mbn, lbf, null);
101         tile.newT(rbf, mbf, rbn, null);
102         tile.newT(mbn, rbn, mbf, null);
103         
104         // left
105         tile.newT(ltf, ltn, lbn, null);
106         tile.newT(lbn, lbf, ltf, null);
107
108         // right (swap normals)
109         tile.newT(rtn, rtf, rbn, null);
110         tile.newT(rbf, rbn, rtf, null);
111
112         // front
113         tile.newT(ltn, mtn, mbn, null);
114         tile.newT(ltn, mbn, lbn, null);
115         tile.newT(mtn, rtn, rbn, null);
116         tile.newT(mtn, rbn, mbn, null);
117
118         // back
119         tile.newT(mtf, ltf, mbf, null);
120         tile.newT(mbf, ltf, lbf, null);
121         tile.newT(rtf, mtf, rbf, null);
122         tile.newT(rbf, mtf, mbf, null);
123
124         for(Matrix m : translations) {
125             for(Mesh.T t1 : tile) {
126                 for(Mesh.T t2 : tile) {
127                     if (t1==t2) continue;
128
129                     if ((t1.v1().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
130                         (t1.v2().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
131                         (t1.v3().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
132                         t1.e1().bindEdge(t2.e3());
133                         t1.e2().bindEdge(t2.e2());
134                         t1.e3().bindEdge(t2.e1());
135                     }
136                     if ((t1.v2().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
137                         (t1.v3().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
138                         (t1.v1().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
139                         t1.e2().bindEdge(t2.e3());
140                         t1.e3().bindEdge(t2.e2());
141                         t1.e1().bindEdge(t2.e1());
142                     }
143                     if ((t1.v3().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
144                         (t1.v1().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
145                         (t1.v2().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
146                         t1.e3().bindEdge(t2.e3());
147                         t1.e1().bindEdge(t2.e2());
148                         t1.e2().bindEdge(t2.e1());
149                     }
150                 }
151             }
152         }
153
154         //xMesh.Vert mid = lbf.getE(mbn).shatter();
155
156         // rescale to match volume
157         float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
158         goal.transform(new Matrix(factor));
159
160         // translate to match centroid
161         goal.transform(new Matrix(tile.centroid().minus(goal.centroid())));
162
163         //tx.e2.shatter();
164         //tx.e3.shatter();
165
166
167         tile.rebindPoints();
168
169         //mid.move(new Vec((float)0,0,(float)-0.05));
170         //ltn.move(new Vec((float)0,0,(float)-0.05));
171
172         //mtf.move(new Vec(0, (float)-0.05, (float)0.05));
173
174
175         System.out.println("tile volume: " + tile.volume());
176         System.out.println("goal volume: " + goal.volume());
177
178         tile.score_against = goal;
179         goal.score_against = tile;
180     }
181
182     public synchronized void breakit() {
183         if (verts > 300) return;
184         PriorityQueue<Mesh.E> es = new PriorityQueue<Mesh.E>();
185         for(Mesh.E e : tile.edges()) es.add(e);
186         for(int i=0; i<10; i++) {
187             Mesh.E e = es.poll();
188             verts++;
189             //System.out.println("shatter " + e);
190             e.shatter();
191             tile.rebindPoints();
192         }
193     }
194
195     public synchronized void rand(double temperature, Mesh.Vert p) {
196         double tile_score = tile.score();
197         double goal_score = goal.score();
198         p.reComputeError();
199
200         Vec v = new Vec((random.nextFloat() - (float)0.5) / 1000,
201                         (random.nextFloat() - (float)0.5) / 1000,
202                         (random.nextFloat() - (float)0.5) / 1000);
203         //Matrix inv = p.errorQuadric();
204         //Vec v = new Vec(inv.d, inv.h, inv.l).norm().times(1/(float)1000);
205
206         boolean good = p.move(v);
207         double new_tile_score = tile.score();
208         double new_goal_score = goal.score();
209         double tile_delta = new_tile_score - tile_score;
210         double goal_delta = 0;//new_goal_score - goal_score;
211         double delta = tile_delta + goal_delta;
212         //double swapProbability = Math.exp((-1 * delta) / temperature);
213         //boolean doSwap = Math.random() < swapProbability;
214         boolean doSwap = good && (tile_delta <= 0 && goal_delta <= 0);
215
216         if (doSwap) {
217             tile_score = new_tile_score;
218             goal_score = new_goal_score;
219             //System.out.println("score: " + tile_score + " / " + goal_score);
220         } else {
221             p.move(v.times(-1));
222         }
223     }
224
225     public void anneal() throws Exception {
226         int verts = 0;
227         while(true) {
228             HashSet<Mesh.Vert> hs = new HashSet<Mesh.Vert>();
229             for(Mesh.Vert p : tile.vertices()) hs.add(p);
230             for(int i=0; i<10; i++) {
231                 repaint();
232                 for(Mesh.Vert v : hs) rand(10,v);
233             }
234             tile.rebuildPointSet();
235             breakit();
236             repaint();
237             goal.unApplyQuadricToNeighborAll();
238             repaint();
239             tile.recomputeAllFundamentalQuadrics();
240             repaint();
241             goal.applyQuadricToNeighborAll();
242        }
243     }
244
245     public static void main(String[] s) throws Exception {
246         StlFile stlf = new StlFile();
247         stlf.load("simplefish.stl");
248         Frame f = new Frame();
249         Main main = new Main(stlf, f);
250         main.anneal();
251     }
252
253 }