da10ac99a225e95d672fbf0c5c3a7394c9959cda
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
1 package edu.berkeley.qfat;
2 import java.awt.*;
3 import java.util.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.media.opengl.*;
7 import javax.media.opengl.glu.*;
8 import edu.berkeley.qfat.geom.*;
9 import edu.wlu.cs.levy.CG.KDTree;
10 import edu.berkeley.qfat.geom.Point;
11
12 public class Mesh implements Iterable<Mesh.T> {
13
14     public static final float EPSILON = (float)0.0001;
15     public static final Random random = new Random();
16
17     private PointSet<Vert> pointset = new PointSet<Vert>();
18
19     public Iterable<Vert> vertices() { return pointset; }
20
21     public Iterable<E> edges() {
22         return
23             new Iterable<E>() {
24             public Iterator<E> iterator() {
25                 // HACK
26                 HashSet<E> hse = new HashSet<E>();
27                 for(T t : Mesh.this) {
28                     hse.add(t.e1());
29                     hse.add(t.e2());
30                     hse.add(t.e3());
31                     hse.add(t.e1().pair);
32                     hse.add(t.e2().pair);
33                     hse.add(t.e3().pair);
34                 }
35                 return hse.iterator();
36             } };
37     }
38
39     public Iterator<T> iterator() {
40         for(Vert v : pointset)
41             if (v.e != null && v.e.t != null)
42                 return new FaceIterator(v);
43         return new FaceIterator();
44     }
45
46     public Mesh score_against = null;
47     public double score = 0;
48     public float score() { return (float)score; }
49
50     public int numedges = 0;
51     public float avgedge = 0;
52
53     public void rebindPoints() {
54         // unbind all points
55         for(Mesh.T t : this) {
56             t.v1().unbind();
57             t.v2().unbind();
58             t.v3().unbind();
59         }
60         // ask edges to re-implement their bindings
61         for(Mesh.T t : this) {
62             t.e1().dobind();
63             t.e2().dobind();
64             t.e3().dobind();
65         }
66     }
67
68     public void unApplyQuadricToNeighborAll() {
69         HashSet<Vert> done = new HashSet<Vert>();
70         for(T t : this)
71             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
72                 if (done.contains(p)) continue;
73                 done.add(p);
74                 p.unApplyQuadricToNeighbor();
75             }
76     }
77     public void recomputeAllFundamentalQuadrics() {
78         HashSet<Vert> done = new HashSet<Vert>();
79         for(T t : this)
80             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
81                 if (done.contains(p)) continue;
82                 done.add(p);
83                 p.recomputeFundamentalQuadric();
84             }
85     }
86     public float applyQuadricToNeighborAll() {
87         int num = 0;
88         double dist = 0;
89         HashSet<Vert> done = new HashSet<Vert>();
90         for(T t : this)
91             for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
92                 if (done.contains(p)) continue;
93                 done.add(p);
94                 p.applyQuadricToNeighbor();
95                 
96             }
97         return (float)(dist/num);
98     }
99
100     public void transform(Matrix m) {
101         ArrayList<Vert> set = new ArrayList<Vert>();
102         for (Vert v : pointset)
103             set.add(v);
104         for(Vert v : set) v.transform(m);
105     }
106
107     public float volume() {
108         double total = 0;
109         for(T t : this) {
110             double area = t.area();
111             Vec origin_to_centroid = new Vec(new Point(0, 0, 0), t.centroid());
112             boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
113             double height = Math.abs(t.norm().dot(origin_to_centroid));
114             total += ((facingAway ? 1 : -1) * area * height) / 3.0;
115         }
116         return (float)total;
117     }
118
119     public void rebuildPointSet() { pointset.rebuild(); }
120     public Vec diagonal() { return pointset.diagonal(); }
121     public Point centroid() { return pointset.centroid(); }
122     public Vert nearest(Point p) { return pointset.nearest(p); }
123
124     public final class Vert extends HasPoint {
125         public Point p;
126         E e;                // some edge *leaving* this point
127
128         /** the nearest vertex in the "score_against" mesh */
129         Vert   nearest_in_other_mesh;
130         /** the number of vertices in the other mesh for which this is the nearest_in_other_mesh */
131         int    quadric_count;
132         /** the total error quadric (contributions from all vertices in other mesh for which this is nearest) */
133         Matrix quadric = Matrix.ZERO;
134
135         Vert bound_to = this;
136         Matrix binding = new Matrix();
137         float oldscore = 0;
138         boolean quadricStale = false;
139
140         public Matrix errorQuadric() { return quadric; }
141         public Point getPoint() { return p; }
142         public float score() { return oldscore; }
143
144         private Matrix fundamentalQuadric = null;
145         public Matrix fundamentalQuadric() {
146             if (fundamentalQuadric == null) recomputeFundamentalQuadric();
147             return fundamentalQuadric;
148         }
149
150         private Vert(Point p) {
151             this.p = p;
152             if (pointset.get(p) != null) throw new Error();
153             pointset.add(this);
154         }
155
156         private void glNormal(GL gl) {
157             Vec norm = norm();
158             gl.glNormal3f(norm.x, norm.y, norm.z);
159         }
160
161         public void recomputeFundamentalQuadric() {
162             //if (!quadricStale && fundamentalQuadric != null) return;
163             quadricStale = false;
164             unApplyQuadricToNeighbor();
165             Matrix m = Matrix.ZERO;
166             E e = this.e;
167             do {
168                 T t = e.t;
169                 m = m.plus(t.norm().fundamentalQuadric(t.centroid()));
170                 e = e.pair.next;
171             } while(e != this.e);
172             fundamentalQuadric = m;
173             applyQuadricToNeighbor();
174         }
175
176         public void unApplyQuadricToNeighbor() {
177             if (nearest_in_other_mesh == null) return;
178             if (fundamentalQuadric == null) return;
179             nearest_in_other_mesh.unComputeError();
180             nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.minus(fundamentalQuadric);
181             nearest_in_other_mesh.quadric_count--;
182             if (nearest_in_other_mesh.quadric_count==0)
183                 nearest_in_other_mesh.quadric = Matrix.ZERO;
184             nearest_in_other_mesh.computeError();
185             nearest_in_other_mesh = null;
186         }
187
188         public void applyQuadricToNeighbor() {
189             if (score_against == null) return;
190
191             Vert new_nearest = score_against.nearest(p);
192             if (nearest_in_other_mesh != null && new_nearest == nearest_in_other_mesh) return;
193
194             if (nearest_in_other_mesh != null) unApplyQuadricToNeighbor();
195             if (nearest_in_other_mesh != null) throw new Error();
196
197             nearest_in_other_mesh = new_nearest;
198                 
199             // don't attract to vertices that face the other way
200             if (nearest_in_other_mesh.e == null || nearest_in_other_mesh.norm().dot(norm()) < 0) {
201                 nearest_in_other_mesh = null;
202             } else {
203                 nearest_in_other_mesh.unComputeError();
204                 nearest_in_other_mesh.quadric = nearest_in_other_mesh.quadric.plus(fundamentalQuadric());
205                 nearest_in_other_mesh.quadric_count++;
206                 nearest_in_other_mesh.computeError();
207             }
208             reComputeError();
209         }
210
211         public void reComputeError() {
212             unComputeError();
213             computeError();
214         }
215         public void unComputeError() {
216             score -= oldscore;
217             oldscore = 0;
218         }
219         public void computeError() {
220             oldscore = quadric_count == 0 ? 0 : ((quadric.preAndPostMultiply(p) * 100) / quadric_count);
221             double ang = Math.abs(e.crossAngle());
222             if (ang < Math.PI * 0.2)
223                 oldscore += ((Math.PI*0.2) - ang) * 10;
224             //System.out.println(oldscore);
225             score += oldscore;
226         }
227
228         /** does NOT update bound pairs! */
229         public boolean transform(Matrix m) {
230             unApplyQuadricToNeighbor();
231             try {
232                 if (pointset.get(this.p)==null) throw new Error();
233                 pointset.remove(this);
234                 float newx = m.a*p.x + m.b*p.y + m.c*p.z + m.d;
235                 float newy = m.e*p.x + m.f*p.y + m.g*p.z + m.h;
236                 float newz = m.i*p.x + m.j*p.y + m.k*p.z + m.l;
237                 this.p = new Point(newx, newy, newz);
238                 pointset.add(this);
239             } catch (Exception e) {
240                 throw new RuntimeException(e);
241             }
242             applyQuadricToNeighbor();
243
244             // should recompute fundamental quadrics of all vertices sharing a face, but we defer...
245             E e = this.e;
246             do {
247                 e.p2.quadricStale = true;
248                 e = e.pair.next;
249             } while(e != this.e);
250
251             // FIXME: intersection test needed?
252             boolean good = true;
253             /*
254             for(T t : Mesh.this) {
255                 if (!good) break;
256                 e = this.e;
257                 do {
258                     if (!t.has(e.p1) && !t.has(e.p2) && e.intersects(t)) { good = false; break; }
259                     if (e.t != null) {
260                         if (!e.t.has(t.e1().p1) && !e.t.has(t.e1().p2) && t.e1().intersects(e.t)) { good = false; break; }
261                         if (!e.t.has(t.e2().p1) && !e.t.has(t.e2().p2) && t.e2().intersects(e.t)) { good = false; break; }
262                         if (!e.t.has(t.e3().p1) && !e.t.has(t.e3().p2) && t.e3().intersects(e.t)) { good = false; break; }
263                     }
264                     e = e.pair.next;
265                 } while(e != this.e);
266             }
267 */
268             return good;
269         }
270
271         public boolean move(Vec v) {
272             Matrix m = new Matrix(v);
273             Vert p = this;
274             boolean good = true;
275             do {
276                 good &= p.transform(m);
277                 p = p.bound_to;
278             } while (p != this);
279             return good;
280         }
281
282         public E getFreeIncident() {
283             E ret = getFreeIncident(e, e);
284             if (ret != null) return ret;
285             ret = getFreeIncident(e.pair.next, e.pair.next);
286             if (ret == null) throw new Error("unable to find free incident to " + this);
287             return ret;
288         }
289
290         public E getFreeIncident(E start, E before) {
291             E e = start;
292             do {
293                 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
294                 e = e.pair.next;
295             } while(e != before);
296             return null;
297         }
298
299         public E getE(Point p2) {
300             Vert v = pointset.get(p2);
301             if (v==null) return null;
302             return getE(v);
303         }
304         public E getE(Vert p2) {
305             E e = this.e;
306             do {
307                 if (e==null) return null;
308                 if (e.p1 == this && e.p2 == p2) return e;
309                 e = e.pair.next;
310             } while (e!=this.e);
311             return null;
312         }
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
324         public boolean isBoundTo(Vert p) {
325             Vert px = p;
326             do {
327                 if (px==this) return true;
328                 px = px.bound_to;
329             } while(px != p);
330             return false;
331         }
332         public void unbind() { bound_to = this; binding = new Matrix(); }
333         public void bind(Vert p) { bind(p, new Matrix()); }
334         public void bind(Vert p, Matrix binding) {
335             if (isBoundTo(p)) return;
336             Vert temp_bound_to = p.bound_to;
337             Matrix temp_binding = p.binding;
338             p.bound_to = this.bound_to;
339             p.binding = binding.times(this.binding); // FIXME: may have order wrong here
340             this.bound_to = temp_bound_to;
341             this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
342         }
343     }
344
345     public class BindingGroup {
346         private HashSet<E> set = new HashSet<E>();
347         public BindingGroup bind_others;
348         public BindingGroup other() { return bind_others; }
349         public BindingGroup(BindingGroup bind_others) { this.bind_others = bind_others; }
350         public BindingGroup() { this.bind_others = new BindingGroup(this); }
351         public BindingGroup(E e) { this(); set.add(e); }
352         public void add(E e) {
353             if (set.contains(e)) return;
354             set.add(e);
355             BindingGroup e_bind_peers = e.bind_peers;
356             BindingGroup e_bind_to    = e.bind_to;
357             e.bind_peers = this;
358             e.bind_to    = bind_others;
359             for (E epeer  : e_bind_peers.set) add(epeer);
360             for (E eother : e_bind_to.set)    bind_others.add(eother);
361
362             for(E eother : bind_others.set) {
363                 if (e.next.bind_to.set.contains(eother.prev)) {
364                     e.next.next.bindEdge(eother.prev.prev);
365                 }
366                 if (e.prev.bind_to.set.contains(eother.next)) {
367                     e.prev.prev.bindEdge(eother.next.next);
368                 }
369             }
370
371         }
372         public void dobind(E e) {
373             for(E ebound : set) {
374                 e.p1.bind(ebound.p2);
375                 e.p2.bind(ebound.p1);
376             }
377         }
378         public void shatter(BindingGroup bg1, BindingGroup bg2) {
379             for(E e : set) {
380                 e.shatter(e.midpoint(), bg1, bg2);
381             }
382         }
383     }
384
385     /** [UNIQUE] an edge */
386     public final class E implements Comparable<E> {
387
388         public final Vert p1, p2;
389         T t;     // triangle to our "left"
390         E prev;  // previous half-edge
391         E next;  // next half-edge
392         E pair;  // partner half-edge
393         public BindingGroup bind_peers  = new BindingGroup(this);
394         public BindingGroup bind_to     = bind_peers.other();
395         boolean shattered = false;
396
397         public int compareTo(E e) { return e.length() > length() ? 1 : -1; }
398         public void bindEdge(E e) { bind_to.add(e); }
399         public void dobind() { bind_to.dobind(this); }
400
401         public Point shatter() { return shatter(midpoint(), null, null); }
402         public Point shatter(Point mid, BindingGroup bg1, BindingGroup bg2) {
403             if (shattered || destroyed) return mid;
404             shattered = true;
405
406             Vert r = next.p2;
407             E next = this.next;
408             E prev = this.prev;
409
410             int old_colorclass = t==null ? 0 : t.colorclass;
411             if (bg1==null) bg1 = new BindingGroup();
412             if (bg2==null) bg2 = new BindingGroup();
413             BindingGroup old_bind_to = bind_to;
414             bind_peers.shatter(bg1, bg2);
415             old_bind_to.shatter(bg2.other(), bg1.other());
416             pair.shatter();
417             destroy();
418
419             newT(r.p, p1.p, mid, null, old_colorclass);
420             newT(r.p, mid, p2.p, null, old_colorclass);
421             bg1.add(p1.getE(mid));
422             bg2.add(p2.getE(mid).pair);
423             return mid;
424         }
425
426         public boolean destroyed = false;
427         public void destroy() {
428             if (destroyed) return;
429             destroyed = true;
430             pair.destroyed = true;
431             if (next.t != null) next.t.destroy();
432             if (prev.t != null) prev.t.destroy();
433             next.t = null;
434             prev.t = null;
435             pair.next.t = null;
436             pair.prev.t = null;
437             this.bind_to = null;
438             pair.bind_to = null;
439             this.bind_peers = null;
440             pair.bind_peers = null;
441             pair.prev.next = next;
442             next.prev = pair.prev;
443             prev.next = pair.next;
444             pair.next = prev;
445             if (p1.e == this) p1.e = prev.next;
446             if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
447             avgedge -= this.length();
448             avgedge -= pair.length();
449             numedges--;
450             numedges--;
451         }
452
453         private void sync() {
454             this.prev.next = this;
455             this.next.prev = this;
456             this.pair.pair = this;
457             bind_peers.add(this);
458             if (this.next.p1 != p2) throw new Error();
459             if (this.prev.p2 != p1) throw new Error();
460             if (this.p1.e == null) this.p1.e = this;
461             if (!added) {
462                 added = true;
463                 numedges++;
464                 avgedge += length();
465             }
466         }
467         private boolean added = false;
468
469         public T makeT(int colorclass) { return t==null ? (t = new T(this, colorclass)) : t; }
470
471         public double crossAngle() {
472             Vec v1 = t.norm().times(-1);
473             Vec v2 = pair.t.norm().times(-1);
474             return Math.acos(v1.norm().dot(v2.norm()));
475         }
476
477         /** angle between this half-edge and the next */
478         public double angle() {
479             Vec v1 = next.p2.p.minus(p2.p);
480             Vec v2 = this.p1.p.minus(p2.p);
481             return Math.acos(v1.norm().dot(v2.norm()));
482         }
483
484         public void makeAdjacent(E e) {
485             if (this.next == e) return;
486             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
487             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
488
489             E freeIncident = p2.getFreeIncident(e, this);
490
491             e.prev.next = freeIncident.next;
492             freeIncident.next.prev = e.prev;
493
494             freeIncident.next = this.next;
495             this.next.prev = freeIncident;
496             
497             this.next = e;
498             e.prev = this;
499
500             sync();
501             freeIncident.sync();
502         }
503
504         /** creates an isolated edge out in the middle of space */
505         public E(Point p1, Point p2) {
506             if (pointset.get(p1) != null) throw new Error();
507             if (pointset.get(p2) != null) throw new Error();
508             this.p1 = new Vert(p1);
509             this.p2 = new Vert(p2);
510             this.prev = this.next = this.pair = new E(this, this, this);
511             this.p1.e = this;
512             this.p2.e = this.pair;
513             sync();
514         }
515
516         /** adds a new half-edge from prev.p2 to p2 */
517         public E(E prev, Point p) {
518             Vert p2;
519             p2 = pointset.get(p);
520             if (p2 == null) p2 = new Vert(p);
521             this.p1 = prev.p2;
522             this.p2 = p2;
523             this.prev = prev;
524             if (p2.getE(p1) != null) throw new Error();
525             if (p2.e==null) {
526                 this.next = this.pair = new E(this, this, prev.next);
527             } else {
528                 E q = p2.getFreeIncident();
529                 this.next = q.next;
530                 this.next.prev = this;
531                 E z = prev.next;
532                 this.prev.next = this;
533                 this.pair = new E(q, this, z);
534             }
535             if (p2.e==null) p2.e = this.pair;
536             sync();
537         }
538
539         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
540         public E(E prev, E pair, E next) {
541             this.p1 = prev.p2;
542             this.p2 = next.p1;
543             this.prev = prev;
544             this.next = next;
545             this.pair = pair;
546             sync();
547         }
548         public Point midpoint() { return new Point((p1.p.x+p2.p.x)/2, (p1.p.y+p2.p.y)/2, (p1.p.z+p2.p.z)/2); }
549         public boolean has(Vert v) { return v==p1 || v==p2; }
550         public float length() { return p1.p.minus(p2.p).mag(); }
551         public String toString() { return p1+"->"+p2; }
552
553         public boolean intersects(T t) {
554             double A0=t.v1().p.x, A1=t.v1().p.y, A2=t.v1().p.z;
555             double B0=t.v2().p.x, B1=t.v2().p.y, B2=t.v2().p.z;
556             double C0=t.v3().p.x, C1=t.v3().p.y, C2=t.v3().p.z;
557             double j0=p1.p.x, j1=p1.p.y, j2=p1.p.z;
558             double k0=p2.p.x, k1=p2.p.y, k2=p2.p.z;
559             double J0, J1, J2;
560             double K0, K1, K2;
561             double i0, i1, i2;
562             double a0, a1, a2;
563             double b0, b1, b2;
564             double c0, c1, c2;
565             double in_det;
566             double R00, R01, R02, R03,
567                 R10, R11, R12, R13,
568                 R20, R21, R22, R23,
569                 R30, R31, R32, R33;
570
571
572             /* a = B - A */
573             a0 = B0 - A0; 
574             a1 = B1 - A1; 
575             a2 = B2 - A2;
576             /* b = C - B */
577             b0 = C0 - A0;
578             b1 = C1 - A1;
579             b2 = C2 - A2;
580             /* c = a &times; b */
581             c0 = a1 * b2 - a2 * b1;
582             c1 = a2 * b0 - a0 * b2;
583             c2 = a0 * b1 - a1 * b0;
584  
585             /* M^(-1) = (1/det(M)) * adj(M) */
586             in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
587             R00 = (b1 * c2 - b2 * c1) * in_det;
588             R01 = (b2 * c0 - b0 * c2) * in_det;
589             R02 = (b0 * c1 - b1 * c0) * in_det;
590             R10 = (c1 * a2 - c2 * a1) * in_det;
591             R11 = (c2 * a0 - c0 * a2) * in_det;
592             R12 = (c0 * a1 - c1 * a0) * in_det;
593             R20 = (c0) * in_det;
594             R21 = (c1) * in_det;
595             R22 = (c2) * in_det;
596   
597             /* O = M^(-1) * A */
598             R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
599             R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
600             R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
601  
602             /* fill in last row of 4x4 matrix */
603             R30 = R31 = R32 = 0;
604             R33 = 1;
605   
606             J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
607             K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
608             if (J2 * K2 >= 0) return false;
609
610             J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
611             K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
612             i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
613             if (i0 < 0 || i0 > 1) return false;
614   
615             J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
616             K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
617             i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
618             if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
619
620             return true;            
621         }
622     }
623
624     public E makeE(Point p1, Point p2) {
625         Vert v1 = pointset.get(p1);
626         Vert v2 = pointset.get(p2);
627         if (v1 != null && v2 != null) {
628             E e = v1.getE(v2);
629             if (e != null) return e;
630             e = v2.getE(v1);
631             if (e != null) return e;
632         }
633         if (v1 != null) return new E(v1.getFreeIncident(), p2);
634         if (v2 != null) return new E(v2.getFreeIncident(), p1).pair;
635         return new E(p1, p2);
636     }
637     public T newT(Point p1, Point p2, Point p3, Vec norm, int colorclass) {
638         if (norm != null) {
639             Vec norm2 = p3.minus(p1).cross(p2.minus(p1));
640             float dot = norm.dot(norm2);
641             //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within evertsilon of each other: "+norm+" "+norm2);
642             if (dot < 0) { Point p = p1; p1=p2; p2 = p; }
643         }
644         E e12 = makeE(p1, p2);
645         E e23 = makeE(p2, p3);
646         E e31 = makeE(p3, p1);
647         while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
648             e12.makeAdjacent(e23);
649             e23.makeAdjacent(e31);
650             e31.makeAdjacent(e12);
651         }
652         T ret = e12.makeT(colorclass);
653         if (e12.t == null) throw new Error();
654         if (e23.t == null) throw new Error();
655         if (e31.t == null) throw new Error();
656         return ret;
657     }
658
659
660     public class FaceIterator implements Iterator<T> {
661         private HashSet<T> visited = new HashSet<T>();
662         private LinkedList<T> next = new LinkedList<T>();
663         public FaceIterator() { }
664         public FaceIterator(Vert v) { next.addFirst(v.e.t); }
665         public boolean hasNext() { return next.peek()!=null; }
666         public void remove() { throw new Error(); }
667         public T next() {
668             T ret = next.removeFirst();
669             if (ret == null) return null;
670             visited.add(ret);
671             T t1 = ret.e1().pair.t;
672             T t2 = ret.e2().pair.t;
673             T t3 = ret.e3().pair.t;
674             if (t1 != null && !visited.contains(t1)) next.addFirst(t1);
675             if (t2 != null && !visited.contains(t2)) next.addFirst(t2);
676             if (t3 != null && !visited.contains(t3)) next.addFirst(t3);
677             return ret;
678         }
679     }
680
681     /** [UNIQUE] a triangle (face) */
682     public final class T extends Triangle {
683         public final E e1;
684         public final int color;
685         public final int colorclass;
686
687         public void destroy() {
688         }
689
690         T(E e1, int colorclass) {
691             this.e1 = e1;
692             E e2 = e1.next;
693             E e3 = e2.next;
694             if (e1==e2 || e1==e3) throw new Error();
695             if (e3.next!=e1) throw new Error();
696             if (e1.t!=null || e2.t!=null || e3.t!=null) throw new Error("non-manifold surface or disagreeing normals");
697             e1.t = this;
698             e1.next.t = this;
699             e1.next.next.t = this;
700
701             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
702
703             int color = Math.abs(random.nextInt());
704             while(true) {
705                 color = color % 4;
706                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
707                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
708                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
709                 break;
710             }
711             this.color = color;
712             this.colorclass = colorclass;
713         }
714         public E e1() { return e1; }
715         public E e2() { return e1.next; }
716         public E e3() { return e1.prev; }
717         public Vert v1() { return e1.p1; }
718         public Vert v2() { return e1.p2; }
719         public Vert v3() { return e1.next.p2; }
720         public Point p1() { return e1.p1.p; }
721         public Point p2() { return e1.p2.p; }
722         public Point p3() { return e1.next.p2.p; }
723         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
724         public boolean has(Vert v) { return v1()==v || v2()==v || v3()==v; }
725
726         public void glVertices(GL gl) {
727             if (e1().bind_to.set.size() == 0) return;
728             if (e2().bind_to.set.size() == 0) return;
729             if (e3().bind_to.set.size() == 0) return;
730             norm().glNormal(gl);
731             p1().glVertex(gl);
732             p2().glVertex(gl);
733             p3().glVertex(gl);
734         }
735     }
736
737 }