got edge bindings right
[anneal.git] / src / Geom.java
1 import java.awt.*;
2 import java.util.*;
3 import java.awt.event.*;
4 import javax.swing.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
7 import edu.wlu.cs.levy.CG.KDTree;
8
9 public class Geom implements Iterable<Geom.T> {
10
11     private KDTree kd = new KDTree(3);
12
13     public static float EPSILON = (float)0.000001;
14     public static Random random = new Random();
15
16     private HashMap<P,P> ps = new HashMap<P,P>();
17     private HashSet<T>   ts = new HashSet<T>();
18
19     public Iterator<T> iterator() { return ts.iterator(); }
20
21     public P newP(double x, double y, double z) { return newP((float)x, (float)y, (float)z); }
22     public P newP(float x, float y, float z) {
23         P p = new P(x, y, z);
24         P p2 = ps.get(p);
25         if (p2 != null) return p2;
26         ps.put(p,p);
27         p.name = allname++;
28         try { kd.insert(new double[]{p.x,p.y,p.z},p); } catch (Exception e) { throw new Error(e); }
29         return p;
30     }
31     
32     public T newT(P p12, P p23, P p31, V norm) {
33         V norm2 = p31.minus(p12).cross(p23.minus(p12));
34         float dot = norm.dot(norm2);
35         if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
36         if (dot < 0) { P p = p12; p12=p23; p23 = p; }
37         return newT(p12, p23, p31);
38     }
39
40     public T newT(P p1, P p2, P p3) {
41         E e12 = p1.makeE(p2);
42         E e23 = p2.makeE(p3);
43         E e31 = p3.makeE(p1);
44         while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
45             e12.makeAdjacent(e23);
46             e23.makeAdjacent(e31);
47             e31.makeAdjacent(e12);
48         }
49         T ret = e12.makeT();
50         if (e12.t == null) throw new Error();
51         if (e23.t == null) throw new Error();
52         if (e31.t == null) throw new Error();
53         return ret;
54     }
55
56     private char allname = 'A';
57
58     /** [UNIQUE] point in 3-space */
59     public final class P {
60         char name;
61
62         float x, y, z;
63
64         private E e;                // some edge *leaving* this point
65         private M binding = new M();
66         private P bound_to = this;
67
68         public E makeE(P p2) {
69             E e = getE(p2);
70             if (e != null) return e;
71             e = p2.getE(this);
72             if (this.e == null && p2.e == null) return this.e = new E(this, p2);
73             if (this.e == null && p2.e != null) return p2.makeE(this).pair;
74             return new E(getFreeIncident(), p2);
75         }
76
77         public E getFreeIncident() {
78             E ret = getFreeIncident(e, e);
79             if (ret != null) return ret;
80             ret = getFreeIncident(e.pair.next, e.pair.next);
81             if (ret == null) throw new Error("unable to find free incident to " + this);
82             return ret;
83         }
84
85         public E getFreeIncident(E start, E before) {
86             E e = start;
87             do {
88                 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
89                 e = e.pair.next;
90             } while(e != before);
91             return null;
92         }
93
94         public E getE(P p2) {
95             E e = this.e;
96             do {
97                 if (e==null) return null;
98                 if (e.p1 == this && e.p2 == p2) return e;
99                 e = e.pair.next;
100             } while (e!=this.e);
101             return null;
102         }
103
104         public boolean isBoundTo(P p) {
105             P px = p;
106             do {
107                 if (px==this) return true;
108                 px = px.bound_to;
109             } while(px != p);
110             return false;
111         }
112
113         public void unbind() { bound_to = null; binding = null; }
114         public void bind(P p) { bind(p, new M()); }
115         public void bind(P p, M binding) {
116             if (isBoundTo(p)) return;
117             P temp_bound_to = p.bound_to;
118             M temp_binding = p.binding;
119             p.bound_to = this.bound_to;
120             p.binding = binding.times(this.binding); // FIXME: may have order wrong here
121             this.bound_to = temp_bound_to;
122             this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
123         }
124
125         public void move(V v) {
126             P p = this;
127             do {
128                 p.x = p.x+v.x;
129                 p.y = p.y+v.y;
130                 p.z = p.z+v.z;
131                 v = v.times(binding);
132                 p = p.bound_to;
133             } while (p != this);
134         }
135
136         public P(float x, float y, float z) {
137             this.x = x; this.y = y; this.z = z;
138         }
139
140         public P nearest() {
141             Object[] results;
142             try { results = kd.nearest(new double[]{x,y,z},2); } catch (Exception e) { throw new Error(e); }
143             if (results[0] != this) throw new Error();
144             return (P)results[1];
145         }
146
147         public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
148         public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
149         public P times(M m) { return m.apply(this); }
150         public boolean equals(Object o) {
151             if (o==null || !(o instanceof P)) return false;
152             P p = (P)o;
153             return p.x==x && p.y==y && p.z==z;
154         }
155         // FIXME: moving a point alters its hashCode
156         public int hashCode() {
157             return
158                 Float.floatToIntBits(x) ^
159                 Float.floatToIntBits(y) ^
160                 Float.floatToIntBits(z);
161         }
162         public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
163         public String toString() { return "("+x+","+y+","+z+")"; }
164         public V norm() {
165             V norm = new V(0, 0, 0);
166             E e = this.e;
167             do {
168                 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
169                 e = e.pair.next;
170             } while(e != this.e);
171             return norm.norm();
172         }
173     }
174
175     /** vector in 3-space */
176     public final class V {
177         public final float x, y, z;
178         public V(double x, double y, double z) { this((float)x, (float)y, (float)z); }
179         public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
180         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); }
181         public V plus(V v) { return new V(x+v.x, y+v.y, z+v.z); }
182         public V norm() { return div(mag()); }
183         public V times(M m) { return m.apply(this); }
184         public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
185         public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
186         public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
187         public V div(float mag) { return new V(x/mag, y/mag, z/mag); }
188         public String toString() { return "<"+x+","+y+","+z+">"; }
189     }
190
191     /** [UNIQUE] an edge */
192     public final class E {
193         public final P p1, p2;
194         T t;     // triangle to our "left"
195         E prev;  // previous half-edge
196         E next;  // next half-edge
197         E pair;  // partner half-edge
198
199         public void bind(E e) { bind(e, new M()); }
200         public void bind(E e, M m) {
201             p1.bind(e.p2);
202             p2.bind(e.p1);
203         }
204
205         private void sync() {
206             this.prev.next = this;
207             this.next.prev = this;
208             this.pair.pair = this;
209             if (this.next.p1 != p2) throw new Error();
210             if (this.prev.p2 != p1) throw new Error();
211             if (this.p1.e == null) this.p1.e = this;
212         }
213
214         public T makeT() { return t==null ? (t = new T(this)) : t; }
215
216         /** angle between this half-edge and the next */
217         public double angle() {
218             V v1 = next.p2.minus(p2);
219             V v2 = this.p1.minus(p2);
220             return Math.acos(v1.norm().dot(v2.norm()));
221         }
222
223         public void makeAdjacent(E e) {
224             if (this.next == e) return;
225             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
226             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
227
228             E freeIncident = p2.getFreeIncident(e, this);
229
230             e.prev.next = freeIncident.next;
231             freeIncident.next.prev = e.prev;
232
233             freeIncident.next = this.next;
234             this.next.prev = freeIncident;
235             
236             this.next = e;
237             e.prev = this;
238
239             sync();
240             freeIncident.sync();
241         }
242
243         /** creates an isolated edge out in the middle of space */
244         public E(P p1, P p2) {
245             if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
246             this.p1 = p1;
247             this.p2 = p2;
248             this.prev = this.next = this.pair = new E(this, this, this);
249             sync();
250         }
251
252         /** adds a new half-edge from prev.p2 to p2 */
253         public E(E prev, P p2) {
254             this.p1 = prev.p2;
255             this.p2 = p2;
256             this.prev = prev;
257             if (p2.getE(p1) != null) throw new Error();
258             if (p2.e==null) {
259                 this.next = this.pair = new E(this, this, prev.next);
260             } else {
261                 E q = p2.getFreeIncident();
262                 this.next = q.next;
263                 this.next.prev = this;
264                 E z = prev.next;
265                 this.prev.next = this;
266                 this.pair = new E(q, this, z);
267             }
268             sync();
269         }
270
271         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
272         public E(E prev, E pair, E next) {
273             this.p1 = prev.p2;
274             this.p2 = next.p1;
275             this.prev = prev;
276             this.next = next;
277             this.pair = pair;
278             sync();
279         }
280         public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2); }
281         public boolean has(P p) { return p==p1 || p==p2; }
282         public float length() { return p1.minus(p2).mag(); }
283         public String toString() { return p1+"->"+p2; }
284     }
285
286     /** [UNIQUE] a triangle (face) */
287     public final class T {
288         public final E e1;
289         public final int color;
290
291         T(E e1) {
292             this.e1 = e1;
293             E e2 = e1.next;
294             E e3 = e2.next;
295             if (e1==e2     || e1==e3) throw new Error();
296             if (e3.next!=e1) throw new Error();
297             if (e1.t!=null || e2.t!=null || e3.t!=null)
298                 throw new Error("non-manifold surface or disagreeing normals");
299             e1.t = this;
300             e1.next.t = this;
301             e1.next.next.t = this;
302
303             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
304
305             int color = Math.abs(random.nextInt());
306             while(true) {
307                 color = color % 4;
308                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
309                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
310                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
311                 break;
312             }
313
314             // FIXME unnecssary
315             ts.add(this);
316
317             this.color = color;
318         }
319         public P p1() { return e1.p1; }
320         public P p2() { return e1.p2; }
321         public P p3() { return e1.next.p2; }
322         public E e1() { return e1; }
323         public E e2() { return e1.next; }
324         public E e3() { return e1.prev; }
325         public V norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
326         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
327         public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
328         public void glVertices(GL gl) {
329             p1().glVertex(gl);
330             p2().glVertex(gl);
331             p3().glVertex(gl);
332         }
333
334         public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
335                                           (p1().y+p2().y+p3().y)/3, 
336                                           (p1().z+p2().z+p3().z)/3); }
337         public float diameter() {
338             // FIXME: what is this supposed to be?
339             return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
340         }
341
342
343     }
344
345
346     /** matrix */
347     public class M {
348         public M() { }
349         public P apply(P p) { return p; }
350         public V apply(V v) { return v; }
351         public M invert() { return this; }
352         public M times(M m) { return this; }
353     }
354
355 }