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