-public class Geom implements GLEventListener {
-
- public static StlFile stlf = null;
-
- /**
- * Take care of initialization here.
- */
- public void init(GLAutoDrawable gld) {
- GL gl = gld.getGL();
- GLU glu = new GLU();
- gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- gl.glViewport(0, 0, 500, 300);
- gl.glMatrixMode(GL.GL_PROJECTION);
- gl.glLoadIdentity();
- //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
- display(gld);
- }
-
-
- /**
- * Take care of drawing here.
- */
- public void display(GLAutoDrawable drawable) {
- float red = 0.0f;
- float green = 0.0f;
- float blue = 0.0f;
- GL gl = drawable.getGL();
- gl.glClear(GL.GL_COLOR_BUFFER_BIT);
- gl.glPointSize(5.0f);
-
- for(int i=0; i<stlf.coordArray.length; i+=3) {
- red -= .09f;
- green -= .12f;
- blue -= .15f;
- if (red < 0.15) red = 1.0f;
- if (green < 0.15) green = 1.0f;
- if (blue < 0.15) blue = 1.0f;
- gl.glColor3f(red, green, blue);
-
- gl.glBegin(GL.GL_TRIANGLES);
- for(int j=0; j<3; j++)
- gl.glVertex3f(stlf.coordArray[i+j].x,
- stlf.coordArray[i+j].y,
- stlf.coordArray[i+j].z);
- gl.glEnd();
- }
- }
-
-
- public void reshape(
- GLAutoDrawable drawable,
- int x,
- int y,
- int width,
- int height
- ) {}
- public void displayChanged(
- GLAutoDrawable drawable,
- boolean modeChanged,
- boolean deviceChanged
- ) {}
-
- /** point in 3-space */
- public static class P {
- public final float x, y, z;
+public class Geom implements Iterable<Geom.T> {
+
+ private KDTree kd = new KDTree(3);
+
+ public static float EPSILON = (float)0.0001;
+ public static Random random = new Random();
+
+ private HashMap<P,V> ps = new HashMap<P,V>();
+ public HashSet<E> es = new HashSet<E>();
+ public ArrayList<T> ts = new ArrayList<T>();
+
+ public Iterator<T> iterator() { return ts.iterator(); }
+
+ public P origin() { return new P(0, 0, 0); }
+
+ public Geom score_against = null;
+ public double score = 0;
+ public float score() { return (float)score; }
+
+ public float rescore() {
+ int num = 0;
+ double dist = 0;
+ HashSet<V> done = new HashSet<V>();
+ for(T t : ts)
+ for(V p : new V[] { t.p1(), t.p2(), t.p3() }) {
+ if (done.contains(p)) continue;
+ done.add(p);
+ p.rescore();
+ }
+ for(T t : ts)
+ for(V p : new V[] { t.p1(), t.p2(), t.p3() })
+ p.kdremove();
+ kd = new KDTree(3);
+ for(T t : ts)
+ for(V p : new V[] { t.p1(), t.p2(), t.p3() })
+ p.kdinsert();
+ return (float)(dist/num);
+ }
+
+ public void transform(M m) {
+ ArrayList<V> set = new ArrayList<V>();
+ set.addAll(ps.values());
+ for(V v : set) v.transform(m);
+ }
+
+ public Vec diagonal() {
+ float min_x = Float.MAX_VALUE;
+ float min_y = Float.MAX_VALUE;
+ float min_z = Float.MAX_VALUE;
+ float max_x = Float.MIN_VALUE;
+ float max_y = Float.MIN_VALUE;
+ float max_z = Float.MIN_VALUE;
+ for(P p : ps.keySet()) {
+ if (p.x < min_x) min_x = p.x;
+ if (p.y < min_y) min_y = p.y;
+ if (p.z < min_z) min_z = p.z;
+ if (p.x > max_x) max_x = p.x;
+ if (p.y > max_y) max_y = p.y;
+ if (p.z > max_z) max_z = p.z;
+ }
+ return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
+ }
+
+ public P centroid() {
+ float min_x = Float.MAX_VALUE;
+ float min_y = Float.MAX_VALUE;
+ float min_z = Float.MAX_VALUE;
+ float max_x = Float.MIN_VALUE;
+ float max_y = Float.MIN_VALUE;
+ float max_z = Float.MIN_VALUE;
+ for(P p : ps.keySet()) {
+ if (p.x < min_x) min_x = p.x;
+ if (p.y < min_y) min_y = p.y;
+ if (p.z < min_z) min_z = p.z;
+ if (p.x > max_x) max_x = p.x;
+ if (p.y > max_y) max_y = p.y;
+ if (p.z > max_z) max_z = p.z;
+ }
+ return new P((float)(max_x + min_x)/2,
+ (float)(max_y + min_y)/2,
+ (float)(max_z + min_z)/2);
+ }
+
+ public T newT(V p12, V p23, V p31, Vec norm) {
+ Vec norm2 = p31.p.minus(p12.p).cross(p23.p.minus(p12.p));
+ float dot = norm.dot(norm2);
+ //if (Math.abs(dot) < EPSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2);
+ if (dot < 0) { V p = p12; p12=p23; p23 = p; }
+ return newT(p12, p23, p31);
+ }
+
+ public float volume() {
+ double total = 0;
+ for(T t : ts) {
+ double area = t.area();
+ Vec origin_to_centroid = new Vec(new P(0, 0, 0), t.centroid());
+ boolean facingAway = t.norm().dot(origin_to_centroid) > 0;
+ double height = Math.abs(t.norm().dot(origin_to_centroid));
+ total += ((facingAway ? 1 : -1) * area * height) / 3.0;
+ }
+ return (float)total;
+ }
+
+ public V nearest(P p) {
+ Object[] results;
+ try { results = kd.nearest(new double[]{p.x,p.y,p.z},1); } catch (Exception e) { throw new Error(e); }
+ return (V)results[0];
+ }
+
+ public T newT(V p1, V p2, V p3) {
+ E e12 = p1.makeE(p2);
+ E e23 = p2.makeE(p3);
+ E e31 = p3.makeE(p1);
+ while(e12.next != e23 || e23.next != e31 || e31.next != e12) {
+ e12.makeAdjacent(e23);
+ e23.makeAdjacent(e31);
+ e31.makeAdjacent(e12);
+ }
+ T ret = e12.makeT();
+ if (e12.t == null) throw new Error();
+ if (e23.t == null) throw new Error();
+ if (e31.t == null) throw new Error();
+ return ret;
+ }
+
+ public final class V {
+ public P p;
+ public V(P p) {
+ this.p = p;
+ if (ps.get(p) != null) throw new Error();
+ ps.put(this.p, this);
+ }
+ public int hashCode() {
+ throw new Error();
+ }
+ public void kdremove() {
+ if (!inserted) return;
+ inserted = false;
+ try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { }
+ }
+ public void kdinsert() {
+ if (inserted) return;
+ inserted = true;
+ try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); }
+ }
+
+ public float score() { return oldscore; }
+ public void unscore() {
+ if (watch == null) return;
+ watch.watch_x -= p.x;
+ watch.watch_y -= p.y;
+ watch.watch_z -= p.z;
+ watch.watch_count--;
+ if (watch.watch_count==0) {
+ watch.watch_x = 0;
+ watch.watch_y = 0;
+ watch.watch_z = 0;
+ }
+ watch = null;
+ }
+ public V partner() { return watch==null ? this : watch; }
+ public V watchback() { return watch_count==0 ? partner() :
+ new P(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count).register(); }
+ public void rescore() {
+ if (score_against == null) return;
+
+ score -= oldscore;
+ oldscore = 0;
+
+ if (watch != null) unscore();
+ V po = this;
+ if (watch == null) {
+ watch = score_against.nearest(po.p);
+
+ // don't attract to vertices that face the other way
+ if (watch.norm().dot(norm()) < 0) {
+ watch = null;
+ } else {
+ watch.watch_x += po.p.x;
+ watch.watch_y += po.p.y;
+ watch.watch_z += po.p.z;
+ watch.watch_count++;
+ }
+ }
+
+ double s1, s2;
+ if (watch_count==0) s1 = 0;
+ else s1 = p.distance(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
+ s2 = watch==null ? 0 : po.p.distance(watch.p);
+ oldscore = (float)(s1 + s2);
+ score += oldscore;
+ }
+
+
+ /** does NOT update bound pairs! */
+ public boolean transform(M m) {
+ // FIXME: screws up kdtree
+ // FIXME: screws up hashmap
+ unscore();
+ try {
+ if (ps.get(this.p)==null) throw new Error();
+ ps.remove(this.p);
+ float newx = m.a*p.x + m.b*p.y + m.c*p.z + m.d;
+ float newy = m.e*p.x + m.f*p.y + m.g*p.z + m.h;
+ float newz = m.i*p.x + m.j*p.y + m.k*p.z + m.l;
+ this.p = new P(newx, newy, newz);
+ // FIXME: what if we move onto exactly where another point is?
+ ps.put(this.p,(V)this);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ rescore();
+ boolean good = true;
+ /*
+ for(T t : ts) {
+ for(E e = this.e; ;) {
+ if (e.intersects(t)) { good = false; break; }
+ e = e.pair.next;
+ if (e == this.e) break;
+ }
+ }
+ */
+ /*
+ if (t==this.t) continue;
+ if (this.intersects(t)) good = false;
+ }
+ */
+ return good;
+ }
+ public boolean move(Vec v) {
+ M m = new M(v);
+ V p = this;
+ boolean good = true;
+ do {
+ good &= p.transform(m);
+ v = v.times(binding); // bleh wrong
+ p = p.bound_to;
+ } while (p != this);
+ return good;
+ }
+
+ public E makeE(V p2) {
+ E e = getE(p2);
+ if (e != null) return e;
+ e = p2.getE(this);
+ if (this.e == null && p2.e == null) return this.e = new E(this, p2);
+ if (this.e == null && p2.e != null) return p2.makeE(this).pair;
+ return new E(getFreeIncident(), p2);
+ }
+
+ public E getFreeIncident() {
+ E ret = getFreeIncident(e, e);
+ if (ret != null) return ret;
+ ret = getFreeIncident(e.pair.next, e.pair.next);
+ if (ret == null) throw new Error("unable to find free incident to " + this);
+ return ret;
+ }
+
+ public E getFreeIncident(E start, E before) {
+ E e = start;
+ do {
+ if (e.pair.p2 == this && e.pair.t == null && e.pair.next.t == null) return e.pair;
+ e = e.pair.next;
+ } while(e != before);
+ return null;
+ }
+
+ public E getE(V p2) {
+ E e = this.e;
+ do {
+ if (e==null) return null;
+ if (e.p1 == this && e.p2 == p2) return e;
+ e = e.pair.next;
+ } while (e!=this.e);
+ return null;
+ }
+
+ public boolean isBoundTo(V p) {
+ V px = p;
+ do {
+ if (px==this) return true;
+ px = px.bound_to;
+ } while(px != p);
+ return false;
+ }
+
+ public void unbind() { bound_to = this; binding = new M(); }
+ public void bind(V p) { bind(p, new M()); }
+ public void bind(V p, M binding) {
+ if (isBoundTo(p)) return;
+ V temp_bound_to = p.bound_to;
+ M temp_binding = p.binding;
+ p.bound_to = this.bound_to;
+ p.binding = binding.times(this.binding); // FIXME: may have order wrong here
+ this.bound_to = temp_bound_to;
+ this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
+ }
+ public Vec norm() {
+ Vec norm = new Vec(0, 0, 0);
+ E e = this.e;
+ do {
+ if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
+ e = e.pair.next;
+ } while(e != this.e);
+ return norm.norm();
+ }
+ V bound_to = this;
+
+ int watch_count;
+ float watch_x;
+ float watch_y;
+ float watch_z;
+ V watch;
+ E e; // some edge *leaving* this point
+ M binding = new M();
+ float oldscore = 0;
+ boolean inserted = false;
+ }
+
+ public class P {
+ float x, y, z;
+ public P(double x, double y, double z) { this((float)x, (float)y, (float)z); }