checkpoint
[anneal.git] / src / Geom.java
index c7374f7..9cc1a4c 100644 (file)
@@ -13,7 +13,7 @@ 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>();
+    private HashMap<P,V>  ps = new HashMap<P,V>();
     public  HashSet<E>    es = new HashSet<E>();
     public  ArrayList<T>  ts = new ArrayList<T>();
 
@@ -30,27 +30,27 @@ 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 Vec diagonal() {
@@ -94,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, Vec norm) {
+    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);
     }
 
@@ -121,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();
@@ -140,44 +140,27 @@ public class Geom implements Iterable<Geom.T> {
         return ret;
     }
 
-    public M aspect = new M();
-    public M invaspect = new M();
-
-    /** [UNIQUE] point in 3-space */
-    public final class P {
-
-        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); }
-            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); }
         }
 
@@ -195,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;
 
@@ -211,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;
@@ -234,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) {
@@ -248,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);
             }
@@ -281,7 +252,7 @@ public class Geom implements Iterable<Geom.T> {
         }
         public boolean move(Vec v) {
             M m = new M(v);
-            P p = this;
+            V p = this;
             boolean good = true;
             do {
                 good &= p.transform(m);
@@ -291,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);
@@ -318,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 {
@@ -329,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;
@@ -340,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;
 
+        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) {
-            if (o==null || !(o instanceof P)) return false;
-            P p = (P)o;
-            return p.x==x && p.y==y && p.z==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
@@ -370,22 +363,6 @@ 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 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();
-        }
     }
 
     /** vector in 3-space */
@@ -501,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
@@ -523,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();
 
@@ -621,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);
@@ -632,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;
@@ -718,9 +695,9 @@ 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; }