eaafed6fa4a9a70a0735c5e0fff06dcbe2aa8d1a
[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.glDisable(GL.GL_DEPTH_TEST);
105         gl.glColor4f(1,1,1,1);
106         for(Matrix m : transforms) {
107             /*
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);
112               gl.glEnd();
113               gl.glEnable(GL.GL_LIGHTING);
114             */
115             //if (v1.z==0 && v1.y==0) continue;
116             i++;
117             if (neighborsWireOne && i!=whichNeighbor) continue;
118             //if (i>4) continue;
119             /*
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);
124             */
125             if (neighbors) draw(gl, true, safeTriangles, m);
126             else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
127             /*
128             gl.glTranslatef(-v.x, -v.y, -v.z);
129             */
130         }
131         //gl.glEnable(GL.GL_DEPTH_TEST);
132
133         gl.glDisable(GL.GL_LIGHTING);
134         gl.glShadeModel(GL.GL_FLAT);
135         if (closest != null) {
136             gl.glColor3f(1,1,1);
137             gl.glBegin(gl.GL_POINTS);
138             closest.getPoint().glVertex(gl);
139             gl.glEnd();
140             /*
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);
145             gl.glEnd();
146             */
147         }
148
149         gl.glFlush();
150         gl.glDrawBuffer(GL.GL_BACK);
151         gl.glReadBuffer( GL.GL_BACK );
152         gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
153         gl.glFlush();
154         gl.glDisable(GL.GL_LIGHTING);
155         gl.glShadeModel(GL.GL_FLAT);
156
157         IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
158         gl.glColor3f(0,0,0);
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;
162         synchronized(this) {
163             for(Mesh.T t : safeTriangles)
164                 t.glTriangle(gl, null);
165             for(Mesh.Vertex v : tile.vertices()) {
166                 Point p = v.getPoint();
167                 gl.glColor3f(1,1,1);
168                 gl.glBegin(gl.GL_POINTS);
169                 p.glVertex(gl);
170                 gl.glEnd();
171                 gl.glFlush();
172
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);
177
178                 boolean vis = false;
179                 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
180                 v.visible = vis;
181                 if (vis) {
182                     gl.glColor3f(0,0,0);
183                     gl.glBegin(gl.GL_POINTS);
184                     p.glVertex(gl);
185                     gl.glEnd();
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);
190                             closest = v;
191                         }
192                     }
193                 }
194             }
195         }
196         gl.glShadeModel(GL.GL_SMOOTH);
197         gl.glEnable(GL.GL_LIGHTING);
198         gl.glDrawBuffer(GL.GL_FRONT);
199     }
200
201
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) {
204         float red = 0.0f;
205         float green = 0.0f;
206         float blue = 0.0f;
207         synchronized(this) {
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;
212             red -= .09f;
213             green -= .12f;
214             blue -= .15f;
215
216             /*
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;
225             }
226             */
227             
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)),
231                          (float)0.3); 
232             //if (t.v1().visible && t.v2().visible && t.v3().visible) continue;
233
234             /*
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);
239             */
240             //gl.glBegin(GL.GL_LINES);
241
242             if (triangles) {
243                 t.glTriangle(gl, m);
244             } else {
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);
254                 gl.glEnd();
255                 gl.glEnable(GL.GL_LIGHTING);
256             }
257
258             Point centroid = t.centroid();
259             gl.glBegin(GL.GL_LINES);
260             gl.glColor3f(1, 1, 1);
261
262             if (triangles && errorNormals)
263                 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
264                     if (p.ok) {
265                         //gl.glDisable(GL.GL_LIGHTING);
266                         gl.glBegin(GL.GL_LINES);
267                         gl.glColor3f(1, 1, 1);
268                         p.p.glVertex(gl);
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);
272                         gl.glEnd();
273                         //gl.glEnable(GL.GL_LIGHTING);
274                     }
275                 }
276             gl.glEnd();
277             
278         }
279         }
280     }
281
282     public synchronized void dump() {
283         try {
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);
292             }
293             pw.println("  endloop");
294             pw.println("endfacet");
295         }
296         pw.println("endsolid dump");
297         pw.flush();
298         pw.close();
299         } catch (Exception e) { throw new RuntimeException(e); }
300     }
301
302     public void keyPressed(KeyEvent e)  {
303         super.keyPressed(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;
318         }
319     }
320
321 }