Apache Lucene open-source search software
LUCENE-8245: Use strict bounds checking for edge planes when assessing envelope crossings. It's the only way to insure we don't overdetect or underdetect such intersections.
Karl Wright832e89748ea9
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.javaindex 487a7716398..c4e2c93e3a4 100644--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java@@ -971,12 +971,14 @@ class GeoComplexPolygon extends GeoBasePolygon {*/private int countCrossings(final Edge edge,final Plane envelopePlane, final Membership envelopeBound) {- final GeoPoint[] intersections = edge.plane.findIntersections(planetModel, envelopePlane, edge.startPlane, edge.endPlane, envelopeBound);+ final GeoPoint[] intersections = edge.plane.findIntersections(planetModel, envelopePlane, envelopeBound);int crossings = 0;if (intersections != null) {for (final GeoPoint intersection : intersections) {- // It's unique, so assess it- crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;+ if (edge.startPlane.strictlyWithin(intersection) && edge.endPlane.strictlyWithin(intersection)) {+ // It's unique, so assess it+ crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;+ }}}return crossings;@@ -1062,12 +1064,14 @@ class GeoComplexPolygon extends GeoBasePolygon {*/private int countCrossings(final Edge edge,final Plane envelopePlane, final Membership envelopeBound1, final Membership envelopeBound2) {- final GeoPoint[] intersections = edge.plane.findIntersections(planetModel, envelopePlane, edge.startPlane, edge.endPlane, envelopeBound1, envelopeBound2);+ final GeoPoint[] intersections = edge.plane.findIntersections(planetModel, envelopePlane, envelopeBound1, envelopeBound2);int crossings = 0;if (intersections != null) {for (final GeoPoint intersection : intersections) {- // It's unique, so assess it- crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;+ if (edge.startPlane.strictlyWithin(intersection) && edge.endPlane.strictlyWithin(intersection)) {+ // It's unique, so assess it+ crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;+ }}}return crossings;@@ -1381,35 +1385,39 @@ class GeoComplexPolygon extends GeoBasePolygon {private int countCrossings(final Edge edge,final Plane travelEnvelopePlane, final Membership travelEnvelopeBound1, final Membership travelEnvelopeBound2,final Plane testPointEnvelopePlane, final Membership testPointEnvelopeBound1, final Membership testPointEnvelopeBound2) {- final GeoPoint[] travelIntersections = edge.plane.findIntersections(planetModel, travelEnvelopePlane, edge.startPlane, edge.endPlane, travelEnvelopeBound1, travelEnvelopeBound2);- final GeoPoint[] testPointIntersections = edge.plane.findIntersections(planetModel, testPointEnvelopePlane, edge.startPlane, edge.endPlane, testPointEnvelopeBound1, testPointEnvelopeBound2);+ final GeoPoint[] travelIntersections = edge.plane.findIntersections(planetModel, travelEnvelopePlane, travelEnvelopeBound1, travelEnvelopeBound2);+ final GeoPoint[] testPointIntersections = edge.plane.findIntersections(planetModel, testPointEnvelopePlane, testPointEnvelopeBound1, testPointEnvelopeBound2);int crossings = 0;if (travelIntersections != null) {for (final GeoPoint intersection : travelIntersections) {- // Make sure it's not a dup- boolean notDup = true;- if (testPointIntersections != null) {- for (final GeoPoint otherIntersection : testPointIntersections) {- if (intersection.isNumericallyIdentical(otherIntersection)) {- //System.out.println(" Points "+intersection+" and "+otherIntersection+" are duplicates");- notDup = false;- break;+ if (edge.startPlane.strictlyWithin(intersection) && edge.endPlane.strictlyWithin(intersection)) {+ // Make sure it's not a dup+ boolean notDup = true;+ if (testPointIntersections != null) {+ for (final GeoPoint otherIntersection : testPointIntersections) {+ if (edge.startPlane.strictlyWithin(otherIntersection) && edge.endPlane.strictlyWithin(otherIntersection) && intersection.isNumericallyIdentical(otherIntersection)) {+ //System.out.println(" Points "+intersection+" and "+otherIntersection+" are duplicates");+ notDup = false;+ break;+ }}}+ if (!notDup) {+ continue;+ }+ // It's unique, so assess it+ //System.out.println(" Assessing travel envelope intersection point "+intersection+"...");+ crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;}- if (!notDup) {- continue;- }- // It's unique, so assess it- //System.out.println(" Assessing travel envelope intersection point "+intersection+"...");- crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;}}if (testPointIntersections != null) {for (final GeoPoint intersection : testPointIntersections) {- // It's unique, so assess it- //System.out.println(" Assessing testpoint envelope intersection point "+intersection+"...");- crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;+ if (edge.startPlane.strictlyWithin(intersection) && edge.endPlane.strictlyWithin(intersection)) {+ // It's unique, so assess it+ //System.out.println(" Assessing testpoint envelope intersection point "+intersection+"...");+ crossings += edgeCrossesEnvelope(edge.plane, intersection)?1:0;+ }}}return crossings;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 66e93764dff..238933c3a84 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@@ -234,6 +234,18 @@ public class SidedPlane extends Plane implements Membership {return sigNum == this.sigNum;}+ public boolean strictlyWithin(final Vector v) {+ double evalResult = evaluate(v.x, v.y, v.z);+ double sigNum = Math.signum(evalResult);+ return sigNum == 0.0 || sigNum == this.sigNum;+ }++ public boolean strictlyWithin(double x, double y, double z) {+ double evalResult = evaluate(x, y, z);+ double sigNum = Math.signum(evalResult);+ return sigNum == 0.0 || sigNum == this.sigNum;+ }+@Overridepublic boolean equals(Object o) {if (this == o) return true;diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPolygonTest.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPolygonTest.javaindex cd650181fb7..adff16c140c 100755--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPolygonTest.java+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPolygonTest.java@@ -1428,7 +1428,6 @@ shape:}@Test- //@AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8245")public void testComplexPolygonPlaneOutsideWorld() {List<GeoPoint> points = new ArrayList<>();points.add(new GeoPoint(PlanetModel.SPHERE, -0.5, -0.5));@@ -1503,7 +1502,6 @@ shape:}@Test- @AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8245")public void testLUCENE8245() {//POLYGON((-70.19447784626787 -83.117346007187,0.0 2.8E-322,-139.99870438810106 7.994601469571884,-143.14292702670522 -18.500141088122664,-158.7373186858464 -35.42942085357812,-70.19447784626787 -83.117346007187))final List<GeoPoint> points = new ArrayList<>();diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeoPolygonTest.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeoPolygonTest.javaindex 93d675ae041..44d682ccc86 100644--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeoPolygonTest.java+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeoPolygonTest.java@@ -24,6 +24,8 @@ import com.carrotsearch.randomizedtesting.annotations.Repeat;import com.carrotsearch.randomizedtesting.generators.BiasedNumbers;import org.junit.Test;+import static com.carrotsearch.randomizedtesting.RandomizedTest.randomDouble;+/*** Random test for polygons.*/@@ -92,7 +94,7 @@ public class RandomGeoPolygonTest extends RandomGeo3dShapeGenerator {* biased doubles.*/@Test- @AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8245")+ //@AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/LUCENE-8245")@Repeat(iterations = 10)public void testComparePolygons() {final PlanetModel planetModel = randomPlanetModel();