Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-7942: Add aggregateDistances() method to DistanceStyle, and use it in GeoStandardPath to aggregate distances from segment to segment.

Karl Wright92c94d8ca201
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/DistanceStyle.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/DistanceStyle.java
index 3657fe9c529..1dccffba273 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/DistanceStyle.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/DistanceStyle.java
@@ -78,6 +78,16 @@ public interface DistanceStyle {
*/
public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds);
+ /** Aggregate two distances together to produce a "sum".
+ * This is usually just an addition operation, but in the case of squared distances it is more complex.
+ * @param distance1 is the first distance.
+ * @param distance2 is the second distance.
+ * @return the combined distance.
+ */
+ public default double aggregateDistances(final double distance1, final double distance2) {
+ return distance1 + distance2;
+ }
+
// The following methods are used to go from a distance value back to something
// that can be used to construct a constrained shape.
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoStandardPath.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoStandardPath.java
index c30414c7a26..cde1c49e23b 100755
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoStandardPath.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoStandardPath.java
@@ -223,8 +223,8 @@ class GeoStandardPath extends GeoBasePath {
for (PathSegment segment : segments) {
double distance = segment.pathDistance(planetModel, distanceStyle, x,y,z);
if (distance != Double.POSITIVE_INFINITY)
- return currentDistance + distance;
- currentDistance += segment.fullPathDistance(distanceStyle);
+ return distanceStyle.aggregateDistances(currentDistance, distance);
+ currentDistance = distanceStyle.aggregateDistances(currentDistance, segment.fullPathDistance(distanceStyle));
}
int segmentIndex = 0;
@@ -232,9 +232,9 @@ class GeoStandardPath extends GeoBasePath {
for (SegmentEndpoint endpoint : endPoints) {
double distance = endpoint.pathDistance(distanceStyle, x, y, z);
if (distance != Double.POSITIVE_INFINITY)
- return currentDistance + distance;
+ return distanceStyle.aggregateDistances(currentDistance, distance);
if (segmentIndex < segments.size())
- currentDistance += segments.get(segmentIndex++).fullPathDistance(distanceStyle);
+ currentDistance = distanceStyle.aggregateDistances(currentDistance, segments.get(segmentIndex++).fullPathDistance(distanceStyle));
}
return Double.POSITIVE_INFINITY;
@@ -807,7 +807,7 @@ class GeoStandardPath extends GeoBasePath {
else
throw new RuntimeException("Can't find world intersection for point x="+x+" y="+y+" z="+z);
}
- return distanceStyle.computeDistance(thePoint, x, y, z) + distanceStyle.computeDistance(start, thePoint.x, thePoint.y, thePoint.z);
+ return distanceStyle.aggregateDistances(distanceStyle.computeDistance(thePoint, x, y, z), distanceStyle.computeDistance(start, thePoint.x, thePoint.y, thePoint.z));
}
/** Compute external distance.
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/LinearSquaredDistance.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/LinearSquaredDistance.java
index 4b26f8e4947..c1800f61803 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/LinearSquaredDistance.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/LinearSquaredDistance.java
@@ -51,6 +51,12 @@ public class LinearSquaredDistance implements DistanceStyle {
return plane.linearDistanceSquared(planetModel, x,y,z, bounds);
}
+ @Override
+ public double aggregateDistances(final double distance1, final double distance2) {
+ final double linearDistance = Math.sqrt(distance1) + Math.sqrt(distance2);
+ return linearDistance * linearDistance;
+ }
+
@Override
public GeoPoint[] findDistancePoints(final PlanetModel planetModel, final double distanceValue, final GeoPoint startPoint, final Plane plane, final Membership... bounds) {
throw new IllegalStateException("Reverse mapping not implemented for this distance metric");
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/NormalSquaredDistance.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/NormalSquaredDistance.java
index 428e04202b5..3a68cc97c9b 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/NormalSquaredDistance.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/NormalSquaredDistance.java
@@ -51,6 +51,12 @@ public class NormalSquaredDistance implements DistanceStyle {
return plane.normalDistanceSquared(x,y,z, bounds);
}
+ @Override
+ public double aggregateDistances(final double distance1, final double distance2) {
+ final double normalDistance = Math.sqrt(distance1) + Math.sqrt(distance2);
+ return normalDistance * normalDistance;
+ }
+
@Override
public GeoPoint[] findDistancePoints(final PlanetModel planetModel, final double distanceValue, final GeoPoint startPoint, final Plane plane, final Membership... bounds) {
throw new IllegalStateException("Reverse mapping not implemented for this distance metric");