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: recenter goal to have centroid coincident with tile
12 // FIXME: re-orient goal (how?)
13 // fish case: ensure that spinal axis of fish is the x-axis of the tile
15 public class Main implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
18 boolean shift = false;
19 boolean control = false;
21 public void mouseWheelMoved(MouseWheelEvent e) {
22 tz -= e.getWheelRotation();
25 public void keyTyped(KeyEvent e) { }
26 public void keyPressed(KeyEvent e) {
27 switch(e.getKeyCode()) {
28 case KeyEvent.VK_CONTROL: control = true; break;
29 case KeyEvent.VK_ALT: alt = true; break;
30 case KeyEvent.VK_SHIFT: shift = true; break;
33 public void keyReleased(KeyEvent e) {
34 switch(e.getKeyCode()) {
35 case KeyEvent.VK_CONTROL: control = false; break;
36 case KeyEvent.VK_ALT: alt = false; break;
37 case KeyEvent.VK_SHIFT: shift = false; break;
41 public void mouseClicked(MouseEvent e) { }
42 public void mouseEntered(MouseEvent e) { }
43 public void mouseExited(MouseEvent e) { }
44 public void mousePressed(MouseEvent e) { }
45 public void mouseReleased(MouseEvent e) { }
49 public void mouseMoved(MouseEvent e) {
59 public void mouseDragged(MouseEvent e) {
61 tx += (mousex - e.getX())/(float)20;
62 ty += (mousey - e.getY())/(float)20;
64 anglex -= mousex - e.getX();
65 angley += mousey - e.getY();
71 private Mesh tile = new Mesh();
72 private Mesh goal = new Mesh();
74 /** magnification factor */
75 private static final float MAG = 1;
77 Matrix[] translations;
80 public Main(StlFile stlf) {
82 for(int i=0; i<stlf.coordArray.length; i+=3) {
83 Point p0 = new Point(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
84 Point p1 = new Point(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
85 Point p2 = new Point(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
86 Vec n = new Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
87 Mesh.T t = goal.newT(p0, p1, p2, n);
90 // rotate to align major axis -- this probably needs to be done by a human.
91 goal.transform(new Matrix(new Vec(0, 0, 1), (float)(Math.PI/2)));
94 float goal_width = goal.diagonal().dot(new Vec(1, 0, 0));
95 float goal_height = goal.diagonal().dot(new Vec(0, 1, 0));
96 float goal_depth = goal.diagonal().dot(new Vec(0, 0, 1));
98 float width = (float)0.6;
99 float height = (float)0.08;
100 float depth = (float)0.3;
101 translations = new Matrix[] {
103 new Matrix(new Vec(-(width/2), height, 0)),
104 new Matrix(new Vec( (width/2), height, 0)),
105 new Matrix(new Vec(-(width/2), -height, 0)),
106 new Matrix(new Vec( (width/2), -height, 0)),
107 new Matrix(new Vec(-(width/2), 0, depth)),
108 new Matrix(new Vec( (width/2), 0, depth)),
109 new Matrix(new Vec(-(width/2), 0, -depth)),
110 new Matrix(new Vec( (width/2), 0, -depth)),
112 new Matrix(new Vec( width, 0, 0)),
113 new Matrix(new Vec(-width, 0, 0)),
115 new Matrix(new Vec( 0, 0, depth)),
116 new Matrix(new Vec( 0, 0, -depth)),
121 Point ltf = new Point(-(width/2), (height/2), (depth/2));
122 Point mtf = new Point( 0.0, (height/2), (depth/2));
123 Point rtf = new Point( (width/2), (height/2), (depth/2));
124 Point ltn = new Point(-(width/2), (height/2), -(depth/2));
125 Point mtn = new Point( 0.0, (height/2), -(depth/2));
126 Point rtn = new Point( (width/2), (height/2), -(depth/2));
127 Point lbf = new Point(-(width/2), -(height/2), (depth/2));
128 Point mbf = new Point( 0.0, -(height/2), (depth/2));
129 Point rbf = new Point( (width/2), -(height/2), (depth/2));
130 Point lbn = new Point(-(width/2), -(height/2), -(depth/2));
131 Point mbn = new Point( 0.0, -(height/2), -(depth/2));
132 Point rbn = new Point( (width/2), -(height/2), -(depth/2));
134 Point[] points = new Point[] {
151 tile.newT(ltf, mtf, mtn, null);
152 tile.newT(mtn, ltn, ltf, null);
153 tile.newT(mtf, rtf, rtn, null);
154 tile.newT(rtn, mtn, mtf, null);
156 // bottom (swap normals)
157 tile.newT(mbf, lbf, mbn, null);
158 tile.newT(lbn, mbn, lbf, null);
159 tile.newT(rbf, mbf, rbn, null);
160 tile.newT(mbn, rbn, mbf, null);
163 tile.newT(ltf, ltn, lbn, null);
164 tile.newT(lbn, lbf, ltf, null);
166 // right (swap normals)
167 tile.newT(rtn, rtf, rbn, null);
168 tile.newT(rbf, rbn, rtf, null);
171 tile.newT(ltn, mtn, mbn, null);
172 tile.newT(ltn, mbn, lbn, null);
173 tile.newT(mtn, rtn, rbn, null);
174 tile.newT(mtn, rbn, mbn, null);
177 tile.newT(mtf, ltf, mbf, null);
178 tile.newT(mbf, ltf, lbf, null);
179 tile.newT(rtf, mtf, rbf, null);
180 tile.newT(rbf, mtf, mbf, null);
182 for(Matrix m : translations) {
183 for(Mesh.T t1 : tile) {
184 for(Mesh.T t2 : tile) {
185 if (t1==t2) continue;
187 if ((t1.v1().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
188 (t1.v2().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
189 (t1.v3().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
190 t1.e1().bind(t2.e3().pair);
191 t1.e2().bind(t2.e2().pair);
192 t1.e3().bind(t2.e1().pair);
194 if ((t1.v2().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
195 (t1.v3().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
196 (t1.v1().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
197 t1.e2().bind(t2.e3().pair);
198 t1.e3().bind(t2.e2().pair);
199 t1.e1().bind(t2.e1().pair);
201 if ((t1.v3().p.times(m).minus(t2.v1().p).mag() < Mesh.EPSILON) &&
202 (t1.v1().p.times(m).minus(t2.v3().p).mag() < Mesh.EPSILON) &&
203 (t1.v2().p.times(m).minus(t2.v2().p).mag() < Mesh.EPSILON)) {
204 t1.e3().bind(t2.e3().pair);
205 t1.e1().bind(t2.e2().pair);
206 t1.e2().bind(t2.e1().pair);
212 //xMesh.Vert mid = lbf.getE(mbn).shatter();
214 // rescale to match volume
215 float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
216 goal.transform(new Matrix(factor));
218 // translate to match centroid
219 goal.transform(new Matrix(tile.centroid().minus(goal.centroid())));
227 //mid.move(new Vec((float)0,0,(float)-0.05));
228 //ltn.move(new Vec((float)0,0,(float)-0.05));
230 //mtf.move(new Vec(0, (float)-0.05, (float)0.05));
233 System.out.println("tile volume: " + tile.volume());
234 System.out.println("goal volume: " + goal.volume());
236 tile.score_against = goal;
237 goal.score_against = tile;
239 Random random = new Random();
241 public synchronized void breakit() {
242 if (verts > 400) return;
243 //double min = (tile.avgedge/tile.numedges)*(1+(4/(double)verts));
244 //if (verts>0 && tile.es.peek().length() < min) return;
245 PriorityQueue<Mesh.E> es = new PriorityQueue<Mesh.E>();
246 for(Mesh.E e : tile.edges()) es.add(e);
247 for(int i=0; i<10; i++) {
248 Mesh.E e = es.poll();
250 System.out.println("shatter " + e);
257 public synchronized void rand(double temperature, Mesh.Vert p) {
258 double tile_score = tile.score();
259 double goal_score = goal.score();
263 //if (p.watch==null) return;
264 float r1 = Math.abs(random.nextFloat());
265 r1 = r1 - (float)Math.floor(r1);
266 r1 = r1 * (float)0.01;
267 r1 = r1 - (float)0.005;
268 public Vert partner() { return quadric==null ? this : quadric; }
269 public Point quadric() { return quadric_count==0 ? partner().p :
270 new Point(quadric_x/quadric_count, quadric_y/quadric_count, quadric_z/quadric_count); }
272 Vec v = p.nearest_vert_in_other_mesh().minus(p.p).norm().times(r1);
274 //v = p.norm().times(v.dot(p.norm()));
276 Vec v = new Vec((random.nextFloat() - (float)0.5) / 1000,
277 (random.nextFloat() - (float)0.5) / 1000,
278 (random.nextFloat() - (float)0.5) / 1000);
280 Matrix inv = p.errorQuadric();
281 Vec v = new Vec(inv.d, inv.h, inv.l).norm().times(1/(float)1000);
283 boolean aspect = false;//(Math.abs(random.nextInt()) % 100) <= 2;
284 Matrix old_tile_aspect = null;//goal.aspect;
289 tile.aspect = new Matrix(tile.aspect.a / (v.x+1), tile.aspect.f / (v.y+1), tile.aspect.k / (v.z+1));
290 tile.invaspect = new Matrix(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
297 double new_tile_score = tile.score();
298 double new_goal_score = goal.score();
299 double tile_delta = new_tile_score - tile_score;
300 double goal_delta = 0;//new_goal_score - goal_score;
301 double delta = tile_delta + goal_delta;
302 double swapProbability = Math.exp((-1 * delta) / temperature);
303 //boolean doSwap = Math.random() < swapProbability;
304 boolean doSwap = good && (tile_delta <= 0 && goal_delta <= 0);
305 //boolean doSwap = true;
306 //System.out.println(doSwap);
308 tile_score = new_tile_score;
309 goal_score = new_goal_score;
310 System.out.println("score: " + tile_score + " / " + goal_score);
311 if (aspect) System.out.println("aspect " + v);
314 //tile.aspect = old_tile_aspect;
315 //tile.invaspect = new Matrix(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
325 * Take care of initialization here.
327 public void init(GLAutoDrawable gld) {
329 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
330 gl.glViewport(0, 0, 500, 300);
331 gl.glEnable(GL.GL_DEPTH_TEST);
332 gl.glClearDepth(1.0);
333 gl.glDepthFunc(GL.GL_LEQUAL);
334 gl.glMatrixMode(GL.GL_PROJECTION);
336 gl.glMatrixMode(GL.GL_MODELVIEW);
340 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
341 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
342 public synchronized void display(GLAutoDrawable drawable) {
343 GL gl = drawable.getGL();
345 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
346 gl.glPointSize(5.0f);
348 glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
349 glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
350 gl.glTranslatef(tx/(float)20, ty/(float)20, 0);
351 gl.glRotatef(anglex/3, 0, 1, 0);
352 gl.glRotatef(angley/3, 1, 0, 0);
354 gl.glBegin(GL.GL_TRIANGLES);
355 draw(gl, true, tile);
358 gl.glBegin(GL.GL_TRIANGLES);
359 gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
360 //draw(gl, false, goal);
365 //gl.glDisable(GL.GL_DEPTH_TEST);
366 gl.glColor4f(1,1,1,1);
367 for(Matrix m : translations) {
368 //if (v1.z==0 && v1.y==0) continue;
370 if (i != 1 /*&& i!=4*/) continue;
371 Point p = new Point(0, 0, 0).times(m);
372 Vec v = new Vec(p.x, p.y, p.z);
373 v = v.times((float)1.04);
374 gl.glTranslatef(v.x, v.y, v.z);
375 draw(gl, false, tile);
376 gl.glTranslatef(-v.x, -v.y, -v.z);
378 //gl.glEnable(GL.GL_DEPTH_TEST);
381 private synchronized void draw(GL gl, boolean triangles, Mesh mesh) {
385 for(Mesh.T t : mesh) {
386 if (red < 0.15) red = 1.0f;
387 if (green < 0.15) green = 1.0f;
388 if (blue < 0.15) blue = 1.0f;
393 if (triangles) switch(t.color) {
394 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
395 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
396 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
397 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
399 //gl.glBegin(GL.GL_LINES);
402 gl.glBegin(GL.GL_TRIANGLES);
406 gl.glBegin(GL.GL_LINES);
407 t.e1().p1.p.glVertex(gl);
408 t.e1().p2.p.glVertex(gl);
409 t.e2().p1.p.glVertex(gl);
410 t.e2().p2.p.glVertex(gl);
411 t.e3().p1.p.glVertex(gl);
412 t.e3().p2.p.glVertex(gl);
416 Point centroid = t.centroid();
417 gl.glBegin(GL.GL_LINES);
418 gl.glColor3f(1, 1, 1);
420 centroid.glVertex(gl);
421 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
425 for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {
427 //p.plus(p.norm().times(p.score()*10)).glVertex(gl);
428 //p.partner().p.glVertex(gl);
429 //tile.nearest(p).centroid().glVertex(gl);
437 public static void main(String[] s) throws Exception {
438 StlFile stlf = new StlFile();
439 stlf.load("simplefish.stl");
440 Main main = new Main(stlf);
441 Frame f = new Frame();
442 GLCapabilities glcaps = new GLCapabilities();
443 GLCanvas glcanvas = new GLCanvas();
444 glcanvas.addGLEventListener(main);
445 f.add(glcanvas, BorderLayout.CENTER);
451 glcanvas.addMouseListener(main);
452 glcanvas.addMouseMotionListener(main);
453 glcanvas.addMouseWheelListener(main);
454 glcanvas.addKeyListener(main);
456 main.anneal(glcanvas);
458 public static int verts = 0;
459 public void anneal(GLCanvas glcanvas) throws Exception {
463 for(int i=0; i<1; i++) {
465 //tile.ts.get(Math.abs(random.nextInt()) % tile.ts.size()).e1().p1
467 for(Mesh.Vert p : new Mesh.Vert[] { t.v1(), t.v2(), t.v3() }) {