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 MeshViewer extends Viewer {
16 public int whichNeighbor = 1;
19 public boolean tileon = true;
20 public boolean tilemeshon = false;
21 public boolean goalon = true;
22 public boolean anneal = false;
23 public boolean hillclimb = false;
24 public boolean neighbors = false;
25 public boolean neighborsWire = false;
26 public boolean neighborsWireOne = false;
27 public boolean errorNormals = false;
29 public boolean force = false;
30 public Matrix[] transforms;
31 public Mesh.Vertex[] points;
32 public int breaks = 0;
37 protected HashSet<Mesh.T> safeTriangles = new HashSet<Mesh.T>();
39 public MeshViewer(JFrame f) { super(f); }
42 public void _display(GLAutoDrawable drawable, GL gl) {
44 if (transforms==null) return;
45 glcanvas.setSize(f.getWidth(), f.getHeight() - 100);
46 Graphics2D g = (Graphics2D)f.getGraphics();
47 g.setColor(Color.black);
48 g.fillRect(0, f.getHeight()-100, f.getWidth(), f.getHeight());
49 g.setColor(Color.red);
50 int top = f.getHeight()-70;
51 g.drawString("temperature: "+temps, 10, 30+top);
52 g.drawString("acceptance: "+accepts, 10, 50+top);
53 g.drawString("vertices: "+vertss, 10, 70+top);
54 g.fillRect(140, 25+top, temps, 10);
55 g.fillRect(140, 45+top, accepts, 10);
56 g.fillRect(140, 65+top, vertss, 10);
59 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
62 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
63 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
64 gl.glRotatef(anglex/3, 0, 1, 0);
65 gl.glRotatef(-(angley/3), 1, 0, 0);
68 gl.glDisable(GL.GL_LIGHTING);
69 gl.glColor4f(1, 0, 0, 1);
70 gl.glBegin(GL.GL_LINES);
72 gl.glVertex3f(.3f,0,0);
74 gl.glColor4f(0, 1, 0, 1);
75 gl.glBegin(GL.GL_LINES);
77 gl.glVertex3f(0,.3f,0);
79 gl.glColor4f(0, 0, 1, 1);
80 gl.glBegin(GL.GL_LINES);
82 gl.glVertex3f(0,0,.3f);
84 gl.glEnable(GL.GL_LIGHTING);
87 gl.glBegin(GL.GL_TRIANGLES);
89 draw(gl, true, safeTriangles);
91 draw(gl, false, safeTriangles);
94 //draw(gl, false, tile);
96 gl.glBegin(GL.GL_TRIANGLES);
97 gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
99 draw(gl, false, goal);
104 gl.glColor4f(1,1,1,1);
105 for(Matrix m : transforms) {
107 if (neighborsWireOne && i!=whichNeighbor) continue;
108 if (neighbors) draw(gl, true, safeTriangles, m);
109 else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
112 gl.glDisable(GL.GL_LIGHTING);
113 gl.glShadeModel(GL.GL_FLAT);
114 if (closest != null) {
116 gl.glBegin(gl.GL_POINTS);
117 closest.getPoint().glVertex(gl);
122 gl.glDrawBuffer(GL.GL_BACK);
123 gl.glReadBuffer( GL.GL_BACK );
124 gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
126 gl.glDisable(GL.GL_LIGHTING);
127 gl.glShadeModel(GL.GL_FLAT);
130 updateVisibility(gl, tile);
132 Matrix projection = Matrix.getProjectionMatrix(gl);
133 double dist = Double.MAX_VALUE;
134 if (clickPoint != null) return;
136 for(Mesh.Vertex v : tile.vertices()) {
137 if (!v.visible) continue;
138 Point p = projection.times(v.getPoint());
141 y = glcanvas.getHeight()-y;
142 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
143 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
150 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris) { draw(gl, triangles, tris, Matrix.ONE); }
151 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris, Matrix m) {
156 for(Mesh.T t : tris) {
157 if (red < 0.15) red = 1.0f;
158 if (green < 0.15) green = 1.0f;
159 if (blue < 0.15) blue = 1.0f;
165 if (triangles) switch(t.color) {
166 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
167 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
168 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
169 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
170 case 4: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
171 case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
172 case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break;
176 gl.glColor4f((float)(0.25+(0.05*t.color)),
177 (float)(0.25+(0.05*t.color)),
178 (float)(0.75+(0.05*t.color)),
180 //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
183 if (t.e1().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
184 else if (t.e2().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
185 else if (t.e3().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
186 else gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3);
188 //gl.glBegin(GL.GL_LINES);
193 gl.glDisable(GL.GL_LIGHTING);
194 gl.glBegin(GL.GL_LINES);
195 gl.glColor3f(1, 1, 1);
196 m.times(t.e1().p1.goodp).glVertex(gl);
197 m.times(t.e1().p2.goodp).glVertex(gl);
198 m.times(t.e2().p1.goodp).glVertex(gl);
199 m.times(t.e2().p2.goodp).glVertex(gl);
200 m.times(t.e3().p1.goodp).glVertex(gl);
201 m.times(t.e3().p2.goodp).glVertex(gl);
203 gl.glEnable(GL.GL_LIGHTING);
206 Point centroid = t.centroid();
207 gl.glBegin(GL.GL_LINES);
208 gl.glColor3f(1, 1, 1);
210 if (triangles && errorNormals)
211 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
213 //gl.glDisable(GL.GL_LIGHTING);
214 gl.glBegin(GL.GL_LINES);
215 gl.glColor3f(1, 1, 1);
217 p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
218 //if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
219 //tile.nearest(p).centroid().glVertex(gl);
221 //gl.glEnable(GL.GL_LIGHTING);
230 public synchronized void dump() {
232 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
233 pw.println("solid dump");
234 for(Mesh.T t : tile) {
235 Vec normal = t.norm();
236 pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
237 pw.println(" outer loop");
238 for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
239 pw.println(" vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
241 pw.println(" endloop");
242 pw.println("endfacet");
244 pw.println("endsolid dump");
247 } catch (Exception e) { throw new RuntimeException(e); }
250 public void keyPressed(KeyEvent e) {
252 switch(e.getKeyCode()) {
253 case KeyEvent.VK_SPACE: breaks++; force = true; break;
254 case KeyEvent.VK_UP: temp = temp * 2; break;
255 case KeyEvent.VK_ENTER: temp = 10; break;
256 case KeyEvent.VK_LEFT: whichNeighbor--; break;
257 case KeyEvent.VK_RIGHT: whichNeighbor++; break;
258 case KeyEvent.VK_D: dump(); break;
259 case KeyEvent.VK_E: errorNormals = !errorNormals; break;
260 case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
261 case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
262 case KeyEvent.VK_N: neighbors = !neighbors; break;
263 case KeyEvent.VK_T: tileon = !tileon; break;
264 case KeyEvent.VK_G: goalon = !goalon; break;
265 case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;