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 final Random random = new Random();
16     
17     /** magnification factor */
18     private static final float MAG = 1;
19
20     public Main(StlFile stlf, Frame f) {
21         super(f);
22
23         for(int i=0; i<stlf.coordArray.length; i+=3) {
24             Point p0 = new Point(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
25             Point p1 = new Point(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
26             Point p2 = new Point(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
27             Vec n  = new Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
28             Mesh.T t  = goal.newT(p0, p1, p2, n);
29         }
30
31         // rotate to align major axis -- this probably needs to be done by a human.
32         goal.transform(new Matrix(new Vec(0, 0, 1), (float)(Math.PI/2)));
33
34
35         float goal_width  = goal.diagonal().dot(new Vec(1, 0, 0));
36         float goal_height = goal.diagonal().dot(new Vec(0, 1, 0));
37         float goal_depth  = goal.diagonal().dot(new Vec(0, 0, 1));
38
39         float width  = (float)0.6;
40         float height = (float)0.08;
41         float depth  = (float)0.3;
42         translations = new Matrix[] {
43
44             new Matrix(new Vec(-(width/2),  height,    0)),
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),       0,  depth)),
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
53             new Matrix(new Vec( width,           0,    0)),
54             new Matrix(new Vec(-width,           0,    0)),
55             /*
56             new Matrix(new Vec(     0,           0,    depth)),
57             new Matrix(new Vec(     0,           0,   -depth)),
58             */
59         };
60
61
62         Point ltf = new Point(-(width/2),  (height/2),  (depth/2));
63         Point mtf = new Point( 0.0,        (height/2),  (depth/2));
64         Point rtf = new Point( (width/2),  (height/2),  (depth/2));
65         Point ltn = new Point(-(width/2),  (height/2), -(depth/2));
66         Point mtn = new Point( 0.0,        (height/2), -(depth/2));
67         Point rtn = new Point( (width/2),  (height/2), -(depth/2));
68         Point lbf = new Point(-(width/2), -(height/2),  (depth/2));
69         Point mbf = new Point( 0.0,       -(height/2),  (depth/2));
70         Point rbf = new Point( (width/2), -(height/2),  (depth/2));
71         Point lbn = new Point(-(width/2), -(height/2), -(depth/2));
72         Point mbn = new Point( 0.0,       -(height/2), -(depth/2));
73         Point rbn = new Point( (width/2), -(height/2), -(depth/2));
74         
75         Point[] points = new Point[] {
76             ltf,
77             mtf,
78             rtf,
79             ltn,
80             mtn,
81             rtn,
82             lbf,
83             mbf,
84             rbf,
85             lbn,
86             mbn,
87             rbn
88         };
89
90
91         // top
92         tile.newT(ltf, mtf, mtn, null);
93         tile.newT(mtn, ltn, ltf, null);
94         tile.newT(mtf, rtf, rtn, null);
95         tile.newT(rtn, mtn, mtf, null);
96
97         // bottom (swap normals)
98         tile.newT(mbf, lbf, mbn, null);
99         tile.newT(lbn, mbn, lbf, null);
100         tile.newT(rbf, mbf, rbn, null);
101         tile.newT(mbn, rbn, mbf, null);
102         
103         // left
104         tile.newT(ltf, ltn, lbn, null);
105         tile.newT(lbn, lbf, ltf, null);
106
107         // right (swap normals)
108         tile.newT(rtn, rtf, rbn, null);
109         tile.newT(rbf, rbn, rtf, null);
110
111         // front
112         tile.newT(ltn, mtn, mbn, null);
113         tile.newT(ltn, mbn, lbn, null);
114         tile.newT(mtn, rtn, rbn, null);
115         tile.newT(mtn, rbn, mbn, null);
116
117         // back
118         tile.newT(mtf, ltf, mbf, null);
119         tile.newT(mbf, ltf, lbf, null);
120         tile.newT(rtf, mtf, rbf, null);
121         tile.newT(rbf, mtf, mbf, null);
122
123         for(Matrix m : translations) {
124             for(Mesh.T t1 : tile) {
125                 for(Mesh.T t2 : tile) {
126                     if (t1==t2) continue;
127
128                     if ((t1.v1().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
129                         (t1.v2().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
130                         (t1.v3().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
131                         t1.e1().bind(t2.e3().pair);
132                         t1.e2().bind(t2.e2().pair);
133                         t1.e3().bind(t2.e1().pair);
134                     }
135                     if ((t1.v2().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
136                         (t1.v3().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
137                         (t1.v1().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
138                         t1.e2().bind(t2.e3().pair);
139                         t1.e3().bind(t2.e2().pair);
140                         t1.e1().bind(t2.e1().pair);
141                     }
142                     if ((t1.v3().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
143                         (t1.v1().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
144                         (t1.v2().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
145                         t1.e3().bind(t2.e3().pair);
146                         t1.e1().bind(t2.e2().pair);
147                         t1.e2().bind(t2.e1().pair);
148                     }
149                 }
150             }
151         }
152
153         //xMesh.Vert mid = lbf.getE(mbn).shatter();
154
155         // rescale to match volume
156         float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
157         goal.transform(new Matrix(factor));
158
159         // translate to match centroid
160         goal.transform(new Matrix(tile.centroid().minus(goal.centroid())));
161
162         //tx.e2.shatter();
163         //tx.e3.shatter();
164
165
166         tile.bind();
167
168         //mid.move(new Vec((float)0,0,(float)-0.05));
169         //ltn.move(new Vec((float)0,0,(float)-0.05));
170
171         //mtf.move(new Vec(0, (float)-0.05, (float)0.05));
172
173
174         System.out.println("tile volume: " + tile.volume());
175         System.out.println("goal volume: " + goal.volume());
176
177         tile.score_against = goal;
178         goal.score_against = tile;
179     }
180
181     public synchronized void breakit() {
182         if (verts > 200) return;
183         PriorityQueue<Mesh.E> es = new PriorityQueue<Mesh.E>();
184         for(Mesh.E e : tile.edges()) es.add(e);
185         for(int i=0; i<10; i++) {
186             Mesh.E e = es.poll();
187             verts++;
188             System.out.println("shatter " + e);
189             e.shatter();
190             tile.unbind();
191             tile.bind();
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.rescore();
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
226     public static int verts = 0;
227     public void anneal() throws Exception {
228         int verts = 0;
229         while(true) {
230             //Thread.sleep(10);
231             for(int i=0; i<1; i++) {
232                 repaint();
233                 //tile.ts.get(Math.abs(random.nextInt()) % tile.ts.size()).e1().p1
234                 for(Mesh.T t : tile)
235                     for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
236                         rand(10,p);
237                     }
238                 goal.unscore();
239                 tile.unscore();
240                 goal.fundamental();
241                 tile.fundamental();
242                 goal.rescore();
243                 tile.rescore();
244             }
245             breakit();
246        }
247
248     }
249
250     public static void main(String[] s) throws Exception {
251         StlFile stlf = new StlFile();
252         stlf.load("simplefish.stl");
253         Frame f = new Frame();
254         Main main = new Main(stlf, f);
255         main.anneal();
256     }
257 }