checkpoint
[anneal.git] / src / edu / berkeley / qfat / Geom.java
index 48484b6..18b17d1 100644 (file)
@@ -5,7 +5,9 @@ import java.awt.event.*;
 import javax.swing.*;
 import javax.media.opengl.*;
 import javax.media.opengl.glu.*;
+import edu.berkeley.qfat.geom.*;
 import edu.wlu.cs.levy.CG.KDTree;
+import edu.berkeley.qfat.geom.Point;
 
 public class Geom implements Iterable<Geom.T> {
 
@@ -14,13 +16,13 @@ public class Geom implements Iterable<Geom.T> {
     public static float EPSILON = (float)0.0001;
     public static Random random = new Random();
 
-    private HashMap<P,V>  ps = new HashMap<P,V>();
+    private HashMap<Point,Vert>  ps = new HashMap<Point,Vert>();
     public  HashSet<E>    es = new HashSet<E>();
     public  ArrayList<T>  ts = new ArrayList<T>();
 
     public Iterator<T> 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 +50,27 @@ public class Geom implements Iterable<Geom.T> {
     public float rescore() {
         int num = 0;
         double dist = 0;
-        HashSet<V> done = new HashSet<V>();
+        HashSet<Vert> done = new HashSet<Vert>();
         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<V> set = new ArrayList<V>();
+    public void transform(Matrix m) {
+        ArrayList<Vert> set = new ArrayList<Vert>();
         set.addAll(ps.values());
-        for(V v : set) v.transform(m);
+        for(Vert v : set) v.transform(m);
     }
 
     public Vec diagonal() {
@@ -78,7 +80,7 @@ public class Geom implements Iterable<Geom.T> {
         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 +91,14 @@ public class Geom implements Iterable<Geom.T> {
         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 +106,16 @@ public class Geom implements Iterable<Geom.T> {
             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 +123,7 @@ public class Geom implements Iterable<Geom.T> {
         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 +131,13 @@ public class Geom implements Iterable<Geom.T> {
         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 +172,9 @@ public class Geom implements Iterable<Geom.T> {
         }
     }
 
-    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 +204,9 @@ public class Geom implements Iterable<Geom.T> {
             }
             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 +214,7 @@ public class Geom implements Iterable<Geom.T> {
             oldscore = 0;
 
             if (watch != null) unscore();
-            V po = this;
+            Vert po = this;
             if (watch == null) {
                 watch = score_against.nearest(po.p);
 
@@ -237,7 +239,7 @@ public class Geom implements Iterable<Geom.T> {
 
 
         /** does NOT update bound pairs! */
-        public boolean transform(M m) {
+        public boolean transform(Matrix m) {
             // FIXME: screws up kdtree 
             // FIXME: screws up hashmap
             unscore();
@@ -247,9 +249,9 @@ public class Geom implements Iterable<Geom.T> {
                 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);
             }
@@ -272,8 +274,8 @@ public class Geom implements Iterable<Geom.T> {
             return good;
         }
         public boolean move(Vec v) {
-            M m = new M(v);
-            V p = this;
+            Matrix m = new Matrix(v);
+            Vert p = this;
             boolean good = true;
             do {
                 good &= p.transform(m);
@@ -283,7 +285,7 @@ public class Geom implements Iterable<Geom.T> {
             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 +311,7 @@ public class Geom implements Iterable<Geom.T> {
             return null;
         }
 
-        public E getE(V p2) {
+        public E getE(Vert p2) {
             E e = this.e;
             do {
                 if (e==null) return null;
@@ -319,8 +321,8 @@ public class Geom implements Iterable<Geom.T> {
             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;
@@ -328,12 +330,12 @@ public class Geom implements Iterable<Geom.T> {
             return false;
         }
 
-        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 unbind() { bound_to = this; binding = new Matrix(); }
+        public void bind(Vert p) { bind(p, new Matrix()); }
+        public void bind(Vert p, Matrix binding) {
             if (isBoundTo(p)) return;
-            V temp_bound_to = p.bound_to;
-            M temp_binding = p.binding;
+            Vert temp_bound_to = p.bound_to;
+            Matrix 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;
@@ -349,14 +351,14 @@ public class Geom implements Iterable<Geom.T> {
             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();
+        Matrix binding = new Matrix();
         float oldscore = 0;
         boolean inserted = false;
     }
@@ -438,7 +440,7 @@ public class Geom implements Iterable<Geom.T> {
             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
@@ -447,8 +449,8 @@ public class Geom implements Iterable<Geom.T> {
 
         public BindingGroup bg = new BindingGroup(this);
 
-        public void bind(E e) { bind(e, new M()); }
-        public void bind(E e, M m) { e.bg.add(this); }
+        public void bind(E e) { bind(e, new Matrix()); }
+        public void bind(E e, Matrix m) { e.bg.add(this); }
 
         public void dobind() {
             if (bg==null) return;
@@ -460,12 +462,12 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         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 +559,7 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         /** 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 +568,7 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         /** 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 +595,8 @@ public class Geom implements Iterable<Geom.T> {
             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 +610,7 @@ public class Geom implements Iterable<Geom.T> {
             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 +650,15 @@ public class Geom implements Iterable<Geom.T> {
 
             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 +670,7 @@ public class Geom implements Iterable<Geom.T> {
             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,106 +681,6 @@ public class Geom implements Iterable<Geom.T> {
 
     }
     
-    public V register(P p) { V v = ps.get(p); return v==null ? new V(p) : v; }
-
-    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    /** point in 3-space; immutable */
-    public static final class P {
-        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 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 void glVertex(GL gl) { _glVertex(gl); }
-        private void _glVertex(GL gl) { gl.glVertex3f(x, y, z); }
-        public String toString() { return "("+x+","+y+","+z+")"; }
-        public int hashCode() { return Float.floatToIntBits(x) ^ Float.floatToIntBits(y) ^ Float.floatToIntBits(z); }
-    }
-
-    /** vector in 3-space; immutable */
-    public static final class Vec {
-        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 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()); }
-        public Vec times(M m) { return m.apply(this); }
-        public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
-        public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
-        public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
-        public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
-        public String toString() { return "<"+x+","+y+","+z+">"; }
-    }
-
-    /** affine matrix; immutable */
-    public static class M {
-        //
-        //  [ a b c d ]   [ x ]
-        //  [ e f g h ]   [ y ]
-        //  [ i j k l ]   [ z ]
-        //  [ 0 0 0 1 ]   [ 1 ]
-        //
-        public final float a, b, c, d, e, f, g, h, i, j, k, l;
-        public M() { this(1); }
-        public M(float scale) {
-            a = f = k = scale;
-            l = h = d = e = b = i = c = j = g = 0;            
-        }
-        public M(float scalex, float scaley, float scalez) {
-            a = scalex;
-            f = scaley;
-            k = scalez;
-            l = h = d = e = b = i = c = j = g = 0;            
-        }
-        public M(Vec translate) {
-            d = translate.x; h = translate.y; l = translate.z;
-            a = f = k = 1;
-            b = c = e = g = i = j = 0;
-        }
-        public M(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l) {
-            this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h; this.i = i;
-            this.j = j; this.k = k; this.l = l;
-        }
-        public M times(float x) {
-            return new M(a*x, b*x, c*x, d*x, e*x, f*x, g*x, h*x, i*x, j*x, k*x, l*x);
-        }
-        public M(Vec axis, float angle) {
-            double q = Math.cos(angle);
-            double s = Math.sin(angle);
-            double t = 1.0 - q;
-            a = (float)(q + axis.x*axis.x*t);
-            f = (float)(q + axis.y*axis.y*t);
-            k = (float)(q + axis.z*axis.z*t);
-            double tmp1 = axis.x*axis.y*t;
-            double tmp2 = axis.z*s;
-            e = (float)(tmp1 + tmp2);
-            b = (float)(tmp1 - tmp2);
-            tmp1 = axis.x*axis.z*t;
-            tmp2 = axis.y*s;
-            i = (float)(tmp1 - tmp2);
-            c = (float)(tmp1 + tmp2);
-            tmp1 = axis.y*axis.z*t;
-            tmp2 = axis.x*s;
-            j = (float)(tmp1 + tmp2);
-            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,
-                        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 Vec apply(Vec v) { return v; }
-        public M invert() { return this; }
-        public M times(M m) { return this; }
-    }
+    public Vert register(Point p) { Vert v = ps.get(p); return v==null ? new Vert(p) : v; }
 
 }