checkpoint
[anneal.git] / src / Main.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
6 import java.util.*;
7
8 // FEATURE: check google's 3D warehouse for sample shapes
9
10 public class Main implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
11     
12     boolean alt = false;
13     boolean shift = false;
14     boolean control = false;
15
16     public void mouseWheelMoved(MouseWheelEvent e) {
17         tz -= e.getWheelRotation() ;
18     }
19
20     public void keyTyped(KeyEvent e)  { }
21     public void keyPressed(KeyEvent e)  {
22         switch(e.getKeyCode()) {
23             case KeyEvent.VK_CONTROL: control = true; break;
24             case KeyEvent.VK_ALT: alt = true; break;
25             case KeyEvent.VK_SHIFT: shift = true; break;
26         }
27     }
28     public void keyReleased(KeyEvent e) {
29         switch(e.getKeyCode()) {
30             case KeyEvent.VK_CONTROL: control = false; break;
31             case KeyEvent.VK_ALT: alt = false; break;
32             case KeyEvent.VK_SHIFT: shift = false; break;
33         }
34     }
35
36     public void mouseClicked(MouseEvent e) { }
37     public void mouseEntered(MouseEvent e) { }
38     public void mouseExited(MouseEvent e) { }
39     public void mousePressed(MouseEvent e) { }
40     public void mouseReleased(MouseEvent e) { }
41
42     int mousex;
43     int mousey;
44     public void mouseMoved(MouseEvent e) {
45         mousex = e.getX();
46         mousey = e.getY();
47     }
48
49     float tx = 0;
50     float ty = 0;
51     float tz = 0;
52     float anglex = 0;
53     float angley = 0;
54     public void mouseDragged(MouseEvent e) {
55         if (shift) {
56             tx += (mousex - e.getX())/(float)20;
57             ty += (mousey - e.getY())/(float)20;
58         } else {
59             anglex -= mousex - e.getX();
60             angley += mousey - e.getY();
61         }
62         mousex = e.getX();
63         mousey = e.getY();
64     }
65
66     private Geom geom = new Geom();
67
68     /** magnification factor */
69     private static final float MAG = 1;
70
71     Geom.V[] translations;
72     Geom.P[] points;
73
74     public Main(StlFile stlf) {
75
76         Geom.P ltf = geom.newP(-0.2,  0.1,  0.1);
77         Geom.P mtf = geom.newP( 0.0,  0.1,  0.1);
78         Geom.P rtf = geom.newP( 0.2,  0.1,  0.1);
79         Geom.P ltn = geom.newP(-0.2,  0.1, -0.1);
80         Geom.P mtn = geom.newP( 0.0,  0.1, -0.1);
81         Geom.P rtn = geom.newP( 0.2,  0.1, -0.1);
82         Geom.P lbf = geom.newP(-0.2, -0.1,  0.1);
83         Geom.P mbf = geom.newP( 0.0, -0.1,  0.1);
84         Geom.P rbf = geom.newP( 0.2, -0.1,  0.1);
85         Geom.P lbn = geom.newP(-0.2, -0.1, -0.1);
86         Geom.P mbn = geom.newP( 0.0, -0.1, -0.1);
87         Geom.P rbn = geom.newP( 0.2, -0.1, -0.1);
88         
89         points = new Geom.P[] {
90             ltf,
91             mtf,
92             rtf,
93             ltn,
94             mtn,
95             rtn,
96             lbf,
97             mbf,
98             rbf,
99             lbn,
100             mbn,
101             rbn
102         };
103
104         translations = new Geom.V[] {
105             geom.new V(-0.2,  0.2,    0),
106             geom.new V( 0.2,  0.2,    0),
107             geom.new V(-0.2, -0.2,    0),
108             geom.new V( 0.2, -0.2,    0),
109             geom.new V( 0.4,    0,    0),
110             geom.new V(-0.4,    0,    0),
111             geom.new V(   0,    0,  0.2),
112             geom.new V(   0,    0, -0.2),
113         };
114
115         // top
116         geom.newT(ltf, mtf, mtn);
117         geom.newT(mtn, ltn, ltf);
118         geom.newT(mtf, rtf, rtn);
119         geom.newT(rtn, mtn, mtf);
120
121         // bottom (swap normals)
122         geom.newT(mbf, lbf, mbn);
123         geom.newT(lbn, mbn, lbf);
124         geom.newT(rbf, mbf, rbn);
125         geom.newT(mbn, rbn, mbf);
126         
127         // left
128         geom.newT(ltf, ltn, lbn);
129         geom.newT(lbn, lbf, ltf);
130
131         // right (swap normals)
132         geom.newT(rtn, rtf, rbn);
133         geom.newT(rbf, rbn, rtf);
134
135         // front
136         geom.newT(ltn, mtn, mbn);
137         geom.newT(ltn, mbn, lbn);
138         geom.newT(mtn, rtn, rbn);
139         geom.newT(mtn, rbn, mbn);
140
141         // back
142         geom.newT(mtf, ltf, mbf);
143         geom.newT(mbf, ltf, lbf);
144         geom.newT(rtf, mtf, rbf);
145         geom.newT(rbf, mtf, mbf);
146
147         for(Geom.V v : translations) {
148             for(Geom.T t1 : geom) {
149                 for(Geom.T t2 : geom) {
150                     if (t1==t2) continue;
151
152                     if ((t1.p1().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
153                         (t1.p2().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
154                         (t1.p3().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
155                         t1.e1().bind(t2.e3().pair);
156                         t1.e2().bind(t2.e2().pair);
157                         t1.e3().bind(t2.e1().pair);
158                     }
159                     if ((t1.p2().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
160                         (t1.p3().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
161                         (t1.p1().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
162                         t1.e2().bind(t2.e3().pair);
163                         t1.e3().bind(t2.e2().pair);
164                         t1.e1().bind(t2.e1().pair);
165                     }
166                     if ((t1.p3().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
167                         (t1.p1().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
168                         (t1.p2().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
169                         t1.e3().bind(t2.e3().pair);
170                         t1.e1().bind(t2.e2().pair);
171                         t1.e2().bind(t2.e1().pair);
172                     }
173                 }
174             }
175         }
176
177         //Geom.P mid = geom.newE(ltf, lbn).shatter();
178
179         //tx.e2.shatter();
180         //tx.e3.shatter();
181
182         //mid.move(geom.new V((float)-0.05,0,0));
183
184         //mtf.move(geom.new V(0, (float)-0.05, (float)0.05));
185
186
187         for(Geom.T t : geom) {
188             t.e1().dobind();
189             t.e2().dobind();
190             t.e3().dobind();
191         }
192
193         for(int i=0; i<100; i++) {
194             rand();
195         }
196         
197
198
199         /*
200         for(int i=0; i<stlf.coordArray.length; i+=3) {
201             Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
202             Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
203             Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
204             Geom.V n  = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
205             Geom.T t  = geom.newT(p0, p1, p2, n);
206         }
207         */
208
209
210     }
211         Random random = new Random();
212
213     public void rand() {
214             int which = Math.abs(random.nextInt()) % points.length;
215             float r1 = Math.abs(random.nextFloat());
216             r1 = r1 - (float)Math.floor(r1);
217             r1 = r1 * (float)0.01;
218             r1 = r1 - (float)0.005;
219             switch(Math.abs(random.nextInt()) % 3) {
220                 case 0: points[which].move(geom.new V((float)0,  (float)r1, (float)0.00)); break;
221                 case 1: points[which].move(geom.new V((float)r1, (float)0,  (float)0.00)); break;
222                 case 2: points[which].move(geom.new V((float)0,  (float)0, (float)r1)); break;
223             }
224     }
225
226     /**
227      * Take care of initialization here.
228      */
229     public void init(GLAutoDrawable gld) {
230         GL gl = gld.getGL();
231         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
232         gl.glViewport(0, 0, 500, 300);
233         gl.glMatrixMode(GL.GL_PROJECTION);
234         gl.glEnable(GL.GL_DEPTH_TEST);
235         gl.glClearDepth(1.0);
236         gl.glDepthFunc(GL.GL_LEQUAL);
237         gl.glLoadIdentity();
238         //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
239         display(gld);
240     }
241
242     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
243     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
244     public void display(GLAutoDrawable drawable) {
245         GL gl = drawable.getGL();
246         GLU glu = new GLU();
247         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
248         gl.glPointSize(5.0f);
249         gl.glLoadIdentity();
250         glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
251         glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
252         gl.glTranslatef(tx/(float)20, ty/(float)20, 0);
253         gl.glRotatef(anglex, 0, 1, 0);
254         gl.glRotatef(angley, 1, 0, 0);
255
256         gl.glBegin(GL.GL_TRIANGLES);
257         draw(gl, true);
258         gl.glEnd();
259
260         for(Geom.V v1 : translations) {
261             if (v1.z==0 && v1.y==0) continue;
262             Geom.V v = v1.times((float)1.1);
263             gl.glTranslatef(v.x, v.y, v.z);
264             draw(gl, false);
265             gl.glTranslatef(-v.x, -v.y, -v.z);
266         }
267
268     }
269
270     private void draw(GL gl, boolean triangles) {
271         float red = 0.0f;
272         float green = 0.0f;
273         float blue = 0.0f;
274         for(Geom.T t : geom) {
275             if (red < 0.15) red = 1.0f;
276             if (green < 0.15) green = 1.0f;
277             if (blue < 0.15) blue = 1.0f;
278             red -= .09f;
279             green -= .12f;
280             blue -= .15f;
281             gl.glColor3f(red, green, blue);
282             switch(t.color) {
283                 case 0: gl.glColor3f((float)0.25, (float)0.25, (float)0.75); break;
284                 case 1: gl.glColor3f((float)0.25, (float)0.75, (float)0.25); break;
285                 case 2: gl.glColor3f((float)0.75, (float)0.25, (float)0.25); break;
286                 case 3: gl.glColor3f((float)0.50, (float)0.50, (float)0.50); break;
287             }
288             //gl.glBegin(GL.GL_LINES);
289             if (triangles) {
290                 gl.glBegin(GL.GL_TRIANGLES);
291                 t.glVertices(gl);
292                 gl.glEnd();
293             } else {
294                 gl.glBegin(GL.GL_LINES);
295                 t.e1().p1.glVertex(gl);
296                 t.e1().p2.glVertex(gl);
297                 t.e2().p1.glVertex(gl);
298                 t.e2().p2.glVertex(gl);
299                 t.e3().p1.glVertex(gl);
300                 t.e3().p2.glVertex(gl);
301                 gl.glEnd();
302             }
303
304             Geom.P centroid = t.centroid();
305             gl.glBegin(GL.GL_LINES);
306             gl.glColor3f(1, 1, 1);
307             /*
308             centroid.glVertex(gl);
309             centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
310             */
311             /*
312             t.p1().glVertex(gl);
313             t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
314             t.p2().glVertex(gl);
315             t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
316             t.p3().glVertex(gl);
317             t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
318             */
319             gl.glEnd();
320
321         }
322     }
323
324     public static void main(String[] s) throws Exception {
325         StlFile stlf = new StlFile();
326         stlf.load("teapot.stl");
327         Main main = new Main(stlf);
328         Frame f = new Frame();
329         GLCapabilities glcaps = new GLCapabilities();
330         GLCanvas glcanvas = new GLCanvas();
331         glcanvas.addGLEventListener(main);
332         f.add(glcanvas, BorderLayout.CENTER);
333         f.pack();
334         f.show();
335         f.setSize(900, 900);
336         f.doLayout();
337
338         glcanvas.addMouseListener(main);
339         glcanvas.addMouseMotionListener(main);
340         glcanvas.addMouseWheelListener(main);
341         glcanvas.addKeyListener(main);
342
343         while(true) {
344             Thread.sleep(100);
345             glcanvas.repaint();
346             main.rand();
347        }
348
349     }
350
351 }