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