-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add polygon triangulation algorithms and Tri data structure (#775)
* Add polygon triangulation algorithms and Tri data structure Signed-off-by: Martin Davis <[email protected]>
- Loading branch information
Showing
21 changed files
with
2,590 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
modules/app/src/main/java/org/locationtech/jtstest/function/TriangulatePolyFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2021 Martin Davis. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* and Eclipse Distribution License v. 1.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*/ | ||
package org.locationtech.jtstest.function; | ||
|
||
import org.locationtech.jts.geom.Geometry; | ||
import org.locationtech.jts.triangulate.polygon.ConstrainedDelaunayTriangulator; | ||
import org.locationtech.jts.triangulate.polygon.PolygonTriangulator; | ||
|
||
|
||
public class TriangulatePolyFunctions | ||
{ | ||
public static Geometry triangulate(Geometry geom) | ||
{ | ||
return PolygonTriangulator.triangulate(geom); | ||
} | ||
|
||
public static Geometry constrainedDelaunay(Geometry geom) | ||
{ | ||
return ConstrainedDelaunayTriangulator.triangulate(geom); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...c/main/java/org/locationtech/jts/triangulate/polygon/ConstrainedDelaunayTriangulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (c) 2021 Martin Davis. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* and Eclipse Distribution License v. 1.0 which accompanies this distribution. | ||
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
*/ | ||
package org.locationtech.jts.triangulate.polygon; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.locationtech.jts.geom.Coordinate; | ||
import org.locationtech.jts.geom.Geometry; | ||
import org.locationtech.jts.geom.GeometryFactory; | ||
import org.locationtech.jts.geom.Polygon; | ||
import org.locationtech.jts.geom.util.PolygonExtracter; | ||
import org.locationtech.jts.triangulate.tri.Tri; | ||
import org.locationtech.jts.triangulate.tri.TriangulationBuilder; | ||
|
||
/** | ||
* Computes the Constrained Delaunay Triangulation of polygons. | ||
* The Constrained Delaunay Triangulation of a polygon is a set of triangles | ||
* covering the polygon, with the maximum total interior angle over all | ||
* possible triangulations. It provides the "best quality" triangulation | ||
* of the polygon. | ||
* <p> | ||
* Holes are supported. | ||
*/ | ||
public class ConstrainedDelaunayTriangulator { | ||
|
||
/** | ||
* Computes the Constrained Delaunay Triangulation of each polygon element in a geometry. | ||
* | ||
* @param geom the input geometry | ||
* @return a GeometryCollection of the computed triangle polygons | ||
*/ | ||
public static Geometry triangulate(Geometry geom) { | ||
ConstrainedDelaunayTriangulator cdt = new ConstrainedDelaunayTriangulator(geom); | ||
return cdt.compute(); | ||
} | ||
|
||
private final GeometryFactory geomFact; | ||
private final Geometry inputGeom; | ||
|
||
/** | ||
* Constructs a new Constrained Delaunay triangulator. | ||
* | ||
* @param inputGeom the input geometry | ||
*/ | ||
public ConstrainedDelaunayTriangulator(Geometry inputGeom) { | ||
geomFact = new GeometryFactory(); | ||
this.inputGeom = inputGeom; | ||
} | ||
|
||
private Geometry compute() { | ||
List<Polygon> polys = PolygonExtracter.getPolygons(inputGeom); | ||
List<Tri> triList = new ArrayList<Tri>(); | ||
for (Polygon poly : polys) { | ||
List<Tri> polyTriList = triangulatePolygon(poly); | ||
triList.addAll(polyTriList); | ||
} | ||
return Tri.toGeometry(triList, geomFact); | ||
} | ||
|
||
/** | ||
* Computes the triangulation of a single polygon | ||
* and returns it as a list of {@link Tri}s. | ||
* | ||
* @param poly the input polygon | ||
* @return list of Tris forming the triangulation | ||
*/ | ||
List<Tri> triangulatePolygon(Polygon poly) { | ||
/** | ||
* Normalize to ensure that shell and holes have canonical orientation. | ||
* | ||
* TODO: perhaps better to just correct orientation of rings? | ||
*/ | ||
Polygon polyNorm = (Polygon) poly.norm(); | ||
Coordinate[] polyShell = PolygonHoleJoiner.join(polyNorm); | ||
List<Tri> triList = PolygonEarClipper.triangulate(polyShell); | ||
|
||
//long start = System.currentTimeMillis(); | ||
TriangulationBuilder.build(triList); | ||
TriDelaunayImprover.improve(triList); | ||
//System.out.println("swap used: " + (System.currentTimeMillis() - start) + " milliseconds"); | ||
|
||
return triList; | ||
} | ||
|
||
} |
Oops, something went wrong.