From 84765ab1ebd4913c7ff3e4a27d86cab22648ecfe Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 4 Dec 2007 19:39:36 -0800 Subject: [PATCH] checkpoint darcs-hash:20071205033936-5007d-8f3230f7f6164c62a9c44d0da53b2c5963190570.gz --- src/Geom.java | 166 ++++++++++++++++++++++++++------------------------------- src/Main.java | 54 ++++++++++--------- 2 files changed, 103 insertions(+), 117 deletions(-) diff --git a/src/Geom.java b/src/Geom.java index 89acb70..1c050c4 100644 --- a/src/Geom.java +++ b/src/Geom.java @@ -30,27 +30,27 @@ public class Geom implements Iterable { public float rescore() { int num = 0; double dist = 0; - HashSet

done = new HashSet

(); + HashSet done = new HashSet(); for(T t : ts) - for(P p : new P[] { t.p1(), t.p2(), t.p3() }) { + 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(P p : new P[] { t.p1(), t.p2(), t.p3() }) + for(V p : new V[] { t.p1(), t.p2(), t.p3() }) p.kdremove(); kd = new KDTree(3); for(T t : ts) - for(P p : new P[] { t.p1(), t.p2(), t.p3() }) + for(V p : new V[] { t.p1(), t.p2(), t.p3() }) p.kdinsert(); return (float)(dist/num); } public void transform(M m) { - ArrayList

set = new ArrayList

(); - set.addAll(ps.keySet()); - for(P p : set) p.transform(m); + ArrayList set = new ArrayList(); + set.addAll(ps.values()); + for(V v : set) v.transform(m); } public Vec diagonal() { @@ -94,11 +94,11 @@ public class Geom implements Iterable { public P newP(double x, double y, double z) { return newP((float)x, (float)y, (float)z); } public P newP(float x, float y, float z) { return new P(x, y, z); } - public T newT(P p12, P p23, P p31, Vec norm) { + public T newT(V p12, V p23, V p31, Vec norm) { Vec norm2 = p31.minus(p12).cross(p23.minus(p12)); 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) { P p = p12; p12=p23; p23 = p; } + if (dot < 0) { V p = p12; p12=p23; p23 = p; } return newT(p12, p23, p31); } @@ -121,7 +121,7 @@ public class Geom implements Iterable { return (P)results[0]; } - public T newT(P p1, P p2, P p3) { + public T newT(V p1, V p2, V p3) { p1 = p1.register(); p2 = p2.register(); p3 = p3.register(); @@ -140,50 +140,27 @@ public class Geom implements Iterable { return ret; } - public M aspect = new M(); - public M invaspect = new M(); - public final class V extends P { public V(P p) { super(p.x, p.y, p.z); if (ps.get(p) != null) throw new Error(); ps.put(this, this); } - } - - public class P { - float x, y, z; - - int watch_count; - float watch_x; - float watch_y; - float watch_z; - P watch; - - private E e; // some edge *leaving* this point - private M binding = new M(); - private P bound_to = this; - - private float oldscore = 0; - - private boolean inserted = false; - - public V register() { - V v = ps.get(this); - if (v==null) v = new V(this); - return (V)v; + /* + public int hashCode() { + throw new Error(); } - + */ public void kdremove() { if (!inserted) return; inserted = false; - P p = this; + V p = this; try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { } } public void kdinsert() { if (inserted) return; inserted = true; - P p = this; + V p = this; try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); } } @@ -201,15 +178,9 @@ public class Geom implements Iterable { } watch = null; } - public P times(M m) { return m.times(this); } - public P partner() { - if (watch==null) return this; - return watch.times(score_against.aspect).times(invaspect); - } - public P watchback() { - if (watch_count==0) return partner(); - return newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count); - } + public V partner() { return watch==null ? this : watch; } + public V watchback() { return watch_count==0 ? partner() : + newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count).register(); } public void rescore() { if (score_against == null) return; @@ -217,12 +188,12 @@ public class Geom implements Iterable { oldscore = 0; if (watch != null) unscore(); - P po = this.times(aspect).times(score_against.invaspect); + V po = this; if (watch == null) { - watch = score_against.nearest(po); + watch = (V)score_against.nearest(po); // don't attract to vertices that face the other way - if (watch.norm().times(score_against.aspect).times(invaspect).dot(norm()) < 0) { + if (watch.norm().dot(norm()) < 0) { watch = null; } else { watch.watch_x += po.x; @@ -240,13 +211,6 @@ public class Geom implements Iterable { score += oldscore; } - public float distance(P p) { return distance(p.x, p.y, p.z); } - 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)); - } /** does NOT update bound pairs! */ public boolean transform(M m) { @@ -287,7 +251,7 @@ public class Geom implements Iterable { } public boolean move(Vec v) { M m = new M(v); - P p = this; + V p = this; boolean good = true; do { good &= p.transform(m); @@ -297,7 +261,7 @@ public class Geom implements Iterable { return good; } - public E makeE(P p2) { + public E makeE(V p2) { p2 = p2.register(); E e = getE(p2); if (e != null) return e; @@ -324,7 +288,7 @@ public class Geom implements Iterable { return null; } - public E getE(P p2) { + public E getE(V p2) { p2 = p2.register(); E e = this.e; do { @@ -335,9 +299,9 @@ public class Geom implements Iterable { return null; } - public boolean isBoundTo(P p) { + public boolean isBoundTo(V p) { p = p.register(); - P px = p; + V px = p; do { if (px==this) return true; px = px.bound_to; @@ -346,17 +310,50 @@ public class Geom implements Iterable { } public void unbind() { bound_to = this; binding = new M(); } - public void bind(P p) { bind(p, new M()); } - public void bind(P p, M binding) { + public void bind(V p) { bind(p, new M()); } + public void bind(V p, M binding) { p = p.register(); if (isBoundTo(p)) return; - P temp_bound_to = p.bound_to; + 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; + } + + public class P { + float x, y, z; + + 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 float distance(P p) { return distance(p.x, p.y, p.z); } + 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)); } + + public V register() { V v = ps.get(this); return v==null ? new V(this) : v; } + + public P times(M m) { return m.times(this); } public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; @@ -376,22 +373,9 @@ public class Geom implements Iterable { Float.floatToIntBits(y) ^ Float.floatToIntBits(z); } - public void glVertex(GL gl) { - this.times(aspect)._glVertex(gl); - } - private void _glVertex(GL gl) { - gl.glVertex3f(x, y, z); - } + public void glVertex(GL gl) { _glVertex(gl); } + private void _glVertex(GL gl) { gl.glVertex3f(x, y, z); } public String toString() { return "("+x+","+y+","+z+")"; } - 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(); - } } /** vector in 3-space */ @@ -507,7 +491,7 @@ public class Geom implements Iterable { return e.length() > length() ? 1 : -1; } - public final P p1, p2; + public final V p1, p2; T t; // triangle to our "left" E prev; // previous half-edge E next; // next half-edge @@ -529,19 +513,19 @@ public class Geom implements Iterable { } boolean shattered = false; - public P shatter() { return shatter(midpoint(), null, null); } - public P shatter(P mid, BindingGroup bg1, BindingGroup bg2) { + public V shatter() { return shatter(midpoint().register(), null, null); } + public V shatter(V mid, BindingGroup bg1, BindingGroup bg2) { mid = mid.register(); if (shattered) return mid; shattered = true; - P r = next.p2; + V r = next.p2; E next = this.next; E prev = this.prev; if (bg1==null) bg1 = new BindingGroup(); if (bg2==null) bg2 = new BindingGroup(); - for(E e : bg.es) e.shatter(e.midpoint(), bg1, bg2); + for(E e : bg.es) e.shatter(e.midpoint().register(), bg1, bg2); pair.shatter(); destroy(); @@ -627,7 +611,7 @@ public class Geom implements Iterable { } /** creates an isolated edge out in the middle of space */ - public E(P p1, P p2) { + public E(V p1, V p2) { p1 = p1.register(); p2 = p2.register(); if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1); @@ -638,7 +622,7 @@ public class Geom implements Iterable { } /** adds a new half-edge from prev.p2 to p2 */ - public E(E prev, P p2) { + public E(E prev, V p2) { p2 = p2.register(); this.p1 = prev.p2; this.p2 = p2; @@ -724,9 +708,9 @@ public class Geom implements Iterable { this.color = color; } - public P p1() { return e1.p1; } - public P p2() { return e1.p2; } - public P p3() { return e1.next.p2; } + public V p1() { return e1.p1; } + public V p2() { return e1.p2; } + public V p3() { return e1.next.p2; } public E e1() { return e1; } public E e2() { return e1.next; } public E e3() { return e1.prev; } diff --git a/src/Main.java b/src/Main.java index dc880cd..5fae70a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -72,14 +72,14 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener private static final float MAG = 1; Geom.M[] translations; - Geom.P[] points; + Geom.V[] points; public Main(StlFile stlf) { for(int i=0; i