From 12b0a64be8407a118d820904d6eaf1eefe8ba489 Mon Sep 17 00:00:00 2001 From: Martin Davis Date: Sat, 8 Aug 2020 15:14:15 -0700 Subject: [PATCH] Improve performance of PreparedPolygon.contains(Points) Signed-off-by: Martin Davis --- .../prep/AbstractPreparedPolygonContains.java | 52 +++++-- .../geom/prep/PreparedPolygonPredicate.java | 42 ++++-- .../prep/PreparedPolygonCoversPerfTest.java | 132 ++++++++++++++++++ 3 files changed, 199 insertions(+), 27 deletions(-) create mode 100644 modules/core/src/test/java/test/jts/perf/geom/prep/PreparedPolygonCoversPerfTest.java diff --git a/modules/core/src/main/java/org/locationtech/jts/geom/prep/AbstractPreparedPolygonContains.java b/modules/core/src/main/java/org/locationtech/jts/geom/prep/AbstractPreparedPolygonContains.java index 06a9e2c717..2e623cb019 100644 --- a/modules/core/src/main/java/org/locationtech/jts/geom/prep/AbstractPreparedPolygonContains.java +++ b/modules/core/src/main/java/org/locationtech/jts/geom/prep/AbstractPreparedPolygonContains.java @@ -76,6 +76,10 @@ public AbstractPreparedPolygonContains(PreparedPolygon prepPoly) */ protected boolean eval(Geometry geom) { + if (geom.getDimension() == 0) { + return evalPoints(geom); + } + /** * Do point-in-poly tests first, since they are cheaper and may result * in a quick negative result. @@ -85,20 +89,6 @@ protected boolean eval(Geometry geom) boolean isAllInTargetArea = isAllTestComponentsInTarget(geom); if (! isAllInTargetArea) return false; - /** - * If the test geometry consists of only Points, - * then it is now sufficient to test if any of those - * points lie in the interior of the target geometry. - * If so, the test is contained. - * If not, all points are on the boundary of the area, - * which implies not contained. - */ - if (requireSomePointInInterior - && geom.getDimension() == 0) { - boolean isAnyInTargetInterior = isAnyTestComponentInTargetInterior(geom); - return isAnyInTargetInterior; - } - /** * Check if there is any intersection between the line segments * in target and test. @@ -164,7 +154,39 @@ protected boolean eval(Geometry geom) return true; } - private boolean isProperIntersectionImpliesNotContainedSituation(Geometry testGeom) + /** + * Evaluation optimized for Point geometries. + * This provides about a 2x performance increase, and less memory usage. + * + * @param geom a Point or MultiPoint geometry + * @return the value of the predicate being evaluated + */ + private boolean evalPoints(Geometry geom) { + /** + * Do point-in-poly tests first, since they are cheaper and may result + * in a quick negative result. + * + * If a point of any test components does not lie in target, result is false + */ + boolean isAllInTargetArea = isAllTestPointsInTarget(geom); + if (! isAllInTargetArea) return false; + + /** + * If the test geometry consists of only Points, + * then it is now sufficient to test if any of those + * points lie in the interior of the target geometry. + * If so, the test is contained. + * If not, all points are on the boundary of the area, + * which implies not contained. + */ + if (requireSomePointInInterior) { + boolean isAnyInTargetInterior = isAnyTestPointInTargetInterior(geom); + return isAnyInTargetInterior; + } + return true; + } + + private boolean isProperIntersectionImpliesNotContainedSituation(Geometry testGeom) { /** * If the test geometry is polygonal we have the A/A situation. diff --git a/modules/core/src/main/java/org/locationtech/jts/geom/prep/PreparedPolygonPredicate.java b/modules/core/src/main/java/org/locationtech/jts/geom/prep/PreparedPolygonPredicate.java index 86f7769d66..8045601b36 100644 --- a/modules/core/src/main/java/org/locationtech/jts/geom/prep/PreparedPolygonPredicate.java +++ b/modules/core/src/main/java/org/locationtech/jts/geom/prep/PreparedPolygonPredicate.java @@ -20,6 +20,7 @@ import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Location; +import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.util.ComponentCoordinateExtracter; @@ -64,7 +65,7 @@ protected boolean isAllTestComponentsInTarget(Geometry testGeom) } return true; } - + /** * Tests whether all components of the test Geometry * are contained in the interior of the target geometry. @@ -84,7 +85,7 @@ protected boolean isAllTestComponentsInTargetInterior(Geometry testGeom) } return true; } - + /** * Tests whether any component of the test Geometry intersects * the area of the target geometry. @@ -104,28 +105,45 @@ protected boolean isAnyTestComponentInTarget(Geometry testGeom) } return false; } - + /** - * Tests whether any component of the test Geometry intersects + * Tests whether all points of the test Pointal geometry + * are contained in the target geometry. + * + * @param geom a Pointal geometry to test + * @return true if all points of the argument are contained in the target geometry + */ + protected boolean isAllTestPointsInTarget(Geometry testGeom) + { + for (int i = 0; i < testGeom.getNumGeometries(); i++) { + Point pt = (Point) testGeom.getGeometryN(i); + Coordinate p = pt.getCoordinate(); + int loc = targetPointLocator.locate(p); + if (loc == Location.EXTERIOR) + return false; + } + return true; + } + + /** + * Tests whether any point of the test Geometry intersects * the interior of the target geometry. - * Handles test geometries with both linear and point components. * * @param geom a geometry to test - * @return true if any component of the argument intersects the prepared area geometry interior + * @return true if any point of the argument intersects the prepared area geometry interior */ - protected boolean isAnyTestComponentInTargetInterior(Geometry testGeom) + protected boolean isAnyTestPointInTargetInterior(Geometry testGeom) { - List coords = ComponentCoordinateExtracter.getCoordinates(testGeom); - for (Iterator i = coords.iterator(); i.hasNext(); ) { - Coordinate p = (Coordinate) i.next(); + for (int i = 0; i < testGeom.getNumGeometries(); i++) { + Point pt = (Point) testGeom.getGeometryN(i); + Coordinate p = pt.getCoordinate(); int loc = targetPointLocator.locate(p); if (loc == Location.INTERIOR) return true; } - return false; + return false; } - /** * Tests whether any component of the target geometry * intersects the test geometry (which must be an areal geometry) diff --git a/modules/core/src/test/java/test/jts/perf/geom/prep/PreparedPolygonCoversPerfTest.java b/modules/core/src/test/java/test/jts/perf/geom/prep/PreparedPolygonCoversPerfTest.java new file mode 100644 index 0000000000..9e4bf41f8d --- /dev/null +++ b/modules/core/src/test/java/test/jts/perf/geom/prep/PreparedPolygonCoversPerfTest.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2016 Vivid Solutions. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * + * http://www.eclipse.org/org/documents/edl-v10.php. + */ +package test.jts.perf.geom.prep; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.locationtech.jts.algorithm.locate.IndexedPointInAreaLocator; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.GeometryFactory; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.Point; +import org.locationtech.jts.geom.PrecisionModel; +import org.locationtech.jts.geom.prep.PreparedGeometry; +import org.locationtech.jts.geom.prep.PreparedGeometryFactory; +import org.locationtech.jts.geom.util.SineStarFactory; +import org.locationtech.jts.io.WKTReader; +import org.locationtech.jts.io.WKTWriter; +import org.locationtech.jts.util.GeometricShapeFactory; +import org.locationtech.jts.util.Stopwatch; + +import test.jts.perf.PerformanceTestCase; +import test.jts.perf.PerformanceTestRunner; + + +public class PreparedPolygonCoversPerfTest extends PerformanceTestCase +{ + static final int NUM_ITER = 10_000; + + static final int NUM_PTS = 2000; + + static PrecisionModel pm = new PrecisionModel(); + static GeometryFactory fact = new GeometryFactory(pm, 0); + static WKTReader wktRdr = new WKTReader(fact); + static WKTWriter wktWriter = new WKTWriter(); + + Stopwatch sw = new Stopwatch(); + + public static void main(String[] args) { + PerformanceTestRunner.run(PreparedPolygonCoversPerfTest.class); + } + + boolean testFailed = false; + + private PreparedGeometry prepGeom; + + private List testPoints; + + private Geometry sinePoly; + + public PreparedPolygonCoversPerfTest(String name) { + super(name); + setRunSize(new int[] { 1000}); + setRunIterations(1); + } + + public void startRun(int nPts) + { + System.out.println("Running with size " + nPts); + System.out.println("Iterations per run = " + NUM_ITER); + +// Geometry poly = createCircle(new Coordinate(0, 0), 100, nPts); + sinePoly = createSineStar(new Coordinate(0, 0), 100, nPts); +// System.out.println(poly); +// Geometry target = sinePoly.getBoundary(); + prepGeom = (new PreparedGeometryFactory()).create(sinePoly); + + testPoints = createPoints(sinePoly.getEnvelopeInternal(), NUM_PTS); + } + + Geometry createSineStar(Coordinate origin, double size, int nPts) { + SineStarFactory gsf = new SineStarFactory(); + gsf.setCentre(origin); + gsf.setSize(size); + gsf.setNumPoints(nPts); + gsf.setArmLengthRatio(0.1); + gsf.setNumArms(50); + Geometry poly = gsf.createSineStar(); + return poly; + } + + List createPoints(Envelope env, int nPts) + { + int nCells = (int) Math.sqrt(nPts); + + List geoms = new ArrayList(); + double width = env.getWidth(); + double xInc = width / nCells; + double yInc = width / nCells; + for (int i = 0; i < nCells; i++) { + for (int j = 0; j < nCells; j++) { + Coordinate base = new Coordinate( + env.getMinX() + i * xInc, + env.getMinY() + j * yInc); + Point pt = fact.createPoint(base); + geoms.add(pt); + } + } + return geoms; + } + + public void runPreparedPolygon() { + for (int i = 0; i < NUM_ITER; i++) { + prepGeom = (new PreparedGeometryFactory()).create(sinePoly); + for (Point pt : testPoints) { + prepGeom.covers(pt); + //prepGeom.contains(pt); + } + } + } + public void runIndexPointInAreaLocator() { + for (int i = 0; i < NUM_ITER; i++) { + IndexedPointInAreaLocator ipa = new IndexedPointInAreaLocator(sinePoly); + for (Point pt : testPoints) { + ipa.locate(pt.getCoordinate()); + } + } + } + +}