
// Conforming.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

/* The points and constraining lines are displayed in GeomsViewer,
   and then the resulting triangles after calling 
   ConformingDelaunayTriangulationBuilder.

   Usage:
    > compile Conforming.java
    > run Conforming

  When prompted, press <enter> on the command line to move 
  to the next example.

  For:
   * Delaunay Triangulation -- see Covers.java
   * Voronoi diagramming
   * Convex Hull

   * Constrained Delaunay  -- see Triangulate.java
   * Polygon Triangulation

   * Concave Hull -- see Concanvity.java 
*/

import org.locationtech.jts.geom.*;
// import org.locationtech.jts.io.*;
import org.locationtech.jts.triangulate.*;



public class Conforming
{
  private static GeomsViewer gv;


  public static void main(String args[]) 
  {
    gv = Utils.view("Conforming Delaunay");

    // initial points and constraining lines
  	String ptsWKT = "MULTIPOINT ((90 290), (120 250), (280 250), (200 200), (220 290), (170 320), (110 180), (70 140), (180 110), (210 80), (250 120))";
  	Geometry pts = Utils.toGeometry(ptsWKT);
    gv.add(pts);

  	String constraints = "MULTILINESTRING ((130 160, 150 280, 200 250), (180 120, 240 230), (40 270, 90 220), (90 270, 130 290, 100 280, 140 310))";
  	Geometry lines = Utils.toGeometry(constraints);
  	gv.add(lines);

    // triangulate
  	ConformingDelaunayTriangulationBuilder builder = 
             new ConformingDelaunayTriangulationBuilder();
  	builder.setSites(pts);
  	builder.setConstraints(lines);

  	GeometryFactory gf = new GeometryFactory();
  	GeometryCollection polys = (GeometryCollection)builder.getTriangles(gf);  		

    Utils.waitEnter();
  	gv.remove(lines);

    // display the triangles
    int count = polys.getNumGeometries();
    for (int i=0; i < count; i++) {
      Geometry tri = polys.getGeometryN(i);
      System.out.println(" " + (i+1)+ ". " + Utils.simplify(tri));
      gv.add(tri);
    }

    Utils.waitEnter();
    gv.close();
  }  // end of main()

}  // end of Conforming class
