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                 if (t==this.t) continue;
281                 if (this.intersects(t)) good = false;
282             }
283             */
284             return good;
285         }
286         public boolean move(V v) {
287             M m = new M(v);
288             P p = this;
289             boolean good = true;
290             do {
291                 good &= p.transform(m);
292                 v = v.times(binding); // bleh wrong
293                 p = p.bound_to;
294             } while (p != this);
295             return good;
296         }
297
298         public E makeE(P p2) {
299             p2 = p2.register();
300             E e = getE(p2);
301             if (e != null) return e;
302             e = p2.getE(this);
303             if (this.e == null && p2.e == null) return this.e = new E(this, p2);
304             if (this.e == null && p2.e != null) return p2.makeE(this).pair;
305             return new E(getFreeIncident(), p2);
306         }
307
308         public E getFreeIncident() {
309             E ret = getFreeIncident(e, e);
310             if (ret != null) return ret;
311             ret = getFreeIncident(e.pair.next, e.pair.next);
312             if (ret == null) throw new Error("unable to find free incident to " + this);
313             return ret;
314         }
315
316         public E getFreeIncident(E start, E before) {
317             E e = start;
318             do {
319                 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
320                 e = e.pair.next;
321             } while(e != before);
322             return null;
323         }
324
325         public E getE(P p2) {
326             p2 = p2.register();
327             E e = this.e;
328             do {
329                 if (e==null) return null;
330                 if (e.p1 == this && e.p2 == p2) return e;
331                 e = e.pair.next;
332             } while (e!=this.e);
333             return null;
334         }
335
336         public boolean isBoundTo(P p) {
337             p = p.register();
338             P px = p;
339             do {
340                 if (px==this) return true;
341                 px = px.bound_to;
342             } while(px != p);
343             return false;
344         }
345
346         public void unbind() { bound_to = this; binding = new M(); }
347         public void bind(P p) { bind(p, new M()); }
348         public void bind(P p, M binding) {
349             p = p.register();
350             if (isBoundTo(p)) return;
351             P temp_bound_to = p.bound_to;
352             M temp_binding = p.binding;
353             p.bound_to = this.bound_to;
354             p.binding = binding.times(this.binding); // FIXME: may have order wrong here
355             this.bound_to = temp_bound_to;
356             this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
357         }
358
359         public P(float x, float y, float z) {
360             this.x = x; this.y = y; this.z = z;
361         }
362
363         public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
364         public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
365         public boolean equals(Object o) {
366             if (o==null || !(o instanceof P)) return false;
367             P p = (P)o;
368             return p.x==x && p.y==y && p.z==z;
369         }
370         // FIXME: moving a point alters its hashCode
371         public int hashCode() {
372             return
373                 Float.floatToIntBits(x) ^
374                 Float.floatToIntBits(y) ^
375                 Float.floatToIntBits(z);
376         }
377         public void glVertex(GL gl) {
378             this.times(aspect)._glVertex(gl);
379         }
380         private void _glVertex(GL gl) {
381             gl.glVertex3f(x, y, z);
382         }
383         public String toString() { return "("+x+","+y+","+z+")"; }
384         public V norm() {
385             V norm = new V(0, 0, 0);
386             E e = this.e;
387             do {
388                 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
389                 e = e.pair.next;
390             } while(e != this.e);
391             return norm.norm();
392         }
393     }
394
395     /** vector in 3-space */
396     public final class V {
397         public final float x, y, z;
398         public V(double x, double y, double z) { this((float)x, (float)y, (float)z); }
399         public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
400         public V(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
401         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); }
402         public V plus(V v) { return new V(x+v.x, y+v.y, z+v.z); }
403         public V norm() { return mag()==0 ? this : div(mag()); }
404         public V times(M m) { return m.apply(this); }
405         public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
406         public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
407         public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
408         public V div(float mag) { return new V(x/mag, y/mag, z/mag); }
409         public String toString() { return "<"+x+","+y+","+z+">"; }
410     }
411
412     public class BindingGroup {
413         public HashSet<E> es = new HashSet<E>();
414         public BindingGroup() { }
415         public BindingGroup(E e) {
416             es.add(e);
417         }
418         public void add(E e) {
419             if (e.bg != null) { merge(e.bg); return; }
420             es.add(e);
421             e.bg = this;
422         }
423         public void merge(BindingGroup bg) {
424             for(E e : bg.es) {
425                 e.bg = null;
426                 add(e);
427             }
428         }
429     }
430
431     /** [UNIQUE] an edge */
432     public final class E implements Comparable<E> {
433
434         public boolean intersects(T t) {
435             double A0=t.p1().x, A1=t.p1().y, A2=t.p1().z;
436             double B0=t.p2().x, B1=t.p2().y, B2=t.p2().z;
437             double C0=t.p3().x, C1=t.p3().y, C2=t.p3().z;
438             double j0=p1.x, j1=p1.y, j2=p1.z;
439             double k0=p2.x, k1=p2.y, k2=p2.z;
440             double J0, J1, J2;
441             double K0, K1, K2;
442             double i0, i1, i2;
443             double a0, a1, a2;
444             double b0, b1, b2;
445             double c0, c1, c2;
446             double in_det;
447             double R00, R01, R02, R03,
448                 R10, R11, R12, R13,
449                 R20, R21, R22, R23,
450                 R30, R31, R32, R33;
451
452
453             /* a = B - A */
454             a0 = B0 - A0; 
455             a1 = B1 - A1; 
456             a2 = B2 - A2;
457             /* b = C - B */
458             b0 = C0 - A0;
459             b1 = C1 - A1;
460             b2 = C2 - A2;
461             /* c = a &times; b */
462             c0 = a1 * b2 - a2 * b1;
463             c1 = a2 * b0 - a0 * b2;
464             c2 = a0 * b1 - a1 * b0;
465  
466             /* M^(-1) = (1/det(M)) * adj(M) */
467             in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
468             R00 = (b1 * c2 - b2 * c1) * in_det;
469             R01 = (b2 * c0 - b0 * c2) * in_det;
470             R02 = (b0 * c1 - b1 * c0) * in_det;
471             R10 = (c1 * a2 - c2 * a1) * in_det;
472             R11 = (c2 * a0 - c0 * a2) * in_det;
473             R12 = (c0 * a1 - c1 * a0) * in_det;
474             R20 = (c0) * in_det;
475             R21 = (c1) * in_det;
476             R22 = (c2) * in_det;
477   
478             /* O = M^(-1) * A */
479             R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
480             R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
481             R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
482  
483             /* fill in last row of 4x4 matrix */
484             R30 = R31 = R32 = 0;
485             R33 = 1;
486   
487             J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
488             K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
489             if (J2 * K2 >= 0) return false;
490
491             J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
492             K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
493             i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
494             if (i0 < 0 || i0 > 1) return false;
495   
496             J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
497             K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
498             i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
499             if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
500
501             return true;            
502         }
503
504         public int compareTo(E e) {
505             return e.length() > length() ? 1 : -1;
506         }
507
508         public final P p1, p2;
509         T t;     // triangle to our "left"
510         E prev;  // previous half-edge
511         E next;  // next half-edge
512         E pair;  // partner half-edge
513
514
515         public BindingGroup bg = new BindingGroup(this);
516
517         public void bind(E e) { bind(e, new M()); }
518         public void bind(E e, M m) { e.bg.add(this); }
519
520         public void dobind() {
521             if (bg==null) return;
522             for(E ex : bg.es) {
523                 if (ex==this) continue;
524                 p1.bind(ex.p1);
525                 p2.bind(ex.p2);
526             }
527         }
528
529         boolean shattered = false;
530         public P shatter() { return shatter(midpoint(), null, null); }
531         public P shatter(P mid, BindingGroup bg1, BindingGroup bg2) {
532             mid = mid.register();
533             if (shattered) return mid;
534             shattered = true;
535
536             P r = next.p2;
537             E next = this.next;
538             E prev = this.prev;
539
540             if (bg1==null) bg1 = new BindingGroup();
541             if (bg2==null) bg2 = new BindingGroup();
542             for(E e : bg.es) e.shatter(e.midpoint(), bg1, bg2);
543             pair.shatter();
544             destroy();
545
546             newT(r, p1, mid);
547             newT(r, mid, p2);
548             bg1.add(p1.getE(mid));
549             bg2.add(mid.getE(p2));
550             return mid;
551         }
552
553         public boolean destroyed = false;
554         public void destroy() {
555             if (destroyed) return;
556             destroyed = true;
557             pair.destroyed = true;
558             if (next.t != null) next.t.destroy();
559             if (prev.t != null) prev.t.destroy();
560             if (pair.next.t != null) ts.remove(pair.next.t);
561             if (pair.prev.t != null) ts.remove(pair.prev.t);
562             next.t = null;
563             prev.t = null;
564             pair.next.t = null;
565             pair.prev.t = null;
566             this.bg = null;
567             pair.bg = null;
568             pair.prev.next = next;
569             next.prev = pair.prev;
570             prev.next = pair.next;
571             pair.next = prev;
572             if (p1.e == this) p1.e = prev.next;
573             if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
574             es.remove(this);
575             es.remove(pair);
576             avgedge -= this.length();
577             avgedge -= pair.length();
578             numedges--;
579             numedges--;
580         }
581
582         private void sync() {
583             this.prev.next = this;
584             this.next.prev = this;
585             this.pair.pair = this;
586             if (this.next.p1 != p2) throw new Error();
587             if (this.prev.p2 != p1) throw new Error();
588             if (this.p1.e == null) this.p1.e = this;
589             es.add(this);
590             if (!added) {
591                 added = true;
592                 numedges++;
593                 avgedge += length();
594             }
595         }
596         private boolean added = false;
597
598         public T makeT() { return t==null ? (t = new T(this)) : t; }
599
600         /** angle between this half-edge and the next */
601         public double angle() {
602             V v1 = next.p2.minus(p2);
603             V v2 = this.p1.minus(p2);
604             return Math.acos(v1.norm().dot(v2.norm()));
605         }
606
607         public void makeAdjacent(E e) {
608             if (this.next == e) return;
609             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
610             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
611
612             E freeIncident = p2.getFreeIncident(e, this);
613
614             e.prev.next = freeIncident.next;
615             freeIncident.next.prev = e.prev;
616
617             freeIncident.next = this.next;
618             this.next.prev = freeIncident;
619             
620             this.next = e;
621             e.prev = this;
622
623             sync();
624             freeIncident.sync();
625         }
626
627         /** creates an isolated edge out in the middle of space */
628         public E(P p1, P p2) {
629             p1 = p1.register();
630             p2 = p2.register();
631             if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
632             this.p1 = p1;
633             this.p2 = p2;
634             this.prev = this.next = this.pair = new E(this, this, this);
635             sync();
636         }
637
638         /** adds a new half-edge from prev.p2 to p2 */
639         public E(E prev, P p2) {
640             p2 = p2.register();
641             this.p1 = prev.p2;
642             this.p2 = p2;
643             this.prev = prev;
644             if (p2.getE(p1) != null) throw new Error();
645             if (p2.e==null) {
646                 this.next = this.pair = new E(this, this, prev.next);
647             } else {
648                 E q = p2.getFreeIncident();
649                 this.next = q.next;
650                 this.next.prev = this;
651                 E z = prev.next;
652                 this.prev.next = this;
653                 this.pair = new E(q, this, z);
654             }
655             sync();
656         }
657
658         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
659         public E(E prev, E pair, E next) {
660             this.p1 = prev.p2;
661             this.p2 = next.p1;
662             this.prev = prev;
663             this.next = next;
664             this.pair = pair;
665             sync();
666         }
667         public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2).register(); }
668         public boolean has(P p) {
669             p = p.register();
670             return p==p1 || p==p2;
671         }
672         public float length() { return p1.minus(p2).mag(); }
673         public String toString() { return p1+"->"+p2; }
674     }
675
676     /** [UNIQUE] a triangle (face) */
677     public final class T {
678         public final E e1;
679         public final int color;
680
681         public void destroy() {
682             ts.remove(this);
683         }
684
685         public P nearest(P p) {
686             float d1 = p1().distance(p);
687             float d2 = p2().distance(p);
688             float d3 = p3().distance(p);
689             if (d1 < d2 && d1 < d3) return p1();
690             if (d2 < d3) return p2();
691             return p3();
692         }
693
694         T(E e1) {
695             this.e1 = e1;
696             E e2 = e1.next;
697             E e3 = e2.next;
698             if (e1==e2     || e1==e3) throw new Error();
699             if (e3.next!=e1) throw new Error();
700             if (e1.t!=null || e2.t!=null || e3.t!=null)
701                 throw new Error("non-manifold surface or disagreeing normals");
702             e1.t = this;
703             e1.next.t = this;
704             e1.next.next.t = this;
705
706             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
707
708             int color = Math.abs(random.nextInt());
709             while(true) {
710                 color = color % 4;
711                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
712                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
713                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
714                 break;
715             }
716
717             // FIXME unnecssary
718             ts.add(this);
719             p1().kdinsert();
720             p2().kdinsert();
721             p3().kdinsert();
722
723             this.color = color;
724         }
725         public P p1() { return e1.p1; }
726         public P p2() { return e1.p2; }
727         public P p3() { return e1.next.p2; }
728         public E e1() { return e1; }
729         public E e2() { return e1.next; }
730         public E e3() { return e1.prev; }
731         public V norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
732         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
733         public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
734
735         public float area() {
736             return (float)Math.abs(0.5 * e1().length() * new V(p1(), p2()).norm().dot(new V(p2(), p3())));
737         }
738
739         public void glVertices(GL gl) {
740             p1().glVertex(gl);
741             p2().glVertex(gl);
742             p3().glVertex(gl);
743         }
744
745         public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
746                                           (p1().y+p2().y+p3().y)/3, 
747                                           (p1().z+p2().z+p3().z)/3); }
748         public float diameter() {
749             // FIXME: what is this supposed to be?
750             return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
751         }
752
753
754     }
755
756
757     /** matrix */
758     public class M {
759         //
760         //  [ a b c d ]   [ x ]
761         //  [ e f g h ]   [ y ]
762         //  [ i j k l ]   [ z ]
763         //  [ 0 0 0 1 ]   [ 1 ]
764         //
765         public final float a, b, c, d, e, f, g, h, i, j, k, l;
766         public M() { this(1); }
767         public M(float scale) {
768             a = f = k = scale;
769             l = h = d = e = b = i = c = j = g = 0;            
770         }
771         public M(float scalex, float scaley, float scalez) {
772             a = scalex;
773             f = scaley;
774             k = scalez;
775             l = h = d = e = b = i = c = j = g = 0;            
776         }
777         public M(V translate) {
778             d = translate.x; h = translate.y; l = translate.z;
779             a = f = k = 1;
780             b = c = e = g = i = j = 0;
781         }
782         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) {
783             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;
784             this.j = j; this.k = k; this.l = l;
785         }
786         public M times(float x) {
787             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);
788         }
789         public M(V axis, float angle) {
790             double q = Math.cos(angle);
791             double s = Math.sin(angle);
792             double t = 1.0 - q;
793             a = (float)(q + axis.x*axis.x*t);
794             f = (float)(q + axis.y*axis.y*t);
795             k = (float)(q + axis.z*axis.z*t);
796             double tmp1 = axis.x*axis.y*t;
797             double tmp2 = axis.z*s;
798             e = (float)(tmp1 + tmp2);
799             b = (float)(tmp1 - tmp2);
800             tmp1 = axis.x*axis.z*t;
801             tmp2 = axis.y*s;
802             i = (float)(tmp1 - tmp2);
803             c = (float)(tmp1 + tmp2);
804             tmp1 = axis.y*axis.z*t;
805             tmp2 = axis.x*s;
806             j = (float)(tmp1 + tmp2);
807             g = (float)(tmp1 - tmp2);
808             d = h = l = 0;
809         }
810         public P times(P p) {
811             return newP(a*p.x + b*p.y + c*p.z + d,
812                         e*p.x + f*p.y + g*p.z + h,
813                         i*p.x + j*p.y + k*p.z + l);
814         }
815         public P apply(P p) { return p; }
816         public V apply(V v) { return v; }
817         public M invert() { return this; }
818         public M times(M m) { return this; }
819     }
820
821     public void unbind() {
822
823         for(Geom.T t : this) {
824             t.p1().unbind();
825             t.p2().unbind();
826             t.p3().unbind();
827         }
828
829     }
830     public void bind() {
831         for(Geom.T t : this) {
832             t.e1().dobind();
833             t.e2().dobind();
834             t.e3().dobind();
835         }
836     }
837     public int numedges = 0;
838     public float avgedge = 0;
839 }