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 /** ensures that e1.cross(e2).norm()==e2.cross(e3).norm()==e3.cross(e1).norm()==t.norm() */
35 public T newT(E e1, E e2, E e3, V norm) {
36 P p12 = e1.shared(e2);
37 P p23 = e2.shared(e3);
38 P p31 = e3.shared(e1);
39 V norm2 = p31.minus(p12).cross(p23.minus(p12));
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 private T t = null; // any of the triangles incident at this point
58 public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
59 public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
60 public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
61 public boolean equals(Object o) {
62 if (o==null || !(o instanceof P)) return false;
64 return p.x==x && p.y==y && p.z==z;
66 public int hashCode() {
68 Float.floatToIntBits(x) ^
69 Float.floatToIntBits(y) ^
70 Float.floatToIntBits(z);
72 public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
73 public String toString() { return "("+x+","+y+","+z+")"; }
75 if (t==null) throw new Error("attempt to get vertex normal for point which does not belong to any triangles");
77 V norm = new V(0, 0, 0);
79 norm = norm.plus(ti.norm().times((float)ti.angle(this)));
86 /** vector in 3-space */
87 public final class V {
88 private final float x, y, z;
89 public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
90 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); }
91 public V plus(V v) { return new V(x+v.x, y+v.y, z+v.z); }
92 public V norm() { float m = mag(); return new V(x/m, y/m, z/m); }
93 public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
94 public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
95 public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
96 public String toString() { return "<"+x+","+y+","+z+">"; }
99 /** [UNIQUE] an edge */
100 public final class E {
101 public final P p1, p2;
103 public E(P p1, P p2) {
104 if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
108 public int hashCode() { return p1.hashCode() ^ p2.hashCode(); }
109 public float length() { return p1.minus(p2).mag(); }
110 public boolean equals(Object o) {
111 if (o==null || !(o instanceof E)) return false;
113 if (this.p1 == e.p1 && this.p2 == e.p2) return true;
114 if (this.p2 == e.p1 && this.p1 == e.p2) return true;
117 public P shared(E e) {
118 if (p1==e.p1) return p1;
119 if (p1==e.p2) return p1;
120 if (p2==e.p1) return p2;
121 if (p2==e.p2) return p2;
122 throw new Error("no shared vertex in shared()");
124 public P unshared(E e) {
125 if (p1==e.p1) return p2;
126 if (p1==e.p2) return p2;
127 if (p2==e.p1) return p1;
128 if (p2==e.p2) return p1;
129 throw new Error("no shared vertex in unshared()");
131 public T other(T t) {
132 if (t1==t) return t2;
133 if (t2==t) return t1;
134 throw new Error("edge " + this + " does not own triangle " + t);
136 public P other(P p) {
137 if (p==p1) return p2;
138 if (p==p2) return p1;
139 throw new Error("edge " + this + " does not own point " + p);
143 /** [UNIQUE] a triangle (face) */
144 public final class T {
145 public final E e1, e2, e3;
146 T(E e1, E e2, E e3) {
147 if (e1.p1.t==null) e1.p1.t = this;
148 if (e1.p2.t==null) e1.p2.t = this;
149 if (e2.p1.t==null) e2.p1.t = this;
150 if (e2.p2.t==null) e2.p2.t = this;
151 if (e3.p1.t==null) e3.p1.t = this;
152 if (e3.p2.t==null) e3.p2.t = this;
153 if (e1==e2) throw new Error("attempt to create triangle with duplicate edge: " + e1);
154 if (e2==e3) throw new Error("attempt to create triangle with duplicate edge: " + e2);
155 if (e3==e1) throw new Error("attempt to create triangle with duplicate edge: " + e3);
156 // check that each pair of edges shares a vertex
163 // FEATURE: colinearity/sliverness check?
164 if (e1.t1 == null) e1.t1 = this; else if (e1.t2 == null) e1.t2 = this; else throw new Error("non-manifold surface");
165 if (e2.t1 == null) e2.t1 = this; else if (e2.t2 == null) e2.t2 = this; else throw new Error("non-manifold surface");
166 if (e3.t1 == null) e3.t1 = this; else if (e3.t2 == null) e3.t2 = this; else throw new Error("non-manifold surface");
167 // FIXME: check that triangles we share an edge with agree on the direction of the normal vector
168 // FIXME: check for sealed/watertight surface once construction is complete (an infer normal(s)?)
171 P p1 = e1.shared(e2);
172 P p2 = e2.shared(e3);
173 P p3 = e3.shared(e1);
174 return p2.minus(p1).cross(p3.minus(p1)).norm();
176 public boolean hasE(E e) { return e1==e || e2==e || e3==e; }
177 public void glVertices(GL gl) {
182 public P p1() { return e1.shared(e2); }
183 public P p2() { return e1.shared(e3); }
184 public P p3() { return e3.shared(e2); }
185 public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
186 (p1().y+p2().y+p3().y)/3,
187 (p1().z+p2().z+p3().z)/3); }
188 public float diameter() {
189 // FIXME: what is this supposed to be?
190 return Math.max(Math.max(e1.length(), e2.length()), e3.length()) / 2;
193 /** returns the next triangle walking clockwise around the vertex normal */
194 public T nextT(P p) { return prevE(p).other(this); }
195 public T prevT(P p) { return nextE(p).other(this); }
197 /** edge "after" this point, moving clockwise around the normal */
198 public E nextE(P p) {
199 if (p == e1.shared(e2)) return e1;
200 else if (p == e2.shared(e3)) return e2;
201 else if (p == e3.shared(e1)) return e3;
202 else throw new Error("triangle " + this + " does not own point " + p);
205 /** edge "before" this point, moving clockwise around the normal */
206 public E prevE(P p) {
207 if (p == e1.shared(e2)) return e2;
208 else if (p == e2.shared(e3)) return e3;
209 else if (p == e3.shared(e1)) return e1;
210 else throw new Error("triangle " + this + " does not own point " + p);
213 /** returns the angle at point p */
214 public double angle(P p) {
215 V v1 = firstEdge(p).other(p).minus(p);
216 V v2 = secondEdge(p).other(p).minus(p);
217 return Math.acos(v1.norm().dot(v2.norm()));