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 767f4bbb0986c4517051fa9f8756ddf1e8932a52 Author: jsorel <[email protected]> AuthorDate: Fri Nov 27 09:46:11 2026 +0100 feat(Geometry): add nearest point queries --- .../main/org/apache/sis/geometries/BBox.java | 15 ++ .../main/org/apache/sis/geometries/OBBox.java | 22 +++ .../apache/sis/geometries/operation/Distance.java | 168 +++++++++++++++++++++ .../geometries/operation/GeometryProcessor.java | 18 +++ .../apache/sis/geometries/surface/Triangle.java | 16 ++ .../test/org/apache/sis/geometries/BBoxTest.java | 11 ++ .../test/org/apache/sis/geometries/OBBoxTest.java | 11 ++ .../sis/geometries/operation/DistanceTest.java | 82 ++++++++++ 8 files changed, 343 insertions(+) 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 ee50efe01d..8f4735d82f 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,21 @@ public final class BBox extends GeneralEnvelope implements Geometry { return Quantities.create(length(0) * length(1) * length(2), Geometries.getVolumeUnit(this)); } + /** + * Returns the position of this box nearest to the given position. + * A position inside this box is its own nearest position. + * + * @param position the position for which to find the nearest position, of the dimension of this box. + * @return the position of this box nearest to the given position. + */ + public Vector<?> nearest(final Vector<?> position) { + final double[] nearest = new double[getDimension()]; + for (int i = 0; i < nearest.length; i++) { + nearest[i] = Math.clamp(position.get(i), getMinimum(i), getMaximum(i)); + } + return Vector.of(nearest); + } + /** * Returns the signed distance from this box to the given position: * positive outside the box, negative inside. diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java index c624be4128..6a9a47808c 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/OBBox.java @@ -248,6 +248,28 @@ public final class OBBox extends AbstractGeometry { return Quantities.create(volume, Geometries.getVolumeUnit(this)); } + /** + * Returns the position of this box nearest to the given position. + * A position inside this box is its own nearest position. + * + * <ul> + * <li>The axes of this box shall be orthogonal.</li> + * </ul> + * + * @param position the position for which to find the nearest position. + * @return the position of this box nearest to the given position. + */ + public Vector<?> nearest(final Vector<?> position) { + final Vector<?> offset = position.copy(DataType.DOUBLE).subtract(center); + Vector<?> nearest = center.copy(DataType.DOUBLE); + for (final Vector<?> axis : new Vector<?>[] {xAxis, yAxis, zAxis}) { + // Coordinate of the position along the axis, from -1 to 1 inside the box. + final double t = Math.clamp(offset.dot(axis) / axis.lengthSquare(), -1, 1); + nearest = nearest.combine(1, axis, t); + } + return nearest; + } + /** * Build oriented bbox from given envelope. * @param env 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 1b5df0a0c0..b37ae42291 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 @@ -135,6 +135,174 @@ public final class Distance { return Math.min(Math.min(d1, d2), Math.min(distance.applyAsDouble(0), distance.applyAsDouble(1))); } + /** + * Nearest points of two geometries. + * + * @param first the point of the first geometry nearest to the second geometry. + * @param second the point of the second geometry nearest to the first geometry. + */ + public record NearestPoints(Vector<?> first, Vector<?> second) { + /** + * Returns the distance between the two geometries, which is the distance between their nearest points. + * + * @return the distance between the nearest points. + */ + public double distance() { + return first.distance(second); + } + } + + /** + * Returns the barycentric weights of the point of a triangle nearest to the given position, in any number of dimensions. + * The weights are those of a corner or of a point on an edge when the nearest point is on the boundary of the triangle. + * + * @param position the position for which to find the nearest point. + * @param a first corner of the triangle. + * @param b second corner of the triangle. + * @param c third corner of the triangle. + * @return barycentric weights of the nearest point, one per corner. + * + * @see "Christer Ericson, Real-Time Collision Detection, section 5.1.5" + */ + public static double[] nearestOnTriangle(Vector<?> position, Vector<?> a, Vector<?> b, Vector<?> c) { + final Vector<?> origin = a.copy(DataType.DOUBLE); + final Vector<?> ab = b.copy(DataType.DOUBLE).subtract(origin); + final Vector<?> ac = c.copy(DataType.DOUBLE).subtract(origin); + final Vector<?> ap = position.copy(DataType.DOUBLE).subtract(origin); + final double d1 = ab.dot(ap); + final double d2 = ac.dot(ap); + if (d1 <= 0 && d2 <= 0) { + return new double[] {1, 0, 0}; // Corner a. + } + final Vector<?> bp = position.copy(DataType.DOUBLE).subtract(b); + final double d3 = ab.dot(bp); + final double d4 = ac.dot(bp); + if (d3 >= 0 && d4 <= d3) { + return new double[] {0, 1, 0}; // Corner b. + } + final double vc = d1 * d4 - d3 * d2; + if (vc <= 0 && d1 >= 0 && d3 <= 0) { + final double v = d1 / (d1 - d3); + return new double[] {1 - v, v, 0}; // Edge ab. + } + final Vector<?> cp = position.copy(DataType.DOUBLE).subtract(c); + final double d5 = ab.dot(cp); + final double d6 = ac.dot(cp); + if (d6 >= 0 && d5 <= d6) { + return new double[] {0, 0, 1}; // Corner c. + } + final double vb = d5 * d2 - d1 * d6; + if (vb <= 0 && d2 >= 0 && d6 <= 0) { + final double w = d2 / (d2 - d6); + return new double[] {1 - w, 0, w}; // Edge ac. + } + final double va = d3 * d6 - d5 * d4; + if (va <= 0 && (d4 - d3) >= 0 && (d5 - d6) >= 0) { + final double w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); + return new double[] {0, 1 - w, w}; // Edge bc. + } + final double v = vb / (va + vb + vc); + final double w = vc / (va + vb + vc); + return new double[] {1 - v - w, v, w}; // Inside. + } + + /** + * Returns the nearest points of a segment and a triangle, in 3 dimensions. + * When the segment touches the triangle, both points are the same. + * + * @param start start of the segment. + * @param end end of the segment. + * @param a first corner of the triangle. + * @param b second corner of the triangle. + * @param c third corner of the triangle. + * @return the point of the segment and the point of the triangle nearest to each other. + */ + public static NearestPoints nearestSegmentTriangle(Vector<?> start, Vector<?> end, Vector<?> a, Vector<?> b, Vector<?> c) { + final double[] s0 = start.toArrayDouble(), s1 = end.toArrayDouble(); + final double[] pa = a.toArrayDouble(), pb = b.toArrayDouble(), pc = c.toArrayDouble(); + final Intersection.SegmentTriangle hit = Intersection.segmentTriangle(s0, s1, pa, pb, pc, 0); + double ratio = Double.NaN; + switch (hit.type()) { + case INTERIOR, EDGE, VERTEX -> ratio = hit.ratio(); + case COPLANAR -> { + final double[] normal = Maths.cross(Vectors.subtract(pb, pa), Vectors.subtract(pc, pa)); + if (Vectors.lengthSquare(normal) != 0) { + final List<Intersection.SegmentTriangle> inside = Intersection.segmentInTriangle2D( + s0, s1, pa, pb, pc, Maths.projectionAxes(normal)); + if (!inside.isEmpty()) { + ratio = inside.get(0).ratio(); + } + } + } + default -> {} + } + if (!Double.isNaN(ratio)) { + final Vector<?> point = start.copy(DataType.DOUBLE).lerp(end, ratio); + return new NearestPoints(point, point); + } + // Apart: the nearest points are on the ends of the segment or on the edges of the triangle. + NearestPoints nearest = null; + for (final Vector<?> point : new Vector<?>[] {start, end}) { + nearest = nearer(nearest, new NearestPoints(point.copy(DataType.DOUBLE), atWeights(nearestOnTriangle(point, a, b, c), a, b, c))); + } + final double[][] corners = {pa, pb, pc}; + for (int i = 0; i < 3; i++) { + final double[] onSegment = new double[s0.length]; + final double[] onEdge = new double[s0.length]; + distanceSquare(s0, s1, onSegment, corners[i], corners[(i + 1) % 3], onEdge, new double[2], 0); + nearest = nearer(nearest, new NearestPoints(Vector.of(onSegment), Vector.of(onEdge))); + } + return nearest; + } + + /** + * Returns the nearest points of two triangles, in 3 dimensions. + * When the triangles touch, both points are the same. + * + * @param a0 first corner of the first triangle. + * @param a1 second corner of the first triangle. + * @param a2 third corner of the first triangle. + * @param b0 first corner of the second triangle. + * @param b1 second corner of the second triangle. + * @param b2 third corner of the second triangle. + * @return the point of the first triangle and the point of the second triangle nearest to each other. + */ + public static NearestPoints nearestTriangleTriangle(Vector<?> a0, Vector<?> a1, Vector<?> a2, + Vector<?> b0, Vector<?> b1, Vector<?> b2) + { + final Intersection.TriangleTriangle hit = Intersection.triangleTriangle( + a0.toArrayDouble(), a1.toArrayDouble(), a2.toArrayDouble(), + b0.toArrayDouble(), b1.toArrayDouble(), b2.toArrayDouble(), 0); + if (hit.start() != null) { + final Vector<?> point = Vector.of(hit.start().position()); + return new NearestPoints(point, point); + } + // Apart or coplanar: the nearest points include a point on an edge of one of the triangles. + final Vector<?>[] first = {a0, a1, a2}, second = {b0, b1, b2}; + NearestPoints nearest = null; + for (int i = 0; i < 3; i++) { + final int j = (i + 1) % 3; + nearest = nearer(nearest, nearestSegmentTriangle(first[i], first[j], b0, b1, b2)); + final NearestPoints reverse = nearestSegmentTriangle(second[i], second[j], a0, a1, a2); + nearest = nearer(nearest, new NearestPoints(reverse.second(), reverse.first())); + } + return nearest; + } + + /** + * Returns the candidate whose points are the nearest to each other. + */ + private static NearestPoints nearer(final NearestPoints current, final NearestPoints candidate) { + return (current == null || candidate.distance() < current.distance()) ? candidate : current; + } + + /** + * Returns the position of the given barycentric weights in the triangle of the given corners. + */ + private static Vector<?> atWeights(final double[] weights, final Vector<?> a, final Vector<?> b, final Vector<?> c) { + return a.copy(DataType.DOUBLE).combine(weights[0], b, weights[1]).combine(1, c, weights[2]); + } + /** * Calculate distance between two points. * 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 d973129fa1..e00695c7c3 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 @@ -34,6 +34,7 @@ import org.opengis.util.FactoryException; import org.apache.sis.geometries.BBox; import org.apache.sis.geometries.Curve; import org.apache.sis.geometries.DE9IM; +import org.apache.sis.geometries.DataPoints; import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.HyperPlane; @@ -206,6 +207,23 @@ public final class GeometryProcessor { return Quantities.create(Distance.distance(pt1, pt2), Geometries.getLinearUnit(pt1)); } } + if (geom1 instanceof Triangle t1 && geom2 instanceof Triangle t2 && t1.getDimension() == 3) { + ProcessorUtils.ensureSameCRS(t1, t2); + final DataPoints c1 = t1.getExteriorRing().getDataPoints(); + final DataPoints c2 = t2.getExteriorRing().getDataPoints(); + final double distance = Distance.nearestTriangleTriangle( + c1.getPosition(0), c1.getPosition(1), c1.getPosition(2), + c2.getPosition(0), c2.getPosition(1), c2.getPosition(2)).distance(); + return Quantities.create(distance, Geometries.getLinearUnit(t1)); + } + if (geom1 instanceof Point pt && geom2 instanceof Triangle triangle) { + ProcessorUtils.ensureSameCRS(pt, triangle); + return Quantities.create(triangle.nearest(pt.getPosition()).getPosition().distance(pt.getPosition()), + Geometries.getLinearUnit(pt)); + } + if (geom2 instanceof Point pt && geom1 instanceof Triangle triangle) { + return distance(pt, triangle); + } //TODO : fallback on JTS until implemented return Quantities.create(jts(geom1).distance(jts(geom2)), Geometries.getLinearUnit(geom1)); diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Triangle.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Triangle.java index cc58634b46..9b933fc07a 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Triangle.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/surface/Triangle.java @@ -32,6 +32,7 @@ import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.curve.LinearRing; import org.apache.sis.geometries.internal.shared.Curves; import org.apache.sis.geometries.internal.shared.DefaultTriangle; +import org.apache.sis.geometries.operation.Distance; import org.apache.sis.geometries.operation.simplify.greedyinsert.WTriangle; import org.apache.sis.geometries.operation.triangulate.delaunay.OrientedTriangle; import org.apache.sis.maths.DataType; @@ -210,6 +211,21 @@ public sealed interface Triangle extends Polygon return getExteriorRing().getDataPoints().viewInterpolatedPoint(new int[] {0, 1, 2}, weights); } + /** + * Returns the point of this triangle nearest to the given position, with the attributes of this triangle + * interpolated at that point. + * + * @param position the position for which to find the nearest point. + * @return the point of this triangle nearest to the given position, independent of this triangle. + * + * @see Distance#nearestOnTriangle(Vector, Vector, Vector, Vector) + */ + default Point nearest(Vector<?> position) { + final DataPoints corners = getExteriorRing().getDataPoints(); + return corners.getInterpolatedPoint(new int[] {0, 1, 2}, Distance.nearestOnTriangle(position, + corners.getPosition(0), corners.getPosition(1), corners.getPosition(2))); + } + /** * Returns the barycentric weights of the given position relative to the three corners of this triangle. * 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 2f6381fc0f..12c461d3dc 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 @@ -19,6 +19,7 @@ package org.apache.sis.geometries; import org.apache.sis.maths.Vector; // Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -99,4 +100,14 @@ public class BBoxTest extends GeometryTest { assertEquals(-1, box.signedDistance(Vector.of(1, 1, 1)), TOLERANCE); assertEquals(-0.5, box.signedDistance(Vector.of(1, 1, 1.5)), TOLERANCE); } + + /** + * The nearest position of a box is the given position clamped to the box. + */ + @Test + public void testNearest() { + final BBox box = new BBox(CRS_3D, 0,0,0, 2,2,2); + assertArrayEquals(new double[] {2, 1, 0}, box.nearest(Vector.of(5, 1, -3)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] {1, 1, 1}, box.nearest(Vector.of(1, 1, 1)).toArrayDouble(), TOLERANCE); + } } diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java index 83aed0c804..b01ed1a101 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/OBBoxTest.java @@ -19,6 +19,7 @@ package org.apache.sis.geometries; import org.apache.sis.maths.Vector; // Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; @@ -65,4 +66,14 @@ public class OBBoxTest extends GeometryTest { box.setZAxis(Vector.of(1, 0, 2)); assertEquals(32, box.getVolume().getValue().doubleValue(), TOLERANCE); } + + /** + * The nearest position of the box turned by 45° is a corner, a face or the position itself. + */ + @Test + public void testNearest() { + assertArrayEquals(new double[] {-1, 2, 3}, box().nearest(Vector.of(-5, 2, 3)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] { 1, 2, 5}, box().nearest(Vector.of(1, 2, 10)).toArrayDouble(), TOLERANCE); + assertArrayEquals(new double[] { 1, 2, 3}, box().nearest(Vector.of(1, 2, 3)).toArrayDouble(), 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 bc136ae602..233a52be81 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 @@ -16,6 +16,7 @@ */ package org.apache.sis.geometries.operation; +import java.util.Arrays; import java.util.List; import javax.measure.Quantity; import org.apache.sis.geometries.Geometry; @@ -24,6 +25,7 @@ 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.geometries.surface.Triangle; import org.apache.sis.maths.SampleSystem; import org.apache.sis.maths.Vector; import org.apache.sis.maths.DataType; @@ -33,6 +35,7 @@ import org.apache.sis.measure.Units; import org.apache.sis.referencing.CommonCRS; // Test dependencies +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -243,4 +246,83 @@ public class DistanceTest extends AbstractD9IMTest { assertThrows(UnsupportedOperationException.class, () -> Distance.signedDistance(BOX_3D, PLANE_Z5)); assertThrows(OperationException.class, () -> Distance.signedDistance(sphere(0, 0, 0, 1), SQUARE)); } + + private static final Vector<?> A = Vector.of(0, 0, 0), B = Vector.of(4, 0, 0), C = Vector.of(0, 4, 0); + + 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))); + } + + private static void assertNearest(final double[] first, final double[] second, final Distance.NearestPoints actual) { + assertArrayEquals(first, actual.first().toArrayDouble(), TOLERANCE, "first"); + assertArrayEquals(second, actual.second().toArrayDouble(), TOLERANCE, "second"); + } + + /** + * Tests {@link Distance#nearestOnTriangle(Vector, Vector, Vector, Vector)} in each region around the triangle + * (0 0 0), (4 0 0), (0 4 0). + */ + @Test + public void testNearestOnTriangle() { + assertArrayEquals(new double[] {0.5, 0.25, 0.25}, Distance.nearestOnTriangle(Vector.of( 1, 1, 5), A, B, C), TOLERANCE); + assertArrayEquals(new double[] {1, 0, 0}, Distance.nearestOnTriangle(Vector.of(-2, -3, 0), A, B, C), TOLERANCE); + assertArrayEquals(new double[] {0, 1, 0}, Distance.nearestOnTriangle(Vector.of( 6, -1, 0), A, B, C), TOLERANCE); + assertArrayEquals(new double[] {0, 0, 1}, Distance.nearestOnTriangle(Vector.of(-1, 6, 2), A, B, C), TOLERANCE); + assertArrayEquals(new double[] {0.5, 0.5, 0}, Distance.nearestOnTriangle(Vector.of( 2, -3, 1), A, B, C), TOLERANCE); + assertArrayEquals(new double[] {0.75, 0, 0.25}, Distance.nearestOnTriangle(Vector.of(-2, 1, 0), A, B, C), TOLERANCE); + assertArrayEquals(new double[] {0, 0.5, 0.5}, Distance.nearestOnTriangle(Vector.of( 3, 3, 0), A, B, C), TOLERANCE); + } + + /** + * Tests {@link Distance#nearestSegmentTriangle(Vector, Vector, Vector, Vector, Vector)}. + */ + @Test + public void testNearestSegmentTriangle() { + assertNearest(new double[] {1, 1, 2}, new double[] {1, 1, 0}, + Distance.nearestSegmentTriangle(Vector.of(1, 1, 2), Vector.of(1, 1, 5), A, B, C)); + assertNearest(new double[] {1, 1, 0}, new double[] {1, 1, 0}, + Distance.nearestSegmentTriangle(Vector.of(1, 1, -1), Vector.of(1, 1, 1), A, B, C)); + assertNearest(new double[] {6, 0, 1}, new double[] {4, 0, 0}, + Distance.nearestSegmentTriangle(Vector.of(6, 1, 1), Vector.of(6, -1, 1), A, B, C)); + assertNearest(new double[] {0, 1, 0}, new double[] {0, 1, 0}, + Distance.nearestSegmentTriangle(Vector.of(-1, 1, 0), Vector.of(5, 1, 0), A, B, C)); + } + + /** + * Tests {@link Distance#nearestTriangleTriangle(Vector, Vector, Vector, Vector, Vector, Vector)}. + */ + @Test + public void testNearestTriangleTriangle() { + assertEquals(3, Distance.nearestTriangleTriangle(A, B, C, + Vector.of(1, 1, 3), Vector.of(2, 1, 3), Vector.of(1, 2, 3)).distance(), TOLERANCE); + assertEquals(0, Distance.nearestTriangleTriangle(A, B, C, + Vector.of(1, 1, -1), Vector.of(1, 1, 1), Vector.of(2, 2, 1)).distance(), TOLERANCE); + assertNearest(new double[] {4, 0, 0}, new double[] {6, 0, 1}, Distance.nearestTriangleTriangle(A, B, C, + Vector.of(6, 1, 1), Vector.of(6, -1, 1), Vector.of(7, 0, 1))); + } + + /** + * Tests the distance between triangles, and between a point and a triangle, in 3 dimensions. + */ + @Test + public void testDistanceOfTriangles() { + final GeometryProcessor processor = new GeometryProcessor(); + final Triangle base = triangle(0, 0, 0, 4, 0, 0, 0, 4, 0); + assertEquals(3, processor.distance(base, triangle(1, 1, 3, 2, 1, 3, 1, 2, 3)).getValue().doubleValue(), TOLERANCE); + assertEquals(3, processor.distance(GeometryFactory.DEFAULT.createPoint(CRS_3D, 1, 2, 8), TRIANGLE_3D).getValue().doubleValue(), TOLERANCE); + assertEquals(3, processor.distance(TRIANGLE_3D, GeometryFactory.DEFAULT.createPoint(CRS_3D, 1, 2, 8)).getValue().doubleValue(), TOLERANCE); + } + + /** + * Tests {@link Triangle#nearest(Vector)}, which interpolates the attributes of the triangle. + */ + @Test + public void testTriangleNearest() { + final Point nearest = TRIANGLE_3D.nearest(Vector.of(1, 2, 8)); + assertArrayEquals(new double[] {1, 2, 5}, nearest.getPosition().toArrayDouble(), TOLERANCE); + assertEquals(22.5, nearest.getAttribute(ATT_TEST).get(0), TOLERANCE); + } }
