aea2799d9abe324a39c91788be5321a42dbfc480
[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
131
132     private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris) { draw(gl, triangles, tris, Matrix.ONE); }
133     private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris, Matrix m) {
134         float red = 0.0f;
135         float green = 0.0f;
136         float blue = 0.0f;
137         synchronized(this) {
138         for(Mesh.T t : tris) {
139             if (red < 0.15) red = 1.0f;
140             if (green < 0.15) green = 1.0f;
141             if (blue < 0.15) blue = 1.0f;
142             red -= .09f;
143             green -= .12f;
144             blue -= .15f;
145
146             /*
147             if (triangles) switch(t.color) {
148                 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
149                 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
150                 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
151                 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
152                 case 4: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
153                 case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
154                 case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break;
155             }
156             */
157             
158             gl.glColor4f((float)(0.25+(0.05*t.color)),
159                          (float)(0.25+(0.05*t.color)),
160                          (float)(0.75+(0.05*t.color)),
161                          (float)0.3); 
162             //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
163
164             /*
165             if (t.e1().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
166             else if (t.e2().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
167             else if (t.e3().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
168             else  gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3);
169             */
170             //gl.glBegin(GL.GL_LINES);
171
172             if (triangles) {
173                 t.glTriangle(gl, m);
174             } else {
175                 gl.glDisable(GL.GL_LIGHTING);
176                 gl.glBegin(GL.GL_LINES);
177                 gl.glColor3f(1, 1, 1);
178                 m.times(t.e1().p1.goodp).glVertex(gl);
179                 m.times(t.e1().p2.goodp).glVertex(gl);
180                 m.times(t.e2().p1.goodp).glVertex(gl);
181                 m.times(t.e2().p2.goodp).glVertex(gl);
182                 m.times(t.e3().p1.goodp).glVertex(gl);
183                 m.times(t.e3().p2.goodp).glVertex(gl);
184                 gl.glEnd();
185                 gl.glEnable(GL.GL_LIGHTING);
186             }
187
188             Point centroid = t.centroid();
189             gl.glBegin(GL.GL_LINES);
190             gl.glColor3f(1, 1, 1);
191
192             if (triangles && errorNormals)
193                 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
194                     if (p.ok) {
195                         //gl.glDisable(GL.GL_LIGHTING);
196                         gl.glBegin(GL.GL_LINES);
197                         gl.glColor3f(1, 1, 1);
198                         p.p.glVertex(gl);
199                         p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
200                         //if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
201                         //tile.nearest(p).centroid().glVertex(gl);
202                         gl.glEnd();
203                         //gl.glEnable(GL.GL_LIGHTING);
204                     }
205                 }
206             gl.glEnd();
207             
208         }
209         }
210     }
211
212     public synchronized void dump() {
213         try {
214         PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
215         pw.println("solid dump");
216         for(Mesh.T t : tile) {
217             Vec normal = t.norm();
218             pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
219             pw.println("  outer loop");
220             for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
221                 pw.println("    vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
222             }
223             pw.println("  endloop");
224             pw.println("endfacet");
225         }
226         pw.println("endsolid dump");
227         pw.flush();
228         pw.close();
229         } catch (Exception e) { throw new RuntimeException(e); }
230     }
231
232     public void keyPressed(KeyEvent e)  {
233         super.keyPressed(e);
234         switch(e.getKeyCode()) {
235             case KeyEvent.VK_SPACE: breaks++; force = true; break;
236             case KeyEvent.VK_UP: temp = temp * 2; break;
237             case KeyEvent.VK_ENTER: temp = 10; break;
238             case KeyEvent.VK_LEFT: whichNeighbor--; break;
239             case KeyEvent.VK_RIGHT: whichNeighbor++; break;
240             case KeyEvent.VK_D: dump(); break;
241             case KeyEvent.VK_E: errorNormals = !errorNormals; break;
242             case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
243             case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
244             case KeyEvent.VK_N: neighbors = !neighbors; break;
245             case KeyEvent.VK_T: tileon = !tileon; break;
246             case KeyEvent.VK_G: goalon = !goalon; break;
247             case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;
248         }
249     }
250
251 }