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.wlu.cs.levy.CG.KDTree;
10 import edu.berkeley.qfat.geom.Point;
12 public class Mesh implements Iterable<Mesh.T> {
14 public static final float EPSILON = (float)0.0001;
15 public static final Random random = new Random();
17 private PointSet<Vert> pointset = new PointSet<Vert>();
19 public Iterable<Vert> vertices() { return pointset; }
21 public Iterable<E> edges() {
24 public Iterator<E> iterator() {
26 HashSet<E> hse = new HashSet<E>();
27 for(T t : Mesh.this) {
35 return hse.iterator();
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();
46 public Mesh score_against = null;
47 public double score = 0;
48 public float score() { return (float)score; }
50 public int numedges = 0;
51 public float avgedge = 0;
53 public void rebindPoints() {
55 for(Mesh.T t : this) {
60 // ask edges to re-implement their bindings
61 for(Mesh.T t : this) {
68 public void unApplyQuadricToNeighborAll() {
69 HashSet<Vert> done = new HashSet<Vert>();
71 for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
72 if (done.contains(p)) continue;
74 p.unApplyQuadricToNeighbor();
77 public void recomputeAllFundamentalQuadrics() {
78 HashSet<Vert> done = new HashSet<Vert>();
80 for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
81 if (done.contains(p)) continue;
83 p.recomputeFundamentalQuadric();
86 public float applyQuadricToNeighborAll() {
89 HashSet<Vert> done = new HashSet<Vert>();
91 for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) {
92 if (done.contains(p)) continue;
94 p.applyQuadricToNeighbor();
97 return (float)(dist/num);
100 public void transform(Matrix m) {
101 ArrayList<Vert> set = new ArrayList<Vert>();
102 for (Vert v : pointset)
104 for(Vert v : set) v.transform(m);
107 public float volume() {
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;
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); }
124 public final class Vert extends HasPoint {
126 E e; // some edge *leaving* this point
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 */
132 /** the total error quadric (contributions from all vertices in other mesh for which this is nearest) */
133 Matrix quadric = Matrix.ZERO;
135 Vert bound_to = this;
136 Matrix binding = new Matrix();
138 boolean quadricStale = false;
140 public Matrix errorQuadric() { return quadric; }
141 public Point getPoint() { return p; }
142 public float score() { return oldscore; }
144 private Matrix fundamentalQuadric = null;
145 public Matrix fundamentalQuadric() {
146 if (fundamentalQuadric == null) recomputeFundamentalQuadric();
147 return fundamentalQuadric;
150 private Vert(Point p) {
152 if (pointset.get(p) != null) throw new Error();
156 private void glNormal(GL gl) {
158 gl.glNormal3f(norm.x, norm.y, norm.z);
161 public void recomputeFundamentalQuadric() {
162 //if (!quadricStale && fundamentalQuadric != null) return;
163 quadricStale = false;
164 unApplyQuadricToNeighbor();
165 Matrix m = Matrix.ZERO;
169 m = m.plus(t.norm().fundamentalQuadric(t.centroid()));
171 } while(e != this.e);
172 fundamentalQuadric = m;
173 applyQuadricToNeighbor();
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;
188 public void applyQuadricToNeighbor() {
189 if (score_against == null) return;
191 Vert new_nearest = score_against.nearest(p);
192 if (nearest_in_other_mesh != null && new_nearest == nearest_in_other_mesh) return;
194 if (nearest_in_other_mesh != null) unApplyQuadricToNeighbor();
195 if (nearest_in_other_mesh != null) throw new Error();
197 nearest_in_other_mesh = new_nearest;
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;
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();
211 public void reComputeError() {
215 public void unComputeError() {
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);
228 /** does NOT update bound pairs! */
229 public boolean transform(Matrix m) {
230 unApplyQuadricToNeighbor();
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);
239 } catch (Exception e) {
240 throw new RuntimeException(e);
242 applyQuadricToNeighbor();
244 // should recompute fundamental quadrics of all vertices sharing a face, but we defer...
247 e.p2.quadricStale = true;
249 } while(e != this.e);
251 // FIXME: intersection test needed?
254 for(T t : Mesh.this) {
258 if (!t.has(e.p1) && !t.has(e.p2) && e.intersects(t)) { good = false; break; }
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; }
265 } while(e != this.e);
271 public boolean move(Vec v) {
272 Matrix m = new Matrix(v);
276 good &= p.transform(m);
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);
290 public E getFreeIncident(E start, E before) {
293 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
295 } while(e != before);
299 public E getE(Point p2) {
300 Vert v = pointset.get(p2);
301 if (v==null) return null;
304 public E getE(Vert p2) {
307 if (e==null) return null;
308 if (e.p1 == this && e.p2 == p2) return e;
315 Vec norm = new Vec(0, 0, 0);
318 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
320 } while(e != this.e);
324 public boolean isBoundTo(Vert p) {
327 if (px==this) return true;
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
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;
355 BindingGroup e_bind_peers = e.bind_peers;
356 BindingGroup e_bind_to = e.bind_to;
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);
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);
366 if (e.prev.bind_to.set.contains(eother.next)) {
367 e.prev.prev.bindEdge(eother.next.next);
372 public void dobind(E e) {
373 for(E ebound : set) {
374 e.p1.bind(ebound.p2);
375 e.p2.bind(ebound.p1);
378 public void shatter(BindingGroup bg1, BindingGroup bg2) {
380 e.shatter(e.midpoint(), bg1, bg2);
385 /** [UNIQUE] an edge */
386 public final class E implements Comparable<E> {
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;
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); }
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;
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());
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);
426 public boolean destroyed = false;
427 public void destroy() {
428 if (destroyed) return;
430 pair.destroyed = true;
431 if (next.t != null) next.t.destroy();
432 if (prev.t != null) prev.t.destroy();
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;
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();
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;
467 private boolean added = false;
469 public T makeT(int colorclass) { return t==null ? (t = new T(this, colorclass)) : t; }
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()));
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()));
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");
489 E freeIncident = p2.getFreeIncident(e, this);
491 e.prev.next = freeIncident.next;
492 freeIncident.next.prev = e.prev;
494 freeIncident.next = this.next;
495 this.next.prev = freeIncident;
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);
512 this.p2.e = this.pair;
516 /** adds a new half-edge from prev.p2 to p2 */
517 public E(E prev, Point p) {
519 p2 = pointset.get(p);
520 if (p2 == null) p2 = new Vert(p);
524 if (p2.getE(p1) != null) throw new Error();
526 this.next = this.pair = new E(this, this, prev.next);
528 E q = p2.getFreeIncident();
530 this.next.prev = this;
532 this.prev.next = this;
533 this.pair = new E(q, this, z);
535 if (p2.e==null) p2.e = this.pair;
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) {
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; }
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;
566 double R00, R01, R02, R03,
580 /* c = a × b */
581 c0 = a1 * b2 - a2 * b1;
582 c1 = a2 * b0 - a0 * b2;
583 c2 = a0 * b1 - a1 * b0;
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;
598 R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
599 R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
600 R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
602 /* fill in last row of 4x4 matrix */
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;
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;
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;
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) {
629 if (e != null) return e;
631 if (e != null) return e;
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);
637 public T newT(Point p1, Point p2, Point p3, Vec norm, int colorclass) {
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; }
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);
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();
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(); }
668 T ret = next.removeFirst();
669 if (ret == null) return null;
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);
681 /** [UNIQUE] a triangle (face) */
682 public final class T extends Triangle {
684 public final int color;
685 public final int colorclass;
687 public void destroy() {
690 T(E e1, int colorclass) {
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");
699 e1.next.next.t = this;
701 // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
703 int color = Math.abs(random.nextInt());
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; }
712 this.colorclass = colorclass;
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; }
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;