
// Triangulate.java
// Martin Davis
// The LocationTech Technology (LTT) Group; Vivid Solutions
// https://github.com/locationtech/jts
// Modified by Andrew Davison, ad@coe.psu.ac.th, March 2023

/* For each of the three examples, the initial polygon is drawn
   and then is triangulated after the user presses <enter>.
   This done first by using Constrained Delaunay and then 
   with Polygon Triangulation.

   Usage:
    > compile Triangulate.java
    > run Triangulate

  When prompted, press <enter> on the command line to move 
  to the next example.

  For:
   * Delaunay Triangulation -- see Covers.java
   * Voronoi diagramming
   * Convex Hull

   * Conforming Delaunay   -- see Conforming.java

   * Concave Hull -- see Concanvity.java 
*/

import java.util.*;

import org.locationtech.jts.geom.*;
import org.locationtech.jts.triangulate.polygon.*;



public class Triangulate
{
  private static GeomsViewer gv;


  public static void main(String[] args)
  {
    gv = Utils.view("Triangulate");

    triangulate("pentagon",
          "POLYGON ((110 110, 120 140, 190 190, 200 150, 190 110, 110 110))");

    triangulate("poly with hole",
          "POLYGON ((110 190, 190 190, 190 120, 110 110, 110 190), (130 170, 180 170, 150 130, 130 170))");

    triangulate("c shape",
          "POLYGON ((110 170, 138 272, 145 286, 152 296, 160 307, 303 307, 314 301, 332 287, 343 278, 352 270, 385 99, 374 89, 359 79, 178 89, 167 91, 153 99, 146 107, 173 157, 182 163, 191 170, 199 176, 208 184, 218 194, 226 203, 198 252, 188 247, 182 239, 175 231, 167 223, 161 213, 156 203, 155 198, 110 170))");

    gv.close();
  } /* main */


  private static void triangulate(String title, String wtk)
  /* Triangulate in two different ways -- first with 
     Constrained Delaunay and then with Polygon Triangulation
  */
  {
    System.out.println("--- " + title + " ---");
    Geometry geom = Utils.toGeometry(wtk);

    // show original geometry
    gv.clear();
    gv.setTitle(title);
    gv.add(geom);
    Utils.waitEnter();

    // Constrained Delaunay
    gv.setTitle(title + ": Con Del");
    System.out.println(title + ": Con Del");
    GeometryCollection polys = 
         (GeometryCollection) ConstrainedDelaunayTriangulator.triangulate(geom);
    displayTris(polys);
    Utils.waitEnter();

    // show original geometry again
    gv.clear();
    gv.setTitle(title);
    System.out.println(title);
    gv.add(geom);
    Utils.waitEnter();

    // Polygon Triangulation
    gv.setTitle(title + ": Poly Tri");
    System.out.println(title + ": Poly Tri");
    polys =  (GeometryCollection) PolygonTriangulator.triangulate(geom);
    displayTris(polys);
    Utils.waitEnter();
  }  // end of triangulate()


  
  private static void displayTris(GeometryCollection polys)
  {
    int count = polys.getNumGeometries();
    System.out.println("Poly count: " + count);

    for (int i=0; i < count; i++) {
      Geometry tri = polys.getGeometryN(i);
      System.out.println(" " + (i+1)+ ". " + Utils.simplify(tri));
      gv.add(tri);
    }
  }  // end of displayTris()
 
} // end of Triangulate class
