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() {
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);
}
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();
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); }
}
}
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;
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;
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) {
}
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) {
+ public E makeE(V p2) {
p2 = p2.register();
E e = getE(p2);
if (e != null) return e;
return null;
}
- public E getE(P p2) {
+ public E getE(V p2) {
p2 = p2.register();
E e = this.e;
do {
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;
}
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;
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 */
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) {
+ 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();
}
/** 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);
}
/** 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;
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; }
private static final float MAG = 1;
Geom.M[] translations;
- Geom.P[] points;
+ Geom.V[] points;
public Main(StlFile stlf) {
for(int i=0; i<stlf.coordArray.length; i+=3) {
- Geom.P p0 = goal.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
- Geom.P p1 = goal.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
- Geom.P p2 = goal.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
+ Geom.V p0 = goal.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG).register();
+ Geom.V p1 = goal.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG).register();
+ Geom.V p2 = goal.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG).register();
Geom.Vec n = goal.new Vec(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
Geom.T t = goal.newT(p0, p1, p2, n);
}
};
- Geom.P ltf = tile.newP(-(width/2), (height/2), (depth/2));
- Geom.P mtf = tile.newP( 0.0, (height/2), (depth/2));
- Geom.P rtf = tile.newP( (width/2), (height/2), (depth/2));
- Geom.P ltn = tile.newP(-(width/2), (height/2), -(depth/2));
- Geom.P mtn = tile.newP( 0.0, (height/2), -(depth/2));
- Geom.P rtn = tile.newP( (width/2), (height/2), -(depth/2));
- Geom.P lbf = tile.newP(-(width/2), -(height/2), (depth/2));
- Geom.P mbf = tile.newP( 0.0, -(height/2), (depth/2));
- Geom.P rbf = tile.newP( (width/2), -(height/2), (depth/2));
- Geom.P lbn = tile.newP(-(width/2), -(height/2), -(depth/2));
- Geom.P mbn = tile.newP( 0.0, -(height/2), -(depth/2));
- Geom.P rbn = tile.newP( (width/2), -(height/2), -(depth/2));
+ Geom.V ltf = tile.newP(-(width/2), (height/2), (depth/2)).register();
+ Geom.V mtf = tile.newP( 0.0, (height/2), (depth/2)).register();
+ Geom.V rtf = tile.newP( (width/2), (height/2), (depth/2)).register();
+ Geom.V ltn = tile.newP(-(width/2), (height/2), -(depth/2)).register();
+ Geom.V mtn = tile.newP( 0.0, (height/2), -(depth/2)).register();
+ Geom.V rtn = tile.newP( (width/2), (height/2), -(depth/2)).register();
+ Geom.V lbf = tile.newP(-(width/2), -(height/2), (depth/2)).register();
+ Geom.V mbf = tile.newP( 0.0, -(height/2), (depth/2)).register();
+ Geom.V rbf = tile.newP( (width/2), -(height/2), (depth/2)).register();
+ Geom.V lbn = tile.newP(-(width/2), -(height/2), -(depth/2)).register();
+ Geom.V mbn = tile.newP( 0.0, -(height/2), -(depth/2)).register();
+ Geom.V rbn = tile.newP( (width/2), -(height/2), -(depth/2)).register();
- points = new Geom.P[] {
+ points = new Geom.V[] {
ltf,
mtf,
rtf,
}
}
- //xGeom.P mid = lbf.getE(mbn).shatter();
+ //xGeom.V mid = lbf.getE(mbn).shatter();
// rescale to match volume
float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
}
}
- public synchronized void rand(double temperature, Geom.P p) {
+ public synchronized void rand(double temperature, Geom.V p) {
double tile_score = tile.score();
double goal_score = goal.score();
//v = p.norm().times(v.dot(p.norm()));
boolean aspect = false;//(Math.abs(random.nextInt()) % 100) <= 2;
- Geom.M old_tile_aspect = goal.aspect;
+ Geom.M old_tile_aspect = null;//goal.aspect;
boolean good = true;
if (aspect) {
+ /*
v = v.times(10);
tile.aspect = tile.new M(tile.aspect.a / (v.x+1), tile.aspect.f / (v.y+1), tile.aspect.k / (v.z+1));
tile.invaspect = tile.new M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
goal.rescore();
tile.rescore();
+ */
} else {
good = p.move(v);
}
if (aspect) System.out.println("aspect " + v);
} else {
if (aspect) {
- tile.aspect = old_tile_aspect;
- tile.invaspect = tile.new M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
+ //tile.aspect = old_tile_aspect;
+ //tile.invaspect = tile.new M(1/tile.aspect.a, 1/tile.aspect.f, 1/tile.aspect.k);
goal.rescore();
tile.rescore();
} else {
//if (v1.z==0 && v1.y==0) continue;
i++;
if (i != 1 /*&& i!=4*/) continue;
- Geom.P p = tile.newP(0, 0, 0).times(m);
+ Geom.V p = tile.newP(0, 0, 0).times(m).register();
Geom.Vec v = tile.new Vec(p.x, p.y, p.z);
v = v.times((float)1.04);
gl.glTranslatef(v.x, v.y, v.z);
gl.glEnd();
}
- Geom.P centroid = t.centroid();
+ Geom.V centroid = t.centroid().register();
gl.glBegin(GL.GL_LINES);
gl.glColor3f(1, 1, 1);
/*
*/
if (mesh==goal)
- for(Geom.P p : new Geom.P[] { t.p1(), t.p2(), t.p3() }) {
+ for(Geom.V p : new Geom.V[] { t.p1(), t.p2(), t.p3() }) {
p.glVertex(gl);
//p.plus(p.norm().times(p.score()*10)).glVertex(gl);
p.partner().glVertex(gl);
glcanvas.repaint();
//tile.ts.get(Math.abs(random.nextInt()) % tile.ts.size()).e1().p1
for(Geom.T t : tile)
- for(Geom.P p : new Geom.P[] { t.p1(), t.p2(), t.p3() }) {
+ for(Geom.V p : new Geom.V[] { t.p1(), t.p2(), t.p3() }) {
rand(10,p);
}
goal.rescore();