1 package edu.berkeley.qfat;
3 import java.awt.event.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
8 import edu.berkeley.qfat.geom.*;
9 import edu.berkeley.qfat.geom.Point;
11 // FIXME: re-orient goal (how?)
13 public class Main extends MeshViewer {
15 public static final Random random = new Random();
17 /** magnification factor */
18 private static final float MAG = 1;
20 public Main(StlFile stlf, Frame f) {
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);
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)));
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));
38 float width = (float)0.6;
39 float height = (float)0.08;
40 float depth = (float)0.3;
41 translations = new Matrix[] {
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)),
52 new Matrix(new Vec( width, 0, 0)),
53 new Matrix(new Vec(-width, 0, 0)),
55 new Matrix(new Vec( 0, 0, depth)),
56 new Matrix(new Vec( 0, 0, -depth)),
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));
74 Point[] points = new Point[] {
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);
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);
103 tile.newT(ltf, ltn, lbn, null);
104 tile.newT(lbn, lbf, ltf, null);
106 // right (swap normals)
107 tile.newT(rtn, rtf, rbn, null);
108 tile.newT(rbf, rbn, rtf, null);
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);
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);
122 for(Matrix m : translations) {
123 for(Mesh.T t1 : tile) {
124 for(Mesh.T t2 : tile) {
125 if (t1==t2) continue;
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);
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);
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);
152 //xMesh.Vert mid = lbf.getE(mbn).shatter();
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));
158 // translate to match centroid
159 goal.transform(new Matrix(tile.centroid().minus(goal.centroid())));
167 //mid.move(new Vec((float)0,0,(float)-0.05));
168 //ltn.move(new Vec((float)0,0,(float)-0.05));
170 //mtf.move(new Vec(0, (float)-0.05, (float)0.05));
173 System.out.println("tile volume: " + tile.volume());
174 System.out.println("goal volume: " + goal.volume());
176 tile.score_against = goal;
177 goal.score_against = tile;
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();
187 System.out.println("shatter " + e);
193 public synchronized void rand(double temperature, Mesh.Vert p) {
194 double tile_score = tile.score();
195 double goal_score = goal.score();
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);
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);
215 tile_score = new_tile_score;
216 goal_score = new_goal_score;
217 //System.out.println("score: " + tile_score + " / " + goal_score);
223 public static int verts = 0;
225 public void anneal() throws Exception {
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++) {
232 for(Mesh.Vert v : hs) rand(10,v);
236 goal.unApplyQuadricToNeighborAll();
238 tile.recomputeAllFundamentalQuadrics();
240 goal.applyQuadricToNeighborAll();
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);