X-Git-Url: http://git.megacz.com/?p=anneal.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fqfat%2FMesh.java;h=993f5e1945989a3ba6274898a89f3996413e01a8;hp=c570edea97ab1ff8249dfd8ad8c279ddcf6960fd;hb=4b736bc17c9596c4cc1e7f80b750e1f58af60f2d;hpb=a1c8b8ce6c3b7840a29a04a6d9ec970a01937534 diff --git a/src/edu/berkeley/qfat/Mesh.java b/src/edu/berkeley/qfat/Mesh.java index c570ede..993f5e1 100644 --- a/src/edu/berkeley/qfat/Mesh.java +++ b/src/edu/berkeley/qfat/Mesh.java @@ -11,13 +11,12 @@ 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 ps = new HashMap(); - //public HashSet es = new HashSet(); + private PointSet pointset = new PointSet(); + public Vert nearest(Point p) { return pointset.nearest(p); } + private HashMap verts = new HashMap(); public Iterable edges() { return @@ -38,9 +37,9 @@ public class Mesh implements Iterable { } public Iterator iterator() { - for(Vert v : ps.values()) { - if (v.e != null && v.e.t != null) return new FaceIterator(v); - } + for(Vert v : verts.values()) + if (v.e != null && v.e.t != null) + return new FaceIterator(v); return new FaceIterator(); } @@ -79,19 +78,21 @@ 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(ps.values()); + set.addAll(verts.values()); for(Vert v : set) v.transform(m); } @@ -102,7 +103,7 @@ public class Mesh implements Iterable { float max_x = Float.MIN_VALUE; float max_y = Float.MIN_VALUE; float max_z = Float.MIN_VALUE; - for(Point p : ps.keySet()) { + 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; @@ -120,7 +121,7 @@ public class Mesh implements Iterable { float max_x = Float.MIN_VALUE; float max_y = Float.MIN_VALUE; float max_z = Float.MIN_VALUE; - for(Point p : ps.keySet()) { + 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; @@ -145,11 +146,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(); @@ -170,23 +166,29 @@ public class Mesh implements Iterable { } } - public Vert register(Point p) { Vert v = ps.get(p); return v==null ? new Vert(p) : v; } - public final class Vert { + public Vert register(Point p) { Vert v = verts.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 (ps.get(p) != null) throw new Error(); - ps.put(this.p, this); + if (verts.get(p) != null) throw new Error(); + verts.put(this.p, this); + 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 +220,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 +244,16 @@ public class Mesh implements Iterable { // FIXME: screws up hashmap unscore(); try { - if (ps.get(this.p)==null) throw new Error(); - ps.remove(this.p); + if (verts.get(this.p)==null) throw new Error(); + verts.remove(this.p); + 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? - ps.put(this.p,(Vert)this); + pointset.add(this); + verts.put(this.p,(Vert)this); } catch (Exception e) { throw new RuntimeException(e); } @@ -598,7 +602,7 @@ public class Mesh implements Iterable { if (norm != null) { Vec norm2 = p3.p.minus(p1.p).cross(p2.p.minus(p1.p)); float dot = norm.dot(norm2); - //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2); + //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within evertsilon of each other: "+norm+" "+norm2); if (dot < 0) { Vert p = p1; p1=p2; p2 = p; } } E e12 = p1.makeE(p2);