teapot works again
[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         private void sync() {
200             this.prev.next = this;
201             this.next.prev = this;
202             this.pair.pair = this;
203             if (this.next.p1 != p2) throw new Error();
204             if (this.prev.p2 != p1) throw new Error();
205             if (this.p1.e == null) this.p1.e = this;
206         }
207
208         public T makeT() { return t==null ? (t = new T(this)) : t; }
209
210         /** angle between this half-edge and the next */
211         public double angle() {
212             V v1 = next.p2.minus(p2);
213             V v2 = this.p1.minus(p2);
214             return Math.acos(v1.norm().dot(v2.norm()));
215         }
216
217         public void makeAdjacent(E e) {
218             if (this.next == e) return;
219             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
220             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
221
222             E freeIncident = p2.getFreeIncident(e, this);
223
224             e.prev.next = freeIncident.next;
225             freeIncident.next.prev = e.prev;
226
227             freeIncident.next = this.next;
228             this.next.prev = freeIncident;
229             
230             this.next = e;
231             e.prev = this;
232
233             sync();
234             freeIncident.sync();
235         }
236
237         /** creates an isolated edge out in the middle of space */
238         public E(P p1, P p2) {
239             if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
240             this.p1 = p1;
241             this.p2 = p2;
242             this.prev = this.next = this.pair = new E(this, this, this);
243             sync();
244         }
245
246         /** adds a new half-edge from prev.p2 to p2 */
247         public E(E prev, P p2) {
248             this.p1 = prev.p2;
249             this.p2 = p2;
250             this.prev = prev;
251             if (p2.getE(p1) != null) throw new Error();
252             if (p2.e==null) {
253                 this.next = this.pair = new E(this, this, prev.next);
254             } else {
255                 E q = p2.getFreeIncident();
256                 this.next = q.next;
257                 this.next.prev = this;
258                 E z = prev.next;
259                 this.prev.next = this;
260                 this.pair = new E(q, this, z);
261             }
262             sync();
263         }
264
265         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
266         public E(E prev, E pair, E next) {
267             this.p1 = prev.p2;
268             this.p2 = next.p1;
269             this.prev = prev;
270             this.next = next;
271             this.pair = pair;
272             sync();
273         }
274         public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2); }
275         public boolean has(P p) { return p==p1 || p==p2; }
276         public float length() { return p1.minus(p2).mag(); }
277         public String toString() { return p1+"->"+p2; }
278     }
279
280     /** [UNIQUE] a triangle (face) */
281     public final class T {
282         public final E e1;
283         public final int color;
284
285         public void bind(T t2, int rot) {
286             // FIXME
287         }
288
289         T(E e1) {
290             this.e1 = e1;
291             E e2 = e1.next;
292             E e3 = e2.next;
293             if (e1==e2     || e1==e3) throw new Error();
294             if (e3.next!=e1) throw new Error();
295             if (e1.t!=null || e2.t!=null || e3.t!=null)
296                 throw new Error("non-manifold surface or disagreeing normals");
297             e1.t = this;
298             e1.next.t = this;
299             e1.next.next.t = this;
300
301             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
302
303             int color = Math.abs(random.nextInt());
304             while(true) {
305                 color = color % 4;
306                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
307                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
308                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
309                 break;
310             }
311
312             // FIXME unnecssary
313             ts.add(this);
314
315             this.color = color;
316         }
317         public P p1() { return e1.p1; }
318         public P p2() { return e1.p2; }
319         public P p3() { return e1.next.p2; }
320         public E e1() { return e1; }
321         public E e2() { return e1.next; }
322         public E e3() { return e1.prev; }
323         public V norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
324         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
325         public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
326         public void glVertices(GL gl) {
327             p1().glVertex(gl);
328             p2().glVertex(gl);
329             p3().glVertex(gl);
330         }
331
332         public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
333                                           (p1().y+p2().y+p3().y)/3, 
334                                           (p1().z+p2().z+p3().z)/3); }
335         public float diameter() {
336             // FIXME: what is this supposed to be?
337             return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
338         }
339
340
341     }
342
343
344     /** matrix */
345     public class M {
346         public M() { }
347         public P apply(P p) { return p; }
348         public V apply(V v) { return v; }
349         public M invert() { return this; }
350         public M times(M m) { return this; }
351     }
352
353 }