public static float EPSILON = (float)0.0001;
public static Random random = new Random();
- private HashMap<P,P> ps = new HashMap<P,P>();
+ 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 newP(0, 0, 0); }
+ 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 score() { return (float)score; }
+
+ public void unbind() {
+
+ for(Geom.T t : this) {
+ t.p1().unbind();
+ t.p2().unbind();
+ t.p3().unbind();
+ }
+
}
+ public void bind() {
+ for(Geom.T t : this) {
+ t.e1().dobind();
+ t.e2().dobind();
+ t.e3().dobind();
+ }
+ }
+ public int numedges = 0;
+ public float avgedge = 0;
public float rescore() {
int num = 0;
double dist = 0;
- HashSet<P> done = new HashSet<P>();
+ HashSet<V> done = new HashSet<V>();
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<P> set = new ArrayList<P>();
- set.addAll(ps.keySet());
- for(P p : set) p.transform(m);
+ ArrayList<V> set = new ArrayList<V>();
+ set.addAll(ps.values());
+ for(V v : set) v.transform(m);
}
public Vec diagonal() {
(float)(max_z + min_z)/2);
}
- 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) {
- Vec norm2 = p31.minus(p12).cross(p23.minus(p12));
+ 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) { P p = p12; p12=p23; p23 = p; }
+ 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(newP(0, 0, 0), t.centroid());
+ 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 P nearest(P p) {
+ 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 (P)results[0];
+ return (V)results[0];
}
- public T newT(P p1, P p2, P p3) {
- p1 = p1.register();
- p2 = p2.register();
- p3 = p3.register();
+ public T newT(V p1, V p2, V p3) {
E e12 = p1.makeE(p2);
E e23 = p2.makeE(p3);
E e31 = p3.makeE(p1);
return ret;
}
- public M aspect = new M();
- public M invaspect = new M();
-
- /** [UNIQUE] point in 3-space */
- public final 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 P register() {
- P p2 = ps.get(this);
- if (p2==null) { p2 = this; ps.put(this,this); }
- return p2;
+ public class BindingGroup {
+ public HashSet<E> es = new HashSet<E>();
+ public BindingGroup() { }
+ public BindingGroup(E e) {
+ es.add(e);
}
+ public void add(E e) {
+ if (e.bg != null) { merge(e.bg); return; }
+ es.add(e);
+ e.bg = this;
+ }
+ public void merge(BindingGroup bg) {
+ for(E e : bg.es) {
+ e.bg = null;
+ add(e);
+ }
+ }
+ }
+ 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 void kdremove() {
if (!inserted) return;
inserted = false;
- P 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;
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 -= x;
- watch.watch_y -= y;
- watch.watch_z -= z;
+ 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 = 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() :
+ register(new P(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count)); }
public void rescore() {
if (score_against == null) return;
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 = score_against.nearest(po.p);
// 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;
- watch.watch_y += po.y;
- watch.watch_z += po.z;
+ 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 = this.distance(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
- s2 = watch==null ? 0 : po.distance(watch);
+ 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;
}
- 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) {
// FIXME: screws up hashmap
unscore();
try {
- if (ps.get(this)==null) throw new Error();
- ps.remove(this);
- float newx = m.a*x + m.b*y + m.c*z + m.d;
- float newy = m.e*x + m.f*y + m.g*z + m.h;
- float newz = m.i*x + m.j*y + m.k*z + m.l;
- this.x = newx;
- this.y = newy;
- this.z = newz;
+ 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,this);
+ ps.put(this.p,(V)this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public boolean move(Vec v) {
M m = new M(v);
- P p = this;
+ V p = this;
boolean good = true;
do {
good &= p.transform(m);
return good;
}
- public E makeE(P p2) {
- p2 = p2.register();
+ public E makeE(V p2) {
E e = getE(p2);
if (e != null) return e;
e = p2.getE(this);
return null;
}
- public E getE(P p2) {
- p2 = p2.register();
+ public E getE(V p2) {
E e = this.e;
do {
if (e==null) return null;
return null;
}
- public boolean isBoundTo(P p) {
- p = p.register();
- P px = p;
+ public boolean isBoundTo(V p) {
+ V px = p;
do {
if (px==this) return true;
px = px.bound_to;
}
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) {
- p = p.register();
+ public void bind(V p) { bind(p, new M()); }
+ public void bind(V p, M binding) {
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 P(float x, float y, float z) {
- this.x = x; this.y = y; this.z = z;
- }
-
- public Vec minus(P p) { return new Vec(x-p.x, y-p.y, z-p.z); }
- public P plus(Vec v) { return newP(x+v.x, y+v.y, z+v.z); }
- public boolean equals(Object o) {
- if (o==null || !(o instanceof P)) return false;
- P p = (P)o;
- return p.x==x && p.y==y && p.z==z;
- }
- // FIXME: moving a point alters its hashCode
- public int hashCode() {
- return
- Float.floatToIntBits(x) ^
- 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 String toString() { return "("+x+","+y+","+z+")"; }
public Vec norm() {
Vec norm = new Vec(0, 0, 0);
E e = this.e;
} while(e != this.e);
return norm.norm();
}
- }
- /** vector in 3-space */
- public final class Vec {
- public final float x, y, z;
- public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
- public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
- public Vec(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
- 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); }
- public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
- public Vec norm() { return mag()==0 ? this : div(mag()); }
- public Vec times(M m) { return m.apply(this); }
- public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
- public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
- public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
- public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
- public String toString() { return "<"+x+","+y+","+z+">"; }
- }
-
- public class BindingGroup {
- public HashSet<E> es = new HashSet<E>();
- public BindingGroup() { }
- public BindingGroup(E e) {
- es.add(e);
- }
- public void add(E e) {
- if (e.bg != null) { merge(e.bg); return; }
- es.add(e);
- e.bg = this;
- }
- public void merge(BindingGroup bg) {
- for(E e : bg.es) {
- e.bg = null;
- add(e);
- }
- }
+ 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;
}
/** [UNIQUE] an edge */
public final class E implements Comparable<E> {
public boolean intersects(T t) {
- double A0=t.p1().x, A1=t.p1().y, A2=t.p1().z;
- double B0=t.p2().x, B1=t.p2().y, B2=t.p2().z;
- double C0=t.p3().x, C1=t.p3().y, C2=t.p3().z;
- double j0=p1.x, j1=p1.y, j2=p1.z;
- double k0=p2.x, k1=p2.y, k2=p2.z;
+ double A0=t.p1().p.x, A1=t.p1().p.y, A2=t.p1().p.z;
+ double B0=t.p2().p.x, B1=t.p2().p.y, B2=t.p2().p.z;
+ double C0=t.p3().p.x, C1=t.p3().p.y, C2=t.p3().p.z;
+ double j0=p1.p.x, j1=p1.p.y, j2=p1.p.z;
+ double k0=p2.p.x, k1=p2.p.y, k2=p2.p.z;
double J0, J1, J2;
double K0, K1, K2;
double i0, i1, i2;
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
}
boolean shattered = false;
- public P shatter() { return shatter(midpoint(), null, null); }
- public P shatter(P mid, BindingGroup bg1, BindingGroup bg2) {
- mid = mid.register();
+ public V shatter() { return shatter(register(midpoint()), null, null); }
+ public V shatter(V mid, BindingGroup bg1, BindingGroup bg2) {
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(register(e.midpoint()), bg1, bg2);
pair.shatter();
destroy();
/** angle between this half-edge and the next */
public double angle() {
- Vec v1 = next.p2.minus(p2);
- Vec v2 = this.p1.minus(p2);
+ Vec v1 = next.p2.p.minus(p2.p);
+ Vec v2 = this.p1.p.minus(p2.p);
return Math.acos(v1.norm().dot(v2.norm()));
}
}
/** creates an isolated edge out in the middle of space */
- public E(P p1, P p2) {
- p1 = p1.register();
- p2 = p2.register();
+ public E(V p1, V p2) {
if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
this.p1 = p1;
this.p2 = p2;
}
/** adds a new half-edge from prev.p2 to p2 */
- public E(E prev, P p2) {
- p2 = p2.register();
+ public E(E prev, V p2) {
this.p1 = prev.p2;
this.p2 = p2;
this.prev = prev;
this.pair = pair;
sync();
}
- public P midpoint() { return newP((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2).register(); }
- public boolean has(P p) {
- p = p.register();
- return p==p1 || p==p2;
- }
- public float length() { return p1.minus(p2).mag(); }
+ public P midpoint() { return new P((p1.p.x+p2.p.x)/2, (p1.p.y+p2.p.y)/2, (p1.p.z+p2.p.z)/2); }
+ public boolean has(V v) { return v==p1 || v==p2; }
+ public float length() { return p1.p.minus(p2.p).mag(); }
public String toString() { return p1+"->"+p2; }
}
ts.remove(this);
}
- public P nearest(P p) {
- float d1 = p1().distance(p);
- float d2 = p2().distance(p);
- float d3 = p3().distance(p);
+ public V nearest(P p) {
+ float d1 = p1().p.distance(p);
+ float d2 = p2().p.distance(p);
+ float d3 = p3().p.distance(p);
if (d1 < d2 && d1 < d3) return p1();
if (d2 < d3) return p2();
return p3();
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; }
- public Vec norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
+ public Vec norm() { return p2().p.minus(p1().p).cross(p3().p.minus(p1().p)).norm(); }
public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
- public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
+ public boolean has(V v) { return p1()==v || p2()==v || p3()==v; }
public float area() {
- return (float)Math.abs(0.5 * e1().length() * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
+ return (float)Math.abs(0.5 * e1().length() * new Vec(p1().p, p2().p).norm().dot(new Vec(p2().p, p3().p)));
}
public void glVertices(GL gl) {
- p1().glVertex(gl);
- p2().glVertex(gl);
- p3().glVertex(gl);
+ p1().p.glVertex(gl);
+ p2().p.glVertex(gl);
+ p3().p.glVertex(gl);
}
- public P centroid() { return newP((p1().x+p2().x+p3().x)/3,
- (p1().y+p2().y+p3().y)/3,
- (p1().z+p2().z+p3().z)/3); }
+ public P centroid() { return new P((p1().p.x+p2().p.x+p3().p.x)/3,
+ (p1().p.y+p2().p.y+p3().p.y)/3,
+ (p1().p.z+p2().p.z+p3().p.z)/3); }
public float diameter() {
// FIXME: what is this supposed to be?
return Math.max(Math.max(e1().length(), e2().length()), e3().length()) / 2;
}
+
+ public V register(P p) { V v = ps.get(p); return v==null ? new V(p) : v; }
+
+ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+ /** point in 3-space; immutable */
+ public static final class P {
+ public final float x, y, z;
+ public P(double x, double y, double z) { this((float)x, (float)y, (float)z); }
+ public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
+ 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 P times(M m) { return m.times(this); }
+ public Vec minus(P p) { return new Vec(x-p.x, y-p.y, z-p.z); }
+ public P plus(Vec v) { return new P(x+v.x, y+v.y, z+v.z); }
+ public boolean equals(Object o) { return o!=null && (o instanceof P) && ((P)o).x==x && ((P)o).y==y && ((P)o).z==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 int hashCode() { return Float.floatToIntBits(x) ^ Float.floatToIntBits(y) ^ Float.floatToIntBits(z); }
+ }
+ /** vector in 3-space; immutable */
+ public static final class Vec {
+ public final float x, y, z;
+ public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
+ public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
+ public Vec(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
+ 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); }
+ public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
+ public Vec norm() { return mag()==0 ? this : div(mag()); }
+ public Vec times(M m) { return m.apply(this); }
+ public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
+ public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
+ public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
+ public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
+ public String toString() { return "<"+x+","+y+","+z+">"; }
+ }
- /** matrix */
- public class M {
+ /** affine matrix */
+ public static class M {
//
// [ a b c d ] [ x ]
// [ e f g h ] [ y ]
d = h = l = 0;
}
public P times(P p) {
- return newP(a*p.x + b*p.y + c*p.z + d,
+ return new P(a*p.x + b*p.y + c*p.z + d,
e*p.x + f*p.y + g*p.z + h,
i*p.x + j*p.y + k*p.z + l);
}
public M times(M m) { return this; }
}
- public void unbind() {
-
- for(Geom.T t : this) {
- t.p1().unbind();
- t.p2().unbind();
- t.p3().unbind();
- }
-
- }
- public void bind() {
- for(Geom.T t : this) {
- t.e1().dobind();
- t.e2().dobind();
- t.e3().dobind();
- }
- }
- public int numedges = 0;
- public float avgedge = 0;
}