add face normals
authoradam <adam@megacz.com>
Mon, 26 Nov 2007 04:51:02 +0000 (20:51 -0800)
committeradam <adam@megacz.com>
Mon, 26 Nov 2007 04:51:02 +0000 (20:51 -0800)
darcs-hash:20071126045102-5007d-896a150e0e1f83e2bacbad532f32cbb8fbbb3c50.gz

src/Geom.java
src/Main.java

index 9c639e1..1817bea 100644 (file)
@@ -7,6 +7,8 @@ import javax.media.opengl.glu.*;
 
 public class Geom implements Iterable<Geom.T> {
 
 
 public class Geom implements Iterable<Geom.T> {
 
+    public static float EPSILON = (float)0.000001;
+
     private HashMap<P,P> ps = new HashMap<P,P>();
     private HashMap<E,E> es = new HashMap<E,E>();
     private HashSet<T>   ts = new HashSet<T>();
     private HashMap<P,P> ps = new HashMap<P,P>();
     private HashMap<E,E> es = new HashMap<E,E>();
     private HashSet<T>   ts = new HashSet<T>();
@@ -29,14 +31,24 @@ public class Geom implements Iterable<Geom.T> {
         return e;
     }
 
         return e;
     }
 
-    public T newT(E e1, E e2, E e3) {
+    public T newT(E e1, E e2, E e3, V norm) {
+        P p1 = e1.shared(e2);
+        P p2 = e2.shared(e3);
+        P p3 = e3.shared(e1);
+        // FEATURE: colinearity check?
+        V norm2 = p2.minus(p1).cross(p3.minus(p1));
+        float dot = norm.dot(norm2);
+        if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
+        if (dot < 0) { E t = e1; e1 = e3; e2 = e2; e3 = t; }
         if (e1.t1 != null && e1.t1.hasE(e1) && e1.t1.hasE(e2) && e1.t1.hasE(e3)) return e1.t1;
         if (e1.t2 != null && e1.t2.hasE(e1) && e1.t2.hasE(e2) && e1.t2.hasE(e3)) return e1.t2;
         if (e2.t1 != null && e2.t1.hasE(e1) && e2.t1.hasE(e2) && e2.t1.hasE(e3)) return e2.t1;
         if (e2.t2 != null && e2.t2.hasE(e1) && e2.t2.hasE(e2) && e2.t2.hasE(e3)) return e2.t2;
         if (e3.t1 != null && e3.t1.hasE(e1) && e3.t1.hasE(e2) && e3.t1.hasE(e3)) return e3.t1;
         if (e3.t2 != null && e3.t2.hasE(e1) && e3.t2.hasE(e2) && e3.t2.hasE(e3)) return e3.t2;
         if (e1.t1 != null && e1.t1.hasE(e1) && e1.t1.hasE(e2) && e1.t1.hasE(e3)) return e1.t1;
         if (e1.t2 != null && e1.t2.hasE(e1) && e1.t2.hasE(e2) && e1.t2.hasE(e3)) return e1.t2;
         if (e2.t1 != null && e2.t1.hasE(e1) && e2.t1.hasE(e2) && e2.t1.hasE(e3)) return e2.t1;
         if (e2.t2 != null && e2.t2.hasE(e1) && e2.t2.hasE(e2) && e2.t2.hasE(e3)) return e2.t2;
         if (e3.t1 != null && e3.t1.hasE(e1) && e3.t1.hasE(e2) && e3.t1.hasE(e3)) return e3.t1;
         if (e3.t2 != null && e3.t2.hasE(e1) && e3.t2.hasE(e2) && e3.t2.hasE(e3)) return e3.t2;
-        return new T(e1, e2, e3);
+        T ret = new T(e1, e2, e3);
+        ts.add(ret);
+        return ret;
     }
 
     /** [UNIQUE] point in 3-space */
     }
 
     /** [UNIQUE] point in 3-space */
@@ -44,6 +56,7 @@ public class Geom implements Iterable<Geom.T> {
         public final float x, y, z;
         public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
         public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
         public final float x, y, z;
         public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
         public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
+        public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
         public boolean equals(Object o) {
             if (o==null || !(o instanceof P)) return false;
             P p = (P)o;
         public boolean equals(Object o) {
             if (o==null || !(o instanceof P)) return false;
             P p = (P)o;
@@ -56,16 +69,19 @@ public class Geom implements Iterable<Geom.T> {
                 Float.floatToIntBits(z);
         }
         public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
                 Float.floatToIntBits(z);
         }
         public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
+        public String toString() { return "("+x+","+y+","+z+")"; }
     }
 
     /** vector in 3-space */
     public final class V {
         private final float x, y, z;
         public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
     }
 
     /** vector in 3-space */
     public final class V {
         private final float x, y, z;
         public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
-        public boolean sameDirection(V v) { throw new Error(); }
         public V cross(V v) { return new V(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
         public V cross(V v) { return new V(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
-        public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
         public V norm() { float m = mag(); return new V(x/m, y/m, z/m); }
         public V norm() { float m = mag(); return new V(x/m, y/m, z/m); }
+        public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
+        public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
+        public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
+        public String toString() { return "<"+x+","+y+","+z+">"; }
     }
 
     /** [UNIQUE] an edge */
     }
 
     /** [UNIQUE] an edge */
@@ -74,6 +90,7 @@ public class Geom implements Iterable<Geom.T> {
         T t1, t2;
         public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
         public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
         T t1, t2;
         public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
         public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
+        public float length() { return p1.minus(p2).mag(); }
         public boolean equals(Object o) {
             if (o==null || !(o instanceof E)) return false;
             E e = (E)o;
         public boolean equals(Object o) {
             if (o==null || !(o instanceof E)) return false;
             E e = (E)o;
@@ -100,45 +117,35 @@ public class Geom implements Iterable<Geom.T> {
     /** [UNIQUE] a triangle (face) */
     public final class T {
         public final E e1, e2, e3;
     /** [UNIQUE] a triangle (face) */
     public final class T {
         public final E e1, e2, e3;
-        public T(E e1, E e2, E e3, V normal) {
-            P p1 = e1.shared(e2);
-            P p2 = e2.shared(e3);
-            P p3 = e3.shared(e1);
-            V norm = p2.minus(p1).cross(p3.minus(p1));
-            if (norm.sameDirection(normal)) {
-                this.e1 = e1;
-                this.e2 = e2;
-                this.e3 = e3;
-            } else {
-                this.e1 = e3;
-                this.e2 = e2;
-                this.e3 = e1;
-            }
-            if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
-            if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
-            if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
-            ts.add(this);
-        }
         public V norm() {
             P p1 = e1.shared(e2);
             P p2 = e2.shared(e3);
             P p3 = e3.shared(e1);
             return p2.minus(p1).cross(p3.minus(p1)).norm();
         }
         public V norm() {
             P p1 = e1.shared(e2);
             P p2 = e2.shared(e3);
             P p3 = e3.shared(e1);
             return p2.minus(p1).cross(p3.minus(p1)).norm();
         }
-        public T(E e1, E e2, E e3) {
+        T(E e1, E e2, E e3) {
             this.e1 = e1;
             this.e2 = e2;
             this.e3 = e3;
             if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
             if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
             if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
             this.e1 = e1;
             this.e2 = e2;
             this.e3 = e3;
             if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
             if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
             if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
-            ts.add(this);
         }
         public boolean hasE(E e) { return e1==e || e2==e || e3==e; }
         public void glVertices(GL gl) {
         }
         public boolean hasE(E e) { return e1==e || e2==e || e3==e; }
         public void glVertices(GL gl) {
-            e1.unshared(e2).glVertex(gl);
-            e1.shared(e2).glVertex(gl);
-            e2.shared(e3).glVertex(gl);
+            p1().glVertex(gl);
+            p2().glVertex(gl);
+            p3().glVertex(gl);
+        }
+        public P p1() { return e1.shared(e2); }
+        public P p2() { return e1.shared(e3); }
+        public P p3() { return e3.shared(e2); }
+        public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
+                                          (p1().y+p2().y+p3().y)/3, 
+                                          (p1().z+p2().z+p3().z)/3); }
+        public float diameter() {
+            // FIXME: what is this supposed to be?
+            return Math.max(Math.max(e1.length(), e2.length()), e3.length()) / 2;
         }
     }
 
         }
     }
 
index 3acb5e0..bd40d13 100644 (file)
@@ -8,12 +8,16 @@ public class Main implements GLEventListener {
 
     private Geom geom = new Geom();
 
 
     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) {
     public Main(StlFile stlf) {
         for(int i=0; i<stlf.coordArray.length; i+=3) {
-            Geom.P p0 = geom.newP(stlf.coordArray[i+0].x, stlf.coordArray[i+0].y, stlf.coordArray[i+0].z);
-            Geom.P p1 = geom.newP(stlf.coordArray[i+1].x, stlf.coordArray[i+1].y, stlf.coordArray[i+1].z);
-            Geom.P p2 = geom.newP(stlf.coordArray[i+2].x, stlf.coordArray[i+2].y, stlf.coordArray[i+2].z);
-            Geom.T t  = geom.newT(geom.newE(p0, p1), geom.newE(p1, p2), geom.newE(p2, p0));
+            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);
         }
     }
 
         }
     }
 
@@ -52,6 +56,14 @@ public class Main implements GLEventListener {
             gl.glBegin(GL.GL_TRIANGLES);
             t.glVertices(gl);
             gl.glEnd();
             gl.glBegin(GL.GL_TRIANGLES);
             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);
+            gl.glEnd();
+            
+
         }
     }
 
         }
     }