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 public class Mesh implements Iterable<Mesh.T> {
16 public static final float EPSILON = (float)0.0001;
17 public static final Random random = new Random();
19 private RTree<T> triangles = new RTree<T>();
20 private PointSet<Vertex> vertices = new PointSet<Vertex>();
22 public boolean immutableVertices;
23 public Mesh error_against = null;
24 public double error = 0;
26 public Mesh(boolean immutableVertices) { this.immutableVertices = immutableVertices; }
28 public void makeVerticesImmutable() { this.immutableVertices = true; }
29 public float error() { return (float)error; }
31 public int size() { return vertices.size(); }
32 public Iterable<Vertex> vertices() { return vertices; }
33 public Iterator<T> iterator() { return triangles.iterator(); }
35 public void rebindPoints() {
37 for(Mesh.T t : this) {
42 // ask edges to re-implement their bindings
43 for(Mesh.T t : this) {
48 System.out.println("rebound!");
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);
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); }
62 /** compute the volume of the mesh */
63 public float volume() {
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;
76 // Vertexices //////////////////////////////////////////////////////////////////////////////
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
83 private boolean illegal = false;
85 public Point getPoint() { return p; }
86 public float error() { return olderror; }
88 private Vertex(Point p) {
91 if (vertices.get(p) != null) throw new Error();
95 public void reinsert() {
96 vertices.remove(this);
98 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) e.t.reinsert();
101 public float olderror = 0;
102 public void setError(float nerror) {
108 public float averageTriangleArea() {
111 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
117 public float averageEdgeLength() {
120 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) {
127 public Matrix _recomputeFundamentalQuadric() {
128 Matrix m = Matrix.ZERO;
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()));
134 return m.times(1/(float)count);
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;
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);
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);
153 if (e.t.aspect() < 0.2) {
154 nerror += (0.2-e.t.aspect()) * 10;
162 public boolean move(Matrix m, boolean ignoreProblems) {
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),
174 for(Vertex v : (Iterable<Vertex>)getBoundPeers())
175 if (good || ignoreProblems) v.reComputeErrorAround();
176 else v.transform(v.oldp, true);
181 /** does NOT update bound pairs! */
182 private boolean transform(Point newp, boolean ignoreProblems) {
184 if (immutableVertices) throw new Error();
186 unApplyQuadricToNeighbor();
189 applyQuadricToNeighbor();
191 if (!ignoreProblems) {
195 for(E e = this.e; e!=null; e=e.pair.next==this.e?null:e.pair.next) e.p2.quadricStale = true;
199 public void checkLegality() {
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;
207 if (!illegal) triangles.range(oldp, this.p, (Visitor<T>)this);
210 public void reComputeErrorAround() {
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();
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;
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; }
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; }
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);
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)
249 public E getE(Point p2) {
250 Vertex v = vertices.get(p2);
251 if (v==null) return null;
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;
260 private void glNormal(GL gl) {
262 gl.glNormal3f(norm.x, norm.y, norm.z);
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)
268 norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
272 public void bindTo(Vertex p) { bindTo(Matrix.ONE, p); }
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;
285 BindingGroup e_bind_peers = e.bind_peers;
286 BindingGroup e_bind_to = e.bind_to;
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);
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);
296 if (e.prev.bind_to.set.contains(eother.next)) {
297 e.prev.prev.bindEdge(eother.next.next);
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);
308 public void shatter(BindingGroup bg1, BindingGroup bg2, boolean triangles) {
310 e.shatter(e.midpoint(), bg1, bg2, triangles);
315 /** [UNIQUE] an edge */
316 public final class E extends HasBindingGroup implements Comparable<E> {
318 public final Vertex p1, p2;
319 T t; // triangle to our "left"
320 E prev; // previous half-edge
321 E next; // next half-edge
322 E pair; // partner half-edge
323 public BindingGroup bind_peers = new BindingGroup(this);
324 public BindingGroup bind_to = bind_peers.other();
325 boolean shattered = false;
327 public boolean intersects(T t) { return t.intersects(p1.p, p2.p); }
329 public void bindingGroupChanged(edu.berkeley.qfat.geom.BindingGroup newBindingGroup_) {
330 edu.berkeley.qfat.geom.BindingGroup<E> newBindingGroup =
331 (edu.berkeley.qfat.geom.BindingGroup<E>)newBindingGroup_;
332 if (newBindingGroup==null) return;
333 if (this==newBindingGroup.getMaster()) return;
335 for(E eother : (Iterable<E>)newBindingGroup) {
336 this.next.bindTo(newBindingGroup.getMatrix(eother), eother.next);
337 this.prev.bindTo(newBindingGroup.getMatrix(eother), eother.prev);
342 public float stretchRatio() {
343 Vertex nearest = error_against.nearest(midpoint());
344 float nearest_distance = midpoint().distance(nearest.p);
345 float other_distance =
346 (p1.p.distance(error_against.nearest(p1.p).p)+
347 p2.p.distance(error_against.nearest(p2.p).p))/2;
348 return nearest_distance/other_distance;
350 public float comparator() {
352 //return t==null?0:(1/t.aspect());
354 public int compareTo(E e) {
355 return e.comparator() > comparator() ? 1 : -1;
357 public void bindEdge(E e, Matrix m) {
359 pair._bindEdge(e.pair, m);
361 public void _bindEdge(E e, Matrix m) {
366 //assumes edges are identical length at binding time
367 Vec reflectionPlaneNormal = e.p2.p.minus(e.p1.p).norm();
368 float a = reflectionPlaneNormal.x;
369 float b = reflectionPlaneNormal.y;
370 float c = reflectionPlaneNormal.z;
371 Matrix reflectionMatrix =
372 new Matrix( 1-2*a*a, -2*a*b, -2*a*c, 0,
373 -2*a*b, 1-2*b*b, -2*b*c, 0,
374 -2*a*c, -2*b*c, 1-2*c*c, 0,
376 m = m.times(Matrix.translate(e.midpoint().minus(Point.ORIGIN))
377 .times(reflectionMatrix)
378 .times(Matrix.translate(Point.ORIGIN.minus(e.midpoint()))));
379 System.out.println(reflectionPlaneNormal);
380 System.out.println(" " + p1.p + " " + m.times(e.p1.p));
381 System.out.println(" " + p2.p + " " + m.times(e.p2.p));
383 if (m.times(e.p1.p).minus(p1.p).mag() > EPSILON) throw new Error();
384 if (m.times(e.p2.p).minus(p2.p).mag() > EPSILON) throw new Error();
388 public void dobind() {
389 //bind_to.dobind(this);
390 for(E e : (Iterable<E>)getBoundPeers()) {
391 if (e==this) continue;
392 p1.bindTo(getBindingMatrix(e), e.p1);
393 p2.bindTo(getBindingMatrix(e), e.p2);
397 public Point shatter() { return shatter(true); }
398 public Point shatter(boolean triangles) { return shatter(midpoint(), null, null, triangles); }
399 public Point shatter(Point mid, BindingGroup bg1, BindingGroup bg2, boolean triangles) {
400 return shatter(mid, bg1, bg2, triangles, false);
402 public Point shatter(Point mid, BindingGroup bg1, BindingGroup bg2, boolean triangles, boolean leader) {
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, triangles);
415 old_bind_to.shatter(bg2.other(), bg1.other(), triangles);
424 newT(r.p, p1.p, mid, null, old_colorclass);
425 newT(r.p, mid, p2.p, null, old_colorclass);
426 bg1.add(p1.getE(mid));
427 bg2.add(p2.getE(mid).pair);
428 if (leader) p1.getE(mid).shatter();
433 public boolean destroyed = false;
434 public void destroy() {
435 if (destroyed) return;
437 pair.destroyed = true;
439 if (t != null) t.destroy();
442 if (pair.t != null) pair.t.destroy();
445 if (next.t != null) next.t.destroy();
446 if (prev.t != null) prev.t.destroy();
450 if (pair.next.t != null) pair.next.t.destroy();
451 if (pair.prev.t != null) pair.next.t.destroy();
457 this.bind_peers = null;
458 pair.bind_peers = null;
459 pair.prev.next = next;
460 next.prev = pair.prev;
461 prev.next = pair.next;
463 if (p1.e == this) p1.e = prev.next;
464 if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
467 private void sync() {
468 this.prev.next = this;
469 this.next.prev = this;
470 this.pair.pair = this;
471 bind_peers.add(this);
472 if (this.next.p1 != p2) throw new Error();
473 if (this.prev.p2 != p1) throw new Error();
474 if (this.p1.e == null) this.p1.e = this;
475 if (!added) added = true;
477 private boolean added = false;
479 public T makeT(int colorclass) { return t==null ? (t = new T(this, colorclass)) : t; }
481 public double dihedralAngle() {
482 Vec v1 = t.norm().times(-1);
483 Vec v2 = pair.t.norm().times(-1);
484 return Math.acos(v1.norm().dot(v2.norm()));
487 /** angle between this half-edge and the next */
488 public double angle() {
489 Vec v1 = next.p2.p.minus(p2.p);
490 Vec v2 = this.p1.p.minus(p2.p);
491 return Math.acos(v1.norm().dot(v2.norm()));
494 public void makeAdjacent(E e) {
495 if (this.next == e) return;
496 if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
497 if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
499 E freeIncident = p2.getFreeIncident(e, this);
501 e.prev.next = freeIncident.next;
502 freeIncident.next.prev = e.prev;
504 freeIncident.next = this.next;
505 this.next.prev = freeIncident;
514 /** creates an isolated edge out in the middle of space */
515 public E(Point p1, Point p2) {
516 if (vertices.get(p1) != null) throw new Error();
517 if (vertices.get(p2) != null) throw new Error();
518 this.p1 = new Vertex(p1);
519 this.p2 = new Vertex(p2);
520 this.prev = this.next = this.pair = new E(this, this, this);
522 this.p2.e = this.pair;
526 /** adds a new half-edge from prev.p2 to p2 */
527 public E(E prev, Point p) {
529 p2 = vertices.get(p);
530 if (p2 == null) p2 = new Vertex(p);
534 if (p2.getE(p1) != null) throw new Error();
536 this.next = this.pair = new E(this, this, prev.next);
538 E q = p2.getFreeIncident();
540 this.next.prev = this;
542 this.prev.next = this;
543 this.pair = new E(q, this, z);
545 if (p2.e==null) p2.e = this.pair;
549 /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
550 public E(E prev, E pair, E next) {
558 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); }
559 public boolean has(Vertex v) { return v==p1 || v==p2; }
560 public float length() { return p1.p.minus(p2.p).mag(); }
561 public String toString() { return p1+"->"+p2; }
565 public E makeE(Point p1, Point p2) {
566 Vertex v1 = vertices.get(p1);
567 Vertex v2 = vertices.get(p2);
568 if (v1 != null && v2 != null) {
570 if (e != null) return e;
572 if (e != null) return e;
574 if (v1 != null) return new E(v1.getFreeIncident(), p2);
575 if (v2 != null) return new E(v2.getFreeIncident(), p1).pair;
576 return new E(p1, p2);
578 public T newT(Point p1, Point p2, Point p3, Vec norm, int colorclass) {
580 Vec norm2 = p3.minus(p1).cross(p2.minus(p1));
581 float dot = norm.dot(norm2);
582 //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within evertsilon of each other: "+norm+" "+norm2);
583 if (dot < 0) { Point p = p1; p1=p2; p2 = p; }
585 E e12 = makeE(p1, p2);
586 E e23 = makeE(p2, p3);
587 E e31 = makeE(p3, p1);
588 while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
589 e12.makeAdjacent(e23);
590 e23.makeAdjacent(e31);
591 e31.makeAdjacent(e12);
593 T ret = e12.makeT(colorclass);
594 if (e12.t == null) throw new Error();
595 if (e23.t == null) throw new Error();
596 if (e31.t == null) throw new Error();
600 /** [UNIQUE] a triangle (face) */
601 public final class T extends Triangle {
603 public final int color;
604 public final int colorclass;
606 T(E e1, int colorclass) {
610 if (e1==e2 || e1==e3) throw new Error();
611 if (e3.next!=e1) throw new Error();
612 if (e1.t!=null || e2.t!=null || e3.t!=null) throw new Error("non-manifold surface or disagreeing normals");
615 e1.next.next.t = this;
617 // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
619 int color = Math.abs(random.nextInt());
622 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
623 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
624 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
628 this.colorclass = colorclass;
631 public E e1() { return e1; }
632 public E e2() { return e1.next; }
633 public E e3() { return e1.prev; }
634 public Vertex v1() { return e1.p1; }
635 public Vertex v2() { return e1.p2; }
636 public Vertex v3() { return e1.next.p2; }
637 public Point p1() { return e1.p1.p; }
638 public Point p2() { return e1.p2.p; }
639 public Point p3() { return e1.next.p2.p; }
640 public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
641 public boolean has(Vertex v) { return v1()==v || v2()==v || v3()==v; }
643 public void removeFromRTree() { triangles.remove(this); }
644 public void addToRTree() { triangles.insert(this); }
645 public void destroy() { triangles.remove(this); }
646 public void reinsert() { triangles.remove(this); triangles.add(this); }
648 public boolean shouldBeDrawn() {
650 if (e1().bind_to==null) return false;
651 if (e2().bind_to==null) return false;
652 if (e3().bind_to==null) return false;
653 if (e1().bind_to.set.size() == 0) return false;
654 if (e2().bind_to.set.size() == 0) return false;
655 if (e3().bind_to.set.size() == 0) return false;
660 /** issue gl.glVertex() for each of the triangle's points */
661 public void glVertices(GL gl) {
662 if (!shouldBeDrawn()) return;
664 Point p1 = v1().goodp;
665 Point p2 = v2().goodp;
666 Point p3 = v3().goodp;