add vertex normals
[anneal.git] / src / Main.java
index 1e496ea..f598ac1 100644 (file)
@@ -6,7 +6,20 @@ import javax.media.opengl.glu.*;
 
 public class Main implements GLEventListener {
 
-    public static StlFile stlf = null;
+    private Geom geom = new Geom();
+
+    /** magnification factor */
+    private static final float MAG = 1;
+
+    public Main(StlFile stlf) {
+        for(int i=0; i<stlf.coordArray.length; i+=3) {
+            Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
+            Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
+            Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
+            Geom.V n  = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
+            Geom.T t  = geom.newT(geom.newE(p0, p1), geom.newE(p1, p2), geom.newE(p2, p0), n);
+        }
+    }
 
     /**
      * Take care of initialization here.
@@ -22,9 +35,8 @@ public class Main implements GLEventListener {
         display(gld);
     }
 
-    /**
-     * Take care of drawing here.
-     */
+    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
+    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
     public void display(GLAutoDrawable drawable) {
         float red = 0.0f;
         float green = 0.0f;
@@ -33,7 +45,7 @@ public class Main implements GLEventListener {
         gl.glClear(GL.GL_COLOR_BUFFER_BIT);
         gl.glPointSize(5.0f);
 
-        for(int i=0; i<stlf.coordArray.length; i+=3) {
+        for(Geom.T t : geom) {
             red -= .09f;
             green -= .12f;
             blue -= .15f;
@@ -41,33 +53,28 @@ public class Main implements GLEventListener {
             if (green < 0.15) green = 1.0f;
             if (blue < 0.15) blue = 1.0f;
             gl.glColor3f(red, green, blue);
-
             gl.glBegin(GL.GL_TRIANGLES);
-            for(int j=0; j<3; j++)
-                gl.glVertex3f(stlf.coordArray[i+j].x,
-                              stlf.coordArray[i+j].y,
-                              stlf.coordArray[i+j].z);
+            t.glVertices(gl);
             gl.glEnd();
+
+            Geom.P centroid = t.centroid();
+            gl.glBegin(GL.GL_LINES);
+            /*
+            centroid.glVertex(gl);
+            centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
+            */
+            t.p1().glVertex(gl);
+            t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
+            gl.glEnd();
+            
+
         }
     }
-    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
-    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
-
 
     public static void main(String[] s) throws Exception {
-        stlf = new StlFile();
+        StlFile stlf = new StlFile();
         stlf.load("teapot.stl");
-
-        Main main = new Main();
-
-        Geom geom = new Geom();
-        for(int i=0; i<stlf.coordArray.length; i+=3) {
-            for(int j=0; j<3; j++)
-                geom.newP(stlf.coordArray[i+j].x,
-                          stlf.coordArray[i+j].y,
-                          stlf.coordArray[i+j].z);
-        }
-
+        Main main = new Main(stlf);
         Frame f = new Frame();
         GLCapabilities glcaps = new GLCapabilities();
         GLCanvas glcanvas = new GLCanvas();