3 import java.awt.event.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
8 public class Geom implements Iterable<Geom.T> {
10 public static float EPSILON = (float)0.000001;
12 private HashMap<P,P> ps = new HashMap<P,P>();
13 private HashMap<E,E> es = new HashMap<E,E>();
14 private HashSet<T> ts = new HashSet<T>();
16 public Iterator<T> iterator() { return ts.iterator(); }
18 public P newP(float x, float y, float z) {
21 if (p2 != null) return p2;
26 public E newE(P p1, P p2) {
29 if (e2 != null) return e2;
34 public T newT(E e1, E e2, E e3, V norm) {
38 // FEATURE: colinearity check?
39 V norm2 = p2.minus(p1).cross(p3.minus(p1));
40 float dot = norm.dot(norm2);
41 if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
42 if (dot < 0) { E t = e1; e1 = e3; e2 = e2; e3 = t; }
43 if (e1.t1 != null && e1.t1.hasE(e1) && e1.t1.hasE(e2) && e1.t1.hasE(e3)) return e1.t1;
44 if (e1.t2 != null && e1.t2.hasE(e1) && e1.t2.hasE(e2) && e1.t2.hasE(e3)) return e1.t2;
45 if (e2.t1 != null && e2.t1.hasE(e1) && e2.t1.hasE(e2) && e2.t1.hasE(e3)) return e2.t1;
46 if (e2.t2 != null && e2.t2.hasE(e1) && e2.t2.hasE(e2) && e2.t2.hasE(e3)) return e2.t2;
47 if (e3.t1 != null && e3.t1.hasE(e1) && e3.t1.hasE(e2) && e3.t1.hasE(e3)) return e3.t1;
48 if (e3.t2 != null && e3.t2.hasE(e1) && e3.t2.hasE(e2) && e3.t2.hasE(e3)) return e3.t2;
49 T ret = new T(e1, e2, e3);
54 /** [UNIQUE] point in 3-space */
55 public final class P {
56 public final float x, y, z;
57 public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
58 public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
59 public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
60 public boolean equals(Object o) {
61 if (o==null || !(o instanceof P)) return false;
63 return p.x==x && p.y==y && p.z==z;
65 public int hashCode() {
67 Float.floatToIntBits(x) ^
68 Float.floatToIntBits(y) ^
69 Float.floatToIntBits(z);
71 public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
72 public String toString() { return "("+x+","+y+","+z+")"; }
75 /** vector in 3-space */
76 public final class V {
77 private final float x, y, z;
78 public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
79 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); }
80 public V norm() { float m = mag(); return new V(x/m, y/m, z/m); }
81 public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
82 public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
83 public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
84 public String toString() { return "<"+x+","+y+","+z+">"; }
87 /** [UNIQUE] an edge */
88 public final class E {
89 public final P p1, p2;
91 public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
92 public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
93 public float length() { return p1.minus(p2).mag(); }
94 public boolean equals(Object o) {
95 if (o==null || !(o instanceof E)) return false;
97 if (this.p1 == e.p1 && this.p2 == e.p2) return true;
98 if (this.p2 == e.p1 && this.p1 == e.p2) return true;
101 public P shared(E e) {
102 if (p1==e.p1) return p1;
103 if (p1==e.p2) return p1;
104 if (p2==e.p1) return p2;
105 if (p2==e.p2) return p2;
106 throw new Error("no shared vertex in shared()");
108 public P unshared(E e) {
109 if (p1==e.p1) return p2;
110 if (p1==e.p2) return p2;
111 if (p2==e.p1) return p1;
112 if (p2==e.p2) return p1;
113 throw new Error("no shared vertex in unshared()");
117 /** [UNIQUE] a triangle (face) */
118 public final class T {
119 public final E e1, e2, e3;
121 P p1 = e1.shared(e2);
122 P p2 = e2.shared(e3);
123 P p3 = e3.shared(e1);
124 return p2.minus(p1).cross(p3.minus(p1)).norm();
126 T(E e1, E e2, E e3) {
130 if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
131 if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
132 if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
134 public boolean hasE(E e) { return e1==e || e2==e || e3==e; }
135 public void glVertices(GL gl) {
140 public P p1() { return e1.shared(e2); }
141 public P p2() { return e1.shared(e3); }
142 public P p3() { return e3.shared(e2); }
143 public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
144 (p1().y+p2().y+p3().y)/3,
145 (p1().z+p2().z+p3().z)/3); }
146 public float diameter() {
147 // FIXME: what is this supposed to be?
148 return Math.max(Math.max(e1.length(), e2.length()), e3.length()) / 2;