Apache Solr open-source search software
LUCENE-7222: Improve Polygon.contains()
Robert Muira60265ef5a3b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex decb5db6101..717d9585d24 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -61,7 +61,7 @@ Optimizationsmultiple polygons and holes, with memory usage independent ofpolygon complexity. (Karl Wright, Mike McCandless, Robert Muir)-* LUCENE-7159: Speed up LatLonPoint polygon performance for complex+* LUCENE-7159, LUCENE-7222: Speed up LatLonPoint polygon performance for complexpolygons. (Robert Muir)* LUCENE-7211: Reduce memory & GC for spatial RPT Intersects when the number ofdiff --git a/lucene/core/src/java/org/apache/lucene/geo/Polygon.java b/lucene/core/src/java/org/apache/lucene/geo/Polygon.javaindex 145b939e4e2..75788dd2543 100644--- a/lucene/core/src/java/org/apache/lucene/geo/Polygon.java+++ b/lucene/core/src/java/org/apache/lucene/geo/Polygon.java@@ -113,32 +113,51 @@ public final class Polygon {this.maxLon = maxLon;}- /** Returns true if the point is contained within this polygon */+ /**+ * Returns true if the point is contained within this polygon.+ * <p>+ * See <a href="https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html">+ * https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html</a> for more information.+ */+ // ported to java from https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html+ // original code under the BSD license (https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html#License%20to%20Use)+ //+ // Copyright (c) 1970-2003, Wm. Randolph Franklin+ //+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated+ // documentation files (the "Software"), to deal in the Software without restriction, including without limitation+ // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and+ // to permit persons to whom the Software is furnished to do so, subject to the following conditions:+ //+ // 1. Redistributions of source code must retain the above copyright+ // notice, this list of conditions and the following disclaimers.+ // 2. Redistributions in binary form must reproduce the above copyright+ // notice in the documentation and/or other materials provided with+ // the distribution.+ // 3. The name of W. Randolph Franklin may not be used to endorse or+ // promote products derived from this Software without specific+ // prior written permission.+ //+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED+ // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL+ // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF+ // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS+ // IN THE SOFTWARE.public boolean contains(double latitude, double longitude) {// check bounding boxif (latitude < minLat || latitude > maxLat || longitude < minLon || longitude > maxLon) {return false;}- /*- * simple even-odd point in polygon computation- * 1. Determine if point is contained in the longitudinal range- * 2. Determine whether point crosses the edge by computing the latitudinal delta- * between the end-point of a parallel vector (originating at the point) and the- * y-component of the edge sink- *- * NOTE: Requires polygon point (x,y) order either clockwise or counter-clockwise- */+boolean inPoly = false;- /*- * Note: This is using a euclidean coordinate system which could result in- * upwards of 110KM error at the equator.- * TODO convert coordinates to cylindrical projection (e.g. mercator)- */+ boolean previous = polyLats[0] > latitude;for (int i = 1; i < polyLats.length; i++) {- if (polyLons[i] <= longitude && polyLons[i-1] >= longitude || polyLons[i-1] <= longitude && polyLons[i] >= longitude) {- if (polyLats[i] + (longitude - polyLons[i]) / (polyLons[i-1] - polyLons[i]) * (polyLats[i-1] - polyLats[i]) <= latitude) {+ boolean current = polyLats[i] > latitude;+ if (current != previous) {+ if (longitude < (polyLons[i-1] - polyLons[i]) * (latitude - polyLats[i]) / (polyLats[i-1] - polyLats[i]) + polyLons[i]) {inPoly = !inPoly;}+ previous = current;}}if (inPoly) {diff --git a/lucene/core/src/test/org/apache/lucene/geo/TestPolygon.java b/lucene/core/src/test/org/apache/lucene/geo/TestPolygon.javaindex d2c1d67484e..86545ed6548 100644--- a/lucene/core/src/test/org/apache/lucene/geo/TestPolygon.java+++ b/lucene/core/src/test/org/apache/lucene/geo/TestPolygon.java@@ -245,4 +245,123 @@ public class TestPolygon extends LuceneTestCase {}}}++ /** Tests edge case behavior with respect to insideness */+ public void testEdgeInsideness() {+ Polygon poly = new Polygon(new double[] { -2, -2, 2, 2, -2 }, new double[] { -2, 2, 2, -2, -2 });+ assertTrue(poly.contains(-2, -2)); // bottom left corner: true+ assertFalse(poly.contains(-2, 2)); // bottom right corner: false+ assertFalse(poly.contains(2, -2)); // top left corner: false+ assertFalse(poly.contains(2, 2)); // top right corner: false+ assertTrue(poly.contains(-2, -1)); // bottom side: true+ assertTrue(poly.contains(-2, 0)); // bottom side: true+ assertTrue(poly.contains(-2, 1)); // bottom side: true+ assertFalse(poly.contains(2, -1)); // top side: false+ assertFalse(poly.contains(2, 0)); // top side: false+ assertFalse(poly.contains(2, 1)); // top side: false+ assertFalse(poly.contains(-1, 2)); // right side: false+ assertFalse(poly.contains(0, 2)); // right side: false+ assertFalse(poly.contains(1, 2)); // right side: false+ assertTrue(poly.contains(-1, -2)); // left side: true+ assertTrue(poly.contains(0, -2)); // left side: true+ assertTrue(poly.contains(1, -2)); // left side: true+ }++ /** Tests that our impl supports multiple components and holes (not currently used) */+ public void testMultiPolygonContains() {+ // this is the equivalent of the following: we don't recommend anyone do this (e.g. relation logic will not work)+ // but lets not lose the property that it works.+ ///+ // Polygon hole = new Polygon(new double[] { -10, -10, 10, 10, -10 }, new double[] { -10, 10, 10, -10, -10 });+ // Polygon outer = new Polygon(new double[] { -50, -50, 50, 50, -50 }, new double[] { -50, 50, 50, -50, -50 }, hole);+ // Polygon island = new Polygon(new double[] { -5, -5, 5, 5, -5 }, new double[] { -5, 5, 5, -5, -5 } );+ // Polygon polygons[] = new Polygon[] { outer, island };++ Polygon polygon = new Polygon(new double[] { 0, -50, -50, 50, 50, -50, 0, -5, -5, 5, 5, -5, 0, -10, -10, 10, 10, -10, 0 },+ new double[] { 0, -50, 50, 50, -50, -50, 0, -5, 5, 5, -5, -5, 0, -10, 10, 10, -10, -10, 0 });++ assertTrue(polygon.contains(-2, 2)); // on the island+ assertFalse(polygon.contains(-6, 6)); // in the hole+ assertTrue(polygon.contains(-25, 25)); // on the mainland+ assertFalse(polygon.contains(-51, 51)); // in the ocean+ }++ /** Tests current impl against original algorithm */+ public void testContainsAgainstOriginal() {+ for (int i = 0; i < 1000; i++) {+ Polygon polygon = nextPolygon();+ // currently we don't generate these, but this test does not want holes.+ while (polygon.getHoles().length > 0) {+ polygon = nextPolygon();+ }++ double polyLats[] = polygon.getPolyLats();+ double polyLons[] = polygon.getPolyLons();++ // random lat/lons in bounding box+ for (int j = 0; j < 1000; j++) {+ double latitude = nextLatitudeAround(polygon.minLat, polygon.maxLat);+ double longitude = nextLongitudeAround(polygon.minLon, polygon.maxLon);+ // bounding box check required due to rounding errors (we don't solve that problem)+ if (latitude >= polygon.minLat && latitude <= polygon.maxLat && longitude >= polygon.minLon && longitude <= polygon.maxLon) {+ boolean expected = containsOriginal(polyLats, polyLons, latitude, longitude);+ assertEquals(expected, polygon.contains(latitude, longitude));+ }+ }++ // lat lons targeted near vertices+ for (int vertex = 0; vertex < polyLats.length; vertex++) {+ for (int j = 0; j < 100; j++) {+ double latitude = GeoTestUtil.nextLatitudeNear(polyLats[vertex]);+ double longitude = GeoTestUtil.nextLongitudeNear(polyLons[vertex]);+ // bounding box check required due to rounding errors (we don't solve that problem)+ if (latitude >= polygon.minLat && latitude <= polygon.maxLat && longitude >= polygon.minLon && longitude <= polygon.maxLon) {+ boolean expected = containsOriginal(polyLats, polyLons, latitude, longitude);+ assertEquals(expected, polygon.contains(latitude, longitude));+ }+ }+ }+ }+ }++ // direct port of PNPOLY C code (https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html)+ // this allows us to improve the code yet still ensure we have its properties+ // it is under the BSD license (https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html#License%20to%20Use)+ //+ // Copyright (c) 1970-2003, Wm. Randolph Franklin+ //+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated+ // documentation files (the "Software"), to deal in the Software without restriction, including without limitation+ // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and+ // to permit persons to whom the Software is furnished to do so, subject to the following conditions:+ //+ // 1. Redistributions of source code must retain the above copyright+ // notice, this list of conditions and the following disclaimers.+ // 2. Redistributions in binary form must reproduce the above copyright+ // notice in the documentation and/or other materials provided with+ // the distribution.+ // 3. The name of W. Randolph Franklin may not be used to endorse or+ // promote products derived from this Software without specific+ // prior written permission.+ //+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED+ // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL+ // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF+ // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS+ // IN THE SOFTWARE.+ private static boolean containsOriginal(double polyLats[], double polyLons[], double latitude, double longitude) {+ boolean c = false;+ int i, j;+ int nvert = polyLats.length;+ double verty[] = polyLats;+ double vertx[] = polyLons;+ double testy = latitude;+ double testx = longitude;+ for (i = 0, j = nvert-1; i < nvert; j = i++) {+ if ( ((verty[i]>testy) != (verty[j]>testy)) &&+ (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )+ c = !c;+ }+ return c;+ }}