From b1832d008534acfc7fe0dc8677387ed1016a5b38 Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 4 Dec 2007 20:11:54 -0800 Subject: [PATCH] checkpoint darcs-hash:20071205041154-5007d-9990b4f0faae2b861164e5311f9017592cbc7ad3.gz --- src/edu/berkeley/qfat/Geom.java | 130 +++++++++++++++++++-------------------- src/edu/berkeley/qfat/Main.java | 49 +++++++-------- 2 files changed, 90 insertions(+), 89 deletions(-) diff --git a/src/edu/berkeley/qfat/Geom.java b/src/edu/berkeley/qfat/Geom.java index 48484b6..80d2dd6 100644 --- a/src/edu/berkeley/qfat/Geom.java +++ b/src/edu/berkeley/qfat/Geom.java @@ -14,13 +14,13 @@ public class Geom implements Iterable { public static float EPSILON = (float)0.0001; public static Random random = new Random(); - private HashMap ps = new HashMap(); + private HashMap ps = new HashMap(); public HashSet es = new HashSet(); public ArrayList ts = new ArrayList(); public Iterator iterator() { return ts.iterator(); } - public P origin() { return new P(0, 0, 0); } + public Point origin() { return new Point(0, 0, 0); } public Geom score_against = null; public double score = 0; @@ -48,27 +48,27 @@ public class Geom implements Iterable { public float rescore() { int num = 0; double dist = 0; - HashSet done = new HashSet(); + HashSet done = new HashSet(); for(T t : ts) - for(V p : new V[] { t.p1(), t.p2(), t.p3() }) { + for(Vert p : new Vert[] { t.p1(), t.p2(), t.p3() }) { if (done.contains(p)) continue; done.add(p); p.rescore(); } for(T t : ts) - for(V p : new V[] { t.p1(), t.p2(), t.p3() }) + for(Vert p : new Vert[] { t.p1(), t.p2(), t.p3() }) p.kdremove(); kd = new KDTree(3); for(T t : ts) - for(V p : new V[] { t.p1(), t.p2(), t.p3() }) + for(Vert p : new Vert[] { t.p1(), t.p2(), t.p3() }) p.kdinsert(); return (float)(dist/num); } public void transform(M m) { - ArrayList set = new ArrayList(); + ArrayList set = new ArrayList(); set.addAll(ps.values()); - for(V v : set) v.transform(m); + for(Vert v : set) v.transform(m); } public Vec diagonal() { @@ -78,7 +78,7 @@ public class Geom implements Iterable { float max_x = Float.MIN_VALUE; float max_y = Float.MIN_VALUE; float max_z = Float.MIN_VALUE; - for(P p : ps.keySet()) { + for(Point p : ps.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; @@ -89,14 +89,14 @@ public class Geom implements Iterable { return new Vec(max_x - min_x, max_y - min_y, max_z - min_z); } - public P centroid() { + 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(P p : ps.keySet()) { + for(Point p : ps.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; @@ -104,16 +104,16 @@ public class Geom implements Iterable { if (p.y > max_y) max_y = p.y; if (p.z > max_z) max_z = p.z; } - return new P((float)(max_x + min_x)/2, + return new Point((float)(max_x + min_x)/2, (float)(max_y + min_y)/2, (float)(max_z + min_z)/2); } - public T newT(V p12, V p23, V p31, Vec norm) { + public T newT(Vert p12, Vert p23, Vert 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) { V p = p12; p12=p23; p23 = p; } + //if (Math.abs(dot) < EPointSILON) throw new Error("dot products within epsilon of each other: "+norm+" "+norm2); + if (dot < 0) { Vert p = p12; p12=p23; p23 = p; } return newT(p12, p23, p31); } @@ -121,7 +121,7 @@ public class Geom implements Iterable { double total = 0; for(T t : ts) { double area = t.area(); - Vec origin_to_centroid = new Vec(new P(0, 0, 0), t.centroid()); + Vec origin_to_centroid = new Vec(new Point(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; @@ -129,13 +129,13 @@ public class Geom implements Iterable { return (float)total; } - public V nearest(P p) { + 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 (V)results[0]; + return (Vert)results[0]; } - public T newT(V p1, V p2, V p3) { + public T newT(Vert p1, Vert p2, Vert p3) { E e12 = p1.makeE(p2); E e23 = p2.makeE(p3); E e31 = p3.makeE(p1); @@ -170,9 +170,9 @@ public class Geom implements Iterable { } } - public final class V { - public P p; - public V(P p) { + public final class Vert { + public Point p; + public Vert(Point p) { this.p = p; if (ps.get(p) != null) throw new Error(); ps.put(this.p, this); @@ -202,9 +202,9 @@ public class Geom implements Iterable { } watch = null; } - 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 Vert partner() { return watch==null ? this : watch; } + public Vert watchback() { return watch_count==0 ? partner() : + register(new Point(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count)); } public void rescore() { if (score_against == null) return; @@ -212,7 +212,7 @@ public class Geom implements Iterable { oldscore = 0; if (watch != null) unscore(); - V po = this; + Vert po = this; if (watch == null) { watch = score_against.nearest(po.p); @@ -247,9 +247,9 @@ public class Geom implements Iterable { 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); + this.p = new Point(newx, newy, newz); // FIXME: what if we move onto exactly where another point is? - ps.put(this.p,(V)this); + ps.put(this.p,(Vert)this); } catch (Exception e) { throw new RuntimeException(e); } @@ -273,7 +273,7 @@ public class Geom implements Iterable { } public boolean move(Vec v) { M m = new M(v); - V p = this; + Vert p = this; boolean good = true; do { good &= p.transform(m); @@ -283,7 +283,7 @@ public class Geom implements Iterable { return good; } - public E makeE(V p2) { + public E makeE(Vert p2) { E e = getE(p2); if (e != null) return e; e = p2.getE(this); @@ -309,7 +309,7 @@ public class Geom implements Iterable { return null; } - public E getE(V p2) { + public E getE(Vert p2) { E e = this.e; do { if (e==null) return null; @@ -319,8 +319,8 @@ public class Geom implements Iterable { return null; } - public boolean isBoundTo(V p) { - V px = p; + public boolean isBoundTo(Vert p) { + Vert px = p; do { if (px==this) return true; px = px.bound_to; @@ -329,10 +329,10 @@ public class Geom implements Iterable { } public void unbind() { bound_to = this; binding = new M(); } - public void bind(V p) { bind(p, new M()); } - public void bind(V p, M binding) { + public void bind(Vert p) { bind(p, new M()); } + public void bind(Vert p, M binding) { if (isBoundTo(p)) return; - V temp_bound_to = p.bound_to; + Vert 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 @@ -349,12 +349,12 @@ public class Geom implements Iterable { return norm.norm(); } - V bound_to = this; + Vert bound_to = this; int watch_count; float watch_x; float watch_y; float watch_z; - V watch; + Vert watch; E e; // some edge *leaving* this point M binding = new M(); float oldscore = 0; @@ -438,7 +438,7 @@ public class Geom implements Iterable { return e.length() > length() ? 1 : -1; } - public final V p1, p2; + public final Vert p1, p2; T t; // triangle to our "left" E prev; // previous half-edge E next; // next half-edge @@ -460,12 +460,12 @@ public class Geom implements Iterable { } boolean shattered = false; - public V shatter() { return shatter(register(midpoint()), null, null); } - public V shatter(V mid, BindingGroup bg1, BindingGroup bg2) { + public Vert shatter() { return shatter(register(midpoint()), null, null); } + public Vert shatter(Vert mid, BindingGroup bg1, BindingGroup bg2) { if (shattered) return mid; shattered = true; - V r = next.p2; + Vert r = next.p2; E next = this.next; E prev = this.prev; @@ -557,7 +557,7 @@ public class Geom implements Iterable { } /** creates an isolated edge out in the middle of space */ - public E(V p1, V p2) { + public E(Vert p1, Vert p2) { if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1); this.p1 = p1; this.p2 = p2; @@ -566,7 +566,7 @@ public class Geom implements Iterable { } /** adds a new half-edge from prev.p2 to p2 */ - public E(E prev, V p2) { + public E(E prev, Vert p2) { this.p1 = prev.p2; this.p2 = p2; this.prev = prev; @@ -593,8 +593,8 @@ public class Geom implements Iterable { this.pair = pair; sync(); } - 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 Point midpoint() { return new Point((p1.p.x+p2.p.x)/2, (p1.p.y+p2.p.y)/2, (p1.p.z+p2.p.z)/2); } + public boolean has(Vert v) { return v==p1 || v==p2; } public float length() { return p1.p.minus(p2.p).mag(); } public String toString() { return p1+"->"+p2; } } @@ -608,7 +608,7 @@ public class Geom implements Iterable { ts.remove(this); } - public V nearest(P p) { + public Vert nearest(Point p) { float d1 = p1().p.distance(p); float d2 = p2().p.distance(p); float d3 = p3().p.distance(p); @@ -648,15 +648,15 @@ public class Geom implements Iterable { this.color = color; } - public V p1() { return e1.p1; } - public V p2() { return e1.p2; } - public V p3() { return e1.next.p2; } + public Vert p1() { return e1.p1; } + public Vert p2() { return e1.p2; } + public Vert 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().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(V v) { return p1()==v || p2()==v || p3()==v; } + public boolean has(Vert v) { return p1()==v || p2()==v || p3()==v; } public float area() { return (float)Math.abs(0.5 * e1().length() * new Vec(p1().p, p2().p).norm().dot(new Vec(p2().p, p3().p))); @@ -668,7 +668,7 @@ public class Geom implements Iterable { p3().p.glVertex(gl); } - public P centroid() { return new P((p1().p.x+p2().p.x+p3().p.x)/3, + public Point centroid() { return new Point((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() { @@ -679,21 +679,21 @@ public class Geom implements Iterable { } - public V register(P p) { V v = ps.get(p); return v==null ? new V(p) : v; } + public Vert register(Point p) { Vert v = ps.get(p); return v==null ? new Vert(p) : v; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** point in 3-space; immutable */ - public static final class P { + public static final class Point { 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 Point(double x, double y, double z) { this((float)x, (float)y, (float)z); } + public Point(float x, float y, float z) { this.x = x; this.y = y; this.z = z; } + public float distance(Point 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 Point times(M m) { return m.times(this); } + public Vec minus(Point p) { return new Vec(x-p.x, y-p.y, z-p.z); } + public Point plus(Vec v) { return new Point(x+v.x, y+v.y, z+v.z); } + public boolean equals(Object o) { return o!=null && (o instanceof Point) && ((Point)o).x==x && ((Point)o).y==y && ((Point)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+")"; } @@ -705,7 +705,7 @@ public class Geom implements Iterable { 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(Point p1, Point 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()); } @@ -770,12 +770,12 @@ public class Geom implements Iterable { g = (float)(tmp1 - tmp2); d = h = l = 0; } - public P times(P p) { - return new P(a*p.x + b*p.y + c*p.z + d, + public Point times(Point p) { + return new Point(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 P apply(P p) { return p; } + public Point apply(Point p) { return p; } public Vec apply(Vec v) { return v; } public M invert() { return this; } public M times(M m) { return this; } diff --git a/src/edu/berkeley/qfat/Main.java b/src/edu/berkeley/qfat/Main.java index 65c6c2a..8607e0a 100644 --- a/src/edu/berkeley/qfat/Main.java +++ b/src/edu/berkeley/qfat/Main.java @@ -5,6 +5,7 @@ import javax.swing.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; import java.util.*; +import static edu.berkeley.qfat.Geom.*; // FIXME: recenter goal to have centroid coincident with tile // FIXME: re-orient goal (how?) @@ -17,7 +18,7 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener boolean control = false; public void mouseWheelMoved(MouseWheelEvent e) { - tz -= e.getWheelRotation() ; + tz -= e.getWheelRotation(); } public void keyTyped(KeyEvent e) { } @@ -73,14 +74,14 @@ public class Main implements GLEventListener, MouseListener, MouseMotionListener private static final float MAG = 1; Geom.M[] translations; - Geom.V[] points; + Geom.Vert[] points; public Main(StlFile stlf) { for(int i=0; i