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 68a1ff9e104f0853668c8e25e492c237989124e4 Author: jsorel <[email protected]> AuthorDate: Wed Dec 2 10:09:27 2026 +0100 refactor(Geometry): remove the copies of vectors known to have double values --- .../main/org/apache/sis/geometries/OBBox.java | 13 +++++--- .../apache/sis/geometries/operation/Collision.java | 22 ++++++------- .../apache/sis/geometries/operation/Nearest.java | 37 +++++++++++++--------- 3 files changed, 41 insertions(+), 31 deletions(-) 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 f53b8d755c..25a9bebcf5 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 @@ -36,6 +36,9 @@ import org.apache.sis.measure.Quantities; public final class OBBox extends AbstractGeometry { private CoordinateReferenceSystem crs; + /* + * The center and the half axes always have double values, see ensure3D(Vector). + */ private Vector<?> center; private Vector<?> xAxis; private Vector<?> yAxis; @@ -56,10 +59,10 @@ public final class OBBox extends AbstractGeometry { throw new IllegalArgumentException("Oriented BBox only support 3 dimensions."); } this.crs = crs; - center = Vector.of(0,0,0); - xAxis = Vector.of(0,0,0); - yAxis = Vector.of(0,0,0); - zAxis = Vector.of(0,0,0); + center = Vector.of(0.0, 0.0, 0.0); + xAxis = Vector.of(0.0, 0.0, 0.0); + yAxis = Vector.of(0.0, 0.0, 0.0); + zAxis = Vector.of(0.0, 0.0, 0.0); } @Override @@ -261,7 +264,7 @@ public final class OBBox extends AbstractGeometry { */ public Vector<?> nearest(final Vector<?> position) { final Vector<?> offset = position.copy(DataType.DOUBLE).subtract(center); - Vector<?> nearest = center.copy(DataType.DOUBLE); + Vector<?> nearest = center; 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); diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java index 77c8bf3633..69cc7ae658 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Collision.java @@ -206,7 +206,7 @@ public final class Collision { for (int i = 0; i < depths.length; i++) { if (depths[i] >= threshold) { // Halfway between the deepest point of the sphere and the boundary of the plane. - final Vector<?> point = centers.get(i).copy(DataType.DOUBLE).combine(1, normal, depths[i] / 2 - radii[i]); + final Vector<?> point = centers.get(i).combine(1, normal, depths[i] / 2 - radii[i]); sum = (sum == null) ? point : sum.add(point); count++; } @@ -299,13 +299,13 @@ public final class Collision { { return deepest; } - final Vector<?> axis = balls.end().copy(DataType.DOUBLE).subtract(balls.start()); - final Vector<?> otherAxis = other.end().copy(DataType.DOUBLE).subtract(other.start()); + final Vector<?> axis = balls.end().subtract(balls.start()); + final Vector<?> otherAxis = other.end().subtract(other.start()); if (isParallel(axis, otherAxis)) { // Ratios on the other axis of the projections of the two ends of the balls axis. final double l2 = otherAxis.lengthSquare(); - final double s0 = balls.start().copy(DataType.DOUBLE).subtract(other.start()).dot(otherAxis) / l2; - final double s1 = balls.end() .copy(DataType.DOUBLE).subtract(other.start()).dot(otherAxis) / l2; + final double s0 = balls.start().subtract(other.start()).dot(otherAxis) / l2; + final double s1 = balls.end() .subtract(other.start()).dot(otherAxis) / l2; if (s1 != s0) { final double t0 = -s0 / (s1 - s0); final double t1 = (1 - s0) / (s1 - s0); @@ -355,7 +355,7 @@ public final class Collision { face = k; } } - final Vector<?> axis = box.axes()[face].copy(DataType.DOUBLE); + final Vector<?> axis = box.axes()[face]; return (local[face] < 0) ? axis.negate() : axis; } case Nearest.Balls other -> { @@ -380,8 +380,8 @@ public final class Collision { * @return unit direction from the first balls toward the second. */ private static Vector<?> crossing(final Nearest.Balls other, final Nearest.Balls balls) { - final Vector<?> axis = balls.end().copy(DataType.DOUBLE).subtract(balls.start()); - final Vector<?> otherAxis = other.end().copy(DataType.DOUBLE).subtract(other.start()); + final Vector<?> axis = balls.end().subtract(balls.start()); + final Vector<?> otherAxis = other.end().subtract(other.start()); final int dimension = axis.getDimension(); if (dimension == 3) { if (!isParallel(otherAxis, axis)) { @@ -431,8 +431,8 @@ public final class Collision { best = axis; } } - final Vector<?> between = other.center().copy(DataType.DOUBLE).subtract(box.center()); - Vector<?> normal = best.direction().copy(DataType.DOUBLE); + final Vector<?> between = other.center().subtract(box.center()); + Vector<?> normal = best.direction(); if (between.dot(normal) < 0) { normal = normal.negate(); } @@ -462,7 +462,7 @@ public final class Collision { final double[] depths = new double[corners.size()]; double deepest = Double.NEGATIVE_INFINITY; for (int i = 0; i < depths.length; i++) { - depths[i] = reference.halfSizes()[face] - corners.get(i).copy(DataType.DOUBLE).subtract(reference.center()).dot(normal); + depths[i] = reference.halfSizes()[face] - corners.get(i).subtract(reference.center()).dot(normal); deepest = Math.max(deepest, depths[i]); } final double threshold = deepest - FLATNESS * Vector.of(incident.halfSizes()).length(); diff --git a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java index 983e518ee1..85836d4f60 100644 --- a/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java +++ b/incubator/src/org.apache.sis.geometry/main/org/apache/sis/geometries/operation/Nearest.java @@ -82,9 +82,16 @@ sealed interface Nearest { */ static Nearest of(final Geometry geometry) { return switch (geometry) { - case Point cdt -> new Balls(cdt.getPosition(), cdt.getPosition(), 0, 0, cdt); - case Sphere cdt -> new Balls(cdt.getCenter(), cdt.getCenter(), cdt.getRadius(), cdt.getRadius(), cdt); - case Capsule cdt -> new Balls(cdt.getDataPoints().getPosition(0), cdt.getDataPoints().getPosition(1), + case Point cdt -> { + final Vector<?> position = cdt.getPosition().copy(DataType.DOUBLE); + yield new Balls(position, position, 0, 0, cdt); + } + case Sphere cdt -> { + final Vector<?> center = cdt.getCenter().copy(DataType.DOUBLE); + yield new Balls(center, center, cdt.getRadius(), cdt.getRadius(), cdt); + } + case Capsule cdt -> new Balls(cdt.getDataPoints().getPosition(0).copy(DataType.DOUBLE), + cdt.getDataPoints().getPosition(1).copy(DataType.DOUBLE), cdt.getRadiusBottom(), cdt.getRadiusTop(), cdt); case BBox cdt -> Box.of(cdt); case OBBox cdt -> Box.of(cdt); @@ -98,8 +105,8 @@ sealed interface Nearest { * The union of the balls whose centers and radii are interpolated between two ends. * A sphere has the same center at both ends, a point has also a radius of zero. * - * @param start center of the ball at the start. - * @param end center of the ball at the end. + * @param start center of the ball at the start, with double values. + * @param end center of the ball at the end, with double values. * @param startRadius radius of the ball at the start. * @param endRadius radius of the ball at the end. * @param geometry the point, sphere or capsule. @@ -123,7 +130,7 @@ sealed interface Nearest { * @return center of the ball. */ Vector<?> center(final double t) { - return start.copy(DataType.DOUBLE).lerp(end, t); + return start.lerp(end, t); } /** @@ -172,7 +179,7 @@ sealed interface Nearest { * @return ratio from 0 at the start to 1 at the end. */ double nearestRatio(final Vector<?> position) { - final Vector<?> axis = end.copy(DataType.DOUBLE).subtract(start); + final Vector<?> axis = end.subtract(start); final double l2 = axis.lengthSquare(); if (l2 == 0) { return 0; @@ -204,7 +211,7 @@ sealed interface Nearest { if (!Double.isNaN(exact)) { return exact; } - final Vector<?> axis = end.copy(DataType.DOUBLE).subtract(start); + final Vector<?> axis = end.subtract(start); if (axis.lengthSquare() == 0) { return 0; } @@ -341,8 +348,8 @@ sealed interface Nearest { /** * A box of any orientation. * - * @param center center of the box. - * @param axes unit vectors along the edges of the box, orthogonal to each other. + * @param center center of the box, with double values. + * @param axes unit vectors along the edges of the box, orthogonal to each other, with double values. * @param halfSizes half of the size of the box along each axis. * @param geometry the {@link BBox} or {@link OBBox}. */ @@ -387,7 +394,7 @@ sealed interface Nearest { halfSizes[i] = axes[i].length(); axes[i] = axes[i].copy(DataType.DOUBLE).normalize(); } - return new Box(box.getCenter(), axes, halfSizes, box); + return new Box(box.getCenter().copy(DataType.DOUBLE), axes, halfSizes, box); } @Override @@ -416,7 +423,7 @@ sealed interface Nearest { final int count = 1 << axes.length; final List<Vector<?>> corners = new ArrayList<>(count); for (int i = 0; i < count; i++) { - Vector<?> corner = center.copy(DataType.DOUBLE); + Vector<?> corner = center; for (int k = 0; k < axes.length; k++) { corner = corner.combine(1, axes[k], ((i & (1 << k)) != 0) ? halfSizes[k] : -halfSizes[k]); } @@ -460,7 +467,7 @@ sealed interface Nearest { /** * An axis which may separate two boxes. * - * @param direction unit direction of the axis. + * @param direction unit direction of the axis, with double values. * @param axis index of the axis of the first box normal to its faces or parallel to its edges, or -1. * @param otherAxis index of the axis of the other box normal to its faces or parallel to its edges, or -1. */ @@ -519,7 +526,7 @@ sealed interface Nearest { */ double overlap(final Box other, final SeparatingAxis axis) { final Vector<?> direction = axis.direction(); - final double between = other.center.copy(DataType.DOUBLE).subtract(center).dot(direction); + final double between = other.center.subtract(center).dot(direction); return projectedRadius(direction) + other.projectedRadius(direction) - Math.abs(between); } @@ -561,7 +568,7 @@ sealed interface Nearest { * @return the position. */ Vector<?> world(final double[] local) { - Vector<?> position = center.copy(DataType.DOUBLE); + Vector<?> position = center; for (int k = 0; k < axes.length; k++) { position = position.combine(1, axes[k], local[k]); }
