checkpoint
[anneal.git] / src / Geom.java
index 83df984..9f83c77 100644 (file)
@@ -25,6 +25,25 @@ public class Geom implements Iterable<Geom.T> {
     public double score = 0;
     public float score() { return (float)score; }
 
+    public void unbind() {
+
+        for(Geom.T t : this) {
+            t.p1().unbind();
+            t.p2().unbind();
+            t.p3().unbind();
+        }
+
+    }
+    public void bind() {
+        for(Geom.T t : this) {
+            t.e1().dobind();
+            t.e2().dobind();
+            t.e3().dobind();
+        }
+    }
+    public int numedges = 0;
+    public float avgedge = 0;
+
     public float rescore() {
         int num = 0;
         double dist = 0;
@@ -131,6 +150,25 @@ public class Geom implements Iterable<Geom.T> {
         return ret;
     }
 
+    public class BindingGroup {
+        public HashSet<E> es = new HashSet<E>();
+        public BindingGroup() { }
+        public BindingGroup(E e) {
+            es.add(e);
+        }
+        public void add(E e) {
+            if (e.bg != null) { merge(e.bg); return; }
+            es.add(e);
+            e.bg = this;
+        }
+        public void merge(BindingGroup bg) {
+            for(E e : bg.es) {
+                e.bg = null;
+                add(e);
+            }
+        }
+    }
+
     public final class V {
         public P p;
         public V(P p) {
@@ -138,9 +176,6 @@ public class Geom implements Iterable<Geom.T> {
             if (ps.get(p) != null) throw new Error();
             ps.put(this.p, this);
         }
-        public int hashCode() {
-            throw new Error();
-        }
         public void kdremove() {
             if (!inserted) return;
             inserted = false;
@@ -168,7 +203,7 @@ public class Geom implements Iterable<Geom.T> {
         }
         public V partner() { return watch==null ? this : watch; }
         public V watchback() { return watch_count==0 ? partner() :
-                new P(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count).register(); }
+                register(new P(watch_x/watch_count, watch_y/watch_count, watch_z/watch_count)); }
         public void rescore() {
             if (score_against == null) return;
 
@@ -312,8 +347,8 @@ public class Geom implements Iterable<Geom.T> {
             } while(e != this.e);
             return norm.norm();
         }
-        V bound_to = this;
 
+        V bound_to = this;
         int watch_count;
         float watch_x;
         float watch_y;
@@ -325,65 +360,6 @@ public class Geom implements Iterable<Geom.T> {
         boolean inserted = false;
     }
 
-    public class P {
-        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 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 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+")"; }
-        // FIXME: moving a point alters its hashCode
-        public int hashCode() {
-            return
-                Float.floatToIntBits(x) ^
-                Float.floatToIntBits(y) ^
-                Float.floatToIntBits(z);
-        }
-    }
-
-    /** vector in 3-space */
-    public 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+">"; }
-    }
-
-    public class BindingGroup {
-        public HashSet<E> es = new HashSet<E>();
-        public BindingGroup() { }
-        public BindingGroup(E e) {
-            es.add(e);
-        }
-        public void add(E e) {
-            if (e.bg != null) { merge(e.bg); return; }
-            es.add(e);
-            e.bg = this;
-        }
-        public void merge(BindingGroup bg) {
-            for(E e : bg.es) {
-                e.bg = null;
-                add(e);
-            }
-        }
-    }
-
     /** [UNIQUE] an edge */
     public final class E implements Comparable<E> {
 
@@ -483,7 +459,7 @@ public class Geom implements Iterable<Geom.T> {
         }
 
         boolean shattered = false;
-        public V shatter() { return shatter(midpoint().register(), null, null); }
+        public V shatter() { return shatter(register(midpoint()), null, null); }
         public V shatter(V mid, BindingGroup bg1, BindingGroup bg2) {
             if (shattered) return mid;
             shattered = true;
@@ -494,7 +470,7 @@ public class Geom implements Iterable<Geom.T> {
 
             if (bg1==null) bg1 = new BindingGroup();
             if (bg2==null) bg2 = new BindingGroup();
-            for(E e : bg.es) e.shatter(e.midpoint().register(), bg1, bg2);
+            for(E e : bg.es) e.shatter(register(e.midpoint()), bg1, bg2);
             pair.shatter();
             destroy();
 
@@ -701,10 +677,47 @@ 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+">"; }
+    }
 
-    /** matrix */
-    public class M {
+    /** affine matrix */
+    public static class M {
         //
         //  [ a b c d ]   [ x ]
         //  [ e f g h ]   [ y ]
@@ -767,22 +780,4 @@ public class Geom implements Iterable<Geom.T> {
         public M times(M m) { return this; }
     }
 
-    public void unbind() {
-
-        for(Geom.T t : this) {
-            t.p1().unbind();
-            t.p2().unbind();
-            t.p3().unbind();
-        }
-
-    }
-    public void bind() {
-        for(Geom.T t : this) {
-            t.e1().dobind();
-            t.e2().dobind();
-            t.e3().dobind();
-        }
-    }
-    public int numedges = 0;
-    public float avgedge = 0;
 }