From 2c711bea0246aca2caeace0a704a97d1305970d6 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 25 Nov 2007 20:51:02 -0800 Subject: [PATCH] add face normals darcs-hash:20071126045102-5007d-896a150e0e1f83e2bacbad532f32cbb8fbbb3c50.gz --- src/Geom.java | 63 ++++++++++++++++++++++++++++++++------------------------- src/Main.java | 20 ++++++++++++++---- 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/Geom.java b/src/Geom.java index 9c639e1..1817bea 100644 --- a/src/Geom.java +++ b/src/Geom.java @@ -7,6 +7,8 @@ import javax.media.opengl.glu.*; public class Geom implements Iterable { + public static float EPSILON = (float)0.000001; + private HashMap ps = new HashMap(); private HashMap es = new HashMap(); private HashSet ts = new HashSet(); @@ -29,14 +31,24 @@ public class Geom implements Iterable { 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; - return new T(e1, e2, e3); + T ret = new T(e1, e2, e3); + ts.add(ret); + return ret; } /** [UNIQUE] point in 3-space */ @@ -44,6 +56,7 @@ public class Geom implements Iterable { 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; @@ -56,16 +69,19 @@ public class Geom implements Iterable { 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; } - 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 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 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 */ @@ -74,6 +90,7 @@ public class Geom implements Iterable { 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; @@ -100,45 +117,35 @@ public class Geom implements Iterable { /** [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 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"); - ts.add(this); } 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; } } diff --git a/src/Main.java b/src/Main.java index 3acb5e0..bd40d13 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,12 +8,16 @@ public class Main implements GLEventListener { private Geom geom = new Geom(); + /** magnification factor */ + private static final float MAG = 1; + public Main(StlFile stlf) { for(int i=0; i