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 754b4e16f973eeba6adf4107e8361d284644482c Author: jsorel <[email protected]> AuthorDate: Tue Dec 1 17:21:23 2026 +0100 feat(Geometry): add signed distance between solids --- .../main/org/apache/sis/geometries/Geometry.java | 23 +++++ .../apache/sis/geometries/operation/Distance.java | 106 +++++++++++++-------- .../geometries/operation/GeometryProcessor.java | 28 ++++++ .../sis/geometries/operation/DistanceTest.java | 50 +++++++++- 4 files changed, 160 insertions(+), 47 deletions(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java index b57ea6a1eb..06b2c1ac3e 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/Geometry.java @@ -733,6 +733,29 @@ public sealed interface Geometry return new GeometryProcessor().distance(this, other); } + /** + * Returns the signed distance between this geometry and the given geometry, as in collision detection: + * the distance when they are apart, zero or minus the depth of their overlap otherwise. + * The depth of the overlap is the length of the shortest translation separating the two geometries. + * + * <p>Constraints:</p> + * <ul> + * <li>A geometry enclosing a volume, such as a sphere, a capsule or a box, is seen as the solid it encloses, + * and a hyperplane as the half-space opposite to its normal.</li> + * <li>Equal to the {@linkplain #distance(Geometry) distance} when the geometries are apart.</li> + * <li>Infinite if either geometry is empty.</li> + * </ul> + * + * @param other the geometry to measure the signed distance to. + * @return signed distance between the two geometries. + * @throws OperationException if the signed distance cannot be computed. + * + * @see GeometryProcessor#signedDistance(org.apache.sis.geometries.Geometry, org.apache.sis.geometries.Geometry) + */ + default Quantity<?> signedDistance(Geometry other) throws OperationException { + return new GeometryProcessor().signedDistance(this, other); + } + /** * Returns the set-theoretic intersection of this geometry and the given geometry. * diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Distance.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Distance.java index 4db346d3f1..5782b2caa4 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Distance.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Distance.java @@ -44,70 +44,93 @@ public final class Distance { /** * Returns the signed distance between the solids enclosed by the two given geometries, as in collision detection: - * the gap between them when they are apart, zero or a negative value when they overlap. - * The solids intersect when the result is zero or negative. + * the gap between them when they are apart, zero or minus the depth of their overlap otherwise. + * The depth of the overlap is the length of the shortest translation separating the solids. * * <ul> - * <li>One of the geometries shall be a {@link Sphere} or a {@link Capsule}.</li> - * <li>The other geometry may be a sphere, a capsule, a {@link BBox}, a {@link HyperPlane} or a {@link Plane}. - * The planes are taken as infinite sheets without side.</li> - * <li>The negative value is the depth of the overlap for two spheres or a sphere and a plane, - * and an approximation of it otherwise.</li> + * <li>The geometries are seen as solids as in {@link #nearestSolids(Geometry, Geometry)}: + * a {@link HyperPlane} is a half-space and a {@link Plane} is a sheet without side.</li> + * <li>The depth may be underestimated when a {@link Capsule} overlaps a box, or when a capsule of different + * end radii overlaps another capsule: it is then the depth of the capsule ball reaching the deepest.</li> * </ul> * * @param first the first geometry. * @param second the second geometry, in the reference system of the first one. * @return the signed distance, in the units of the coordinate system axes. * @throws OperationException if the geometries are not in the same reference system. - * @throws UnsupportedOperationException if the pair of geometry types is not supported. + * @throws UnsupportedOperationException if a geometry type, or a pair of planes, is not supported. */ public static double signedDistance(final Geometry first, final Geometry second) { ProcessorUtils.ensureSameCRS(first, second); - if (first instanceof Sphere sphere) { - return signedDistance(second, sphere.getCenter()) - sphere.getRadius(); + final Nearest a = Nearest.of(first); + final Nearest b = Nearest.of(second); + if (b instanceof Nearest.HalfSpace || b instanceof Nearest.Sheet) { + return toPlane(a, b); } - if (second instanceof Sphere sphere) { - return signedDistance(first, sphere.getCenter()) - sphere.getRadius(); + if (a instanceof Nearest.HalfSpace || a instanceof Nearest.Sheet) { + return toPlane(b, a); } - if (first instanceof Capsule capsule) { - return alongAxis(capsule, (position) -> signedDistance(second, position)); + if (a instanceof Nearest.Balls balls) { + return alongAxis(balls, b); } - if (second instanceof Capsule capsule) { - return alongAxis(capsule, (position) -> signedDistance(first, position)); + if (b instanceof Nearest.Balls balls) { + return alongAxis(balls, a); } - throw new UnsupportedOperationException("The signed distance between a " + first.getGeometryType() - + " and a " + second.getGeometryType() + " is not supported."); + final Nearest.Box box = (Nearest.Box) a; + final Nearest.Box other = (Nearest.Box) b; + final double separation = box.separation(other); + return (separation > 0) ? nearest(box, other).orElseThrow().distance() : separation; } /** - * Returns the signed distance from the solid enclosed by the given geometry to the given position. + * Returns the signed distance between balls interpolated along an axis and another solid, + * measured on the ball reaching the deepest into the other solid. */ - private static double signedDistance(final Geometry geometry, final Vector<?> position) { - return switch (geometry) { - case Sphere cdt -> cdt.signedDistance(position); - case Capsule cdt -> cdt.signedDistance(position); - case BBox cdt -> cdt.signedDistance(position); - case HyperPlane cdt -> Math.abs(cdt.signedDistance(position)); - case Plane cdt -> cdt.distance(position); - default -> throw new UnsupportedOperationException( - "The signed distance to a " + geometry.getGeometryType() + " is not supported."); - }; + private static double alongAxis(final Nearest.Balls balls, final Nearest other) { + final double t = balls.deepest(other::signedDistance); + return other.signedDistance(balls.center(t)) - balls.radius(t); } /** - * Returns the smallest signed distance between the solid enclosed by a capsule and a convex solid. - * The capsule is the union of the balls whose centers and radii are interpolated along its axis, - * so the signed distance to the balls is a convex function of the position along the axis, - * whose minimum is found by a golden-section search. + * Returns the signed distance between a solid and a half-space or a sheet, + * from the extent of the solid along the normal of the plane. + * A solid crossing a sheet is moved to the side where most of it is. * - * @param capsule the capsule. - * @param signedDistance signed distance from the convex solid to a position. - * @return the smallest signed distance between the capsule and the convex solid. + * @param solid balls or a box. + * @param plane a half-space or a sheet. */ - private static double alongAxis(final Capsule capsule, final ToDoubleFunction<Vector<?>> signedDistance) { - final Nearest.Balls balls = (Nearest.Balls) Nearest.of(capsule); - final double t = balls.deepest(signedDistance); - return signedDistance.applyAsDouble(balls.center(t)) - balls.radius(t); + private static double toPlane(final Nearest solid, final Nearest plane) { + final ToDoubleFunction<Vector<?>> offset = switch (plane) { + case Nearest.HalfSpace cdt -> cdt::signedDistance; + case Nearest.Sheet cdt -> cdt::offset; + default -> throw new AssertionError(plane); + }; + double lowest = Double.POSITIVE_INFINITY; + double highest = Double.NEGATIVE_INFINITY; + switch (solid) { + case Nearest.Balls cdt -> { + // The extremes along the normal are on the balls at the two ends. + final double start = offset.applyAsDouble(cdt.start()); + final double end = offset.applyAsDouble(cdt.end()); + lowest = Math.min(start - cdt.startRadius(), end - cdt.endRadius()); + highest = Math.max(start + cdt.startRadius(), end + cdt.endRadius()); + } + case Nearest.Box cdt -> { + for (final Vector<?> corner : cdt.corners()) { + final double distance = offset.applyAsDouble(corner); + lowest = Math.min(lowest, distance); + highest = Math.max(highest, distance); + } + } + default -> throw new UnsupportedOperationException("The signed distance between two planes is not supported."); + } + if (plane instanceof Nearest.HalfSpace || lowest >= 0) { + return lowest; + } + if (highest <= 0) { + return -highest; + } + return -Math.min(highest, -lowest); } /** @@ -154,8 +177,7 @@ public final class Distance { * Otherwise the nearest points are on the ball nearest to the other solid. */ private static Optional<NearestPoints> nearest(final Nearest.Balls balls, final Nearest other) { - final double deepest = balls.deepest(other::signedDistance); - if (other.signedDistance(balls.center(deepest)) < balls.radius(deepest)) { + if (alongAxis(balls, other) < 0) { return Optional.empty(); } final double t = balls.nearestRatio(other); 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 83eb5b34e6..b97d623ff2 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 @@ -313,6 +313,34 @@ public final class GeometryProcessor { return Quantities.create(jts(geom1).distance(jts(geom2)), Geometries.getLinearUnit(geom1)); } + /** + * Returns the signed distance between two geometries, as in collision detection: + * the distance between them when they are apart, zero or minus the depth of their overlap otherwise. + * The depth of the overlap is the length of the shortest translation separating the two geometries. + * + * <ul> + * <li>A geometry enclosing a volume, such as a sphere, a capsule or a box, is seen as the solid it encloses, + * and a hyperplane as the half-space opposite to its normal.</li> + * <li>Equal to the {@linkplain #distance(Geometry, Geometry) distance} when the geometries are apart.</li> + * <li>Infinite if either geometry is empty.</li> + * <li>The quantity is expressed in the unit of the axes of the coordinate reference system, + * with the same limitation as {@link #distance(Geometry, Geometry)}.</li> + * </ul> + * + * @param geom1 the first geometry. + * @param geom2 the second geometry, in the reference system of the first one. + * @return the signed distance between the two geometries. + * @throws OperationException if the signed distance cannot be computed. + * @throws UnsupportedOperationException if the computation is not yet implemented for the types of the geometries. + */ + public Quantity<?> signedDistance(Geometry geom1, Geometry geom2) throws OperationException { + if (geom1.isEmpty() || geom2.isEmpty()) { + // Same as distance(…): an empty geometry is infinitely far from any geometry. + return Quantities.create(Double.POSITIVE_INFINITY, Units.UNITY); + } + return Quantities.create(Distance.signedDistance(geom1, geom2), Geometries.getLinearUnit(geom1)); + } + /** * Returns a geometric object that represents the Point set intersection of this geometric object with anotherGeometry. */ diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java index 1374143b48..10d67140b5 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/DistanceTest.java @@ -156,7 +156,7 @@ public class DistanceTest extends AbstractD9IMTest { assertEquals( 1.5, Distance.signedDistance(sphere(3, 0, 1, 0.5), capsule(1, 1, 0,0,0, 0,0,2)), TOLERANCE); assertEquals( 0.6622776601683795, Distance.signedDistance(capsule(2, 1, 0,0,0, 0,0,2), sphere(3, 0, 1, 0.5)), TOLERANCE); assertEquals( 2, Distance.signedDistance(sphere(5, 1, 1, 1), BOX_3D), TOLERANCE); - assertEquals( 3, Distance.signedDistance(sphere(1, 2, 0, 2), PLANE_Z5), TOLERANCE); + assertEquals(-7, Distance.signedDistance(sphere(1, 2, 0, 2), PLANE_Z5), TOLERANCE); // Inside the half-space. assertEquals(-1, Distance.signedDistance(PLANE_Z5, sphere(1, 2, 6, 2)), TOLERANCE); } @@ -169,8 +169,47 @@ public class DistanceTest extends AbstractD9IMTest { final Capsule other = capsule(0.5, 0.5, 4,-1,1, 4,1,1); assertEquals(1.6231056256176606, Distance.signedDistance(cone, other), TOLERANCE); // √17 − 2.5 assertEquals(1.6231056256176606, Distance.signedDistance(other, cone), TOLERANCE); - assertEquals(1, Distance.signedDistance(cone, new BBox(CRS_3D, 3,-1,-1, 5,1,1)), TOLERANCE); - assertEquals(2, Distance.signedDistance(cone, PLANE_Z5), TOLERANCE); + assertEquals( 1, Distance.signedDistance(cone, new BBox(CRS_3D, 3,-1,-1, 5,1,1)), TOLERANCE); + assertEquals(-7, Distance.signedDistance(cone, PLANE_Z5), TOLERANCE); // Lowest point at z = −2. + } + + /** + * Tests {@link Distance#signedDistance(Geometry, Geometry)} with boxes, which overlap by the depth + * of the shortest translation separating them. + */ + @Test + public void testSignedDistanceOfBoxes() { + assertEquals(-1, Distance.signedDistance(BOX_3D, new BBox(CRS_3D, 1,1,1, 3,3,3)), TOLERANCE); + assertEquals( 1.7320508075688772, Distance.signedDistance(BOX_3D, new BBox(CRS_3D, 3,3,3, 4,4,4)), TOLERANCE); + assertEquals(-1.5, Distance.signedDistance(new BBox(CRS_3D, -5,-1,-1, 5,1,1), new BBox(CRS_3D, -1,-5,-0.5, 1,5,0.5)), + TOLERANCE, "Crossing bars, separated along the third axis."); + assertEquals(-5, Distance.signedDistance(BOX_3D, PLANE_Z5), TOLERANCE, "Box inside the half-space."); + assertEquals(-0.5, Distance.signedDistance(BOX_3D, new Plane(CRS_3D, Vector.of(0.0, 0.0, 0.5), Vector.of(0.0, 0.0, 1.0))), + TOLERANCE, "Box crossing a sheet, pushed to the side where it is the most."); + } + + /** + * Tests {@link Distance#signedDistance(Geometry, Geometry)} with a capsule crossing planes. + * The capsule overlaps the sheet by the depth needed to move it entirely on one side, + * which is more than the depth of the ball reaching the deepest. + */ + @Test + public void testSignedDistanceToPlanes() { + final Plane sheet = new Plane(CRS_3D, Vector.of(0.0, 0.0, 1.0), Vector.of(0.0, 0.0, 1.0)); + assertEquals(-1.5, Distance.signedDistance(capsule(0.5, 0.5, 0,0,0, 0,0,4), sheet), TOLERANCE); + assertEquals(-1.5, Distance.signedDistance(capsule(0.5, 0.5, 0,0,4, 0,0,8), PLANE_Z5), TOLERANCE); + assertEquals( 2.5, Distance.signedDistance(sheet, capsule(0.5, 0.5, 0,0,4, 0,0,8)), TOLERANCE); + } + + /** + * Tests {@code signedDistance(Geometry, Geometry)} of the geometry processor. + */ + @Test + public void testProcessorSignedDistance() { + final GeometryProcessor processor = new GeometryProcessor(); + assertEquals(-1, processor.signedDistance(BOX_3D, new BBox(CRS_3D, 1,1,1, 3,3,3)).getValue().doubleValue(), TOLERANCE); + assertEquals( 2, processor.signedDistance(sphere(5, 1, 1, 1), BOX_3D).getValue().doubleValue(), TOLERANCE); + assertEquals(-1, BOX_3D.signedDistance(new BBox(CRS_3D, 1,1,1, 3,3,3)).getValue().doubleValue(), TOLERANCE, "Geometry method."); } /** @@ -264,11 +303,12 @@ public class DistanceTest extends AbstractD9IMTest { } /** - * The signed distance needs a sphere or a capsule, in the same reference system as the other geometry. + * The signed distance needs solids in the same reference system, not both planes. */ @Test public void testSignedDistanceUnsupported() { - assertThrows(UnsupportedOperationException.class, () -> Distance.signedDistance(BOX_3D, PLANE_Z5)); + final Plane sheet = new Plane(CRS_3D, Vector.of(0.0, 0.0, 1.0), Vector.of(0.0, 0.0, 1.0)); + assertThrows(UnsupportedOperationException.class, () -> Distance.signedDistance(sheet, PLANE_Z5)); assertThrows(OperationException.class, () -> Distance.signedDistance(sphere(0, 0, 0, 1), SQUARE)); }
