1 package edu.berkeley.qfat;
5 import java.awt.event.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
14 public class Viewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
16 public boolean force = false;
17 public Mesh tile = new Mesh(false);
18 public Mesh goal = new Mesh(false);
19 public Matrix[] transforms;
20 public Mesh.Vertex[] points;
22 public int whichNeighbor = 1;
25 public boolean tileon = true;
26 public boolean tilemeshon = false;
27 public boolean goalon = true;
28 public boolean anneal = false;
29 public boolean hillclimb = false;
30 public boolean neighbors = false;
31 public boolean neighborsWire = false;
32 public boolean neighborsWireOne = false;
33 public boolean errorNormals = false;
35 public int breaks = 0;
37 boolean shift = false;
38 boolean control = false;
40 public Point getMouse() {
41 return new Point(mousex, glcanvas.getHeight()-mousey, 0);
44 public void mouseWheelMoved(MouseWheelEvent e) {
45 tz -= e.getWheelRotation();
48 public void keyTyped(KeyEvent e) { }
49 public void keyPressed(KeyEvent e) {
50 switch(e.getKeyCode()) {
51 case KeyEvent.VK_CONTROL: control = true; break;
52 case KeyEvent.VK_ALT: alt = true; break;
53 case KeyEvent.VK_SHIFT: shift = true; break;
54 case KeyEvent.VK_SPACE: breaks++; force = true; break;
55 case KeyEvent.VK_UP: temp = temp * 2; break;
56 case KeyEvent.VK_ENTER: temp = 10; break;
57 case KeyEvent.VK_LEFT: whichNeighbor--; break;
58 case KeyEvent.VK_RIGHT: whichNeighbor++; break;
59 case KeyEvent.VK_D: dump(); break;
60 case KeyEvent.VK_E: errorNormals = !errorNormals; break;
61 case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
62 case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
63 case KeyEvent.VK_N: neighbors = !neighbors; break;
64 case KeyEvent.VK_T: tileon = !tileon; break;
65 case KeyEvent.VK_G: goalon = !goalon; break;
66 case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;
69 public synchronized void dump() {
71 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
72 pw.println("solid dump");
73 for(Mesh.T t : tile) {
74 Vec normal = t.norm();
75 pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
76 pw.println(" outer loop");
77 for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
78 pw.println(" vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
80 pw.println(" endloop");
81 pw.println("endfacet");
83 pw.println("endsolid dump");
86 } catch (Exception e) { throw new RuntimeException(e); }
88 public void keyReleased(KeyEvent e) {
89 switch(e.getKeyCode()) {
90 case KeyEvent.VK_CONTROL: control = false; break;
91 case KeyEvent.VK_ALT: alt = false; break;
92 case KeyEvent.VK_SHIFT: shift = false; break;
96 public void mouseClicked(MouseEvent e) { }
97 public void mouseEntered(MouseEvent e) { }
98 public void mouseExited(MouseEvent e) { }
99 public void mousePressed(MouseEvent e) {
100 clickPoint = getMouse();
101 clickClosest = closest == null ? null : closest.getPoint();
103 public void mouseReleased(MouseEvent e) {
108 Point clickPoint = null;
109 Point clickClosest = null;
113 public void mouseMoved(MouseEvent e) {
123 public void mouseDragged(MouseEvent e) {
126 tx += (mousex - e.getX())/(float)20;
127 ty += (mousey - e.getY())/(float)20;
129 if (closest != null && lastGL != null && projection != null && clickClosest != null) {
130 synchronized(safeTriangles) {
131 Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
132 Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
133 //System.out.println(delta + " " + closest.getPoint());
134 System.out.println(getMouse().minus(clickPoint));
135 closest.move(Matrix.translate(delta), true);
139 anglex -= mousex - e.getX();
140 angley += mousey - e.getY();
147 * Take care of initialization here.
149 public void init(GLAutoDrawable gld) {
151 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
152 gl.glViewport(0, 0, 500, 300);
153 gl.glEnable(GL.GL_DEPTH_TEST);
154 gl.glClearDepth(1.0);
155 gl.glDepthFunc(GL.GL_LEQUAL);
156 gl.glMatrixMode(GL.GL_PROJECTION);
158 gl.glMatrixMode(GL.GL_MODELVIEW);
160 float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
161 float mat_shininess[] = { 50.0f };
162 gl.glShadeModel(GL.GL_SMOOTH);
163 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
164 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);
165 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);
166 //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
167 gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f, 4.0f, -10.0f, 0.0f }, 0);
168 gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f, 10.0f, 0.0f }, 0);
169 gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f, 10.0f, 0.0f }, 0);
170 gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f, 10.0f, -10.0f, 0.0f }, 0);
171 gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f, -10.0f, 0.0f }, 0);
172 gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f, -10.0f, 0.0f }, 0);
173 gl.glEnable(GL.GL_LIGHTING);
174 gl.glEnable(GL.GL_LIGHT0);
176 gl.glEnable(GL.GL_LIGHT1);
177 gl.glEnable(GL.GL_LIGHT2);
178 gl.glEnable(GL.GL_LIGHT3);
179 gl.glEnable(GL.GL_LIGHT4);
180 gl.glEnable(GL.GL_LIGHT5);
182 gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
183 gl.glEnable(GL.GL_COLOR_MATERIAL);
188 private GL lastGL = null;
193 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
194 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
195 public void display(GLAutoDrawable drawable) {
197 if (transforms==null) return;
200 glcanvas.setSize(f.getWidth(), f.getHeight() - 100);
201 Graphics2D g = (Graphics2D)f.getGraphics();
202 g.setColor(Color.black);
203 g.fillRect(0, f.getHeight()-100, f.getWidth(), f.getHeight());
204 g.setColor(Color.red);
205 int top = f.getHeight()-70;
206 g.drawString("temperature: "+temps, 10, 30+top);
207 g.drawString("acceptance: "+accepts, 10, 50+top);
208 g.drawString("vertices: "+vertss, 10, 70+top);
209 g.fillRect(140, 25+top, temps, 10);
210 g.fillRect(140, 45+top, accepts, 10);
211 g.fillRect(140, 65+top, vertss, 10);
213 GL gl = drawable.getGL();
216 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
217 gl.glPointSize(5.0f);
219 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
220 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
221 gl.glRotatef(anglex/3, 0, 1, 0);
222 gl.glRotatef(-(angley/3), 1, 0, 0);
225 gl.glDisable(GL.GL_LIGHTING);
226 gl.glColor4f(1, 0, 0, 1);
227 gl.glBegin(GL.GL_LINES);
228 gl.glVertex3f(0,0,0);
229 gl.glVertex3f(.3f,0,0);
231 gl.glColor4f(0, 1, 0, 1);
232 gl.glBegin(GL.GL_LINES);
233 gl.glVertex3f(0,0,0);
234 gl.glVertex3f(0,.3f,0);
236 gl.glColor4f(0, 0, 1, 1);
237 gl.glBegin(GL.GL_LINES);
238 gl.glVertex3f(0,0,0);
239 gl.glVertex3f(0,0,.3f);
241 gl.glEnable(GL.GL_LIGHTING);
244 gl.glBegin(GL.GL_TRIANGLES);
246 draw(gl, true, safeTriangles);
248 draw(gl, false, safeTriangles);
251 //draw(gl, false, tile);
253 gl.glBegin(GL.GL_TRIANGLES);
254 gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
256 draw(gl, false, goal);
261 //gl.glDisable(GL.GL_DEPTH_TEST);
262 gl.glColor4f(1,1,1,1);
263 for(Matrix m : transforms) {
265 gl.glColor4f(0, 1, 1, 1);
266 gl.glBegin(GL.GL_LINES);
267 new Point(0,0,0).glVertex(gl);
268 new Point(0,0,0).plus(m.getTranslationalComponent()).glVertex(gl);
270 gl.glEnable(GL.GL_LIGHTING);
272 //if (v1.z==0 && v1.y==0) continue;
274 if (neighborsWireOne && i!=whichNeighbor) continue;
277 Point p = new Point(0, 0, 0).times(m);
278 Vec v = new Vec(p.x, p.y, p.z);
279 v = v.times((float)1.04);
280 gl.glTranslatef(v.x, v.y, v.z);
282 if (neighbors) draw(gl, true, safeTriangles, m);
283 else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
285 gl.glTranslatef(-v.x, -v.y, -v.z);
288 //gl.glEnable(GL.GL_DEPTH_TEST);
290 gl.glDisable(GL.GL_LIGHTING);
291 gl.glShadeModel(GL.GL_FLAT);
292 if (closest != null) {
294 gl.glBegin(gl.GL_POINTS);
295 closest.getPoint().glVertex(gl);
298 Mesh.Vertex v2 = closest.hack(gl, getMouse());
299 gl.glBegin(GL.GL_LINES);
300 closest.getPoint().glVertex(gl);
301 if (v2 != null) v2.getPoint().glVertex(gl);
307 gl.glDrawBuffer(GL.GL_BACK);
308 gl.glReadBuffer( GL.GL_BACK );
309 gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
311 gl.glDisable(GL.GL_LIGHTING);
312 gl.glShadeModel(GL.GL_FLAT);
314 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
316 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
317 double dist = Double.MAX_VALUE;
318 if (clickPoint==null) closest = null;
319 projection = Matrix.getProjectionMatrix(gl);
320 synchronized(safeTriangles) {
321 for(Mesh.T t : safeTriangles)
322 t.glTriangle(gl, null);
323 for(Mesh.Vertex v : tile.vertices()) {
324 Point p = v.getPoint();
326 gl.glBegin(gl.GL_POINTS);
331 Point projected = projection.times(p);
332 int x = (int)projected.x;
333 int y = (int)projected.y;
334 gl.glReadPixels(x-1, y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
337 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
341 gl.glBegin(gl.GL_POINTS);
344 y = glcanvas.getHeight()-y;
345 if (clickPoint==null) {
346 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
347 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
354 gl.glShadeModel(GL.GL_SMOOTH);
355 gl.glEnable(GL.GL_LIGHTING);
356 gl.glDrawBuffer(GL.GL_FRONT);
358 Mesh.Vertex closest = null;
359 Matrix projection = null;
361 protected HashSet<Mesh.T> safeTriangles = new HashSet<Mesh.T>();
363 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris) { draw(gl, triangles, tris, Matrix.ONE); }
364 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris, Matrix m) {
368 synchronized(safeTriangles) {
369 for(Mesh.T t : tris) {
370 if (red < 0.15) red = 1.0f;
371 if (green < 0.15) green = 1.0f;
372 if (blue < 0.15) blue = 1.0f;
378 if (triangles) switch(t.color) {
379 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
380 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
381 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
382 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
383 case 4: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
384 case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
385 case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break;
389 gl.glColor4f((float)(0.25+(0.05*t.color)),
390 (float)(0.25+(0.05*t.color)),
391 (float)(0.75+(0.05*t.color)),
393 //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
396 if (t.e1().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
397 else if (t.e2().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
398 else if (t.e3().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
399 else gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3);
401 //gl.glBegin(GL.GL_LINES);
406 gl.glDisable(GL.GL_LIGHTING);
407 gl.glBegin(GL.GL_LINES);
408 gl.glColor3f(1, 1, 1);
409 m.times(t.e1().p1.goodp).glVertex(gl);
410 m.times(t.e1().p2.goodp).glVertex(gl);
411 m.times(t.e2().p1.goodp).glVertex(gl);
412 m.times(t.e2().p2.goodp).glVertex(gl);
413 m.times(t.e3().p1.goodp).glVertex(gl);
414 m.times(t.e3().p2.goodp).glVertex(gl);
416 gl.glEnable(GL.GL_LIGHTING);
419 Point centroid = t.centroid();
420 gl.glBegin(GL.GL_LINES);
421 gl.glColor3f(1, 1, 1);
423 if (triangles && errorNormals)
424 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
426 //gl.glDisable(GL.GL_LIGHTING);
427 gl.glBegin(GL.GL_LINES);
428 gl.glColor3f(1, 1, 1);
430 p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
431 //if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
432 //tile.nearest(p).centroid().glVertex(gl);
434 //gl.glEnable(GL.GL_LIGHTING);
444 private GLCanvas glcanvas;
445 public Viewer(JFrame f) {
448 GLCapabilities glcaps = new GLCapabilities();
449 glcanvas = new GLCanvas();
450 glcanvas.addGLEventListener(this);
451 f.add(glcanvas, BorderLayout.CENTER);
453 glcanvas.addMouseListener(this);
454 glcanvas.addMouseMotionListener(this);
455 glcanvas.addMouseWheelListener(this);
456 glcanvas.addKeyListener(this);
458 public void repaint() {