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); }
41 public void _display(GLAutoDrawable drawable, GL gl) {
43 if (transforms==null) return;
44 glcanvas.setSize(f.getWidth(), f.getHeight() - 100);
45 Graphics2D g = (Graphics2D)f.getGraphics();
46 g.setColor(Color.black);
47 g.fillRect(0, f.getHeight()-100, f.getWidth(), f.getHeight());
48 g.setColor(Color.red);
49 int top = f.getHeight()-70;
50 g.drawString("temperature: "+temps, 10, 30+top);
51 g.drawString("acceptance: "+accepts, 10, 50+top);
52 g.drawString("vertices: "+vertss, 10, 70+top);
53 g.fillRect(140, 25+top, temps, 10);
54 g.fillRect(140, 45+top, accepts, 10);
55 g.fillRect(140, 65+top, vertss, 10);
58 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
61 glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
62 glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
63 gl.glRotatef(anglex/3, 0, 1, 0);
64 gl.glRotatef(-(angley/3), 1, 0, 0);
67 gl.glDisable(GL.GL_LIGHTING);
68 gl.glColor4f(1, 0, 0, 1);
69 gl.glBegin(GL.GL_LINES);
71 gl.glVertex3f(.3f,0,0);
73 gl.glColor4f(0, 1, 0, 1);
74 gl.glBegin(GL.GL_LINES);
76 gl.glVertex3f(0,.3f,0);
78 gl.glColor4f(0, 0, 1, 1);
79 gl.glBegin(GL.GL_LINES);
81 gl.glVertex3f(0,0,.3f);
83 gl.glEnable(GL.GL_LIGHTING);
86 gl.glBegin(GL.GL_TRIANGLES);
88 draw(gl, true, safeTriangles);
90 draw(gl, false, safeTriangles);
93 //draw(gl, false, tile);
95 gl.glBegin(GL.GL_TRIANGLES);
96 gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
98 draw(gl, false, goal);
103 gl.glColor4f(1,1,1,1);
104 for(Matrix m : transforms) {
106 if (neighborsWireOne && i!=whichNeighbor) continue;
107 if (neighbors) draw(gl, true, safeTriangles, m);
108 else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
111 gl.glDisable(GL.GL_LIGHTING);
112 gl.glShadeModel(GL.GL_FLAT);
113 if (closest != null) {
115 gl.glBegin(gl.GL_POINTS);
116 closest.getPoint().glVertex(gl);
121 gl.glDrawBuffer(GL.GL_BACK);
122 gl.glReadBuffer( GL.GL_BACK );
123 gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
125 gl.glDisable(GL.GL_LIGHTING);
126 gl.glShadeModel(GL.GL_FLAT);
128 updateVisibility(gl, tile);
130 Matrix projection = Matrix.getProjectionMatrix(gl);
131 double dist = Double.MAX_VALUE;
132 if (clickPoint != null) return;
134 for(Mesh.Vertex v : tile.vertices()) {
135 if (!v.visible) continue;
136 Point p = projection.times(v.getPoint());
139 y = glcanvas.getHeight()-y;
140 if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) {
141 dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey);
147 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris) { draw(gl, triangles, tris, Matrix.ONE); }
148 private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris, Matrix m) {
153 for(Mesh.T t : tris) {
154 if (red < 0.15) red = 1.0f;
155 if (green < 0.15) green = 1.0f;
156 if (blue < 0.15) blue = 1.0f;
162 if (triangles) switch(t.color) {
163 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
164 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
165 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
166 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
167 case 4: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
168 case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
169 case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break;
173 gl.glColor4f((float)(0.25+(0.05*t.color)),
174 (float)(0.25+(0.05*t.color)),
175 (float)(0.75+(0.05*t.color)),
177 //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
180 if (t.e1().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
181 else if (t.e2().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
182 else if (t.e3().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
183 else gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3);
185 //gl.glBegin(GL.GL_LINES);
190 gl.glDisable(GL.GL_LIGHTING);
191 gl.glBegin(GL.GL_LINES);
192 gl.glColor3f(1, 1, 1);
193 m.times(t.e1().p1.goodp).glVertex(gl);
194 m.times(t.e1().p2.goodp).glVertex(gl);
195 m.times(t.e2().p1.goodp).glVertex(gl);
196 m.times(t.e2().p2.goodp).glVertex(gl);
197 m.times(t.e3().p1.goodp).glVertex(gl);
198 m.times(t.e3().p2.goodp).glVertex(gl);
200 gl.glEnable(GL.GL_LIGHTING);
203 Point centroid = t.centroid();
204 gl.glBegin(GL.GL_LINES);
205 gl.glColor3f(1, 1, 1);
207 if (triangles && errorNormals)
208 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
210 //gl.glDisable(GL.GL_LIGHTING);
211 gl.glBegin(GL.GL_LINES);
212 gl.glColor3f(1, 1, 1);
214 p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
215 //if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
216 //tile.nearest(p).centroid().glVertex(gl);
218 //gl.glEnable(GL.GL_LIGHTING);
227 public synchronized void dump() {
229 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
230 pw.println("solid dump");
231 for(Mesh.T t : tile) {
232 Vec normal = t.norm();
233 pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
234 pw.println(" outer loop");
235 for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
236 pw.println(" vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
238 pw.println(" endloop");
239 pw.println("endfacet");
241 pw.println("endsolid dump");
244 } catch (Exception e) { throw new RuntimeException(e); }
247 public void keyPressed(KeyEvent e) {
249 switch(e.getKeyCode()) {
250 case KeyEvent.VK_SPACE: breaks++; force = true; break;
251 case KeyEvent.VK_UP: temp = temp * 2; break;
252 case KeyEvent.VK_ENTER: temp = 10; break;
253 case KeyEvent.VK_LEFT: whichNeighbor--; break;
254 case KeyEvent.VK_RIGHT: whichNeighbor++; break;
255 case KeyEvent.VK_D: dump(); break;
256 case KeyEvent.VK_E: errorNormals = !errorNormals; break;
257 case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
258 case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
259 case KeyEvent.VK_N: neighbors = !neighbors; break;
260 case KeyEvent.VK_T: tileon = !tileon; break;
261 case KeyEvent.VK_G: goalon = !goalon; break;
262 case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;