checkpoint
[anneal.git] / src / edu / berkeley / qfat / MeshViewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.media.opengl.*;
7 import javax.media.opengl.glu.*;
8 import com.sun.opengl.util.*;
9 import java.util.*;
10 import edu.berkeley.qfat.geom.*;
11 import edu.berkeley.qfat.geom.Point;
12
13 public class MeshViewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener  {
14
15     public boolean force = false;
16     public Mesh tile = new Mesh(false);
17     public Mesh goal = new Mesh(false);
18     public Matrix[] transforms;
19     public Mesh.Vertex[] points;
20
21     public int whichNeighbor = 1;
22
23     public double temp;
24     public boolean tileon = true;
25     public boolean tilemeshon = false;
26     public boolean goalon = true;
27     public boolean anneal = false;
28     public boolean hillclimb = false;
29     public boolean neighbors = false;
30     public boolean neighborsWire = false;
31     public boolean neighborsWireOne = false;
32     public boolean errorNormals = false;
33
34     public int breaks = 0;
35     boolean alt = false;
36     boolean shift = false;
37     boolean control = false;
38
39     public void mouseWheelMoved(MouseWheelEvent e) {
40         tz -= e.getWheelRotation();
41     }
42
43     public void keyTyped(KeyEvent e)  { }
44     public void keyPressed(KeyEvent e)  {
45         switch(e.getKeyCode()) {
46             case KeyEvent.VK_CONTROL: control = true; break;
47             case KeyEvent.VK_ALT: alt = true; break;
48             case KeyEvent.VK_SHIFT: shift = true; break;
49             case KeyEvent.VK_SPACE: breaks++; force = true; break;
50             case KeyEvent.VK_UP: temp = temp * 2; break;
51             case KeyEvent.VK_ENTER: temp = 10; break;
52             case KeyEvent.VK_LEFT: whichNeighbor--; break;
53             case KeyEvent.VK_RIGHT: whichNeighbor++; break;
54             case KeyEvent.VK_D: dump(); break;
55             case KeyEvent.VK_E: errorNormals = !errorNormals; break;
56             case KeyEvent.VK_A: hillclimb = false; anneal = !anneal; break;
57             case KeyEvent.VK_H: anneal = true; hillclimb = !hillclimb; break;
58             case KeyEvent.VK_N: neighbors = !neighbors; break;
59             case KeyEvent.VK_T: tileon = !tileon; break;
60             case KeyEvent.VK_G: goalon = !goalon; break;
61             case KeyEvent.VK_M: tilemeshon = !tilemeshon; break;
62         }
63     }
64     public synchronized void dump() {
65         try {
66         PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("dump.stl")));
67         pw.println("solid dump");
68         for(Mesh.T t : tile) {
69             Vec normal = t.norm();
70             pw.println("facet normal " + normal.x + " " + normal.y + " " + normal.z);
71             pw.println("  outer loop");
72             for(Mesh.Vertex v : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
73                 pw.println("    vertex " + v.p.x + " " + v.p.y + " " + v.p.z);
74             }
75             pw.println("  endloop");
76             pw.println("endfacet");
77         }
78         pw.println("endsolid dump");
79         pw.flush();
80         pw.close();
81         } catch (Exception e) { throw new RuntimeException(e); }
82     }
83     public void keyReleased(KeyEvent e) {
84         switch(e.getKeyCode()) {
85             case KeyEvent.VK_CONTROL: control = false; break;
86             case KeyEvent.VK_ALT: alt = false; break;
87             case KeyEvent.VK_SHIFT: shift = false; break;
88         }
89     }
90
91     public void mouseClicked(MouseEvent e) { }
92     public void mouseEntered(MouseEvent e) { }
93     public void mouseExited(MouseEvent e) { }
94     public void mousePressed(MouseEvent e) { }
95     public void mouseReleased(MouseEvent e) { }
96
97     int mousex;
98     int mousey;
99     public void mouseMoved(MouseEvent e) {
100         mousex = e.getX();
101         mousey = e.getY();
102     }
103
104     float tx = 0;
105     float ty = 0;
106     float tz = 0;
107     float anglex = 0;
108     float angley = 0;
109     public void mouseDragged(MouseEvent e) {
110         if (shift) {
111             tx += (mousex - e.getX())/(float)20;
112             ty += (mousey - e.getY())/(float)20;
113         } else {
114             anglex -= mousex - e.getX();
115             angley += mousey - e.getY();
116         }
117         mousex = e.getX();
118         mousey = e.getY();
119     }
120
121     /**
122      * Take care of initialization here.
123      */
124     public void init(GLAutoDrawable gld) {
125         GL gl = gld.getGL();
126         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
127         gl.glViewport(0, 0, 500, 300);
128         gl.glEnable(GL.GL_DEPTH_TEST);
129         gl.glClearDepth(1.0);
130         gl.glDepthFunc(GL.GL_LEQUAL);
131         gl.glMatrixMode(GL.GL_PROJECTION);
132         gl.glLoadIdentity();
133         gl.glMatrixMode(GL.GL_MODELVIEW);
134
135         float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
136         float mat_shininess[] = { 50.0f };
137         gl.glShadeModel(GL.GL_SMOOTH);
138         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
139         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);  
140         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);  
141         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
142         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f,    4.0f,  -10.0f, 0.0f }, 0);
143         gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f,   10.0f, 0.0f }, 0);
144         gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f,   10.0f, 0.0f }, 0);
145         gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f,  10.0f,  -10.0f, 0.0f }, 0);
146         gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f,  -10.0f, 0.0f }, 0);
147         gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f,  -10.0f, 0.0f }, 0);
148         gl.glEnable(GL.GL_LIGHTING);
149         gl.glEnable(GL.GL_LIGHT0);
150         /*
151         gl.glEnable(GL.GL_LIGHT1);
152         gl.glEnable(GL.GL_LIGHT2);
153         gl.glEnable(GL.GL_LIGHT3);
154         gl.glEnable(GL.GL_LIGHT4);
155         gl.glEnable(GL.GL_LIGHT5);
156         */
157         gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
158         gl.glEnable(GL.GL_COLOR_MATERIAL);
159
160         display(gld);
161     }
162
163     public int temps;
164     public int accepts;
165     public    int vertss;
166     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
167     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
168     public void display(GLAutoDrawable drawable) {
169
170         if (transforms==null) return;
171
172
173         glcanvas.setSize(f.getWidth(), f.getHeight() - 100);
174         Graphics2D g = (Graphics2D)f.getGraphics();
175         g.setColor(Color.black);
176         g.fillRect(0, f.getHeight()-100, f.getWidth(), f.getHeight());
177         g.setColor(Color.red);
178         int top = f.getHeight()-70;
179         g.drawString("temperature: "+temps, 10, 30+top);
180         g.drawString("acceptance: "+accepts, 10, 50+top);
181         g.drawString("vertices: "+vertss, 10, 70+top);
182         g.fillRect(140, 25+top, temps, 10);
183         g.fillRect(140, 45+top, accepts, 10);
184         g.fillRect(140, 65+top, vertss, 10);
185
186         GL gl = drawable.getGL();
187         GLU glu = new GLU();
188         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
189         gl.glPointSize(5.0f);
190         gl.glLoadIdentity();
191         glu.gluPerspective(50, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
192         glu.gluLookAt(0, 0, -((tz/10)-1), 0, 0, 0, 0, 1, 0);
193         gl.glRotatef(anglex/3, 0, 1, 0);
194         gl.glRotatef(-(angley/3), 1, 0, 0);
195
196
197         gl.glDisable(GL.GL_LIGHTING);
198         gl.glColor4f(1, 0, 0, 1);
199         gl.glBegin(GL.GL_LINES);
200         gl.glVertex3f(0,0,0);
201         gl.glVertex3f(.3f,0,0);
202         gl.glEnd();
203         gl.glColor4f(0, 1, 0, 1);
204         gl.glBegin(GL.GL_LINES);
205         gl.glVertex3f(0,0,0);
206         gl.glVertex3f(0,.3f,0);
207         gl.glEnd();
208         gl.glColor4f(0, 0, 1, 1);
209         gl.glBegin(GL.GL_LINES);
210         gl.glVertex3f(0,0,0);
211         gl.glVertex3f(0,0,.3f);
212         gl.glEnd();
213         gl.glEnable(GL.GL_LIGHTING);
214
215
216         gl.glBegin(GL.GL_TRIANGLES);
217         if (tileon)
218         draw(gl, true, safeTriangles);
219         if (tilemeshon)
220         draw(gl, false, safeTriangles);
221         gl.glEnd();
222
223         //draw(gl, false, tile);
224
225         gl.glBegin(GL.GL_TRIANGLES);
226         gl.glColor4f((float)0.5, (float)0.5, (float)0.5, (float)0.8);
227         if (goalon)
228             draw(gl, false, goal);
229         gl.glEnd();
230
231
232         int i = 0;
233         //gl.glDisable(GL.GL_DEPTH_TEST);
234         gl.glColor4f(1,1,1,1);
235         for(Matrix m : transforms) {
236             /*
237               gl.glColor4f(0, 1, 1, 1);
238               gl.glBegin(GL.GL_LINES);
239               new Point(0,0,0).glVertex(gl);
240               new Point(0,0,0).plus(m.getTranslationalComponent()).glVertex(gl);
241               gl.glEnd();
242               gl.glEnable(GL.GL_LIGHTING);
243             */
244             //if (v1.z==0 && v1.y==0) continue;
245             i++;
246             if (neighborsWireOne && i!=whichNeighbor) continue;
247             //if (i>4) continue;
248             /*
249             Point p = new Point(0, 0, 0).times(m);
250             Vec v = new Vec(p.x, p.y, p.z);
251             v = v.times((float)1.04);
252             gl.glTranslatef(v.x, v.y, v.z);
253             */
254             if (neighbors) draw(gl, true, safeTriangles, m);
255             else if (neighborsWire || neighborsWireOne) draw(gl, false, safeTriangles, m);
256             /*
257             gl.glTranslatef(-v.x, -v.y, -v.z);
258             */
259         }
260         //gl.glEnable(GL.GL_DEPTH_TEST);
261         gl.glEnable (GL.GL_LIGHTING);
262
263     }
264
265     protected HashSet<Mesh.T> safeTriangles = new HashSet<Mesh.T>();
266
267     private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris) { draw(gl, triangles, tris, Matrix.ONE); }
268     private void draw(GL gl, boolean triangles, Iterable<Mesh.T> tris, Matrix m) {
269         float red = 0.0f;
270         float green = 0.0f;
271         float blue = 0.0f;
272         synchronized(safeTriangles) {
273         for(Mesh.T t : tris) {
274             if (red < 0.15) red = 1.0f;
275             if (green < 0.15) green = 1.0f;
276             if (blue < 0.15) blue = 1.0f;
277             red -= .09f;
278             green -= .12f;
279             blue -= .15f;
280
281             if (triangles) switch(t.color/*class*/) {
282                 case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break;
283                 case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break;
284                 case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break;
285                 case 3: gl.glColor4f((float)0.50, (float)0.50, (float)0.50, (float)0.3); break;
286                 case 4: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
287                 case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break;
288                 case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break;
289             }
290             /*
291             if (t.e1().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
292             else if (t.e2().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
293             else if (t.e3().pair.t==null) gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3);
294             else  gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3);
295             */
296             //gl.glBegin(GL.GL_LINES);
297
298             if (triangles) {
299                 gl.glBegin(GL.GL_TRIANGLES);
300                 if (t.shouldBeDrawn()) {
301                     m.times(t.norm()).glNormal(gl);
302                     m.times(t.v1().goodp).glVertex(gl);
303                     m.times(t.v2().goodp).glVertex(gl);
304                     m.times(t.v3().goodp).glVertex(gl);
305                     //t.glVertices(gl, m);
306                 }
307                 gl.glEnd();
308             } else {
309                 gl.glDisable(GL.GL_LIGHTING);
310                 gl.glBegin(GL.GL_LINES);
311                 gl.glColor3f(1, 1, 1);
312                 m.times(t.e1().p1.goodp).glVertex(gl);
313                 m.times(t.e1().p2.goodp).glVertex(gl);
314                 m.times(t.e2().p1.goodp).glVertex(gl);
315                 m.times(t.e2().p2.goodp).glVertex(gl);
316                 m.times(t.e3().p1.goodp).glVertex(gl);
317                 m.times(t.e3().p2.goodp).glVertex(gl);
318                 gl.glEnd();
319                 gl.glEnable(GL.GL_LIGHTING);
320             }
321
322             Point centroid = t.centroid();
323             gl.glBegin(GL.GL_LINES);
324             gl.glColor3f(1, 1, 1);
325
326             if (triangles && errorNormals)
327                 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
328                     if (p.ok) {
329                         //gl.glDisable(GL.GL_LIGHTING);
330                         gl.glBegin(GL.GL_LINES);
331                         gl.glColor3f(1, 1, 1);
332                         p.p.glVertex(gl);
333                         p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
334                         //if (p.nearest_in_other_mesh != null) p.nearest_in_other_mesh.p.glVertex(gl);
335                         //tile.nearest(p).centroid().glVertex(gl);
336                         gl.glEnd();
337                         //gl.glEnable(GL.GL_LIGHTING);
338                     }
339                 }
340             gl.glEnd();
341             
342         }
343         }
344     }
345
346
347     //private JTextArea ocanvas = new JTextArea();
348     private JFrame f;
349     private GLCanvas glcanvas;
350     public MeshViewer(JFrame f) {
351         this.f = f;
352
353         GLCapabilities glcaps = new GLCapabilities();
354         glcanvas = new GLCanvas();
355         glcanvas.addGLEventListener(this);
356         f.add(glcanvas, BorderLayout.CENTER);
357
358         glcanvas.addMouseListener(this);
359         glcanvas.addMouseMotionListener(this);
360         glcanvas.addMouseWheelListener(this);
361         glcanvas.addKeyListener(this);
362     }
363     public void repaint() {
364         glcanvas.repaint();
365     }
366
367
368
369 }