This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 0faf70fa52b27a4a992d66474a0969862196980d Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:52:09 2026 +0100 feat(Geometry): compute 2D convex hulls natively --- .../geometries/operation/GeometryProcessor.java | 82 +++++++++++++++++++++- .../sis/geometries/operation/ConvexHullTest.java | 45 ++++++++++++ 2 files changed, 124 insertions(+), 3 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java index e00695c7c3..afd492bf39 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/GeometryProcessor.java @@ -18,6 +18,7 @@ package org.apache.sis.geometries.operation; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -25,6 +26,7 @@ import java.util.Map.Entry; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.UnaryOperator; +import java.util.stream.IntStream; import javax.measure.Quantity; import static org.opengis.annotation.Specification.ISO_19107; import org.opengis.annotation.UML; @@ -71,6 +73,7 @@ import org.apache.sis.maths.Cursor; import org.apache.sis.maths.DataType; import org.apache.sis.maths.NDArrays; import org.apache.sis.maths.SampleSystem; +import org.apache.sis.maths.Maths; import org.apache.sis.maths.Vector; import org.apache.sis.measure.Quantities; import org.apache.sis.measure.Units; @@ -153,15 +156,88 @@ public final class GeometryProcessor { */ //@UML(identifier="3DconvexHull", specification=ISO_19107) // section 6.4.9 public Geometry convexHull(Geometry geom) throws OperationException { - if (geom.isEmpty()) { - // The convex hull of the empty set is empty. + if (geom.isEmpty() || geom instanceof Point) { + // The convex hull of the empty set is empty, and a point is its own hull. return geom; } - + if (geom.getDimension() == 2 && isMadeOfVertices(geom)) { + return convexHull2D(geom.getDataPoints()); + } //TODO : fallback on JTS until implemented, this loss the attributes ! return fromJTS(jts(geom).convexHull(), geom); } + /** + * Returns whether the given geometry is entirely defined by its vertices, which are its data points. + * The convex hull of such a geometry is the convex hull of its data points. + */ + private static boolean isMadeOfVertices(final Geometry geometry) { + return switch (geometry) { + case Point cdt -> true; + case LineString cdt -> true; + case Polygon cdt -> true; + case PolyhedralSurface<?> cdt -> true; + case MeshPrimitive cdt -> true; + case GeometryCollection<?> cdt -> cdt.stream().allMatch(GeometryProcessor::isMadeOfVertices); + default -> false; + }; + } + + /** + * Returns the convex hull of the given points in 2 dimensions, with Andrew's monotone chain algorithm. + * The hull is made of the given points with their attributes: a polygon whose ring is counterclockwise, + * or a line string or a point if all points are aligned or the same. + * + * @see "A. M. Andrew, Another efficient algorithm for convex hulls in two dimensions, 1979" + */ + private static Geometry convexHull2D(final DataPoints points) { + final Vector<?>[] positions = new Vector<?>[points.size()]; + for (int i = 0; i < positions.length; i++) { + positions[i] = points.getPosition(i); + } + final int[] sorted = IntStream.range(0, positions.length) + .filter((i) -> positions[i].isFinite()).boxed() + .sorted(Comparator.comparingDouble((Integer i) -> positions[i].get(0)).thenComparingDouble((i) -> positions[i].get(1))) + .mapToInt(Integer::intValue).toArray(); + // Identical positions are kept once, otherwise they would make edges without length. + final int[] order = new int[sorted.length]; + int count = 0; + for (final int i : sorted) { + if (count == 0 || !positions[i].equals(positions[order[count - 1]])) { + order[count++] = i; + } + } + if (count == 0) { + return GeometryFactory.DEFAULT.createEmpty(points.getCoordinateReferenceSystem()); + } + // Lower chain from left to right, then upper chain from right to left, keeping only left turns. + final int[] hull = new int[2 * count]; + int size = 0; + for (int i = 0; i < count; i++) { + while (size >= 2 && Maths.lineSide(positions[hull[size - 2]], positions[hull[size - 1]], positions[order[i]]) <= 0) { + size--; + } + hull[size++] = order[i]; + } + for (int i = count - 2, lower = size + 1; i >= 0; i--) { + while (size >= lower && Maths.lineSide(positions[hull[size - 2]], positions[hull[size - 1]], positions[order[i]]) <= 0) { + size--; + } + hull[size++] = order[i]; + } + size = Math.max(size - 1, 1); // The last point is the first one. + final GeometryFactory factory = GeometryFactory.DEFAULT; + switch (size) { + case 1: return points.getInterpolatedPoint(new int[] {hull[0]}, new double[] {1}); + case 2: return factory.createLineString(points.subset(Arrays.copyOf(hull, 2))); + default: { + final int[] ring = Arrays.copyOf(hull, size + 1); + ring[size] = ring[0]; + return factory.createPolygon(factory.createLinearRing(points.subset(ring)), List.of()); + } + } + } + /** * Returns a geometric object that represents the Point set difference of this geometric object with anotherGeometry. */ diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java index 8fd6b67d27..2d60ca7364 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/ConvexHullTest.java @@ -16,10 +16,21 @@ */ package org.apache.sis.geometries.operation; +import java.util.LinkedHashMap; +import java.util.Map; +import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.geometries.Point; +import org.apache.sis.geometries.curve.LineString; +import org.apache.sis.geometries.surface.Polygon; +import org.apache.sis.maths.Array; +import org.apache.sis.maths.NDArrays; // Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test; @@ -93,4 +104,38 @@ public class ConvexHullTest extends AbstractD9IMTest { assertEquals(500, areaOf(processor.convexHull(MULTI_POLYGON)), TOLERANCE, "The hull of two squares placed along a diagonal is a hexagon."); } + private static double[] positions(final Geometry geometry) { + return geometry.getDataPoints().getAttributeArray(DataPointsType.ATT_POSITION).toArrayDouble(); + } + + /** + * The hull of positions in 2 dimensions is computed natively: its ring is counterclockwise + * and starts at the lowest position. + */ + @Test + public void testConvexHullVertices() { + final GeometryProcessor processor = new GeometryProcessor(); + final Polygon hull = assertInstanceOf(Polygon.class, processor.convexHull(LINE_SELF_CROSSING)); + assertArrayEquals(new double[] {0,0, 10,0, 10,10, 0,10, 0,0}, positions(hull)); + + final LineString aligned = assertInstanceOf(LineString.class, processor.convexHull(MULTI_POINT_SPREAD)); + assertArrayEquals(new double[] {5,5, 20,20}, positions(aligned)); + + final Point same = assertInstanceOf(Point.class, processor.convexHull(wkt("MULTIPOINT ((1 1), (1 1))"))); + assertArrayEquals(new double[] {1, 1}, same.getPosition().toArrayDouble()); + } + + /** + * The vertices of the hull keep their attributes. + */ + @Test + public void testConvexHullAttributes() { + final Map<String,Array> attributes = new LinkedHashMap<>(); + attributes.put(DataPointsType.ATT_POSITION, NDArrays.of(CRS_2D, 0, 0, 2, 1, 4, 0, 2, 4)); + attributes.put("TEST", NDArrays.of(1, 1.0, 2, 3, 4)); + final LineString line = GeometryFactory.DEFAULT.createLineString(GeometryFactory.DEFAULT.createDataPoints(attributes)); + final Polygon hull = assertInstanceOf(Polygon.class, new GeometryProcessor().convexHull(line)); + assertArrayEquals(new double[] {0,0, 4,0, 2,4, 0,0}, positions(hull)); + assertArrayEquals(new double[] {1, 3, 4, 1}, hull.getDataPoints().getAttributeArray("TEST").toArrayDouble()); + } }
