3 import java.awt.event.*;
5 import javax.media.opengl.*;
6 import javax.media.opengl.glu.*;
7 import edu.wlu.cs.levy.CG.KDTree;
9 public class Geom implements Iterable<Geom.T> {
11 private KDTree kd = new KDTree(3);
13 public static float EPSILON = (float)0.0001;
14 public static Random random = new Random();
16 private HashMap<P,V> ps = new HashMap<P,V>();
17 public HashSet<E> es = new HashSet<E>();
18 public ArrayList<T> ts = new ArrayList<T>();
20 public Iterator<T> iterator() { return ts.iterator(); }
22 public P origin() { return newP(0, 0, 0); }
24 public Geom score_against = null;
25 public double score = 0;
26 public float score() {
30 public float rescore() {
33 HashSet<V> done = new HashSet<V>();
35 for(V p : new V[] { t.p1(), t.p2(), t.p3() }) {
36 if (done.contains(p)) continue;
41 for(V p : new V[] { t.p1(), t.p2(), t.p3() })
45 for(V p : new V[] { t.p1(), t.p2(), t.p3() })
47 return (float)(dist/num);
50 public void transform(M m) {
51 ArrayList<V> set = new ArrayList<V>();
52 set.addAll(ps.values());
53 for(V v : set) v.transform(m);
56 public Vec diagonal() {
57 float min_x = Float.MAX_VALUE;
58 float min_y = Float.MAX_VALUE;
59 float min_z = Float.MAX_VALUE;
60 float max_x = Float.MIN_VALUE;
61 float max_y = Float.MIN_VALUE;
62 float max_z = Float.MIN_VALUE;
63 for(P p : ps.keySet()) {
64 if (p.x < min_x) min_x = p.x;
65 if (p.y < min_y) min_y = p.y;
66 if (p.z < min_z) min_z = p.z;
67 if (p.x > max_x) max_x = p.x;
68 if (p.y > max_y) max_y = p.y;
69 if (p.z > max_z) max_z = p.z;
71 return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
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(P 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 P((float)(max_x + min_x)/2,
90 (float)(max_y + min_y)/2,
91 (float)(max_z + min_z)/2);
94 public P newP(double x, double y, double z) { return newP((float)x, (float)y, (float)z); }
95 public P newP(float x, float y, float z) { return new P(x, y, z); }
97 public T newT(V p12, V p23, V p31, Vec norm) {
98 Vec norm2 = p31.minus(p12).cross(p23.minus(p12));
99 float dot = norm.dot(norm2);
100 //if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
101 if (dot < 0) { V p = p12; p12=p23; p23 = p; }
102 return newT(p12, p23, p31);
106 public float volume() {
109 double area = t.area();
110 Vec origin_to_centroid = new Vec(newP(0, 0, 0), t.centroid());
111 boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
112 double height = Math.abs(t.norm().dot(origin_to_centroid));
113 total += ((facingAway ? 1 : -1) * area * height) / 3.0;
118 public P nearest(P p) {
120 try { results = kd.nearest(new double[]{p.x,p.y,p.z},1); } catch (Exception e) { throw new Error(e); }
121 return (P)results[0];
124 public T newT(V p1, V p2, V p3) {
128 E e12 = p1.makeE(p2);
129 E e23 = p2.makeE(p3);
130 E e31 = p3.makeE(p1);
131 while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
132 e12.makeAdjacent(e23);
133 e23.makeAdjacent(e31);
134 e31.makeAdjacent(e12);
137 if (e12.t == null) throw new Error();
138 if (e23.t == null) throw new Error();
139 if (e31.t == null) throw new Error();
143 public final class V extends P {
145 super(p.x, p.y, p.z);
146 if (ps.get(p) != null) throw new Error();
150 public int hashCode() {
154 public void kdremove() {
155 if (!inserted) return;
158 try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { }
160 public void kdinsert() {
161 if (inserted) return;
164 try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); }
167 public float score() { return oldscore; }
168 public void unscore() {
169 if (watch == null) return;
174 if (watch.watch_count==0) {
181 public V partner() { return watch==null ? this : watch; }
182 public V watchback() { return watch_count==0 ? partner() :
183 newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count).register(); }
184 public void rescore() {
185 if (score_against == null) return;
190 if (watch != null) unscore();
193 watch = (V)score_against.nearest(po);
195 // don't attract to vertices that face the other way
196 if (watch.norm().dot(norm()) < 0) {
199 watch.watch_x += po.x;
200 watch.watch_y += po.y;
201 watch.watch_z += po.z;
207 if (watch_count==0) s1 = 0;
208 else s1 = this.distance(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
209 s2 = watch==null ? 0 : po.distance(watch);
210 oldscore = (float)(s1 + s2);
215 /** does NOT update bound pairs! */
216 public boolean transform(M m) {
217 // FIXME: screws up kdtree
218 // FIXME: screws up hashmap
221 if (ps.get(this)==null) throw new Error();
223 float newx = m.a*x + m.b*y + m.c*z + m.d;
224 float newy = m.e*x + m.f*y + m.g*z + m.h;
225 float newz = m.i*x + m.j*y + m.k*z + m.l;
229 // FIXME: what if we move onto exactly where another point is?
230 ps.put((V)this,(V)this);
231 } catch (Exception e) {
232 throw new RuntimeException(e);
238 for(E e = this.e; ;) {
239 if (e.intersects(t)) { good = false; break; }
241 if (e == this.e) break;
246 if (t==this.t) continue;
247 if (this.intersects(t)) good = false;
252 public boolean move(Vec v) {
257 good &= p.transform(m);
258 v = v.times(binding); // bleh wrong
264 public E makeE(V p2) {
267 if (e != null) return e;
269 if (this.e == null && p2.e == null) return this.e = new E(this, p2);
270 if (this.e == null && p2.e != null) return p2.makeE(this).pair;
271 return new E(getFreeIncident(), p2);
274 public E getFreeIncident() {
275 E ret = getFreeIncident(e, e);
276 if (ret != null) return ret;
277 ret = getFreeIncident(e.pair.next, e.pair.next);
278 if (ret == null) throw new Error("unable to find free incident to " + this);
282 public E getFreeIncident(E start, E before) {
285 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
287 } while(e != before);
291 public E getE(V p2) {
295 if (e==null) return null;
296 if (e.p1 == this && e.p2 == p2) return e;
302 public boolean isBoundTo(V p) {
306 if (px==this) return true;
312 public void unbind() { bound_to = this; binding = new M(); }
313 public void bind(V p) { bind(p, new M()); }
314 public void bind(V p, M binding) {
316 if (isBoundTo(p)) return;
317 V temp_bound_to = p.bound_to;
318 M temp_binding = p.binding;
319 p.bound_to = this.bound_to;
320 p.binding = binding.times(this.binding); // FIXME: may have order wrong here
321 this.bound_to = temp_bound_to;
322 this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
325 Vec norm = new Vec(0, 0, 0);
328 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
330 } while(e != this.e);
345 E e; // some edge *leaving* this point
350 boolean inserted = false;
351 public float distance(P p) { return distance(p.x, p.y, p.z); }
352 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)); }
354 public V register() { V v = ps.get(this); return v==null ? new V(this) : v; }
356 public P times(M m) { return m.times(this); }
358 public P(float x, float y, float z) {
359 this.x = x; this.y = y; this.z = z;
362 public Vec minus(P p) { return new Vec(x-p.x, y-p.y, z-p.z); }
363 public P plus(Vec v) { return newP(x+v.x, y+v.y, z+v.z); }
364 public boolean equals(Object o) {
365 if (o==null || !(o instanceof P)) return false;
367 return p.x==x && p.y==y && p.z==z;
369 // FIXME: moving a point alters its hashCode
370 public int hashCode() {
372 Float.floatToIntBits(x) ^
373 Float.floatToIntBits(y) ^
374 Float.floatToIntBits(z);
376 public void glVertex(GL gl) { _glVertex(gl); }
377 private void _glVertex(GL gl) { gl.glVertex3f(x, y, z); }
378 public String toString() { return "("+x+","+y+","+z+")"; }
381 /** vector in 3-space */
382 public final class Vec {
383 public final float x, y, z;
384 public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
385 public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
386 public Vec(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
387 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); }
388 public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
389 public Vec norm() { return mag()==0 ? this : div(mag()); }
390 public Vec times(M m) { return m.apply(this); }
391 public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
392 public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
393 public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
394 public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
395 public String toString() { return "<"+x+","+y+","+z+">"; }
398 public class BindingGroup {
399 public HashSet<E> es = new HashSet<E>();
400 public BindingGroup() { }
401 public BindingGroup(E e) {
404 public void add(E e) {
405 if (e.bg != null) { merge(e.bg); return; }
409 public void merge(BindingGroup bg) {
417 /** [UNIQUE] an edge */
418 public final class E implements Comparable<E> {
420 public boolean intersects(T t) {
421 double A0=t.p1().x, A1=t.p1().y, A2=t.p1().z;
422 double B0=t.p2().x, B1=t.p2().y, B2=t.p2().z;
423 double C0=t.p3().x, C1=t.p3().y, C2=t.p3().z;
424 double j0=p1.x, j1=p1.y, j2=p1.z;
425 double k0=p2.x, k1=p2.y, k2=p2.z;
433 double R00, R01, R02, R03,
447 /* c = a × b */
448 c0 = a1 * b2 - a2 * b1;
449 c1 = a2 * b0 - a0 * b2;
450 c2 = a0 * b1 - a1 * b0;
452 /* M^(-1) = (1/det(M)) * adj(M) */
453 in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
454 R00 = (b1 * c2 - b2 * c1) * in_det;
455 R01 = (b2 * c0 - b0 * c2) * in_det;
456 R02 = (b0 * c1 - b1 * c0) * in_det;
457 R10 = (c1 * a2 - c2 * a1) * in_det;
458 R11 = (c2 * a0 - c0 * a2) * in_det;
459 R12 = (c0 * a1 - c1 * a0) * in_det;
465 R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
466 R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
467 R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
469 /* fill in last row of 4x4 matrix */
473 J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
474 K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
475 if (J2 * K2 >= 0) return false;
477 J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
478 K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
479 i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
480 if (i0 < 0 || i0 > 1) return false;
482 J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
483 K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
484 i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
485 if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
490 public int compareTo(E e) {
491 return e.length() > length() ? 1 : -1;
494 public final V p1, p2;
495 T t; // triangle to our "left"
496 E prev; // previous half-edge
497 E next; // next half-edge
498 E pair; // partner half-edge
501 public BindingGroup bg = new BindingGroup(this);
503 public void bind(E e) { bind(e, new M()); }
504 public void bind(E e, M m) { e.bg.add(this); }
506 public void dobind() {
507 if (bg==null) return;
509 if (ex==this) continue;
515 boolean shattered = false;
516 public V shatter() { return shatter(midpoint().register(), null, null); }
517 public V shatter(V mid, BindingGroup bg1, BindingGroup bg2) {
518 mid = mid.register();
519 if (shattered) return mid;
526 if (bg1==null) bg1 = new BindingGroup();
527 if (bg2==null) bg2 = new BindingGroup();
528 for(E e : bg.es) e.shatter(e.midpoint().register(), bg1, bg2);
534 bg1.add(p1.getE(mid));
535 bg2.add(mid.getE(p2));
539 public boolean destroyed = false;
540 public void destroy() {
541 if (destroyed) return;
543 pair.destroyed = true;
544 if (next.t != null) next.t.destroy();
545 if (prev.t != null) prev.t.destroy();
546 if (pair.next.t != null) ts.remove(pair.next.t);
547 if (pair.prev.t != null) ts.remove(pair.prev.t);
554 pair.prev.next = next;
555 next.prev = pair.prev;
556 prev.next = pair.next;
558 if (p1.e == this) p1.e = prev.next;
559 if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
562 avgedge -= this.length();
563 avgedge -= pair.length();
568 private void sync() {
569 this.prev.next = this;
570 this.next.prev = this;
571 this.pair.pair = this;
572 if (this.next.p1 != p2) throw new Error();
573 if (this.prev.p2 != p1) throw new Error();
574 if (this.p1.e == null) this.p1.e = this;
582 private boolean added = false;
584 public T makeT() { return t==null ? (t = new T(this)) : t; }
586 /** angle between this half-edge and the next */
587 public double angle() {
588 Vec v1 = next.p2.minus(p2);
589 Vec v2 = this.p1.minus(p2);
590 return Math.acos(v1.norm().dot(v2.norm()));
593 public void makeAdjacent(E e) {
594 if (this.next == e) return;
595 if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
596 if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
598 E freeIncident = p2.getFreeIncident(e, this);
600 e.prev.next = freeIncident.next;
601 freeIncident.next.prev = e.prev;
603 freeIncident.next = this.next;
604 this.next.prev = freeIncident;
613 /** creates an isolated edge out in the middle of space */
614 public E(V p1, V p2) {
617 if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
620 this.prev = this.next = this.pair = new E(this, this, this);
624 /** adds a new half-edge from prev.p2 to p2 */
625 public E(E prev, V p2) {
630 if (p2.getE(p1) != null) throw new Error();
632 this.next = this.pair = new E(this, this, prev.next);
634 E q = p2.getFreeIncident();
636 this.next.prev = this;
638 this.prev.next = this;
639 this.pair = new E(q, this, z);
644 /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
645 public E(E prev, E pair, E next) {
653 public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2).register(); }
654 public boolean has(P p) {
656 return p==p1 || p==p2;
658 public float length() { return p1.minus(p2).mag(); }
659 public String toString() { return p1+"->"+p2; }
662 /** [UNIQUE] a triangle (face) */
663 public final class T {
665 public final int color;
667 public void destroy() {
671 public P nearest(P p) {
672 float d1 = p1().distance(p);
673 float d2 = p2().distance(p);
674 float d3 = p3().distance(p);
675 if (d1 < d2 && d1 < d3) return p1();
676 if (d2 < d3) return p2();
684 if (e1==e2 || e1==e3) throw new Error();
685 if (e3.next!=e1) throw new Error();
686 if (e1.t!=null || e2.t!=null || e3.t!=null)
687 throw new Error("non-manifold surface or disagreeing normals");
690 e1.next.next.t = this;
692 // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
694 int color = Math.abs(random.nextInt());
697 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
698 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
699 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
711 public V p1() { return e1.p1; }
712 public V p2() { return e1.p2; }
713 public V p3() { return e1.next.p2; }
714 public E e1() { return e1; }
715 public E e2() { return e1.next; }
716 public E e3() { return e1.prev; }
717 public Vec norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
718 public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
719 public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
721 public float area() {
722 return (float)Math.abs(0.5 * e1().length() * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
725 public void glVertices(GL gl) {
731 public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
732 (p1().y+p2().y+p3().y)/3,
733 (p1().z+p2().z+p3().z)/3); }
734 public float diameter() {
735 // FIXME: what is this supposed to be?
736 return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
751 public final float a, b, c, d, e, f, g, h, i, j, k, l;
752 public M() { this(1); }
753 public M(float scale) {
755 l = h = d = e = b = i = c = j = g = 0;
757 public M(float scalex, float scaley, float scalez) {
761 l = h = d = e = b = i = c = j = g = 0;
763 public M(Vec translate) {
764 d = translate.x; h = translate.y; l = translate.z;
766 b = c = e = g = i = j = 0;
768 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) {
769 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;
770 this.j = j; this.k = k; this.l = l;
772 public M times(float x) {
773 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);
775 public M(Vec axis, float angle) {
776 double q = Math.cos(angle);
777 double s = Math.sin(angle);
779 a = (float)(q + axis.x*axis.x*t);
780 f = (float)(q + axis.y*axis.y*t);
781 k = (float)(q + axis.z*axis.z*t);
782 double tmp1 = axis.x*axis.y*t;
783 double tmp2 = axis.z*s;
784 e = (float)(tmp1 + tmp2);
785 b = (float)(tmp1 - tmp2);
786 tmp1 = axis.x*axis.z*t;
788 i = (float)(tmp1 - tmp2);
789 c = (float)(tmp1 + tmp2);
790 tmp1 = axis.y*axis.z*t;
792 j = (float)(tmp1 + tmp2);
793 g = (float)(tmp1 - tmp2);
796 public P times(P p) {
797 return newP(a*p.x + b*p.y + c*p.z + d,
798 e*p.x + f*p.y + g*p.z + h,
799 i*p.x + j*p.y + k*p.z + l);
801 public P apply(P p) { return p; }
802 public Vec apply(Vec v) { return v; }
803 public M invert() { return this; }
804 public M times(M m) { return this; }
807 public void unbind() {
809 for(Geom.T t : this) {
817 for(Geom.T t : this) {
823 public int numedges = 0;
824 public float avgedge = 0;