import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;

public class TriangulatorTest {


  public TriangulatorTest(){
  }

 //makes a circle with added jitter on vertices to make it concave
  public static Point3d[] makeRandomPolygon(){
    int FLATNESS = Math.max((int)(Math.random()*200),3);
    Point3d[] pts = new Point3d[FLATNESS];
    double angIncr = Math.PI / (FLATNESS+1);
    double ang = 0;
    double rad = Math.random()*100+50;
    for(int i = 0;i < FLATNESS;i++){
      Vector3d toPoint = new Vector3d(Math.cos(ang),Math.sin(ang),0 );
      toPoint.scale(rad+(Math.random()*10) );
      pts[i] = new Point3d(toPoint.x+300,toPoint.y+300,0);
      ang+=angIncr;
    }
    return(pts);
  }


  public static void test(){
      Triangulator tr = new Triangulator();
      GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY );
      for(int i = 0;i < 1000;i++){
        gi.reset(GeometryInfo.POLYGON_ARRAY);
        Point3d[] poly = makeRandomPolygon();
        int[] strips = {poly.length };
        gi.setCoordinates(poly) ;
        gi.setStripCounts(strips);
        //the following commented line fixes the problem
    //    tr = new Triangulator();
        try{
        tr.triangulate(gi);
        }
        catch(Exception ex){
/*          System.out.println("Exception occurred for polygon:");
          for(int j = 0;j < poly.length;j++){
            System.out.println(poly[j]);
          }*/
          ex.printStackTrace();
        }

      }
  }

  public static void main(String args[]){
    test();
  }
}