Skip to content

Commit

Permalink
Add PolygonHull class (#861)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Davis <[email protected]>
  • Loading branch information
dr-jts authored Apr 14, 2022
1 parent a3a3860 commit a9614df
Show file tree
Hide file tree
Showing 10 changed files with 973 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2016 Vivid Solutions.
*
* 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.algorithm.hull.ConcaveHull;
import org.locationtech.jts.algorithm.hull.PolygonHull;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jtstest.geomfunction.Metadata;

public class HullFunctions {
public static Geometry convexHull(Geometry g) { return g.convexHull(); }

public static Geometry concaveHullByLen(Geometry geom,
@Metadata(title="Length")
double maxLen) {
return ConcaveHull.concaveHullByLength(geom, maxLen);
}

public static Geometry concaveHullWithHolesByLen(Geometry geom,
@Metadata(title="Length")
double maxLen) {
return ConcaveHull.concaveHullByLength(geom, maxLen, true);
}

public static Geometry concaveHullByLenRatio(Geometry geom,
@Metadata(title="Length Ratio")
double maxLen) {
return ConcaveHull.concaveHullByLengthRatio(geom, maxLen);
}

public static Geometry concaveHullWithHolesByLenRatio(Geometry geom,
@Metadata(title="Length Ratio")
double maxLen) {
return ConcaveHull.concaveHullByLengthRatio(geom, maxLen, true);
}

public static double concaveHullLenGuess(Geometry geom) {
return ConcaveHull.uniformGridEdgeLength(geom);
}

/**
* A concaveness measure defined in terms of the perimeter length
* relative to the convex hull perimeter.
* <pre>
* C = ( P(geom) - P(CH) ) / P(CH)
* </pre>
* Concaveness values are >= 0.
* A convex polygon has C = 0.
* A higher concaveness indicates a more concave polygon.
* <p>
* Originally defined by Park & Oh, 2012.
*
* @param geom a polygonal geometry
* @return the concaveness measure of the geometry
*/
public static double concaveness(Geometry geom) {
double convexLen = geom.convexHull().getLength();
return (geom.getLength() - convexLen) / convexLen;
}

public static Geometry polygonHull(Geometry geom,
@Metadata(title="Vertex Frac")
double vertexFrac) {
return PolygonHull.hull(geom, vertexFrac);
}

public static Geometry polygonHullByArea(Geometry geom,
@Metadata(title="Area Delta Ratio")
double areaFrac) {
return PolygonHull.hullByAreaDelta(geom, areaFrac);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.locationtech.jtstest.function.DoubleKeyMap;
import org.locationtech.jtstest.function.EditFunctions;
import org.locationtech.jtstest.function.GeometryFunctions;
import org.locationtech.jtstest.function.HullFunctions;
import org.locationtech.jtstest.function.JTSFunctions;
import org.locationtech.jtstest.function.LineHandlingFunctions;
import org.locationtech.jtstest.function.LineSegmentFunctions;
Expand Down Expand Up @@ -91,6 +92,7 @@ public static GeometryFunctionRegistry createTestBuilderRegistry()
funcRegistry.add(ConstructionFunctions.class);
funcRegistry.add(ConversionFunctions.class);
funcRegistry.add(EditFunctions.class);
funcRegistry.add(HullFunctions.class);
funcRegistry.add(LinearReferencingFunctions.class);
funcRegistry.add(LineHandlingFunctions.class);
funcRegistry.add(NodingFunctions.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.algorithm.hull;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateList;

class LinkedRing {

private static final int NO_COORD_INDEX = -1;

private final Coordinate[] coord;
private int[] next = null;
private int[] prev = null;
private int size;

public LinkedRing(Coordinate[] pts) {
coord = pts;
size = pts.length - 1;
next = createNextLinks(size);
prev = createPrevLinks(size);
}

private static int[] createNextLinks(int size) {
int[] next = new int[size];
for (int i = 0; i < size; i++) {
next[i] = i + 1;
}
next[size - 1] = 0;
return next;
}

private static int[] createPrevLinks(int size) {
int[] prev = new int[size];
for (int i = 0; i < size; i++) {
prev[i] = i - 1;
}
prev[0] = size - 1;
return prev;
}

public int size() {
return size;
}

public int next(int i) {
return next[i];
}

public int prev(int i) {
return prev[i];
}

public Coordinate getCoordinate(int index) {
return coord[index];
}

public Coordinate prevCoordinate(int index) {
return coord[prev(index)];
}

public Coordinate nextCoordinate(int index) {
return coord[next(index)];
}

public boolean hasCoordinate(int index) {
return index >= 0 && index < prev.length
&& prev[index] != NO_COORD_INDEX;
}

public void remove(int index) {
int iprev = prev[index];
int inext = next[index];
next[iprev] = inext;
prev[inext] = iprev;
prev[index] = NO_COORD_INDEX;
next[index] = NO_COORD_INDEX;
size--;
}

public Coordinate[] getCoordinates() {
CoordinateList coords = new CoordinateList();
for (int i = 0; i < coord.length - 1; i++) {
if (prev[i] != NO_COORD_INDEX) {
coords.add(coord[i].copy(), false);
}
}
coords.closeRing();
return coords.toCoordinateArray();
}
}
Loading

0 comments on commit a9614df

Please sign in to comment.