X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2FMesh.java;h=157955153cdc1b2c28650008a7052f8bb348d28f;hp=0a26699d8f7623ae87bb1a762a4016e24f24bca4;hb=3b84875407de81c3ca4481b1a7f323df6204de5d;hpb=ef71dffc8c85bae1d65ca44cfaef8698f83ca1eb diff --git a/src/edu/berkeley/qfat/Mesh.java b/src/edu/berkeley/qfat/Mesh.java index 0a26699..1579551 100644 --- a/src/edu/berkeley/qfat/Mesh.java +++ b/src/edu/berkeley/qfat/Mesh.java @@ -11,12 +11,11 @@ import edu.berkeley.qfat.geom.Point; public class Mesh implements Iterable { - private KDTree kd = new KDTree(3); - public static float EPSILON = (float)0.0001; public static Random random = new Random(); - private HashMap verts = new HashMap(); + private PointSet pointset = new PointSet(); + public Vert nearest(Point p) { return pointset.nearest(p); } public Iterable edges() { return @@ -37,7 +36,7 @@ public class Mesh implements Iterable { } public Iterator iterator() { - for(Vert v : verts.values()) + for(Vert v : pointset) if (v.e != null && v.e.t != null) return new FaceIterator(v); return new FaceIterator(); @@ -78,59 +77,27 @@ public class Mesh implements Iterable { done.add(p); p.rescore(); } + /* for(T t : this) for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) p.kdremove(); - kd = new KDTree(3); + pointset.clear(); for(T t : this) for(Vert p : new Vert[] { t.v1(), t.v2(), t.v3() }) p.kdinsert(); + */ return (float)(dist/num); } public void transform(Matrix m) { ArrayList set = new ArrayList(); - set.addAll(verts.values()); + for (Vert v : pointset) + set.add(v); for(Vert 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(Point p : verts.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 Point 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(Point p : verts.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 Point((float)(max_x + min_x)/2, - (float)(max_y + min_y)/2, - (float)(max_z + min_z)/2); - } + public Vec diagonal() { return pointset.diagonal(); } + public Point centroid() { return pointset.centroid(); } public float volume() { double total = 0; @@ -144,11 +111,6 @@ public class Mesh implements Iterable { return (float)total; } - public Vert nearest(Point p) { - Object[] results; - try { results = kd.nearest(new double[]{p.x,p.y,p.z},1); } catch (Exception e) { throw new Error(e); } - return (Vert)results[0]; - } public class BindingGroup { public HashSet es = new HashSet(); @@ -169,24 +131,28 @@ public class Mesh implements Iterable { } } - public Vert register(Point p) { Vert v = verts.get(p); return v==null ? new Vert(p) : v; } + public Vert register(Point p) { Vert v = pointset.get(p); return v==null ? new Vert(p) : v; } public final class Vert extends HasPoint { public Point p; public Point getPoint() { return p; } private Vert(Point p) { this.p = p; - if (verts.get(p) != null) throw new Error(); - verts.put(this.p, this); + if (pointset.get(p) != null) throw new Error(); + pointset.add(this); + } + public void reinsert() { + pointset.remove(this); + pointset.add(this); } public void kdremove() { if (!inserted) return; inserted = false; - try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { } + pointset.remove(this); } 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); } + pointset.add(this); } public float score() { return oldscore; } @@ -218,7 +184,7 @@ public class Mesh implements Iterable { watch = score_against.nearest(po.p); // don't attract to vertices that face the other way - if (watch.norm().dot(norm()) < 0) { + if (watch.e == null || watch.norm().dot(norm()) < 0) { watch = null; } else { watch.watch_x += po.p.x; @@ -242,14 +208,14 @@ public class Mesh implements Iterable { // FIXME: screws up hashmap unscore(); try { - if (verts.get(this.p)==null) throw new Error(); - verts.remove(this.p); + if (pointset.get(this.p)==null) throw new Error(); + pointset.remove(this); 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 Point(newx, newy, newz); // FIXME: what if we move onto exactly where another point is? - verts.put(this.p,(Vert)this); + pointset.add(this); } catch (Exception e) { throw new RuntimeException(e); }