From: adam Date: Mon, 26 Nov 2007 04:25:34 +0000 (-0800) Subject: checkpoint X-Git-Url: http://git.megacz.com/?p=anneal.git;a=commitdiff_plain;h=bb4936991545937dbde5d59a3fd230aeca7fb481 checkpoint darcs-hash:20071126042534-5007d-e179bb755aca414a3109ae51aa21dac8b5ac86b6.gz --- diff --git a/src/Geom.java b/src/Geom.java index c879bfe..944ee61 100644 --- a/src/Geom.java +++ b/src/Geom.java @@ -5,20 +5,42 @@ import javax.swing.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; -public class Geom { +public class Geom implements Iterable { private HashMap ps = new HashMap(); + private HashMap es = new HashMap(); + private HashSet ts = new HashSet(); + + public Iterator 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; } @@ -33,10 +55,11 @@ public class Geom { 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 */ } @@ -45,10 +68,11 @@ public class Geom { } /** 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; @@ -56,11 +80,24 @@ public class Geom { 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); @@ -76,6 +113,10 @@ public class Geom { 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); @@ -83,7 +124,34 @@ public class Geom { 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 */ diff --git a/src/Main.java b/src/Main.java index 1e496ea..64852c9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,7 +6,24 @@ import javax.media.opengl.glu.*; public class Main implements GLEventListener { - public static StlFile stlf = null; + private Geom geom = new Geom(); + + public Main(StlFile stlf) { + for(int i=0; i