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 3648feb7cbaacc56b8697de34df33e44413547c7 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:29:29 2026 +0100 feat(Geometry): add distance, project, reflect and angle on planes --- .../main/org/apache/sis/geometries/HyperPlane.java | 49 +++++++++++++++++ .../main/org/apache/sis/geometries/Plane.java | 56 ++++++++++++++++++++ .../org/apache/sis/geometries/HyperPlaneTest.java | 61 ++++++++++++++++++++++ .../test/org/apache/sis/geometries/PlaneTest.java | 60 +++++++++++++++++++++ 4 files changed, 226 insertions(+) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/HyperPlane.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/HyperPlane.java index d0dfac5ec4..6291c7459d 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/HyperPlane.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/HyperPlane.java @@ -22,6 +22,7 @@ import org.opengis.annotation.UML; 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.NDArrays; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.SampleSystem; @@ -112,6 +113,54 @@ public final class HyperPlane extends AbstractGeometry{ this.normal = normal; } + /** + * Returns the signed distance from this hyperplane to the given position: + * positive on the side the normal points to, negative on the other side. + * + * @param position the position to measure, of the dimension of this hyperplane. + * @return the signed distance, in the units of the coordinate system axes. + */ + public double signedDistance(final Vector<?> position) { + return offset(position) / normal.length(); + } + + /** + * Returns the position of this hyperplane nearest to the given position. + * + * @param position the position to project, of the dimension of this hyperplane. + * @return the orthogonal projection of the position on this hyperplane. + */ + public Vector<?> project(final Vector<?> position) { + return position.copy(DataType.DOUBLE).combine(1, normal, -offset(position) / normal.lengthSquare()); + } + + /** + * Returns the mirror image of the given position through this hyperplane. + * + * @param position the position to reflect, of the dimension of this hyperplane. + * @return the position at the same distance on the other side of this hyperplane. + */ + public Vector<?> reflect(final Vector<?> position) { + return position.copy(DataType.DOUBLE).combine(1, normal, -2 * offset(position) / normal.lengthSquare()); + } + + /** + * Returns the angle between the given direction and this hyperplane. + * + * @param direction the direction to measure, of the dimension of this hyperplane. + * @return the angle in radians, from 0 for a direction in this hyperplane to π/2 for a direction along the normal. + */ + public double angle(final Vector<?> direction) { + return Math.abs(Math.PI / 2 - direction.shortestAngle(normal)); + } + + /** + * Returns the dot product of the normal with the vector from the position of this hyperplane to the given position. + */ + private double offset(final Vector<?> position) { + return normal.dot(position.copy(DataType.DOUBLE).subtract(getPosition())); + } + /** * {@inheritDoc} * 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 32ef306ae5..907fba11af 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 @@ -22,6 +22,7 @@ import org.opengis.annotation.UML; 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.NDArrays; import org.apache.sis.maths.NDRange; import org.apache.sis.maths.SampleSystem; @@ -124,6 +125,61 @@ public final class Plane extends AbstractGeometry{ this.normal = normal; } + /** + * Returns the distance from this plane to the given position. + * + * <ul> + * <li>The sizes of a {@linkplain #isQuad() quad} are ignored: the plane is taken as infinite.</li> + * </ul> + * + * @param position the position to measure, of the dimension of this plane. + * @return the distance, in the units of the coordinate system axes. + */ + public double distance(final Vector<?> position) { + return Math.abs(offset(position)) / normal.length(); + } + + /** + * Returns the position of this plane nearest to the given position. + * + * <ul> + * <li>The sizes of a {@linkplain #isQuad() quad} are ignored: the plane is taken as infinite.</li> + * </ul> + * + * @param position the position to project, of the dimension of this plane. + * @return the orthogonal projection of the position on this plane. + */ + public Vector<?> project(final Vector<?> position) { + return position.copy(DataType.DOUBLE).combine(1, normal, -offset(position) / normal.lengthSquare()); + } + + /** + * Returns the mirror image of the given position through this plane. + * + * @param position the position to reflect, of the dimension of this plane. + * @return the position at the same distance on the other side of this plane. + */ + public Vector<?> reflect(final Vector<?> position) { + return position.copy(DataType.DOUBLE).combine(1, normal, -2 * offset(position) / normal.lengthSquare()); + } + + /** + * Returns the angle between the given direction and this plane. + * + * @param direction the direction to measure, of the dimension of this plane. + * @return the angle in radians, from 0 for a direction in this plane to π/2 for a direction along the normal. + */ + public double angle(final Vector<?> direction) { + return Math.abs(Math.PI / 2 - direction.shortestAngle(normal)); + } + + /** + * Returns the dot product of the normal with the vector from the position of this plane to the given position. + */ + private double offset(final Vector<?> position) { + return normal.dot(position.copy(DataType.DOUBLE).subtract(getPosition())); + } + /** * @return the plane X size */ diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/HyperPlaneTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/HyperPlaneTest.java new file mode 100644 index 0000000000..8ab077a8aa --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/HyperPlaneTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.geometries; + +import org.apache.sis.maths.Vector; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link HyperPlane} on the hyperplane <var>z</var> = 5, whose normal is not a unit vector. + * + * @author Johann Sorel (Geomatys) + */ +public class HyperPlaneTest extends GeometryTest { + + private static final HyperPlane PLANE = new HyperPlane(Geometries.getUndefinedCRS(3), Vector.of(0, 0, 5), Vector.of(0, 0, 2)); + + @Test + public void testSignedDistance() { + assertEquals( 3, PLANE.signedDistance(Vector.of(1, 2, 8)), TOLERANCE); + assertEquals(-4, PLANE.signedDistance(Vector.of(1, 2, 1)), TOLERANCE); + assertEquals( 0, PLANE.signedDistance(Vector.of(7, 9, 5)), TOLERANCE); + } + + @Test + public void testProject() { + assertArrayEquals(new double[] {1, 2, 5}, PLANE.project(Vector.of(1, 2, 8)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {1, 2, 5}, PLANE.project(Vector.of(1, 2, -3)).toArrayDouble(), TOLERANCE); + } + + @Test + public void testReflect() { + assertArrayEquals(new double[] {1, 2, 2}, PLANE.reflect(Vector.of(1, 2, 8)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {7, 9, 5}, PLANE.reflect(Vector.of(7, 9, 5)).toArrayDouble(), TOLERANCE); + } + + @Test + public void testAngle() { + assertEquals(0, PLANE.angle(Vector.of(1, 0, 0)), TOLERANCE); + assertEquals(Math.PI / 2, PLANE.angle(Vector.of(0, 0, -3)), TOLERANCE); + assertEquals(Math.PI / 4, PLANE.angle(Vector.of(1, 0, 1)), TOLERANCE); + } +} 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 new file mode 100644 index 0000000000..507aac2566 --- /dev/null +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/PlaneTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.geometries; + +import org.apache.sis.maths.Vector; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + + +/** + * Tests {@link Plane} on the plane <var>x</var> = 1, whose normal is not a unit vector. + * + * @author Johann Sorel (Geomatys) + */ +public class PlaneTest extends GeometryTest { + + private static final Plane PLANE = new Plane(Geometries.getUndefinedCRS(3), Vector.of(1, 0, 0), Vector.of(-3, 0, 0)); + + /** + * A plane has no side: the distance is the same on both sides. + */ + @Test + public void testDistance() { + assertEquals(2, PLANE.distance(Vector.of( 3, 5, 6)), TOLERANCE); + assertEquals(2, PLANE.distance(Vector.of(-1, 5, 6)), TOLERANCE); + } + + @Test + public void testProject() { + assertArrayEquals(new double[] {1, 5, 6}, PLANE.project(Vector.of(3, 5, 6)).toArrayDouble(), TOLERANCE); + } + + @Test + public void testReflect() { + assertArrayEquals(new double[] {-1, 5, 6}, PLANE.reflect(Vector.of(3, 5, 6)).toArrayDouble(), TOLERANCE); + } + + @Test + public void testAngle() { + assertEquals(0, PLANE.angle(Vector.of(0, 1, 1)), TOLERANCE); + assertEquals(Math.PI / 2, PLANE.angle(Vector.of(2, 0, 0)), TOLERANCE); + } +}
