checkpoint autogen tile
[anneal.git] / src / edu / berkeley / qfat / geom / Polygon.java
index e2d3bc5..ff75939 100644 (file)
@@ -25,6 +25,10 @@ public final class Polygon {
         return new Polygon(plane, newHalfSpaces);
     }
 
+    private static final float EPSILON = 0.00001f;
+    private static float round(float f) {
+        return Math.round(f*1000)/1000f;
+    }
     public Triangle[] tesselate(Mesh mesh) {
         // FIXME: check for closedness
         // find a starting point
@@ -36,18 +40,21 @@ public final class Polygon {
                 if (i==j) continue;
                 Point p = plane.intersect(halfSpaces[i], halfSpaces[j]);
                 if (p==null) continue;
+
+                //p = new Point(round(p.x), round(p.y), round(p.z));
+
                 for(int k=0; k<halfSpaces.length; k++) {
                     if (i==k || j==k) continue;
-                    //if (!halfSpaces[k].contains(p)) { p = null; break; }
+                    if (!halfSpaces[k].contains(p)) { p = null; break; }
                 }
                 if (p!=null) {
                     if (p1==null) p1 = p;
-                    else if (p2==null) { if (!p.equals(p1)) p2 = p; }
-                    else throw new Error();
+                    else if (p2==null) { if (p.distance(p1)>EPSILON) p2 = p; }
+                    else if (p.distance(p1)>EPSILON && p.distance(p2)>EPSILON) throw new Error("three points! " + p + " " + p1 + " " + p2);
                 }
             }
             if (p1!=null && p2!=null) {
-                System.out.println("new segment: " + p1 + " " + p2);
+                //System.out.println("new segment: " + p1 + " " + p2);
                 segments.add(new Segment(p1, p2));
             }
         }
@@ -55,6 +62,8 @@ public final class Polygon {
         for(Segment s : segments)
             cen = cen.plus(s.p1.minus(Point.ORIGIN)).plus(s.p2.minus(Point.ORIGIN));
         Point centroid = Point.ORIGIN.plus(cen.times(1f/(2*segments.size())));
+        //centroid = new Point(round(centroid.x), round(centroid.y), round(centroid.z));
+        if (segments.size() >= 3)
         for(Segment s : segments) {
             System.out.println("newt! " + s.p1 + " " + centroid + " " + s.p2 + " " + plane.norm.times(-1));
             mesh.newT(s.p1, centroid, s.p2, plane.norm.times(-1), 0);