f0671584f6ddd2a1d653bf5be3a3b639cfd31c7b
[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         float goal_width  = goal.diagonal().dot(new Vec(1, 0, 0));
35         float goal_height = goal.diagonal().dot(new Vec(0, 1, 0));
36         float goal_depth  = goal.diagonal().dot(new Vec(0, 0, 1));
37
38         float width  = (float)0.6;
39         float height = (float)0.08;
40         float depth  = (float)0.3;
41         translations = new Matrix[] {
42
43             new Matrix(new Vec(-(width/2),  height,    0)),
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),       0,  depth)),
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
52             new Matrix(new Vec( width,           0,    0)),
53             new Matrix(new Vec(-width,           0,    0)),
54             /*
55             new Matrix(new Vec(     0,           0,    depth)),
56             new Matrix(new Vec(     0,           0,   -depth)),
57             */
58         };
59
60
61         Point ltf = new Point(-(width/2),  (height/2),  (depth/2));
62         Point mtf = new Point( 0.0,        (height/2),  (depth/2));
63         Point rtf = new Point( (width/2),  (height/2),  (depth/2));
64         Point ltn = new Point(-(width/2),  (height/2), -(depth/2));
65         Point mtn = new Point( 0.0,        (height/2), -(depth/2));
66         Point rtn = new Point( (width/2),  (height/2), -(depth/2));
67         Point lbf = new Point(-(width/2), -(height/2),  (depth/2));
68         Point mbf = new Point( 0.0,       -(height/2),  (depth/2));
69         Point rbf = new Point( (width/2), -(height/2),  (depth/2));
70         Point lbn = new Point(-(width/2), -(height/2), -(depth/2));
71         Point mbn = new Point( 0.0,       -(height/2), -(depth/2));
72         Point rbn = new Point( (width/2), -(height/2), -(depth/2));
73         
74         Point[] points = new Point[] {
75             ltf,
76             mtf,
77             rtf,
78             ltn,
79             mtn,
80             rtn,
81             lbf,
82             mbf,
83             rbf,
84             lbn,
85             mbn,
86             rbn
87         };
88
89
90         // top
91         tile.newT(ltf, mtf, mtn, null);
92         tile.newT(mtn, ltn, ltf, null);
93         tile.newT(mtf, rtf, rtn, null);
94         tile.newT(rtn, mtn, mtf, null);
95
96         // bottom (swap normals)
97         tile.newT(mbf, lbf, mbn, null);
98         tile.newT(lbn, mbn, lbf, null);
99         tile.newT(rbf, mbf, rbn, null);
100         tile.newT(mbn, rbn, mbf, null);
101         
102         // left
103         tile.newT(ltf, ltn, lbn, null);
104         tile.newT(lbn, lbf, ltf, null);
105
106         // right (swap normals)
107         tile.newT(rtn, rtf, rbn, null);
108         tile.newT(rbf, rbn, rtf, null);
109
110         // front
111         tile.newT(ltn, mtn, mbn, null);
112         tile.newT(ltn, mbn, lbn, null);
113         tile.newT(mtn, rtn, rbn, null);
114         tile.newT(mtn, rbn, mbn, null);
115
116         // back
117         tile.newT(mtf, ltf, mbf, null);
118         tile.newT(mbf, ltf, lbf, null);
119         tile.newT(rtf, mtf, rbf, null);
120         tile.newT(rbf, mtf, mbf, null);
121
122         for(Matrix m : translations) {
123             for(Mesh.T t1 : tile) {
124                 for(Mesh.T t2 : tile) {
125                     if (t1==t2) continue;
126
127                     if ((t1.v1().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
128                         (t1.v2().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
129                         (t1.v3().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
130                         t1.e1().bind(t2.e3().pair);
131                         t1.e2().bind(t2.e2().pair);
132                         t1.e3().bind(t2.e1().pair);
133                     }
134                     if ((t1.v2().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
135                         (t1.v3().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
136                         (t1.v1().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
137                         t1.e2().bind(t2.e3().pair);
138                         t1.e3().bind(t2.e2().pair);
139                         t1.e1().bind(t2.e1().pair);
140                     }
141                     if ((t1.v3().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
142                         (t1.v1().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
143                         (t1.v2().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
144                         t1.e3().bind(t2.e3().pair);
145                         t1.e1().bind(t2.e2().pair);
146                         t1.e2().bind(t2.e1().pair);
147                     }
148                 }
149             }
150         }
151
152         //xMesh.Vert mid = lbf.getE(mbn).shatter();
153
154         // rescale to match volume
155         float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
156         goal.transform(new Matrix(factor));
157
158         // translate to match centroid
159         goal.transform(new Matrix(tile.centroid().minus(goal.centroid())));
160
161         //tx.e2.shatter();
162         //tx.e3.shatter();
163
164
165         tile.rebindPoints();
166
167         //mid.move(new Vec((float)0,0,(float)-0.05));
168         //ltn.move(new Vec((float)0,0,(float)-0.05));
169
170         //mtf.move(new Vec(0, (float)-0.05, (float)0.05));
171
172
173         System.out.println("tile volume: " + tile.volume());
174         System.out.println("goal volume: " + goal.volume());
175
176         tile.score_against = goal;
177         goal.score_against = tile;
178     }
179
180     public synchronized void breakit() {
181         if (verts > 50) return;
182         PriorityQueue<Mesh.E> es = new PriorityQueue<Mesh.E>();
183         for(Mesh.E e : tile.edges()) es.add(e);
184         for(int i=0; i<10; i++) {
185             Mesh.E e = es.poll();
186             verts++;
187             System.out.println("shatter " + e);
188             e.shatter();
189             tile.rebindPoints();
190         }
191     }
192
193     public synchronized void rand(double temperature, Mesh.Vert p) {
194         double tile_score = tile.score();
195         double goal_score = goal.score();
196         p.rescore();
197
198         Vec v = new Vec((random.nextFloat() - (float)0.5) / 1000,
199                         (random.nextFloat() - (float)0.5) / 1000,
200                         (random.nextFloat() - (float)0.5) / 1000);
201         //Matrix inv = p.errorQuadric();
202         //Vec v = new Vec(inv.d, inv.h, inv.l).norm().times(1/(float)1000);
203
204         boolean good = p.move(v);
205         double new_tile_score = tile.score();
206         double new_goal_score = goal.score();
207         double tile_delta = new_tile_score - tile_score;
208         double goal_delta = 0;//new_goal_score - goal_score;
209         double delta = tile_delta + goal_delta;
210         //double swapProbability = Math.exp((-1 * delta) / temperature);
211         //boolean doSwap = Math.random() < swapProbability;
212         boolean doSwap = good && (tile_delta <= 0 && goal_delta <= 0);
213
214         if (doSwap) {
215             tile_score = new_tile_score;
216             goal_score = new_goal_score;
217             //System.out.println("score: " + tile_score + " / " + goal_score);
218         } else {
219             p.move(v.times(-1));
220         }
221     }
222
223     public static int verts = 0;
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             breakit();
235             repaint();
236             goal.unscoreAll();
237             repaint();
238             tile.recomputeAllFundamentalQuadrics();
239             repaint();
240             goal.rescoreAll();
241        }
242     }
243
244     public static void main(String[] s) throws Exception {
245         StlFile stlf = new StlFile();
246         stlf.load("simplefish.stl");
247         Frame f = new Frame();
248         Main main = new Main(stlf, f);
249         main.anneal();
250     }
251 }