checkpoint
[anneal.git] / src / Geom.java
index 6bfe4f1..9cc1a4c 100644 (file)
@@ -13,11 +13,9 @@ public class Geom implements Iterable<Geom.T> {
     public static float EPSILON = (float)0.0001;
     public static Random random = new Random();
 
-    private HashMap<P,P> ps = new HashMap<P,P>();
-    //public PriorityQueue<E>   es = new PriorityQueue<E>();
-    public HashSet<E>   es = new HashSet<E>();
-    //private HashSet<T>   ts = new HashSet<T>();
-    public ArrayList<T>   ts = new ArrayList<T>();
+    private HashMap<P,V>  ps = new HashMap<P,V>();
+    public  HashSet<E>    es = new HashSet<E>();
+    public  ArrayList<T>  ts = new ArrayList<T>();
 
     public Iterator<T> iterator() { return ts.iterator(); }
 
@@ -32,30 +30,30 @@ public class Geom implements Iterable<Geom.T> {
     public float rescore() {
         int num = 0;
         double dist = 0;
-        HashSet<P> done = new HashSet<P>();
+        HashSet<V> done = new HashSet<V>();
         for(T t : ts)
-            for(P p : new P[] { t.p1(), t.p2(), t.p3() }) {
+            for(V p : new V[] { t.p1(), t.p2(), t.p3() }) {
                 if (done.contains(p)) continue;
                 done.add(p);
                 p.rescore();
             }
         for(T t : ts)
-            for(P p : new P[] { t.p1(), t.p2(), t.p3() })
+            for(V p : new V[] { t.p1(), t.p2(), t.p3() })
                 p.kdremove();
         kd = new KDTree(3);
         for(T t : ts)
-            for(P p : new P[] { t.p1(), t.p2(), t.p3() })
+            for(V p : new V[] { t.p1(), t.p2(), t.p3() })
                 p.kdinsert();
         return (float)(dist/num);
     }
 
     public void transform(M m) {
-        ArrayList<P> set = new ArrayList<P>();
-        set.addAll(ps.keySet());
-        for(P p : set) p.transform(m);
+        ArrayList<V> set = new ArrayList<V>();
+        set.addAll(ps.values());
+        for(V v : set) v.transform(m);
     }
 
-    public V diagonal() {
+    public Vec diagonal() {
         float min_x = Float.MAX_VALUE;
         float min_y = Float.MAX_VALUE;
         float min_z = Float.MAX_VALUE;
@@ -70,7 +68,7 @@ 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 V(max_x - min_x, max_y - min_y, max_z - min_z);
+        return new Vec(max_x - min_x, max_y - min_y, max_z - min_z);
     }
 
     public P centroid() {
@@ -96,11 +94,11 @@ public class Geom implements Iterable<Geom.T> {
     public P newP(double x, double y, double z) { return newP((float)x, (float)y, (float)z); }
     public P newP(float x, float y, float z) { return new P(x, y, z); }
     
-    public T newT(P p12, P p23, P p31, V norm) {
-        V norm2 = p31.minus(p12).cross(p23.minus(p12));
+    public T newT(V p12, V p23, V p31, Vec norm) {
+        Vec norm2 = p31.minus(p12).cross(p23.minus(p12));
         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) { P p = p12; p12=p23; p23 = p; }
+        if (dot < 0) { V p = p12; p12=p23; p23 = p; }
         return newT(p12, p23, p31);
     }
 
@@ -109,7 +107,7 @@ public class Geom implements Iterable<Geom.T> {
         double total = 0;
         for(T t : ts) {
             double area = t.area();
-            V origin_to_centroid = new V(newP(0, 0, 0), t.centroid());
+            Vec origin_to_centroid = new Vec(newP(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;
@@ -123,7 +121,7 @@ public class Geom implements Iterable<Geom.T> {
         return (P)results[0];
     }
 
-    public T newT(P p1, P p2, P p3) {
+    public T newT(V p1, V p2, V p3) {
         p1 = p1.register();
         p2 = p2.register();
         p3 = p3.register();
@@ -142,47 +140,27 @@ public class Geom implements Iterable<Geom.T> {
         return ret;
     }
 
-    private char allname = 'A';
-
-    public M aspect = new M();
-    public M invaspect = new M();
-
-    /** [UNIQUE] point in 3-space */
-    public final class P {
-        char name;
-
-        float x, y, z;
-
-        int watch_count;
-        float watch_x;
-        float watch_y;
-        float watch_z;
-        P watch;
-
-        private E e;                // some edge *leaving* this point
-        private M binding = new M();
-        private P bound_to = this;
-
-        private float oldscore = 0;
-        
-        private boolean inserted = false;
-
-        public P register() {
-            P p2 = ps.get(this);
-            if (p2==null) { p2 = this; ps.put(this,this); name = allname++; }
-            return p2;
+    public final class V extends P {
+        public P p = this;
+        public V(P p) {
+            super(p.x, p.y, p.z);
+            if (ps.get(p) != null) throw new Error();
+            this.p = p;
+            ps.put(this.p, this);
         }
-
+        /*
+        public int hashCode() {
+            throw new Error();
+        }
+        */
         public void kdremove() {
             if (!inserted) return;
             inserted = false;
-            P p = this;
             try { kd.delete(new double[]{p.x,p.y,p.z}); } catch (Exception e) { }
         }
         public void kdinsert() {
             if (inserted) return;
             inserted = true;
-            P p = this;
             try { kd.insert(new double[]{p.x,p.y,p.z},this); } catch (Exception e) { throw new Error(e); }
         }
 
@@ -200,15 +178,9 @@ public class Geom implements Iterable<Geom.T> {
             }
             watch = null;
         }
-        public P times(M m) { return m.times(this); }
-        public P partner() {
-            if (watch==null) return this;
-            return watch.times(score_against.aspect).times(invaspect);
-        }
-        public P watchback() {
-            if (watch_count==0) return partner();
-            return newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count);
-        }
+        public V partner() { return watch==null ? this : watch; }
+        public V watchback() { return watch_count==0 ? partner() :
+                newP(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count).register(); }
         public void rescore() {
             if (score_against == null) return;
 
@@ -216,12 +188,12 @@ public class Geom implements Iterable<Geom.T> {
             oldscore = 0;
 
             if (watch != null) unscore();
-            P po = this.times(aspect).times(score_against.invaspect);
+            V po = this;
             if (watch == null) {
-                watch = score_against.nearest(po);
+                watch = (V)score_against.nearest(po);
 
                 // don't attract to vertices that face the other way
-                if (watch.norm().times(score_against.aspect).times(invaspect).dot(norm()) < 0) {
+                if (watch.norm().dot(norm()) < 0) {
                     watch = null;
                 } else {
                     watch.watch_x += po.x;
@@ -239,13 +211,6 @@ public class Geom implements Iterable<Geom.T> {
             score += oldscore;
         }
 
-        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));
-        }
 
         /** does NOT update bound pairs! */
         public boolean transform(M m) {
@@ -253,16 +218,17 @@ public class Geom implements Iterable<Geom.T> {
             // FIXME: screws up hashmap
             unscore();
             try {
-                if (ps.get(this)==null) throw new Error();
-                ps.remove(this);
-                float newx = m.a*x + m.b*y + m.c*z + m.d;
-                float newy = m.e*x + m.f*y + m.g*z + m.h;
-                float newz = m.i*x + m.j*y + m.k*z + m.l;
+                if (ps.get(this.p)==null) throw new Error();
+                ps.remove(this.p);
+                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.x = newx;
                 this.y = newy;
                 this.z = newz;
+                this.p = new P(newx, newy, newz);
                 // FIXME: what if we move onto exactly where another point is?
-                ps.put(this,this);
+                ps.put(this.p,(V)this);
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
@@ -284,9 +250,9 @@ public class Geom implements Iterable<Geom.T> {
             */
             return good;
         }
-        public boolean move(V v) {
+        public boolean move(Vec v) {
             M m = new M(v);
-            P p = this;
+            V p = this;
             boolean good = true;
             do {
                 good &= p.transform(m);
@@ -296,8 +262,7 @@ public class Geom implements Iterable<Geom.T> {
             return good;
         }
 
-        public E makeE(P p2) {
-            p2 = p2.register();
+        public E makeE(V p2) {
             E e = getE(p2);
             if (e != null) return e;
             e = p2.getE(this);
@@ -323,7 +288,7 @@ public class Geom implements Iterable<Geom.T> {
             return null;
         }
 
-        public E getE(P p2) {
+        public E getE(V p2) {
             p2 = p2.register();
             E e = this.e;
             do {
@@ -334,9 +299,9 @@ public class Geom implements Iterable<Geom.T> {
             return null;
         }
 
-        public boolean isBoundTo(P p) {
+        public boolean isBoundTo(V p) {
             p = p.register();
-            P px = p;
+            V px = p;
             do {
                 if (px==this) return true;
                 px = px.bound_to;
@@ -345,29 +310,52 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         public void unbind() { bound_to = this; binding = new M(); }
-        public void bind(P p) { bind(p, new M()); }
-        public void bind(P p, M binding) {
+        public void bind(V p) { bind(p, new M()); }
+        public void bind(V p, M binding) {
             p = p.register();
             if (isBoundTo(p)) return;
-            P temp_bound_to = p.bound_to;
+            V 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
             this.bound_to = temp_bound_to;
             this.binding = temp_binding.times(temp_binding); // FIXME: may have order wrong here
         }
-
-        public P(float x, float y, float z) {
-            this.x = x; this.y = y; this.z = z;
+        public Vec norm() {
+            Vec norm = new Vec(0, 0, 0);
+            E e = this.e;
+            do {
+                if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
+                e = e.pair.next;
+            } while(e != this.e);
+            return norm.norm();
         }
+        V bound_to = this;
 
-        public V minus(P p) { return new V(x-p.x, y-p.y, z-p.z); }
-        public P plus(V v) { return newP(x+v.x, y+v.y, z+v.z); }
-        public boolean equals(Object o) {
-            if (o==null || !(o instanceof P)) return false;
-            P p = (P)o;
-            return p.x==x && p.y==y && p.z==z;
-        }
+        int watch_count;
+        float watch_x;
+        float watch_y;
+        float watch_z;
+        V watch;
+        E e;                // some edge *leaving* this point
+        M binding = new M();
+        float oldscore = 0;
+        boolean inserted = false;
+    }
+
+    public class P {
+        float x, y, 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 V register() { V v = ps.get(this); return v==null ? new V(this) : v; }
+        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 newP(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+")"; }
         // FIXME: moving a point alters its hashCode
         public int hashCode() {
             return
@@ -375,38 +363,22 @@ public class Geom implements Iterable<Geom.T> {
                 Float.floatToIntBits(y) ^
                 Float.floatToIntBits(z);
         }
-        public void glVertex(GL gl) {
-            this.times(aspect)._glVertex(gl);
-        }
-        private void _glVertex(GL gl) {
-            gl.glVertex3f(x, y, z);
-        }
-        public String toString() { return "("+x+","+y+","+z+")"; }
-        public V norm() {
-            V norm = new V(0, 0, 0);
-            E e = this.e;
-            do {
-                if (e.t != null) norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
-                e = e.pair.next;
-            } while(e != this.e);
-            return norm.norm();
-        }
     }
 
     /** vector in 3-space */
-    public final class V {
+    public final class Vec {
         public final float x, y, z;
-        public V(double x, double y, double z) { this((float)x, (float)y, (float)z); }
-        public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
-        public V(P p1, P p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
-        public V cross(V v) { return new V(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
-        public V plus(V v) { return new V(x+v.x, y+v.y, z+v.z); }
-        public V norm() { return mag()==0 ? this : div(mag()); }
-        public V times(M m) { return m.apply(this); }
+        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(V v) { return x*v.x + y*v.y + z*v.z; }
-        public V times(float mag) { return new V(x*mag, y*mag, z*mag); }
-        public V div(float mag) { return new V(x/mag, y/mag, z/mag); }
+        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+">"; }
     }
 
@@ -506,7 +478,7 @@ public class Geom implements Iterable<Geom.T> {
             return e.length() > length() ? 1 : -1;
         }
 
-        public final P p1, p2;
+        public final V p1, p2;
         T t;     // triangle to our "left"
         E prev;  // previous half-edge
         E next;  // next half-edge
@@ -528,19 +500,19 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         boolean shattered = false;
-        public P shatter() { return shatter(midpoint(), null, null); }
-        public P shatter(P mid, BindingGroup bg1, BindingGroup bg2) {
+        public V shatter() { return shatter(midpoint().register(), null, null); }
+        public V shatter(V mid, BindingGroup bg1, BindingGroup bg2) {
             mid = mid.register();
             if (shattered) return mid;
             shattered = true;
 
-            P r = next.p2;
+            V r = next.p2;
             E next = this.next;
             E prev = this.prev;
 
             if (bg1==null) bg1 = new BindingGroup();
             if (bg2==null) bg2 = new BindingGroup();
-            for(E e : bg.es) e.shatter(e.midpoint(), bg1, bg2);
+            for(E e : bg.es) e.shatter(e.midpoint().register(), bg1, bg2);
             pair.shatter();
             destroy();
 
@@ -600,8 +572,8 @@ public class Geom implements Iterable<Geom.T> {
 
         /** angle between this half-edge and the next */
         public double angle() {
-            V v1 = next.p2.minus(p2);
-            V v2 = this.p1.minus(p2);
+            Vec v1 = next.p2.minus(p2);
+            Vec v2 = this.p1.minus(p2);
             return Math.acos(v1.norm().dot(v2.norm()));
         }
 
@@ -626,7 +598,7 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         /** creates an isolated edge out in the middle of space */
-        public E(P p1, P p2) {
+        public E(V p1, V p2) {
             p1 = p1.register();
             p2 = p2.register();
             if (p1==p2) throw new Error("attempt to create edge with single vertex: " + p1);
@@ -637,7 +609,7 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         /** adds a new half-edge from prev.p2 to p2 */
-        public E(E prev, P p2) {
+        public E(E prev, V p2) {
             p2 = p2.register();
             this.p1 = prev.p2;
             this.p2 = p2;
@@ -723,18 +695,18 @@ public class Geom implements Iterable<Geom.T> {
 
             this.color = color;
         }
-        public P p1() { return e1.p1; }
-        public P p2() { return e1.p2; }
-        public P p3() { return e1.next.p2; }
+        public V p1() { return e1.p1; }
+        public V p2() { return e1.p2; }
+        public V p3() { return e1.next.p2; }
         public E e1() { return e1; }
         public E e2() { return e1.next; }
         public E e3() { return e1.prev; }
-        public V norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
+        public Vec norm() { return p2().minus(p1()).cross(p3().minus(p1())).norm(); }
         public boolean hasE(E e) { return e1==e || e1.next==e || e1.prev==e; }
         public boolean has(P p) { return p1()==p || p2()==p || p3()==p; }
 
         public float area() {
-            return (float)Math.abs(0.5 * e1().length() * new V(p1(), p2()).norm().dot(new V(p2(), p3())));
+            return (float)Math.abs(0.5 * e1().length() * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
         }
 
         public void glVertices(GL gl) {
@@ -775,7 +747,7 @@ public class Geom implements Iterable<Geom.T> {
             k = scalez;
             l = h = d = e = b = i = c = j = g = 0;            
         }
-        public M(V translate) {
+        public M(Vec translate) {
             d = translate.x; h = translate.y; l = translate.z;
             a = f = k = 1;
             b = c = e = g = i = j = 0;
@@ -787,7 +759,7 @@ public class Geom implements Iterable<Geom.T> {
         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(V axis, float angle) {
+        public M(Vec axis, float angle) {
             double q = Math.cos(angle);
             double s = Math.sin(angle);
             double t = 1.0 - q;
@@ -814,7 +786,7 @@ public class Geom implements Iterable<Geom.T> {
                         i*p.x + j*p.y + k*p.z + l);
         }
         public P apply(P p) { return p; }
-        public V apply(V v) { return v; }
+        public Vec apply(Vec v) { return v; }
         public M invert() { return this; }
         public M times(M m) { return this; }
     }