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 5a98c42c3608dcd718ce993313336cc5b6bf9dde
Author: jsorel <[email protected]>
AuthorDate: Fri Nov 27 09:35:05 2026 +0100

    feat(Geometry): add ray intersection with boxes
---
 .../sis/geometries/operation/Intersection.java     | 105 ++++++++++++++++++++-
 .../sis/geometries/operation/AbstractD9IMTest.java |   6 ++
 .../sis/geometries/operation/IntersectionTest.java |  50 ++++++++++
 .../sis/geometries/operation/IntersectsTest.java   |   2 +
 4 files changed, 161 insertions(+), 2 deletions(-)

diff --git 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
index 302c708547..cc2e89fa4a 100644
--- 
a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
+++ 
b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Intersection.java
@@ -22,16 +22,20 @@ import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
+import java.util.function.BiFunction;
 import java.util.function.DoubleFunction;
 import java.util.stream.Stream;
+import org.opengis.geometry.Envelope;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.operation.TransformException;
+import org.apache.sis.geometries.BBox;
 import org.apache.sis.geometries.DataPoints;
 import org.apache.sis.geometries.Geometries;
 import org.apache.sis.geometries.Geometry;
 import org.apache.sis.geometries.GeometryFactory;
 import org.apache.sis.geometries.GeometryType;
 import org.apache.sis.geometries.HyperPlane;
+import org.apache.sis.geometries.OBBox;
 import org.apache.sis.geometries.Plane;
 import org.apache.sis.geometries.Point;
 import org.apache.sis.geometries.DataPointsType;
@@ -53,6 +57,7 @@ import org.apache.sis.maths.DataType;
 import org.apache.sis.maths.Maths;
 import org.apache.sis.maths.Matrices;
 import org.apache.sis.maths.NDArrays;
+import org.apache.sis.maths.NDRange;
 import org.apache.sis.maths.Vector;
 import org.apache.sis.maths.Vectors;
 import static org.apache.sis.maths.Maths.clamp;
