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