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