Skip to content

Apache Lucene open-source search software

LUCENE-6578: Geo3D: compute the distance from a point to a shape.

David Wayne Smiley688c67f8ff22
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 5c31ea03dcc..49715c49683 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -109,6 +109,10 @@ New Features
near-real-time or non-NRT reader. (Boaz Leskes, Robert Muir, Mike
McCandless)
+* LUCENE-6578: Geo3D can now compute the distance from a point to a shape, both
+ inner distance and to an outside edge. Multiple distance algorithms are
+ available. (Karl Wright, David Smiley)
+
API Changes
* LUCENE-6508: Simplify Lock api, there is now just
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/ArcDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/ArcDistance.java
new file mode 100644
index 00000000000..c056150f4bb
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/ArcDistance.java
@@ -0,0 +1,51 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Arc distance computation style.
+ *
+ * @lucene.experimental
+ */
+public class ArcDistance implements DistanceStyle {
+
+ public final static ArcDistance INSTANCE = new ArcDistance();
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final GeoPoint point2) {
+ return point1.arcDistance(point2);
+ }
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final double x2, final double y2, final double z2) {
+ return point1.arcDistance(x2,y2,z2);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final GeoPoint point, final Membership... bounds) {
+ return plane.arcDistance(planetModel, point, bounds);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds) {
+ return plane.arcDistance(planetModel, x,y,z, bounds);
+ }
+
+}
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/BasePlanetObject.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/BasePlanetObject.java
new file mode 100644
index 00000000000..39255c8f5aa
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/BasePlanetObject.java
@@ -0,0 +1,48 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * All Geo3D shapes can derive from this base class, which furnishes
+ * some common code
+ *
+ * @lucene.internal
+ */
+public abstract class BasePlanetObject {
+
+ protected final PlanetModel planetModel;
+
+ public BasePlanetObject(final PlanetModel planetModel) {
+ this.planetModel = planetModel;
+ }
+
+ @Override
+ public int hashCode() {
+ return planetModel.hashCode();
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (!(o instanceof BasePlanetObject))
+ return false;
+ return planetModel.equals(((BasePlanetObject)o).planetModel);
+ }
+}
+
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/DistanceStyle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/DistanceStyle.java
new file mode 100644
index 00000000000..7d11c3880fb
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/DistanceStyle.java
@@ -0,0 +1,79 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Distance computation styles, supporting various ways of computing
+ * distance to shapes.
+ *
+ * @lucene.experimental
+ */
+public interface DistanceStyle {
+
+ // convenient access to built-in styles:
+
+ public static final ArcDistance ARC = ArcDistance.INSTANCE;
+ public static final LinearDistance LINEAR = LinearDistance.INSTANCE;
+ public static final LinearSquaredDistance LINEAR_SQUARED = LinearSquaredDistance.INSTANCE;
+ public static final NormalDistance NORMAL = NormalDistance.INSTANCE;
+ public static final NormalSquaredDistance NORMAL_SQUARED = NormalSquaredDistance.INSTANCE;
+
+ /** Compute the distance from a point to another point.
+ * @param point1 Starting point
+ * @param point2 Final point
+ * @return the distance
+ */
+ public default double computeDistance(final GeoPoint point1, final GeoPoint point2) {
+ return computeDistance(point1, point2.x, point2.y, point2.z);
+ }
+
+ /** Compute the distance from a point to another point.
+ * @param point1 Starting point
+ * @param x2 Final point x
+ * @param y2 Final point y
+ * @param z2 Final point z
+ * @return the distance
+ */
+ public double computeDistance(final GeoPoint point1, final double x2, final double y2, final double z2);
+
+ /** Compute the distance from a plane to a point.
+ * @param planetModel The planet model
+ * @param plane The plane
+ * @param point The point
+ * @param bounds are the plane bounds
+ * @return the distance
+ */
+ public default double computeDistance(final PlanetModel planetModel, final Plane plane, final GeoPoint point,
+ final Membership... bounds) {
+ return computeDistance(planetModel, plane, point.x, point.y, point.z, bounds);
+ }
+
+ /** Compute the distance from a plane to a point.
+ * @param planetModel The planet model
+ * @param plane The plane
+ * @param x The point x
+ * @param y The point y
+ * @param z The point z
+ * @param bounds are the plane bounds
+ * @return the distance
+ */
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds);
+
+}
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseBBox.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseBBox.java
index 1e52aaf734f..48183bad601 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseBBox.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseBBox.java
@@ -23,14 +23,11 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*
* @lucene.internal
*/
-public abstract class GeoBaseBBox extends GeoBaseShape implements GeoBBox {
+public abstract class GeoBaseBBox extends GeoBaseMembershipShape implements GeoBBox {
public GeoBaseBBox(final PlanetModel planetModel) {
super(planetModel);
}
-
- @Override
- public abstract boolean isWithin(final Vector point);
protected final static int ALL_INSIDE = 0;
protected final static int SOME_INSIDE = 1;
@@ -58,15 +55,6 @@ public abstract class GeoBaseBBox extends GeoBaseShape implements GeoBBox {
return NONE_INSIDE;
return SOME_INSIDE;
}
-
- @Override
- public int hashCode() {
- return super.hashCode();
- }
-
- @Override
- public boolean equals(final Object o) {
- return super.equals(o);
- }
+
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseDistanceShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseDistanceShape.java
new file mode 100644
index 00000000000..ffdf53853b3
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseDistanceShape.java
@@ -0,0 +1,54 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Distance shapes have capabilities of both geohashing and distance
+ * computation (which also includes point membership determination).
+ *
+ * @lucene.experimental
+ */
+public abstract class GeoBaseDistanceShape extends GeoBaseMembershipShape implements GeoDistanceShape {
+
+ public GeoBaseDistanceShape(final PlanetModel planetModel) {
+ super(planetModel);
+ }
+
+ @Override
+ public boolean isWithin(Vector point) {
+ return isWithin(point.x, point.y, point.z);
+ }
+
+ @Override
+ public double computeDistance(final DistanceStyle distanceStyle, final GeoPoint point) {
+ return computeDistance(distanceStyle, point.x, point.y, point.z);
+ }
+
+ @Override
+ public double computeDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ if (!isWithin(x,y,z)) {
+ return Double.MAX_VALUE;
+ }
+ return distance(distanceStyle, x, y, z);
+ }
+
+ /** Called by a {@code computeDistance} method if X/Y/Z is not within this shape. */
+ protected abstract double distance(final DistanceStyle distanceStyle, final double x, final double y, final double z);
+
+}
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseExtendedShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseExtendedShape.java
deleted file mode 100755
index e3a1c0347f9..00000000000
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseExtendedShape.java
+++ /dev/null
@@ -1,103 +0,0 @@
-package org.apache.lucene.spatial.spatial4j.geo3d;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Base extended shape object.
- *
- * @lucene.internal
- */
-public abstract class GeoBaseExtendedShape extends GeoBaseShape implements GeoShape {
-
- public GeoBaseExtendedShape(final PlanetModel planetModel) {
- super(planetModel);
- }
-
- /**
- * Check if a point is within this shape.
- *
- * @param point is the point to check.
- * @return true if the point is within this shape
- */
- @Override
- public abstract boolean isWithin(final Vector point);
-
- /**
- * Check if a point is within this shape.
- *
- * @param x is x coordinate of point to check.
- * @param y is y coordinate of point to check.
- * @param z is z coordinate of point to check.
- * @return true if the point is within this shape
- */
- @Override
- public abstract boolean isWithin(final double x, final double y, final double z);
-
- /**
- * Return a sample point that is on the edge of the shape.
- *
- * @return a number of edge points, one for each disconnected edge.
- */
- @Override
- public abstract GeoPoint[] getEdgePoints();
-
- /**
- * Assess whether a plane, within the provided bounds, intersects
- * with the shape.
- *
- * @param plane is the plane to assess for intersection with the shape's edges or
- * bounding curves.
- * @param bounds are a set of bounds that define an area that an
- * intersection must be within in order to qualify (provided by a GeoArea).
- * @return true if there's such an intersection, false if not.
- */
- @Override
- public abstract boolean intersects(final Plane plane, final GeoPoint[] notablePoints, final Membership... bounds);
-
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
- @Override
- public Bounds getBounds(Bounds bounds) {
- if (bounds == null)
- bounds = new Bounds();
- if (isWithin(planetModel.NORTH_POLE)) {
- bounds.noTopLatitudeBound().noLongitudeBound();
- }
- if (isWithin(planetModel.SOUTH_POLE)) {
- bounds.noBottomLatitudeBound().noLongitudeBound();
- }
- return bounds;
- }
-
- @Override
- public int hashCode() {
- return super.hashCode();
- }
-
- @Override
- public boolean equals(final Object o) {
- return super.equals(o);
- }
-}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseMembershipShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseMembershipShape.java
new file mode 100644
index 00000000000..941912c6ce3
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseMembershipShape.java
@@ -0,0 +1,54 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Membership shapes have capabilities of both geohashing and membership
+ * determination. This is a useful baseclass for them.
+ *
+ * @lucene.experimental
+ */
+public abstract class GeoBaseMembershipShape extends GeoBaseShape implements GeoMembershipShape {
+
+ public GeoBaseMembershipShape(final PlanetModel planetModel) {
+ super(planetModel);
+ }
+
+ @Override
+ public boolean isWithin(Vector point) {
+ return isWithin(point.x, point.y, point.z);
+ }
+
+ @Override
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final GeoPoint point) {
+ return computeOutsideDistance(distanceStyle, point.x, point.y, point.z);
+ }
+
+ @Override
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ if (isWithin(x,y,z)) {
+ return 0.0;
+ }
+ return outsideDistance(distanceStyle, x,y,z);
+ }
+
+ /** Called by a {@code computeOutsideDistance} method if X/Y/Z is not within this shape. */
+ protected abstract double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z);
+
+}
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseShape.java
index f5a3dad017b..dac5c44df39 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseShape.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoBaseShape.java
@@ -18,30 +18,29 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*/
/**
- * All bounding box shapes can derive from this base class, which furnishes
- * some common code
+ * Base extended shape object.
*
* @lucene.internal
*/
-public abstract class GeoBaseShape {
+public abstract class GeoBaseShape extends BasePlanetObject implements GeoShape {
- protected final PlanetModel planetModel;
-
public GeoBaseShape(final PlanetModel planetModel) {
- this.planetModel = planetModel;
+ super(planetModel);
}
-
- @Override
- public int hashCode() {
- return planetModel.hashCode();
- }
-
+
@Override
- public boolean equals(final Object o) {
- if (!(o instanceof GeoBaseShape))
- return false;
- return planetModel.equals(((GeoBaseShape)o).planetModel);
+ public Bounds getBounds(Bounds bounds) {
+ if (bounds == null)
+ bounds = new Bounds();
+ if (isWithin(planetModel.NORTH_POLE)) {
+ bounds.noTopLatitudeBound().noLongitudeBound();
+ }
+ if (isWithin(planetModel.SOUTH_POLE)) {
+ bounds.noBottomLatitudeBound().noLongitudeBound();
+ }
+ return bounds;
}
+
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircle.java
index fbfc1c34ad9..ee1bb6ed368 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircle.java
@@ -22,7 +22,7 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*
* @lucene.experimental
*/
-public class GeoCircle extends GeoBaseExtendedShape implements GeoDistanceShape, GeoSizeable {
+public class GeoCircle extends GeoBaseDistanceShape implements GeoSizeable {
public final GeoPoint center;
public final double cutoffAngle;
public final SidedPlane circlePlane;
@@ -81,124 +81,19 @@ public class GeoCircle extends GeoBaseExtendedShape implements GeoDistanceShape,
return cutoffAngle;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return center;
}
- /**
- * Compute an estimate of "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- */
@Override
- public double computeNormalDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.center.normalDistance(point);
+ protected double distance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(this.center, x, y, z);
}
- /**
- * Compute an estimate of "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- */
@Override
- public double computeNormalDistance(final double x, final double y, final double z) {
- if (!isWithin(x,y,z))
- return Double.MAX_VALUE;
- return this.center.normalDistance(x, y, z);
- }
-
- /**
- * Compute a squared estimate of the "distance" to the
- * GeoPoint. Double.MAX_VALUE indicates a point outside of the
- * shape.
- */
- @Override
- public double computeSquaredNormalDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.center.normalDistanceSquared(point);
- }
-
- /**
- * Compute a squared estimate of the "distance" to the
- * GeoPoint. Double.MAX_VALUE indicates a point outside of the
- * shape.
- */
- @Override
- public double computeSquaredNormalDistance(final double x, final double y, final double z) {
- if (!isWithin(x,y,z))
- return Double.MAX_VALUE;
- return this.center.normalDistanceSquared(x, y, z);
- }
-
- /**
- * Compute a linear distance to the vector.
- * return Double.MAX_VALUE for points outside the shape.
- */
- @Override
- public double computeLinearDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.center.linearDistance(point);
- }
-
- /**
- * Compute a linear distance to the vector.
- * return Double.MAX_VALUE for points outside the shape.
- */
- @Override
- public double computeLinearDistance(final double x, final double y, final double z) {
- if (!isWithin(x,y,z))
- return Double.MAX_VALUE;
- return this.center.linearDistance(x, y, z);
- }
-
- /**
- * Compute a squared linear distance to the vector.
- */
- @Override
- public double computeSquaredLinearDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.center.linearDistanceSquared(point);
- }
-
- /**
- * Compute a squared linear distance to the vector.
- */
- @Override
- public double computeSquaredLinearDistance(final double x, final double y, final double z) {
- if (!isWithin(x,y,z))
- return Double.MAX_VALUE;
- return this.center.linearDistanceSquared(x, y, z);
- }
-
- /**
- * Compute a true, accurate, great-circle distance.
- * Double.MAX_VALUE indicates a point is outside of the shape.
- */
- @Override
- public double computeArcDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.center.arcDistance(point);
- }
-
- @Override
- public boolean isWithin(final Vector point) {
- if (circlePlane == null) {
- return true;
- }
- // Fastest way of determining membership
- return circlePlane.isWithin(point);
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(planetModel, circlePlane, x, y, z);
}
@Override
@@ -223,15 +118,6 @@ public class GeoCircle extends GeoBaseExtendedShape implements GeoDistanceShape,
return circlePlane.intersects(planetModel, p, notablePoints, circlePoints, bounds);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
bounds = super.getBounds(bounds);
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCompositeMembershipShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCompositeMembershipShape.java
index 5a192f5e37e..a128cb5715e 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCompositeMembershipShape.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoCompositeMembershipShape.java
@@ -40,14 +40,7 @@ public class GeoCompositeMembershipShape implements GeoMembershipShape {
@Override
public boolean isWithin(final Vector point) {
- //System.err.println("Checking whether point "+point+" is within Composite");
- for (GeoMembershipShape shape : shapes) {
- if (shape.isWithin(point)) {
- //System.err.println(" Point is within "+shape);
- return true;
- }
- }
- return false;
+ return isWithin(point.x, point.y, point.z);
}
@Override
@@ -73,15 +66,6 @@ public class GeoCompositeMembershipShape implements GeoMembershipShape {
return false;
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -92,24 +76,37 @@ public class GeoCompositeMembershipShape implements GeoMembershipShape {
return bounds;
}
+ @Override
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final GeoPoint point) {
+ return computeOutsideDistance(distanceStyle, point.x, point.y, point.z);
+ }
+
+ @Override
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ if (isWithin(x,y,z))
+ return 0.0;
+ double distance = Double.MAX_VALUE;
+ for (GeoMembershipShape shape : shapes) {
+ final double normalDistance = shape.computeOutsideDistance(distanceStyle, x, y, z);
+ if (normalDistance < distance) {
+ distance = normalDistance;
+ }
+ }
+ return distance;
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoCompositeMembershipShape))
return false;
GeoCompositeMembershipShape other = (GeoCompositeMembershipShape) o;
- if (other.shapes.size() != shapes.size())
- return false;
- for (int i = 0; i < shapes.size(); i++) {
- if (!other.shapes.get(i).equals(shapes.get(i)))
- return false;
- }
- return true;
+ return super.equals(o) && shapes.equals(other.shapes);
}
@Override
public int hashCode() {
- return shapes.hashCode();//TODO cache
+ return super.hashCode() * 31 + shapes.hashCode();//TODO cache
}
@Override
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygon.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygon.java
index 03aa3fc3ed5..f5c7cebf0dd 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygon.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygon.java
@@ -29,7 +29,7 @@ import java.util.List;
*
* @lucene.experimental
*/
-public class GeoConvexPolygon extends GeoBaseExtendedShape implements GeoMembershipShape {
+public class GeoConvexPolygon extends GeoBaseMembershipShape {
protected final List<GeoPoint> points;
protected final BitSet isInternalEdges;
@@ -151,15 +151,6 @@ public class GeoConvexPolygon extends GeoBaseExtendedShape implements GeoMembers
return index;
}
- @Override
- public boolean isWithin(final Vector point) {
- for (final SidedPlane edge : edges) {
- if (!edge.isWithin(point))
- return false;
- }
- return true;
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
for (final SidedPlane edge : edges) {
@@ -201,15 +192,6 @@ public class GeoConvexPolygon extends GeoBaseExtendedShape implements GeoMembers
return false;
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
bounds = super.getBounds(bounds);
@@ -240,6 +222,32 @@ public class GeoConvexPolygon extends GeoBaseExtendedShape implements GeoMembers
return bounds;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ double minimumDistance = Double.MAX_VALUE;
+ for (final GeoPoint edgePoint : points) {
+ final double newDist = distanceStyle.computeDistance(edgePoint, x,y,z);
+ if (newDist < minimumDistance) {
+ minimumDistance = newDist;
+ }
+ }
+ for (int edgeIndex = 0; edgeIndex < edges.length; edgeIndex++) {
+ final Plane edgePlane = edges[edgeIndex];
+ final Membership[] membershipBounds = new Membership[edges.length - 1];
+ int count = 0;
+ for (int otherIndex = 0; otherIndex < edges.length; otherIndex++) {
+ if (otherIndex != edgeIndex) {
+ membershipBounds[count++] = edges[otherIndex];
+ }
+ }
+ final double newDist = distanceStyle.computeDistance(planetModel, edgePlane, x, y, z, membershipBounds);
+ if (newDist < minimumDistance) {
+ minimumDistance = newDist;
+ }
+ }
+ return minimumDistance;
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoConvexPolygon))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateHorizontalLine.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateHorizontalLine.java
index e1fd4ea2cd9..94dc38a2acc 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateHorizontalLine.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateHorizontalLine.java
@@ -111,13 +111,6 @@ public class GeoDegenerateHorizontalLine extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return plane.evaluateIsZero(point) &&
- leftPlane.isWithin(point) &&
- rightPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return plane.evaluateIsZero(x, y, z) &&
@@ -132,11 +125,6 @@ public class GeoDegenerateHorizontalLine extends GeoBaseBBox {
return Math.max(topAngle, bottomAngle);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -152,15 +140,6 @@ public class GeoDegenerateHorizontalLine extends GeoBaseBBox {
return p.intersects(planetModel, plane, notablePoints, planePoints, bounds, leftPlane, rightPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -186,6 +165,18 @@ public class GeoDegenerateHorizontalLine extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double distance = distanceStyle.computeDistance(planetModel, plane, x,y,z, leftPlane, rightPlane);
+
+ final double LHCDistance = distanceStyle.computeDistance(LHC, x,y,z);
+ final double RHCDistance = distanceStyle.computeDistance(RHC, x,y,z);
+
+ return Math.min(
+ distance,
+ Math.min(LHCDistance, RHCDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegenerateHorizontalLine))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLatitudeZone.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLatitudeZone.java
index 31bd77b80f6..3761f9aa7a3 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLatitudeZone.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLatitudeZone.java
@@ -51,11 +51,6 @@ public class GeoDegenerateLatitudeZone extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, -Math.PI, Math.PI);
}
- @Override
- public boolean isWithin(final Vector point) {
- return Math.abs(point.z - this.sinLatitude) < 1e-10;
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return Math.abs(z - this.sinLatitude) < 1e-10;
@@ -66,11 +61,6 @@ public class GeoDegenerateLatitudeZone extends GeoBaseBBox {
return Math.PI;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
// Totally arbitrary
@@ -121,6 +111,11 @@ public class GeoDegenerateLatitudeZone extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(planetModel, plane, x,y,z);
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegenerateLatitudeZone))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLongitudeSlice.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLongitudeSlice.java
index 1e41aca2283..79d33dd81a2 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLongitudeSlice.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateLongitudeSlice.java
@@ -68,12 +68,6 @@ public class GeoDegenerateLongitudeSlice extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, Math.PI * 0.5, -Math.PI * 0.5, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return plane.evaluateIsZero(point) &&
- boundingPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return plane.evaluateIsZero(x, y, z) &&
@@ -85,11 +79,6 @@ public class GeoDegenerateLongitudeSlice extends GeoBaseBBox {
return Math.PI * 0.5;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return interiorPoint;
@@ -105,15 +94,6 @@ public class GeoDegenerateLongitudeSlice extends GeoBaseBBox {
return p.intersects(planetModel, plane, notablePoints, planePoints, bounds, boundingPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -135,6 +115,18 @@ public class GeoDegenerateLongitudeSlice extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double distance = distanceStyle.computeDistance(planetModel, plane, x,y,z, boundingPlane);
+
+ final double northDistance = distanceStyle.computeDistance(planetModel.NORTH_POLE, x,y,z);
+ final double southDistance = distanceStyle.computeDistance(planetModel.SOUTH_POLE, x,y,z);
+
+ return Math.min(
+ distance,
+ Math.min(northDistance, southDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegenerateLongitudeSlice))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegeneratePoint.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegeneratePoint.java
index 258b8dd1951..355b0f4eca1 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegeneratePoint.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegeneratePoint.java
@@ -37,12 +37,6 @@ public class GeoDegeneratePoint extends GeoPoint implements GeoBBox {
this.edgePoints = new GeoPoint[]{this};
}
- /**
- * Expand box by specified angle.
- *
- * @param angle is the angle amount to expand the GeoBBox by.
- * @return a new GeoBBox.
- */
@Override
public GeoBBox expand(final double angle) {
final double newTopLat = latitude + angle;
@@ -52,26 +46,11 @@ public class GeoDegeneratePoint extends GeoPoint implements GeoBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- /**
- * Return a sample point that is on the edge of the shape.
- *
- * @return an interior point.
- */
@Override
public GeoPoint[] getEdgePoints() {
return edgePoints;
}
- /**
- * Assess whether a plane, within the provided bounds, intersects
- * with the shape.
- *
- * @param plane is the plane to assess for intersection with the shape's edges or
- * bounding curves.
- * @param bounds are a set of bounds that define an area that an
- * intersection must be within in order to qualify (provided by a GeoArea).
- * @return true if there's such an intersection, false if not.
- */
@Override
public boolean intersects(final Plane plane, final GeoPoint[] notablePoints, final Membership... bounds) {
// If not on the plane, no intersection
@@ -85,15 +64,6 @@ public class GeoDegeneratePoint extends GeoPoint implements GeoBBox {
return true;
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -102,9 +72,16 @@ public class GeoDegeneratePoint extends GeoPoint implements GeoBBox {
return bounds;
}
- /**
- * Equals
- */
+ @Override
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final GeoPoint point) {
+ return distanceStyle.computeDistance(this, point);
+ }
+
+ @Override
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(this, x,y,z);
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegeneratePoint))
@@ -128,61 +105,26 @@ public class GeoDegeneratePoint extends GeoPoint implements GeoBBox {
return "GeoDegeneratePoint: {planetmodel="+planetModel+", lat=" + latitude + "(" + latitude * 180.0 / Math.PI + "), lon=" + longitude + "(" + longitude * 180.0 / Math.PI + ")}";
}
- /**
- * Check if a point is within this shape.
- *
- * @param point is the point to check.
- * @return true if the point is within this shape
- */
@Override
public boolean isWithin(final Vector point) {
return isWithin(point.x, point.y, point.z);
}
- /**
- * Check if a point is within this shape.
- *
- * @param x is x coordinate of point to check.
- * @param y is y coordinate of point to check.
- * @param z is z coordinate of point to check.
- * @return true if the point is within this shape
- */
@Override
public boolean isWithin(final double x, final double y, final double z) {
return x == this.x && y == this.y && z == this.z;
}
- /**
- * Returns the radius of a circle into which the GeoSizeable area can
- * be inscribed.
- *
- * @return the radius.
- */
@Override
public double getRadius() {
return 0.0;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return this;
}
- /**
- * Find the spatial relationship between a shape and the current geo area.
- * Note: return value is how the GeoShape relates to the GeoArea, not the
- * other way around. For example, if this GeoArea is entirely within the
- * shape, then CONTAINS should be returned. If the shape is entirely enclosed
- * by this GeoArea, then WITHIN should be returned.
- *
- * @param shape is the shape to consider.
- * @return the relationship, from the perspective of the shape.
- */
@Override
public int getRelationship(final GeoShape shape) {
if (shape.isWithin(this)) {
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateVerticalLine.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateVerticalLine.java
index 7e9c8b51867..b5b35d54959 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateVerticalLine.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDegenerateVerticalLine.java
@@ -102,14 +102,6 @@ public class GeoDegenerateVerticalLine extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return plane.evaluateIsZero(point) &&
- boundingPlane.isWithin(point) &&
- topPlane.isWithin(point) &&
- bottomPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return plane.evaluateIsZero(x, y, z) &&
@@ -128,11 +120,6 @@ public class GeoDegenerateVerticalLine extends GeoBaseBBox {
return Math.max(topAngle, bottomAngle);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -148,15 +135,6 @@ public class GeoDegenerateVerticalLine extends GeoBaseBBox {
return p.intersects(planetModel, plane, notablePoints, planePoints, bounds, boundingPlane, topPlane, bottomPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -183,6 +161,18 @@ public class GeoDegenerateVerticalLine extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double distance = distanceStyle.computeDistance(planetModel, plane, x,y,z, topPlane, bottomPlane, boundingPlane);
+
+ final double UHCDistance = distanceStyle.computeDistance(UHC, x,y,z);
+ final double LHCDistance = distanceStyle.computeDistance(LHC, x,y,z);
+
+ return Math.min(
+ distance,
+ Math.min(UHCDistance, LHCDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoDegenerateVerticalLine))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDistance.java
index e061a95550d..4dc69911af2 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDistance.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoDistance.java
@@ -18,136 +18,43 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*/
/**
- * Generic geo-distance-capable shape class description. An implementer
- * of this interface is capable of computing the described "distance" values,
+ * An implementer of this interface is capable of computing the described "distance" values,
* which are meant to provide both actual distance values, as well as
* distance estimates that can be computed more cheaply.
*
* @lucene.experimental
*/
public interface GeoDistance extends Membership {
- /**
- * Compute this shape's normal "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- *
- * @param point is the point to compute the distance to.
- * @return the normal distance, defined as the perpendicular distance from
- * from the point to one of the shape's bounding plane. Normal
- * distances can therefore typically only go up to PI/2, except
- * when they represent the sum of a sequence of normal distances.
- */
- public double computeNormalDistance(GeoPoint point);
-
- /**
- * Compute this shape's normal "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- *
- * @param x is the point's unit x coordinate (using U.S. convention).
- * @param y is the point's unit y coordinate (using U.S. convention).
- * @param z is the point's unit z coordinate (using U.S. convention).
- * @return the normal distance, defined as the perpendicular distance from
- * from the point to one of the shape's bounding plane. Normal
- * distances can therefore typically only go up to PI/2, except
- * when they represent the sum of a sequence of normal distances.
- */
- public double computeNormalDistance(double x, double y, double z);
-
- /**
- * Compute the square of this shape's normal "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- *
- * @param point is the point to compute the distance to.
- * @return the square of the normal distance, defined as the perpendicular
- * distance from
- * from the point to one of the shape's bounding plane. Normal
- * distances can therefore typically only go up to PI/2, except
- * when they represent the sum of a sequence of normal distances.
- */
- public double computeSquaredNormalDistance(GeoPoint point);
-
- /**
- * Compute the square of this shape's normal "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- *
- * @param x is the point's unit x coordinate (using U.S. convention).
- * @param y is the point's unit y coordinate (using U.S. convention).
- * @param z is the point's unit z coordinate (using U.S. convention).
- * @return the square of the normal distance, defined as the perpendicular
- * distance from
- * from the point to one of the shape's bounding plane. Normal
- * distances can therefore typically only go up to PI/2, except
- * when they represent the sum of a sequence of normal distances.
- */
- public double computeSquaredNormalDistance(double x, double y, double z);
-
- /**
- * Compute this shape's linear "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- *
- * @param point is the point to compute the distance to.
- * @return the linear (or chord) distance, defined as the distance from
- * from the point to the nearest point on the unit sphere and on one of the shape's
- * bounding planes. Linear distances can therefore typically go up to PI,
- * except when they represent the sum of a sequence of linear distances.
- */
- public double computeLinearDistance(GeoPoint point);
-
- /**
- * Compute this shape's linear "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- *
- * @param x is the point's unit x coordinate (using U.S. convention).
- * @param y is the point's unit y coordinate (using U.S. convention).
- * @param z is the point's unit z coordinate (using U.S. convention).
- * @return the linear (or chord) distance, defined as the distance from
- * from the point to the nearest point on the unit sphere and on one of the shape's
- * bounding planes. Linear distances can therefore typically go up to PI,
- * except when they represent the sum of a sequence of linear distances.
- */
- public double computeLinearDistance(double x, double y, double z);
+
+ // The following methods compute distances from the shape to a point
+ // expected to be INSIDE the shape. Typically a value of Double.MAX_VALUE
+ // is returned for points that happen to be outside the shape.
/**
- * Compute the square of this shape's linear "distance" to the GeoPoint.
+ * Compute this shape's <em>internal</em> "distance" to the GeoPoint.
+ * Implementations should clarify how this is computed when it's non-obvious.
* A return value of Double.MAX_VALUE should be returned for
* points outside of the shape.
*
+ * @param distanceStyle is the distance style.
* @param point is the point to compute the distance to.
- * @return the square of the linear (or chord) distance, defined as the
- * distance from
- * from the point to the nearest point on the unit sphere and on one of the shape's
- * bounding planes. Linear distances can therefore typically go up to PI,
- * except when they represent the sum of a sequence of linear distances.
+ * @return the distance.
*/
- public double computeSquaredLinearDistance(GeoPoint point);
+ public default double computeDistance(final DistanceStyle distanceStyle, final GeoPoint point) {
+ return computeDistance(distanceStyle, point.x, point.y, point.z);
+ }
/**
- * Compute the square of this shape's linear "distance" to the GeoPoint.
+ * Compute this shape's <em>internal</em> "distance" to the GeoPoint.
+ * Implementations should clarify how this is computed when it's non-obvious.
* A return value of Double.MAX_VALUE should be returned for
* points outside of the shape.
*
* @param x is the point's unit x coordinate (using U.S. convention).
* @param y is the point's unit y coordinate (using U.S. convention).
* @param z is the point's unit z coordinate (using U.S. convention).
- * @return the square of the linear (or chord) distance, defined as the distance from
- * from the point to the nearest point on the unit sphere and on one of the shape's
- * bounding planes. Linear distances can therefore typically go up to PI,
- * except when they represent the sum of a sequence of linear distances.
- */
- public double computeSquaredLinearDistance(double x, double y, double z);
-
- /**
- * Compute a true, accurate, great-circle distance to a point.
- * Double.MAX_VALUE indicates a point is outside of the shape.
- *
- * @param point is the point.
* @return the distance.
*/
- public double computeArcDistance(GeoPoint point);
+ public double computeDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z);
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLatitudeZone.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLatitudeZone.java
index 5bec5807bf0..2703134650b 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLatitudeZone.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLatitudeZone.java
@@ -70,12 +70,6 @@ public class GeoLatitudeZone extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, -Math.PI, Math.PI);
}
- @Override
- public boolean isWithin(final Vector point) {
- return topPlane.isWithin(point) &&
- bottomPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return topPlane.isWithin(x, y, z) &&
@@ -94,11 +88,6 @@ public class GeoLatitudeZone extends GeoBaseBBox {
return maxCosLat * Math.PI;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
// This is totally arbitrary and only a cartesian could agree with it.
@@ -116,15 +105,6 @@ public class GeoLatitudeZone extends GeoBaseBBox {
p.intersects(planetModel, bottomPlane, notablePoints, planePoints, bounds, topPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -173,6 +153,14 @@ public class GeoLatitudeZone extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double topDistance = distanceStyle.computeDistance(planetModel, topPlane, x,y,z, bottomPlane);
+ final double bottomDistance = distanceStyle.computeDistance(planetModel, bottomPlane, x,y,z, topPlane);
+
+ return Math.min(topDistance, bottomDistance);
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoLatitudeZone))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLongitudeSlice.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLongitudeSlice.java
index d500b95d3ec..358d25668e4 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLongitudeSlice.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoLongitudeSlice.java
@@ -20,7 +20,7 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
/**
* Bounding box limited on left and right.
* The left-right maximum extent for this shape is PI; for anything larger, use
- * GeoWideLongitudeSlice.
+ * {@link GeoWideLongitudeSlice}.
*
* @lucene.internal
*/
@@ -91,12 +91,6 @@ public class GeoLongitudeSlice extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, Math.PI * 0.5, -Math.PI * 0.5, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return leftPlane.isWithin(point) &&
- rightPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return leftPlane.isWithin(x, y, z) &&
@@ -112,11 +106,6 @@ public class GeoLongitudeSlice extends GeoBaseBBox {
return Math.max(Math.PI * 0.5, extent * 0.5);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -133,15 +122,6 @@ public class GeoLongitudeSlice extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, planePoints, bounds, leftPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -178,6 +158,20 @@ public class GeoLongitudeSlice extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, rightPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, leftPlane);
+
+ final double northDistance = distanceStyle.computeDistance(planetModel.NORTH_POLE, x,y,z);
+ final double southDistance = distanceStyle.computeDistance(planetModel.SOUTH_POLE, x,y,z);
+
+ return
+ Math.min(
+ Math.min(northDistance, southDistance),
+ Math.min(leftDistance, rightDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoLongitudeSlice))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoMembershipShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoMembershipShape.java
index f055a84e4a8..3c3f64d3f7a 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoMembershipShape.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoMembershipShape.java
@@ -23,6 +23,6 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*
* @lucene.experimental
*/
-public interface GeoMembershipShape extends GeoShape, Membership {
+public interface GeoMembershipShape extends GeoShape, GeoOutsideDistance, Membership {
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthLatitudeZone.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthLatitudeZone.java
index 1a2c1281fbb..c7145574e87 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthLatitudeZone.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthLatitudeZone.java
@@ -59,12 +59,6 @@ public class GeoNorthLatitudeZone extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, -Math.PI, Math.PI);
}
- @Override
- public boolean isWithin(final Vector point) {
- return
- bottomPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return
@@ -81,11 +75,6 @@ public class GeoNorthLatitudeZone extends GeoBaseBBox {
return maxCosLat * Math.PI;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return interiorPoint;
@@ -102,15 +91,6 @@ public class GeoNorthLatitudeZone extends GeoBaseBBox {
p.intersects(planetModel, bottomPlane, notablePoints, planePoints, bounds);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -133,8 +113,7 @@ public class GeoNorthLatitudeZone extends GeoBaseBBox {
// Second, the shortcut of seeing whether endpoints are in/out is not going to
// work with no area endpoints. So we rely entirely on intersections.
- if (
- path.intersects(bottomPlane, planePoints))
+ if (path.intersects(bottomPlane, planePoints))
return OVERLAPS;
// There is another case for latitude zones only. This is when the boundaries of the shape all fit
@@ -152,6 +131,11 @@ public class GeoNorthLatitudeZone extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(planetModel, bottomPlane, x,y,z);
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoNorthLatitudeZone))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthRectangle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthRectangle.java
index 32f0a2e3498..ea7edf3f162 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthRectangle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoNorthRectangle.java
@@ -21,7 +21,7 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
* Bounding box limited on three sides (bottom lat, left lon, right lon), including
* the north pole.
* The left-right maximum extent for this shape is PI; for anything larger, use
- * GeoWideNorthRectangle.
+ * {@link GeoWideNorthRectangle}.
*
* @lucene.internal
*/
@@ -122,14 +122,6 @@ public class GeoNorthRectangle extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return
- bottomPlane.isWithin(point) &&
- leftPlane.isWithin(point) &&
- rightPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return
@@ -171,15 +163,6 @@ public class GeoNorthRectangle extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, leftPlane, bottomPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -226,6 +209,23 @@ public class GeoNorthRectangle extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double bottomDistance = distanceStyle.computeDistance(planetModel, bottomPlane, x,y,z, leftPlane, rightPlane);
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, rightPlane, bottomPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, leftPlane, bottomPlane);
+
+ final double LRHCDistance = distanceStyle.computeDistance(LRHC, x,y,z);
+ final double LLHCDistance = distanceStyle.computeDistance(LLHC, x,y,z);
+
+ return
+ Math.min(
+ bottomDistance,
+ Math.min(
+ Math.min(leftDistance, rightDistance),
+ Math.min(LRHCDistance, LLHCDistance)));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoNorthRectangle))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoOutsideDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoOutsideDistance.java
new file mode 100644
index 00000000000..c3a345b8b09
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoOutsideDistance.java
@@ -0,0 +1,56 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implemented by Geo3D shapes that can compute the distance from a point to the closest outside edge.
+ *
+ * @lucene.experimental
+ */
+public interface GeoOutsideDistance extends Membership {
+
+ // The following methods compute distances from the shape to a point
+ // expected to be OUTSIDE the shape. Typically a value of 0.0
+ // is returned for points that happen to be within the shape.
+
+ /**
+ * Compute this shape's distance to the GeoPoint.
+ * A return value of 0.0 should be returned for
+ * points inside of the shape.
+ * @param distanceStyle is the distance style.
+ * @param point is the point to compute the distance to.
+ * @return the distance.
+ */
+ public default double computeOutsideDistance(final DistanceStyle distanceStyle, final GeoPoint point) {
+ return computeOutsideDistance(distanceStyle, point.x, point.y, point.z);
+ }
+
+ /**
+ * Compute this shape's distance to the GeoPoint.
+ * A return value of 0.0 should be returned for
+ * points inside of the shape.
+ * @param distanceStyle is the distance style.
+ * @param x is the point's unit x coordinate (using U.S. convention).
+ * @param y is the point's unit y coordinate (using U.S. convention).
+ * @param z is the point's unit z coordinate (using U.S. convention).
+ * @return the distance.
+ */
+ public double computeOutsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z);
+
+}
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPath.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPath.java
index 7ecf5775d3c..4c851fc20f1 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPath.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPath.java
@@ -19,17 +19,19 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
- * GeoSearchableShape representing a path across the surface of the globe,
+ * GeoShape representing a path across the surface of the globe,
* with a specified half-width. Path is described by a series of points.
* Distances are measured from the starting point along the path, and then at right
* angles to the path.
*
* @lucene.experimental
*/
-public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
+public class GeoPath extends GeoBaseDistanceShape {
public final double cutoffAngle;
@@ -83,7 +85,7 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
GeoPoint lastPoint = null;
for (final GeoPoint end : points) {
if (lastPoint != null) {
- final Plane normalizedConnectingPlane = new Plane(lastPoint, end).normalize();
+ final Plane normalizedConnectingPlane = new Plane(lastPoint, end);
if (normalizedConnectingPlane == null) {
continue;
}
@@ -166,173 +168,46 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
}
- /**
- * Compute an estimate of "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- */
- @Override
- public double computeNormalDistance(final GeoPoint point) {
- // Algorithm:
- // (1) If the point is within any of the segments along the path, return that value.
- // (2) If the point is within any of the segment end circles along the path, return that value.
- double currentDistance = 0.0;
- for (PathSegment segment : segments) {
- double distance = segment.pathNormalDistance(point);
- if (distance != Double.MAX_VALUE)
- return currentDistance + distance;
- currentDistance += segment.fullNormalDistance;
- }
-
- int segmentIndex = 0;
- currentDistance = 0.0;
- for (SegmentEndpoint endpoint : endPoints) {
- double distance = endpoint.pathNormalDistance(point);
- if (distance != Double.MAX_VALUE)
- return currentDistance + distance;
- if (segmentIndex < segments.size())
- currentDistance += segments.get(segmentIndex++).fullNormalDistance;
- }
-
- return Double.MAX_VALUE;
- }
-
- /**
- * Compute an estimate of "distance" to the GeoPoint.
- * A return value of Double.MAX_VALUE should be returned for
- * points outside of the shape.
- */
- @Override
- public double computeNormalDistance(final double x, final double y, final double z) {
- return computeNormalDistance(new GeoPoint(x, y, z));
- }
-
- /**
- * Compute a squared estimate of the "distance" to the
- * GeoPoint. Double.MAX_VALUE indicates a point outside of the
- * shape.
- */
- @Override
- public double computeSquaredNormalDistance(final GeoPoint point) {
- double pd = computeNormalDistance(point);
- if (pd == Double.MAX_VALUE)
- return pd;
- return pd * pd;
- }
-
- /**
- * Compute a squared estimate of the "distance" to the
- * GeoPoint. Double.MAX_VALUE indicates a point outside of the
- * shape.
- */
- @Override
- public double computeSquaredNormalDistance(final double x, final double y, final double z) {
- return computeSquaredNormalDistance(new GeoPoint(x, y, z));
- }
-
- /**
- * Compute a linear distance to the point.
- */
- @Override
- public double computeLinearDistance(final GeoPoint point) {
- // Algorithm:
- // (1) If the point is within any of the segments along the path, return that value.
- // (2) If the point is within any of the segment end circles along the path, return that value.
- double currentDistance = 0.0;
- for (PathSegment segment : segments) {
- double distance = segment.pathLinearDistance(point);
- if (distance != Double.MAX_VALUE)
- return currentDistance + distance;
- currentDistance += segment.fullLinearDistance;
- }
-
- int segmentIndex = 0;
- currentDistance = 0.0;
- for (SegmentEndpoint endpoint : endPoints) {
- double distance = endpoint.pathLinearDistance(point);
- if (distance != Double.MAX_VALUE)
- return currentDistance + distance;
- if (segmentIndex < segments.size())
- currentDistance += segments.get(segmentIndex++).fullLinearDistance;
- }
-
- return Double.MAX_VALUE;
- }
-
- /**
- * Compute a linear distance to the point.
- */
- @Override
- public double computeLinearDistance(final double x, final double y, final double z) {
- return computeLinearDistance(new GeoPoint(x, y, z));
- }
-
- /**
- * Compute a squared linear distance to the vector.
- */
- @Override
- public double computeSquaredLinearDistance(final GeoPoint point) {
- double pd = computeLinearDistance(point);
- if (pd == Double.MAX_VALUE)
- return pd;
- return pd * pd;
- }
-
- /**
- * Compute a squared linear distance to the vector.
- */
- @Override
- public double computeSquaredLinearDistance(final double x, final double y, final double z) {
- return computeSquaredLinearDistance(new GeoPoint(x, y, z));
- }
-
- /**
- * Compute a true, accurate, great-circle distance.
- * Double.MAX_VALUE indicates a point is outside of the shape.
- */
@Override
- public double computeArcDistance(final GeoPoint point) {
+ protected double distance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
// Algorithm:
// (1) If the point is within any of the segments along the path, return that value.
// (2) If the point is within any of the segment end circles along the path, return that value.
double currentDistance = 0.0;
for (PathSegment segment : segments) {
- double distance = segment.pathDistance(point);
+ double distance = segment.pathDistance(planetModel, distanceStyle, x,y,z);
if (distance != Double.MAX_VALUE)
return currentDistance + distance;
- currentDistance += segment.fullDistance;
+ currentDistance += segment.fullPathDistance(distanceStyle);
}
int segmentIndex = 0;
currentDistance = 0.0;
for (SegmentEndpoint endpoint : endPoints) {
- double distance = endpoint.pathDistance(point);
+ double distance = endpoint.pathDistance(distanceStyle, x, y, z);
if (distance != Double.MAX_VALUE)
return currentDistance + distance;
if (segmentIndex < segments.size())
- currentDistance += segments.get(segmentIndex++).fullDistance;
+ currentDistance += segments.get(segmentIndex++).fullPathDistance(distanceStyle);
}
return Double.MAX_VALUE;
}
@Override
- public boolean isWithin(final Vector point) {
- //System.err.println("Assessing whether point "+point+" is within geopath "+this);
- for (SegmentEndpoint pathPoint : endPoints) {
- if (pathPoint.isWithin(point)) {
- //System.err.println(" point is within SegmentEndpoint "+pathPoint);
- return true;
- }
- }
- for (PathSegment pathSegment : segments) {
- if (pathSegment.isWithin(point)) {
- //System.err.println(" point is within PathSegment "+pathSegment);
- return true;
- }
- }
- //System.err.println(" point is not within geopath");
- return false;
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ double minDistance = Double.MAX_VALUE;
+ for (final SegmentEndpoint endpoint : endPoints) {
+ final double newDistance = endpoint.outsideDistance(distanceStyle, x,y,z);
+ if (newDistance < minDistance)
+ minDistance = newDistance;
+ }
+ for (final PathSegment segment : segments) {
+ final double newDistance = segment.outsideDistance(planetModel, distanceStyle, x, y, z);
+ if (newDistance < minDistance)
+ minDistance = newDistance;
+ }
+ return minDistance;
}
@Override
@@ -380,15 +255,6 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
return false;
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
bounds = super.getBounds(bounds);
@@ -562,22 +428,14 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
return circlePlane.isWithin(x, y, z);
}
- public double pathDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.point.arcDistance(point);
- }
-
- public double pathNormalDistance(final GeoPoint point) {
- if (!isWithin(point))
+ public double pathDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ if (!isWithin(x,y,z))
return Double.MAX_VALUE;
- return this.point.normalDistance(point);
+ return distanceStyle.computeDistance(this.point, x, y, z);
}
- public double pathLinearDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
- return this.point.linearDistance(point);
+ public double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(this.point, x, y, z);
}
public boolean intersects(final PlanetModel planetModel, final Plane p, final GeoPoint[] notablePoints, final Membership[] bounds) {
@@ -614,14 +472,12 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
}
/**
- * This is the precalculated data for a path segment.
+ * This is the pre-calculated data for a path segment.
*/
public static class PathSegment {
public final GeoPoint start;
public final GeoPoint end;
- public final double fullDistance;
- public final double fullNormalDistance;
- public final double fullLinearDistance;
+ public final Map<DistanceStyle,Double> fullDistanceCache = new HashMap<DistanceStyle,Double>();
public final Plane normalizedConnectingPlane;
public final SidedPlane upperConnectingPlane;
public final SidedPlane lowerConnectingPlane;
@@ -644,9 +500,6 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
this.normalizedConnectingPlane = normalizedConnectingPlane;
this.planeBoundingOffset = planeBoundingOffset;
- fullDistance = start.arcDistance(end);
- fullNormalDistance = start.normalDistance(end);
- fullLinearDistance = start.linearDistance(end);
// Either start or end should be on the correct side
upperConnectingPlane = new SidedPlane(start, normalizedConnectingPlane, -planeBoundingOffset);
lowerConnectingPlane = new SidedPlane(start, normalizedConnectingPlane, planeBoundingOffset);
@@ -684,6 +537,17 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
endCutoffPlanePoints = new GeoPoint[]{URHC, LRHC};
}
+ public double fullPathDistance(final DistanceStyle distanceStyle) {
+ synchronized (fullDistanceCache) {
+ Double dist = fullDistanceCache.get(distanceStyle);
+ if (dist == null) {
+ dist = new Double(distanceStyle.computeDistance(start, end.x, end.y, end.z));
+ fullDistanceCache.put(distanceStyle, dist);
+ }
+ return dist.doubleValue();
+ }
+ }
+
public boolean isWithin(final Vector point) {
//System.err.println(" assessing whether point "+point+" is within path segment "+this);
//System.err.println(" within "+startCutoffPlane+": "+startCutoffPlane.isWithin(point));
@@ -704,74 +568,58 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
lowerConnectingPlane.isWithin(x, y, z);
}
- public double pathDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
-
- // Compute the distance, filling in both components.
- final double perpDistance = Math.PI * 0.5 - Tools.safeAcos(Math.abs(normalizedConnectingPlane.evaluate(point)));
- final Plane normalizedPerpPlane = new Plane(normalizedConnectingPlane, point).normalize();
- final double pathDistance = Math.PI * 0.5 - Tools.safeAcos(Math.abs(normalizedPerpPlane.evaluate(start)));
- return perpDistance + pathDistance;
- }
-
- public double pathNormalDistance(final GeoPoint point) {
- if (!isWithin(point))
- return Double.MAX_VALUE;
-
- final double pointEval = Math.abs(normalizedConnectingPlane.evaluate(point));
-
- // Want no allocations or expensive operations! so we do this the hard way
- final double perpX = normalizedConnectingPlane.y * point.z - normalizedConnectingPlane.z * point.y;
- final double perpY = normalizedConnectingPlane.z * point.x - normalizedConnectingPlane.x * point.z;
- final double perpZ = normalizedConnectingPlane.x * point.y - normalizedConnectingPlane.y * point.x;
-
- // If we have a degenerate line, then just compute the normal distance from point x to the start
- if (Math.abs(perpX) < Vector.MINIMUM_RESOLUTION && Math.abs(perpY) < Vector.MINIMUM_RESOLUTION && Math.abs(perpZ) < Vector.MINIMUM_RESOLUTION)
- return point.normalDistance(start);
-
- final double normFactor = 1.0 / Math.sqrt(perpX * perpX + perpY * perpY + perpZ * perpZ);
- final double perpEval = Math.abs(perpX * start.x + perpY * start.y + perpZ * start.z);
- return perpEval * normFactor + pointEval;
- }
-
- public double pathLinearDistance(final GeoPoint point) {
- if (!isWithin(point))
+ public double pathDistance(final PlanetModel planetModel, final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ if (!isWithin(x,y,z))
return Double.MAX_VALUE;
- // We have a normalized connecting plane.
- // First, compute the perpendicular plane.
+ // (1) Compute normalizedPerpPlane. If degenerate, then return point distance from start to point.
// Want no allocations or expensive operations! so we do this the hard way
- final double perpX = normalizedConnectingPlane.y * point.z - normalizedConnectingPlane.z * point.y;
- final double perpY = normalizedConnectingPlane.z * point.x - normalizedConnectingPlane.x * point.z;
- final double perpZ = normalizedConnectingPlane.x * point.y - normalizedConnectingPlane.y * point.x;
-
- // If we have a degenerate line, then just compute the normal distance from point x to the start
- if (Math.abs(perpX) < Vector.MINIMUM_RESOLUTION && Math.abs(perpY) < Vector.MINIMUM_RESOLUTION && Math.abs(perpZ) < Vector.MINIMUM_RESOLUTION)
- return point.linearDistance(start);
-
- // Next, we need the vector of the line, which is the cross product of the normalized connecting plane
- // and the perpendicular plane that we just calculated.
- final double lineX = normalizedConnectingPlane.y * perpZ - normalizedConnectingPlane.z * perpY;
- final double lineY = normalizedConnectingPlane.z * perpX - normalizedConnectingPlane.x * perpZ;
- final double lineZ = normalizedConnectingPlane.x * perpY - normalizedConnectingPlane.y * perpX;
-
- // Now, compute a normalization factor
- final double normalizer = 1.0 / Math.sqrt(lineX * lineX + lineY * lineY + lineZ * lineZ);
-
- // Pick which point by using bounding planes
- double normLineX = lineX * normalizer;
- double normLineY = lineY * normalizer;
- double normLineZ = lineZ * normalizer;
- if (!startCutoffPlane.isWithin(normLineX, normLineY, normLineZ) ||
- !endCutoffPlane.isWithin(normLineX, normLineY, normLineZ)) {
- normLineX = -normLineX;
- normLineY = -normLineY;
- normLineZ = -normLineZ;
+ final double perpX = normalizedConnectingPlane.y * z - normalizedConnectingPlane.z * y;
+ final double perpY = normalizedConnectingPlane.z * x - normalizedConnectingPlane.x * z;
+ final double perpZ = normalizedConnectingPlane.x * y - normalizedConnectingPlane.y * x;
+ final double magnitude = Math.sqrt(perpX * perpX + perpY * perpY + perpZ * perpZ);
+ if (Math.abs(magnitude) < Vector.MINIMUM_RESOLUTION)
+ return distanceStyle.computeDistance(start, x,y,z);
+ final double normFactor = 1.0/magnitude;
+ final Plane normalizedPerpPlane = new Plane(perpX * normFactor, perpY * normFactor, perpZ * normFactor, 0.0);
+
+ // Old computation: too expensive, because it calculates the intersection point twice.
+ //return distanceStyle.computeDistance(planetModel, normalizedConnectingPlane, x, y, z, startCutoffPlane, endCutoffPlane) +
+ // distanceStyle.computeDistance(planetModel, normalizedPerpPlane, start.x, start.y, start.z, upperConnectingPlane, lowerConnectingPlane);
+
+ final GeoPoint[] intersectionPoints = normalizedConnectingPlane.findIntersections(planetModel, normalizedPerpPlane);
+ GeoPoint thePoint;
+ if (intersectionPoints.length == 0)
+ throw new RuntimeException("Can't find world intersection for point x="+x+" y="+y+" z="+z);
+ else if (intersectionPoints.length == 1)
+ thePoint = intersectionPoints[0];
+ else {
+ if (startCutoffPlane.isWithin(intersectionPoints[0]) && endCutoffPlane.isWithin(intersectionPoints[0]))
+ thePoint = intersectionPoints[0];
+ else if (startCutoffPlane.isWithin(intersectionPoints[1]) && endCutoffPlane.isWithin(intersectionPoints[1]))
+ thePoint = intersectionPoints[1];
+ else
+ throw new RuntimeException("Can't find world intersection for point x="+x+" y="+y+" z="+z);
}
-
- // Compute linear distance for the two points
- return point.linearDistance(normLineX, normLineY, normLineZ) + start.linearDistance(normLineX, normLineY, normLineZ);
+ return distanceStyle.computeDistance(thePoint, x, y, z) + distanceStyle.computeDistance(start, thePoint.x, thePoint.y, thePoint.z);
+ }
+
+ public double outsideDistance(final PlanetModel planetModel, final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double upperDistance = distanceStyle.computeDistance(planetModel, upperConnectingPlane, x,y,z, lowerConnectingPlane, startCutoffPlane, endCutoffPlane);
+ final double lowerDistance = distanceStyle.computeDistance(planetModel, lowerConnectingPlane, x,y,z, upperConnectingPlane, startCutoffPlane, endCutoffPlane);
+ final double startDistance = distanceStyle.computeDistance(planetModel, startCutoffPlane, x,y,z, endCutoffPlane, lowerConnectingPlane, upperConnectingPlane);
+ final double endDistance = distanceStyle.computeDistance(planetModel, endCutoffPlane, x,y,z, startCutoffPlane, lowerConnectingPlane, upperConnectingPlane);
+ final double ULHCDistance = distanceStyle.computeDistance(ULHC, x,y,z);
+ final double URHCDistance = distanceStyle.computeDistance(URHC, x,y,z);
+ final double LLHCDistance = distanceStyle.computeDistance(LLHC, x,y,z);
+ final double LRHCDistance = distanceStyle.computeDistance(LRHC, x,y,z);
+ return Math.min(
+ Math.min(
+ Math.min(upperDistance,lowerDistance),
+ Math.min(startDistance,endDistance)),
+ Math.min(
+ Math.min(ULHCDistance, URHCDistance),
+ Math.min(LLHCDistance, LRHCDistance)));
}
public boolean intersects(final PlanetModel planetModel, final Plane p, final GeoPoint[] notablePoints, final Membership[] bounds) {
@@ -790,12 +638,6 @@ public class GeoPath extends GeoBaseExtendedShape implements GeoDistanceShape {
lowerConnectingPlane.recordBounds(planetModel, bounds, upperConnectingPlane, startCutoffPlane, endCutoffPlane);
startCutoffPlane.recordBounds(planetModel, bounds, endCutoffPlane, upperConnectingPlane, lowerConnectingPlane);
endCutoffPlane.recordBounds(planetModel, bounds, startCutoffPlane, upperConnectingPlane, lowerConnectingPlane);
- if (fullDistance >= Math.PI) {
- // Too large a segment basically means that we can confuse the Bounds object. Specifically, if our span exceeds 180 degrees
- // in longitude (which even a segment whose actual length is less than that might if it goes close to a pole).
- // Unfortunately, we can get arbitrarily close to the pole, so this may still not work in all cases.
- bounds.noLongitudeBound();
- }
}
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPoint.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPoint.java
index fd67ce8403f..6194a5f312e 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPoint.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoPoint.java
@@ -18,7 +18,7 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*/
/**
- * This class represents a point on the surface of a unit sphere.
+ * This class represents a point on the surface of a sphere or ellipsoid.
*
* @lucene.experimental
*/
@@ -124,6 +124,16 @@ public class GeoPoint extends Vector {
return Tools.safeAcos(dotProduct(v)/(magnitude() * v.magnitude()));
}
+ /** Compute an arc distance between two points.
+ * @param x is the x part of the second point.
+ * @param y is the y part of the second point.
+ * @param z is the z part of the second point.
+ * @return the angle, in radians, between the two points.
+ */
+ public double arcDistance(final double x, final double y, final double z) {
+ return Tools.safeAcos(dotProduct(x,y,z)/(magnitude() * Vector.magnitude(x,y,z)));
+ }
+
/** Compute the latitude for the point.
* @return the latitude.
*/
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoRectangle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoRectangle.java
index 166a6684446..2555eca1992 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoRectangle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoRectangle.java
@@ -137,14 +137,6 @@ public class GeoRectangle extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return topPlane.isWithin(point) &&
- bottomPlane.isWithin(point) &&
- leftPlane.isWithin(point) &&
- rightPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return topPlane.isWithin(x, y, z) &&
@@ -169,11 +161,6 @@ public class GeoRectangle extends GeoBaseBBox {
return edgePoints;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -187,15 +174,6 @@ public class GeoRectangle extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, leftPlane, topPlane, bottomPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -242,6 +220,27 @@ public class GeoRectangle extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double topDistance = distanceStyle.computeDistance(planetModel, topPlane, x,y,z, bottomPlane, leftPlane, rightPlane);
+ final double bottomDistance = distanceStyle.computeDistance(planetModel, bottomPlane, x,y,z, topPlane, leftPlane, rightPlane);
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, rightPlane, topPlane, bottomPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, leftPlane, topPlane, bottomPlane);
+
+ final double ULHCDistance = distanceStyle.computeDistance(ULHC, x,y,z);
+ final double URHCDistance = distanceStyle.computeDistance(URHC, x,y,z);
+ final double LRHCDistance = distanceStyle.computeDistance(LRHC, x,y,z);
+ final double LLHCDistance = distanceStyle.computeDistance(LLHC, x,y,z);
+
+ return Math.min(
+ Math.min(
+ Math.min(topDistance, bottomDistance),
+ Math.min(leftDistance, rightDistance)),
+ Math.min(
+ Math.min(ULHCDistance, URHCDistance),
+ Math.min(LRHCDistance, LLHCDistance)));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoRectangle))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoShape.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoShape.java
index ceb303e63ea..cb6968bb007 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoShape.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoShape.java
@@ -64,8 +64,4 @@ public interface GeoShape extends Membership {
*/
public Bounds getBounds(final Bounds bounds);
- /**
- * Equals
- */
- public boolean equals(Object o);
}
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthLatitudeZone.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthLatitudeZone.java
index aa8ae353fba..c0a9efc1cad 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthLatitudeZone.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthLatitudeZone.java
@@ -59,11 +59,6 @@ public class GeoSouthLatitudeZone extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, -Math.PI, Math.PI);
}
- @Override
- public boolean isWithin(final Vector point) {
- return topPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return topPlane.isWithin(x, y, z);
@@ -148,6 +143,11 @@ public class GeoSouthLatitudeZone extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return distanceStyle.computeDistance(planetModel, topPlane, x,y,z);
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoSouthLatitudeZone))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthRectangle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthRectangle.java
index 1270fef4be9..592801453fa 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthRectangle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoSouthRectangle.java
@@ -21,7 +21,7 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
* Bounding box limited on three sides (top lat, left lon, right lon). The
* other corner is the south pole.
* The left-right maximum extent for this shape is PI; for anything larger, use
- * GeoWideSouthRectangle.
+ * {@link GeoWideSouthRectangle}.
*
* @lucene.internal
*/
@@ -123,13 +123,6 @@ public class GeoSouthRectangle extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return topPlane.isWithin(point) &&
- leftPlane.isWithin(point) &&
- rightPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return topPlane.isWithin(x, y, z) &&
@@ -152,11 +145,6 @@ public class GeoSouthRectangle extends GeoBaseBBox {
return edgePoints;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -169,15 +157,6 @@ public class GeoSouthRectangle extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, leftPlane, topPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -223,6 +202,22 @@ public class GeoSouthRectangle extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double topDistance = distanceStyle.computeDistance(planetModel, topPlane, x,y,z, leftPlane, rightPlane);
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, rightPlane, topPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, leftPlane, topPlane);
+
+ final double ULHCDistance = distanceStyle.computeDistance(ULHC, x,y,z);
+ final double URHCDistance = distanceStyle.computeDistance(URHC, x,y,z);
+
+ return Math.min(
+ Math.min(
+ topDistance,
+ Math.min(leftDistance, rightDistance)),
+ Math.min(ULHCDistance, URHCDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoSouthRectangle))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideDegenerateHorizontalLine.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideDegenerateHorizontalLine.java
index 0346dd04830..05b307482e9 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideDegenerateHorizontalLine.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideDegenerateHorizontalLine.java
@@ -116,15 +116,6 @@ public class GeoWideDegenerateHorizontalLine extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- if (point == null)
- return false;
- return plane.evaluateIsZero(point) &&
- (leftPlane.isWithin(point) ||
- rightPlane.isWithin(point));
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return plane.evaluateIsZero(x, y, z) &&
@@ -142,11 +133,6 @@ public class GeoWideDegenerateHorizontalLine extends GeoBaseBBox {
return Math.max(topAngle, bottomAngle);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -164,15 +150,6 @@ public class GeoWideDegenerateHorizontalLine extends GeoBaseBBox {
return p.intersects(planetModel, plane, notablePoints, planePoints, bounds, eitherBound);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -195,6 +172,18 @@ public class GeoWideDegenerateHorizontalLine extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double distance = distanceStyle.computeDistance(planetModel, plane, x,y,z, eitherBound);
+
+ final double LHCDistance = distanceStyle.computeDistance(LHC, x,y,z);
+ final double RHCDistance = distanceStyle.computeDistance(RHC, x,y,z);
+
+ return Math.min(
+ distance,
+ Math.min(LHCDistance, RHCDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoWideDegenerateHorizontalLine))
@@ -220,11 +209,6 @@ public class GeoWideDegenerateHorizontalLine extends GeoBaseBBox {
public EitherBound() {
}
- @Override
- public boolean isWithin(final Vector v) {
- return leftPlane.isWithin(v) || rightPlane.isWithin(v);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return leftPlane.isWithin(x, y, z) || rightPlane.isWithin(x, y, z);
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideLongitudeSlice.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideLongitudeSlice.java
index ff24c496dc1..ddcd3bf329f 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideLongitudeSlice.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideLongitudeSlice.java
@@ -91,12 +91,6 @@ public class GeoWideLongitudeSlice extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, Math.PI * 0.5, -Math.PI * 0.5, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return leftPlane.isWithin(point) ||
- rightPlane.isWithin(point);
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return leftPlane.isWithin(x, y, z) ||
@@ -112,11 +106,6 @@ public class GeoWideLongitudeSlice extends GeoBaseBBox {
return Math.max(Math.PI * 0.5, extent * 0.5);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -135,15 +124,6 @@ public class GeoWideLongitudeSlice extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, planePoints, bounds);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -177,6 +157,21 @@ public class GeoWideLongitudeSlice extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ // Because the rectangle exceeds 180 degrees, it is safe to compute the horizontally
+ // unbounded distance to both the left and the right and only take the minimum of the two.
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z);
+
+ final double northDistance = distanceStyle.computeDistance(planetModel.NORTH_POLE, x,y,z);
+ final double southDistance = distanceStyle.computeDistance(planetModel.SOUTH_POLE, x,y,z);
+
+ return Math.min(
+ Math.min(leftDistance, rightDistance),
+ Math.min(northDistance, southDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoWideLongitudeSlice))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideNorthRectangle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideNorthRectangle.java
index 134dad15c43..842e32d9813 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideNorthRectangle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideNorthRectangle.java
@@ -124,14 +124,6 @@ public class GeoWideNorthRectangle extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return
- bottomPlane.isWithin(point) &&
- (leftPlane.isWithin(point) ||
- rightPlane.isWithin(point));
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return
@@ -150,11 +142,6 @@ public class GeoWideNorthRectangle extends GeoBaseBBox {
return Math.max(centerAngle, bottomAngle);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -175,15 +162,6 @@ public class GeoWideNorthRectangle extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, bottomPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -231,6 +209,24 @@ public class GeoWideNorthRectangle extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double bottomDistance = distanceStyle.computeDistance(planetModel, bottomPlane, x,y,z, eitherBound);
+ // Because the rectangle exceeds 180 degrees, it is safe to compute the horizontally
+ // unbounded distance to both the left and the right and only take the minimum of the two.
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, bottomPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, bottomPlane);
+
+ final double LRHCDistance = distanceStyle.computeDistance(LRHC, x,y,z);
+ final double LLHCDistance = distanceStyle.computeDistance(LLHC, x,y,z);
+
+ return Math.min(
+ Math.min(
+ bottomDistance,
+ Math.min(leftDistance, rightDistance)),
+ Math.min(LRHCDistance, LLHCDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoWideNorthRectangle))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideRectangle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideRectangle.java
index a80b6d1689e..66735f16623 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideRectangle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideRectangle.java
@@ -141,14 +141,6 @@ public class GeoWideRectangle extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return topPlane.isWithin(point) &&
- bottomPlane.isWithin(point) &&
- (leftPlane.isWithin(point) ||
- rightPlane.isWithin(point));
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return topPlane.isWithin(x, y, z) &&
@@ -193,15 +185,6 @@ public class GeoWideRectangle extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, topPlane, bottomPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -249,6 +232,29 @@ public class GeoWideRectangle extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double topDistance = distanceStyle.computeDistance(planetModel, topPlane, x,y,z, bottomPlane, eitherBound);
+ final double bottomDistance = distanceStyle.computeDistance(planetModel, bottomPlane, x,y,z, topPlane, eitherBound);
+ // Because the rectangle exceeds 180 degrees, it is safe to compute the horizontally
+ // unbounded distance to both the left and the right and only take the minimum of the two.
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, topPlane, bottomPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, topPlane, bottomPlane);
+
+ final double ULHCDistance = distanceStyle.computeDistance(ULHC, x,y,z);
+ final double URHCDistance = distanceStyle.computeDistance(URHC, x,y,z);
+ final double LRHCDistance = distanceStyle.computeDistance(LRHC, x,y,z);
+ final double LLHCDistance = distanceStyle.computeDistance(LLHC, x,y,z);
+
+ return Math.min(
+ Math.min(
+ Math.min(topDistance, bottomDistance),
+ Math.min(leftDistance, rightDistance)),
+ Math.min(
+ Math.min(ULHCDistance, URHCDistance),
+ Math.min(LRHCDistance, LLHCDistance)));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoWideRectangle))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideSouthRectangle.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideSouthRectangle.java
index 816f36b05e4..3899afd3db8 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideSouthRectangle.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWideSouthRectangle.java
@@ -125,13 +125,6 @@ public class GeoWideSouthRectangle extends GeoBaseBBox {
return GeoBBoxFactory.makeGeoBBox(planetModel, newTopLat, newBottomLat, newLeftLon, newRightLon);
}
- @Override
- public boolean isWithin(final Vector point) {
- return topPlane.isWithin(point) &&
- (leftPlane.isWithin(point) ||
- rightPlane.isWithin(point));
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return topPlane.isWithin(x, y, z) &&
@@ -149,11 +142,6 @@ public class GeoWideSouthRectangle extends GeoBaseBBox {
return Math.max(centerAngle, topAngle);
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
return centerPoint;
@@ -173,15 +161,6 @@ public class GeoWideSouthRectangle extends GeoBaseBBox {
p.intersects(planetModel, rightPlane, notablePoints, rightPlanePoints, bounds, topPlane);
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -228,12 +207,30 @@ public class GeoWideSouthRectangle extends GeoBaseBBox {
return DISJOINT;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ final double topDistance = distanceStyle.computeDistance(planetModel, topPlane, x,y,z, eitherBound);
+ // Because the rectangle exceeds 180 degrees, it is safe to compute the horizontally
+ // unbounded distance to both the left and the right and only take the minimum of the two.
+ final double leftDistance = distanceStyle.computeDistance(planetModel, leftPlane, x,y,z, topPlane);
+ final double rightDistance = distanceStyle.computeDistance(planetModel, rightPlane, x,y,z, topPlane);
+
+ final double ULHCDistance = distanceStyle.computeDistance(ULHC, x,y,z);
+ final double URHCDistance = distanceStyle.computeDistance(URHC, x,y,z);
+
+ return Math.min(
+ Math.min(
+ topDistance,
+ Math.min(leftDistance, rightDistance)),
+ Math.min(ULHCDistance, URHCDistance));
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoWideSouthRectangle))
return false;
GeoWideSouthRectangle other = (GeoWideSouthRectangle) o;
- return super.equals(other) && other.ULHC.equals(ULHC) && other.URHC.equals(URHC);
+ return super.equals(o) && other.ULHC.equals(ULHC) && other.URHC.equals(URHC);
}
@Override
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWorld.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWorld.java
index 0a6bdedaed5..1e9e0ded3f3 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWorld.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/GeoWorld.java
@@ -41,22 +41,12 @@ public class GeoWorld extends GeoBaseBBox {
return Math.PI;
}
- /**
- * Returns the center of a circle into which the area will be inscribed.
- *
- * @return the center.
- */
@Override
public GeoPoint getCenter() {
// Totally arbitrary
return originPoint;
}
- @Override
- public boolean isWithin(final Vector point) {
- return true;
- }
-
@Override
public boolean isWithin(final double x, final double y, final double z) {
return true;
@@ -72,15 +62,6 @@ public class GeoWorld extends GeoBaseBBox {
return false;
}
- /**
- * Compute longitude/latitude bounds for the shape.
- *
- * @param bounds is the optional input bounds object. If this is null,
- * a bounds object will be created. Otherwise, the input object will be modified.
- * @return a Bounds object describing the shape's bounds. If the bounds cannot
- * be computed, then return a Bounds object with noLongitudeBound,
- * noTopLatitudeBound, and noBottomLatitudeBound.
- */
@Override
public Bounds getBounds(Bounds bounds) {
if (bounds == null)
@@ -98,6 +79,11 @@ public class GeoWorld extends GeoBaseBBox {
return OVERLAPS;
}
+ @Override
+ protected double outsideDistance(final DistanceStyle distanceStyle, final double x, final double y, final double z) {
+ return 0.0;
+ }
+
@Override
public boolean equals(Object o) {
if (!(o instanceof GeoWorld))
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/LinearDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/LinearDistance.java
new file mode 100644
index 00000000000..7184f20a4a9
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/LinearDistance.java
@@ -0,0 +1,51 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Linear distance computation style.
+ *
+ * @lucene.experimental
+ */
+public class LinearDistance implements DistanceStyle {
+
+ public final static LinearDistance INSTANCE = new LinearDistance();
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final GeoPoint point2) {
+ return point1.linearDistance(point2);
+ }
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final double x2, final double y2, final double z2) {
+ return point1.linearDistance(x2,y2,z2);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final GeoPoint point, final Membership... bounds) {
+ return plane.linearDistance(planetModel, point, bounds);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds) {
+ return plane.linearDistance(planetModel, x,y,z, bounds);
+ }
+
+}
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/LinearSquaredDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/LinearSquaredDistance.java
new file mode 100644
index 00000000000..accc0703930
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/LinearSquaredDistance.java
@@ -0,0 +1,51 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Linear squared distance computation style.
+ *
+ * @lucene.experimental
+ */
+public class LinearSquaredDistance implements DistanceStyle {
+
+ public final static LinearSquaredDistance INSTANCE = new LinearSquaredDistance();
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final GeoPoint point2) {
+ return point1.linearDistanceSquared(point2);
+ }
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final double x2, final double y2, final double z2) {
+ return point1.linearDistanceSquared(x2,y2,z2);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final GeoPoint point, final Membership... bounds) {
+ return plane.linearDistanceSquared(planetModel, point, bounds);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds) {
+ return plane.linearDistanceSquared(planetModel, x,y,z, bounds);
+ }
+
+}
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Membership.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Membership.java
index d21e544cf92..6233b364bcc 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Membership.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Membership.java
@@ -18,7 +18,7 @@ package org.apache.lucene.spatial.spatial4j.geo3d;
*/
/**
- * Interface describing 3d shape membership methods.
+ * Implemented by Geo3D shapes that can calculate if a point is within it or not.
*
* @lucene.experimental
*/
@@ -30,7 +30,9 @@ public interface Membership {
* @param point is the point to check.
* @return true if the point is within this shape
*/
- public boolean isWithin(final Vector point);
+ public default boolean isWithin(final Vector point) {
+ return isWithin(point.x, point.y, point.z);
+ }
/**
* Check if a point is within this shape.
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/NormalDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/NormalDistance.java
new file mode 100644
index 00000000000..90b1e3c8668
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/NormalDistance.java
@@ -0,0 +1,51 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Normal distance computation style.
+ *
+ * @lucene.experimental
+ */
+public class NormalDistance implements DistanceStyle {
+
+ public final static NormalDistance INSTANCE = new NormalDistance();
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final GeoPoint point2) {
+ return point1.normalDistance(point2);
+ }
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final double x2, final double y2, final double z2) {
+ return point1.normalDistance(x2,y2,z2);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final GeoPoint point, final Membership... bounds) {
+ return plane.normalDistance(point, bounds);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds) {
+ return plane.normalDistance(x,y,z, bounds);
+ }
+
+}
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/NormalSquaredDistance.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/NormalSquaredDistance.java
new file mode 100644
index 00000000000..be3ded2f648
--- /dev/null
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/NormalSquaredDistance.java
@@ -0,0 +1,51 @@
+package org.apache.lucene.spatial.spatial4j.geo3d;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Normal squared distance computation style.
+ *
+ * @lucene.experimental
+ */
+public class NormalSquaredDistance implements DistanceStyle {
+
+ public final static NormalSquaredDistance INSTANCE = new NormalSquaredDistance();
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final GeoPoint point2) {
+ return point1.normalDistanceSquared(point2);
+ }
+
+ @Override
+ public double computeDistance(final GeoPoint point1, final double x2, final double y2, final double z2) {
+ return point1.normalDistanceSquared(x2,y2,z2);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final GeoPoint point, final Membership... bounds) {
+ return plane.normalDistanceSquared(point, bounds);
+ }
+
+ @Override
+ public double computeDistance(final PlanetModel planetModel, final Plane plane, final double x, final double y, final double z, final Membership... bounds) {
+ return plane.normalDistanceSquared(x,y,z, bounds);
+ }
+
+}
+
+
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Plane.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Plane.java
index b5f79dfdc17..86934897a63 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Plane.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Plane.java
@@ -143,6 +143,178 @@ public class Plane extends Vector {
return new Plane(normVect, this.D);
}
+ /** @see #arcDistance(PlanetModel, double, double, double, Membership...) */
+ public double arcDistance(final PlanetModel planetModel, final GeoPoint v, final Membership... bounds) {
+ return arcDistance(planetModel, v.x, v.y, v.z, bounds);
+ }
+
+ /**
+ * Compute arc distance from plane to a vector.
+ * @param x is the x vector value.
+ * @param y is the y vector value.
+ * @param z is the z vector value.
+ * @return the arc distance.
+ */
+ public double arcDistance(final PlanetModel planetModel, final double x, final double y, final double z, final Membership... bounds) {
+
+ if (evaluateIsZero(x,y,z)) {
+ if (meetsAllBounds(x,y,z, bounds))
+ return 0.0;
+ return Double.MAX_VALUE;
+ }
+
+ // First, compute the perpendicular plane.
+ final Plane perpPlane = new Plane(this.y * z - this.z * y, this.z * x - this.x * z, this.x * y - this.y * x, 0.0);
+
+ // We need to compute the intersection of two planes on the geo surface: this one, and its perpendicular.
+ // Then, we need to choose which of the two points we want to compute the distance to. We pick the
+ // shorter distance always.
+
+ final GeoPoint[] intersectionPoints = findIntersections(planetModel, perpPlane);
+
+ // For each point, compute a linear distance, and take the minimum of them
+ double minDistance = Double.MAX_VALUE;
+
+ for (final GeoPoint intersectionPoint : intersectionPoints) {
+ if (meetsAllBounds(intersectionPoint, bounds)) {
+ final double theDistance = intersectionPoint.arcDistance(x,y,z);
+ if (theDistance < minDistance) {
+ minDistance = theDistance;
+ }
+ }
+ }
+ return minDistance;
+
+ }
+
+ /**
+ * Compute normal distance from plane to a vector.
+ * @param v is the vector.
+ * @return the normal distance.
+ */
+ public double normalDistance(final Vector v, final Membership... bounds) {
+ return normalDistance(v.x, v.y, v.z, bounds);
+ }
+
+ /**
+ * Compute normal distance from plane to a vector.
+ * @param x is the vector x.
+ * @param y is the vector y.
+ * @param z is the vector z.
+ * @return the normal distance.
+ */
+ public double normalDistance(final double x, final double y, final double z, final Membership... bounds) {
+
+ final double dist = evaluate(x,y,z);
+ final double perpX = x - dist * this.x;
+ final double perpY = y - dist * this.y;
+ final double perpZ = z - dist * this.z;
+
+ if (!meetsAllBounds(perpX, perpY, perpZ, bounds)) {
+ return Double.MAX_VALUE;
+ }
+
+ return Math.abs(dist);
+ }
+
+ /**
+ * Compute normal distance squared from plane to a vector.
+ * @param v is the vector.
+ * @return the normal distance squared.
+ */
+ public double normalDistanceSquared(final Vector v, final Membership... bounds) {
+ return normalDistanceSquared(v.x, v.y, v.z, bounds);
+ }
+
+ /**
+ * Compute normal distance squared from plane to a vector.
+ * @param x is the vector x.
+ * @param y is the vector y.
+ * @param z is the vector z.
+ * @return the normal distance squared.
+ */
+ public double normalDistanceSquared(final double x, final double y, final double z, final Membership... bounds) {
+ final double normal = normalDistance(x,y,z,bounds);
+ if (normal == Double.MAX_VALUE)
+ return normal;
+ return normal * normal;
+ }
+
+ /**
+ * Compute linear distance from plane to a vector. This is defined
+ * as the distance from the given point to the nearest intersection of
+ * this plane with the planet surface.
+ * @param v is the vector.
+ * @return the linear distance.
+ */
+ public double linearDistance(final PlanetModel planetModel, final GeoPoint v, final Membership... bounds) {
+ return linearDistance(planetModel, v.x, v.y, v.z, bounds);
+ }
+
+ /**
+ * Compute linear distance from plane to a vector. This is defined
+ * as the distance from the given point to the nearest intersection of
+ * this plane with the planet surface.
+ * @param x is the vector x.
+ * @param y is the vector y.
+ * @param z is the vector z.
+ * @return the linear distance.
+ */
+ public double linearDistance(final PlanetModel planetModel, final double x, final double y, final double z, final Membership... bounds) {
+ if (evaluateIsZero(x,y,z)) {
+ if (meetsAllBounds(x,y,z, bounds))
+ return 0.0;
+ return Double.MAX_VALUE;
+ }
+
+ // First, compute the perpendicular plane.
+ final Plane perpPlane = new Plane(this.y * z - this.z * y, this.z * x - this.x * z, this.x * y - this.y * x, 0.0);
+
+ // We need to compute the intersection of two planes on the geo surface: this one, and its perpendicular.
+ // Then, we need to choose which of the two points we want to compute the distance to. We pick the
+ // shorter distance always.
+
+ final GeoPoint[] intersectionPoints = findIntersections(planetModel, perpPlane);
+
+ // For each point, compute a linear distance, and take the minimum of them
+ double minDistance = Double.MAX_VALUE;
+
+ for (final GeoPoint intersectionPoint : intersectionPoints) {
+ if (meetsAllBounds(intersectionPoint, bounds)) {
+ final double theDistance = intersectionPoint.linearDistance(x,y,z);
+ if (theDistance < minDistance) {
+ minDistance = theDistance;
+ }
+ }
+ }
+ return minDistance;
+ }
+
+ /**
+ * Compute linear distance squared from plane to a vector. This is defined
+ * as the distance from the given point to the nearest intersection of
+ * this plane with the planet surface.
+ * @param v is the vector.
+ * @return the linear distance squared.
+ */
+ public double linearDistanceSquared(final PlanetModel planetModel, final GeoPoint v, final Membership... bounds) {
+ return linearDistanceSquared(planetModel, v.x, v.y, v.z, bounds);
+ }
+
+ /**
+ * Compute linear distance squared from plane to a vector. This is defined
+ * as the distance from the given point to the nearest intersection of
+ * this plane with the planet surface.
+ * @param x is the vector x.
+ * @param y is the vector y.
+ * @param z is the vector z.
+ * @return the linear distance squared.
+ */
+ public double linearDistanceSquared(final PlanetModel planetModel, final double x, final double y, final double z, final Membership... bounds) {
+ final double linearDistance = linearDistance(planetModel, x, y, z, bounds);
+ return linearDistance * linearDistance;
+ }
+
/**
* Find points on the boundary of the intersection of a plane and the unit sphere,
* given a starting point, and ending point, and a list of proportions of the arc (e.g. 0.25, 0.5, 0.75).
@@ -320,7 +492,8 @@ public class Plane extends Vector {
*/
protected GeoPoint[] findIntersections(final PlanetModel planetModel, final Plane q, final Membership[] bounds, final Membership[] moreBounds) {
//System.err.println("Looking for intersection between plane "+this+" and plane "+q+" within bounds");
- final Vector lineVector = new Vector(this, q);
+ // Unnormalized, unchecked...
+ final Vector lineVector = new Vector(y * q.z - z * q.y, z * q.x - x * q.z, x * q.y - y * q.x);
if (Math.abs(lineVector.x) < MINIMUM_RESOLUTION && Math.abs(lineVector.y) < MINIMUM_RESOLUTION && Math.abs(lineVector.z) < MINIMUM_RESOLUTION) {
// Degenerate case: parallel planes
//System.err.println(" planes are parallel - no intersection");
@@ -860,18 +1033,27 @@ public class Plane extends Vector {
return evaluateIsZero(-p.x * p.D * denom, -p.y * p.D * denom, -p.z * p.D * denom);
}
- protected static boolean meetsAllBounds(final GeoPoint p, final Membership[] bounds, final Membership[] moreBounds) {
+ protected static boolean meetsAllBounds(final Vector p, final Membership[] bounds) {
+ return meetsAllBounds(p.x, p.y, p.z, bounds);
+ }
+
+ protected static boolean meetsAllBounds(final double x, final double y, final double z, final Membership[] bounds) {
for (final Membership bound : bounds) {
- if (!bound.isWithin(p))
- return false;
- }
- for (final Membership bound : moreBounds) {
- if (!bound.isWithin(p))
+ if (!bound.isWithin(x,y,z))
return false;
}
return true;
}
+ protected static boolean meetsAllBounds(final Vector p, final Membership[] bounds, final Membership[] moreBounds) {
+ return meetsAllBounds(p.x, p.y, p.z, bounds, moreBounds);
+ }
+
+ protected static boolean meetsAllBounds(final double x, final double y, final double z, final Membership[] bounds,
+ final Membership[] moreBounds) {
+ return meetsAllBounds(x,y,z, bounds) && meetsAllBounds(x,y,z, moreBounds);
+ }
+
/**
* Find a sample point on the intersection between two planes and the unit sphere.
*/
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/SidedPlane.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/SidedPlane.java
index e00b385f355..ee421ce04c4 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/SidedPlane.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/SidedPlane.java
@@ -90,57 +90,58 @@ public class SidedPlane extends Plane implements Membership {
public static SidedPlane constructNormalizedPerpendicularSidedPlane(final Vector insidePoint,
final Vector normalVector, final Vector point1, final Vector point2) {
final Vector pointsVector = new Vector(point1.x - point2.x, point1.y - point2.y, point1.z - point2.z);
- final Vector newNormalVector = new Vector(normalVector, pointsVector).normalize();
- if (newNormalVector == null)
+ final Vector newNormalVector = new Vector(normalVector, pointsVector);
+ try {
+ // To construct the plane, we now just need D, which is simply the negative of the evaluation of the circle normal vector at one of the points.
+ return new SidedPlane(insidePoint, newNormalVector, -newNormalVector.dotProduct(point1));
+ } catch (IllegalArgumentException e) {
return null;
- // To construct the plane, we now just need D, which is simply the negative of the evaluation of the circle normal vector at one of the points.
- return new SidedPlane(insidePoint, newNormalVector, -newNormalVector.dotProduct(point1));
+ }
}
/** Construct a sided plane from three points.
*/
public static SidedPlane constructNormalizedThreePointSidedPlane(final Vector insidePoint,
final Vector point1, final Vector point2, final Vector point3) {
- final Vector planeNormal = new Vector(
- new Vector(point1.x - point2.x, point1.y - point2.y, point1.z - point2.z),
- new Vector(point2.x - point3.x, point2.y - point3.y, point2.z - point3.z)).normalize();
- if (planeNormal == null)
+ try {
+ final Vector planeNormal = new Vector(
+ new Vector(point1.x - point2.x, point1.y - point2.y, point1.z - point2.z),
+ new Vector(point2.x - point3.x, point2.y - point3.y, point2.z - point3.z));
+ return new SidedPlane(insidePoint, planeNormal, -planeNormal.dotProduct(point2));
+ } catch (IllegalArgumentException e) {
return null;
- return new SidedPlane(insidePoint, planeNormal, -planeNormal.dotProduct(point2));
+ }
}
- /**
- * Check if a point is within this shape.
- *
- * @param point is the point to check.
- * @return true if the point is within this shape
- */
@Override
- public boolean isWithin(Vector point) {
- double evalResult = evaluate(point);
+ public boolean isWithin(double x, double y, double z) {
+ double evalResult = evaluate(x, y, z);
if (Math.abs(evalResult) < MINIMUM_RESOLUTION)
return true;
double sigNum = Math.signum(evalResult);
return sigNum == this.sigNum;
}
- /**
- * Check if a point is within this shape.
- *
- * @param x is x coordinate of point to check.
- * @param y is y coordinate of point to check.
- * @param z is z coordinate of point to check.
- * @return true if the point is within this shape
- */
@Override
- public boolean isWithin(double x, double y, double z) {
- double evalResult = evaluate(x, y, z);
- if (Math.abs(evalResult) < MINIMUM_RESOLUTION)
- return true;
- double sigNum = Math.signum(evalResult);
- return sigNum == this.sigNum;
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof SidedPlane)) return false;
+ if (!super.equals(o)) return false;
+
+ SidedPlane that = (SidedPlane) o;
+
+ return Double.compare(that.sigNum, sigNum) == 0;
+
}
+ @Override
+ public int hashCode() {
+ int result = super.hashCode();
+ long temp;
+ temp = Double.doubleToLongBits(sigNum);
+ result = 31 * result + (int) (temp ^ (temp >>> 32));
+ return result;
+ }
@Override
public String toString() {
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Vector.java b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Vector.java
index a29249f4c72..86714b37752 100755
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Vector.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/spatial4j/geo3d/Vector.java
@@ -52,7 +52,8 @@ public class Vector {
/**
* Construct a vector that is perpendicular to
* two other (non-zero) vectors. If the vectors are parallel,
- * the result vector will have magnitude 0.
+ * IllegalArgumentException will be thrown.
+ * Produces a normalized final vector.
*
* @param A is the first vector
* @param B is the second
@@ -61,12 +62,25 @@ public class Vector {
// x = u2v3 - u3v2
// y = u3v1 - u1v3
// z = u1v2 - u2v1
-
- this(A.y * B.z - A.z * B.y,
- A.z * B.x - A.x * B.z,
- A.x * B.y - A.y * B.x);
+ final double thisX = A.y * B.z - A.z * B.y;
+ final double thisY = A.z * B.x - A.x * B.z;
+ final double thisZ = A.x * B.y - A.y * B.x;
+ final double magnitude = magnitude(thisX, thisY, thisZ);
+ if (Math.abs(magnitude) < MINIMUM_RESOLUTION) {
+ throw new IllegalArgumentException("Degenerate/parallel vector constructed");
+ }
+ final double inverseMagnitude = 1.0 / magnitude;
+ this.x = thisX * inverseMagnitude;
+ this.y = thisY * inverseMagnitude;
+ this.z = thisZ * inverseMagnitude;
}
+ /** Compute a magnitude of an x,y,z value.
+ */
+ public static double magnitude(final double x, final double y, final double z) {
+ return Math.sqrt(x*x + y*y + z*z);
+ }
+
/**
* Compute a normalized unit vector based on the current vector.
*
@@ -304,7 +318,7 @@ public class Vector {
* @return the magnitude.
*/
public double magnitude() {
- return Math.sqrt(x * x + y * y + z * z);
+ return magnitude(x,y,z);
}
/** Compute the desired magnitude of a unit vector projected to a given
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoBBoxTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoBBoxTest.java
index 58377cc37b9..0fbdf9eef3a 100755
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoBBoxTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoBBoxTest.java
@@ -66,6 +66,9 @@ public class GeoBBoxTest {
assertFalse(box.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -1.1);
assertFalse(box.isWithin(gp));
+ assertEquals(0.1,box.computeOutsideDistance(DistanceStyle.ARC,gp),1e-2);
+ assertEquals(0.1,box.computeOutsideDistance(DistanceStyle.NORMAL,gp),1e-2);
+ assertEquals(0.1,box.computeOutsideDistance(DistanceStyle.NORMAL,gp),1e-2);
// Standard normal Rect box, crossing dateline
box = GeoBBoxFactory.makeGeoBBox(PlanetModel.SPHERE, 0.0, -Math.PI * 0.25, Math.PI - 1.0, -Math.PI + 1.0);
@@ -77,8 +80,8 @@ public class GeoBBoxTest {
assertFalse(box.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -Math.PI + 1.1);
assertFalse(box.isWithin(gp));
- //bad lon: gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -Math.PI - 1.1);
- //assertFalse(box.isWithin(gp));
+ gp = new GeoPoint(PlanetModel.SPHERE, -0.1, (-Math.PI - 1.1) + Math.PI * 2.0);
+ assertFalse(box.isWithin(gp));
// Latitude zone rectangle
box = GeoBBoxFactory.makeGeoBBox(PlanetModel.SPHERE, 0.0, -Math.PI * 0.25, -Math.PI, Math.PI);
@@ -90,8 +93,8 @@ public class GeoBBoxTest {
assertFalse(box.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -Math.PI + 1.1);
assertTrue(box.isWithin(gp));
- //bad lon: gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -Math.PI - 1.1);
- //assertTrue(box.isWithin(gp));
+ gp = new GeoPoint(PlanetModel.SPHERE, -0.1, (-Math.PI - 1.1) + Math.PI * 2.0);
+ assertTrue(box.isWithin(gp));
// World
box = GeoBBoxFactory.makeGeoBBox(PlanetModel.SPHERE, Math.PI * 0.5, -Math.PI * 0.5, -Math.PI, Math.PI);
@@ -103,8 +106,8 @@ public class GeoBBoxTest {
assertTrue(box.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -Math.PI + 1.1);
assertTrue(box.isWithin(gp));
- //bad lat: gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -Math.PI - 1.1);
- //assertTrue(box.isWithin(gp));
+ gp = new GeoPoint(PlanetModel.SPHERE, -0.1, (-Math.PI - 1.1) + Math.PI * 2.0);
+ assertTrue(box.isWithin(gp));
}
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircleTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircleTest.java
index 1f890e5dfa7..013cf6ebaba 100755
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircleTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoCircleTest.java
@@ -32,17 +32,17 @@ public class GeoCircleTest {
GeoPoint gp;
c = new GeoCircle(PlanetModel.SPHERE, 0.0, -0.5, 0.1);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, 0.0);
- assertEquals(Double.MAX_VALUE, c.computeArcDistance(gp), 0.0);
- assertEquals(Double.MAX_VALUE, c.computeLinearDistance(gp), 0.0);
- assertEquals(Double.MAX_VALUE, c.computeNormalDistance(gp), 0.0);
+ assertEquals(Double.MAX_VALUE, c.computeDistance(DistanceStyle.ARC,gp), 0.0);
+ assertEquals(Double.MAX_VALUE, c.computeDistance(DistanceStyle.NORMAL,gp), 0.0);
+ assertEquals(Double.MAX_VALUE, c.computeDistance(DistanceStyle.NORMAL,gp), 0.0);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, -0.5);
- assertEquals(0.0, c.computeArcDistance(gp), 0.000001);
- assertEquals(0.0, c.computeLinearDistance(gp), 0.000001);
- assertEquals(0.0, c.computeNormalDistance(gp), 0.000001);
+ assertEquals(0.0, c.computeDistance(DistanceStyle.ARC,gp), 0.000001);
+ assertEquals(0.0, c.computeDistance(DistanceStyle.NORMAL,gp), 0.000001);
+ assertEquals(0.0, c.computeDistance(DistanceStyle.NORMAL,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, 0.05, -0.5);
- assertEquals(0.05, c.computeArcDistance(gp), 0.000001);
- assertEquals(0.049995, c.computeLinearDistance(gp), 0.000001);
- assertEquals(0.049979, c.computeNormalDistance(gp), 0.000001);
+ assertEquals(0.05, c.computeDistance(DistanceStyle.ARC,gp), 0.000001);
+ assertEquals(0.049995, c.computeDistance(DistanceStyle.LINEAR,gp), 0.000001);
+ assertEquals(0.049979, c.computeDistance(DistanceStyle.NORMAL,gp), 0.000001);
}
@Test
@@ -75,6 +75,9 @@ public class GeoCircleTest {
c = new GeoCircle(PlanetModel.SPHERE, 0.0, -0.5, 0.1);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, 0.0);
assertFalse(c.isWithin(gp));
+ assertEquals(0.4,c.computeOutsideDistance(DistanceStyle.ARC,gp),1e-12);
+ assertEquals(0.12,c.computeOutsideDistance(DistanceStyle.NORMAL,gp),0.01);
+ assertEquals(0.4,c.computeOutsideDistance(DistanceStyle.LINEAR,gp),0.01);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, -0.5);
assertTrue(c.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, -0.55);
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygonTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygonTest.java
index e682ab8b121..45c44fd9972 100755
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygonTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoConvexPolygonTest.java
@@ -46,9 +46,12 @@ public class GeoConvexPolygonTest {
assertTrue(c.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, 0.05, -0.5);
assertTrue(c.isWithin(gp));
- // Sample some nearby points outside
+ // Sample some nearby points outside, and compute distance-to-shape for them as well
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, -0.65);
assertFalse(c.isWithin(gp));
+ assertEquals(0.05,c.computeOutsideDistance(DistanceStyle.ARC,gp),1e-12);
+ assertEquals(0.05,c.computeOutsideDistance(DistanceStyle.NORMAL,gp),1e-3);
+ assertEquals(0.05,c.computeOutsideDistance(DistanceStyle.LINEAR,gp),1e-3);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, -0.35);
assertFalse(c.isWithin(gp));
gp = new GeoPoint(PlanetModel.SPHERE, -0.15, -0.5);
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoPathTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoPathTest.java
index 998cbe8ccb0..18593669f33 100755
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoPathTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/spatial4j/geo3d/GeoPathTest.java
@@ -37,17 +37,17 @@ public class GeoPathTest {
p.addPoint(0.0, 0.2);
p.done();
gp = new GeoPoint(PlanetModel.SPHERE, Math.PI * 0.5, 0.15);
- assertEquals(Double.MAX_VALUE, p.computeArcDistance(gp), 0.0);
+ assertEquals(Double.MAX_VALUE, p.computeDistance(DistanceStyle.ARC,gp), 0.0);
gp = new GeoPoint(PlanetModel.SPHERE, 0.05, 0.15);
- assertEquals(0.15 + 0.05, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.15 + 0.05, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, 0.12);
- assertEquals(0.12 + 0.0, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.12 + 0.0, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, -0.15, 0.05);
- assertEquals(Double.MAX_VALUE, p.computeArcDistance(gp), 0.000001);
+ assertEquals(Double.MAX_VALUE, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, 0.25);
- assertEquals(0.20 + 0.05, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.20 + 0.05, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, -0.05);
- assertEquals(0.0 + 0.05, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.0 + 0.05, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
// Compute path distances now
p = new GeoPath(PlanetModel.SPHERE, 0.1);
@@ -56,9 +56,9 @@ public class GeoPathTest {
p.addPoint(0.0, 0.2);
p.done();
gp = new GeoPoint(PlanetModel.SPHERE, 0.05, 0.15);
- assertEquals(0.15 + 0.05, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.15 + 0.05, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, 0.12);
- assertEquals(0.12, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.12, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
// Now try a vertical path, and make sure distances are as expected
p = new GeoPath(PlanetModel.SPHERE, 0.1);
@@ -66,13 +66,13 @@ public class GeoPathTest {
p.addPoint(Math.PI * 0.25, -0.5);
p.done();
gp = new GeoPoint(PlanetModel.SPHERE, 0.0, 0.0);
- assertEquals(Double.MAX_VALUE, p.computeArcDistance(gp), 0.0);
+ assertEquals(Double.MAX_VALUE, p.computeDistance(DistanceStyle.ARC,gp), 0.0);
gp = new GeoPoint(PlanetModel.SPHERE, -0.1, -1.0);
- assertEquals(Double.MAX_VALUE, p.computeArcDistance(gp), 0.0);
+ assertEquals(Double.MAX_VALUE, p.computeDistance(DistanceStyle.ARC,gp), 0.0);
gp = new GeoPoint(PlanetModel.SPHERE, Math.PI * 0.25 + 0.05, -0.5);
- assertEquals(Math.PI * 0.5 + 0.05, p.computeArcDistance(gp), 0.000001);
+ assertEquals(Math.PI * 0.5 + 0.05, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
gp = new GeoPoint(PlanetModel.SPHERE, -Math.PI * 0.25 - 0.05, -0.5);
- assertEquals(0.0 + 0.05, p.computeArcDistance(gp), 0.000001);
+ assertEquals(0.0 + 0.05, p.computeDistance(DistanceStyle.ARC,gp), 0.000001);
}
@Test