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