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 d619815f1d4dc5b1d6e288df7862b7571d4007d7
Author: jsorel <[email protected]>
AuthorDate: Wed Dec 2 09:23:11 2026 +0100

    perf(Geometry): faster distances and contacts of capsules
---
 .../apache/sis/geometries/operation/Collision.java | 27 +++----
 .../apache/sis/geometries/operation/Distance.java  |  2 +-
 .../apache/sis/geometries/operation/Nearest.java   | 85 ++++++++++++++++++++--
 3 files changed, 87 insertions(+), 27 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java
index 8e331a738e..77c8bf3633 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java
@@ -224,7 +224,7 @@ public final class Collision {
      * @param  balls  the second solid.
      */
     private static Optional<Contact> withBalls(final Nearest solid, final 
Nearest.Balls balls) {
-        final double deepest = balls.deepest(solid::signedDistance);
+        final double deepest = balls.deepest(solid);
         final double distance = solid.signedDistance(balls.center(deepest)) - 
balls.radius(deepest);
         if (distance > 0) {
             return Optional.empty();
@@ -253,7 +253,7 @@ public final class Collision {
         }
         final double size = Math.max(balls.startRadius(), balls.endRadius()) + 
balls.start().distance(balls.end());
         final double threshold = distance + FLATNESS * size;
-        final DoubleUnaryOperator function = (t) -> 
box.signedDistance(balls.center(t)) - balls.radius(t);
+        final DoubleUnaryOperator function = balls.distanceAlong(box);
         return (boundary(function, deepest, 0, threshold) + boundary(function, 
deepest, 1, threshold)) / 2;
     }
 
@@ -284,9 +284,9 @@ public final class Collision {
     }
 
     /**
-     * Returns the ratio on the axis of balls of their contact with other 
balls: the ball nearest to a sphere,
-     * the middle of the part facing a parallel capsule, or the point nearest 
to the axis of a capsule.
-     * Capsules of different end radii use the ball reaching the deepest.
+     * Returns the ratio on the axis of balls of their contact with other 
balls: the middle of the part
+     * facing a parallel capsule, otherwise the ball reaching the deepest, 
which is the ball nearest
+     * to a sphere or to the axis of a capsule.
      *
      * @param  other    the other balls.
      * @param  balls    the balls on which to measure the contact.
@@ -294,13 +294,9 @@ public final class Collision {
      * @return ratio of the ball of the contact.
      */
     private static double facing(final Nearest.Balls other, final 
Nearest.Balls balls, final double deepest) {
-        if (balls.isBall()) {
-            return 0;
-        }
-        if (other.isBall()) {
-            return balls.nearestRatio(other.start());
-        }
-        if (balls.startRadius() != balls.endRadius() || other.startRadius() != 
other.endRadius()) {
+        if (balls.isBall() || other.isBall() || balls.startRadius() != 
balls.endRadius()
+                || other.startRadius() != other.endRadius())
+        {
             return deepest;
         }
         final Vector<?> axis = 
balls.end().copy(DataType.DOUBLE).subtract(balls.start());
@@ -320,12 +316,7 @@ public final class Collision {
                 }
             }
         }
-        final int dimension = axis.getDimension();
-        final double[] ratios = new double[2];
-        Distance.distanceSquare(balls.start().toArrayDouble(), 
balls.end().toArrayDouble(), new double[dimension],
-                                other.start().toArrayDouble(), 
other.end().toArrayDouble(), new double[dimension],
-                                ratios, EPSILON);
-        return ratios[0];
+        return deepest;
     }
 
     /**
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 5782b2caa4..6be6765bd7 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
@@ -87,7 +87,7 @@ public final class Distance {
      * measured on the ball reaching the deepest into the other solid.
      */
     private static double alongAxis(final Nearest.Balls balls, final Nearest 
other) {
-        final double t = balls.deepest(other::signedDistance);
+        final double t = balls.deepest(other);
         return other.signedDistance(balls.center(t)) - balls.radius(t);
     }
 
diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java
index e12960ec70..983e518ee1 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java
@@ -19,7 +19,6 @@ package org.apache.sis.geometries.operation;
 import java.util.ArrayList;
 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;
@@ -192,14 +191,19 @@ sealed interface Nearest {
 
         /**
          * Returns the ratio between the two ends of the ball nearest to 
another solid apart from these balls.
-         * The distance to the other solid is a convex function of the ratio. 
Its derivative is computed from
-         * the nearest positions of the other solid, and its root is found by 
bisection.
-         * This is more accurate than {@link #deepest(ToDoubleFunction)}, 
which compares distances that become equal.
+         * The closed form of {@link #deepest(Nearest)} is used when there is 
one. Otherwise the distance to
+         * the other solid is a convex function of the ratio. Its derivative 
is computed from the nearest
+         * positions of the other solid, and its root is found by bisection. 
This is more accurate than
+         * a golden-section search, which compares distances that become equal.
          *
          * @param  other  a solid which does not overlap these balls.
          * @return ratio from 0 at the start to 1 at the end.
          */
         double nearestRatio(final Nearest other) {
+            final double exact = exactDeepest(other);
+            if (!Double.isNaN(exact)) {
+                return exact;
+            }
             final Vector<?> axis = end.copy(DataType.DOUBLE).subtract(start);
             if (axis.lengthSquare() == 0) {
                 return 0;
@@ -228,16 +232,81 @@ sealed interface Nearest {
         /**
          * Returns the ratio between the two ends of the ball reaching the 
deepest into another solid,
          * which is the ball whose signed distance to that solid is the 
smallest.
-         * That signed distance is a convex function of the ratio, whose 
minimum is found by a golden-section search.
          *
-         * @param  signedDistance  signed distance from the other solid to a 
position.
+         * <ul>
+         *   <li>The ratio is computed in closed form for a single ball, 
against a single ball,
+         *       and between two capsules of uniform radius.</li>
+         *   <li>Otherwise the signed distance is a convex function of the 
ratio,
+         *       whose minimum is found by a golden-section search.</li>
+         * </ul>
+         *
+         * @param  other  the other solid.
          * @return ratio from 0 at the start to 1 at the end.
          */
-        double deepest(final ToDoubleFunction<Vector<?>> signedDistance) {
+        double deepest(final Nearest other) {
+            final double exact = exactDeepest(other);
+            return Double.isNaN(exact) ? deepest(distanceAlong(other)) : exact;
+        }
+
+        /**
+         * Returns the ratio of the ball reaching the deepest into another 
solid in closed form, or NaN if there is none.
+         * It is zero for a single ball, the ball nearest to the center of 
another single ball, or the nearest points
+         * of the axes of two capsules of uniform radius.
+         */
+        private double exactDeepest(final Nearest other) {
             if (isBall()) {
                 return 0;
             }
-            final DoubleUnaryOperator distance = (t) -> 
signedDistance.applyAsDouble(center(t)) - radius(t);
+            if (other instanceof Balls balls) {
+                if (balls.isBall()) {
+                    return nearestRatio(balls.start);
+                }
+                if (startRadius == endRadius && balls.startRadius == 
balls.endRadius) {
+                    final int dimension = start.getDimension();
+                    final double[] ratios = new double[2];
+                    Distance.distanceSquare(start.toArrayDouble(), 
end.toArrayDouble(), new double[dimension],
+                            balls.start.toArrayDouble(), 
balls.end.toArrayDouble(), new double[dimension], ratios, 0);
+                    return ratios[0];
+                }
+            }
+            return Double.NaN;
+        }
+
+        /**
+         * Returns the signed distance between another solid and the ball at a 
ratio between the two ends.
+         * For a box, the two ends are converted once to the coordinates of 
the box,
+         * then the distance is computed without creating vectors.
+         *
+         * @param  other  the other solid.
+         * @return signed distance as a function of the ratio from 0 at the 
start to 1 at the end.
+         */
+        DoubleUnaryOperator distanceAlong(final Nearest other) {
+            if (other instanceof Box box) {
+                final double[] from = box.local(start);
+                final double[] to   = box.local(end);
+                final double[] halfSizes = box.halfSizes();
+                return (t) -> {
+                    double outside = 0;
+                    double inside  = Double.NEGATIVE_INFINITY;
+                    for (int k = 0; k < from.length; k++) {
+                        final double q = Math.abs(from[k] + (to[k] - from[k]) 
* t) - halfSizes[k];
+                        outside += Math.max(q, 0) * Math.max(q, 0);
+                        inside   = Math.max(inside, q);
+                    }
+                    return Math.sqrt(outside) + Math.min(inside, 0) - 
radius(t);
+                };
+            }
+            return (t) -> other.signedDistance(center(t)) - radius(t);
+        }
+
+        /**
+         * Returns the ratio where a convex function of the ratio between the 
two ends is the smallest,
+         * found by a golden-section search.
+         *
+         * @param  distance  the convex function.
+         * @return ratio from 0 at the start to 1 at the end.
+         */
+        private static double deepest(final DoubleUnaryOperator distance) {
             final double ratio = (Math.sqrt(5) - 1) / 2;
             double lower = 0, upper = 1;
             double t1 = upper - ratio * (upper - lower), d1 = 
distance.applyAsDouble(t1);

Reply via email to