@@ -69,6 +74,11 @@ public final class Intersection {
      */
     private static final double RAY_TOLERANCE = 1E-10;
 
+    /**
+     * The cube from −1 to 1, which is an oriented box in its own coordinates.
+     */
+    private static final NDRange UNIT_CUBE = new NDRange(Vector.of(-1.0, -1.0, 
-1.0), Vector.of(1.0, 1.0, 1.0));
+
     private Intersection(){}
 
     /**
@@ -909,6 +919,79 @@ public final class Intersection {
         return (distance <= tolerance * tolerance) ? 
RayInterval.point(ratios[0] * length) : RayInterval.NONE;
     }
 
+    /**
+     * Computes the part of a ray inside a box, in any number of dimensions, 
with the slab method.
+     *
+     * <ul>
+     *   <li>The boundary of the box belongs to the box: a ray touching only 
an edge or a corner gives a single point.</li>
+     *   <li>A ray starting inside the box gives an interval starting at 
0.</li>
+     * </ul>
+     *
+     * @param  origin     start of the ray.
+     * @param  direction  direction of the ray, not zero.
+     * @param  box        the box, of the dimension of the ray.
+     * @return the part of the ray inside the box.
+     * @throws IllegalArgumentException if the direction is zero.
+     */
+    public static RayInterval rayBox(Vector<?> origin, Vector<?> direction, 
Envelope box) {
+        ensureNonZero(direction, "direction");
+        double start = 0, end = Double.POSITIVE_INFINITY;
+        for (int i = 0; i < box.getDimension(); i++) {
+            final double o = origin.get(i);
+            final double d = direction.get(i);
+            final double min = box.getMinimum(i);
+            final double max = box.getMaximum(i);
+            if (d == 0) {
+                if (!(o >= min && o <= max)) {
+                    return RayInterval.NONE;
+                }
+            } else {
+                final double t0 = (min - o) / d;
+                final double t1 = (max - o) / d;
+                start = Math.max(start, Math.min(t0, t1));
+                end   = Math.min(end,   Math.max(t0, t1));
+                if (!(start <= end)) {
+                    return RayInterval.NONE;
+                }
+            }
+        }
+        return new RayInterval(start, end);
+    }
+
+    /**
+     * Computes the part of a ray inside an oriented box, in 3 dimensions.
+     * The ray is expressed in the coordinates of the box, where the box is 
the cube from −1 to 1,
+     * then intersected with that cube by {@link #rayBox(Vector, Vector, 
Envelope)}.
+     *
+     * <ul>
+     *   <li>The axes of the box need not be orthogonal: the box is then a 
parallelepiped.</li>
+     *   <li>A degenerate box, whose axes are coplanar, is never touched.</li>
+     * </ul>
+     *
+     * @param  origin     start of the ray.
+     * @param  direction  direction of the ray, not zero.
+     * @param  box        the oriented box.
+     * @return the part of the ray inside the box.
+     * @throws IllegalArgumentException if the direction is zero.
+     */
+    public static RayInterval rayBox(Vector<?> origin, Vector<?> direction, 
OBBox box) {
+        final Vector<?>[] axes = {box.getXAxis(), box.getYAxis(), 
box.getZAxis()};
+        final Vector<?> offset = 
origin.copy(DataType.DOUBLE).subtract(box.getCenter());
+        final double[] localOrigin = new double[3];
+        final double[] localDirection = new double[3];
+        for (int i = 0; i < 3; i++) {
+            // Normal of the faces crossing axis i, which contain the two 
other axes.
+            final Vector<?> normal = axes[(i + 1) % 3].cross(axes[(i + 2) % 
3]);
+            final double scale = normal.dot(axes[i]);
+            if (scale == 0) {
+                return RayInterval.NONE;
+            }
+            localOrigin[i]    = normal.dot(offset)    / scale;
+            localDirection[i] = normal.dot(direction) / scale;
+        }
+        return rayBox(Vector.of(localOrigin), Vector.of(localDirection), 
UNIT_CUBE);
+    }
+
     /**
      * Returns the squared length of the given vector.
      *
@@ -960,8 +1043,8 @@ public final class Intersection {
     }
 
     /**
-     * Computes the intersection of a ray with a hyperplane, a plane which is 
not a quad, a triangle or
-     * a line string. The intersection with a triangle or a line string gets 
the attributes of that geometry,
+     * Computes the intersection of a ray with a hyperplane, a plane which is 
not a quad, a triangle, a line string
+     * or a box. The intersection with a triangle or a line string gets the 
attributes of that geometry,
      * interpolated where the ray touches it. The tolerance is {@value 
#RAY_TOLERANCE} times the largest
      * absolute coordinate of the operands.
      *
@@ -981,6 +1064,8 @@ public final class Intersection {
             case Triangle triangle -> hits = triangleHits(ray, triangle);
             case Line line         -> hits = polylineHits(ray, line, 
line.getDataPoints());
             case LineString line   -> hits = polylineHits(ray, line, 
line.getDataPoints());
+            case BBox box          -> hits = boxHits(ray, box, (origin, 
direction) -> rayBox(origin, direction, box));
+            case OBBox box         -> hits = boxHits(ray, box, (origin, 
direction) -> rayBox(origin, direction, box));
             default -> throw new UnsupportedOperationException(
                     "The intersection of a ray with a " + 
other.getGeometryType() + " is not supported.");
         }
@@ -1018,6 +1103,22 @@ public final class Intersection {
         return List.of(new RayHit(hit.start(), hit.end(), geometry));
     }
 
+    /**
+     * Intersection of a ray with a box: a point or a line string without 
attribute other than the positions.
+     *
+     * @param  intersection  the function computing the part of the ray inside 
the box, from the ray origin and direction.
+     */
+    private static List<RayHit> boxHits(final Ray ray, final Geometry box,
+                                        final BiFunction<Vector<?>, Vector<?>, 
RayInterval> intersection)
+    {
+        ProcessorUtils.ensureSameCRS(ray, box);
+        final Vector<?> origin = ray.getPosition();
+        final Vector<?> direction = ray.getDirection();
+        final CoordinateReferenceSystem crs = 
ray.getCoordinateReferenceSystem();
+        return RayHit.of(intersection.apply(origin, direction),
+                (t) -> GeometryFactory.DEFAULT.createPoint(crs, along(origin, 
direction, t).toArrayDouble()));
+    }
+
     /**
      * Intersection of a ray with a triangle: a point or a line string, with 
attributes interpolated between the corners.
      */
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AbstractD9IMTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AbstractD9IMTest.java
index e354b175d6..c9c2b83a1b 100644
--- 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AbstractD9IMTest.java
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/AbstractD9IMTest.java
@@ -22,6 +22,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.apache.sis.geometries.BBox;
 import org.apache.sis.geometries.Curve;
 import org.apache.sis.geometries.DataPoints;
 import org.apache.sis.geometries.DataPointsType;
@@ -317,6 +318,11 @@ public abstract class AbstractD9IMTest {
      */
     public static final HyperPlane PLANE_Z5 = new HyperPlane(CRS_3D, 
Vector.of(0, 0, 5), Vector.of(0, 0, 1));
 
+    /**
+     * The box from (0 0 0) to (2 2 2).
+     */
+    public static final BBox BOX_3D = new BBox(CRS_3D, 0, 0, 0,  2, 2, 2);
+
     /**
      * Creates data points in {@link #CRS_3D} with the given {@value 
#ATT_TEST} attribute values.
      */
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java
index 6c05e98d7e..2c398eff34 100644
--- 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectionTest.java
@@ -16,14 +16,17 @@
  */
 package org.apache.sis.geometries.operation;
 
+import org.opengis.geometry.Envelope;
 import org.apache.sis.geometries.Geometry;
 import org.apache.sis.geometries.GeometryCollection;
+import org.apache.sis.geometries.OBBox;
 import org.apache.sis.geometries.Plane;
 import org.apache.sis.geometries.Point;
 import org.apache.sis.geometries.curve.LineString;
 import org.apache.sis.geometries.curve.Ray;
 import org.apache.sis.maths.Array;
 import org.apache.sis.maths.NDArrays;
+import org.apache.sis.maths.NDRange;
 import org.apache.sis.maths.SampleSystem;
 import org.apache.sis.maths.Vector;
 import org.apache.sis.geometries.mesh.MeshPrimitive;
@@ -308,6 +311,53 @@ public class IntersectionTest extends AbstractD9IMTest {
         assertTrue(Intersection.rayTriangle(v(0, 0, 0), east, a, b, v(8, 0, 
5), 0).isEmpty(), "Degenerate triangle.");
     }
 
+    /**
+     * Tests {@link Intersection#rayBox(Vector, Vector, Envelope)} on the box 
from (0 0 0) to (2 2 2)
+     * and on the square from (0 0) to (2 2).
+     */
+    @Test
+    public void testRayBox() {
+        assertRayInterval(1, 3, Intersection.rayBox(v(-1, 1, 1), v(1, 0, 0), 
BOX_3D));
+        assertRayInterval(1, 3, Intersection.rayBox(v(-1, -1, -1), v(1, 1, 1), 
BOX_3D));
+        assertRayInterval(0, 1, Intersection.rayBox(v(1, 1, 1), v(0, 0, 1), 
BOX_3D));          // Starting inside.
+        assertRayInterval(1, 3, Intersection.rayBox(v(-1, 2, 2), v(1, 0, 0), 
BOX_3D));         // Along an edge.
+        assertTrue(Intersection.rayBox(v(-1, 3, 1), v(1, 0, 0), 
BOX_3D).isEmpty(), "Ray beside the box.");
+        assertTrue(Intersection.rayBox(v( 3, 1, 1), v(1, 0, 0), 
BOX_3D).isEmpty(), "Box behind the ray.");
+        assertThrows(IllegalArgumentException.class, () -> 
Intersection.rayBox(v(-1, 1, 1), v(0, 0, 0), BOX_3D));
+
+        final NDRange square = new NDRange(v(0, 0), v(2, 2));
+        assertRayPoint(1, Intersection.rayBox(v(-1, 1), v(1, 1), square));     
               // Touching a corner.
+        assertRayInterval(0.5, 1.5, Intersection.rayBox(v(-1, 1), v(2, 0), 
square));
+    }
+
+    /**
+     * Tests {@link Intersection#rayBox(Vector, Vector, OBBox)} on a box 
centered on (1 2 3), turned by 45°
+     * around the third axis: a square of corners (1 0), (3 2), (1 4) and (−1 
2) from <var>z</var> = 1 to 5.
+     */
+    @Test
+    public void testRayOrientedBox() {
+        final OBBox box = new OBBox(CRS_3D);
+        box.setCenter(v(1, 2, 3));
+        box.setXAxis(v(1, 1, 0));
+        box.setYAxis(v(-1, 1, 0));
+        box.setZAxis(v(0, 0, 2));
+        assertRayInterval(6, 10, Intersection.rayBox(v(1, 2, -5), v(0, 0, 1), 
box));
+        assertRayInterval(4, 8,  Intersection.rayBox(v(-5, 2, 3), v(1, 0, 0), 
box));
+        assertTrue(Intersection.rayBox(v(-5, 5, 3), v(1, 0, 0), 
box).isEmpty(), "Ray beside the box.");
+    }
+
+    /**
+     * Tests the intersection of a ray with a box: the segment of the ray 
inside the box.
+     */
+    @Test
+    public void testIntersectionOfRayAndBox() {
+        final GeometryProcessor processor = new GeometryProcessor();
+        final LineString inside = assertInstanceOf(LineString.class, 
processor.intersection(ray(-1, 1, 1, 1, 0, 0), BOX_3D));
+        assertArrayEquals(new double[] {0, 1, 1}, 
inside.getPointN(0).getPosition().toArrayDouble(), TOLERANCE);
+        assertArrayEquals(new double[] {2, 1, 1}, 
inside.getPointN(1).getPosition().toArrayDouble(), TOLERANCE);
+        assertTrue(processor.intersection(ray(-1, 3, 1, 1, 0, 0), 
BOX_3D).isEmpty());
+    }
+
     /**
      * Tests {@link Intersection#raySegment(Vector, Vector, Vector, Vector, 
double)} with segments
      * crossing the ray, along the ray, or not touching it.
diff --git 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java
 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java
index ca6b0a4783..91ff8c714a 100644
--- 
a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java
+++ 
b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/operation/IntersectsTest.java
@@ -114,6 +114,8 @@ public class IntersectsTest extends AbstractD9IMTest {
         new TestCase(ray(-1, 2, 1, 1, 0, 0),  RING_3D,                true,  
null),
         new TestCase(ray(-1, 2, 2, 1, 0, 0),  RING_3D,                false, 
null),
         new TestCase(ray(1, 2, 0,  0, 0, 1),  quad(),                 null,  
OperationException.class),
+        new TestCase(ray(-1, 1, 1, 1, 0, 0),  BOX_3D,                 true,  
null),
+        new TestCase(ray(-1, 3, 1, 1, 0, 0),  BOX_3D,                 false, 
null),
         new TestCase(ray(1, 2, 0,  0, 0, 1),  SQUARE,                 null,  
UnsupportedOperationException.class)
     };
 

Reply via email to