+ Geom.V p0 = goal.new P(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG).register();
+ Geom.V p1 = goal.new P(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG).register();
+ Geom.V p2 = goal.new P(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);
+ }
+
+ // rotate to align major axis -- this probably needs to be done by a human.
+ goal.transform(goal.new M(goal.new Vec(0, 0, 1), (float)(Math.PI/2)));
+
+
+ float goal_width = goal.diagonal().dot(goal.new Vec(1, 0, 0));
+ float goal_height = goal.diagonal().dot(goal.new Vec(0, 1, 0));
+ float goal_depth = goal.diagonal().dot(goal.new Vec(0, 0, 1));
+
+ float width = (float)0.6;
+ float height = (float)0.08;
+ float depth = (float)0.3;
+ translations = new Geom.M[] {
+
+ tile.new M(tile.new Vec(-(width/2), height, 0)),
+ tile.new M(tile.new Vec( (width/2), height, 0)),
+ tile.new M(tile.new Vec(-(width/2), -height, 0)),
+ tile.new M(tile.new Vec( (width/2), -height, 0)),
+ tile.new M(tile.new Vec(-(width/2), 0, depth)),
+ tile.new M(tile.new Vec( (width/2), 0, depth)),
+ tile.new M(tile.new Vec(-(width/2), 0, -depth)),
+ tile.new M(tile.new Vec( (width/2), 0, -depth)),
+
+ tile.new M(tile.new Vec( width, 0, 0)),
+ tile.new M(tile.new Vec(-width, 0, 0)),
+ /*
+ tile.new M(tile.new Vec( 0, 0, depth)),
+ tile.new M(tile.new Vec( 0, 0, -depth)),
+ */
+ };
+
+
+ Geom.V ltf = tile.new P(-(width/2), (height/2), (depth/2)).register();
+ Geom.V mtf = tile.new P( 0.0, (height/2), (depth/2)).register();
+ Geom.V rtf = tile.new P( (width/2), (height/2), (depth/2)).register();
+ Geom.V ltn = tile.new P(-(width/2), (height/2), -(depth/2)).register();
+ Geom.V mtn = tile.new P( 0.0, (height/2), -(depth/2)).register();
+ Geom.V rtn = tile.new P( (width/2), (height/2), -(depth/2)).register();
+ Geom.V lbf = tile.new P(-(width/2), -(height/2), (depth/2)).register();
+ Geom.V mbf = tile.new P( 0.0, -(height/2), (depth/2)).register();
+ Geom.V rbf = tile.new P( (width/2), -(height/2), (depth/2)).register();
+ Geom.V lbn = tile.new P(-(width/2), -(height/2), -(depth/2)).register();
+ Geom.V mbn = tile.new P( 0.0, -(height/2), -(depth/2)).register();
+ Geom.V rbn = tile.new P( (width/2), -(height/2), -(depth/2)).register();
+
+ points = new Geom.V[] {
+ ltf,
+ mtf,
+ rtf,
+ ltn,
+ mtn,
+ rtn,
+ lbf,
+ mbf,
+ rbf,
+ lbn,
+ mbn,
+ rbn
+ };
+
+
+ // top
+ tile.newT(ltf, mtf, mtn);
+ tile.newT(mtn, ltn, ltf);
+ tile.newT(mtf, rtf, rtn);
+ tile.newT(rtn, mtn, mtf);
+
+ // bottom (swap normals)
+ tile.newT(mbf, lbf, mbn);
+ tile.newT(lbn, mbn, lbf);
+ tile.newT(rbf, mbf, rbn);
+ tile.newT(mbn, rbn, mbf);
+
+ // left
+ tile.newT(ltf, ltn, lbn);
+ tile.newT(lbn, lbf, ltf);
+
+ // right (swap normals)
+ tile.newT(rtn, rtf, rbn);
+ tile.newT(rbf, rbn, rtf);
+
+ // front
+ tile.newT(ltn, mtn, mbn);
+ tile.newT(ltn, mbn, lbn);
+ tile.newT(mtn, rtn, rbn);
+ tile.newT(mtn, rbn, mbn);
+
+ // back
+ tile.newT(mtf, ltf, mbf);
+ tile.newT(mbf, ltf, lbf);
+ tile.newT(rtf, mtf, rbf);
+ tile.newT(rbf, mtf, mbf);
+
+ for(Geom.M m : translations) {
+ for(Geom.T t1 : tile) {
+ for(Geom.T t2 : tile) {
+ if (t1==t2) continue;
+
+ if ((t1.p1().p.times(m).minus(t2.p1().p).mag() < Geom.EPSILON) &&
+ (t1.p2().p.times(m).minus(t2.p3().p).mag() < Geom.EPSILON) &&
+ (t1.p3().p.times(m).minus(t2.p2().p).mag() < Geom.EPSILON)) {
+ t1.e1().bind(t2.e3().pair);
+ t1.e2().bind(t2.e2().pair);
+ t1.e3().bind(t2.e1().pair);
+ }
+ if ((t1.p2().p.times(m).minus(t2.p1().p).mag() < Geom.EPSILON) &&
+ (t1.p3().p.times(m).minus(t2.p3().p).mag() < Geom.EPSILON) &&
+ (t1.p1().p.times(m).minus(t2.p2().p).mag() < Geom.EPSILON)) {
+ t1.e2().bind(t2.e3().pair);
+ t1.e3().bind(t2.e2().pair);
+ t1.e1().bind(t2.e1().pair);
+ }
+ if ((t1.p3().p.times(m).minus(t2.p1().p).mag() < Geom.EPSILON) &&
+ (t1.p1().p.times(m).minus(t2.p3().p).mag() < Geom.EPSILON) &&
+ (t1.p2().p.times(m).minus(t2.p2().p).mag() < Geom.EPSILON)) {
+ t1.e3().bind(t2.e3().pair);
+ t1.e1().bind(t2.e2().pair);
+ t1.e2().bind(t2.e1().pair);
+ }
+ }
+ }
+ }
+
+ //xGeom.V mid = lbf.getE(mbn).shatter();
+
+ // rescale to match volume
+ float factor = (float)Math.pow(tile.volume() / goal.volume(), 1.0/3.0);
+ goal.transform(goal.new M(factor));
+
+ // translate to match centroid
+ goal.transform(goal.new M(tile.centroid().minus(goal.centroid())));
+
+ //tx.e2.shatter();
+ //tx.e3.shatter();
+
+
+ tile.bind();
+
+ //mid.move(tile.new Vec((float)0,0,(float)-0.05));
+ //ltn.move(tile.new Vec((float)0,0,(float)-0.05));
+
+ //mtf.move(tile.new Vec(0, (float)-0.05, (float)0.05));
+
+
+ System.out.println("tile volume: " + tile.volume());
+ System.out.println("goal volume: " + goal.volume());
+
+ tile.score_against = goal;
+ goal.score_against = tile;
+ }
+ Random random = new Random();
+
+ public synchronized void breakit() {
+ if (verts > 300) return;
+ //double min = (tile.avgedge/tile.numedges)*(1+(4/(double)verts));
+ //if (verts>0 && tile.es.peek().length() < min) return;
+ PriorityQueue<Geom.E> es = new PriorityQueue<Geom.E>();
+ for(Geom.E e : tile.es) es.add(e);
+ for(int i=0; i<10; i++) {
+ Geom.E e = es.poll();
+ verts++;
+ System.out.println("shatter " + e);
+ e.shatter();
+ tile.unbind();
+ tile.bind();
+ }
+ }
+
+ public synchronized void rand(double temperature, Geom.V p) {
+ double tile_score = tile.score();
+ double goal_score = goal.score();
+
+ p.rescore();
+ //if (p.watch==null) return;
+ float r1 = Math.abs(random.nextFloat());
+ r1 = r1 - (float)Math.floor(r1);
+ r1 = r1 * (float)0.01;
+ r1 = r1 - (float)0.005;
+ Geom.Vec v = p.watchback().p.minus(p.p).norm().times(r1);
+
+ //v = p.norm().times(v.dot(p.norm()));
+
+ boolean aspect = false;//(Math.abs(random.nextInt()) % 100) <= 2;
+ 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);
+ }
+ double new_tile_score = tile.score();
+ double new_goal_score = goal.score();
+ double tile_delta = new_tile_score - tile_score;
+ double goal_delta = new_goal_score - goal_score;
+ double delta = tile_delta + goal_delta;
+ double swapProbability = Math.exp((-1 * delta) / temperature);
+ //boolean doSwap = Math.random() < swapProbability;
+ boolean doSwap = good && (tile_delta <= 0 && goal_delta <= 0);
+ if (doSwap) {
+ tile_score = new_tile_score;
+ goal_score = new_goal_score;
+ //System.out.println("score: " + tile_score + " / " + goal_score);
+ 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);
+ goal.rescore();
+ tile.rescore();
+ } else {
+ p.move(v.times(-1));
+ }