You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2020/02/17 09:49:50 UTC
[sis] branch geoapi-4.0 updated: Replace `distance` method by `distanceSquared` for avoiding the Math.hypot(…) cost.
This is an automated email from the ASF dual-hosted git repository.
desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
new a581d4e Replace `distance` method by `distanceSquared` for avoiding the Math.hypot(…) cost.
a581d4e is described below
commit a581d4ef43c49bb9ce91e06ad908e5b1f8f4f7d6
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Mon Feb 17 10:42:18 2020 +0100
Replace `distance` method by `distanceSquared` for avoiding the Math.hypot(…) cost.
---
.../referencing/j2d/IntervalRectangle.java | 23 +++++++++++++++-------
.../referencing/provider/DatumShiftGridGroup.java | 8 ++++----
2 files changed, 20 insertions(+), 11 deletions(-)
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java
index fb589f4..5c36ab9 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/j2d/IntervalRectangle.java
@@ -44,7 +44,7 @@ import org.apache.sis.internal.util.Strings;
* This class provides the following additional methods which are not defined in {@link Rectangle2D}:
* <ul>
* <li>{@link #containsInclusive(double, double)}</li>
- * <li>{@link #distance(double, double)}</li>
+ * <li>{@link #distanceSquared(double, double)}</li>
* </ul>
*
* This class does <strong>not</strong> support by itself rectangles crossing the anti-meridian of a geographic CRS.
@@ -401,14 +401,14 @@ public class IntervalRectangle extends Rectangle2D {
}
/**
- * Returns the minimal distance between a point and this rectangle.
+ * Returns the square of the minimal distance between a point and this rectangle.
* If the point is inside the rectangle or on the edge, then this method returns 0.
*
* @param x the <var>x</var> coordinates to test.
* @param y the <var>y</var> coordinates to test.
- * @return minimal distance, or 0 if the point is inside this rectangle.
+ * @return square of minimal distance, or 0 if the point is inside this rectangle.
*/
- public final double distance(final double x, final double y) {
+ public final double distanceSquared(final double x, final double y) {
int outcode = 0;
double dx = java.lang.Double.POSITIVE_INFINITY;
double dy = java.lang.Double.POSITIVE_INFINITY;
@@ -418,12 +418,21 @@ public class IntervalRectangle extends Rectangle2D {
if ((d = (xmin - x)) >= 0 && d < dx) {dx = d; outcode |= 1;}
if ((d = (ymin - y)) >= 0 && d < dy) {dy = d; outcode |= 2;}
switch (outcode) {
- case 1: return dx; // Only x coordinate is outside.
- case 2: return dy; // Only y coordinate is outside.
- case 3: return Math.hypot(dx, dy); // Both coordinates are outside.
+ case 1: return dx*dx; // Only x coordinate is outside.
+ case 2: return dy*dy; // Only y coordinate is outside.
+ case 3: return dx*dx + dy*dy; // Both coordinates are outside.
case 0: assert containsInclusive(x, y); return 0; // No coordinate is outside.
default: throw new AssertionError(outcode);
}
+ /*
+ * Note: if we want non-squared distance in a future version, we can rely on the fact
+ * that `dx` and `dy` are guaranteed positives (no need to take the absolute value).
+ * So we would replace the 3 first cases as below:
+ *
+ * case 1: return dx;
+ * case 2: return dy;
+ * case 3: return Math.hypot(dx, dy);
+ */
}
/**
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
index c43b225..df2cefa 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/provider/DatumShiftGridGroup.java
@@ -285,12 +285,12 @@ final class DatumShiftGridGroup<C extends Quantity<C>, T extends Quantity<T>> ex
public void interpolateInCell(final double gridX, final double gridY, final double[] vector) {
int ni = 0;
Region nearest = regions[ni];
- double distance = nearest.distance(gridX, gridY);
+ double dmin = nearest.distanceSquared(gridX, gridY);
for (int i=1; i<regions.length; i++) {
final Region r = regions[i];
- final double d = r.distance(gridX, gridY);
- if (d < distance) {
- distance = d;
+ final double d = r.distanceSquared(gridX, gridY);
+ if (d < dmin) {
+ dmin = d;
nearest = r;
ni = i;
if (d == 0) break;