import javax.media.opengl.*;
import javax.media.opengl.glu.*;
-public class Geom {
+public class Geom implements Iterable<Geom.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>();
+
+ public Iterator<T> iterator() { return ts.iterator(); }
public P newP(float x, float y, float z) {
P p = new P(x, y, z);
P p2 = ps.get(p);
if (p2 != null) return p2;
- ps.put(p, p);
+ ps.put(p,p);
return p;
}
+
+ public E newE(P p1, P p2) {
+ E e = new E(p1, p2);
+ E e2 = es.get(e);
+ if (e2 != null) return e2;
+ es.put(e,e);
+ return e;
+ }
+
+ public T newT(E e1, E e2, E e3) {
+ 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);
+ }
/** point in 3-space */
- public class P {
+ public final class P {
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 null; }
Float.floatToIntBits(y) ^
Float.floatToIntBits(z);
}
+ public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
}
/** vector in 3-space */
- public class V {
+ 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 V norm() { return null; /* FIXME */ }
}
/** an edge */
- public class E {
+ public final class E {
public final P p1, p2;
- private T t1, t2;
+ T t1, t2;
public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
+ public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
public boolean equals(Object o) {
if (o==null || !(o instanceof E)) return false;
E e = (E)o;
if (this.p2 == e.p1 && this.p1 == e.p2) return true;
return false;
}
- public P shared(E e) { return null; }
+ public P shared(E e) {
+ if (p1==e.p1) return p1;
+ if (p1==e.p2) return p1;
+ if (p2==e.p1) return p2;
+ if (p2==e.p2) return p2;
+ throw new Error("no shared vertex in shared()");
+ }
+ public P unshared(E e) {
+ if (p1==e.p1) return p2;
+ if (p1==e.p2) return p2;
+ if (p2==e.p1) return p1;
+ if (p2==e.p2) return p1;
+ throw new Error("no shared vertex in unshared()");
+ }
}
/** a triangle (face) */
- public class T {
+ 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);
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 p3 = e3.shared(e1);
return p2.minus(p1).cross(p3.minus(p1)).norm();
}
- public T(E e1, E e2, E e3) { this.e1 = e1; this.e2 = e2; this.e3 = e3; }
+ public 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; }
+
+ // technically not required
+ /*
+ public int hashCode() { return e1.hashCode() ^ e2.hashCode() ^ e3.hashCode(); }
+ public boolean equals(Object o) {
+ if (o==null || !(o instanceof T)) return false;
+ T t = (T)o;
+ if (this.e1 == t.e1 && this.e2 == t.e2 && this.e3 == t.e3) return true;
+ if (this.e1 == t.e2 && this.e2 == t.e3 && this.e3 == t.e1) return true;
+ if (this.e1 == t.e3 && this.e2 == t.e1 && this.e3 == t.e2) return true;
+ return false;
+ }
+ */
+ public void glVertices(GL gl) {
+ e1.unshared(e2).glVertex(gl);
+ e1.shared(e2).glVertex(gl);
+ e2.shared(e3).glVertex(gl);
+ }
}
/** matrix */
public class Main implements GLEventListener {
- public static StlFile stlf = null;
+ private Geom geom = new Geom();
+
+ 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));
+ }
+ }
/**
* Take care of initialization here.
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;
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();
}
}
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();