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