1 package edu.berkeley.qfat;
4 import java.awt.event.*;
6 import javax.media.opengl.*;
7 import javax.media.opengl.glu.*;
8 import edu.wlu.cs.levy.CG.KDTree;
10 public class Geom implements Iterable<Geom.T> {
12 private KDTree kd = new KDTree(3);
14 public static float EPSILON = (float)0.0001;
15 public static Random random = new Random();
17 private HashMap<Point,Vert> ps = new HashMap<Point,Vert>();
18 public HashSet<E> es = new HashSet<E>();
19 public ArrayList<T> ts = new ArrayList<T>();
21 public Iterator<T> iterator() { return ts.iterator(); }
23 public Point origin() { return new Point(0, 0, 0); }
25 public Geom score_against = null;
26 public double score = 0;
27 public float score() { return (float)score; }
29 public void unbind() {
31 for(Geom.T t : this) {
39 for(Geom.T t : this) {
45 public int numedges = 0;
46 public float avgedge = 0;
48 public float rescore() {
51 HashSet<Vert> done = new HashSet<Vert>();
53 for(Vert p : new Vert[] { t.p1(), t.p2(), t.p3() }) {
54 if (done.contains(p)) continue;
59 for(Vert p : new Vert[] { t.p1(), t.p2(), t.p3() })
63 for(Vert p : new Vert[] { t.p1(), t.p2(), t.p3() })
65 return (float)(dist/num);
68 public void transform(M m) {
69 ArrayList<Vert> set = new ArrayList<Vert>();
70 set.addAll(ps.values());
71 for(Vert v : set) v.transform(m);
74 public Vec diagonal() {
75 float min_x = Float.MAX_VALUE;
76 float min_y = Float.MAX_VALUE;
77 float min_z = Float.MAX_VALUE;
78 float max_x = Float.MIN_VALUE;
79 float max_y = Float.MIN_VALUE;
80 float max_z = Float.MIN_VALUE;
81 for(Point p : ps.keySet()) {
82 if (p.x < min_x) min_x = p.x;
83 if (p.y < min_y) min_y = p.y;
84 if (p.z < min_z) min_z = p.z;
85 if (p.x > max_x) max_x = p.x;
86 if (p.y > max_y) max_y = p.y;
87 if (p.z > max_z) max_z = p.z;
89 return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
92 public Point centroid() {
93 float min_x = Float.MAX_VALUE;
94 float min_y = Float.MAX_VALUE;
95 float min_z = Float.MAX_VALUE;
96 float max_x = Float.MIN_VALUE;
97 float max_y = Float.MIN_VALUE;
98 float max_z = Float.MIN_VALUE;
99 for(Point p : ps.keySet()) {
100 if (p.x < min_x) min_x = p.x;
101 if (p.y < min_y) min_y = p.y;
102 if (p.z < min_z) min_z = p.z;
103 if (p.x > max_x) max_x = p.x;
104 if (p.y > max_y) max_y = p.y;
105 if (p.z > max_z) max_z = p.z;
107 return new Point((float)(max_x + min_x)/2,
108 (float)(max_y + min_y)/2,
109 (float)(max_z + min_z)/2);
112 public T newT(Vert p12, Vert p23, Vert p31, Vec norm) {
113 Vec norm2 = p31.p.minus(p12.p).cross(p23.p.minus(p12.p));
114 float dot = norm.dot(norm2);
115 //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
116 if (dot < 0) { Vert p = p12; p12=p23; p23 = p; }
117 return newT(p12, p23, p31);
120 public float volume() {
123 double area = t.area();
124 Vec origin_to_centroid = new Vec(new Point(0, 0, 0), t.centroid());
125 boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
126 double height = Math.abs(t.norm().dot(origin_to_centroid));
127 total += ((facingAway ? 1 : -1) * area * height) / 3.0;
132 public Vert nearest(Point p) {
134 try { results = kd.nearest(new double[]{p.x,p.y,p.z},1); } catch (Exception e) { throw new Error(e); }
135 return (Vert)results[0];
138 public T newT(Vert p1, Vert p2, Vert p3) {
139 E e12 = p1.makeE(p2);
140 E e23 = p2.makeE(p3);
141 E e31 = p3.makeE(p1);
142 while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
143 e12.makeAdjacent(e23);
144 e23.makeAdjacent(e31);
145 e31.makeAdjacent(e12);
148 if (e12.t == null) throw new Error();
149 if (e23.t == null) throw new Error();
150 if (e31.t == null) throw new Error();
154 public class BindingGroup {
155 public HashSet<E> es = new HashSet<E>();
156 public BindingGroup() { }
157 public BindingGroup(E e) {
160 public void add(E e) {
161 if (e.bg != null) { merge(e.bg); return; }
165 public void merge(BindingGroup bg) {
173 public final class Vert {
175 public Vert(Point p) {
177 if (ps.get(p) != null) throw new Error();
178 ps.put(this.p, this);
180 public void kdremove() {
181 if (!inserted) return;
183 try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { }
185 public void kdinsert() {
186 if (inserted) return;
188 try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); }
191 public float score() { return oldscore; }
192 public void unscore() {
193 if (watch == null) return;
194 watch.watch_x -= p.x;
195 watch.watch_y -= p.y;
196 watch.watch_z -= p.z;
198 if (watch.watch_count==0) {
205 public Vert partner() { return watch==null ? this : watch; }
206 public Vert watchback() { return watch_count==0 ? partner() :
207 register(new Point(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count)); }
208 public void rescore() {
209 if (score_against == null) return;
214 if (watch != null) unscore();
217 watch = score_against.nearest(po.p);
219 // don't attract to vertices that face the other way
220 if (watch.norm().dot(norm()) < 0) {
223 watch.watch_x += po.p.x;
224 watch.watch_y += po.p.y;
225 watch.watch_z += po.p.z;
231 if (watch_count==0) s1 = 0;
232 else s1 = p.distance(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
233 s2 = watch==null ? 0 : po.p.distance(watch.p);
234 oldscore = (float)(s1 + s2);
239 /** does NOT update bound pairs! */
240 public boolean transform(M m) {
241 // FIXME: screws up kdtree
242 // FIXME: screws up hashmap
245 if (ps.get(this.p)==null) throw new Error();
247 float newx = m.a*p.x + m.b*p.y + m.c*p.z + m.d;
248 float newy = m.e*p.x + m.f*p.y + m.g*p.z + m.h;
249 float newz = m.i*p.x + m.j*p.y + m.k*p.z + m.l;
250 this.p = new Point(newx, newy, newz);
251 // FIXME: what if we move onto exactly where another point is?
252 ps.put(this.p,(Vert)this);
253 } catch (Exception e) {
254 throw new RuntimeException(e);
260 for(E e = this.e; ;) {
261 if (e.intersects(t)) { good = false; break; }
263 if (e == this.e) break;
268 if (t==this.t) continue;
269 if (this.intersects(t)) good = false;
274 public boolean move(Vec v) {
279 good &= p.transform(m);
280 v = v.times(binding); // bleh wrong
286 public E makeE(Vert p2) {
288 if (e != null) return e;
290 if (this.e == null && p2.e == null) return this.e = new E(this, p2);
291 if (this.e == null && p2.e != null) return p2.makeE(this).pair;
292 return new E(getFreeIncident(), p2);
295 public E getFreeIncident() {
296 E ret = getFreeIncident(e, e);
297 if (ret != null) return ret;
298 ret = getFreeIncident(e.pair.next, e.pair.next);
299 if (ret == null) throw new Error("unable to find free incident to " + this);
303 public E getFreeIncident(E start, E before) {
306 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
308 } while(e != before);
312 public E getE(Vert p2) {
315 if (e==null) return null;
316 if (e.p1 == this && e.p2 == p2) return e;
322 public boolean isBoundTo(Vert p) {
325 if (px==this) return true;
331 public void unbind() { bound_to = this; binding = new M(); }
332 public void bind(Vert p) { bind(p, new M()); }
333 public void bind(Vert p, M binding) {
334 if (isBoundTo(p)) return;
335 Vert temp_bound_to = p.bound_to;
336 M temp_binding = p.binding;
337 p.bound_to = this.bound_to;
338 p.binding = binding.times(this.binding); // FIXME: may have order wrong here
339 this.bound_to = temp_bound_to;
340 this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
343 Vec norm = new Vec(0, 0, 0);
346 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
348 } while(e != this.e);
352 Vert bound_to = this;
358 E e; // some edge *leaving* this point
361 boolean inserted = false;
364 /** [UNIQUE] an edge */
365 public final class E implements Comparable<E> {
367 public boolean intersects(T t) {
368 double A0=t.p1().p.x, A1=t.p1().p.y, A2=t.p1().p.z;
369 double B0=t.p2().p.x, B1=t.p2().p.y, B2=t.p2().p.z;
370 double C0=t.p3().p.x, C1=t.p3().p.y, C2=t.p3().p.z;
371 double j0=p1.p.x, j1=p1.p.y, j2=p1.p.z;
372 double k0=p2.p.x, k1=p2.p.y, k2=p2.p.z;
380 double R00, R01, R02, R03,
394 /* c = a × b */
395 c0 = a1 * b2 - a2 * b1;
396 c1 = a2 * b0 - a0 * b2;
397 c2 = a0 * b1 - a1 * b0;
399 /* M^(-1) = (1/det(M)) * adj(M) */
400 in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
401 R00 = (b1 * c2 - b2 * c1) * in_det;
402 R01 = (b2 * c0 - b0 * c2) * in_det;
403 R02 = (b0 * c1 - b1 * c0) * in_det;
404 R10 = (c1 * a2 - c2 * a1) * in_det;
405 R11 = (c2 * a0 - c0 * a2) * in_det;
406 R12 = (c0 * a1 - c1 * a0) * in_det;
412 R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
413 R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
414 R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
416 /* fill in last row of 4x4 matrix */
420 J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
421 K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
422 if (J2 * K2 >= 0) return false;
424 J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
425 K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
426 i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
427 if (i0 < 0 || i0 > 1) return false;
429 J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
430 K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
431 i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
432 if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
437 public int compareTo(E e) {
438 return e.length() > length() ? 1 : -1;
441 public final Vert p1, p2;
442 T t; // triangle to our "left"
443 E prev; // previous half-edge
444 E next; // next half-edge
445 E pair; // partner half-edge
448 public BindingGroup bg = new BindingGroup(this);
450 public void bind(E e) { bind(e, new M()); }
451 public void bind(E e, M m) { e.bg.add(this); }
453 public void dobind() {
454 if (bg==null) return;
456 if (ex==this) continue;
462 boolean shattered = false;
463 public Vert shatter() { return shatter(register(midpoint()), null, null); }
464 public Vert shatter(Vert mid, BindingGroup bg1, BindingGroup bg2) {
465 if (shattered) return mid;
472 if (bg1==null) bg1 = new BindingGroup();
473 if (bg2==null) bg2 = new BindingGroup();
474 for(E e : bg.es) e.shatter(register(e.midpoint()), bg1, bg2);
480 bg1.add(p1.getE(mid));
481 bg2.add(mid.getE(p2));
485 public boolean destroyed = false;
486 public void destroy() {
487 if (destroyed) return;
489 pair.destroyed = true;
490 if (next.t != null) next.t.destroy();
491 if (prev.t != null) prev.t.destroy();
492 if (pair.next.t != null) ts.remove(pair.next.t);
493 if (pair.prev.t != null) ts.remove(pair.prev.t);
500 pair.prev.next = next;
501 next.prev = pair.prev;
502 prev.next = pair.next;
504 if (p1.e == this) p1.e = prev.next;
505 if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
508 avgedge -= this.length();
509 avgedge -= pair.length();
514 private void sync() {
515 this.prev.next = this;
516 this.next.prev = this;
517 this.pair.pair = this;
518 if (this.next.p1 != p2) throw new Error();
519 if (this.prev.p2 != p1) throw new Error();
520 if (this.p1.e == null) this.p1.e = this;
528 private boolean added = false;
530 public T makeT() { return t==null ? (t = new T(this)) : t; }
532 /** angle between this half-edge and the next */
533 public double angle() {
534 Vec v1 = next.p2.p.minus(p2.p);
535 Vec v2 = this.p1.p.minus(p2.p);
536 return Math.acos(v1.norm().dot(v2.norm()));
539 public void makeAdjacent(E e) {
540 if (this.next == e) return;
541 if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
542 if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
544 E freeIncident = p2.getFreeIncident(e, this);
546 e.prev.next = freeIncident.next;
547 freeIncident.next.prev = e.prev;
549 freeIncident.next = this.next;
550 this.next.prev = freeIncident;
559 /** creates an isolated edge out in the middle of space */
560 public E(Vert p1, Vert p2) {
561 if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
564 this.prev = this.next = this.pair = new E(this, this, this);
568 /** adds a new half-edge from prev.p2 to p2 */
569 public E(E prev, Vert p2) {
573 if (p2.getE(p1) != null) throw new Error();
575 this.next = this.pair = new E(this, this, prev.next);
577 E q = p2.getFreeIncident();
579 this.next.prev = this;
581 this.prev.next = this;
582 this.pair = new E(q, this, z);
587 /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
588 public E(E prev, E pair, E next) {
596 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); }
597 public boolean has(Vert v) { return v==p1 || v==p2; }
598 public float length() { return p1.p.minus(p2.p).mag(); }
599 public String toString() { return p1+"->"+p2; }
602 /** [UNIQUE] a triangle (face) */
603 public final class T {
605 public final int color;
607 public void destroy() {
611 public Vert nearest(Point p) {
612 float d1 = p1().p.distance(p);
613 float d2 = p2().p.distance(p);
614 float d3 = p3().p.distance(p);
615 if (d1 < d2 && d1 < d3) return p1();
616 if (d2 < d3) return p2();
624 if (e1==e2 || e1==e3) throw new Error();
625 if (e3.next!=e1) throw new Error();
626 if (e1.t!=null || e2.t!=null || e3.t!=null)
627 throw new Error("non-manifold surface or disagreeing normals");
630 e1.next.next.t = this;
632 // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
634 int color = Math.abs(random.nextInt());
637 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
638 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
639 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
651 public Vert p1() { return e1.p1; }
652 public Vert p2() { return e1.p2; }
653 public Vert p3() { return e1.next.p2; }
654 public E e1() { return e1; }
655 public E e2() { return e1.next; }
656 public E e3() { return e1.prev; }
657 public Vec norm() { return p2().p.minus(p1().p).cross(p3().p.minus(p1().p)).norm(); }
658 public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
659 public boolean has(Vert v) { return p1()==v || p2()==v || p3()==v; }
661 public float area() {
662 return (float)Math.abs(0.5 * e1().length() * new Vec(p1().p, p2().p).norm().dot(new Vec(p2().p, p3().p)));
665 public void glVertices(GL gl) {
671 public Point centroid() { return new Point((p1().p.x+p2().p.x+p3().p.x)/3,
672 (p1().p.y+p2().p.y+p3().p.y)/3,
673 (p1().p.z+p2().p.z+p3().p.z)/3); }
674 public float diameter() {
675 // FIXME: what is this supposed to be?
676 return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
682 public Vert register(Point p) { Vert v = ps.get(p); return v==null ? new Vert(p) : v; }
684 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
686 /** point in 3-space; immutable */
687 public static final class Point {
688 public final float x, y, z;
689 public Point(double x, double y, double z) { this((float)x, (float)y, (float)z); }
690 public Point(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
691 public float distance(Point p) { return distance(p.x, p.y, p.z); }
692 public float distance(float ox, float oy, float oz) { return (float)Math.sqrt((x-ox)*(x-ox)+(y-oy)*(y-oy)+(z-oz)*(z-oz)); }
693 public Point times(M m) { return m.times(this); }
694 public Vec minus(Point p) { return new Vec(x-p.x, y-p.y, z-p.z); }
695 public Point plus(Vec v) { return new Point(x+v.x, y+v.y, z+v.z); }
696 public boolean equals(Object o) { return o!=null && (o instanceof Point) && ((Point)o).x==x && ((Point)o).y==y && ((Point)o).z==z; }
697 public void glVertex(GL gl) { _glVertex(gl); }
698 private void _glVertex(GL gl) { gl.glVertex3f(x, y, z); }
699 public String toString() { return "("+x+","+y+","+z+")"; }
700 public int hashCode() { return Float.floatToIntBits(x) ^ Float.floatToIntBits(y) ^ Float.floatToIntBits(z); }
703 /** vector in 3-space; immutable */
704 public static final class Vec {
705 public final float x, y, z;
706 public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
707 public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
708 public Vec(Point p1, Point p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
709 public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
710 public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
711 public Vec norm() { return mag()==0 ? this : div(mag()); }
712 public Vec times(M m) { return m.apply(this); }
713 public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
714 public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
715 public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
716 public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
717 public String toString() { return "<"+x+","+y+","+z+">"; }
720 /** affine matrix; immutable */
721 public static class M {
728 public final float a, b, c, d, e, f, g, h, i, j, k, l;
729 public M() { this(1); }
730 public M(float scale) {
732 l = h = d = e = b = i = c = j = g = 0;
734 public M(float scalex, float scaley, float scalez) {
738 l = h = d = e = b = i = c = j = g = 0;
740 public M(Vec translate) {
741 d = translate.x; h = translate.y; l = translate.z;
743 b = c = e = g = i = j = 0;
745 public M(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l) {
746 this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h; this.i = i;
747 this.j = j; this.k = k; this.l = l;
749 public M times(float x) {
750 return new M(a*x, b*x, c*x, d*x, e*x, f*x, g*x, h*x, i*x, j*x, k*x, l*x);
752 public M(Vec axis, float angle) {
753 double q = Math.cos(angle);
754 double s = Math.sin(angle);
756 a = (float)(q + axis.x*axis.x*t);
757 f = (float)(q + axis.y*axis.y*t);
758 k = (float)(q + axis.z*axis.z*t);
759 double tmp1 = axis.x*axis.y*t;
760 double tmp2 = axis.z*s;
761 e = (float)(tmp1 + tmp2);
762 b = (float)(tmp1 - tmp2);
763 tmp1 = axis.x*axis.z*t;
765 i = (float)(tmp1 - tmp2);
766 c = (float)(tmp1 + tmp2);
767 tmp1 = axis.y*axis.z*t;
769 j = (float)(tmp1 + tmp2);
770 g = (float)(tmp1 - tmp2);
773 public Point times(Point p) {
774 return new Point(a*p.x + b*p.y + c*p.z + d,
775 e*p.x + f*p.y + g*p.z + h,
776 i*p.x + j*p.y + k*p.z + l);
778 public Point apply(Point p) { return p; }
779 public Vec apply(Vec v) { return v; }
780 public M invert() { return this; }
781 public M times(M m) { return this; }