fixed bugs
[anneal.git] / src / Geom.java
index 3a5f4a6..f3c37f4 100644 (file)
@@ -47,7 +47,11 @@ public class Geom implements Iterable<Geom.T> {
             e23.makeAdjacent(e31);
             e31.makeAdjacent(e12);
         }
-        return e12.newT();
+        T ret = e12.newT();
+        if (e12.t == null) throw new Error();
+        if (e23.t == null) throw new Error();
+        if (e31.t == null) throw new Error();
+        return ret;
     }
 
     private char allname = 'A';
@@ -65,20 +69,20 @@ public class Geom implements Iterable<Geom.T> {
         public E makeE(P p2) {
             E e = getE(p2);
             if (e != null) return e;
+            e = p2.getE(this);
             if (this.e == null && p2.e == null) return this.e = new E(this, p2);
             if (this.e == null && p2.e != null) return p2.makeE(this).pair;
-
-            e = getFreeIncident();
-            if (e==null) throw new Error("could not find free incident to " + this);
-            return new E(e, p2);
+            return new E(getFreeIncident(), p2);
         }
 
         public E getFreeIncident() {
             E ret = getFreeIncident(e, e);
             if (ret != null) return ret;
             ret = getFreeIncident(e.pair.next, e.pair.next);
+            if (ret == null) throw new Error("unable to find free incident to " + this);
             return ret;
         }
+
         public E getFreeIncident(E start, E before) {
             E e = start;
             do {
@@ -88,15 +92,6 @@ public class Geom implements Iterable<Geom.T> {
             return null;
         }
 
-        public E listIncidents(E start, E before) {
-            E e = start;
-            do {
-                if (e.pair.p2 == this && e.pair.t == null) System.out.println(e.pair + " / " + e.pair.t);
-                e = e.pair.next;
-            } while(e != before);
-            return null;
-        }
-
         public E getE(P p2) {
             E e = this.e;
             do {
@@ -176,13 +171,13 @@ public class Geom implements Iterable<Geom.T> {
                 Float.floatToIntBits(z);
         }
         public void glVertex(GL gl) { gl.glVertex3f(x, y, z); }
-        public String toString() { return ""+name; }
-        //{ return "("+x+","+y+","+z+")"; }
+        //public String toString() { return ""+name; }
+        public String toString() { return "("+x+","+y+","+z+")"; }
         public V norm() {
             V norm = new V(0, 0, 0);
             E e = this.e;
             do {
-                norm = norm.plus(e.t.norm().times((float)e.prev.angle()));
+                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();
@@ -213,21 +208,13 @@ public class Geom implements Iterable<Geom.T> {
         E next;
         E pair;
 
-        private void syncm() {
+        private void sync() {
             this.prev.next = this;
             this.next.prev = this;
             this.pair.pair = this;
             if (this.next.p1 != p2) throw new Error();
             if (this.prev.p2 != p1) throw new Error();
-        }
-        private void sync() {
-            syncm();
-            this.p1.e = this;
-            System.out.println("make " + this);
-            /*
-            if (next != this && next.next != this && next.next.next == this)
-                newT();
-            */
+            if (this.p1.e == null) this.p1.e = this;
         }
 
         public T newT() {
@@ -247,9 +234,7 @@ public class Geom implements Iterable<Geom.T> {
             if (p2 != e.p1) throw new Error("cannot make adjacent -- no shared vertex");
             if (t != null || e.t != null) throw new Error("cannot make adjacent -- edges not both free");
 
-            System.out.println(this + ".makeAdjacent("+e+") -- from " + this.next);
-            E freeIncident = p2.getFreeIncident();
-            if (freeIncident==null) throw new Error("unable to find freeIncident");
+            E freeIncident = p2.getFreeIncident(e, this);
 
             e.prev.next = freeIncident.next;
             freeIncident.next.prev = e.prev;
@@ -260,10 +245,8 @@ public class Geom implements Iterable<Geom.T> {
             this.next = e;
             e.prev = this;
 
-            syncm();
-            freeIncident.syncm();
-
-            //throw new Error("makeAdjacent() failed");
+            sync();
+            freeIncident.sync();
         }
 
         /** creates an isolated edge out in the middle of space */
@@ -280,19 +263,16 @@ public class Geom implements Iterable<Geom.T> {
             this.p1 = prev.p2;
             this.p2 = p2;
             this.prev = prev;
+            if (p2.getE(p1) != null) throw new Error();
             if (p2.e==null) {
                 this.next = this.pair = new E(this, this, prev.next);
             } else {
                 E q = p2.getFreeIncident();
-                if (q==null) {
-                    System.out.println("listing:");
-                    p2.listIncidents(p2.e, p2.e);
-                    p2.listIncidents(p2.e.pair.next, p2.e.pair.next);
-                    throw new Error("could not find free incident to " + p2 + " from " + p2.e);
-                }
                 this.next = q.next;
                 this.next.prev = this;
-                this.pair = new E(q, this, prev.next);
+                E z = prev.next;
+                this.prev.next = this;
+                this.pair = new E(q, this, z);
             }
             sync();
         }