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 9859a99d09a0fde83330349201de0c331ec54720 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:39:01 2026 +0100 feat(Geometry): add signed distances for collision detection --- .../main/org/apache/sis/geometries/BBox.java | 19 ++++ .../apache/sis/geometries/operation/Distance.java | 105 ++++++++++++++++++++- .../org/apache/sis/geometries/surface/Capsule.java | 42 +++++++++ .../org/apache/sis/geometries/surface/Sphere.java | 11 +++ .../test/org/apache/sis/geometries/BBoxTest.java | 12 +++ .../sis/geometries/operation/DistanceTest.java | 50 ++++++++++ .../apache/sis/geometries/surface/CapsuleTest.java | 16 ++++ .../apache/sis/geometries/surface/SphereTest.java | 9 ++ 8 files changed, 263 insertions(+), 1 deletion(-) diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java index d145faff17..ee50efe01d 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/BBox.java @@ -333,6 +333,25 @@ public final class BBox extends GeneralEnvelope implements Geometry { return Quantities.create(length(0) * length(1) * length(2), Geometries.getVolumeUnit(this)); } + /** + * Returns the signed distance from this box to the given position: + * positive outside the box, negative inside. + * + * @param position the position to measure, of the dimension of this box. + * @return the signed distance, in the units of the coordinate system axes. + */ + public double signedDistance(final Vector<?> position) { + double outside = 0; + double inside = Double.NEGATIVE_INFINITY; + for (int i = 0; i < getDimension(); i++) { + // Distance to the nearest face on this axis, negative if the position is between the faces. + final double q = Math.abs(position.get(i) - getMedian(i)) - getSpan(i) / 2; + outside += Math.max(q, 0) * Math.max(q, 0); + inside = Math.max(inside, q); + } + return Math.sqrt(outside) + Math.min(inside, 0); + } + /** * Returns the span of this box on the given axis, or zero if that span is not positive. */ 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 a7d4abc298..1b5df0a0c0 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 @@ -17,21 +17,124 @@ package org.apache.sis.geometries.operation; import java.util.List; +import java.util.function.DoubleUnaryOperator; +import java.util.function.ToDoubleFunction; +import org.apache.sis.geometries.BBox; +import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.HyperPlane; +import org.apache.sis.geometries.Plane; import org.apache.sis.geometries.Point; +import org.apache.sis.geometries.surface.Capsule; +import org.apache.sis.geometries.surface.Sphere; +import org.apache.sis.maths.DataType; import org.apache.sis.maths.Maths; import org.apache.sis.maths.Vectors; import org.apache.sis.maths.Vector; /** - * Distance 2D processors. + * Distance computations between positions and geometries. * * @author Johann Sorel (Geomatys) */ public final class Distance { + /** + * Number of iterations of the golden-section search along the axis of a capsule, + * enough to reach the precision of double values. + */ + private static final int GOLDEN_SECTION_ITERATIONS = 80; + private 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. + * + * <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> + * </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. + */ + 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(); + } + if (second instanceof Sphere sphere) { + return signedDistance(first, sphere.getCenter()) - sphere.getRadius(); + } + if (first instanceof Capsule capsule) { + return alongAxis(capsule, (position) -> signedDistance(second, position)); + } + if (second instanceof Capsule capsule) { + return alongAxis(capsule, (position) -> signedDistance(first, position)); + } + throw new UnsupportedOperationException("The signed distance between a " + first.getGeometryType() + + " and a " + second.getGeometryType() + " is not supported."); + } + + /** + * Returns the signed distance from the solid enclosed by the given geometry to the given position. + */ + 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."); + }; + } + + /** + * 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. + * + * @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. + */ + private static double alongAxis(final Capsule capsule, final ToDoubleFunction<Vector<?>> signedDistance) { + final Vector<?> bottom = capsule.getDataPoints().getPosition(0).copy(DataType.DOUBLE); + final Vector<?> top = capsule.getDataPoints().getPosition(1); + final double r1 = capsule.getRadiusBottom(); + final double r2 = capsule.getRadiusTop(); + final DoubleUnaryOperator distance = (t) -> signedDistance.applyAsDouble(bottom.lerp(top, t)) - (r1 + (r2 - r1) * t); + final double ratio = (Math.sqrt(5) - 1) / 2; + double lower = 0, upper = 1; + double t1 = upper - ratio * (upper - lower), d1 = distance.applyAsDouble(t1); + double t2 = lower + ratio * (upper - lower), d2 = distance.applyAsDouble(t2); + for (int i = 0; i < GOLDEN_SECTION_ITERATIONS; i++) { + if (d1 <= d2) { + upper = t2; + t2 = t1; d2 = d1; + t1 = upper - ratio * (upper - lower); + d1 = distance.applyAsDouble(t1); + } else { + lower = t1; + t1 = t2; d1 = d2; + t2 = lower + ratio * (upper - lower); + d2 = distance.applyAsDouble(t2); + } + } + return Math.min(Math.min(d1, d2), Math.min(distance.applyAsDouble(0), distance.applyAsDouble(1))); + } + /** * Calculate distance between two points. * diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java index fb66c8cc67..9d461f343a 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Capsule.java @@ -186,6 +186,48 @@ public sealed interface Capsule extends Surface return Quantities.create(Math.PI * volume / 3, Geometries.getVolumeUnit(this)); } + /** + * Returns the signed distance from the solid enclosed by this capsule to the given position: + * positive outside, negative inside. + * + * <ul> + * <li>Exact for different radii, the surface being made of two spherical caps and a band of cone.</li> + * <li>A capsule whose larger sphere contains the smaller one is that larger sphere.</li> + * </ul> + * + * @param position the position to measure, of the dimension of this capsule. + * @return the signed distance, in the units of the coordinate system axes. + * + * @see <a href="https://iquilezles.org/articles/distfunctions/">Round cone distance function, by Inigo Quilez</a> + */ + default double signedDistance(final Vector<?> position) { + final double r1 = getRadiusBottom(); + final double r2 = getRadiusTop(); + final Vector<?> bottom = getDataPoints().getPosition(0).copy(DataType.DOUBLE); + final Vector<?> axis = getDataPoints().getPosition(1).copy(DataType.DOUBLE).subtract(bottom); + final Vector<?> offset = position.copy(DataType.DOUBLE).subtract(bottom); + final double l2 = axis.lengthSquare(); + final double rr = r1 - r2; + final double a2 = l2 - rr * rr; + if (!(a2 > 0)) { + // One sphere contains the other, the capsule is the larger sphere. + return (r1 >= r2) ? offset.length() - r1 : offset.subtract(axis).length() - r2; + } + final double y = offset.dot(axis); + final double z = y - l2; + final double x2 = offset.combine(l2, axis, -y).lengthSquare(); + final double y2 = y * y * l2; + final double z2 = z * z * l2; + final double k = Math.signum(rr) * rr * rr * x2; + if (Math.signum(z) * a2 * z2 > k) { + return Math.sqrt(x2 + z2) / l2 - r2; // Nearest to the top cap. + } + if (Math.signum(y) * a2 * y2 < k) { + return Math.sqrt(x2 + y2) / l2 - r1; // Nearest to the bottom cap. + } + return (Math.sqrt(x2 * a2 / l2) + y * rr) / l2 - r1; // Nearest to the band. + } + /** * Returns the centroid of this surface, which is on the axis of this capsule. * The two data points are interpolated, so the centroid gets their attributes. diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java index 14541e4796..e1f439cfa1 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Sphere.java @@ -160,6 +160,17 @@ public sealed interface Sphere extends ParametricCurveSurface return Quantities.create(Maths.unitBallVolume(n) * Math.pow(getRadius(), n), Geometries.getLinearUnit(this).pow(n)); } + /** + * Returns the signed distance from the ball bounded by this sphere to the given position: + * positive outside the ball, negative inside. + * + * @param position the position to measure, of the dimension of this sphere. + * @return the signed distance, in the units of the coordinate system axes. + */ + default double signedDistance(final Vector<?> position) { + return position.distance(getCenter()) - getRadius(); + } + /** * Returns the {@linkplain #getCenter() center} of this sphere, with the attributes of its data point. * diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java index 0c952d68e9..2f6381fc0f 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/BBoxTest.java @@ -87,4 +87,16 @@ public class BBoxTest extends GeometryTest { assertEquals(0, new BBox(CRS_3D, 0,0,0, 2,3,0).getVolume().getValue().doubleValue()); assertThrows(UnsupportedOperationException.class, () -> new BBox(CRS_2D, 0,0, 4,5).getVolume()); } + + /** + * The signed distance is positive outside the box and negative inside. + */ + @Test + public void testSignedDistance() { + final BBox box = new BBox(CRS_3D, 0,0,0, 2,2,2); + assertEquals( 3, box.signedDistance(Vector.of(5, 1, 1)), TOLERANCE); + assertEquals( 2.8284271247461903, box.signedDistance(Vector.of(4, 4, 1)), TOLERANCE); // √8 + assertEquals(-1, box.signedDistance(Vector.of(1, 1, 1)), TOLERANCE); + assertEquals(-0.5, box.signedDistance(Vector.of(1, 1, 1.5)), TOLERANCE); + } } 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 3f4fe24195..bc136ae602 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 @@ -20,7 +20,10 @@ import java.util.List; import javax.measure.Quantity; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.geometries.BBox; import org.apache.sis.geometries.Point; +import org.apache.sis.geometries.surface.Capsule; +import org.apache.sis.geometries.surface.Sphere; import org.apache.sis.maths.SampleSystem; import org.apache.sis.maths.Vector; import org.apache.sis.maths.DataType; @@ -193,4 +196,51 @@ public class DistanceTest extends AbstractD9IMTest { assertEquals(new Distance.SegmentPoint(1, 0.5), Distance.nearestSegment(List.of(vector(0,0), vector(0,0), vector(2,0)), vector(1, 1))); assertThrows(IllegalArgumentException.class, () -> Distance.nearestSegment(List.of(vector(0,0)), vector(1, 1))); } + + private static Sphere sphere(final double x, final double y, final double z, final double radius) { + return GeometryFactory.DEFAULT.createSphere(GeometryFactory.DEFAULT.createDataPoints(CRS_3D, x, y, z), radius); + } + + private static Capsule capsule(final double r1, final double r2, final double... axis) { + final Capsule capsule = GeometryFactory.DEFAULT.createCapsule(GeometryFactory.DEFAULT.createDataPoints(CRS_3D, axis)); + capsule.setRadiusBottom(r1); + capsule.setRadiusTop(r2); + return capsule; + } + + /** + * Tests {@link Distance#signedDistance(Geometry, Geometry)} with spheres. + */ + @Test + public void testSignedDistanceOfSpheres() { + assertEquals( 2, Distance.signedDistance(sphere(0, 0, 0, 1), sphere(5, 0, 0, 2)), TOLERANCE); + assertEquals(-3, Distance.signedDistance(sphere(0, 0, 0, 3), sphere(1, 0, 0, 1)), TOLERANCE); + 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(-1, Distance.signedDistance(PLANE_Z5, sphere(1, 2, 6, 2)), TOLERANCE); + } + + /** + * Tests {@link Distance#signedDistance(Geometry, Geometry)} with capsules of different radii. + */ + @Test + public void testSignedDistanceOfCapsules() { + final Capsule cone = capsule(2, 1, 0,0,0, 0,0,2); + 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); + } + + /** + * The signed distance needs a sphere or a capsule, in the same reference system as the other geometry. + */ + @Test + public void testSignedDistanceUnsupported() { + assertThrows(UnsupportedOperationException.class, () -> Distance.signedDistance(BOX_3D, PLANE_Z5)); + assertThrows(OperationException.class, () -> Distance.signedDistance(sphere(0, 0, 0, 1), SQUARE)); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java index 7934bca7ec..f19ec4f419 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/CapsuleTest.java @@ -179,4 +179,20 @@ public class CapsuleTest extends GeometryTest { assertThrows(UnsupportedOperationException.class, capsule::getCentroid); assertThrows(UnsupportedOperationException.class, capsule::getEnclosedVolume); } + + /** + * The signed distance to a capsule of equal radii is the distance to its axis minus the radius. + * With different radii, the nearest part may be a cap or the band of cone. + */ + @Test + public void testSignedDistance() { + assertEquals(2, capsule(1, 1, 0,0,0, 0,0,2).signedDistance(Vector.of(3, 0, 1)), TOLERANCE); + final Capsule cone = capsule(2, 1, 0,0,0, 0,0,2); + assertEquals(1.1622776601683795, cone.signedDistance(Vector.of(3, 0, 1)), TOLERANCE); // √10 − 2 + assertEquals(3.220153254455, cone.signedDistance(Vector.of(5, 0, 1.5)), TOLERANCE); + assertEquals( 3, cone.signedDistance(Vector.of(0, 0, -5)), TOLERANCE); + assertEquals( 7, cone.signedDistance(Vector.of(0, 0, 10)), TOLERANCE); + assertEquals(-2, cone.signedDistance(Vector.of(0, 0, 0)), TOLERANCE); + assertEquals( 1, capsule(1, 3, 0,0,0, 0,0,1).signedDistance(Vector.of(0, 0, 5)), TOLERANCE); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java index 3e61c41c00..c4a334dd09 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/SphereTest.java @@ -164,4 +164,13 @@ public class SphereTest extends GeometryTest { sphere.setCenter(Vector.of(0, 0, 0)); assertPosition(1, 2, 3, centroid.getPosition()); } + + /** + * The signed distance is positive outside the ball and negative inside. + */ + @Test + public void testSignedDistance() { + assertEquals( 3, sphere().signedDistance(Vector.of(1, 2, 8)), TOLERANCE); + assertEquals(-2, sphere().signedDistance(Vector.of(1, 2, 3)), TOLERANCE); + } }
