Apache Lucene open-source search software
LUCENE-8457: Use backing planes for all potentially narrow GeoBBox shapes.
Karl Wright30f64d778ab2
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoLongitudeSlice.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoLongitudeSlice.javaindex 404bf8c5581..4a8a60474ab 100755--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoLongitudeSlice.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoLongitudeSlice.java@@ -36,6 +36,8 @@ class GeoLongitudeSlice extends GeoBaseBBox {protected final SidedPlane leftPlane;/** The right plane of the slice */protected final SidedPlane rightPlane;+ /** Backing plane (for narrow angles) */+ protected final SidedPlane backingPlane;/** The notable points for the slice (north and south poles) */protected final GeoPoint[] planePoints;/** The center point of the slice */@@ -76,10 +78,18 @@ class GeoLongitudeSlice extends GeoBaseBBox {rightLon += Math.PI * 2.0;}final double middleLon = (leftLon + rightLon) * 0.5;- this.centerPoint = new GeoPoint(planetModel, 0.0, middleLon);+ final double sinMiddleLon = Math.sin(middleLon);+ final double cosMiddleLon = Math.cos(middleLon);++ this.centerPoint = new GeoPoint(planetModel, 0.0, sinMiddleLon, 1.0, cosMiddleLon);this.leftPlane = new SidedPlane(centerPoint, cosLeftLon, sinLeftLon);this.rightPlane = new SidedPlane(centerPoint, cosRightLon, sinRightLon);++ // Compute the backing plane+ // The normal for this plane is a unit vector through the origin that goes through the middle lon. The plane's D is 0,+ // because it goes through the origin.+ this.backingPlane = new SidedPlane(this.centerPoint, cosMiddleLon, sinMiddleLon, 0.0, 0.0);this.planePoints = new GeoPoint[]{planetModel.NORTH_POLE, planetModel.SOUTH_POLE};this.edgePoints = new GeoPoint[]{planetModel.NORTH_POLE};@@ -117,7 +127,8 @@ class GeoLongitudeSlice extends GeoBaseBBox {@Overridepublic boolean isWithin(final double x, final double y, final double z) {- return leftPlane.isWithin(x, y, z) &&+ return backingPlane.isWithin(x, y, z) &&+ leftPlane.isWithin(x, y, z) &&rightPlane.isWithin(x, y, z);}@@ -158,7 +169,7 @@ class GeoLongitudeSlice extends GeoBaseBBox {bounds.addVerticalPlane(planetModel, leftLon, leftPlane, rightPlane).addVerticalPlane(planetModel, rightLon, rightPlane, leftPlane)- .addIntersection(planetModel, rightPlane, leftPlane)+ //.addIntersection(planetModel, rightPlane, leftPlane).addPoint(planetModel.NORTH_POLE).addPoint(planetModel.SOUTH_POLE);}diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoNorthRectangle.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoNorthRectangle.javaindex 1dac9293649..a73645527ed 100644--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoNorthRectangle.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoNorthRectangle.java@@ -47,6 +47,8 @@ class GeoNorthRectangle extends GeoBaseBBox {protected final SidedPlane leftPlane;/** Right-side plane */protected final SidedPlane rightPlane;+ /** Backing plane (for narrow angles) */+ protected final SidedPlane backingPlane;/** Bottom plane notable points */protected final GeoPoint[] bottomPlanePoints;/** Left plane notable points */@@ -113,11 +115,22 @@ class GeoNorthRectangle extends GeoBaseBBox {this.leftPlane = new SidedPlane(centerPoint, cosLeftLon, sinLeftLon);this.rightPlane = new SidedPlane(centerPoint, cosRightLon, sinRightLon);+ assert(bottomPlane.isWithin(centerPoint));+ assert(leftPlane.isWithin(centerPoint));+ assert(rightPlane.isWithin(centerPoint));++ // Compute the backing plane+ // The normal for this plane is a unit vector through the origin that goes through the middle lon. The plane's D is 0,+ // because it goes through the origin.+ this.backingPlane = new SidedPlane(this.centerPoint, cosMiddleLon, sinMiddleLon, 0.0, 0.0);+this.bottomPlanePoints = new GeoPoint[]{LLHC, LRHC};this.leftPlanePoints = new GeoPoint[]{planetModel.NORTH_POLE, LLHC};this.rightPlanePoints = new GeoPoint[]{planetModel.NORTH_POLE, LRHC};this.edgePoints = new GeoPoint[]{planetModel.NORTH_POLE};++ System.out.println("LLHC = "+LLHC+" LRHC = "+LRHC);}/**@@ -156,7 +169,8 @@ class GeoNorthRectangle extends GeoBaseBBox {@Overridepublic boolean isWithin(final double x, final double y, final double z) {return- bottomPlane.isWithin(x, y, z) &&+ backingPlane.isWithin(x, y, z) &&+ bottomPlane.isWithin(x, y, z) &&leftPlane.isWithin(x, y, z) &&rightPlane.isWithin(x, y, z);}@@ -209,7 +223,7 @@ class GeoNorthRectangle extends GeoBaseBBox {.addHorizontalPlane(planetModel, bottomLat, bottomPlane, leftPlane, rightPlane).addVerticalPlane(planetModel, leftLon, leftPlane, bottomPlane, rightPlane).addVerticalPlane(planetModel, rightLon, rightPlane, bottomPlane, leftPlane)- .addIntersection(planetModel, rightPlane, leftPlane, bottomPlane)+ //.addIntersection(planetModel, rightPlane, leftPlane, bottomPlane).addPoint(LLHC).addPoint(LRHC).addPoint(planetModel.NORTH_POLE);}diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoRectangle.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoRectangle.javaindex 7e982143876..16073cd03da 100755--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoRectangle.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoRectangle.java@@ -56,6 +56,8 @@ class GeoRectangle extends GeoBaseBBox {protected final SidedPlane leftPlane;/** The right plane */protected final SidedPlane rightPlane;+ /** Backing plane (for narrow angles) */+ protected final SidedPlane backingPlane;/** Notable points for the top plane */protected final GeoPoint[] topPlanePoints;@@ -138,6 +140,11 @@ class GeoRectangle extends GeoBaseBBox {this.leftPlane = new SidedPlane(centerPoint, cosLeftLon, sinLeftLon);this.rightPlane = new SidedPlane(centerPoint, cosRightLon, sinRightLon);+ // Compute the backing plane+ // The normal for this plane is a unit vector through the origin that goes through the middle lon. The plane's D is 0,+ // because it goes through the origin.+ this.backingPlane = new SidedPlane(this.centerPoint, cosMiddleLon, sinMiddleLon, 0.0, 0.0);+this.topPlanePoints = new GeoPoint[]{ULHC, URHC};this.bottomPlanePoints = new GeoPoint[]{LLHC, LRHC};this.leftPlanePoints = new GeoPoint[]{ULHC, LLHC};@@ -182,7 +189,8 @@ class GeoRectangle extends GeoBaseBBox {@Overridepublic boolean isWithin(final double x, final double y, final double z) {- return topPlane.isWithin(x, y, z) &&+ return backingPlane.isWithin(x,y,z) &&+ topPlane.isWithin(x, y, z) &&bottomPlane.isWithin(x, y, z) &&leftPlane.isWithin(x, y, z) &&rightPlane.isWithin(x, y, z);@@ -232,7 +240,7 @@ class GeoRectangle extends GeoBaseBBox {.addVerticalPlane(planetModel, rightLon, rightPlane, topPlane, bottomPlane, leftPlane).addHorizontalPlane(planetModel, bottomLat, bottomPlane, topPlane, leftPlane, rightPlane).addVerticalPlane(planetModel, leftLon, leftPlane, topPlane, bottomPlane, rightPlane)- .addIntersection(planetModel, leftPlane, rightPlane, topPlane, bottomPlane)+ //.addIntersection(planetModel, leftPlane, rightPlane, topPlane, bottomPlane).addPoint(ULHC).addPoint(URHC).addPoint(LLHC).addPoint(LRHC);}diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoSouthRectangle.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoSouthRectangle.javaindex a23b9277741..fc05fc4f8b9 100644--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoSouthRectangle.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoSouthRectangle.java@@ -48,6 +48,8 @@ class GeoSouthRectangle extends GeoBaseBBox {protected final SidedPlane leftPlane;/** The right plane */protected final SidedPlane rightPlane;+ /** Backing plane (for narrow angles) */+ protected final SidedPlane backingPlane;/** Notable points for the top plane */protected final GeoPoint[] topPlanePoints;@@ -117,6 +119,15 @@ class GeoSouthRectangle extends GeoBaseBBox {this.leftPlane = new SidedPlane(centerPoint, cosLeftLon, sinLeftLon);this.rightPlane = new SidedPlane(centerPoint, cosRightLon, sinRightLon);+ assert(topPlane.isWithin(centerPoint));+ assert(leftPlane.isWithin(centerPoint));+ assert(rightPlane.isWithin(centerPoint));++ // Compute the backing plane+ // The normal for this plane is a unit vector through the origin that goes through the middle lon. The plane's D is 0,+ // because it goes through the origin.+ this.backingPlane = new SidedPlane(this.centerPoint, cosMiddleLon, sinMiddleLon, 0.0, 0.0);+this.topPlanePoints = new GeoPoint[]{ULHC, URHC};this.leftPlanePoints = new GeoPoint[]{ULHC, planetModel.SOUTH_POLE};this.rightPlanePoints = new GeoPoint[]{URHC, planetModel.SOUTH_POLE};@@ -160,7 +171,8 @@ class GeoSouthRectangle extends GeoBaseBBox {@Overridepublic boolean isWithin(final double x, final double y, final double z) {- return topPlane.isWithin(x, y, z) &&+ return backingPlane.isWithin(x, y, z) &&+ topPlane.isWithin(x, y, z) &&leftPlane.isWithin(x, y, z) &&rightPlane.isWithin(x, y, z);}@@ -206,7 +218,7 @@ class GeoSouthRectangle extends GeoBaseBBox {.addHorizontalPlane(planetModel, topLat, topPlane, leftPlane, rightPlane).addVerticalPlane(planetModel, leftLon, leftPlane, topPlane, rightPlane).addVerticalPlane(planetModel, rightLon, rightPlane, topPlane, leftPlane)- .addIntersection(planetModel, rightPlane, leftPlane, topPlane)+ //.addIntersection(planetModel, rightPlane, leftPlane, topPlane).addPoint(URHC).addPoint(ULHC).addPoint(planetModel.SOUTH_POLE);}diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SidedPlane.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SidedPlane.javaindex 61d561d421b..5b22fc4dbdc 100755--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SidedPlane.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/SidedPlane.java@@ -141,6 +141,22 @@ public class SidedPlane extends Plane implements Membership {throw new IllegalArgumentException("Cannot determine sidedness because check point is on plane.");}+ /**+ * Construct a sided plane with a normal vector and offset.+ *+ * @param p point to evaluate.+ * @param vX is the normal vector X.+ * @param vY is the normal vector Y.+ * @param vZ is the normal vector Z.+ * @param D is the origin offset for the plan.+ */+ public SidedPlane(Vector p, double vX, double vY, double vZ, double D) {+ super(vX, vY, vZ, D);+ sigNum = Math.signum(evaluate(p));+ if (sigNum == 0.0)+ throw new IllegalArgumentException("Cannot determine sidedness because check point is on plane.");+ }+/*** Construct a sided plane with a normal vector and offset.*diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/XYZSolidTest.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/XYZSolidTest.javaindex 03255334e28..957e166a858 100644--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/XYZSolidTest.java+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/XYZSolidTest.java@@ -218,14 +218,17 @@ public class XYZSolidTest extends LuceneTestCase {}@Test- @AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8457")+ //@AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8457")public void testLUCENE8457() {GeoShape shape = GeoBBoxFactory.makeGeoBBox(PlanetModel.WGS84, Math.PI, 1.2487354264870392, 0.0, 3.5181789305199657E-12);+ System.out.println("shape = "+shape);XYZBounds bounds = new XYZBounds();shape.getBounds(bounds);XYZSolid solid = XYZSolidFactory.makeXYZSolid(PlanetModel.WGS84, bounds);+ System.out.println("solid = "+solid);GeoPoint point = new GeoPoint(PlanetModel.WGS84, 1.4812439919751819, -3.141592653589793);+ System.out.println("point="+point);//if the point is within the shape, it must be within the solidif (shape.isWithin(point)) {assertTrue(solid.isWithin(point));