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 7b15d2c569b83a232684b99db801c63f48931a7e Author: jsorel <[email protected]> AuthorDate: Thu Nov 26 09:31:30 2026 +0100 feat(Geometry): add ray intersection methods --- .../geometries/operation/GeometryProcessor.java | 15 + .../sis/geometries/operation/Intersection.java | 405 +++++++++++++++++++-- .../apache/sis/geometries/surface/Triangle.java | 32 ++ .../main/org/apache/sis/maths/Maths.java | 39 -- .../sis/geometries/operation/AbstractD9IMTest.java | 57 +++ .../sis/geometries/operation/IntersectionTest.java | 177 +++++++++ .../sis/geometries/operation/IntersectsTest.java | 27 +- .../sis/geometries/surface/TriangleTest.java | 16 + 8 files changed, 704 insertions(+), 64 deletions(-) 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 db4d0e00bc..2211325e60 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 @@ -47,6 +47,7 @@ import org.apache.sis.geometries.Representation; import org.apache.sis.geometries.curve.LineString; import org.apache.sis.geometries.curve.LinearRing; import org.apache.sis.geometries.curve.OffsetCurve; +import org.apache.sis.geometries.curve.Ray; import org.apache.sis.geometries.cs.Bearing; import org.apache.sis.geometries.internal.shared.ArrayDataPoints; import org.apache.sis.geometries.internal.shared.Vertex; @@ -230,6 +231,13 @@ public final class GeometryProcessor { } } + if (geom1 instanceof Ray ray) { + return Intersection.rayIntersection(ray, geom2); + } + if (geom2 instanceof Ray ray) { + return Intersection.rayIntersection(ray, geom1); + } + //TODO : fallback on JTS until implemented, this loss the attributes ! return fromJTS(jts(geom1).intersection(jts(geom2)), geom1); } @@ -349,6 +357,13 @@ public final class GeometryProcessor { return false; } + if (geom1 instanceof Ray ray) { + return !Intersection.rayIntersection(ray, geom2).isEmpty(); + } + if (geom2 instanceof Ray ray) { + return !Intersection.rayIntersection(ray, geom1).isEmpty(); + } + //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 620e0abdb7..0be92c7798 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,15 +22,23 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Optional; +import java.util.function.DoubleFunction; import java.util.stream.Stream; +import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.TransformException; +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.Plane; import org.apache.sis.geometries.Point; import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.GeometryCollection; +import org.apache.sis.geometries.curve.Line; import org.apache.sis.geometries.curve.LineString; -import org.apache.sis.geometries.internal.shared.ArrayDataPoints; +import org.apache.sis.geometries.curve.Ray; import org.apache.sis.geometries.internal.shared.DefaultDataPoints; import org.apache.sis.geometries.internal.shared.DefaultNurbCurve; import org.apache.sis.geometries.internal.shared.DefaultNurbSurface; @@ -41,6 +49,7 @@ import org.apache.sis.geometries.surface.PreparedTIN; import org.apache.sis.geometries.surface.Triangle; import org.apache.sis.maths.Array; import org.apache.sis.maths.Cursor; +import org.apache.sis.maths.DataType; import org.apache.sis.maths.Maths; import org.apache.sis.maths.Matrices; import org.apache.sis.maths.NDArrays; @@ -55,16 +64,12 @@ import static org.apache.sis.maths.Maths.clamp; */ public final class Intersection { - private Intersection(){} - - - static Vector<?> subtract(final Vector<?> a, final Vector<?> b) { - return a.subtract(b); - } + /** + * Tolerance of the ray operations on geometries, relative to the largest absolute coordinate of the operands. + */ + private static final double RAY_TOLERANCE = 1E-10; - static Vector<?> scale(final Vector<?> a, final double s) { - return a.scale(s); - } + private Intersection(){} /** * Inherit attributes from TIN. @@ -757,6 +762,358 @@ public final class Intersection { return contact.edgeOfFirst() ? List.of(onEdge, onOther) : List.of(onOther, onEdge); } + /** + * Part of a ray touching another geometry, as positions along the ray. + * The position <var>t</var> ≥ 0 is the point <var>origin</var> + <var>t</var>⋅<var>direction</var>. + * + * @param start smallest position along the ray, or NaN if the ray touches nothing. + * @param end largest position along the ray, equal to {@code start} for a single point, + * or infinite for a ray lying in a plane. + * + * @see #rayPlane(Vector, Vector, Vector, Vector, double) + * @see #rayTriangle(Vector, Vector, Vector, Vector, Vector, double) + * @see #raySegment(Vector, Vector, Vector, Vector, double) + */ + public record RayInterval(double start, double end) { + /** + * The interval of a ray which touches nothing. + */ + public static final RayInterval NONE = new RayInterval(Double.NaN, Double.NaN); + + static RayInterval point(final double t) { + return new RayInterval(t, t); + } + + /** + * Returns whether the ray touches nothing. + * + * @return whether this interval is empty. + */ + public boolean isEmpty() { + return Double.isNaN(start); + } + + /** + * Returns whether the ray touches a single point. + * + * @return whether this interval is a single position. + */ + public boolean isPoint() { + return start == end; + } + } + + /** + * Computes where a ray crosses a hyperplane, in any number of dimensions. + * + * <ul> + * <li>A ray starting within the tolerance of the hyperplane touches it at its origin, + * or lies in it if the direction is parallel to the hyperplane.</li> + * </ul> + * + * @param origin start of the ray. + * @param direction direction of the ray, not zero. + * @param point a position of the hyperplane. + * @param normal normal of the hyperplane, not zero. + * @param tolerance largest distance to the hyperplane for considering the origin on it. + * @return the crossing point, the whole ray if it lies in the hyperplane, or nothing. + * @throws IllegalArgumentException if the direction or the normal is zero. + */ + public static RayInterval rayPlane(Vector<?> origin, Vector<?> direction, Vector<?> point, Vector<?> normal, double tolerance) { + ensureNonZero(direction, "direction"); + ensureNonZero(normal, "normal"); + final double a = normal.dot(origin.copy(DataType.DOUBLE).subtract(point)); + final double b = normal.dot(direction); + if (Math.abs(a) <= tolerance * normal.length()) { + return (b == 0) ? new RayInterval(0, Double.POSITIVE_INFINITY) : RayInterval.point(0); + } + return (b != 0 && -a / b >= 0) ? RayInterval.point(-a / b) : RayInterval.NONE; + } + + /** + * Computes where a ray touches a triangle, in 3 dimensions. The ray is cut to a segment going beyond + * the triangle, which is given to {@link #segmentTriangle segmentTriangle(…)} for robust side tests. + * + * <ul> + * <li>A ray in the plane of the triangle gives the part of the ray inside the triangle.</li> + * <li>A degenerate triangle, whose corners are aligned, is never touched.</li> + * </ul> + * + * @param origin start of the ray. + * @param direction direction of the ray, not zero. + * @param a first corner of the triangle. + * @param b second corner of the triangle. + * @param c third corner of the triangle. + * @param tolerance largest distance to the plane of the triangle for considering a position on it. + * @return the part of the ray touching the triangle. + * @throws IllegalArgumentException if the direction is zero. + */ + public static RayInterval rayTriangle(Vector<?> origin, Vector<?> direction, Vector<?> a, Vector<?> b, Vector<?> c, double tolerance) { + final double length = reach(origin, direction, a, b, c); + final double[] s0 = origin.toArrayDouble(); + final double[] s1 = along(origin, direction, length).toArrayDouble(); + final double[] pa = a.toArrayDouble(), pb = b.toArrayDouble(), pc = c.toArrayDouble(); + final SegmentTriangle hit = segmentTriangle(s0, s1, pa, pb, pc, tolerance); + return switch (hit.type()) { + case NONE -> RayInterval.NONE; + case COPLANAR -> { + final double[] normal = normal(pa, pb, pc); + if (Vectors.lengthSquare(normal) == 0) { + yield RayInterval.NONE; + } + final List<SegmentTriangle> ends = segmentInTriangle2D(s0, s1, pa, pb, pc, Maths.projectionAxes(normal)); + yield ends.isEmpty() ? RayInterval.NONE + : new RayInterval(ends.getFirst().ratio() * length, ends.getLast().ratio() * length); + } + default -> RayInterval.point(hit.ratio() * length); + }; + } + + /** + * Computes where a ray touches a line segment, in any number of dimensions. + * + * <ul> + * <li>The ray touches the segment where their distance is within the tolerance.</li> + * <li>A segment whose two ends are within the tolerance of the line of the ray + * gives the part of the ray along the segment.</li> + * </ul> + * + * @param origin start of the ray. + * @param direction direction of the ray, not zero. + * @param start start of the segment. + * @param end end of the segment. + * @param tolerance largest distance between the ray and the segment for considering that they touch. + * @return the part of the ray touching the segment. + * @throws IllegalArgumentException if the direction is zero. + */ + public static RayInterval raySegment(Vector<?> origin, Vector<?> direction, Vector<?> start, Vector<?> end, double tolerance) { + final double squaredLength = ensureNonZero(direction, "direction"); + final double t0 = direction.dot(start.copy(DataType.DOUBLE).subtract(origin)) / squaredLength; + final double t1 = direction.dot(end .copy(DataType.DOUBLE).subtract(origin)) / squaredLength; + final double high = Math.max(t0, t1); + if (high > 0 && along(origin, direction, t0).distance(start) <= tolerance + && along(origin, direction, t1).distance(end) <= tolerance) + { + return new RayInterval(Math.max(Math.min(t0, t1), 0), high); + } + /* + * The point of the ray nearest to the segment is not farther than the projections of the segment ends, + * so a part of the ray longer than those projections is enough for computing the distance. + */ + final double length = 2 * Math.max(high, 0) + 1; + final int dimension = origin.getDimension(); + final double[] ratios = new double[2]; + final double distance = Distance.distanceSquare( + origin.toArrayDouble(), along(origin, direction, length).toArrayDouble(), new double[dimension], + start.toArrayDouble(), end.toArrayDouble(), new double[dimension], ratios, 0); + return (distance <= tolerance * tolerance) ? RayInterval.point(ratios[0] * length) : RayInterval.NONE; + } + + /** + * Returns the squared length of the given vector. + * + * @throws IllegalArgumentException if the vector is zero. + */ + private static double ensureNonZero(final Vector<?> vector, final String name) { + final double squaredLength = vector.lengthSquare(); + if (!(squaredLength > 0)) { + throw new IllegalArgumentException("The " + name + " is zero."); + } + return squaredLength; + } + + /** + * Returns a position along the ray beyond the projections of all the given positions on the ray. + */ + private static double reach(final Vector<?> origin, final Vector<?> direction, final Vector<?>... positions) { + final double squaredLength = ensureNonZero(direction, "direction"); + double reach = 0; + for (final Vector<?> position : positions) { + reach = Math.max(reach, direction.dot(position.copy(DataType.DOUBLE).subtract(origin)) / squaredLength); + } + return 2 * reach + 1; + } + + /** + * Returns the point at the given position along the ray, in double precision. + */ + private static Vector<?> along(final Vector<?> origin, final Vector<?> direction, final double t) { + return origin.copy(DataType.DOUBLE).combine(1, direction, t); + } + + /** + * A part of a geometry touched by a ray, with its positions along the ray. + */ + private record RayHit(double start, double end, Geometry geometry) { + /** + * Returns the point or the line string touched by the ray, as built by the given function from positions + * along the ray, or an empty list if the ray touches nothing. + */ + static List<RayHit> of(final RayInterval interval, final DoubleFunction<Point> pointAt) { + if (interval.isEmpty()) { + return List.of(); + } + final Point start = pointAt.apply(interval.start()); + final Geometry geometry = interval.isPoint() ? start : line(start, pointAt.apply(interval.end())); + return List.of(new RayHit(interval.start(), interval.end(), geometry)); + } + } + + /** + * 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, + * interpolated where the ray touches it. The tolerance is {@value #RAY_TOLERANCE} times the largest + * absolute coordinate of the operands. + * + * @param ray the ray. + * @param other the geometry to intersect with the ray. + * @return the intersection, or {@code null} if the type of the other geometry is not supported. + * @throws OperationException if the geometries are not in the same reference system, + * or if the intersection with that geometry cannot be computed. + */ + static Geometry rayIntersection(final Ray ray, final Geometry other) throws OperationException { + final List<RayHit> hits; + switch (other) { + case HyperPlane plane -> hits = planeHits(ray, plane, plane.getPosition(), plane.getNormal()); + case Plane plane when !plane.isQuad() -> hits = planeHits(ray, plane, plane.getPosition(), plane.getNormal()); + case Plane plane -> throw new OperationException("The intersection of a ray with a plane of finite size is not supported."); + 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()); + default -> { + return null; + } + } + final CoordinateReferenceSystem crs = ray.getCoordinateReferenceSystem(); + final GeometryFactory factory = GeometryFactory.DEFAULT; + if (hits.isEmpty()) { + return factory.createEmpty(crs); + } + if (hits.size() == 1) { + return hits.getFirst().geometry(); + } + final Geometry[] parts = hits.stream().sorted(Comparator.comparingDouble(RayHit::start)) + .map(RayHit::geometry).toArray(Geometry[]::new); + if (Arrays.stream(parts).allMatch(Point.class::isInstance)) { + return factory.createMultiPoint(crs, Arrays.copyOf(parts, parts.length, Point[].class)); + } + return factory.createGeometryCollection(GeometryType.GEOMETRY, crs, parts); + } + + /** + * Intersection of a ray with a hyperplane: a point without attribute other than its position, or a copy of the ray. + */ + private static List<RayHit> planeHits(final Ray ray, final Geometry plane, final Vector<?> point, final Vector<?> normal) { + ProcessorUtils.ensureSameCRS(ray, plane); + final Vector<?> origin = ray.getPosition(); + final Vector<?> direction = ray.getDirection(); + final RayInterval hit = rayPlane(origin, direction, point, normal, rayTolerance(origin, point)); + if (hit.isEmpty()) { + return List.of(); + } + final CoordinateReferenceSystem crs = ray.getCoordinateReferenceSystem(); + final Geometry geometry = hit.isPoint() + ? GeometryFactory.DEFAULT.createPoint(crs, along(origin, direction, hit.start()).toArrayDouble()) + : GeometryFactory.DEFAULT.createRay(crs, origin, direction); + return List.of(new RayHit(hit.start(), hit.end(), geometry)); + } + + /** + * Intersection of a ray with a triangle: a point or a line string, with attributes interpolated between the corners. + */ + private static List<RayHit> triangleHits(final Ray ray, final Triangle triangle) { + ProcessorUtils.ensureSameCRS(ray, triangle); + final DataPoints corners = triangle.getExteriorRing().getDataPoints(); + if (corners.getDimension() != 3) { + throw new OperationException("The intersection of a ray with a triangle is computed in 3 dimensions only."); + } + final Vector<?> origin = ray.getPosition(); + final Vector<?> direction = ray.getDirection(); + final Vector<?> a = corners.getPosition(0); + final Vector<?> b = corners.getPosition(1); + final Vector<?> c = corners.getPosition(2); + final RayInterval hit = rayTriangle(origin, direction, a, b, c, rayTolerance(origin, a, b, c)); + return RayHit.of(hit, (t) -> corners.getInterpolatedPoint(new int[] {0, 1, 2}, + triangle.getBarycentricValue(along(origin, direction, t)))); + } + + /** + * Intersection of a ray with each segment of a polyline, with attributes interpolated along the segments. + * A ray crossing a vertex touches the two segments sharing it, and a ray along a segment touches the ends + * of the adjacent segments: the points already in another part of the intersection are removed. + */ + private static List<RayHit> polylineHits(final Ray ray, final Geometry line, final DataPoints points) { + ProcessorUtils.ensureSameCRS(ray, line); + final Vector<?> origin = ray.getPosition(); + final Vector<?> direction = ray.getDirection(); + final int count = points.size(); + final Vector<?>[] positions = new Vector<?>[count + 1]; + for (int i = 0; i < count; i++) { + positions[i] = points.getPosition(i); + } + positions[count] = origin; + final double tolerance = rayTolerance(positions); + final List<RayHit> hits = new ArrayList<>(); + for (int i = 1; i < count; i++) { + final int end = i; + final RayInterval hit = raySegment(origin, direction, positions[i - 1], positions[i], tolerance); + hits.addAll(RayHit.of(hit, (t) -> pointOnSegment(points, end, along(origin, direction, t)))); + } + final double margin = tolerance / direction.length(); + final List<RayHit> kept = new ArrayList<>(); + for (final RayHit hit : hits) { + if (!(hit.geometry() instanceof Point)) { + kept.add(hit); + } + } + for (final RayHit hit : hits) { + if (hit.geometry() instanceof Point && kept.stream().noneMatch( + (other) -> hit.start() >= other.start() - margin && hit.start() <= other.end() + margin)) + { + kept.add(hit); + } + } + return kept; + } + + /** + * Returns the point of the segment ending at the given index which is nearest to the given position, + * with the attributes of the polyline interpolated at that point. + */ + private static Point pointOnSegment(final DataPoints points, final int end, final Vector<?> position) { + final Vector<?> start = points.getPosition(end - 1).copy(DataType.DOUBLE); + final Vector<?> segment = points.getPosition(end).copy(DataType.DOUBLE).subtract(start); + final double squaredLength = segment.lengthSquare(); + final double ratio = (squaredLength > 0) ? clamp(segment.dot(position.subtract(start)) / squaredLength, 0, 1) : 0; + return points.getInterpolatedPoint(end - 1, end, ratio); + } + + /** + * Returns a line string between the two given points, with their attributes. + */ + private static LineString line(final Point start, final Point end) { + final DataPointsType type = start.getDataPointsType(); + final DataPoints points = GeometryFactory.DEFAULT.createDataPoints(type, 2); + for (final String name : type.getAttributeNames()) { + points.setAttribute(0, name, start.getAttribute(name)); + points.setAttribute(1, name, end.getAttribute(name)); + } + return GeometryFactory.DEFAULT.createLineString(points); + } + + /** + * Returns the tolerance of the ray operations on geometries, relative to the largest absolute coordinate. + */ + private static double rayTolerance(final Vector<?>... positions) { + double scale = 0; + for (final Vector<?> position : positions) { + for (int i = 0; i < position.getDimension(); i++) { + scale = Math.max(scale, Math.abs(position.get(i))); + } + } + return scale * RAY_TOLERANCE; + } + /** * Intersection operations on BSpline curves and surfaces. */ @@ -825,10 +1182,10 @@ public final class Intersection { for (int iter = 0; iter < 10; iter++) { final Vector<?> c1u = c1.evaluate(u1), c2u = c2.evaluate(u2); - final Vector<?> f = subtract(c1u, c2u); + final Vector<?> f = c1u.subtract(c2u); final Vector<?> d1 = numericDerivative(c1, u1); - final Vector<?> d2 = scale(numericDerivative(c2, u2), -1); + final Vector<?> d2 = numericDerivative(c2, u2).scale(-1); final double a = d1.dot(d1), b = d1.dot(d2), cc = d2.dot(d2); final double rhs0 = -d1.dot(f), rhs1 = -d2.dot(f); @@ -845,7 +1202,7 @@ public final class Intersection { } final Vector<?> p1 = c1.evaluate(u1), p2 = c2.evaluate(u2); - final double dist = subtract(p1, p2).length(); + final double dist = p1.subtract(p2).length(); if (dist > Math.max(tol, 1e-6)) { return; } @@ -867,9 +1224,9 @@ public final class Intersection { final double uPlus = Math.min(u + h, hi), uMinus = Math.max(u - h, lo); final double denom = uPlus - uMinus; if (denom == 0) { - return scale(curve.evaluate(u), 0); + return curve.evaluate(u).scale(0); } - return scale(subtract(curve.evaluate(uPlus), curve.evaluate(uMinus)), 1.0 / denom); + return curve.evaluate(uPlus).subtract(curve.evaluate(uMinus)).scale(1.0 / denom); } @@ -955,12 +1312,12 @@ public final class Intersection { final double lambda = 1e-8; // Tikhonov regularization (under-determined system) for (int iter = 0; iter < 15; iter++) { - final Vector<?> f = subtract(s1.evaluate(u1, v1), s2.evaluate(u2, v2)); + final Vector<?> f = s1.evaluate(u1, v1).subtract(s2.evaluate(u2, v2)); final Vector<?> dU1 = numericDerivativeU(s1, u1, v1); final Vector<?> dV1 = numericDerivativeV(s1, u1, v1); - final Vector<?> dU2 = scale(numericDerivativeU(s2, u2, v2), -1); - final Vector<?> dV2 = scale(numericDerivativeV(s2, u2, v2), -1); + final Vector<?> dU2 = numericDerivativeU(s2, u2, v2).negate(); + final Vector<?> dV2 = numericDerivativeV(s2, u2, v2).negate(); final Vector<?>[] cols = {dU1, dV1, dU2, dV2}; final double[][] JtJ = new double[4][4]; @@ -984,14 +1341,14 @@ public final class Intersection { } final Vector<?> p1 = s1.evaluate(u1, v1), p2 = s2.evaluate(u2, v2); - final double dist = subtract(p1, p2).length(); + final double dist = p1.subtract(p2).length(); if (dist > Math.max(tol, 1e-6)) { return; } final double mergeTol = Math.max(tol * 5, 1e-4); final boolean duplicate = results.stream().anyMatch(r - -> subtract(r.point, p1).length() < mergeTol); + -> r.point.subtract(p1).length() < mergeTol); if (!duplicate) { results.add(new SurfaceIntersectionPoint(u1, v1, u2, v2, p1)); } @@ -1006,9 +1363,9 @@ public final class Intersection { final double uPlus = Math.min(u + h, hi), uMinus = Math.max(u - h, lo); final double denom = uPlus - uMinus; if (denom == 0) { - return scale(s.evaluate(u, v), 0); + return s.evaluate(u, v).scale(0); } - return scale(subtract(s.evaluate(uPlus, v), s.evaluate(uMinus, v)), 1.0 / denom); + return s.evaluate(uPlus, v).subtract(s.evaluate(uMinus, v)).scale(1.0 / denom); } /** @@ -1020,9 +1377,9 @@ public final class Intersection { final double vPlus = Math.min(v + h, hi), vMinus = Math.max(v - h, lo); final double denom = vPlus - vMinus; if (denom == 0) { - return scale(s.evaluate(u, v), 0); + return s.evaluate(u, v).scale(0); } - return scale(subtract(s.evaluate(u, vPlus), s.evaluate(u, vMinus)), 1.0 / denom); + return s.evaluate(u, vPlus).subtract(s.evaluate(u, vMinus)).scale(1.0 / denom); } /** 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 21b9d1cc63..cc58634b46 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 @@ -210,6 +210,38 @@ public sealed interface Triangle extends Polygon return getExteriorRing().getDataPoints().viewInterpolatedPoint(new int[] {0, 1, 2}, weights); } + /** + * Returns the barycentric weights of the given position relative to the three corners of this triangle. + * + * <ul> + * <li>Computed in all dimensions of the coordinate system. A position outside the plane + * of this triangle gets the weights of its projection on that plane.</li> + * <li>The weights sum to 1. Some of them are negative if the position is outside this triangle.</li> + * <li>The weights are NaN if this triangle is degenerate.</li> + * </ul> + * + * @param position the position for which to compute the weights. + * @return barycentric weight of each corner, in the order of the corners. + * + * @see #interpolate(double[]) + */ + default double[] getBarycentricValue(Vector<?> position) { + final DataPoints corners = getExteriorRing().getDataPoints(); + final Vector<?> origin = corners.getPosition(0).copy(DataType.DOUBLE); + final Vector<?> edge1 = corners.getPosition(1).copy(DataType.DOUBLE).subtract(origin); + final Vector<?> edge2 = corners.getPosition(2).copy(DataType.DOUBLE).subtract(origin); + final Vector<?> offset = position.copy(DataType.DOUBLE).subtract(origin); + final double d11 = edge1.dot(edge1); + final double d12 = edge1.dot(edge2); + final double d22 = edge2.dot(edge2); + final double p1 = offset.dot(edge1); + final double p2 = offset.dot(edge2); + final double denominator = d11 * d22 - d12 * d12; + final double v = (d22 * p1 - d12 * p2) / denominator; + final double w = (d11 * p2 - d12 * p1) / denominator; + return new double[] {1 - v - w, v, w}; + } + /** * Test if point is inside triangle and return it's barycentric weights. * This method first test a bounding box intersection then using side algorithm diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java index faea9f74f1..782d04a2fd 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/maths/Maths.java @@ -896,20 +896,6 @@ public final class Maths { return r; } - /** - * Test if a point is inside given triangle. - * - * @param a first triangle point - * @param b second triangle point - * @param c third triangle point - * @param p point to test - * @return true if point is inside triangle - */ - public static boolean inTriangle(double[] a, double[] b, double[] c, double[] p){ - final double[] bary = getBarycentricValue(a, b, c, p); - return bary[1] >= 0.0 && bary[2] >= 0.0 && (bary[1] + bary[2]) <= 1.0; - } - /** * Test if a point is inside given triangle, its edges and corners included. Only X,Y ordinates are used. * Robust variant : the side of the point compared to each edge is computed with @@ -938,31 +924,6 @@ public final class Maths { && y >= Math.min(a.get(1), Math.min(b.get(1), c.get(1))) && y <= Math.max(a.get(1), Math.max(b.get(1), c.get(1))); } - /** - * Calculate the barycentric value in triangle for given point. - * - * @param a first triangle point - * @param b second triangle point - * @param c third triangle point - * @param p point to test - * @return Vector barycentric values - */ - public static double[] getBarycentricValue(final double[] a, final double[] b, final double[] c, final double[] p){ - final double[] v0 = Vectors.subtract(b, a); - final double[] v1 = Vectors.subtract(c, a); - final double[] v2 = Vectors.subtract(p, a); - final double d00 = dot(v0, v0); - final double d01 = dot(v0,v1); - final double d11 = dot(v1,v1); - final double d20 = dot(v2,v0); - final double d21 = dot(v2,v1); - final double denom = d00 * d11 - d01 * d01; - final double v = (d11 * d20 - d01 * d21) / denom; - final double w = (d00 * d21 - d01 * d20) / denom; - final double u = 1.0f - v - w; - return new double[]{u, v, w}; - } - /** * Calculate constant D of a plan. * Same as normal.dot(point). 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 b3dfb09a18..e354b175d6 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 @@ -18,18 +18,29 @@ package org.apache.sis.geometries.operation; import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.apache.sis.geometries.Curve; +import org.apache.sis.geometries.DataPoints; +import org.apache.sis.geometries.DataPointsType; import org.apache.sis.geometries.Empty; +import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.Geometry; import org.apache.sis.geometries.GeometryCollection; import org.apache.sis.geometries.GeometryFactory; +import org.apache.sis.geometries.HyperPlane; import org.apache.sis.geometries.Point; import org.apache.sis.geometries.Surface; import org.apache.sis.geometries.adapter.WellKnownText; import org.apache.sis.geometries.curve.LineString; +import org.apache.sis.geometries.curve.Ray; import org.apache.sis.geometries.surface.Polygon; +import org.apache.sis.geometries.surface.Triangle; +import org.apache.sis.maths.Array; +import org.apache.sis.maths.NDArrays; +import org.apache.sis.maths.Vector; import org.apache.sis.referencing.CommonCRS; // Test dependencies @@ -54,6 +65,11 @@ public abstract class AbstractD9IMTest { */ public static final CoordinateReferenceSystem CRS_2D = CommonCRS.WGS84.geographic(); + /** + * The coordinate reference system of the three-dimensional geometries undefined. + */ + public static final CoordinateReferenceSystem CRS_3D = Geometries.getUndefinedCRS(3); + /** * Tolerance threshold on the measurements computed by the operations. * For floating point arithmetic errors. @@ -277,6 +293,47 @@ public abstract class AbstractD9IMTest { public static final GeometryCollection<?> COLLECTION = (GeometryCollection<?>) wkt("GEOMETRYCOLLECTION (POINT (5 5), LINESTRING (0 0, 10 0))"); + /** + * Name of the attribute of {@link #TRIANGLE_3D} and {@link #RING_3D}, besides the position. + */ + public static final String ATT_TEST = "TEST"; + + /** + * A triangle in the plane <var>z</var> = 5, of corners (0 0 5), (4 0 5) and (0 4 5) + * with the {@value #ATT_TEST} attribute values 10, 20 and 30. + */ + public static final Triangle TRIANGLE_3D = GeometryFactory.DEFAULT.createTriangle(GeometryFactory.DEFAULT.createLinearRing( + attributed(new double[] {0,0,5, 4,0,5, 0,4,5, 0,0,5}, 10, 20, 30, 10))); + + /** + * A closed square of side 4 in the plane <var>z</var> = 1, of corners (0 0 1), (4 0 1), (4 4 1) and (0 4 1) + * with the {@value #ATT_TEST} attribute values 0, 10, 20 and 40. + */ + public static final LineString RING_3D = GeometryFactory.DEFAULT.createLineString( + attributed(new double[] {0,0,1, 4,0,1, 4,4,1, 0,4,1, 0,0,1}, 0, 10, 20, 40, 0)); + + /** + * The hyperplane <var>z</var> = 5. + */ + public static final HyperPlane PLANE_Z5 = new HyperPlane(CRS_3D, Vector.of(0, 0, 5), Vector.of(0, 0, 1)); + + /** + * Creates data points in {@link #CRS_3D} with the given {@value #ATT_TEST} attribute values. + */ + private static DataPoints attributed(final double[] positions, final double... values) { + final Map<String,Array> attributes = new LinkedHashMap<>(); + attributes.put(DataPointsType.ATT_POSITION, NDArrays.of(CRS_3D, positions)); + attributes.put(ATT_TEST, NDArrays.of(1, values)); + return GeometryFactory.DEFAULT.createDataPoints(attributes); + } + + /** + * Creates a ray in {@link #CRS_3D} from the given origin toward the given direction. + */ + public static Ray ray(double x, double y, double z, double dx, double dy, double dz) { + return GeometryFactory.DEFAULT.createRay(CRS_3D, Vector.of(x, y, z), Vector.of(dx, dy, dz)); + } + /** * Decodes the given Well-Known Text in the {@link #CRS_2D} reference system. * The geometries of this class are declared this way so that the shape under 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 b28fe2cfaf..46f1c8c909 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 @@ -17,17 +17,26 @@ package org.apache.sis.geometries.operation; import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.GeometryCollection; +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.SampleSystem; +import org.apache.sis.maths.Vector; import org.apache.sis.geometries.mesh.MeshPrimitive; +import org.apache.sis.geometries.operation.Intersection.RayInterval; 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.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -211,4 +220,172 @@ public class IntersectionTest extends AbstractD9IMTest { assertEquals(1, edge.getTopologicDimension(), "What two surfaces sharing only an edge have in common is a curve, not a surface."); } + private static Vector<?> v(final double... coordinates) { + return Vector.of(coordinates); + } + + private static void assertRayPoint(final double t, final RayInterval actual) { + assertTrue(actual.isPoint(), "Expected a single point."); + assertEquals(t, actual.start(), TOLERANCE); + } + + private static void assertRayInterval(final double start, final double end, final RayInterval actual) { + assertFalse(actual.isEmpty(), "Expected an interval."); + assertEquals(start, actual.start(), TOLERANCE); + assertEquals(end, actual.end(), TOLERANCE); + } + + /** + * Verifies the position and the {@value #ATT_TEST} attribute of the given point. + */ + private static void assertAttributedPoint(double x, double y, double z, double attribute, Geometry actual) { + final Point point = assertInstanceOf(Point.class, actual); + assertArrayEquals(new double[] {x, y, z}, point.getPosition().toArrayDouble(), TOLERANCE); + assertEquals(attribute, point.getAttribute(ATT_TEST).get(0), TOLERANCE, "attribute"); + } + + /** + * Tests {@link Intersection#rayPlane(Vector, Vector, Vector, Vector, double)} in 3 and 2 dimensions. + */ + @Test + public void testRayPlane() { + final Vector<?> origin = v(0, 0, 0); + final Vector<?> plane = v(0, 0, 5); + final Vector<?> up = v(0, 0, 2); // Not a unit vector, on purpose. + assertRayPoint(5, Intersection.rayPlane(origin, v(0, 0, 1), plane, up, 0)); + assertRayPoint(2.5, Intersection.rayPlane(origin, v(0, 0, 2), plane, up, 0)); + assertRayPoint(5, Intersection.rayPlane(origin, v(1, 0, 1), plane, up, 0)); + assertTrue(Intersection.rayPlane(origin, v(0, 0, -1), plane, up, 0).isEmpty(), "Plane behind the ray."); + assertTrue(Intersection.rayPlane(origin, v(1, 0, 0), plane, up, 0).isEmpty(), "Parallel to the plane."); + + final Vector<?> inside = v(3, 2, 5); + assertRayPoint(0, Intersection.rayPlane(inside, v(0, 0, 1), plane, up, 0)); + assertRayInterval(0, Double.POSITIVE_INFINITY, Intersection.rayPlane(inside, v(1, 1, 0), plane, up, 0)); + assertRayPoint(0, Intersection.rayPlane(v(3, 2, 5 + 1E-12), v(0, 0, 1), plane, up, 1E-10)); + + assertRayPoint(3, Intersection.rayPlane(v(0, 0), v(1, 0), v(3, 7), v(-1, 0), 0)); + assertThrows(IllegalArgumentException.class, () -> Intersection.rayPlane(origin, v(0, 0, 0), plane, up, 0)); + assertThrows(IllegalArgumentException.class, () -> Intersection.rayPlane(origin, up, plane, v(0, 0, 0), 0)); + } + + /** + * Tests {@link Intersection#rayTriangle(Vector, Vector, Vector, Vector, Vector, double)} with a ray + * crossing a triangle inside, on an edge, on a corner, or not at all. + */ + @Test + public void testRayTriangle() { + final Vector<?> a = v(0, 0, 5), b = v(4, 0, 5), c = v(0, 4, 5); + final Vector<?> up = v(0, 0, 2); + assertRayPoint(2.5, Intersection.rayTriangle(v(1, 1, 0), up, a, b, c, 0)); + assertRayPoint(2.5, Intersection.rayTriangle(v(2, 0, 0), up, a, b, c, 0)); // On an edge. + assertRayPoint(2.5, Intersection.rayTriangle(v(0, 0, 0), up, a, b, c, 0)); // On a corner. + assertRayPoint(2.5, Intersection.rayTriangle(v(2, 2, 0), up, a, b, c, 0)); // On the hypotenuse. + assertRayPoint(0, Intersection.rayTriangle(v(1, 1, 5), up, a, b, c, 0)); // Starting on the triangle. + assertRayPoint(5, Intersection.rayTriangle(v(-4, 1, 0), v(1, 0, 1), a, b, c, 0)); + assertTrue(Intersection.rayTriangle(v(3, 3, 0), up, a, b, c, 0).isEmpty(), "Outside the triangle."); + assertTrue(Intersection.rayTriangle(v(1, 1, 0), v(0, 0, -1), a, b, c, 0).isEmpty(), "Triangle behind the ray."); + assertTrue(Intersection.rayTriangle(v(1, 1, 6), up, a, b, c, 0).isEmpty(), "Ray starting after the triangle."); + /* + * Triangles which are not parallel to a coordinate plane. + */ + assertRayPoint(1, Intersection.rayTriangle(v(0, 0, 0), v(1, 1, 1), v(3, 0, 0), v(0, 3, 0), v(0, 0, 3), 0)); + assertRayPoint(2, Intersection.rayTriangle(v(0, 0, 0), v(1, 0, 0.25), v(2, -1, 0), v(2, 1, -1), v(2, 0, 2), 0)); + assertTrue(Intersection.rayTriangle(v(0, 0, 0), v(1, 0, 1.5), v(2, -1, 0), v(2, 1, -1), v(2, 0, 2), 0).isEmpty()); + } + + /** + * Tests {@link Intersection#rayTriangle(Vector, Vector, Vector, Vector, Vector, double)} with a ray + * in the plane of the triangle, which touches it along a segment or not at all. + */ + @Test + public void testRayTriangleCoplanar() { + final Vector<?> a = v(0, 0, 5), b = v(4, 0, 5), c = v(0, 4, 5); + final Vector<?> east = v(1, 0, 0); + assertRayInterval(1, 4, Intersection.rayTriangle(v(-1, 1, 5), east, a, b, c, 0)); + assertRayInterval(0, 2, Intersection.rayTriangle(v( 1, 1, 5), east, a, b, c, 0)); + assertTrue(Intersection.rayTriangle(v(5, 1, 5), east, a, b, c, 0).isEmpty(), "Triangle behind the ray."); + assertTrue(Intersection.rayTriangle(v(0, 0, 0), east, a, b, c, 0).isEmpty(), "Parallel, not coplanar."); + assertTrue(Intersection.rayTriangle(v(0, 0, 0), east, a, b, v(8, 0, 5), 0).isEmpty(), "Degenerate triangle."); + } + + /** + * Tests {@link Intersection#raySegment(Vector, Vector, Vector, Vector, double)} with segments + * crossing the ray, along the ray, or not touching it. + */ + @Test + public void testRaySegment() { + final Vector<?> origin = v(0, 0, 0); + final Vector<?> east = v(1, 0, 0); + assertRayPoint(2, Intersection.raySegment(origin, east, v(2, -1, 0), v(2, 1, 0), 1E-12)); + assertRayPoint(1, Intersection.raySegment(origin, v(2, 0, 0), v(2, -1, 0), v(2, 1, 0), 1E-12)); + assertRayPoint(2, Intersection.raySegment(origin, east, v(2, -1, 1E-13), v(2, 1, 1E-13), 1E-12)); + assertTrue(Intersection.raySegment(origin, east, v(2, -1, 1), v(2, 1, 1), 1E-12).isEmpty(), "Skew segment."); + assertTrue(Intersection.raySegment(origin, east, v(-2, -1, 0), v(-2, 1, 0), 1E-12).isEmpty(), "Segment behind."); + assertTrue(Intersection.raySegment(origin, east, v(2, 1, 0), v(2, 3, 0), 1E-12).isEmpty(), "Segment beside."); + + assertRayInterval(3, 5, Intersection.raySegment(origin, east, v(5, 0, 0), v(3, 0, 0), 1E-12)); + assertRayInterval(0, 2, Intersection.raySegment(origin, east, v(-1, 0, 0), v(2, 0, 0), 1E-12)); + assertRayPoint(0, Intersection.raySegment(origin, east, v(-1, 0, 0), v(0, 0, 0), 1E-12)); + assertTrue(Intersection.raySegment(origin, east, v(-3, 0, 0), v(-1, 0, 0), 1E-12).isEmpty(), "Collinear behind."); + + assertRayPoint(3, Intersection.raySegment(v(0, 0), v(1, 1), v(0, 6), v(6, 0), 1E-12)); + } + + /** + * Tests the intersection of a ray with a triangle, the attributes of the triangle being interpolated. + */ + @Test + public void testIntersectionOfRayAndTriangle() { + final GeometryProcessor processor = new GeometryProcessor(); + assertAttributedPoint(1, 2, 5, 22.5, processor.intersection(ray(1, 2, 0, 0, 0, 1), TRIANGLE_3D)); + assertAttributedPoint(1, 2, 5, 22.5, processor.intersection(TRIANGLE_3D, ray(1, 2, 0, 0, 0, 1))); + assertTrue(processor.intersection(ray(3, 3, 0, 0, 0, 1), TRIANGLE_3D).isEmpty()); + + final LineString inside = assertInstanceOf(LineString.class, processor.intersection(ray(-1, 1, 5, 1, 0, 0), TRIANGLE_3D)); + assertEquals(2, inside.getNumPoints()); + assertAttributedPoint(0, 1, 5, 15, inside.getPointN(0)); + assertAttributedPoint(3, 1, 5, 22.5, inside.getPointN(1)); + } + + /** + * Tests the intersection of a ray with hyperplanes and planes. + */ + @Test + public void testIntersectionOfRayAndPlane() { + final GeometryProcessor processor = new GeometryProcessor(); + final Point point = assertInstanceOf(Point.class, processor.intersection(ray(1, 2, 0, 0, 1, 1), PLANE_Z5)); + assertArrayEquals(new double[] {1, 7, 5}, point.getPosition().toArrayDouble(), TOLERANCE); + assertInstanceOf(Ray.class, processor.intersection(ray(1, 2, 5, 1, 0, 0), PLANE_Z5)); + assertTrue(processor.intersection(ray(1, 2, 0, 0, 0, -1), PLANE_Z5).isEmpty()); + + final Plane plane = new Plane(CRS_3D, v(0, 0, 5), v(0, 0, 1)); + final Point onPlane = assertInstanceOf(Point.class, processor.intersection(ray(1, 2, 0, 0, 0, 1), plane)); + assertArrayEquals(new double[] {1, 2, 5}, onPlane.getPosition().toArrayDouble(), TOLERANCE); + } + + /** + * Tests the intersection of a ray with a closed line string, which the ray crosses inside two segments, + * through two corners including the closing one, or along a segment. The attributes are interpolated. + */ + @Test + public void testIntersectionOfRayAndLineString() { + final GeometryProcessor processor = new GeometryProcessor(); + final GeometryCollection<?> crossings = assertInstanceOf(GeometryCollection.class, + processor.intersection(ray(-1, 2, 1, 1, 0, 0), RING_3D)); + assertEquals(2, crossings.getNumGeometries()); + assertAttributedPoint(0, 2, 1, 20, crossings.getGeometryN(0)); + assertAttributedPoint(4, 2, 1, 15, crossings.getGeometryN(1)); + + final GeometryCollection<?> corners = assertInstanceOf(GeometryCollection.class, + processor.intersection(ray(-1, -1, 1, 1, 1, 0), RING_3D)); + assertEquals(2, corners.getNumGeometries()); + assertAttributedPoint(0, 0, 1, 0, corners.getGeometryN(0)); + assertAttributedPoint(4, 4, 1, 20, corners.getGeometryN(1)); + + final LineString along = assertInstanceOf(LineString.class, processor.intersection(ray(-1, 0, 1, 1, 0, 0), RING_3D)); + assertAttributedPoint(0, 0, 1, 0, along.getPointN(0)); + assertAttributedPoint(4, 0, 1, 10, along.getPointN(1)); + + assertTrue(processor.intersection(ray(-1, 2, 2, 1, 0, 0), RING_3D).isEmpty()); + } } 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 e624e95fa1..57e06e5bca 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 @@ -17,6 +17,8 @@ package org.apache.sis.geometries.operation; import org.apache.sis.geometries.Geometry; +import org.apache.sis.geometries.Plane; +import org.apache.sis.maths.Vector; // Test dependencies import static org.junit.jupiter.api.Assertions.assertEquals; @@ -98,9 +100,32 @@ public class IntersectsTest extends AbstractD9IMTest { new TestCase(MULTI_POINT_SPREAD, SQUARE, true, null), new TestCase(MULTI_LINE, SQUARE, true, null), new TestCase(MULTI_POLYGON, SQUARE_INNER, true, null), - new TestCase(COLLECTION, SQUARE, true, null) + new TestCase(COLLECTION, SQUARE, true, null), + /* + * A ray and a triangle, a hyperplane or a line string, in either order. The ray touches + * the triangle inside, outside, or starts after it. A plane of finite size is not supported. + */ + new TestCase(ray(1, 2, 0, 0, 0, 1), TRIANGLE_3D, true, null), + new TestCase(TRIANGLE_3D, ray(1, 2, 0, 0, 0, 1), true, null), + new TestCase(ray(3, 3, 0, 0, 0, 1), TRIANGLE_3D, false, null), + new TestCase(ray(1, 2, 6, 0, 0, 1), TRIANGLE_3D, false, null), + new TestCase(ray(1, 2, 0, 0, 1, 1), PLANE_Z5, true, null), + new TestCase(ray(1, 2, 0, 0, 0, -1), PLANE_Z5, false, null), + 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) }; + /** + * Returns the square of size 2 × 2 centered on (0 0 5) in the plane <var>z</var> = 5. + */ + private static Plane quad() { + final Plane plane = new Plane(CRS_3D, Vector.of(0, 0, 5), Vector.of(0, 0, 1)); + plane.setSizeX(2); + plane.setSizeZ(2); + return plane; + } + /** * Tests {@code intersects(Geometry, Geometry)} on all declared test cases. */ diff --git a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/TriangleTest.java b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/TriangleTest.java index d394f4755f..26cf76f50e 100644 --- a/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/TriangleTest.java +++ b/incubator/src/org.apache.sis.geometry/test/org/apache/sis/geometries/surface/TriangleTest.java @@ -17,10 +17,12 @@ package org.apache.sis.geometries.surface; import org.apache.sis.geometries.DataPoints; +import org.apache.sis.geometries.Geometries; import org.apache.sis.geometries.GeometryFactory; import org.apache.sis.geometries.curve.LinearRing; import org.apache.sis.maths.Array; import org.apache.sis.maths.NDArrays; +import org.apache.sis.maths.Vector; import org.apache.sis.referencing.CommonCRS; // Test dependencies @@ -220,4 +222,18 @@ public class TriangleTest extends GeometryTest { } } + + /** + * Tests {@link Triangle#getBarycentricValue(Vector)} on a triangle which is not parallel to an axis plane. + */ + @Test + public void testGetBarycentricValue() { + final GeometryFactory f = GeometryFactory.DEFAULT; + final Triangle triangle = f.createTriangle(f.createLinearRing( + f.createDataPoints(Geometries.getUndefinedCRS(3), 0,0,0, 4,0,4, 0,4,4, 0,0,0))); + assertArrayEquals(new double[] {1, 0, 0}, triangle.getBarycentricValue(Vector.of(0, 0, 0)), TOLERANCE); + assertArrayEquals(new double[] {0, 1, 0}, triangle.getBarycentricValue(Vector.of(4, 0, 4)), TOLERANCE); + assertArrayEquals(new double[] {0.5, 0.25, 0.25}, triangle.getBarycentricValue(Vector.of(1, 1, 2)), TOLERANCE); + assertArrayEquals(new double[] {-0.5, 0.75, 0.75}, triangle.getBarycentricValue(Vector.of(3, 3, 6)), TOLERANCE); + } }
