checkpoint
[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     public class BindingGroup {
192         public HashSet<E> es = new HashSet<E>();
193         public BindingGroup() { }
194         public merge(E e) {
195             if (e.bg != null) merge(e.bg);
196             else { es.add(e); e.bg = this; }
197         }
198         public merge(BindingGroup bg) {
199             for(E e : bg.es) {
200                 e.bg = this;
201                 this.bg.add(e);
202             }
203         }
204     }
205
206     /** [UNIQUE] an edge */
207     public final class E {
208         public final P p1, p2;
209         T t;     // triangle to our "left"
210         E prev;  // previous half-edge
211         E next;  // next half-edge
212         E pair;  // partner half-edge
213
214
215         public BindingGroup bg = null;
216
217         public void bind(E e) { bind(e, new M()); }
218         public void bind(E e, M m) {
219             if (e.bg != null) e.bg.merge(this);
220             else if (bg != null) bg.merge(e);
221             else {
222                 bg = new BindingGroup();
223                 bg.merge(this);
224                 bg.merge(e);
225             }
226         }
227
228         public void dobind() {
229             if (bg==null) return;
230             for(E ex : bg.es) {
231                 if (ex==this) continue;
232                 p1.bind(ex.p1);
233                 p2.bind(ex.p2);
234             }
235         }
236
237         public boolean destroyed = false;
238         public void shatter() {
239             if (destroyed) return;
240             /*
241             HashSet<E> eh = new HashSet<E>();
242             eh.add(this);
243             for(E ex = bound_to; ex != this; ex = ex.bound_to) eh.add(ex);
244             E[] es = (E[])eh.toArray(new E[0]);
245             */
246             destroy();
247             pair.shatter();
248             for(E ex = bound_to; ex != this; ex = ex.bound_to) ex.shatter();
249         }
250         public void destroy() {
251             this.destroyed = true;
252         }
253
254         private void sync() {
255             this.prev.next = this;
256             this.next.prev = this;
257             this.pair.pair = this;
258             if (this.next.p1 != p2) throw new Error();
259             if (this.prev.p2 != p1) throw new Error();
260             if (this.p1.e == null) this.p1.e = this;
261         }
262
263         public T makeT() { return t==null ? (t = new T(this)) : t; }
264
265         /** angle between this half-edge and the next */
266         public double angle() {
267             V v1 = next.p2.minus(p2);
268             V v2 = this.p1.minus(p2);
269             return Math.acos(v1.norm().dot(v2.norm()));
270         }
271
272         public void makeAdjacent(E e) {
273             if (this.next == e) return;
274             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
275             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
276
277             E freeIncident = p2.getFreeIncident(e, this);
278
279             e.prev.next = freeIncident.next;
280             freeIncident.next.prev = e.prev;
281
282             freeIncident.next = this.next;
283             this.next.prev = freeIncident;
284             
285             this.next = e;
286             e.prev = this;
287
288             sync();
289             freeIncident.sync();
290         }
291
292         /** creates an isolated edge out in the middle of space */
293         public E(P p1, P p2) {
294             if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
295             this.p1 = p1;
296             this.p2 = p2;
297             this.prev = this.next = this.pair = new E(this, this, this);
298             sync();
299         }
300
301         /** adds a new half-edge from prev.p2 to p2 */
302         public E(E prev, P p2) {
303             this.p1 = prev.p2;
304             this.p2 = p2;
305             this.prev = prev;
306             if (p2.getE(p1) != null) throw new Error();
307             if (p2.e==null) {
308                 this.next = this.pair = new E(this, this, prev.next);
309             } else {
310                 E q = p2.getFreeIncident();
311                 this.next = q.next;
312                 this.next.prev = this;
313                 E z = prev.next;
314                 this.prev.next = this;
315                 this.pair = new E(q, this, z);
316             }
317             sync();
318         }
319
320         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
321         public E(E prev, E pair, E next) {
322             this.p1 = prev.p2;
323             this.p2 = next.p1;
324             this.prev = prev;
325             this.next = next;
326             this.pair = pair;
327             sync();
328         }
329         public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2); }
330         public boolean has(P p) { return p==p1 || p==p2; }
331         public float length() { return p1.minus(p2).mag(); }
332         public String toString() { return p1+"->"+p2; }
333     }
334
335     /** [UNIQUE] a triangle (face) */
336     public final class T {
337         public final E e1;
338         public final int color;
339
340         T(E e1) {
341             this.e1 = e1;
342             E e2 = e1.next;
343             E e3 = e2.next;
344             if (e1==e2     || e1==e3) throw new Error();
345             if (e3.next!=e1) throw new Error();
346             if (e1.t!=null || e2.t!=null || e3.t!=null)
347                 throw new Error("non-manifold surface or disagreeing normals");
348             e1.t = this;
349             e1.next.t = this;
350             e1.next.next.t = this;
351
352             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
353
354             int color = Math.abs(random.nextInt());
355             while(true) {
356                 color = color % 4;
357                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
358                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
359                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
360                 break;
361             }
362
363             // FIXME unnecssary
364             ts.add(this);
365
366             this.color = color;
367         }
368         public P p1() { return e1.p1; }
369         public P p2() { return e1.p2; }
370         public P p3() { return e1.next.p2; }
371         public E e1() { return e1; }
372         public E e2() { return e1.next; }
373         public E e3() { return e1.prev; }
374         public V norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
375         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
376         public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
377         public void glVertices(GL gl) {
378             p1().glVertex(gl);
379             p2().glVertex(gl);
380             p3().glVertex(gl);
381         }
382
383         public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
384                                           (p1().y+p2().y+p3().y)/3, 
385                                           (p1().z+p2().z+p3().z)/3); }
386         public float diameter() {
387             // FIXME: what is this supposed to be?
388             return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
389         }
390
391
392     }
393
394
395     /** matrix */
396     public class M {
397         public M() { }
398         public P apply(P p) { return p; }
399         public V apply(V v) { return v; }
400         public M invert() { return this; }
401         public M times(M m) { return this; }
402     }
403
404 }