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,P> ps = new HashMap<P,P>();
17 //public PriorityQueue<E> es = new PriorityQueue<E>();
18 public HashSet<E> es = new HashSet<E>();
19 //private HashSet<T> ts = new HashSet<T>();
20 public ArrayList<T> ts = new ArrayList<T>();
22 public Iterator<T> iterator() { return ts.iterator(); }
24 public P origin() { return newP(0, 0, 0); }
26 public Geom score_against = null;
27 public double score = 0;
28 public float score() {
32 public float rescore() {
35 HashSet<P> done = new HashSet<P>();
37 for(P p : new P[] { t.p1(), t.p2(), t.p3() }) {
38 if (done.contains(p)) continue;
43 for(P p : new P[] { t.p1(), t.p2(), t.p3() })
47 for(P p : new P[] { t.p1(), t.p2(), t.p3() })
49 return (float)(dist/num);
52 public void transform(M m) {
53 ArrayList<P> set = new ArrayList<P>();
54 set.addAll(ps.keySet());
55 for(P p : set) p.transform(m);
59 float min_x = Float.MAX_VALUE;
60 float min_y = Float.MAX_VALUE;
61 float min_z = Float.MAX_VALUE;
62 float max_x = Float.MIN_VALUE;
63 float max_y = Float.MIN_VALUE;
64 float max_z = Float.MIN_VALUE;
65 for(P p : ps.keySet()) {
66 if (p.x < min_x) min_x = p.x;
67 if (p.y < min_y) min_y = p.y;
68 if (p.z < min_z) min_z = p.z;
69 if (p.x > max_x) max_x = p.x;
70 if (p.y > max_y) max_y = p.y;
71 if (p.z > max_z) max_z = p.z;
73 return new V(max_x - min_x, max_y - min_y, max_z - min_z);
77 float min_x = Float.MAX_VALUE;
78 float min_y = Float.MAX_VALUE;
79 float min_z = Float.MAX_VALUE;
80 float max_x = Float.MIN_VALUE;
81 float max_y = Float.MIN_VALUE;
82 float max_z = Float.MIN_VALUE;
83 for(P p : ps.keySet()) {
84 if (p.x < min_x) min_x = p.x;
85 if (p.y < min_y) min_y = p.y;
86 if (p.z < min_z) min_z = p.z;
87 if (p.x > max_x) max_x = p.x;
88 if (p.y > max_y) max_y = p.y;
89 if (p.z > max_z) max_z = p.z;
91 return new P((float)(max_x + min_x)/2,
92 (float)(max_y + min_y)/2,
93 (float)(max_z + min_z)/2);
96 public P newP(double x, double y, double z) { return newP((float)x, (float)y, (float)z); }
97 public P newP(float x, float y, float z) { return new P(x, y, z); }
99 public T newT(P p12, P p23, P p31, V norm) {
100 V norm2 = p31.minus(p12).cross(p23.minus(p12));
101 float dot = norm.dot(norm2);
102 //if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
103 if (dot < 0) { P p = p12; p12=p23; p23 = p; }
104 return newT(p12, p23, p31);
108 public float volume() {
111 double area = t.area();
112 V origin_to_centroid = new V(newP(0, 0, 0), t.centroid());
113 boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
114 double height = Math.abs(t.norm().dot(origin_to_centroid));
115 total += ((facingAway ? 1 : -1) * area * height) / 3.0;
120 public P nearest(P p) {
122 try { results = kd.nearest(new double[]{p.x,p.y,p.z},1); } catch (Exception e) { throw new Error(e); }
123 return (P)results[0];
126 public T newT(P p1, P p2, P p3) {
130 E e12 = p1.makeE(p2);
131 E e23 = p2.makeE(p3);
132 E e31 = p3.makeE(p1);
133 while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
134 e12.makeAdjacent(e23);
135 e23.makeAdjacent(e31);
136 e31.makeAdjacent(e12);
139 if (e12.t == null) throw new Error();
140 if (e23.t == null) throw new Error();
141 if (e31.t == null) throw new Error();
145 private char allname = 'A';
147 public M aspect = new M();
148 public M invaspect = new M();
150 /** [UNIQUE] point in 3-space */
151 public final class P {
162 private E e; // some edge *leaving* this point
163 private M binding = new M();
164 private P bound_to = this;
166 private float oldscore = 0;
168 private boolean inserted = false;
170 public P register() {
172 if (p2==null) { p2 = this; ps.put(this,this); name = allname++; }
176 public void kdremove() {
177 if (!inserted) return;
180 try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { }
182 public void kdinsert() {
183 if (inserted) return;
186 try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); }
189 public float score() { return oldscore; }
190 public void unscore() {
191 if (watch == null) return;
196 if (watch.watch_count==0) {
203 public P times(M m) { return m.times(this); }
205 if (watch==null) return this;
206 return watch.times(score_against.aspect).times(invaspect);
208 public P watchback() {
209 if (watch_count==0) return partner();
210 return newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
212 public void rescore() {
213 if (score_against == null) return;
218 if (watch != null) unscore();
219 P po = this.times(aspect).times(score_against.invaspect);
221 watch = score_against.nearest(po);
223 // don't attract to vertices that face the other way
224 if (watch.norm().times(score_against.aspect).times(invaspect).dot(norm()) < 0) {
227 watch.watch_x += po.x;
228 watch.watch_y += po.y;
229 watch.watch_z += po.z;
235 if (watch_count==0) s1 = 0;
236 else s1 = this.distance(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
237 s2 = watch==null ? 0 : po.distance(watch);
238 oldscore = (float)(s1 + s2);
242 public float distance(P p) { return distance(p.x, p.y, p.z); }
243 public float distance(float ox, float oy, float oz) {
245 (float)Math.sqrt((x-ox)*(x-ox)+
250 /** does NOT update bound pairs! */
251 public boolean transform(M m) {
252 // FIXME: screws up kdtree
253 // FIXME: screws up hashmap
256 if (ps.get(this)==null) throw new Error();
258 float newx = m.a*x + m.b*y + m.c*z + m.d;
259 float newy = m.e*x + m.f*y + m.g*z + m.h;
260 float newz = m.i*x + m.j*y + m.k*z + m.l;
264 // FIXME: what if we move onto exactly where another point is?
266 } catch (Exception e) {
267 throw new RuntimeException(e);
273 for(E e = this.e; ;) {
274 if (e.intersects(t)) { good = false; break; }
276 if (e == this.e) break;
280 if (t==this.t) continue;
281 if (this.intersects(t)) good = false;
286 public boolean move(V v) {
291 good &= p.transform(m);
292 v = v.times(binding); // bleh wrong
298 public E makeE(P p2) {
301 if (e != null) return e;
303 if (this.e == null && p2.e == null) return this.e = new E(this, p2);
304 if (this.e == null && p2.e != null) return p2.makeE(this).pair;
305 return new E(getFreeIncident(), p2);
308 public E getFreeIncident() {
309 E ret = getFreeIncident(e, e);
310 if (ret != null) return ret;
311 ret = getFreeIncident(e.pair.next, e.pair.next);
312 if (ret == null) throw new Error("unable to find free incident to " + this);
316 public E getFreeIncident(E start, E before) {
319 if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
321 } while(e != before);
325 public E getE(P p2) {
329 if (e==null) return null;
330 if (e.p1 == this && e.p2 == p2) return e;
336 public boolean isBoundTo(P p) {
340 if (px==this) return true;
346 public void unbind() { bound_to = this; binding = new M(); }
347 public void bind(P p) { bind(p, new M()); }
348 public void bind(P p, M binding) {
350 if (isBoundTo(p)) return;
351 P temp_bound_to = p.bound_to;
352 M temp_binding = p.binding;
353 p.bound_to = this.bound_to;
354 p.binding = binding.times(this.binding); // FIXME: may have order wrong here
355 this.bound_to = temp_bound_to;
356 this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
359 public P(float x, float y, float z) {
360 this.x = x; this.y = y; this.z = z;
363 public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
364 public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
365 public boolean equals(Object o) {
366 if (o==null || !(o instanceof P)) return false;
368 return p.x==x && p.y==y && p.z==z;
370 // FIXME: moving a point alters its hashCode
371 public int hashCode() {
373 Float.floatToIntBits(x) ^
374 Float.floatToIntBits(y) ^
375 Float.floatToIntBits(z);
377 public void glVertex(GL gl) {
378 this.times(aspect)._glVertex(gl);
380 private void _glVertex(GL gl) {
381 gl.glVertex3f(x, y, z);
383 public String toString() { return "("+x+","+y+","+z+")"; }
385 V norm = new V(0, 0, 0);
388 if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
390 } while(e != this.e);
395 /** vector in 3-space */
396 public final class V {
397 public final float x, y, z;
398 public V(double x, double y, double z) { this((float)x, (float)y, (float)z); }
399 public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
400 public V(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
401 public V cross(V v) { return new V(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
402 public V plus(V v) { return new V(x+v.x, y+v.y, z+v.z); }
403 public V norm() { return mag()==0 ? this : div(mag()); }
404 public V times(M m) { return m.apply(this); }
405 public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
406 public float dot(V v) { return x*v.x + y*v.y + z*v.z; }
407 public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
408 public V div(float mag) { return new V(x/mag, y/mag, z/mag); }
409 public String toString() { return "<"+x+","+y+","+z+">"; }
412 public class BindingGroup {
413 public HashSet<E> es = new HashSet<E>();
414 public BindingGroup() { }
415 public BindingGroup(E e) {
418 public void add(E e) {
419 if (e.bg != null) { merge(e.bg); return; }
423 public void merge(BindingGroup bg) {
431 /** [UNIQUE] an edge */
432 public final class E implements Comparable<E> {
434 public boolean intersects(T t) {
435 double A0=t.p1().x, A1=t.p1().y, A2=t.p1().z;
436 double B0=t.p2().x, B1=t.p2().y, B2=t.p2().z;
437 double C0=t.p3().x, C1=t.p3().y, C2=t.p3().z;
438 double j0=p1.x, j1=p1.y, j2=p1.z;
439 double k0=p2.x, k1=p2.y, k2=p2.z;
447 double R00, R01, R02, R03,
461 /* c = a × b */
462 c0 = a1 * b2 - a2 * b1;
463 c1 = a2 * b0 - a0 * b2;
464 c2 = a0 * b1 - a1 * b0;
466 /* M^(-1) = (1/det(M)) * adj(M) */
467 in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
468 R00 = (b1 * c2 - b2 * c1) * in_det;
469 R01 = (b2 * c0 - b0 * c2) * in_det;
470 R02 = (b0 * c1 - b1 * c0) * in_det;
471 R10 = (c1 * a2 - c2 * a1) * in_det;
472 R11 = (c2 * a0 - c0 * a2) * in_det;
473 R12 = (c0 * a1 - c1 * a0) * in_det;
479 R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
480 R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
481 R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
483 /* fill in last row of 4x4 matrix */
487 J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
488 K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
489 if (J2 * K2 >= 0) return false;
491 J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
492 K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
493 i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
494 if (i0 < 0 || i0 > 1) return false;
496 J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
497 K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
498 i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
499 if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
504 public int compareTo(E e) {
505 return e.length() > length() ? 1 : -1;
508 public final P p1, p2;
509 T t; // triangle to our "left"
510 E prev; // previous half-edge
511 E next; // next half-edge
512 E pair; // partner half-edge
515 public BindingGroup bg = new BindingGroup(this);
517 public void bind(E e) { bind(e, new M()); }
518 public void bind(E e, M m) { e.bg.add(this); }
520 public void dobind() {
521 if (bg==null) return;
523 if (ex==this) continue;
529 boolean shattered = false;
530 public P shatter() { return shatter(midpoint(), null, null); }
531 public P shatter(P mid, BindingGroup bg1, BindingGroup bg2) {
532 mid = mid.register();
533 if (shattered) return mid;
540 if (bg1==null) bg1 = new BindingGroup();
541 if (bg2==null) bg2 = new BindingGroup();
542 for(E e : bg.es) e.shatter(e.midpoint(), bg1, bg2);
548 bg1.add(p1.getE(mid));
549 bg2.add(mid.getE(p2));
553 public boolean destroyed = false;
554 public void destroy() {
555 if (destroyed) return;
557 pair.destroyed = true;
558 if (next.t != null) next.t.destroy();
559 if (prev.t != null) prev.t.destroy();
560 if (pair.next.t != null) ts.remove(pair.next.t);
561 if (pair.prev.t != null) ts.remove(pair.prev.t);
568 pair.prev.next = next;
569 next.prev = pair.prev;
570 prev.next = pair.next;
572 if (p1.e == this) p1.e = prev.next;
573 if (pair.p1.e == pair) pair.p1.e = pair.prev.next;
576 avgedge -= this.length();
577 avgedge -= pair.length();
582 private void sync() {
583 this.prev.next = this;
584 this.next.prev = this;
585 this.pair.pair = this;
586 if (this.next.p1 != p2) throw new Error();
587 if (this.prev.p2 != p1) throw new Error();
588 if (this.p1.e == null) this.p1.e = this;
596 private boolean added = false;
598 public T makeT() { return t==null ? (t = new T(this)) : t; }
600 /** angle between this half-edge and the next */
601 public double angle() {
602 V v1 = next.p2.minus(p2);
603 V v2 = this.p1.minus(p2);
604 return Math.acos(v1.norm().dot(v2.norm()));
607 public void makeAdjacent(E e) {
608 if (this.next == e) return;
609 if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
610 if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
612 E freeIncident = p2.getFreeIncident(e, this);
614 e.prev.next = freeIncident.next;
615 freeIncident.next.prev = e.prev;
617 freeIncident.next = this.next;
618 this.next.prev = freeIncident;
627 /** creates an isolated edge out in the middle of space */
628 public E(P p1, P p2) {
631 if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
634 this.prev = this.next = this.pair = new E(this, this, this);
638 /** adds a new half-edge from prev.p2 to p2 */
639 public E(E prev, P p2) {
644 if (p2.getE(p1) != null) throw new Error();
646 this.next = this.pair = new E(this, this, prev.next);
648 E q = p2.getFreeIncident();
650 this.next.prev = this;
652 this.prev.next = this;
653 this.pair = new E(q, this, z);
658 /** adds a new half-edge to the mesh with a given predecessor, successor, and pair */
659 public E(E prev, E pair, E next) {
667 public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2).register(); }
668 public boolean has(P p) {
670 return p==p1 || p==p2;
672 public float length() { return p1.minus(p2).mag(); }
673 public String toString() { return p1+"->"+p2; }
676 /** [UNIQUE] a triangle (face) */
677 public final class T {
679 public final int color;
681 public void destroy() {
685 public P nearest(P p) {
686 float d1 = p1().distance(p);
687 float d2 = p2().distance(p);
688 float d3 = p3().distance(p);
689 if (d1 < d2 && d1 < d3) return p1();
690 if (d2 < d3) return p2();
698 if (e1==e2 || e1==e3) throw new Error();
699 if (e3.next!=e1) throw new Error();
700 if (e1.t!=null || e2.t!=null || e3.t!=null)
701 throw new Error("non-manifold surface or disagreeing normals");
704 e1.next.next.t = this;
706 // FIXME: check for sealed/watertight surface once construction is complete (and infer normal(s)?)
708 int color = Math.abs(random.nextInt());
711 if (e1().pair.t != null && color == e1().pair.t.color) { color++; continue; }
712 if (e2().pair.t != null && color == e2().pair.t.color) { color++; continue; }
713 if (e3().pair.t != null && color == e3().pair.t.color) { color++; continue; }
725 public P p1() { return e1.p1; }
726 public P p2() { return e1.p2; }
727 public P p3() { return e1.next.p2; }
728 public E e1() { return e1; }
729 public E e2() { return e1.next; }
730 public E e3() { return e1.prev; }
731 public V norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
732 public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
733 public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
735 public float area() {
736 return (float)Math.abs(0.5 * e1().length() * new V(p1(), p2()).norm().dot(new V(p2(), p3())));
739 public void glVertices(GL gl) {
745 public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
746 (p1().y+p2().y+p3().y)/3,
747 (p1().z+p2().z+p3().z)/3); }
748 public float diameter() {
749 // FIXME: what is this supposed to be?
750 return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
765 public final float a, b, c, d, e, f, g, h, i, j, k, l;
766 public M() { this(1); }
767 public M(float scale) {
769 l = h = d = e = b = i = c = j = g = 0;
771 public M(float scalex, float scaley, float scalez) {
775 l = h = d = e = b = i = c = j = g = 0;
777 public M(V translate) {
778 d = translate.x; h = translate.y; l = translate.z;
780 b = c = e = g = i = j = 0;
782 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) {
783 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;
784 this.j = j; this.k = k; this.l = l;
786 public M times(float x) {
787 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);
789 public M(V axis, float angle) {
790 double q = Math.cos(angle);
791 double s = Math.sin(angle);
793 a = (float)(q + axis.x*axis.x*t);
794 f = (float)(q + axis.y*axis.y*t);
795 k = (float)(q + axis.z*axis.z*t);
796 double tmp1 = axis.x*axis.y*t;
797 double tmp2 = axis.z*s;
798 e = (float)(tmp1 + tmp2);
799 b = (float)(tmp1 - tmp2);
800 tmp1 = axis.x*axis.z*t;
802 i = (float)(tmp1 - tmp2);
803 c = (float)(tmp1 + tmp2);
804 tmp1 = axis.y*axis.z*t;
806 j = (float)(tmp1 + tmp2);
807 g = (float)(tmp1 - tmp2);
810 public P times(P p) {
811 return newP(a*p.x + b*p.y + c*p.z + d,
812 e*p.x + f*p.y + g*p.z + h,
813 i*p.x + j*p.y + k*p.z + l);
815 public P apply(P p) { return p; }
816 public V apply(V v) { return v; }
817 public M invert() { return this; }
818 public M times(M m) { return this; }
821 public void unbind() {
823 for(Geom.T t : this) {
831 for(Geom.T t : this) {
837 public int numedges = 0;
838 public float avgedge = 0;