2 import java.awt.event.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
8 // FIXME: recenter goal to have centroid coincident with tile
9 // FIXME: re-orient goal (how?)
10 // fish case: ensure that spinal axis of fish is the x-axis of the tile
12 public class Main implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
15 boolean shift = false;
16 boolean control = false;
18 public void mouseWheelMoved(MouseWheelEvent e) {
19 tz -= e.getWheelRotation() ;
22 public void keyTyped(KeyEvent e) { }
23 public void keyPressed(KeyEvent e) {
24 switch(e.getKeyCode()) {
25 case KeyEvent.VK_CONTROL: control = true; break;
26 case KeyEvent.VK_ALT: alt = true; break;
27 case KeyEvent.VK_SHIFT: shift = true; break;
30 public void keyReleased(KeyEvent e) {
31 switch(e.getKeyCode()) {
32 case KeyEvent.VK_CONTROL: control = false; break;
33 case KeyEvent.VK_ALT: alt = false; break;
34 case KeyEvent.VK_SHIFT: shift = false; break;
38 public void mouseClicked(MouseEvent e) { }
39 public void mouseEntered(MouseEvent e) { }
40 public void mouseExited(MouseEvent e) { }
41 public void mousePressed(MouseEvent e) { }
42 public void mouseReleased(MouseEvent e) { }
46 public void mouseMoved(MouseEvent e) {
56 public void mouseDragged(MouseEvent e) {
58 tx += (mousex - e.getX())/(float)20;
59 ty += (mousey - e.getY())/(float)20;
61 anglex -= mousex - e.getX();
62 angley += mousey - e.getY();
68 private Geom tile = new Geom();
69 private Geom goal = new Geom();
71 /** magnification factor */
72 private static final float MAG = 1;
74 Geom.M[] translations;
77 public Main(StlFile stlf) {
79 for(int i=0; i<stlf.coordArray.length; i+=3) {
80 Geom.V p0 = goal.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG).register();
81 Geom.V p1 = goal.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG).register();
82 Geom.V p2 = goal.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG).register();
83 Geom.Vec n = goal.new Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
84 Geom.T t = goal.newT(p0, p1, p2, n);
87 // rotate to align major axis -- this probably needs to be done by a human.
88 goal.transform(goal.new M(goal.new Vec(0, 0, 1), (float)(Math.PI/2)));
91 float goal_width = goal.diagonal().dot(goal.new Vec(1, 0, 0));
92 float goal_height = goal.diagonal().dot(goal.new Vec(0, 1, 0));
93 float goal_depth = goal.diagonal().dot(goal.new Vec(0, 0, 1));
95 float width = (float)0.6;
96 float height = (float)0.08;
97 float depth = (float)0.3;
98 translations = new Geom.M[] {
100 tile.new M(tile.new Vec(-(width/2), height, 0)),
101 tile.new M(tile.new Vec( (width/2), height, 0)),
102 tile.new M(tile.new Vec(-(width/2), -height, 0)),
103 tile.new M(tile.new Vec( (width/2), -height, 0)),
104 tile.new M(tile.new Vec(-(width/2), 0, depth)),
105 tile.new M(tile.new Vec( (width/2), 0, depth)),
106 tile.new M(tile.new Vec(-(width/2), 0, -depth)),
107 tile.new M(tile.new Vec( (width/2), 0, -depth)),
109 tile.new M(tile.new Vec( width, 0, 0)),
110 tile.new M(tile.new Vec(-width, 0, 0)),
112 tile.new M(tile.new Vec( 0, 0, depth)),
113 tile.new M(tile.new Vec( 0, 0, -depth)),
118 Geom.V ltf = tile.newP(-(width/2), (height/2), (depth/2)).register();
119 Geom.V mtf = tile.newP( 0.0, (height/2), (depth/2)).register();
120 Geom.V rtf = tile.newP( (width/2), (height/2), (depth/2)).register();
121 Geom.V ltn = tile.newP(-(width/2), (height/2), -(depth/2)).register();
122 Geom.V mtn = tile.newP( 0.0, (height/2), -(depth/2)).register();
123 Geom.V rtn = tile.newP( (width/2), (height/2), -(depth/2)).register();
124 Geom.V lbf = tile.newP(-(width/2), -(height/2), (depth/2)).register();
125 Geom.V mbf = tile.newP( 0.0, -(height/2), (depth/2)).register();
126 Geom.V rbf = tile.newP( (width/2), -(height/2), (depth/2)).register();
127 Geom.V lbn = tile.newP(-(width/2), -(height/2), -(depth/2)).register();
128 Geom.V mbn = tile.newP( 0.0, -(height/2), -(depth/2)).register();
129 Geom.V rbn = tile.newP( (width/2), -(height/2), -(depth/2)).register();
131 points = new Geom.V[] {
148 tile.newT(ltf, mtf, mtn);
149 tile.newT(mtn, ltn, ltf);
150 tile.newT(mtf, rtf, rtn);
151 tile.newT(rtn, mtn, mtf);
153 // bottom (swap normals)
154 tile.newT(mbf, lbf, mbn);
155 tile.newT(lbn, mbn, lbf);
156 tile.newT(rbf, mbf, rbn);
157 tile.newT(mbn, rbn, mbf);
160 tile.newT(ltf, ltn, lbn);
161 tile.newT(lbn, lbf, ltf);
163 // right (swap normals)
164 tile.newT(rtn, rtf, rbn);
165 tile.newT(rbf, rbn, rtf);
168 tile.newT(ltn, mtn, mbn);
169 tile.newT(ltn, mbn, lbn);
170 tile.newT(mtn, rtn, rbn);
171 tile.newT(mtn, rbn, mbn);
174 tile.newT(mtf, ltf, mbf);
175 tile.newT(mbf, ltf, lbf);
176 tile.newT(rtf, mtf, rbf);
177 tile.newT(rbf, mtf, mbf);
179 for(Geom.M m : translations) {
180 for(Geom.T t1 : tile) {
181 for(Geom.T t2 : tile) {
182 if (t1==t2) continue;
184 if ((t1.p1().times(m).minus(t2.p1()).mag() < Geom.EPSILON) &&
185 (t1.p2().times(m).minus(t2.p3()).mag() < Geom.EPSILON) &&
186 (t1.p3().times(m).minus(t2.p2()).mag() < Geom.EPSILON)) {
187 t1.e1().bind(t2.e3().pair);
188 t1.e2().bind(t2.e2().pair);
189 t1.e3().bind(t2.e1().pair);
191 if ((t1.p2().times(m).minus(t2.p1()).mag() < Geom.EPSILON) &&
192 (t1.p3().times(m).minus(t2.p3()).mag() < Geom.EPSILON) &&
193 (t1.p1().times(m).minus(t2.p2()).mag() < Geom.EPSILON)) {
194 t1.e2().bind(t2.e3().pair);
195 t1.e3().bind(t2.e2().pair);
196 t1.e1().bind(t2.e1().pair);
198 if ((t1.p3().times(m).minus(t2.p1()).mag() < Geom.EPSILON) &&
199 (t1.p1().times(m).minus(t2.p3()).mag() < Geom.EPSILON) &&
200 (t1.p2().times(m).minus(t2.p2()).mag() < Geom.EPSILON)) {
201 t1.e3().bind(t2.e3().pair);
202 t1.e1().bind(t2.e2().pair);
203 t1.e2().bind(t2.e1().pair);
209 //xGeom.V mid = lbf.getE(mbn).shatter();
211 // rescale to match volume
212 float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
213 goal.transform(goal.new M(factor));
215 // translate to match centroid
216 goal.transform(goal.new M(tile.centroid().minus(goal.centroid())));
224 //mid.move(tile.new Vec((float)0,0,(float)-0.05));
225 //ltn.move(tile.new Vec((float)0,0,(float)-0.05));
227 //mtf.move(tile.new Vec(0, (float)-0.05, (float)0.05));
230 System.out.println("tile volume: " + tile.volume());
231 System.out.println("goal volume: " + goal.volume());
233 tile.score_against = goal;
234 goal.score_against = tile;
236 Random random = new Random();
238 public synchronized void breakit() {
239 if (verts > 300) return;
240 //double min = (tile.avgedge/tile.numedges)*(1+(4/(double)verts));
241 //if (verts>0 && tile.es.peek().length() < min) return;
242 PriorityQueue<Geom.E> es = new PriorityQueue<Geom.E>();
243 for(Geom.E e : tile.es) es.add(e);
244 for(int i=0; i<10; i++) {
245 Geom.E e = es.poll();
247 System.out.println("shatter " + e);
254 public synchronized void rand(double temperature, Geom.V p) {
255 double tile_score = tile.score();
256 double goal_score = goal.score();
259 //if (p.watch==null) return;
260 float r1 = Math.abs(random.nextFloat());
261 r1 = r1 - (float)Math.floor(r1);
262 r1 = r1 * (float)0.01;
263 r1 = r1 - (float)0.005;
264 Geom.Vec v = p.watchback().minus(p).norm().times(r1);
266 //v = p.norm().times(v.dot(p.norm()));
268 boolean aspect = false;//(Math.abs(random.nextInt()) % 100) <= 2;
269 Geom.M old_tile_aspect = null;//goal.aspect;
274 tile.aspect = tile.new M(tile.aspect.a / (v.x+1), tile.aspect.f / (v.y+1), tile.aspect.k / (v.z+1));
275 tile.invaspect = tile.new M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
282 double new_tile_score = tile.score();
283 double new_goal_score = goal.score();
284 double tile_delta = new_tile_score - tile_score;
285 double goal_delta = new_goal_score - goal_score;
286 double delta = tile_delta + goal_delta;
287 double swapProbability = Math.exp((-1 * delta) / temperature);
288 //boolean doSwap = Math.random() < swapProbability;
289 boolean doSwap = good && (tile_delta <= 0 && goal_delta <= 0);
291 tile_score = new_tile_score;
292 goal_score = new_goal_score;
293 //System.out.println("score: " + tile_score + " / " + goal_score);
294 if (aspect) System.out.println("aspect " + v);
297 //tile.aspect = old_tile_aspect;
298 //tile.invaspect = tile.new M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
308 * Take care of initialization here.
310 public void init(GLAutoDrawable gld) {
312 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
313 gl.glViewport(0, 0, 500, 300);
314 gl.glEnable(GL.GL_DEPTH_TEST);
315 gl.glClearDepth(1.0);
316 gl.glDepthFunc(GL.GL_LEQUAL);
317 gl.glMatrixMode(GL.GL_PROJECTION);
319 gl.glMatrixMode(GL.GL_MODELVIEW);
323 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
324 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
325 public synchronized void display(GLAutoDrawable drawable) {
326 GL gl = drawable.getGL();
328 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
329 gl.glPointSize(5.0f);
331 glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
332 glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
333 gl.glTranslatef(tx/(float)20, ty/(float)20, 0);
334 gl.glRotatef(anglex/3, 0, 1, 0);
335 gl.glRotatef(angley/3, 1, 0, 0);
337 gl.glBegin(GL.GL_TRIANGLES);
338 draw(gl, true, tile);
341 gl.glBegin(GL.GL_TRIANGLES);
342 gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
343 //draw(gl, false, goal);
348 //gl.glDisable(GL.GL_DEPTH_TEST);
349 gl.glColor4f(1,1,1,1);
350 for(Geom.M m : translations) {
351 //if (v1.z==0 && v1.y==0) continue;
353 if (i != 1 /*&& i!=4*/) continue;
354 Geom.V p = tile.newP(0, 0, 0).times(m).register();
355 Geom.Vec v = tile.new Vec(p.x, p.y, p.z);
356 v = v.times((float)1.04);
357 gl.glTranslatef(v.x, v.y, v.z);
358 draw(gl, false, tile);
359 gl.glTranslatef(-v.x, -v.y, -v.z);
361 //gl.glEnable(GL.GL_DEPTH_TEST);
364 private synchronized void draw(GL gl, boolean triangles, Geom mesh) {
368 for(Geom.T t : mesh) {
369 if (red < 0.15) red = 1.0f;
370 if (green < 0.15) green = 1.0f;
371 if (blue < 0.15) blue = 1.0f;
376 if (triangles) switch(t.color) {
377 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
378 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
379 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
380 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
382 //gl.glBegin(GL.GL_LINES);
385 gl.glBegin(GL.GL_TRIANGLES);
389 gl.glBegin(GL.GL_LINES);
390 t.e1().p1.glVertex(gl);
391 t.e1().p2.glVertex(gl);
392 t.e2().p1.glVertex(gl);
393 t.e2().p2.glVertex(gl);
394 t.e3().p1.glVertex(gl);
395 t.e3().p2.glVertex(gl);
399 Geom.V centroid = t.centroid().register();
400 gl.glBegin(GL.GL_LINES);
401 gl.glColor3f(1, 1, 1);
403 centroid.glVertex(gl);
404 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
408 for(Geom.V p : new Geom.V[] { t.p1(), t.p2(), t.p3() }) {
410 //p.plus(p.norm().times(p.score()*10)).glVertex(gl);
411 p.partner().glVertex(gl);
412 //tile.nearest(p).centroid().glVertex(gl);
420 public static void main(String[] s) throws Exception {
421 StlFile stlf = new StlFile();
422 stlf.load("simplefish.stl");
423 Main main = new Main(stlf);
424 Frame f = new Frame();
425 GLCapabilities glcaps = new GLCapabilities();
426 GLCanvas glcanvas = new GLCanvas();
427 glcanvas.addGLEventListener(main);
428 f.add(glcanvas, BorderLayout.CENTER);
434 glcanvas.addMouseListener(main);
435 glcanvas.addMouseMotionListener(main);
436 glcanvas.addMouseWheelListener(main);
437 glcanvas.addKeyListener(main);
439 main.anneal(glcanvas);
441 public static int verts = 0;
442 public void anneal(GLCanvas glcanvas) throws Exception {
446 for(int i=0; i<1; i++) {
448 //tile.ts.get(Math.abs(random.nextInt()) % tile.ts.size()).e1().p1
450 for(Geom.V p : new Geom.V[] { t.p1(), t.p2(), t.p3() }) {