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 ffd2751d2765ee6098a1f861cb7a54cc658c17aa Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:55:10 2026 +0100 feat(Geometry): add Plane.fit --- .../main/org/apache/sis/geometries/Plane.java | 45 ++++++++++++++++++++++ .../test/org/apache/sis/geometries/PlaneTest.java | 20 ++++++++++ 2 files changed, 65 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Plane.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Plane.java index 907fba11af..50a5d17917 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Plane.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Plane.java @@ -23,6 +23,8 @@ import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.geometry.Envelope; import org.apache.sis.geometries.internal.shared.AbstractGeometry; import org.apache.sis.maths.DataType; +import org.apache.sis.maths.Matrix; +import org.apache.sis.maths.MatrixND; import org.apache.sis.maths.NDArrays; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.SampleSystem; @@ -87,6 +89,49 @@ public final class Plane extends AbstractGeometry{ this.normal = normal; } + /** + * Returns the plane best fitting the given positions, which minimizes the sum of their squared distances + * to the plane. The plane passes by the centroid of the positions, and its normal is the direction in which + * the positions are the least spread: the eigenvector of the smallest eigenvalue of their covariance matrix. + * + * <ul> + * <li>The direction of the normal is arbitrary, a plane having no side.</li> + * <li>The normal is arbitrary among several ones if the positions are aligned.</li> + * </ul> + * + * @param points the positions to fit, at least one. + * @return the plane best fitting the positions. + * @throws IllegalArgumentException if there is no position. + * + * @see Matrix#symmetricEigen() + */ + public static Plane fit(final DataPoints points) { + final int count = points.size(); + if (count == 0) { + throw new IllegalArgumentException("No position to fit."); + } + Vector<?> centroid = points.getPosition(0).copy(DataType.DOUBLE); + for (int i = 1; i < count; i++) { + centroid = centroid.add(points.getPosition(i)); + } + centroid = centroid.scale(1.0 / count); + final int dimension = centroid.getDimension(); + final Matrix<?> covariance = MatrixND.create(dimension, dimension); + for (int r = 0; r < dimension; r++) { + covariance.set(r, r, 0); + } + for (int i = 0; i < count; i++) { + final Vector<?> offset = points.getPosition(i).copy(DataType.DOUBLE).subtract(centroid); + for (int r = 0; r < dimension; r++) { + for (int c = 0; c < dimension; c++) { + covariance.set(r, c, covariance.get(r, c) + offset.get(r) * offset.get(c)); + } + } + } + final Vector<?> normal = covariance.symmetricEigen().vectors()[0]; + return new Plane(points.getCoordinateReferenceSystem(), centroid, normal); + } + @Override public GeometryType getGeometryType() { return GeometryType.PLANE; diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/PlaneTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/PlaneTest.java index 507aac2566..63234afba3 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/PlaneTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/PlaneTest.java @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries; +import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.apache.sis.maths.Vector; // Test dependencies @@ -57,4 +58,23 @@ public class PlaneTest extends GeometryTest { assertEquals(0, PLANE.angle(Vector.of(0, 1, 1)), TOLERANCE); assertEquals(Math.PI / 2, PLANE.angle(Vector.of(2, 0, 0)), TOLERANCE); } + + /** + * Fits a plane on positions of the plane <var>z</var> = 2<var>x</var> + 1, then on positions + * alternately 0.1 above and below the plane <var>z</var> = 0. The direction of the normal is arbitrary. + */ + @Test + public void testFit() { + final CoordinateReferenceSystem crs = Geometries.getUndefinedCRS(3); + Plane plane = Plane.fit(GeometryFactory.DEFAULT.createDataPoints(crs, 0,0,1, 1,0,3, 0,1,1, 1,1,3)); + assertArrayEquals(new double[] {0.5, 0.5, 2}, plane.getPosition().toArrayDouble(), TOLERANCE); + final double[] normal = plane.getNormal().toArrayDouble(); + final double sign = Math.signum(normal[0]); + assertArrayEquals(new double[] {0.8944271909999159, 0, -0.4472135954999579}, + new double[] {sign * normal[0], sign * normal[1], sign * normal[2]}, TOLERANCE); + + plane = Plane.fit(GeometryFactory.DEFAULT.createDataPoints(crs, 0,0,0.1, 2,0,-0.1, 0,2,-0.1, 2,2,0.1)); + assertArrayEquals(new double[] {1, 1, 0}, plane.getPosition().toArrayDouble(), TOLERANCE); + assertEquals(1, Math.abs(plane.getNormal().get(2)), TOLERANCE); + } }
