got edge bindings right
[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
7 // FEATURE: check google's 3D warehouse for sample shapes
8
9 public class Main implements GLEventListener {
10
11     private Geom geom = new Geom();
12
13     /** magnification factor */
14     private static final float MAG = 1;
15
16     Geom.V[] translations;
17     Geom.P[] points;
18
19     public Main(StlFile stlf) {
20
21         Geom.P ltf = geom.newP(-0.2,  0.1,  0.1);
22         Geom.P mtf = geom.newP( 0.0,  0.1,  0.1);
23         Geom.P rtf = geom.newP( 0.2,  0.1,  0.1);
24         Geom.P ltn = geom.newP(-0.2,  0.1, -0.1);
25         Geom.P mtn = geom.newP( 0.0,  0.1, -0.1);
26         Geom.P rtn = geom.newP( 0.2,  0.1, -0.1);
27         Geom.P lbf = geom.newP(-0.2, -0.1,  0.1);
28         Geom.P mbf = geom.newP( 0.0, -0.1,  0.1);
29         Geom.P rbf = geom.newP( 0.2, -0.1,  0.1);
30         Geom.P lbn = geom.newP(-0.2, -0.1, -0.1);
31         Geom.P mbn = geom.newP( 0.0, -0.1, -0.1);
32         Geom.P rbn = geom.newP( 0.2, -0.1, -0.1);
33         
34         points = new Geom.P[] {
35             ltf,
36             mtf,
37             rtf,
38             ltn,
39             mtn,
40             rtn,
41             lbf,
42             mbf,
43             rbf,
44             lbn,
45             mbn,
46             rbn
47         };
48
49         translations = new Geom.V[] {
50             geom.new V(-0.2,  0.2,    0),
51             geom.new V( 0.2,  0.2,    0),
52             geom.new V(-0.2, -0.2,    0),
53             geom.new V( 0.2, -0.2,    0),
54             geom.new V( 0.4,    0,    0),
55             geom.new V(-0.4,    0,    0),
56             geom.new V(   0,    0,  0.2),
57             geom.new V(   0,    0, -0.2),
58         };
59
60         // top
61         geom.newT(ltf, mtf, mtn);
62         geom.newT(mtn, ltn, ltf);
63         geom.newT(mtf, rtf, rtn);
64         geom.newT(rtn, mtn, mtf);
65
66         // bottom (swap normals)
67         geom.newT(mbf, lbf, mbn);
68         geom.newT(lbn, mbn, lbf);
69         geom.newT(rbf, mbf, rbn);
70         geom.newT(mbn, rbn, mbf);
71         
72         // left
73         geom.newT(ltf, ltn, lbn);
74         geom.newT(lbn, lbf, ltf);
75
76         // right (swap normals)
77         geom.newT(rtn, rtf, rbn);
78         geom.newT(rbf, rbn, rtf);
79
80         // front
81         geom.newT(ltn, mtn, mbn);
82         geom.newT(ltn, mbn, lbn);
83         geom.newT(mtn, rtn, rbn);
84         geom.newT(mtn, rbn, mbn);
85
86         // back
87         geom.newT(mtf, ltf, mbf);
88         geom.newT(mbf, ltf, lbf);
89         geom.newT(rtf, mtf, rbf);
90         geom.newT(rbf, mtf, mbf);
91
92         for(Geom.V v : translations) {
93             for(Geom.T t1 : geom) {
94                 for(Geom.T t2 : geom) {
95                     if (t1==t2) continue;
96
97                     if ((t1.p1().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
98                         (t1.p2().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
99                         (t1.p3().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
100                         t1.e1().bind(t2.e3());
101                         t1.e2().bind(t2.e2());
102                         t1.e3().bind(t2.e1());
103                     }
104                     if ((t1.p2().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
105                         (t1.p3().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
106                         (t1.p1().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
107                         t1.e2().bind(t2.e3());
108                         t1.e3().bind(t2.e2());
109                         t1.e1().bind(t2.e1());
110                     }
111                     if ((t1.p3().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
112                         (t1.p1().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
113                         (t1.p2().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
114                         t1.e3().bind(t2.e3());
115                         t1.e1().bind(t2.e2());
116                         t1.e2().bind(t2.e1());
117                     }
118                 }
119             }
120         }
121
122         //Geom.P mid = geom.newE(ltf, lbn).shatter();
123
124         //tx.e2.shatter();
125         //tx.e3.shatter();
126
127         //mid.move(geom.new V((float)-0.05,0,0));
128
129         //mtf.move(geom.new V(0, (float)-0.05, (float)0.05));
130         mtf.move(geom.new V(0, (float)-0.05, (float)0.00));
131
132
133         /*
134         for(int i=0; i<stlf.coordArray.length; i+=3) {
135             Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
136             Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
137             Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
138             Geom.V n  = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
139             Geom.T t  = geom.newT(p0, p1, p2, n);
140         }
141         */
142
143
144     }
145
146     /**
147      * Take care of initialization here.
148      */
149     public void init(GLAutoDrawable gld) {
150         GL gl = gld.getGL();
151         GLU glu = new GLU();
152         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
153         gl.glClearDepth(1.0);
154         gl.glViewport(0, 0, 500, 300);
155         gl.glMatrixMode(GL.GL_PROJECTION);
156         gl.glEnable(GL.GL_DEPTH_TEST);
157         gl.glDepthFunc(GL.GL_LEQUAL);
158         
159         gl.glLoadIdentity();
160         gl.glRotatef((float)45, 1, 1, 1);
161
162         //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
163         display(gld);
164     }
165
166     public static int angle = 0;
167
168     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
169     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
170     public void display(GLAutoDrawable drawable) {
171         GL gl = drawable.getGL();
172         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
173         gl.glPointSize(5.0f);
174         gl.glLoadIdentity();
175         gl.glRotatef(angle, 1, 1, 1);
176
177         gl.glBegin(GL.GL_TRIANGLES);
178         draw(gl, true);
179         gl.glEnd();
180         /*        
181         for(Geom.V v1 : translations) {
182             if (v1.z==0 && v1.y==0) continue;
183             Geom.V v = v1.times((float)1.1);
184             gl.glTranslatef(v.x, v.y, v.z);
185             //draw(gl, false);
186             gl.glTranslatef(-v.x, -v.y, -v.z);
187         }
188         */
189     }
190
191     private void draw(GL gl, boolean triangles) {
192         float red = 0.0f;
193         float green = 0.0f;
194         float blue = 0.0f;
195         for(Geom.T t : geom) {
196             if (red < 0.15) red = 1.0f;
197             if (green < 0.15) green = 1.0f;
198             if (blue < 0.15) blue = 1.0f;
199             red -= .09f;
200             green -= .12f;
201             blue -= .15f;
202             gl.glColor3f(red, green, blue);
203             switch(t.color) {
204                 case 0: gl.glColor3f((float)0.25, (float)0.25, (float)0.75); break;
205                 case 1: gl.glColor3f((float)0.25, (float)0.75, (float)0.25); break;
206                 case 2: gl.glColor3f((float)0.75, (float)0.25, (float)0.25); break;
207                 case 3: gl.glColor3f((float)0.50, (float)0.50, (float)0.50); break;
208             }
209             //gl.glBegin(GL.GL_LINES);
210             if (triangles) {
211                 gl.glBegin(GL.GL_TRIANGLES);
212                 t.glVertices(gl);
213                 gl.glEnd();
214             } else {
215                 gl.glBegin(GL.GL_LINES);
216                 t.e1().p1.glVertex(gl);
217                 t.e1().p2.glVertex(gl);
218                 t.e2().p1.glVertex(gl);
219                 t.e2().p2.glVertex(gl);
220                 t.e3().p1.glVertex(gl);
221                 t.e3().p2.glVertex(gl);
222                 gl.glEnd();
223             }
224
225             Geom.P centroid = t.centroid();
226             gl.glBegin(GL.GL_LINES);
227             gl.glColor3f(1, 1, 1);
228             /*
229             centroid.glVertex(gl);
230             centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
231             */
232
233             t.p1().glVertex(gl);
234             t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
235             t.p2().glVertex(gl);
236             t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
237             t.p3().glVertex(gl);
238             t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
239
240             gl.glEnd();
241
242         }
243     }
244
245     public static void main(String[] s) throws Exception {
246         StlFile stlf = new StlFile();
247         stlf.load("teapot.stl");
248         Main main = new Main(stlf);
249         Frame f = new Frame();
250         GLCapabilities glcaps = new GLCapabilities();
251         GLCanvas glcanvas = new GLCanvas();
252         glcanvas.addGLEventListener(main);
253         f.add(glcanvas, BorderLayout.CENTER);
254         f.pack();
255         f.show();
256         f.setSize(900, 900);
257         f.doLayout();
258
259         while(true) {
260             Thread.sleep(100);
261             angle+=5;
262             glcanvas.repaint();
263         }
264
265     }
266
267 }