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.glDisable(GL.GL_DEPTH_TEST);
105 gl.glColor4f(1,1,1,1);
106 for(Matrix m : transforms) {
108 gl.glColor4f(0, 1, 1, 1);
109 gl.glBegin(GL.GL_LINES);
110 new Point(0,0,0).glVertex(gl);
111 new Point(0,0,0).plus(m.getTranslationalComponent()).glVertex(gl);
113 gl.glEnable(GL.GL_LIGHTING);
115 //if (v1.z==0 && v1.y==0) continue;
117 if (neighborsWireOne && i!=whichNeighbor) continue;
120 Point p = new Point(0, 0, 0).times(m);
121 Vec v = new Vec(p.x, p.y, p.z);
122 v = v.times((float)1.04);
123 gl.glTranslatef(v.x, v.y, v.z);
125 if (neighbors) draw(gl, true, safeTriangles, m);
126 else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
128 gl.glTranslatef(-v.x, -v.y, -v.z);
131 //gl.glEnable(GL.GL_DEPTH_TEST);
133 gl.glDisable(GL.GL_LIGHTING);
134 gl.glShadeModel(GL.GL_FLAT);
135 if (closest != null) {
137 gl.glBegin(gl.GL_POINTS);
138 closest.getPoint().glVertex(gl);
141 Mesh.Vertex v2 = closest.hack(gl, getMouse());
142 gl.glBegin(GL.GL_LINES);
143 closest.getPoint().glVertex(gl);
144 if (v2 != null) v2.getPoint().glVertex(gl);
150 gl.glDrawBuffer(GL.GL_BACK);
151 gl.glReadBuffer( GL.GL_BACK );
152 gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
154 gl.glDisable(GL.GL_LIGHTING);
155 gl.glShadeModel(GL.GL_FLAT);
157 IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
159 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
160 double dist = Double.MAX_VALUE;
161 if (clickPoint==null) closest = null;
163 for(Mesh.T t : safeTriangles)
164 t.glTriangle(gl, null);
165 for(Mesh.Vertex v : tile.vertices()) {
166 Point p = v.getPoint();
168 gl.glBegin(gl.GL_POINTS);
173 Point projected = projection.times(p);
174 int x = (int)projected.x;
175 int y = (int)projected.y;
176 gl.glReadPixels(x-1, y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
179 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
183 gl.glBegin(gl.GL_POINTS);
186 y = glcanvas.getHeight()-y;
187 if (clickPoint==null) {
188 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
189 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
196 gl.glShadeModel(GL.GL_SMOOTH);
197 gl.glEnable(GL.GL_LIGHTING);
198 gl.glDrawBuffer(GL.GL_FRONT);
202 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris) { draw(gl, triangles, tris, Matrix.ONE); }
203 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris, Matrix m) {
208 for(Mesh.T t : tris) {
209 if (red < 0.15) red = 1.0f;
210 if (green < 0.15) green = 1.0f;
211 if (blue < 0.15) blue = 1.0f;
217 if (triangles) switch(t.color) {
218 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
219 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
220 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
221 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
222 case 4: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
223 case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
224 case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break;
228 gl.glColor4f((float)(0.25+(0.05*t.color)),
229 (float)(0.25+(0.05*t.color)),
230 (float)(0.75+(0.05*t.color)),
232 //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
235 if (t.e1().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
236 else if (t.e2().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
237 else if (t.e3().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
238 else gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3);
240 //gl.glBegin(GL.GL_LINES);
245 gl.glDisable(GL.GL_LIGHTING);
246 gl.glBegin(GL.GL_LINES);
247 gl.glColor3f(1, 1, 1);
248 m.times(t.e1().p1.goodp).glVertex(gl);
249 m.times(t.e1().p2.goodp).glVertex(gl);
250 m.times(t.e2().p1.goodp).glVertex(gl);
251 m.times(t.e2().p2.goodp).glVertex(gl);
252 m.times(t.e3().p1.goodp).glVertex(gl);
253 m.times(t.e3().p2.goodp).glVertex(gl);
255 gl.glEnable(GL.GL_LIGHTING);
258 Point centroid = t.centroid();
259 gl.glBegin(GL.GL_LINES);
260 gl.glColor3f(1, 1, 1);
262 if (triangles && errorNormals)
263 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
265 //gl.glDisable(GL.GL_LIGHTING);
266 gl.glBegin(GL.GL_LINES);
267 gl.glColor3f(1, 1, 1);
269 p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
270 //if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
271 //tile.nearest(p).centroid().glVertex(gl);
273 //gl.glEnable(GL.GL_LIGHTING);
282 public synchronized void dump() {
284 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
285 pw.println("solid dump");
286 for(Mesh.T t : tile) {
287 Vec normal = t.norm();
288 pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
289 pw.println(" outer loop");
290 for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
291 pw.println(" vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
293 pw.println(" endloop");
294 pw.println("endfacet");
296 pw.println("endsolid dump");
299 } catch (Exception e) { throw new RuntimeException(e); }
302 public void keyPressed(KeyEvent e) {
304 switch(e.getKeyCode()) {
305 case KeyEvent.VK_SPACE: breaks++; force = true; break;
306 case KeyEvent.VK_UP: temp = temp * 2; break;
307 case KeyEvent.VK_ENTER: temp = 10; break;
308 case KeyEvent.VK_LEFT: whichNeighbor--; break;
309 case KeyEvent.VK_RIGHT: whichNeighbor++; break;
310 case KeyEvent.VK_D: dump(); break;
311 case KeyEvent.VK_E: errorNormals = !errorNormals; break;
312 case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
313 case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
314 case KeyEvent.VK_N: neighbors = !neighbors; break;
315 case KeyEvent.VK_T: tileon = !tileon; break;
316 case KeyEvent.VK_G: goalon = !goalon; break;
317 case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;