checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index f4f0837..0df0072 100644 (file)
@@ -116,25 +116,6 @@ public class Mesh implements Iterable<Mesh.T> {
         return (float)total;
     }
 
-    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 void rebuildPointSet() { pointset.rebuild(); }
     public Vec diagonal() { return pointset.diagonal(); }
     public Point centroid() { return pointset.centroid(); }
@@ -337,6 +318,52 @@ public class Mesh implements Iterable<Mesh.T> {
         }
     }
 
+    public class BindingGroup {
+        private HashSet<E> left = new HashSet<E>();
+        private HashSet<E> right = new HashSet<E>();
+        public BindingGroup() { }
+        public BindingGroup(E e) {
+            left.add(e);
+        }
+        public void add(E e) {
+            if (e.bg != null) {
+                if (e.bg == this) return;
+                for(E ex : e.bg.left) {
+                    ex.bg = this;
+                    left.add(ex);
+                }
+                for(E ex : e.bg.right) {
+                    ex.bg = this;
+                    right.add(ex);
+                }
+            } else {
+                left.add(e);
+                e.bg = this;
+            }
+        }
+        public void dobind(E e) {
+            // assumes e is part of the "left" set
+            for(E ex : left) {
+                if (ex==e) continue;
+                e.p1.bind(ex.p1);
+                e.p2.bind(ex.p2);
+            }
+            for(E ex : right) {
+                if (ex==e) continue;
+                e.p1.bind(ex.pair.p1);
+                e.p2.bind(ex.pair.p2);
+            }
+        }
+        public void shatter(BindingGroup bg1, BindingGroup bg2) {
+            for(E e : left) {
+                e.shatter(e.midpoint(), bg1, bg2);
+            }
+            for(E e : right) {
+                e.shatter(e.midpoint(), bg2, bg1);  /* swap correct? */
+            }
+        }
+    }
+
     /** [UNIQUE] an edge */
     public final class E implements Comparable<E> {
 
@@ -350,17 +377,10 @@ public class Mesh implements Iterable<Mesh.T> {
 
         public int compareTo(E e) { return e.length() > length() ? 1 : -1; }
 
-        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;
-            for(E ex : bg.es) {
-                if (ex==this) continue;
-                p1.bind(ex.p1);
-                p2.bind(ex.p2);
-            }
+        public void bindEdge(E e) {
+            e.pair.bg.add(this);
         }
+        public void dobind() { if (bg != null) bg.dobind(this); }
 
         public Point shatter() { return shatter(midpoint(), null, null); }
         public Point shatter(Point mid, BindingGroup bg1, BindingGroup bg2) {
@@ -373,7 +393,7 @@ public class Mesh implements Iterable<Mesh.T> {
 
             if (bg1==null) bg1 = new BindingGroup();
             if (bg2==null) bg2 = new BindingGroup();
-            for(E e : bg.es) e.shatter(e.midpoint(), bg1, bg2);
+            bg.shatter(bg1, bg2);
             pair.shatter();
             destroy();