checkpoint
[anneal.git] / src / edu / berkeley / qfat / MeshViewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.nio.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
10 import java.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
13
14 public class MeshViewer extends Viewer {
15
16     public int whichNeighbor = 1;
17
18     public double temp;
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;
28
29     public boolean force = false;
30     public Matrix[] transforms;
31     public Mesh.Vertex[] points;
32     public int breaks = 0;
33
34     public int temps;
35     public int accepts;
36     public    int vertss;
37     protected HashSet<Mesh.T> safeTriangles = new HashSet<Mesh.T>();
38
39     public MeshViewer(JFrame f) { super(f); }
40
41
42     public void _display(GLAutoDrawable drawable, GL gl) {
43
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);
57
58         GLU glu = new GLU();
59         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
60         gl.glPointSize(5.0f);
61         gl.glLoadIdentity();
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);
66
67
68         gl.glDisable(GL.GL_LIGHTING);
69         gl.glColor4f(1, 0, 0, 1);
70         gl.glBegin(GL.GL_LINES);
71         gl.glVertex3f(0,0,0);
72         gl.glVertex3f(.3f,0,0);
73         gl.glEnd();
74         gl.glColor4f(0, 1, 0, 1);
75         gl.glBegin(GL.GL_LINES);
76         gl.glVertex3f(0,0,0);
77         gl.glVertex3f(0,.3f,0);
78         gl.glEnd();
79         gl.glColor4f(0, 0, 1, 1);
80         gl.glBegin(GL.GL_LINES);
81         gl.glVertex3f(0,0,0);
82         gl.glVertex3f(0,0,.3f);
83         gl.glEnd();
84         gl.glEnable(GL.GL_LIGHTING);
85
86
87         gl.glBegin(GL.GL_TRIANGLES);
88         if (tileon)
89             draw(gl, true, safeTriangles);
90         if (tilemeshon)
91             draw(gl, false, safeTriangles);
92         gl.glEnd();
93
94         //draw(gl, false, tile);
95
96         gl.glBegin(GL.GL_TRIANGLES);
97         gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
98         if (goalon)
99             draw(gl, false, goal);
100         gl.glEnd();
101
102
103         int i = 0;
104         gl.glColor4f(1,1,1,1);
105         for(Matrix m : transforms) {
106             i++;
107             if (neighborsWireOne && i!=whichNeighbor) continue;
108             if (neighbors) draw(gl, true, safeTriangles, m);
109             else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
110         }
111
112         gl.glDisable(GL.GL_LIGHTING);
113         gl.glShadeModel(GL.GL_FLAT);
114         if (closest != null) {
115             gl.glColor3f(1,1,1);
116             gl.glBegin(gl.GL_POINTS);
117             closest.getPoint().glVertex(gl);
118             gl.glEnd();
119         }
120
121         gl.glFlush();
122         gl.glDrawBuffer(GL.GL_BACK);
123         gl.glReadBuffer( GL.GL_BACK );
124         gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
125         gl.glFlush();
126         gl.glDisable(GL.GL_LIGHTING);
127         gl.glShadeModel(GL.GL_FLAT);
128
129
130         updateVisibility(gl, tile);
131
132         Matrix projection = Matrix.getProjectionMatrix(gl);
133         double dist = Double.MAX_VALUE;
134         if (clickPoint != null) return;
135         closest = null;
136         for(Mesh.Vertex v : tile.vertices()) {
137             if (!v.visible) continue;
138             Point p = projection.times(v.getPoint());
139             int x = (int)p.x;
140             int y = (int)p.y;
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);
144                 closest = v;
145             }
146         }
147     }
148
149
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) {
152         float red = 0.0f;
153         float green = 0.0f;
154         float blue = 0.0f;
155         synchronized(this) {
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;
160             red -= .09f;
161             green -= .12f;
162             blue -= .15f;
163
164             /*
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;
173             }
174             */
175             
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)),
179                          (float)0.3); 
180             //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
181
182             /*
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);
187             */
188             //gl.glBegin(GL.GL_LINES);
189
190             if (triangles) {
191                 t.glTriangle(gl, m);
192             } else {
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);
202                 gl.glEnd();
203                 gl.glEnable(GL.GL_LIGHTING);
204             }
205
206             Point centroid = t.centroid();
207             gl.glBegin(GL.GL_LINES);
208             gl.glColor3f(1, 1, 1);
209
210             if (triangles && errorNormals)
211                 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
212                     if (p.ok) {
213                         //gl.glDisable(GL.GL_LIGHTING);
214                         gl.glBegin(GL.GL_LINES);
215                         gl.glColor3f(1, 1, 1);
216                         p.p.glVertex(gl);
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);
220                         gl.glEnd();
221                         //gl.glEnable(GL.GL_LIGHTING);
222                     }
223                 }
224             gl.glEnd();
225             
226         }
227         }
228     }
229
230     public synchronized void dump() {
231         try {
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);
240             }
241             pw.println("  endloop");
242             pw.println("endfacet");
243         }
244         pw.println("endsolid dump");
245         pw.flush();
246         pw.close();
247         } catch (Exception e) { throw new RuntimeException(e); }
248     }
249
250     public void keyPressed(KeyEvent e)  {
251         super.keyPressed(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;
266         }
267     }
268
269 }