1 package edu.berkeley.qfat;
4 import java.awt.event.*;
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;
14 // EDGES RUN COUNTER-CLOCKWISE
16 public class Mesh implements Iterable<Mesh.T> {
18 public static final float EPSILON = (float)0.0001;
19 public static final Random random = new Random();
21 private RTree<T> triangles = new RTree<T>();
22 private PointSet<Vertex> vertices = new PointSet<Vertex>();
24 public boolean option_wireframe = false;
25 public boolean option_errorNormals = false;
26 public boolean option_selectable = true;
28 public void render(GL gl, Matrix m) {
29 if (option_wireframe) {
30 gl.glDisable(GL.GL_LIGHTING);
31 gl.glBegin(GL.GL_LINES);
32 gl.glColor3f(1, 1, 1);
34 // fixme used to be .goodp
35 m.times(t.e1().v1.p).glVertex(gl);
36 m.times(t.e1().v2.p).glVertex(gl);
37 m.times(t.e2().v1.p).glVertex(gl);
38 m.times(t.e2().v2.p).glVertex(gl);
39 m.times(t.e3().v1.p).glVertex(gl);
40 m.times(t.e3().v2.p).glVertex(gl);
43 gl.glEnable(GL.GL_LIGHTING);
47 gl.glColor4f((float)(0.25+(0.05*t.color)),
48 (float)(0.25+(0.05*t.color)),
49 (float)(0.75+(0.05*t.color)),
53 gl.glColor4f((float)(0.75+(0.05*t.color)),
54 (float)(0.25+(0.05*t.color)),
55 (float)(0.25+(0.05*t.color)),
61 if (option_errorNormals)
63 for(Mesh.Vertex p : new Mesh.Vertex[] { t.v1(), t.v2(), t.v3() }) {
65 gl.glBegin(GL.GL_LINES);
66 gl.glColor3f(1, 1, 1);
68 p.p.plus(p.norm().times((float)p.error()*10)).glVertex(gl);
74 public boolean immutableVertices;
75 public Mesh error_against = null;
76 public double error = 0;
78 public Mesh(boolean immutableVertices) { this.immutableVertices = immutableVertices; }
80 public void makeVerticesImmutable() { this.immutableVertices = true; }
81 public float error() { return (float)error; }
83 public int size() { return vertices.size(); }
84 public Iterable<Vertex> vertices() { return vertices; }
85 public Iterator<T> iterator() { return triangles.iterator(); }
87 public void rebindPoints() {
89 for(Mesh.T t : this) {
94 // ask edges to re-implement their bindings
95 for(Mesh.T t : this) {
100 System.out.println("rebound!");
103 public void transform(Matrix m) {
104 ArrayList<Vertex> set = new ArrayList<Vertex>();
105 for(Vertex v : vertices) set.add(v);
106 for(Vertex v : set) v.transform(m.times(v.p), true, null);
107 for(Vertex v : set) v.goodp = v.p;
110 public void rebuild() { /*vertices.rebuild();*/ }
111 public Vec diagonal() { return vertices.diagonal(); }
112 public Point centroid() { return vertices.centroid(); }
113 public Vertex nearest(Point p) { return vertices.nearest(p); }
115 /** compute the volume of the mesh */
116 public float volume() {
119 double area = t.area();
120 Vec origin_to_centroid = new Vec(new Point(0, 0, 0), t.centroid());
121 boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
122 double height = Math.abs(t.norm().dot(origin_to_centroid));
123 total += ((facingAway ? 1 : -1) * area * height) / 3.0;
129 public void subdivide() {
130 for (Vertex v : vertices()) v.original = true;
131 HashSet<E> edges = new HashSet<E>();
132 HashSet<E> flip = new HashSet<E>();
133 HashSet<T> tris = new HashSet<T>();
142 System.out.println("triangles="+count);
145 if (e.destroyed || e.shattered) continue;
146 e.shatter().edge = true;
147 for(E ex : (Iterable<E>)e.getBoundPeers()) {
148 Vertex m = nearest(ex.midpoint());
150 E e3 = ex.v1.getE(m).next;
160 System.out.println("flip!");
164 System.out.println("count="+count);
167 HashSet<Vertex> verts = new HashSet<Vertex>();
168 for(Vertex v : vertices()) verts.add(v);
169 for (Vertex v : verts)
171 for (Vertex v : verts) {
173 for(E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
181 for(E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
182 for(E e2 = e.v2.e; e2!=null; e2=e2.pair.next==e.v2.e?null:e2.pair.next) {
183 if (e2.v2.original) {
190 for (Vertex v : verts)
192 System.out.println("-------------------------------------------------------------------");
194 for (Vertex v : verts) {
197 for(E e = v.e; e!=null; e=e.pair.next==v.e?null:e.pair.next) {
199 v.wish(e.midpoint());
200 v.wish(e.midpoint());
201 v.wish(e.next.pair.t.centroid());
205 for(int j=0; j<n-3; j++)
206 v.wish(v.getPoint());
209 for (Vertex v : verts)
211 for (Vertex v : verts)
216 // Vertexices //////////////////////////////////////////////////////////////////////////////
218 /** a vertex in the mesh */
219 public final class Vertex extends HasQuadric implements Visitor {
220 public Point p, goodp;
222 E e; // some edge *leaving* this point
224 public boolean original = false;
225 public boolean edge = false;
226 public boolean face = false;
228 private int wishes = 0;
229 private Point wish = Point.ZERO;
230 public void clearWish() { wishes = 0; wish = Point.ZERO; }
231 public void wish(HasPoint hp) {
232 Point p = hp.getPoint();
234 wish = new Point(wish.x+p.x, wish.y+p.y, wish.z+p.z);
236 public void grantWish() {
237 for(Vertex v : (Iterable<Vertex>)getBoundPeers()) {
238 if (v==this) continue;
239 if (v.wishes==0) continue;
240 Point p = this.getBindingMatrix(v).times(v.wish.minus(Point.ZERO).div(v.wishes).plus(Point.ZERO));
241 wish = p.minus(Point.ZERO).times(v.wishes).plus(wish);
245 if (wishes==0) return;
246 Vec d = wish.minus(Point.ZERO).div(wishes).plus(Point.ZERO).minus(getPoint());
250 public void avgWish() {
251 if (wishes==0) return;
252 wish = wish.minus(Point.ZERO).div(wishes).plus(Point.ZERO);
256 private boolean illegal = false;
258 public boolean visible = false;
260 public Point getPoint() { return p; }
261 public float error() { return olderror; }
263 private Vertex(Point p) {
267 if (vertices.get(p) != null) throw new Error();
271 public void reinsert() {
272 vertices.remove(this);
274 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) e.t.reinsert();
277 // the average of all adjacent points
278 public Point recenter() {
281 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
282 vec = vec.plus(e.getOther(this).getPoint().minus(Point.ZERO));
285 return Point.ZERO.plus(vec.div(count));
288 public float olderror = 0;
289 public void setError(float nerror) {
296 public Vertex hack(GL gl, Point mouse) {
297 double dist = Double.MAX_VALUE;
299 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
300 Vertex v = e.getOther(this);
301 double dist2 = v.getPoint().glProject(gl).distance(mouse);
302 if ((cur==null || dist2 < dist) && v.visible) {
311 public float averageTriangleArea() {
314 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
320 public float averageEdgeLength() {
323 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
330 public Matrix _recomputeFundamentalQuadric() {
331 Matrix m = Matrix.ZERO;
333 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
334 m = m.plus(e.t.norm().fundamentalQuadric(e.t.centroid()));
338 m = m.plus(norm().fundamentalQuadric(this.p).times(count));
341 return m.times(1/(float)count);
344 public HasQuadric nearest() { return error_against==null ? null : error_against.vertices.nearest(p, this); }
345 public void computeError() {
346 if (error_against==null) return;
347 if (nearest_in_other_mesh == null && nearest()==null) return;
349 nearest_in_other_mesh != null
350 ? nearest_in_other_mesh.fundamentalQuadric().preAndPostMultiply(p)
351 : nearest().fundamentalQuadric().preAndPostMultiply(p);
352 if (quadric_count != 0)
353 nerror = (nerror + quadric.preAndPostMultiply(p))/(quadric_count+1);
355 if (!immutableVertices && quadric_count == 0) {
356 //nerror = Math.max(nerror, 0.4f);
359 //System.out.println(nerror);
360 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
361 double ang = e.dihedralAngle();
362 if (ang > Math.PI) throw new Error();
363 if (ang < -Math.PI) throw new Error();
364 float minangle = (float)(Math.PI * 0.8);
365 //nerror += ((ang / Math.PI)*(ang/Math.PI)) * e.length() * 0.05;
367 nerror += (1-e.t.quality())*0.0001;
368 if (ang > minangle) nerror += (ang - minangle);
370 //System.out.println(((ang / Math.PI)*(ang/Math.PI)) * 0.000001);
372 if (e.t.aspect() < 0.2) {
373 nerror += (0.2-e.t.aspect()) * 10;
377 if (!immutableVertices) {
378 Vertex n = (Vertex)nearest();
379 float d = norm().dot(n.norm());
380 if (d > 1 || d < -1) throw new Error();
382 nerror *= (2.0f - d);
384 nerror += 0.0003 * (2.0f + d);
385 nerror *= (2.0f + d);
392 public boolean move(Vec vv, boolean ignoreProblems) {
397 // t2' = t2.getMatrix(t1) * t1'
398 // t2' = t2.getMatrix(t1) * M * t1
399 // t1 = t1.getMatrix(t2) * t2
400 // M * t1 = M * t1.getMatrix(t2) * t2
402 if (bindingGroup!=null && this != bindingGroup.getMaster()) {
403 Matrix m2 = getBindingMatrix(bindingGroup.getMaster());
404 Vec v2 = m2.times(vv.plus(getPoint())).minus(m2.times(getPoint()));
405 return ((Vertex)bindingGroup.getMaster()).move(v2, ignoreProblems);
409 Point pp = vv.plus(getPoint());
410 if (bindingGroup != null) {
412 for(int i=0; i<20 ; i++) {
413 Point v2 = getConstraint().times(pp);
414 pp = pp.midpoint(v2);
415 //System.out.println(m.minus(m2));
418 //pp = getConstraint().times(pp);
420 //pp = pp.minus(op).norm().times(vv.mag()).plus(op);
423 for(Vertex v : (Iterable<Vertex>)getBoundPeers()) {
424 Point pt2 = v.getBindingMatrix(this).times(pt);
426 if (Math.abs( v.p.minus(pt2).mag() / pt.minus(op).mag() ) > 5)
427 throw new Error(v.p+" "+pt2+"\n"+op+" "+pt+"\n"+v.getBindingMatrix(this));
428 if (Math.abs( v.p.minus(pt2).mag() / pt.minus(op).mag() ) < 1/5) throw new Error();
430 good &= v.transform(pt2, ignoreProblems, v.getBindingMatrix(this));
433 if (!good && !ignoreProblems) {
434 for(Vertex v : (Iterable<Vertex>)getBoundPeers())
435 v.transform(v.oldp, true, null);
438 for(Vertex v : (Iterable<Vertex>)getBoundPeers())
439 v.recomputeFundamentalQuadricIfNeighborChanged();
440 for(Vertex v : (Iterable<Vertex>)getBoundPeers())
441 v.reComputeErrorAround();
445 public boolean ok = true;
447 /** does NOT update bound pairs! */
448 private boolean transform(Point newp, boolean ignoreProblems, Matrix yes) {
450 if (immutableVertices) throw new Error();
452 unApplyQuadricToNeighbor();
454 boolean illegalbefore = illegal;
457 if (this.p.minus(newp).mag() > 0.1 && !ignoreProblems) {
459 throw new Exception(""+this.p.minus(newp).mag()+" "+ignoreProblems+" "+yes);
460 } catch(Exception e) {
469 applyQuadricToNeighbor();
471 if (!ignoreProblems) {
474 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
475 e.v2.quadricStale = true;
476 return !illegal || (illegalbefore && illegal);
479 public void checkLegality() {
481 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
482 if (Math.abs(e.dihedralAngle()) > (Math.PI * 0.9) ||
483 Math.abs(e.next.dihedralAngle()) > (Math.PI * 0.9)) illegal = true;
484 if (e.t.aspect() < 0.2) illegal = true;
487 if (!illegal) triangles.range(oldp, this.p, (Visitor<T>)this);
490 public void reComputeErrorAround() {
492 if (nearest_in_other_mesh != null)
493 nearest_in_other_mesh.reComputeError();
494 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
495 e.v2.reComputeError();
498 public boolean visit(Object o) {
499 if (o instanceof Vertex)
500 return ((Vertex)o).e != null && ((Vertex)o).norm().dot(Vertex.this.norm()) >= 0;
502 if (illegal) return false;
503 for(E e = Vertex.this.e; e!=null; e=e.pair.next==Vertex.this.e?null:e.pair.next) {
504 if (!t.has(e.v1) && !t.has(e.v2) && e.intersects(t)) { illegal = true; }
506 if (!e.t.has(t.e1().v1) && !e.t.has(t.e1().v2) && t.e1().intersects(e.t)) { illegal = true; }
507 if (!e.t.has(t.e2().v1) && !e.t.has(t.e2().v2) && t.e2().intersects(e.t)) { illegal = true; }
508 if (!e.t.has(t.e3().v1) && !e.t.has(t.e3().v2) && t.e3().intersects(e.t)) { illegal = true; }
514 public E getEdge() { return e; }
515 public E getFreeIncident() {
516 E ret = getFreeIncident(e, e);
517 if (ret != null) return ret;
518 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
519 System.out.println(e + " " + e.t);
520 throw new Error("unable to find free incident to " + this);
523 public E getFreeIncident(E start, E before) {
524 for(E e = start; e!=null; e=e.pair.next==before?null:e.pair.next)
525 if (e.pair.v2 == this && e.pair.t == null && e.pair.next.t == null)
530 public E getE(Point v2) {
531 Vertex v = vertices.get(v2);
532 if (v==null) return null;
535 public E getE(Vertex v2) {
536 if (this.e!=null && this!=this.e.v1 && this!=this.e.v2) throw new RuntimeException();
538 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
539 if (e.v1 == this && e.v2 == v2) return e;
542 if (e.destroyed) throw new RuntimeException("fark " + i + " " + e.prev + " " + (e.prev.next==e) + " " + e.prev.destroyed);
547 private void glNormal(GL gl) {
549 gl.glNormal3f(norm.x, norm.y, norm.z);
552 Vec norm = new Vec(0, 0, 0);
553 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next)
555 norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
559 public void bindTo(Vertex p) { bindTo(Matrix.ONE, p); }
563 /** [UNIQUE] an edge */
564 public final class E extends HasBindingGroup implements Comparable<E> {
566 public void sanity() {
567 if (destroyed) return;
568 if (pair!=null && (pair.v1!=v2 || pair.v2!=v1)) throw new RuntimeException();
569 if (next!=null && next.v1!=v2) throw new RuntimeException();
570 if (prev!=null && prev.v2!=v1) throw new RuntimeException();
573 public final Vertex v1, v2;
574 T t; // triangle to our "left"
575 E prev; // previous half-edge
576 E next; // next half-edge
577 E pair; // partner half-edge
578 boolean shattered = false;
580 public boolean intersects(T t) { return t.intersects(v1.p, v2.p); }
582 public Segment getSegment() { return new Segment(v1.getPoint(), v2.getPoint()); }
585 // FIXME: coplanarity check needed
586 if (destroyed) return;
587 for (E e : (Iterable<E>)getBoundPeers()) {
588 if (!e.pair.isBoundTo(pair)) throw new RuntimeException("cannot flip!");
590 Vertex v1 = t.getOtherVertex(this);
591 Vertex v2 = pair.t.getOtherVertex(pair);
594 T t1 = newT(v1, v2, this.v2);
595 T t2 = newT(v2, v1, this.v1);
603 for (E e : (Iterable<E>)getBoundPeers()) {
604 if (e==this) continue;
605 if (e.destroyed) continue;
606 Vertex v1e = e.t.getOtherVertex(e);
607 Vertex v2e = e.pair.t.getOtherVertex(e.pair);
610 if (v1e.getE(v2e)!=null) throw new RuntimeException();
611 newT(v1e, v2e, e.v2).red = true;
612 newT(v2e, v1e, e.v1).red = true;
613 v2e.getE(v1e).bindTo(e.getBindingMatrix(this), v1.getE(v2));
614 v1e.getE(v2e).bindTo(e.pair.getBindingMatrix(this.pair), v2.getE(v1));
619 public void bindingGroupChanged(edu.berkeley.qfat.geom.BindingGroup newBindingGroup_) {
621 edu.berkeley.qfat.geom.BindingGroup<E> newBindingGroup =
622 (edu.berkeley.qfat.geom.BindingGroup<E>)newBindingGroup_;
623 if (newBindingGroup==null) return;
624 if (this==newBindingGroup.getMaster()) return;
625 HashSet<E> nbg = new HashSet<E>();
626 for(E eother : (Iterable<E>)newBindingGroup) nbg.add(eother);
627 for(E eother : nbg) {
628 if (next==null || prev==null) continue;
629 if (eother.next==null || eother.prev==null) continue;
631 if (next.isBoundTo(eother.pair.prev.pair) && !prev.isBoundTo(eother.pair.next.pair))
632 prev.bindTo(next.getBindingMatrix(eother.pair.prev.pair), eother.pair.next.pair);
633 if (!next.isBoundTo(eother.pair.prev.pair) && prev.isBoundTo(eother.pair.next.pair))
634 next.bindTo(prev.getBindingMatrix(eother.pair.next.pair), eother.pair.prev.pair);
637 if (next.isBoundTo(eother.prev) && !prev.isBoundTo(eother.next))
638 prev.bindTo(next.getBindingMatrix(eother.prev), eother.next);
639 if (!next.isBoundTo(eother.prev) && prev.isBoundTo(eother.next))
640 next.bindTo(prev.getBindingMatrix(eother.next), eother.prev);
642 if (next.isBoundTo(eother.next) && !prev.isBoundTo(eother.prev))
643 prev.bindTo(next.getBindingMatrix(eother.next), eother.prev);
644 if (!next.isBoundTo(eother.next) && prev.isBoundTo(eother.prev))
645 next.bindTo(prev.getBindingMatrix(eother.prev), eother.next);
650 public float stretchRatio() {
651 Vertex nearest = error_against.nearest(midpoint());
652 float nearest_distance = midpoint().distance(nearest.p);
653 float other_distance =
654 (v1.p.distance(error_against.nearest(v1.p).p)+
655 v2.p.distance(error_against.nearest(v2.p).p))/2;
656 return nearest_distance/other_distance;
658 public float comparator() {
661 public int compareTo(E e) {
662 return e.comparator() > comparator() ? 1 : -1;
664 public void bindEdge(E e, Matrix m) {
666 pair._bindEdge(e.pair, m);
668 public void _bindEdge(E e, Matrix m) {
671 //assumes edges are identical length at binding time
672 Vec reflectionPlaneNormal = e.v2.p.minus(e.v1.p).norm();
673 float a = reflectionPlaneNormal.x;
674 float b = reflectionPlaneNormal.y;
675 float c = reflectionPlaneNormal.z;
676 Matrix reflectionMatrix =
677 new Matrix( 1-2*a*a, -2*a*b, -2*a*c, 0,
678 -2*a*b, 1-2*b*b, -2*b*c, 0,
679 -2*a*c, -2*b*c, 1-2*c*c, 0,
681 m = m.times(Matrix.translate(e.midpoint().minus(Point.ORIGIN))
682 .times(reflectionMatrix)
683 .times(Matrix.translate(Point.ORIGIN.minus(e.midpoint()))));
684 System.out.println(reflectionPlaneNormal);
685 System.out.println(" " + v1.p + " " + m.times(e.v1.p));
686 System.out.println(" " + v2.p + " " + m.times(e.v2.p));
689 if (m.times(e.v1.p).minus(v1.p).mag() > EPSILON) throw new Error();
690 if (m.times(e.v2.p).minus(v2.p).mag() > EPSILON) throw new Error();
695 public void dobind() {
696 for(E e : (Iterable<E>)getBoundPeers()) {
697 if (e==this) continue;
698 v1.bindTo(getBindingMatrix(e), e.v1);
699 v2.bindTo(getBindingMatrix(e), e.v2);
700 e.v1.setConstraint(getConstraint());
701 e.v2.setConstraint(getConstraint());
705 public Vertex shatter() {
706 if (shattered || destroyed) return nearest(midpoint());
712 for(E e : (Iterable<E>)getBoundPeers()) {
715 E pnext = e.pair.next;
716 E pprev = e.pair.prev;
717 Point mid = e.midpoint();
718 Vertex r = e.next.v2;
719 Vertex l = e.pair.next.v2;
723 newT(r.p, e.v1.p, mid, null, 0);
724 newT(r.p, mid, e.v2.p, null, 0);
725 newT(l.p, mid, e.v1.p, null, 0);
726 newT(l.p, e.v2.p, mid, null, 0);
729 for(E e : (Iterable<E>)getBoundPeers()) {
730 Point mid = e.midpoint();
732 first = e.v1.getE(mid);
735 firstq = e.v2.getE(mid).pair;
738 e.v1.getE(mid). bindTo(e.getBindingMatrix(firste), first);
739 e.v1.getE(mid).pair. bindTo(e.getBindingMatrix(firste), first.pair);
740 e.v2.getE(mid).pair. bindTo(e.getBindingMatrix(firste), firstq);
741 e.v2.getE(mid).pair.pair.bindTo(e.getBindingMatrix(firste), firstq.pair);
744 first.setConstraint(firste.getConstraint());
745 firstq.setConstraint(firste.getConstraint());
747 return nearest(midpoint());
750 public boolean destroyed = false;
751 public void destroy() {
752 if (destroyed) return;
754 pair.destroyed = true;
756 if (t != null) t.destroy();
759 if (pair.t != null) pair.t.destroy();
762 if (next.t != null) next.t.destroy();
763 if (prev.t != null) prev.t.destroy();
767 if (pair.next.t != null) pair.next.t.destroy();
768 if (pair.prev.t != null) pair.next.t.destroy();
772 pair.prev.next = next;
773 next.prev = pair.prev;
774 prev.next = pair.next;
775 pair.next.prev = prev;
777 if (v1.e == this) v1.e = pair.next;
778 if (pair.v1.e == pair) pair.v1.e = next;
780 if (v2.e == this) throw new RuntimeException();
781 if (pair.v2.e == pair) throw new RuntimeException();
805 private void sync() {
806 this.prev.next = this;
807 this.next.prev = this;
808 this.pair.pair = this;
809 if (this.next.v1 != v2) throw new Error();
810 if (this.prev.v2 != v1) throw new Error();
811 if (this.v1.e == null) this.v1.e = this;
812 if (!added) added = true;
814 private boolean added = false;
816 public T makeT(int colorclass) { return t==null ? (t = new T(this, colorclass)) : t; }
818 public double dihedralAngle() {
819 Vec v1 = t.norm().times(-1);
820 Vec v2 = pair.t.norm().times(-1);
821 double prod = v1.norm().dot(v2.norm());
822 prod = Math.min(1,prod);
823 prod = Math.max(-1,prod);
824 double ret = Math.acos(prod);
825 if (Double.isNaN(ret)) throw new Error("nan! " + prod);
829 /** angle between this half-edge and the next */
830 public double angle() {
831 Vec v1 = next.v2.p.minus(this.v2.p);
832 Vec v2 = this.v1.p.minus(this.v2.p);
833 return Math.acos(v1.norm().dot(v2.norm()));
836 public Vertex getOther(Vertex v) {
837 if (this.v1 == v) return v2;
838 if (this.v2 == v) return v1;
842 public void makeAdjacent(E e) {
843 if (this.next == e) return;
844 if (v2 != e.v1) throw new Error("cannot make adjacent -- no shared vertex: " + this + " " + e);
845 if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free " + t + " " + e.t);
847 E freeIncident = v2.getFreeIncident(e, this);
849 e.prev.next = freeIncident.next;
850 freeIncident.next.prev = e.prev;
852 freeIncident.next = this.next;
853 this.next.prev = freeIncident;
862 /** creates an isolated edge out in the middle of space */
863 public E(Point v1, Point v2) {
864 if (vertices.get(v1) != null) throw new Error();
865 if (vertices.get(v2) != null) throw new Error();
866 this.v1 = new Vertex(v1);
867 this.v2 = new Vertex(v2);
868 this.prev = this.next = this.pair = new E(this, this, this);
870 this.v2.e = this.pair;
874 /** adds a new half-edge from prev.v2 to v2 */
875 public E(E prev, Point p) {
877 v2 = vertices.get(p);
878 if (v2 == null) v2 = new Vertex(p);
882 if (prev.destroyed) throw new RuntimeException();
883 if (v2.getE(v1) != null) throw new Error();
885 this.next = this.pair = new E(this, this, prev.next);
887 E q = v2.getFreeIncident();
889 this.next.prev = this;
891 this.prev.next = this;
892 this.pair = new E(q, this, z);
894 if (v2.e==null) v2.e = this.pair;
898 /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
899 public E(E prev, E pair, E next) {
902 if (prev.destroyed) throw new RuntimeException();
908 public Point midpoint() { return new Point((v1.p.x+v2.p.x)/2, (v1.p.y+v2.p.y)/2, (v1.p.z+v2.p.z)/2); }
909 public boolean has(Vertex v) { return v==v1 || v==v2; }
910 public float length() { return v1.p.minus(v2.p).mag(); }
911 public String toString() { return v1+"->"+v2; }
915 public E makeE(Point p1, Point p2) {
916 Vertex v1 = vertices.get(p1);
917 Vertex v2 = vertices.get(p2);
918 if (v1 != null && v2 != null) {
920 if (e != null) return e;
922 if (e != null) return e;
924 if (v1 != null) return new E(v1.getFreeIncident(), p2);
925 if (v2 != null) return new E(v2.getFreeIncident(), p1).pair;
926 return new E(p1, p2);
928 public boolean coalesce = false;
929 private static float round(float f) {
930 return Math.round(f*1000)/1000f;
932 public T newT(HasPoint v1, HasPoint v2, HasPoint v3) {
933 return newT(v1.getPoint(), v2.getPoint(), v3.getPoint(), null, 0);
935 public T newT(Point v1, Point v2, Point v3, Vec norm, int colorclass) {
938 for(Vertex v : vertices) { if (v1.distance(v.p) < EPSILON) { v1 = v.p; break; } }
939 for(Vertex v : vertices) { if (v2.distance(v.p) < EPSILON) { v2 = v.p; break; } }
940 for(Vertex v : vertices) { if (v3.distance(v.p) < EPSILON) { v3 = v.p; break; } }
942 v1 = new Point(round(v1.x), round(v1.y), round(v1.z));
943 v2 = new Point(round(v2.x), round(v2.y), round(v2.z));
944 v3 = new Point(round(v3.x), round(v3.y), round(v3.z));
948 Vec norm2 = v3.minus(v1).cross(v2.minus(v1));
949 float dot = norm.dot(norm2);
950 //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within evertsilon of each other: "+norm+" "+norm2);
951 if (dot < 0) { Point p = v1; v1=v2; v2 = p; }
953 E e12 = makeE(v1, v2);
954 E e23 = makeE(v2, v3);
955 E e31 = makeE(v3, v1);
956 while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
957 e12.makeAdjacent(e23);
958 e23.makeAdjacent(e31);
959 e31.makeAdjacent(e12);
961 T ret = e12.makeT(colorclass);
962 if (e12.t == null) throw new Error();
963 if (e23.t == null) throw new Error();
964 if (e31.t == null) throw new Error();
968 private int max_serial = 0;
969 /** [UNIQUE] a triangle (face) */
970 public final class T extends Triangle {
972 public final int color;
973 public final int colorclass;
975 public boolean red = false;
976 public boolean old = false;
978 public final int serial = max_serial++;
979 public boolean occluded;
981 public Point shatter() {
982 if (destroyed) return null;
985 HashSet<E> forward = new HashSet<E>();
986 HashSet<E> backward = new HashSet<E>();
987 HashSet<E> both = new HashSet<E>();
989 for(E eb : (Iterable<E>)e.getBoundPeers()) {
991 if (eb.next.isBoundTo(e.next) && eb.prev.isBoundTo(e.prev)) {
995 if (eb.pair.next.pair.isBoundTo(e.prev) && eb.pair.prev.pair.isBoundTo(e.next)) {
996 backward.add(eb.pair);
1001 Vertex v1 = e.t.v1();
1002 Vertex v2 = e.t.v2();
1003 Vertex v3 = e.t.v3();
1004 Point c = e.t.centroid();
1012 // FIXME: forward too
1013 for(E ex : backward) {
1014 Vertex v1x = ex.t.v1();
1015 Vertex v2x = ex.t.v2();
1016 Vertex v3x = ex.t.v3();
1017 Point cx = ex.t.centroid();
1018 E ex_next = ex.next;
1019 E ex_prev = ex.prev;
1025 // FIXME: i have no idea if this is right
1026 e.next.bindTo(e.getBindingMatrix(ex.pair), ex.prev);
1027 e.prev.bindTo(e.getBindingMatrix(ex.pair), ex.next);
1028 e.next.pair.bindTo(e.getBindingMatrix(ex.pair), ex.prev.pair);
1029 e.prev.pair.bindTo(e.getBindingMatrix(ex.pair), ex.next.pair);
1031 e_next.next.bindTo(e_next.getBindingMatrix(ex_prev.pair), ex_prev.prev.pair);
1032 e_next.prev.bindTo(e_next.getBindingMatrix(ex_prev.pair), ex_prev.next.pair);
1034 e_prev.next.bindTo(e_prev.getBindingMatrix(ex_next.pair), ex_next.prev.pair);
1035 e_prev.prev.bindTo(e_prev.getBindingMatrix(ex_next.pair), ex_next.next.pair);
1044 for(E e : (Iterable<E>)getBoundPeers()) {
1047 E pnext = e.pair.next;
1048 E pprev = e.pair.prev;
1049 Point mid = e.midpoint();
1050 Vertex r = e.next.v2;
1051 Vertex l = e.pair.next.v2;
1055 newT(r.p, e.v1.p, mid, null, 0);
1056 newT(r.p, mid, e.v2.p, null, 0);
1057 newT(l.p, mid, e.v1.p, null, 0);
1058 newT(l.p, e.v2.p, mid, null, 0);
1061 for(E e : (Iterable<E>)getBoundPeers()) {
1062 Point mid = e.midpoint();
1064 first = e.v1.getE(mid);
1067 firstq = e.v2.getE(mid).pair;
1070 e.v1.getE(mid). bindTo(e.getBindingMatrix(firste), first);
1071 e.v1.getE(mid).pair. bindTo(e.getBindingMatrix(firste), first.pair);
1072 e.v2.getE(mid).pair. bindTo(e.getBindingMatrix(firste), firstq);
1073 e.v2.getE(mid).pair.pair.bindTo(e.getBindingMatrix(firste), firstq.pair);
1077 first.setConstraint(firste.getConstraint());
1078 firstq.setConstraint(firste.getConstraint());
1084 T(E e1, int colorclass) {
1088 if (e1==e2 || e1==e3) throw new Error();
1089 if (e3.next!=e1) throw new Error();
1090 if (e1.t!=null || e2.t!=null || e3.t!=null) throw new Error("non-manifold surface or disagreeing normals");
1093 e1.next.next.t = this;
1095 // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
1097 int color = Math.abs(random.nextInt());
1100 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
1101 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
1102 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
1106 this.colorclass = colorclass;
1107 triangles.add(this);
1109 public E e1() { return e1; }
1110 public E e2() { return e1.next; }
1111 public E e3() { return e1.prev; }
1112 public Vertex v1() { return e1.v1; }
1113 public Vertex v2() { return e1.v2; }
1114 public Vertex v3() { return e1.next.v2; }
1115 public Point p1() { return e1.v1.p; }
1116 public Point p2() { return e1.v2.p; }
1117 public Point p3() { return e1.next.v2.p; }
1118 public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
1119 public boolean has(Vertex v) { return v1()==v || v2()==v || v3()==v; }
1121 public Vertex getOtherVertex(E e) {
1122 if (!hasE(e)) throw new RuntimeException();
1123 if (!e.has(v1())) return v1();
1124 if (!e.has(v2())) return v2();
1125 if (!e.has(v3())) return v3();
1126 throw new RuntimeException();
1129 public void removeFromRTree() { triangles.remove(this); }
1130 public void addToRTree() { triangles.insert(this); }
1131 public void destroy() {
1137 triangles.remove(this);
1140 public void reinsert() { triangles.remove(this); triangles.add(this); }
1142 private boolean destroyed = false;
1143 public boolean destroyed() { return destroyed; }
1145 public boolean shouldBeDrawn() {
1147 if (e1().bindingGroupSize() <= 1) return false;
1148 if (e2().bindingGroupSize() <= 1) return false;
1149 if (e3().bindingGroupSize() <= 1) return false;
1154 public void glTriangle(GL gl, Matrix m) {
1155 gl.glPushName(serial);
1156 gl.glBegin(GL.GL_TRIANGLES);
1162 /** issue gl.glVertex() for each of the triangle's points */
1163 public void glVertices(GL gl, Matrix m) {
1164 if (!shouldBeDrawn()) return;
1165 super.glVertices(gl, m);