--- /dev/null
+package edu.berkeley.qfat.geom;
+import javax.media.opengl.*;
+import java.util.*;
+import edu.berkeley.qfat.Mesh;
+
+// FIXME: numerical stability issues galore
+/** a planar, convex, possibly infinite/open polygon */
+public final class Polygon {
+
+ public final Plane plane;
+
+ private final HalfSpace[] halfSpaces;
+
+ public Polygon(Plane plane) { this(plane, new HalfSpace[0]); }
+
+ public Polygon(Plane plane, HalfSpace[] halfSpaces) {
+ this.plane = plane;
+ this.halfSpaces = halfSpaces;
+ }
+
+ public Polygon intersect(HalfSpace hs) {
+ HalfSpace[] newHalfSpaces = new HalfSpace[halfSpaces.length+1];
+ System.arraycopy(halfSpaces, 0, newHalfSpaces, 0, halfSpaces.length);
+ newHalfSpaces[newHalfSpaces.length-1] = hs;
+ return new Polygon(plane, newHalfSpaces);
+ }
+
+ public Triangle[] tesselate(Mesh mesh) {
+ // FIXME: check for closedness
+ // find a starting point
+ HashSet<Segment> segments = new HashSet<Segment>();
+ int i;
+ for(i=0; i<halfSpaces.length; i++) {
+ Point p1=null, p2=null;
+ for(int j=0; j<halfSpaces.length; j++) {
+ if (i==j) continue;
+ Point p = plane.intersect(halfSpaces[i], halfSpaces[j]);
+ if (p==null) continue;
+ for(int k=0; k<halfSpaces.length; k++) {
+ if (i==k || j==k) continue;
+ //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();
+ }
+ }
+ if (p1!=null && p2!=null) {
+ System.out.println("new segment: " + p1 + " " + p2);
+ segments.add(new Segment(p1, p2));
+ }
+ }
+ Vec cen = new Vec(0,0,0);
+ 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())));
+ 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);
+ }
+ System.out.println("done");
+ return null;
+ }
+
+}