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.0001;
14     public static Random random = new Random();
15
16     private HashMap<P,V>  ps = new HashMap<P,V>();
17     public  HashSet<E>    es = new HashSet<E>();
18     public  ArrayList<T>  ts = new ArrayList<T>();
19
20     public Iterator<T> iterator() { return ts.iterator(); }
21
22     public P origin() { return newP(0, 0, 0); }
23
24     public Geom score_against = null;
25     public double score = 0;
26     public float score() {
27         return (float)score;
28     }
29
30     public float rescore() {
31         int num = 0;
32         double dist = 0;
33         HashSet<P> done = new HashSet<P>();
34         for(T t : ts)
35             for(P p : new P[] { t.p1(), t.p2(), t.p3() }) {
36                 if (done.contains(p)) continue;
37                 done.add(p);
38                 p.rescore();
39             }
40         for(T t : ts)
41             for(P p : new P[] { t.p1(), t.p2(), t.p3() })
42                 p.kdremove();
43         kd = new KDTree(3);
44         for(T t : ts)
45             for(P p : new P[] { t.p1(), t.p2(), t.p3() })
46                 p.kdinsert();
47         return (float)(dist/num);
48     }
49
50     public void transform(M m) {
51         ArrayList<P> set = new ArrayList<P>();
52         set.addAll(ps.keySet());
53         for(P p : set) p.transform(m);
54     }
55
56     public Vec diagonal() {
57         float min_x = Float.MAX_VALUE;
58         float min_y = Float.MAX_VALUE;
59         float min_z = Float.MAX_VALUE;
60         float max_x = Float.MIN_VALUE;
61         float max_y = Float.MIN_VALUE;
62         float max_z = Float.MIN_VALUE;
63         for(P p : ps.keySet()) {
64             if (p.x < min_x) min_x = p.x;
65             if (p.y < min_y) min_y = p.y;
66             if (p.z < min_z) min_z = p.z;
67             if (p.x > max_x) max_x = p.x;
68             if (p.y > max_y) max_y = p.y;
69             if (p.z > max_z) max_z = p.z;
70         }
71         return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
72     }
73
74     public P centroid() {
75         float min_x = Float.MAX_VALUE;
76         float min_y = Float.MAX_VALUE;
77         float min_z = Float.MAX_VALUE;
78         float max_x = Float.MIN_VALUE;
79         float max_y = Float.MIN_VALUE;
80         float max_z = Float.MIN_VALUE;
81         for(P p : ps.keySet()) {
82             if (p.x < min_x) min_x = p.x;
83             if (p.y < min_y) min_y = p.y;
84             if (p.z < min_z) min_z = p.z;
85             if (p.x > max_x) max_x = p.x;
86             if (p.y > max_y) max_y = p.y;
87             if (p.z > max_z) max_z = p.z;
88         }
89         return new P((float)(max_x + min_x)/2,
90                      (float)(max_y + min_y)/2,
91                      (float)(max_z + min_z)/2);
92     }
93
94     public P newP(double x, double y, double z) { return newP((float)x, (float)y, (float)z); }
95     public P newP(float x, float y, float z) { return new P(x, y, z); }
96     
97     public T newT(P p12, P p23, P p31, Vec norm) {
98         Vec norm2 = p31.minus(p12).cross(p23.minus(p12));
99         float dot = norm.dot(norm2);
100         //if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
101         if (dot < 0) { P p = p12; p12=p23; p23 = p; }
102         return newT(p12, p23, p31);
103     }
104
105
106     public float volume() {
107         double total = 0;
108         for(T t : ts) {
109             double area = t.area();
110             Vec origin_to_centroid = new Vec(newP(0, 0, 0), t.centroid());
111             boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
112             double height = Math.abs(t.norm().dot(origin_to_centroid));
113             total += ((facingAway ? 1 : -1) * area * height) / 3.0;
114         }
115         return (float)total;
116     }
117
118     public P nearest(P p) {
119         Object[] results;
120         try { results = kd.nearest(new double[]{p.x,p.y,p.z},1); } catch (Exception e) { throw new Error(e); }
121         return (P)results[0];
122     }
123
124     public T newT(P p1, P p2, P p3) {
125         p1 = p1.register();
126         p2 = p2.register();
127         p3 = p3.register();
128         E e12 = p1.makeE(p2);
129         E e23 = p2.makeE(p3);
130         E e31 = p3.makeE(p1);
131         while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
132             e12.makeAdjacent(e23);
133             e23.makeAdjacent(e31);
134             e31.makeAdjacent(e12);
135         }
136         T ret = e12.makeT();
137         if (e12.t == null) throw new Error();
138         if (e23.t == null) throw new Error();
139         if (e31.t == null) throw new Error();
140         return ret;
141     }
142
143     public M aspect = new M();
144     public M invaspect = new M();
145
146     public final class V extends P {
147         public V(P p) {
148             super(p.x, p.y, p.z);
149             if (ps.get(p) != null) throw new Error();
150             ps.put(this, this);
151         }
152     }
153
154     public class P {
155         float x, y, z;
156
157         int watch_count;
158         float watch_x;
159         float watch_y;
160         float watch_z;
161         P watch;
162
163         private E e;                // some edge *leaving* this point
164         private M binding = new M();
165         private P bound_to = this;
166
167         private float oldscore = 0;
168         
169         private boolean inserted = false;
170
171         public V register() {
172             V v = ps.get(this);
173             if (v==null) v = new V(this);
174             return (V)v;
175         }
176
177         public void kdremove() {
178             if (!inserted) return;
179             inserted = false;
180             P p = this;
181             try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { }
182         }
183         public void kdinsert() {
184             if (inserted) return;
185             inserted = true;
186             P p = this;
187             try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); }
188         }
189
190         public float score() { return oldscore; }
191         public void unscore() {
192             if (watch == null) return;
193             watch.watch_x -= x;
194             watch.watch_y -= y;
195             watch.watch_z -= z;
196             watch.watch_count--;
197             if (watch.watch_count==0) {
198                 watch.watch_x = 0;
199                 watch.watch_y = 0;
200                 watch.watch_z = 0;
201             }
202             watch = null;
203         }
204         public P times(M m) { return m.times(this); }
205         public P partner() {
206             if (watch==null) return this;
207             return watch.times(score_against.aspect).times(invaspect);
208         }
209         public P watchback() {
210             if (watch_count==0) return partner();
211             return newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
212         }
213         public void rescore() {
214             if (score_against == null) return;
215
216             score -= oldscore;
217             oldscore = 0;
218
219             if (watch != null) unscore();
220             P po = this.times(aspect).times(score_against.invaspect);
221             if (watch == null) {
222                 watch = score_against.nearest(po);
223
224                 // don't attract to vertices that face the other way
225                 if (watch.norm().times(score_against.aspect).times(invaspect).dot(norm()) < 0) {
226                     watch = null;
227                 } else {
228                     watch.watch_x += po.x;
229                     watch.watch_y += po.y;
230                     watch.watch_z += po.z;
231                     watch.watch_count++;
232                 }
233             }
234
235             double s1, s2;
236             if (watch_count==0) s1 = 0;
237             else                s1 = this.distance(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
238             s2 = watch==null ? 0 : po.distance(watch);
239             oldscore = (float)(s1 + s2);
240             score += oldscore;
241         }
242
243         public float distance(P p) { return distance(p.x, p.y, p.z); }
244         public float distance(float ox, float oy, float oz) {
245             return 
246                 (float)Math.sqrt((x-ox)*(x-ox)+
247                                  (y-oy)*(y-oy)+
248                                  (z-oz)*(z-oz));
249         }
250
251         /** does NOT update bound pairs! */
252         public boolean transform(M m) {
253             // FIXME: screws up kdtree 
254             // FIXME: screws up hashmap
255             unscore();
256             try {
257                 if (ps.get(this)==null) throw new Error();
258                 ps.remove(this);
259                 float newx = m.a*x + m.b*y + m.c*z + m.d;
260                 float newy = m.e*x + m.f*y + m.g*z + m.h;
261                 float newz = m.i*x + m.j*y + m.k*z + m.l;
262                 this.x = newx;
263                 this.y = newy;
264                 this.z = newz;
265                 // FIXME: what if we move onto exactly where another point is?
266                 ps.put((V)this,(V)this);
267             } catch (Exception e) {
268                 throw new RuntimeException(e);
269             }
270             rescore();
271             boolean good = true;
272             /*
273             for(T t : ts) {
274                 for(E e = this.e; ;) {
275                     if (e.intersects(t)) { good = false; break; }
276                     e = e.pair.next;
277                     if (e == this.e) break;
278                 }
279             }
280             */
281             /*
282                 if (t==this.t) continue;
283                 if (this.intersects(t)) good = false;
284             }
285             */
286             return good;
287         }
288         public boolean move(Vec v) {
289             M m = new M(v);
290             P p = this;
291             boolean good = true;
292             do {
293                 good &= p.transform(m);
294                 v = v.times(binding); // bleh wrong
295                 p = p.bound_to;
296             } while (p != this);
297             return good;
298         }
299
300         public E makeE(P p2) {
301             p2 = p2.register();
302             E e = getE(p2);
303             if (e != null) return e;
304             e = p2.getE(this);
305             if (this.e == null && p2.e == null) return this.e = new E(this, p2);
306             if (this.e == null && p2.e != null) return p2.makeE(this).pair;
307             return new E(getFreeIncident(), p2);
308         }
309
310         public E getFreeIncident() {
311             E ret = getFreeIncident(e, e);
312             if (ret != null) return ret;
313             ret = getFreeIncident(e.pair.next, e.pair.next);
314             if (ret == null) throw new Error("unable to find free incident to " + this);
315             return ret;
316         }
317
318         public E getFreeIncident(E start, E before) {
319             E e = start;
320             do {
321                 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
322                 e = e.pair.next;
323             } while(e != before);
324             return null;
325         }
326
327         public E getE(P p2) {
328             p2 = p2.register();
329             E e = this.e;
330             do {
331                 if (e==null) return null;
332                 if (e.p1 == this && e.p2 == p2) return e;
333                 e = e.pair.next;
334             } while (e!=this.e);
335             return null;
336         }
337
338         public boolean isBoundTo(P p) {
339             p = p.register();
340             P px = p;
341             do {
342                 if (px==this) return true;
343                 px = px.bound_to;
344             } while(px != p);
345             return false;
346         }
347
348         public void unbind() { bound_to = this; binding = new M(); }
349         public void bind(P p) { bind(p, new M()); }
350         public void bind(P p, M binding) {
351             p = p.register();
352             if (isBoundTo(p)) return;
353             P temp_bound_to = p.bound_to;
354             M temp_binding = p.binding;
355             p.bound_to = this.bound_to;
356             p.binding = binding.times(this.binding); // FIXME: may have order wrong here
357             this.bound_to = temp_bound_to;
358             this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
359         }
360
361         public P(float x, float y, float z) {
362             this.x = x; this.y = y; this.z = z;
363         }
364
365         public Vec minus(P p) { return new Vec(x-p.x, y-p.y, z-p.z); }
366         public P plus(Vec v) { return newP(x+v.x, y+v.y, z+v.z); }
367         public boolean equals(Object o) {
368             if (o==null || !(o instanceof P)) return false;
369             P p = (P)o;
370             return p.x==x && p.y==y && p.z==z;
371         }
372         // FIXME: moving a point alters its hashCode
373         public int hashCode() {
374             return
375                 Float.floatToIntBits(x) ^
376                 Float.floatToIntBits(y) ^
377                 Float.floatToIntBits(z);
378         }
379         public void glVertex(GL gl) {
380             this.times(aspect)._glVertex(gl);
381         }
382         private void _glVertex(GL gl) {
383             gl.glVertex3f(x, y, z);
384         }
385         public String toString() { return "("+x+","+y+","+z+")"; }
386         public Vec norm() {
387             Vec norm = new Vec(0, 0, 0);
388             E e = this.e;
389             do {
390                 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
391                 e = e.pair.next;
392             } while(e != this.e);
393             return norm.norm();
394         }
395     }
396
397     /** vector in 3-space */
398     public final class Vec {
399         public final float x, y, z;
400         public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
401         public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
402         public Vec(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
403         public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
404         public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
405         public Vec norm() { return mag()==0 ? this : div(mag()); }
406         public Vec times(M m) { return m.apply(this); }
407         public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
408         public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
409         public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
410         public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
411         public String toString() { return "<"+x+","+y+","+z+">"; }
412     }
413
414     public class BindingGroup {
415         public HashSet<E> es = new HashSet<E>();
416         public BindingGroup() { }
417         public BindingGroup(E e) {
418             es.add(e);
419         }
420         public void add(E e) {
421             if (e.bg != null) { merge(e.bg); return; }
422             es.add(e);
423             e.bg = this;
424         }
425         public void merge(BindingGroup bg) {
426             for(E e : bg.es) {
427                 e.bg = null;
428                 add(e);
429             }
430         }
431     }
432
433     /** [UNIQUE] an edge */
434     public final class E implements Comparable<E> {
435
436         public boolean intersects(T t) {
437             double A0=t.p1().x, A1=t.p1().y, A2=t.p1().z;
438             double B0=t.p2().x, B1=t.p2().y, B2=t.p2().z;
439             double C0=t.p3().x, C1=t.p3().y, C2=t.p3().z;
440             double j0=p1.x, j1=p1.y, j2=p1.z;
441             double k0=p2.x, k1=p2.y, k2=p2.z;
442             double J0, J1, J2;
443             double K0, K1, K2;
444             double i0, i1, i2;
445             double a0, a1, a2;
446             double b0, b1, b2;
447             double c0, c1, c2;
448             double in_det;
449             double R00, R01, R02, R03,
450                 R10, R11, R12, R13,
451                 R20, R21, R22, R23,
452                 R30, R31, R32, R33;
453
454
455             /* a = B - A */
456             a0 = B0 - A0; 
457             a1 = B1 - A1; 
458             a2 = B2 - A2;
459             /* b = C - B */
460             b0 = C0 - A0;
461             b1 = C1 - A1;
462             b2 = C2 - A2;
463             /* c = a &times; b */
464             c0 = a1 * b2 - a2 * b1;
465             c1 = a2 * b0 - a0 * b2;
466             c2 = a0 * b1 - a1 * b0;
467  
468             /* M^(-1) = (1/det(M)) * adj(M) */
469             in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
470             R00 = (b1 * c2 - b2 * c1) * in_det;
471             R01 = (b2 * c0 - b0 * c2) * in_det;
472             R02 = (b0 * c1 - b1 * c0) * in_det;
473             R10 = (c1 * a2 - c2 * a1) * in_det;
474             R11 = (c2 * a0 - c0 * a2) * in_det;
475             R12 = (c0 * a1 - c1 * a0) * in_det;
476             R20 = (c0) * in_det;
477             R21 = (c1) * in_det;
478             R22 = (c2) * in_det;
479   
480             /* O = M^(-1) * A */
481             R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
482             R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
483             R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
484  
485             /* fill in last row of 4x4 matrix */
486             R30 = R31 = R32 = 0;
487             R33 = 1;
488   
489             J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
490             K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
491             if (J2 * K2 >= 0) return false;
492
493             J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
494             K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
495             i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
496             if (i0 < 0 || i0 > 1) return false;
497   
498             J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
499             K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
500             i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
501             if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
502
503             return true;            
504         }
505
506         public int compareTo(E e) {
507             return e.length() > length() ? 1 : -1;
508         }
509
510         public final P p1, p2;
511         T t;     // triangle to our "left"
512         E prev;  // previous half-edge
513         E next;  // next half-edge
514         E pair;  // partner half-edge
515
516
517         public BindingGroup bg = new BindingGroup(this);
518
519         public void bind(E e) { bind(e, new M()); }
520         public void bind(E e, M m) { e.bg.add(this); }
521
522         public void dobind() {
523             if (bg==null) return;
524             for(E ex : bg.es) {
525                 if (ex==this) continue;
526                 p1.bind(ex.p1);
527                 p2.bind(ex.p2);
528             }
529         }
530
531         boolean shattered = false;
532         public P shatter() { return shatter(midpoint(), null, null); }
533         public P shatter(P mid, BindingGroup bg1, BindingGroup bg2) {
534             mid = mid.register();
535             if (shattered) return mid;
536             shattered = true;
537
538             P r = next.p2;
539             E next = this.next;
540             E prev = this.prev;
541
542             if (bg1==null) bg1 = new BindingGroup();
543             if (bg2==null) bg2 = new BindingGroup();
544             for(E e : bg.es) e.shatter(e.midpoint(), bg1, bg2);
545             pair.shatter();
546             destroy();
547
548             newT(r, p1, mid);
549             newT(r, mid, p2);
550             bg1.add(p1.getE(mid));
551             bg2.add(mid.getE(p2));
552             return mid;
553         }
554
555         public boolean destroyed = false;
556         public void destroy() {
557             if (destroyed) return;
558             destroyed = true;
559             pair.destroyed = true;
560             if (next.t != null) next.t.destroy();
561             if (prev.t != null) prev.t.destroy();
562             if (pair.next.t != null) ts.remove(pair.next.t);
563             if (pair.prev.t != null) ts.remove(pair.prev.t);
564             next.t = null;
565             prev.t = null;
566             pair.next.t = null;
567             pair.prev.t = null;
568             this.bg = null;
569             pair.bg = null;
570             pair.prev.next = next;
571             next.prev = pair.prev;
572             prev.next = pair.next;
573             pair.next = prev;
574             if (p1.e == this) p1.e = prev.next;
575             if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
576             es.remove(this);
577             es.remove(pair);
578             avgedge -= this.length();
579             avgedge -= pair.length();
580             numedges--;
581             numedges--;
582         }
583
584         private void sync() {
585             this.prev.next = this;
586             this.next.prev = this;
587             this.pair.pair = this;
588             if (this.next.p1 != p2) throw new Error();
589             if (this.prev.p2 != p1) throw new Error();
590             if (this.p1.e == null) this.p1.e = this;
591             es.add(this);
592             if (!added) {
593                 added = true;
594                 numedges++;
595                 avgedge += length();
596             }
597         }
598         private boolean added = false;
599
600         public T makeT() { return t==null ? (t = new T(this)) : t; }
601
602         /** angle between this half-edge and the next */
603         public double angle() {
604             Vec v1 = next.p2.minus(p2);
605             Vec v2 = this.p1.minus(p2);
606             return Math.acos(v1.norm().dot(v2.norm()));
607         }
608
609         public void makeAdjacent(E e) {
610             if (this.next == e) return;
611             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
612             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
613
614             E freeIncident = p2.getFreeIncident(e, this);
615
616             e.prev.next = freeIncident.next;
617             freeIncident.next.prev = e.prev;
618
619             freeIncident.next = this.next;
620             this.next.prev = freeIncident;
621             
622             this.next = e;
623             e.prev = this;
624
625             sync();
626             freeIncident.sync();
627         }
628
629         /** creates an isolated edge out in the middle of space */
630         public E(P p1, P p2) {
631             p1 = p1.register();
632             p2 = p2.register();
633             if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
634             this.p1 = p1;
635             this.p2 = p2;
636             this.prev = this.next = this.pair = new E(this, this, this);
637             sync();
638         }
639
640         /** adds a new half-edge from prev.p2 to p2 */
641         public E(E prev, P p2) {
642             p2 = p2.register();
643             this.p1 = prev.p2;
644             this.p2 = p2;
645             this.prev = prev;
646             if (p2.getE(p1) != null) throw new Error();
647             if (p2.e==null) {
648                 this.next = this.pair = new E(this, this, prev.next);
649             } else {
650                 E q = p2.getFreeIncident();
651                 this.next = q.next;
652                 this.next.prev = this;
653                 E z = prev.next;
654                 this.prev.next = this;
655                 this.pair = new E(q, this, z);
656             }
657             sync();
658         }
659
660         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
661         public E(E prev, E pair, E next) {
662             this.p1 = prev.p2;
663             this.p2 = next.p1;
664             this.prev = prev;
665             this.next = next;
666             this.pair = pair;
667             sync();
668         }
669         public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2).register(); }
670         public boolean has(P p) {
671             p = p.register();
672             return p==p1 || p==p2;
673         }
674         public float length() { return p1.minus(p2).mag(); }
675         public String toString() { return p1+"->"+p2; }
676     }
677
678     /** [UNIQUE] a triangle (face) */
679     public final class T {
680         public final E e1;
681         public final int color;
682
683         public void destroy() {
684             ts.remove(this);
685         }
686
687         public P nearest(P p) {
688             float d1 = p1().distance(p);
689             float d2 = p2().distance(p);
690             float d3 = p3().distance(p);
691             if (d1 < d2 && d1 < d3) return p1();
692             if (d2 < d3) return p2();
693             return p3();
694         }
695
696         T(E e1) {
697             this.e1 = e1;
698             E e2 = e1.next;
699             E e3 = e2.next;
700             if (e1==e2     || e1==e3) throw new Error();
701             if (e3.next!=e1) throw new Error();
702             if (e1.t!=null || e2.t!=null || e3.t!=null)
703                 throw new Error("non-manifold surface or disagreeing normals");
704             e1.t = this;
705             e1.next.t = this;
706             e1.next.next.t = this;
707
708             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
709
710             int color = Math.abs(random.nextInt());
711             while(true) {
712                 color = color % 4;
713                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
714                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
715                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
716                 break;
717             }
718
719             // FIXME unnecssary
720             ts.add(this);
721             p1().kdinsert();
722             p2().kdinsert();
723             p3().kdinsert();
724
725             this.color = color;
726         }
727         public P p1() { return e1.p1; }
728         public P p2() { return e1.p2; }
729         public P p3() { return e1.next.p2; }
730         public E e1() { return e1; }
731         public E e2() { return e1.next; }
732         public E e3() { return e1.prev; }
733         public Vec norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
734         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
735         public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
736
737         public float area() {
738             return (float)Math.abs(0.5 * e1().length() * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
739         }
740
741         public void glVertices(GL gl) {
742             p1().glVertex(gl);
743             p2().glVertex(gl);
744             p3().glVertex(gl);
745         }
746
747         public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
748                                           (p1().y+p2().y+p3().y)/3, 
749                                           (p1().z+p2().z+p3().z)/3); }
750         public float diameter() {
751             // FIXME: what is this supposed to be?
752             return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
753         }
754
755
756     }
757
758
759     /** matrix */
760     public class M {
761         //
762         //  [ a b c d ]   [ x ]
763         //  [ e f g h ]   [ y ]
764         //  [ i j k l ]   [ z ]
765         //  [ 0 0 0 1 ]   [ 1 ]
766         //
767         public final float a, b, c, d, e, f, g, h, i, j, k, l;
768         public M() { this(1); }
769         public M(float scale) {
770             a = f = k = scale;
771             l = h = d = e = b = i = c = j = g = 0;            
772         }
773         public M(float scalex, float scaley, float scalez) {
774             a = scalex;
775             f = scaley;
776             k = scalez;
777             l = h = d = e = b = i = c = j = g = 0;            
778         }
779         public M(Vec translate) {
780             d = translate.x; h = translate.y; l = translate.z;
781             a = f = k = 1;
782             b = c = e = g = i = j = 0;
783         }
784         public M(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l) {
785             this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h; this.i = i;
786             this.j = j; this.k = k; this.l = l;
787         }
788         public M times(float x) {
789             return new M(a*x, b*x, c*x, d*x, e*x, f*x, g*x, h*x, i*x, j*x, k*x, l*x);
790         }
791         public M(Vec axis, float angle) {
792             double q = Math.cos(angle);
793             double s = Math.sin(angle);
794             double t = 1.0 - q;
795             a = (float)(q + axis.x*axis.x*t);
796             f = (float)(q + axis.y*axis.y*t);
797             k = (float)(q + axis.z*axis.z*t);
798             double tmp1 = axis.x*axis.y*t;
799             double tmp2 = axis.z*s;
800             e = (float)(tmp1 + tmp2);
801             b = (float)(tmp1 - tmp2);
802             tmp1 = axis.x*axis.z*t;
803             tmp2 = axis.y*s;
804             i = (float)(tmp1 - tmp2);
805             c = (float)(tmp1 + tmp2);
806             tmp1 = axis.y*axis.z*t;
807             tmp2 = axis.x*s;
808             j = (float)(tmp1 + tmp2);
809             g = (float)(tmp1 - tmp2);
810             d = h = l = 0;
811         }
812         public P times(P p) {
813             return newP(a*p.x + b*p.y + c*p.z + d,
814                         e*p.x + f*p.y + g*p.z + h,
815                         i*p.x + j*p.y + k*p.z + l);
816         }
817         public P apply(P p) { return p; }
818         public Vec apply(Vec v) { return v; }
819         public M invert() { return this; }
820         public M times(M m) { return this; }
821     }
822
823     public void unbind() {
824
825         for(Geom.T t : this) {
826             t.p1().unbind();
827             t.p2().unbind();
828             t.p3().unbind();
829         }
830
831     }
832     public void bind() {
833         for(Geom.T t : this) {
834             t.e1().dobind();
835             t.e2().dobind();
836             t.e3().dobind();
837         }
838     }
839     public int numedges = 0;
840     public float avgedge = 0;
841 }