checkpoint
[anneal.git] / src / edu / berkeley / qfat / Mesh.java
index 23ae1c8..8a697dc 100644 (file)
@@ -319,33 +319,52 @@ public class Mesh implements Iterable<Mesh.T> {
     }
 
     public class BindingGroup {
-        private HashSet<E> ess = new HashSet<E>();
+        private HashSet<E> left = new HashSet<E>();
+        private HashSet<E> right = new HashSet<E>();
         public BindingGroup() { }
         public BindingGroup(E e) {
-            ess.add(e);
-        }
-        public void add(E e) {
-            if (e.bg != null) { merge(e.bg); return; }
-            ess.add(e);
-            e.bg = this;
-        }
-        public void merge(BindingGroup bg) {
-            for(E e : bg.ess) {
-                e.bg = null;
-                add(e);
+            left.add(e);
+        }
+        public void add(E e, boolean swap) {
+            if (e.bg != null) {
+                if (e.bg == this) return;
+                for(E ex : (!swap ? e.bg.left : e.bg.right)) {
+                    ex.bg = this;
+                    left.add(ex);
+                }
+                for(E ex : (!swap ? e.bg.right : e.bg.left)) {
+                    ex.bg = this;
+                    right.add(ex);
+                }
+            } else {
+                (!swap ? left : right).add(e);
+                e.bg = this;
             }
         }
         public void dobind(E e) {
-            for(E ex : ess) {
+            // assumes e is part of the "left" set
+            Vert v1 = null;
+            Vert v2 = null;
+            if (left.contains(e)) { v1 = e.p1; v2 = e.p2; }
+            if (right.contains(e)) { v1 = e.p2; v2 = e.p1; }
+            for(E ex : left) {
+                if (ex==e) continue;
+                v1.bind(ex.p1);
+                v2.bind(ex.p2);
+            }
+            for(E ex : right) {
                 if (ex==e) continue;
-                e.p1.bind(ex.p1);
-                e.p2.bind(ex.p2);
+                v1.bind(ex.p2);
+                v2.bind(ex.p1);
             }
         }
         public void shatter(BindingGroup bg1, BindingGroup bg2) {
-            for(E e : ess) {
+            for(E e : left) {
                 e.shatter(e.midpoint(), bg1, bg2);
             }
+            for(E e : right) {
+                e.shatter(e.midpoint(), bg2, bg1);  /* swap correct? */
+            }
         }
     }
 
@@ -363,7 +382,7 @@ public class Mesh implements Iterable<Mesh.T> {
         public int compareTo(E e) { return e.length() > length() ? 1 : -1; }
 
         public void bindEdge(E e) {
-            e.pair.bg.add(this);
+            bg.add(e.pair, false);
         }
         public void dobind() { if (bg != null) bg.dobind(this); }
 
@@ -384,8 +403,8 @@ public class Mesh implements Iterable<Mesh.T> {
 
             newT(r.p, p1.p, mid, null);
             newT(r.p, mid, p2.p, null);
-            //bg1.add(p1.getE(mid));
-            //bg2.add(p2.getE(mid).pair);
+            bg1.add(p1.getE(mid), false);
+            bg2.add(p2.getE(mid).pair, false);
             return mid;
         }