64724a62a50b1a1d787dd82c6e42f16f51332bf6
[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.berkeley.qfat.geom.HasBindingGroup;
10 import edu.wlu.cs.levy.CG.KDTree;
11 import edu.berkeley.qfat.geom.Point;
12 import com.infomatiq.jsi.IntProcedure;
13
14 public class Mesh implements Iterable<Mesh.T> {
15
16     public static final float EPSILON = (float)0.0001;
17     public static final Random random = new Random();
18
19     private RTree<T>         triangles = new RTree<T>();
20     private PointSet<Vertex> vertices  = new PointSet<Vertex>();
21
22     public boolean immutableVertices;
23     public Mesh    error_against      = null;
24     public double  error              = 0;
25
26     public Mesh(boolean immutableVertices) { this.immutableVertices = immutableVertices; }
27
28     public void makeVerticesImmutable() { this.immutableVertices = true; }
29     public float error() { return (float)error; }
30
31     public int size() { return vertices.size(); }
32     public Iterable<Vertex> vertices() { return vertices; }
33     public Iterator<T> iterator() { return triangles.iterator(); }
34
35     public void rebindPoints() {
36         // unbind all points
37         for(Mesh.T t : this) {
38             t.v1().unbind();
39             t.v2().unbind();
40             t.v3().unbind();
41         }
42         // ask edges to re-implement their bindings
43         for(Mesh.T t : this) {
44             t.e1().dobind();
45             t.e2().dobind();
46             t.e3().dobind();
47         }
48         System.out.println("rebound!");
49     }
50
51     public void transform(Matrix m) {
52         ArrayList<Vertex> set = new ArrayList<Vertex>();
53         for(Vertex v : vertices) set.add(v);
54         for(Vertex v : set) v.transform(m.times(v.p), true);
55     }
56
57     public void rebuild() { /*vertices.rebuild();*/ }
58     public Vec diagonal() { return vertices.diagonal(); }
59     public Point centroid() { return vertices.centroid(); }
60     public Vertex nearest(Point p) { return vertices.nearest(p); }
61
62     /** compute the volume of the mesh */
63     public float volume() {
64         double total = 0;
65         for(T t : this) {
66             double area = t.area();
67             Vec origin_to_centroid = new Vec(new Point(0, 0, 0), t.centroid());
68             boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
69             double height = Math.abs(t.norm().dot(origin_to_centroid));
70             total += ((facingAway ? 1 : -1) * area * height) / 3.0;
71         }
72         return (float)total;
73     }
74
75
76     // Vertexices //////////////////////////////////////////////////////////////////////////////
77
78     /** a vertex in the mesh */
79     public final class Vertex extends HasQuadric implements Visitor {
80         public Point p, oldp, goodp;
81         E e;                // some edge *leaving* this point
82
83         private boolean illegal = false;
84
85         public Point getPoint() { return p; }
86         public float error() { return olderror; }
87
88         private Vertex(Point p) {
89             this.p = p;
90             this.goodp = p;
91             if (vertices.get(p) != null) throw new Error();
92             vertices.add(this);
93         }
94
95         public void reinsert() {
96             vertices.remove(this);
97             vertices.add(this);
98             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) e.t.reinsert();
99         }
100
101         public float olderror = 0;
102         public void setError(float nerror) {
103             error -= olderror;
104             olderror = nerror;
105             error += olderror;
106         }
107
108         public float averageTriangleArea() {
109             int count = 0;
110             float ret = 0;
111             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
112                 ret += e.t.area();
113                 count++;
114             }
115             return ret/count;
116         }
117         public float averageEdgeLength() {
118             int count = 0;
119             float ret = 0;
120             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
121                 ret += e.length();
122                 count++;
123             }
124             return ret/count;
125         }
126
127         public Matrix _recomputeFundamentalQuadric() {
128             Matrix m = Matrix.ZERO;
129             int count = 0;
130             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
131                 m = m.plus(e.t.norm().fundamentalQuadric(e.t.centroid()));
132                 count++;
133             }
134             return m.times(1/(float)count);
135         }
136
137         public HasQuadric nearest() { return error_against==null ? null : error_against.vertices.nearest(p, this); }
138         public void computeError() {
139             if (error_against==null) return;
140             float nerror =
141                 nearest_in_other_mesh != null
142                 ? nearest_in_other_mesh.fundamentalQuadric().preAndPostMultiply(p)
143                 : nearest().fundamentalQuadric().preAndPostMultiply(p);
144             if (quadric_count != 0)
145                 nerror = (nerror + quadric.preAndPostMultiply(p))/(quadric_count+1);
146
147             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
148                 double ang = Math.abs(e.dihedralAngle());
149                 if (ang > Math.PI) throw new Error();
150                 float minangle = (float)(Math.PI * 0.8);
151                 if (ang > minangle) nerror += (ang - minangle);
152                 /*
153                 if (e.t.aspect() < 0.2) {
154                     nerror += (0.2-e.t.aspect()) * 10;
155                 }
156                 */
157             }
158
159             setError(nerror);
160         }
161
162         public boolean move(Matrix m, boolean ignoreProblems) {
163             boolean good = true;
164
165             //     t1' = M * t1
166             //     t2' = t2.getMatrix(t1) * t1'
167             //     t2' = t2.getMatrix(t1) * M * t1
168             //     t1 =     t1.getMatrix(t2) * t2
169             // M * t1 = M * t1.getMatrix(t2) * t2
170             for(Vertex v : (Iterable<Vertex>)getBoundPeers())
171                 good &= v.transform(v.getBindingMatrix(this).times(m).times(this.p),
172                                     ignoreProblems);
173
174             for(Vertex v : (Iterable<Vertex>)getBoundPeers())
175                 if (good || ignoreProblems)  v.reComputeErrorAround();
176                 else                         v.transform(v.oldp, true);
177
178             return good;
179         }
180
181         /** does NOT update bound pairs! */
182         private boolean transform(Point newp, boolean ignoreProblems) {
183             this.oldp = this.p;
184             if (immutableVertices) throw new Error();
185
186             unApplyQuadricToNeighbor();
187             this.p = newp;
188             reinsert();
189             applyQuadricToNeighbor();
190
191             if (!ignoreProblems) {
192                 illegal = false;
193                 checkLegality();
194             }
195             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) e.p2.quadricStale = true;
196             return !illegal;
197         } 
198
199         public void checkLegality() {
200             /*
201             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
202                 if (Math.abs(e.dihedralAngle()) > (Math.PI * 0.9) ||
203                     Math.abs(e.next.dihedralAngle()) > (Math.PI * 0.9)) illegal = true;
204                 if (e.t.aspect() < 0.1) illegal = true;
205             }
206             */
207             if (!illegal) triangles.range(oldp, this.p, (Visitor<T>)this);
208         }
209
210         public void reComputeErrorAround() {
211             reComputeError();
212             if (nearest_in_other_mesh != null)
213                 nearest_in_other_mesh.reComputeError();
214             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
215                 e.p2.reComputeError();
216         }
217
218         public boolean visit(Object o) {
219             if (o instanceof Vertex)
220                 return ((Vertex)o).e != null && ((Vertex)o).norm().dot(Vertex.this.norm()) >= 0;
221             T t = (T)o;
222             if (illegal) return false;
223             for(E e = Vertex.this.e; e!=null; e=e.pair.next==Vertex.this.e?null:e.pair.next) {
224                 if (!t.has(e.p1) && !t.has(e.p2) && e.intersects(t)) { illegal = true; }
225                 if (e.t != null) {
226                     if (!e.t.has(t.e1().p1) && !e.t.has(t.e1().p2) && t.e1().intersects(e.t)) { illegal = true; }
227                     if (!e.t.has(t.e2().p1) && !e.t.has(t.e2().p2) && t.e2().intersects(e.t)) { illegal = true; }
228                     if (!e.t.has(t.e3().p1) && !e.t.has(t.e3().p2) && t.e3().intersects(e.t)) { illegal = true; }
229                 }
230             }
231             return !illegal;
232         }
233
234         public E getFreeIncident() {
235             E ret = getFreeIncident(e, e);
236             if (ret != null) return ret;
237             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
238                 System.out.println(e + " " + e.t);
239             throw new Error("unable to find free incident to " + this);
240         }
241
242         public E getFreeIncident(E start, E before) {
243             for(E e = start; e!=null; e=e.pair.next==before?null:e.pair.next)
244                 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null)
245                     return e.pair;
246             return null;
247         }
248
249         public E getE(Point p2) {
250             Vertex v = vertices.get(p2);
251             if (v==null) return null;
252             return getE(v);
253         }
254         public E getE(Vertex p2) {
255             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
256                 if (e.p1 == this && e.p2 == p2) return e;
257             return null;
258         }
259
260         private void glNormal(GL gl) {
261             Vec norm = norm();
262             gl.glNormal3f(norm.x, norm.y, norm.z);
263         }
264         public Vec norm() {
265             Vec norm = new Vec(0, 0, 0);
266             for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
267                 if (e.t != null)
268                     norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
269             return norm.norm();
270         }
271
272         public void bindTo(Vertex p) { bindTo(Matrix.ONE, p); }
273     }
274
275     public class BindingGroup {
276         private HashSet<E> set = new HashSet<E>();
277         public BindingGroup bind_others;
278         public BindingGroup other() { return bind_others; }
279         public BindingGroup(BindingGroup bind_others) { this.bind_others = bind_others; }
280         public BindingGroup() { this.bind_others = new BindingGroup(this); }
281         public BindingGroup(E e) { this(); set.add(e); }
282         public void add(E e) {
283             if (set.contains(e)) return;
284             set.add(e);
285             BindingGroup e_bind_peers = e.bind_peers;
286             BindingGroup e_bind_to    = e.bind_to;
287             e.bind_peers = this;
288             e.bind_to    = bind_others;
289             for (E epeer  : e_bind_peers.set) add(epeer);
290             for (E eother : e_bind_to.set)    bind_others.add(eother);
291             /*
292             for(E eother : bind_others.set) {
293                 if (e.next.bind_to.set.contains(eother.prev)) {
294                     e.next.next.bindEdge(eother.prev.prev);
295                 }
296                 if (e.prev.bind_to.set.contains(eother.next)) {
297                     e.prev.prev.bindEdge(eother.next.next);
298                 }
299             }
300             */
301         }
302         public void dobind(E e) {
303             for(E ebound : set) {
304                 e.p1.bindTo(Matrix.ONE, ebound.p2);
305                 e.p2.bindTo(Matrix.ONE, ebound.p1);
306             }
307         }
308     }
309
310     /** [UNIQUE] an edge */
311     public final class E extends HasBindingGroup implements Comparable<E> {
312
313         public final Vertex p1, p2;
314         T t;     // triangle to our "left"
315         E prev;  // previous half-edge
316         E next;  // next half-edge
317         E pair;  // partner half-edge
318         public BindingGroup bind_peers  = new BindingGroup(this);
319         public BindingGroup bind_to     = bind_peers.other();
320         boolean shattered = false;
321
322         public boolean intersects(T t) { return t.intersects(p1.p, p2.p); }
323
324         public void bindingGroupChanged(edu.berkeley.qfat.geom.BindingGroup newBindingGroup_) {
325
326             edu.berkeley.qfat.geom.BindingGroup<E> newBindingGroup =
327                 (edu.berkeley.qfat.geom.BindingGroup<E>)newBindingGroup_;
328             if (newBindingGroup==null) return;
329             if (this==newBindingGroup.getMaster()) return;
330             for(E eother : (Iterable<E>)newBindingGroup) {
331                 if (next==null || prev==null) continue;
332                 if (eother.next==null || eother.prev==null) continue;
333                 if (next.isBoundTo(eother.pair.prev.pair) && !prev.isBoundTo(eother.pair.next.pair))
334                     prev.bindTo(next.getBindingMatrix(eother.pair.prev.pair), eother.pair.next.pair);
335                 if (!next.isBoundTo(eother.pair.prev.pair) && prev.isBoundTo(eother.pair.next.pair))
336                     next.bindTo(prev.getBindingMatrix(eother.pair.next.pair), eother.pair.prev.pair);
337             }
338
339         }
340
341         public float stretchRatio() {
342             Vertex nearest = error_against.nearest(midpoint());
343             float nearest_distance = midpoint().distance(nearest.p);
344             float other_distance =
345                 (p1.p.distance(error_against.nearest(p1.p).p)+
346                  p2.p.distance(error_against.nearest(p2.p).p))/2;
347             return nearest_distance/other_distance;
348         }
349         public float comparator() {
350             return length();
351             //return t==null?0:(1/t.aspect());
352         }
353         public int compareTo(E e) {
354             return e.comparator() > comparator() ? 1 : -1;
355         }
356         public void bindEdge(E e, Matrix m) {
357             _bindEdge(e, m);
358             pair._bindEdge(e.pair, m);
359         }
360         public void _bindEdge(E e, Matrix m) {
361             //bind_to.add(e);
362
363             e = e.pair;
364             /*
365             //assumes edges are identical length at binding time
366             Vec reflectionPlaneNormal = e.p2.p.minus(e.p1.p).norm();
367             float a = reflectionPlaneNormal.x;
368             float b = reflectionPlaneNormal.y;
369             float c = reflectionPlaneNormal.z;
370             Matrix reflectionMatrix =
371                 new Matrix( 1-2*a*a,  -2*a*b,  -2*a*c, 0,
372                             -2*a*b,  1-2*b*b,  -2*b*c, 0,
373                             -2*a*c,   -2*b*c, 1-2*c*c, 0,
374                             0,       0,       0,       1);
375             m = m.times(Matrix.translate(e.midpoint().minus(Point.ORIGIN))
376                         .times(reflectionMatrix)
377                         .times(Matrix.translate(Point.ORIGIN.minus(e.midpoint()))));
378             System.out.println(reflectionPlaneNormal);
379             System.out.println("  " + p1.p + " " + m.times(e.p1.p));
380             System.out.println("  " + p2.p + " " + m.times(e.p2.p));
381             */
382             if (m.times(e.p1.p).minus(p1.p).mag() > EPSILON) throw new Error();
383             if (m.times(e.p2.p).minus(p2.p).mag() > EPSILON) throw new Error();
384             this.bindTo(m, e);
385         }
386         
387         public void dobind() {
388             //bind_to.dobind(this);
389             for(E e : (Iterable<E>)getBoundPeers()) {
390                 if (e==this) continue;
391                 p1.bindTo(getBindingMatrix(e), e.p1);
392                 p2.bindTo(getBindingMatrix(e), e.p2);
393             }
394         }
395
396         public Point shatter() {
397             if (shattered || destroyed) return null;
398             shattered = true;
399             E first = null;
400             E firste = null;
401             E firstx = null;
402             E firstq = null;
403             for(E e : (Iterable<E>)getBoundPeers()) {
404                 E enext = e.next;
405                 E eprev = e.prev;
406                 E pnext = e.pair.next;
407                 E pprev = e.pair.prev;
408                 Point mid = e.midpoint();
409                 Vertex r = e.next.p2;
410                 Vertex l = e.pair.next.p2;
411                 e.destroy();
412                 e.pair.destroy();
413                 newT(r.p, e.p1.p, mid,    null, 0);
414                 newT(r.p, mid,    e.p2.p, null, 0);
415                 newT(l.p, mid,    e.p1.p, null, 0);
416                 newT(l.p, e.p2.p, mid,    null, 0);
417                 /*
418                 */
419             }
420             for(E e : (Iterable<E>)getBoundPeers()) {
421                 Point mid = e.midpoint();
422                 if (first==null) {
423                     first = e.p1.getE(mid);
424                     firste = e;
425                     firstx = e.pair;
426                     firstq = e.p2.getE(mid).pair;
427                     continue;
428                 }
429                 e.p1.getE(mid).          bindTo(e.getBindingMatrix(firste), first);
430                 e.p1.getE(mid).pair.     bindTo(e.getBindingMatrix(firste), first.pair);
431                 e.p2.getE(mid).pair.     bindTo(e.getBindingMatrix(firste), firstq);
432                 e.p2.getE(mid).pair.pair.bindTo(e.getBindingMatrix(firste), firstq.pair);
433                 //e.p1.getE(mid).pair.prev.bindTo(e.pair.getBindingMatrix(firstx), first.pair.prev);
434             }
435             return null;
436         }
437
438         public boolean destroyed = false;
439         public void destroy() {
440             if (destroyed) return;
441             destroyed = true;
442             pair.destroyed = true;
443
444             if (t != null) t.destroy();
445             t = null;
446
447             if (pair.t != null) pair.t.destroy();
448             pair.t = null;
449
450             if (next.t != null) next.t.destroy();
451             if (prev.t != null) prev.t.destroy();
452             next.t = null;
453             prev.t = null;
454
455             if (pair.next.t != null) pair.next.t.destroy();
456             if (pair.prev.t != null) pair.next.t.destroy();
457             pair.next.t = null;
458             pair.prev.t = null;
459
460             this.bind_to = null;
461             pair.bind_to = null;
462             this.bind_peers = null;
463             pair.bind_peers = null;
464             pair.prev.next = next;
465             next.prev = pair.prev;
466             prev.next = pair.next;
467             pair.next = prev;
468             if (p1.e == this) p1.e = prev.next;
469             if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
470         }
471
472         private void sync() {
473             this.prev.next = this;
474             this.next.prev = this;
475             this.pair.pair = this;
476             bind_peers.add(this);
477             if (this.next.p1 != p2) throw new Error();
478             if (this.prev.p2 != p1) throw new Error();
479             if (this.p1.e == null) this.p1.e = this;
480             if (!added) added = true;
481         }
482         private boolean added = false;
483
484         public T makeT(int colorclass) { return t==null ? (t = new T(this, colorclass)) : t; }
485
486         public double dihedralAngle() {
487             Vec v1 = t.norm().times(-1);
488             Vec v2 = pair.t.norm().times(-1);
489             return Math.acos(v1.norm().dot(v2.norm()));
490         }
491
492         /** angle between this half-edge and the next */
493         public double angle() {
494             Vec v1 = next.p2.p.minus(p2.p);
495             Vec v2 = this.p1.p.minus(p2.p);
496             return Math.acos(v1.norm().dot(v2.norm()));
497         }
498
499         public void makeAdjacent(E e) {
500             if (this.next == e) return;
501             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
502             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
503
504             E freeIncident = p2.getFreeIncident(e, this);
505
506             e.prev.next = freeIncident.next;
507             freeIncident.next.prev = e.prev;
508
509             freeIncident.next = this.next;
510             this.next.prev = freeIncident;
511             
512             this.next = e;
513             e.prev = this;
514
515             sync();
516             freeIncident.sync();
517         }
518
519         /** creates an isolated edge out in the middle of space */
520         public E(Point p1, Point p2) {
521             if (vertices.get(p1) != null) throw new Error();
522             if (vertices.get(p2) != null) throw new Error();
523             this.p1 = new Vertex(p1);
524             this.p2 = new Vertex(p2);
525             this.prev = this.next = this.pair = new E(this, this, this);
526             this.p1.e = this;
527             this.p2.e = this.pair;
528             sync();
529         }
530
531         /** adds a new half-edge from prev.p2 to p2 */
532         public E(E prev, Point p) {
533             Vertex p2;
534             p2 = vertices.get(p);
535             if (p2 == null) p2 = new Vertex(p);
536             this.p1 = prev.p2;
537             this.p2 = p2;
538             this.prev = prev;
539             if (p2.getE(p1) != null) throw new Error();
540             if (p2.e==null) {
541                 this.next = this.pair = new E(this, this, prev.next);
542             } else {
543                 E q = p2.getFreeIncident();
544                 this.next = q.next;
545                 this.next.prev = this;
546                 E z = prev.next;
547                 this.prev.next = this;
548                 this.pair = new E(q, this, z);
549             }
550             if (p2.e==null) p2.e = this.pair;
551             sync();
552         }
553
554         /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
555         public E(E prev, E pair, E next) {
556             this.p1 = prev.p2;
557             this.p2 = next.p1;
558             this.prev = prev;
559             this.next = next;
560             this.pair = pair;
561             sync();
562         }
563         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); }
564         public boolean has(Vertex v) { return v==p1 || v==p2; }
565         public float length() { return p1.p.minus(p2.p).mag(); }
566         public String toString() { return p1+"->"+p2; }
567
568     }
569
570     public E makeE(Point p1, Point p2) {
571         Vertex v1 = vertices.get(p1);
572         Vertex v2 = vertices.get(p2);
573         if (v1 != null && v2 != null) {
574             E e = v1.getE(v2);
575             if (e != null) return e;
576             e = v2.getE(v1);
577             if (e != null) return e;
578         }
579         if (v1 != null) return new E(v1.getFreeIncident(), p2);
580         if (v2 != null) return new E(v2.getFreeIncident(), p1).pair;
581         return new E(p1, p2);
582     }
583     public T newT(Point p1, Point p2, Point p3, Vec norm, int colorclass) {
584         if (norm != null) {
585             Vec norm2 = p3.minus(p1).cross(p2.minus(p1));
586             float dot = norm.dot(norm2);
587             //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within evertsilon of each other: "+norm+" "+norm2);
588             if (dot < 0) { Point p = p1; p1=p2; p2 = p; }
589         }
590         E e12 = makeE(p1, p2);
591         E e23 = makeE(p2, p3);
592         E e31 = makeE(p3, p1);
593         while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
594             e12.makeAdjacent(e23);
595             e23.makeAdjacent(e31);
596             e31.makeAdjacent(e12);
597         }
598         T ret = e12.makeT(colorclass);
599         if (e12.t == null) throw new Error();
600         if (e23.t == null) throw new Error();
601         if (e31.t == null) throw new Error();
602         return ret;
603     }
604
605     /** [UNIQUE] a triangle (face) */
606     public final class T extends Triangle {
607         public final E e1;
608         public final int color;
609         public final int colorclass;
610
611         T(E e1, int colorclass) {
612             this.e1 = e1;
613             E e2 = e1.next;
614             E e3 = e2.next;
615             if (e1==e2 || e1==e3) throw new Error();
616             if (e3.next!=e1) throw new Error();
617             if (e1.t!=null || e2.t!=null || e3.t!=null) throw new Error("non-manifold surface or disagreeing normals");
618             e1.t = this;
619             e1.next.t = this;
620             e1.next.next.t = this;
621
622             // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
623
624             int color = Math.abs(random.nextInt());
625             while(true) {
626                 color = color % 4;
627                 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
628                 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
629                 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
630                 break;
631             }
632             this.color = color;
633             this.colorclass = colorclass;
634             triangles.add(this);
635         }
636         public E e1() { return e1; }
637         public E e2() { return e1.next; }
638         public E e3() { return e1.prev; }
639         public Vertex v1() { return e1.p1; }
640         public Vertex v2() { return e1.p2; }
641         public Vertex v3() { return e1.next.p2; }
642         public Point p1() { return e1.p1.p; }
643         public Point p2() { return e1.p2.p; }
644         public Point p3() { return e1.next.p2.p; }
645         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
646         public boolean has(Vertex v) { return v1()==v || v2()==v || v3()==v; }
647
648         public void removeFromRTree() { triangles.remove(this); }
649         public void addToRTree() { triangles.insert(this); }
650         public void destroy() { triangles.remove(this); }
651         public void reinsert() { triangles.remove(this); triangles.add(this); }
652
653         public boolean shouldBeDrawn() {
654             if (e1().bindingGroupSize() <= 1) return false;
655             if (e2().bindingGroupSize() <= 1) return false;
656             if (e3().bindingGroupSize() <= 1) return false;
657             return true;
658         }
659
660         /** issue gl.glVertex() for each of the triangle's points */
661         public void glVertices(GL gl) {
662             if (!shouldBeDrawn()) return;
663             norm().glNormal(gl);
664             Point p1 = v1().goodp;
665             Point p2 = v2().goodp;
666             Point p3 = v3().goodp;
667             p1.glVertex(gl);
668             p2.glVertex(gl);
669             p3.glVertex(gl);
670         }
671
672     }
673 }