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 acc9f9a2158269d344f9a0f3674712f2a4d27847 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:42:19 2026 +0100 feat(Geometry): add plane-plane and triangle-plane intersections --- .../geometries/operation/GeometryProcessor.java | 21 +++ .../sis/geometries/operation/Intersection.java | 151 +++++++++++++++++++++ .../sis/geometries/operation/IntersectionTest.java | 61 +++++++++ .../sis/geometries/operation/IntersectsTest.java | 2 + 4 files changed, 235 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 77af37316e..4b833c8df2 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 @@ -36,6 +36,8 @@ import org.apache.sis.geometries.Curve; import org.apache.sis.geometries.DE9IM; import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.Geometries; +import org.apache.sis.geometries.HyperPlane; +import org.apache.sis.geometries.Plane; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryCollection; import org.apache.sis.geometries.GeometryFactory; @@ -237,6 +239,12 @@ public final class GeometryProcessor { if (geom2 instanceof Ray ray) { return Intersection.rayIntersection(ray, geom1); } + if (geom1 instanceof Triangle triangle && isPlane(geom2)) { + return Intersection.trianglePlaneIntersection(triangle, geom2); + } + if (geom2 instanceof Triangle triangle && isPlane(geom1)) { + return Intersection.trianglePlaneIntersection(triangle, geom1); + } //TODO : fallback on JTS until implemented, this loss the attributes ! return fromJTS(jts(geom1).intersection(jts(geom2)), geom1); @@ -363,11 +371,24 @@ public final class GeometryProcessor { if (geom2 instanceof Ray ray) { return !Intersection.rayIntersection(ray, geom1).isEmpty(); } + if (geom1 instanceof Triangle triangle && isPlane(geom2)) { + return !Intersection.trianglePlaneIntersection(triangle, geom2).isEmpty(); + } + if (geom2 instanceof Triangle triangle && isPlane(geom1)) { + return !Intersection.trianglePlaneIntersection(triangle, geom1).isEmpty(); + } //TODO : fallback on JTS until implemented return jts(geom1).intersects(jts(geom2)); } + /** + * Returns whether the given geometry is a {@link HyperPlane} or a {@link Plane}. + */ + private static boolean isPlane(final Geometry geometry) { + return (geometry instanceof HyperPlane) || (geometry instanceof Plane); + } + /** * Returns a derived geometry collection value that matches the specified m coordinate value. * See Subclause 6.1.2.6 “Measures on Geometry” for more details. 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 cc2e89fa4a..eb2a98da3c 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 @@ -992,6 +992,157 @@ public final class Intersection { return rayBox(Vector.of(localOrigin), Vector.of(localDirection), UNIT_CUBE); } + /** + * Line where two planes meet, in 3 dimensions. + * + * @param type whether the planes meet along a line, are parallel or are the same plane. + * @param position for a line, its position nearest to the origin. Otherwise {@code null}. + * @param direction for a line, its direction, which is the cross product of the plane normals. Otherwise {@code null}. + * + * @see #planePlane(Vector, Vector, Vector, Vector, double) + */ + public record PlanePlane(Type type, Vector<?> position, Vector<?> direction) { + /** + * How two planes meet. + */ + public enum Type { + /** The planes are parallel and distinct. */ + NONE, + /** The planes meet along a line. */ + LINE, + /** The planes are the same plane. */ + COINCIDENT + } + } + + /** + * Computes the line where two planes meet, in 3 dimensions. + * + * @param point1 a position of the first plane. + * @param normal1 normal of the first plane, not zero. + * @param point2 a position of the second plane. + * @param normal2 normal of the second plane, not zero. + * @param tolerance largest distance between parallel planes for considering them as the same plane. + * @return the line where the planes meet, or whether they are parallel or the same plane. + */ + public static PlanePlane planePlane(Vector<?> point1, Vector<?> normal1, Vector<?> point2, Vector<?> normal2, double tolerance) { + final Vector<?> direction = normal1.cross(normal2); + final double det = direction.lengthSquare(); + if (det == 0) { + final double distance = Math.abs(normal1.dot(point2.copy(DataType.DOUBLE).subtract(point1))) / normal1.length(); + return new PlanePlane((distance <= tolerance) ? PlanePlane.Type.COINCIDENT : PlanePlane.Type.NONE, null, null); + } + // The point of the line nearest to the origin is a combination of the two normals. + final double h1 = normal1.dot(point1); + final double h2 = normal2.dot(point2); + final double n11 = normal1.lengthSquare(); + final double n22 = normal2.lengthSquare(); + final double n12 = normal1.dot(normal2); + final double c1 = (h1 * n22 - h2 * n12) / det; + final double c2 = (h2 * n11 - h1 * n12) / det; + return new PlanePlane(PlanePlane.Type.LINE, normal1.copy(DataType.DOUBLE).combine(c1, normal2, c2), direction); + } + + /** + * Part of a triangle on a plane. + * + * @param type whether the plane cuts the triangle along a segment, touches a corner, contains the triangle or misses it. + * @param start first end of the segment, or the touched corner. {@code null} if there is no point. + * @param end second end of the segment, or the same as {@code start} for a single point. + * + * @see #trianglePlane(Vector, Vector, Vector, Vector, Vector, double) + */ + public record TrianglePlane(Type type, Vector<?> start, Vector<?> end) { + /** + * How a plane meets a triangle. + */ + public enum Type { + /** The plane misses the triangle. */ + NONE, + /** The plane touches a corner of the triangle. */ + POINT, + /** The plane cuts the triangle, or contains one of its edges. */ + SEGMENT, + /** The plane contains the triangle. */ + COPLANAR + } + } + + /** + * Computes the part of a triangle on a plane, in any number of dimensions. + * A corner within the tolerance of the plane is on the plane. + * + * @param a first corner of the triangle. + * @param b second corner of the triangle. + * @param c third corner of the triangle. + * @param point a position of the plane. + * @param normal normal of the plane, not zero. + * @param tolerance largest distance to the plane for considering a corner on it. + * @return the part of the triangle on the plane. + */ + public static TrianglePlane trianglePlane(Vector<?> a, Vector<?> b, Vector<?> c, Vector<?> point, Vector<?> normal, double tolerance) { + final Vector<?>[] corners = {a, b, c}; + final double length = normal.length(); + final double[] distances = new double[3]; + for (int i = 0; i < 3; i++) { + final double d = normal.dot(corners[i].copy(DataType.DOUBLE).subtract(point)) / length; + distances[i] = (Math.abs(d) <= tolerance) ? 0 : d; + } + if (distances[0] == 0 && distances[1] == 0 && distances[2] == 0) { + return new TrianglePlane(TrianglePlane.Type.COPLANAR, null, null); + } + // The corners on the plane, and the points where the edges cross the plane. + final List<Vector<?>> points = new ArrayList<>(2); + for (int i = 0; i < 3; i++) { + final int j = (i + 1) % 3; + if (distances[i] == 0) { + points.add(corners[i].copy(DataType.DOUBLE)); + } else if (distances[i] * distances[j] < 0) { + points.add(corners[i].copy(DataType.DOUBLE).lerp(corners[j], distances[i] / (distances[i] - distances[j]))); + } + } + return switch (points.size()) { + case 0 -> new TrianglePlane(TrianglePlane.Type.NONE, null, null); + case 1 -> new TrianglePlane(TrianglePlane.Type.POINT, points.get(0), points.get(0)); + default -> new TrianglePlane(TrianglePlane.Type.SEGMENT, points.get(0), points.get(1)); + }; + } + + /** + * Computes the part of a triangle on a hyperplane or a plane which is not a quad. The result is a point or + * a line string whose attributes are interpolated between the corners of the triangle, or the triangle itself + * if it is in the plane. The tolerance is {@value #RAY_TOLERANCE} times the largest absolute coordinate. + * + * @param triangle the triangle, in 3 dimensions. + * @param plane a {@link HyperPlane} or a {@link Plane}. + * @return the part of the triangle on the plane. + * @throws OperationException if the geometries are not in the same reference system, + * or if the plane is a quad. + */ + static Geometry trianglePlaneIntersection(final Triangle triangle, final Geometry plane) throws OperationException { + ProcessorUtils.ensureSameCRS(triangle, plane); + final Vector<?> point, normal; + switch (plane) { + case HyperPlane cdt -> {point = cdt.getPosition(); normal = cdt.getNormal();} + case Plane cdt when !cdt.isQuad() -> {point = cdt.getPosition(); normal = cdt.getNormal();} + case Plane cdt -> throw new OperationException("The intersection with a plane of finite size is not supported."); + default -> throw new IllegalArgumentException("Not a plane: " + plane.getGeometryType()); + } + final DataPoints corners = triangle.getExteriorRing().getDataPoints(); + final Vector<?> a = corners.getPosition(0); + final Vector<?> b = corners.getPosition(1); + final Vector<?> c = corners.getPosition(2); + final TrianglePlane hit = trianglePlane(a, b, c, point, normal, rayTolerance(a, b, c, point)); + final int[] indices = {0, 1, 2}; + return switch (hit.type()) { + case NONE -> GeometryFactory.DEFAULT.createEmpty(triangle.getCoordinateReferenceSystem()); + case COPLANAR -> triangle; + case POINT -> corners.getInterpolatedPoint(indices, triangle.getBarycentricValue(hit.start())); + case SEGMENT -> line(corners.getInterpolatedPoint(indices, triangle.getBarycentricValue(hit.start())), + corners.getInterpolatedPoint(indices, triangle.getBarycentricValue(hit.end()))); + }; + } + /** * 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 2c398eff34..5ec736925c 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 @@ -30,7 +30,9 @@ 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; +import org.apache.sis.geometries.operation.Intersection.PlanePlane; import org.apache.sis.geometries.operation.Intersection.RayInterval; +import org.apache.sis.geometries.operation.Intersection.TrianglePlane; import org.apache.sis.referencing.CommonCRS; // Test dependencies @@ -39,6 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -358,6 +361,64 @@ public class IntersectionTest extends AbstractD9IMTest { assertTrue(processor.intersection(ray(-1, 3, 1, 1, 0, 0), BOX_3D).isEmpty()); } + /** + * Tests {@link Intersection#planePlane(Vector, Vector, Vector, Vector, double)}. + */ + @Test + public void testPlanePlane() { + PlanePlane line = Intersection.planePlane(v(0, 0, 5), v(0, 0, 2), v(1, 0, 0), v(-3, 0, 0), 0); + assertEquals(PlanePlane.Type.LINE, line.type()); + assertArrayEquals(new double[] {1, 0, 5}, line.position().toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {0, -6, 0}, line.direction().toArrayDouble(), TOLERANCE); + + line = Intersection.planePlane(v(2, 0, 0), v(1, 1, 0), v(0, 0, 0), v(0, 0, 1), 0); + assertArrayEquals(new double[] {1, 1, 0}, line.position().toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {1, -1, 0}, line.direction().toArrayDouble(), TOLERANCE); + + assertEquals(PlanePlane.Type.NONE, Intersection.planePlane(v(0, 0, 5), v(0, 0, 1), v(0, 0, 7), v(0, 0, 2), 0).type()); + assertEquals(PlanePlane.Type.COINCIDENT, Intersection.planePlane(v(0, 0, 5), v(0, 0, 1), v(3, 3, 5), v(0, 0, -1), 0).type()); + } + + /** + * Tests {@link Intersection#trianglePlane(Vector, Vector, Vector, Vector, Vector, double)} + * on the triangle (0 0 0), (4 0 0), (0 4 4). + */ + @Test + public void testTrianglePlane() { + final Vector<?> a = v(0, 0, 0), b = v(4, 0, 0), c = v(0, 4, 4); + final Vector<?> up = v(0, 0, 1); + TrianglePlane cut = Intersection.trianglePlane(a, b, c, v(0, 0, 1), up, 0); + assertEquals(TrianglePlane.Type.SEGMENT, cut.type()); + assertArrayEquals(new double[] {3, 1, 1}, cut.start().toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {0, 1, 1}, cut.end().toArrayDouble(), TOLERANCE); + + cut = Intersection.trianglePlane(a, b, c, v(0, 0, 4), up, 0); + assertEquals(TrianglePlane.Type.POINT, cut.type()); + assertArrayEquals(new double[] {0, 4, 4}, cut.start().toArrayDouble(), TOLERANCE); + + cut = Intersection.trianglePlane(a, b, c, v(0, 0, 0), up, 0); // An edge in the plane. + assertEquals(TrianglePlane.Type.SEGMENT, cut.type()); + assertArrayEquals(new double[] {0, 0, 0}, cut.start().toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {4, 0, 0}, cut.end().toArrayDouble(), TOLERANCE); + + assertEquals(TrianglePlane.Type.NONE, Intersection.trianglePlane(a, b, c, v(0, 0, 10), up, 0).type()); + assertEquals(TrianglePlane.Type.COPLANAR, Intersection.trianglePlane(a, b, v(0, 4, 0), v(7, 7, 0), up, 0).type()); + } + + /** + * Tests the intersection of a triangle with planes, the attributes of the triangle being interpolated. + */ + @Test + public void testIntersectionOfTriangleAndPlane() { + final GeometryProcessor processor = new GeometryProcessor(); + final Plane plane = new Plane(CRS_3D, v(1, 0, 0), v(1, 0, 0)); + final LineString cut = assertInstanceOf(LineString.class, processor.intersection(TRIANGLE_3D, plane)); + assertAttributedPoint(1, 0, 5, 12.5, cut.getPointN(0)); + assertAttributedPoint(1, 3, 5, 27.5, cut.getPointN(1)); + assertSame(TRIANGLE_3D, processor.intersection(PLANE_Z5, TRIANGLE_3D)); + assertTrue(processor.intersection(TRIANGLE_3D, new Plane(CRS_3D, v(9, 0, 0), v(1, 0, 0))).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 91ff8c714a..65632b3f74 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(TRIANGLE_3D, PLANE_Z5, 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)
