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 f240dbe1cde23fa808dca346f01af17c842812fe Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:57:04 2026 +0100 feat(Geometry): add LineString.smooth --- .../apache/sis/geometries/curve/LineString.java | 121 +++++++++++++++++++++ .../sis/geometries/curve/LineStringTest.java | 41 +++++++ 2 files changed, 162 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/LineString.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/LineString.java index e2ee711738..7c9dee8eed 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/LineString.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/curve/LineString.java @@ -16,6 +16,9 @@ */ package org.apache.sis.geometries.curve; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import javax.measure.Quantity; import static org.opengis.annotation.Specification.ISO_19107; import org.opengis.annotation.UML; @@ -37,6 +40,10 @@ import org.apache.sis.geometries.internal.shared.DefaultLineString; import org.apache.sis.geometries.mesh.MeshPrimitive; import org.apache.sis.geometries.operation.triangulate.delaunay.OrientedEdge; import org.apache.sis.maths.Array; +import org.apache.sis.maths.DataType; +import org.apache.sis.maths.NDArrays; +import org.apache.sis.maths.SampleSystem; +import org.apache.sis.maths.Vector; /** @@ -196,6 +203,120 @@ public sealed interface LineString extends Curve return getDataPoints().size() == 2; } + /** + * Returns a smooth curve approximating this line string, made of straight segments and quadratic Bézier curves. + * A section of this line string is replaced by a straight segment if all its positions are within the line + * tolerance of that segment. Otherwise it is replaced by a quadratic curve passing through the position + * the farthest from the segment, if all positions are within the curve tolerance of that curve. + * Otherwise the section is split and both parts are processed in the same way. + * + * <ul> + * <li>The positions of a section are compared to the points of the segment or curve at the same fraction + * of the section length, measured along this line string.</li> + * <li>Consecutive straight segments are merged in a single line string.</li> + * <li>Only the positions are kept, not the other attributes.</li> + * </ul> + * + * @param lineTolerance largest distance between a position and a straight segment replacing it. + * @param curveTolerance largest distance between a position and a quadratic curve replacing it. + * @return a compound curve of line strings and quadratic Bézier curves. + * + * @see <a href="https://github.com/jankovicsandras/imagetracerjava">ImageTracer, by András Jankovics</a> + */ + default CompoundCurve smooth(final double lineTolerance, final double curveTolerance) { + final DataPoints points = getDataPoints(); + final Vector<?>[] positions = new Vector<?>[points.size()]; + final double[] distances = new double[positions.length]; + for (int i = 0; i < positions.length; i++) { + positions[i] = points.getPosition(i).copy(DataType.DOUBLE); + if (i != 0) { + distances[i] = distances[i - 1] + positions[i].distance(positions[i - 1]); + } + } + final List<Vector<?>[]> pieces = new ArrayList<>(); + fit(positions, distances, 0, positions.length - 1, lineTolerance * lineTolerance, curveTolerance * curveTolerance, pieces); + /* + * A piece of 2 positions is a straight segment, merged with the previous one if any. + * A piece of 3 positions is a quadratic Bézier curve. + */ + final CoordinateReferenceSystem crs = getCoordinateReferenceSystem(); + final SampleSystem system = SampleSystem.of(crs); + final GeometryFactory factory = GeometryFactory.DEFAULT; + final List<Curve> parts = new ArrayList<>(); + final List<Vector<?>> line = new ArrayList<>(); + for (final Vector<?>[] piece : pieces) { + if (piece.length == 2) { + if (line.isEmpty()) line.add(piece[0]); + line.add(piece[1]); + continue; + } + if (!line.isEmpty()) { + parts.add(factory.createLineString(factory.createDataPoints(NDArrays.of(line, system, DataType.DOUBLE)))); + line.clear(); + } + parts.add(factory.createBezier(factory.createDataPoints(NDArrays.of(List.of(piece[0], piece[2]), system, DataType.DOUBLE)), + NDArrays.of(Arrays.asList(piece), system, DataType.DOUBLE), new double[] {0, 1}, 2, null, null, null, null)); + } + if (!line.isEmpty()) { + parts.add(factory.createLineString(factory.createDataPoints(NDArrays.of(line, system, DataType.DOUBLE)))); + } + return factory.createCompoundCurve(crs, parts.toArray(Curve[]::new)); + } + + /** + * Fits a straight segment or a quadratic curve on the positions from {@code start} to {@code end} inclusive, + * or splits them in two sections fitted recursively. The fitted pieces are added to the given list as + * 2 positions for a segment, or 3 control points for a quadratic curve. + * + * @param positions all positions of the line string. + * @param distances distance of each position from the start, along the line string. + * @param lineTolerance2 square of the line tolerance. + * @param curveTolerance2 square of the curve tolerance. + * @param pieces where to add the fitted pieces. + */ + private static void fit(final Vector<?>[] positions, final double[] distances, final int start, final int end, + final double lineTolerance2, final double curveTolerance2, final List<Vector<?>[]> pieces) + { + final Vector<?> a = positions[start]; + final Vector<?> b = positions[end]; + final double length = distances[end] - distances[start]; + // Straight segment, and the position the farthest from it. + int farthest = -1; + double error = 0; + for (int i = start + 1; i < end; i++) { + final double d2 = positions[i].distanceSquare(a.lerp(b, (distances[i] - distances[start]) / length)); + if (d2 > error) { + error = d2; + farthest = i; + } + } + if (error <= lineTolerance2) { + pieces.add(new Vector<?>[] {a, b}); + return; + } + // Quadratic curve passing through the farthest position, at its fraction t of the section. + final double t = (distances[farthest] - distances[start]) / length; + final Vector<?> control = positions[farthest].combine(1, a, -(1 - t) * (1 - t)).combine(1, b, -t * t).scale(1 / (2 * (1 - t) * t)); + int worst = farthest; + error = 0; + for (int i = start + 1; i < end; i++) { + final double s = (distances[i] - distances[start]) / length; + final Vector<?> q = a.copy(DataType.DOUBLE).scale((1 - s) * (1 - s)).combine(1, control, 2 * (1 - s) * s).combine(1, b, s * s); + final double d2 = positions[i].distanceSquare(q); + if (d2 > error) { + error = d2; + worst = i; + } + } + if (error <= curveTolerance2) { + pieces.add(new Vector<?>[] {a, control, b}); + return; + } + final int split = (farthest + worst) / 2; + fit(positions, distances, start, split, lineTolerance2, curveTolerance2, pieces); + fit(positions, distances, split, end, lineTolerance2, curveTolerance2, pieces); + } + @Override default Envelope getEnvelope() { DataPoints points = getDataPoints(); diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/curve/LineStringTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/curve/LineStringTest.java index 609ae66704..51e198fe72 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/curve/LineStringTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/curve/LineStringTest.java @@ -25,6 +25,7 @@ import org.apache.sis.geometries.AttributeInterpolation; import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryCollection; +import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.Point; import org.apache.sis.maths.Array; @@ -448,4 +449,44 @@ public abstract class LineStringTest extends GeometryTest { @Disabled("Not implemented yet.") public void testUserProperties() { } + + private static LineString line2D(final double... coordinates) { + return GeometryFactory.DEFAULT.createLineString(GeometryFactory.DEFAULT.createDataPoints(Geometries.getUndefinedCRS(2), coordinates)); + } + + private static double[] positions(final Geometry geometry) { + return geometry.getDataPoints().getAttributeArray(DataPointsType.ATT_POSITION).toArrayDouble(); + } + + /** + * Positions within the line tolerance of the segment joining the ends are replaced by that segment. + */ + @Test + public void testSmoothLine() { + final CompoundCurve curve = line2D(0,0, 1,0.01, 2,0, 3,-0.01, 4,0).smooth(0.1, 0.1); + assertEquals(1, curve.getNumCurves()); + assertArrayEquals(new double[] {0,0, 4,0}, positions(assertInstanceOf(LineString.class, curve.getCurveN(0)))); + } + + /** + * Three positions not aligned are replaced by the quadratic Bézier curve passing through them. + */ + @Test + public void testSmoothCurve() { + final CompoundCurve curve = line2D(0,0, 1,1, 2,0).smooth(0.1, 0.1); + assertEquals(1, curve.getNumCurves()); + final Bezier bezier = assertInstanceOf(Bezier.class, curve.getCurveN(0)); + assertArrayEquals(new double[] {0,0, 1,2, 2,0}, bezier.getControlPoints().toArrayDouble(), 1E-12); + } + + /** + * A sharp corner fits neither a segment nor a quadratic curve: the line string is split + * until each section is straight, and the consecutive segments are merged. + */ + @Test + public void testSmoothCorner() { + final CompoundCurve curve = line2D(0,0, 1,0, 2,0, 2,1, 2,2).smooth(0.1, 0.1); + assertEquals(1, curve.getNumCurves()); + assertArrayEquals(new double[] {0,0, 1,0, 2,0, 2,2}, positions(assertInstanceOf(LineString.class, curve.getCurveN(0)))); + } }
