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 14ff00cd5287f1807ea2a13a6c2a301f83bf07d2 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:43:25 2026 +0100 feat(Geometry): add triangle-box overlap test --- .../geometries/operation/GeometryProcessor.java | 6 ++ .../sis/geometries/operation/Intersection.java | 66 ++++++++++++++++++++++ .../sis/geometries/operation/IntersectionTest.java | 13 +++++ .../sis/geometries/operation/IntersectsTest.java | 15 +++++ 4 files changed, 100 insertions(+) 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 4b833c8df2..d973129fa1 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 @@ -377,6 +377,12 @@ public final class GeometryProcessor { if (geom2 instanceof Triangle triangle && isPlane(geom1)) { return !Intersection.trianglePlaneIntersection(triangle, geom1).isEmpty(); } + if (geom1 instanceof Triangle triangle && geom2 instanceof BBox box) { + return Intersection.triangleBoxIntersects(triangle, box); + } + if (geom2 instanceof Triangle triangle && geom1 instanceof BBox box) { + return Intersection.triangleBoxIntersects(triangle, box); + } //TODO : fallback on JTS until implemented return jts(geom1).intersects(jts(geom2)); 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 eb2a98da3c..9987d6a686 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 @@ -1143,6 +1143,72 @@ public final class Intersection { }; } + /** + * Returns whether a triangle and a box overlap, in 3 dimensions, with the separating axis theorem. + * The shapes are apart if their projections on one of 13 axes are disjoint: the 3 axes of the box, + * the normal of the triangle, and the cross products of the edges of the triangle with the axes of the box. + * + * <ul> + * <li>The boundaries belong to the shapes: a triangle touching the box overlaps it.</li> + * </ul> + * + * @param a first corner of the triangle. + * @param b second corner of the triangle. + * @param c third corner of the triangle. + * @param box the box, in 3 dimensions. + * @return whether the triangle and the box overlap. + * + * @see "Tomas Akenine-Möller, Fast 3D Triangle-Box Overlap Testing, Journal of Graphics Tools, 2001" + */ + public static boolean triangleBox(Vector<?> a, Vector<?> b, Vector<?> c, Envelope box) { + // The triangle is expressed relatively to the center of the box. + final Vector<?> center = Vector.of(box.getMedian(0), box.getMedian(1), box.getMedian(2)); + final double[] half = {box.getSpan(0) / 2, box.getSpan(1) / 2, box.getSpan(2) / 2}; + final Vector<?>[] corners = { + a.copy(DataType.DOUBLE).subtract(center), + b.copy(DataType.DOUBLE).subtract(center), + c.copy(DataType.DOUBLE).subtract(center) + }; + final Vector<?>[] boxAxes = {Vector.of(1.0, 0.0, 0.0), Vector.of(0.0, 1.0, 0.0), Vector.of(0.0, 0.0, 1.0)}; + final List<Vector<?>> axes = new ArrayList<>(13); + axes.addAll(Arrays.asList(boxAxes)); + axes.add(corners[1].subtract(corners[0]).cross(corners[2].subtract(corners[0]))); + for (int i = 0; i < 3; i++) { + final Vector<?> edge = corners[(i + 1) % 3].subtract(corners[i]); + for (final Vector<?> axis : boxAxes) { + axes.add(edge.cross(axis)); + } + } + for (final Vector<?> axis : axes) { + double min = Double.POSITIVE_INFINITY, max = Double.NEGATIVE_INFINITY; + for (final Vector<?> corner : corners) { + final double p = axis.dot(corner); + min = Math.min(min, p); + max = Math.max(max, p); + } + // Half of the projection of the box on the axis. + final double radius = half[0] * Math.abs(axis.get(0)) + half[1] * Math.abs(axis.get(1)) + half[2] * Math.abs(axis.get(2)); + if (min > radius || max < -radius) { + return false; + } + } + return true; + } + + /** + * Returns whether a triangle and a box overlap, in 3 dimensions. + * + * @throws OperationException if the geometries are not in the same reference system or not in 3 dimensions. + */ + static boolean triangleBoxIntersects(final Triangle triangle, final BBox box) throws OperationException { + ProcessorUtils.ensureSameCRS(triangle, box); + if (box.getDimension() != 3) { + throw new OperationException("The intersection of a triangle with a box is computed in 3 dimensions only."); + } + final DataPoints corners = triangle.getExteriorRing().getDataPoints(); + return triangleBox(corners.getPosition(0), corners.getPosition(1), corners.getPosition(2), box); + } + /** * Returns the squared length of the given vector. * 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 5ec736925c..0b2f6d02d7 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 @@ -419,6 +419,19 @@ public class IntersectionTest extends AbstractD9IMTest { assertTrue(processor.intersection(TRIANGLE_3D, new Plane(CRS_3D, v(9, 0, 0), v(1, 0, 0))).isEmpty()); } + /** + * Tests {@link Intersection#triangleBox(Vector, Vector, Vector, Envelope)} on the box from (0 0 0) to (2 2 2). + */ + @Test + public void testTriangleBox() { + assertTrue (Intersection.triangleBox(v(0.5, 0.5, 0.5), v(1, 0.5, 0.5), v(0.5, 1, 0.5), BOX_3D), "Inside."); + assertTrue (Intersection.triangleBox(v(-10, -10, 1), v(10, -10, 1), v(0, 10, 1), BOX_3D), "Crossing the whole box."); + assertTrue (Intersection.triangleBox(v(2, 0, 0), v(3, 0, 0), v(2, 1, 0), BOX_3D), "Touching a face."); + assertTrue (Intersection.triangleBox(v(5.5, 0, 0), v(0, 5.5, 0), v(0, 0, 5.5), BOX_3D), "Cutting a corner."); + assertFalse(Intersection.triangleBox(v(7, 0, 0), v(0, 7, 0), v(0, 0, 7), BOX_3D), "Beyond a corner, separated by the normal."); + assertFalse(Intersection.triangleBox(v(3, 3, 3), v(4, 3, 3), v(3, 4, 3), BOX_3D), "Far from the box."); + } + /** * 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 65632b3f74..36840f628c 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 @@ -16,8 +16,11 @@ */ package org.apache.sis.geometries.operation; +import java.util.Arrays; import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.Plane; +import org.apache.sis.geometries.surface.Triangle; import org.apache.sis.maths.Vector; // Test dependencies @@ -115,12 +118,24 @@ public class IntersectsTest extends AbstractD9IMTest { 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(TRIANGLE_3D, PLANE_Z5, true, null), + new TestCase(TRIANGLE_3D, BOX_3D, false, null), + new TestCase(BOX_3D, triangle(-10, -10, 1, 10, -10, 1, 0, 10, 1), true, null), new TestCase(PLANE_Z5, TRIANGLE_3D, true, null), 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) }; + /** + * Returns the triangle of the given corners, in {@link #CRS_3D}. + */ + private static Triangle triangle(final double... corners) { + final double[] closed = Arrays.copyOf(corners, 12); + System.arraycopy(corners, 0, closed, 9, 3); + return GeometryFactory.DEFAULT.createTriangle(GeometryFactory.DEFAULT.createLinearRing( + GeometryFactory.DEFAULT.createDataPoints(CRS_3D, closed))); + } + /** * Returns the square of size 2 × 2 centered on (0 0 5) in the plane <var>z</var> = 5. */
