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     public void _display(GLAutoDrawable drawable, GL gl) {
42
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);
56
57         GLU glu = new GLU();
58         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
59         gl.glPointSize(5.0f);
60         gl.glLoadIdentity();
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);
65
66
67         gl.glDisable(GL.GL_LIGHTING);
68         gl.glColor4f(1, 0, 0, 1);
69         gl.glBegin(GL.GL_LINES);
70         gl.glVertex3f(0,0,0);
71         gl.glVertex3f(.3f,0,0);
72         gl.glEnd();
73         gl.glColor4f(0, 1, 0, 1);
74         gl.glBegin(GL.GL_LINES);
75         gl.glVertex3f(0,0,0);
76         gl.glVertex3f(0,.3f,0);
77         gl.glEnd();
78         gl.glColor4f(0, 0, 1, 1);
79         gl.glBegin(GL.GL_LINES);
80         gl.glVertex3f(0,0,0);
81         gl.glVertex3f(0,0,.3f);
82         gl.glEnd();
83         gl.glEnable(GL.GL_LIGHTING);
84
85
86         gl.glBegin(GL.GL_TRIANGLES);
87         if (tileon)
88             draw(gl, true, safeTriangles);
89         if (tilemeshon)
90             draw(gl, false, safeTriangles);
91         gl.glEnd();
92
93         //draw(gl, false, tile);
94
95         gl.glBegin(GL.GL_TRIANGLES);
96         gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
97         if (goalon)
98             draw(gl, false, goal);
99         gl.glEnd();
100
101
102         int i = 0;
103         gl.glColor4f(1,1,1,1);
104         for(Matrix m : transforms) {
105             i++;
106             if (neighborsWireOne && i!=whichNeighbor) continue;
107             if (neighbors) draw(gl, true, safeTriangles, m);
108             else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
109         }
110
111         gl.glDisable(GL.GL_LIGHTING);
112         gl.glShadeModel(GL.GL_FLAT);
113         if (closest != null) {
114             gl.glColor3f(1,1,1);
115             gl.glBegin(gl.GL_POINTS);
116             closest.getPoint().glVertex(gl);
117             gl.glEnd();
118         }
119
120         gl.glFlush();
121         gl.glDrawBuffer(GL.GL_BACK);
122         gl.glReadBuffer( GL.GL_BACK );
123         gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
124         gl.glFlush();
125         gl.glDisable(GL.GL_LIGHTING);
126         gl.glShadeModel(GL.GL_FLAT);
127
128         updateVisibility(gl, tile);
129
130         Matrix projection = Matrix.getProjectionMatrix(gl);
131         double dist = Double.MAX_VALUE;
132         if (clickPoint != null) return;
133         closest = null;
134         for(Mesh.Vertex v : tile.vertices()) {
135             if (!v.visible) continue;
136             Point p = projection.times(v.getPoint());
137             int x = (int)p.x;
138             int y = (int)p.y;
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);
142                 closest = v;
143             }
144         }
145     }
146
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) {
149         float red = 0.0f;
150         float green = 0.0f;
151         float blue = 0.0f;
152         synchronized(this) {
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;
157             red -= .09f;
158             green -= .12f;
159             blue -= .15f;
160
161             /*
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;
170             }
171             */
172             
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)),
176                          (float)0.3); 
177             //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
178
179             /*
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);
184             */
185             //gl.glBegin(GL.GL_LINES);
186
187             if (triangles) {
188                 t.glTriangle(gl, m);
189             } else {
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);
199                 gl.glEnd();
200                 gl.glEnable(GL.GL_LIGHTING);
201             }
202
203             Point centroid = t.centroid();
204             gl.glBegin(GL.GL_LINES);
205             gl.glColor3f(1, 1, 1);
206
207             if (triangles && errorNormals)
208                 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
209                     if (p.ok) {
210                         //gl.glDisable(GL.GL_LIGHTING);
211                         gl.glBegin(GL.GL_LINES);
212                         gl.glColor3f(1, 1, 1);
213                         p.p.glVertex(gl);
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);
217                         gl.glEnd();
218                         //gl.glEnable(GL.GL_LIGHTING);
219                     }
220                 }
221             gl.glEnd();
222             
223         }
224         }
225     }
226
227     public synchronized void dump() {
228         try {
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);
237             }
238             pw.println("  endloop");
239             pw.println("endfacet");
240         }
241         pw.println("endsolid dump");
242         pw.flush();
243         pw.close();
244         } catch (Exception e) { throw new RuntimeException(e); }
245     }
246
247     public void keyPressed(KeyEvent e)  {
248         super.keyPressed(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;
263         }
264     }
265
266 }