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