From 1cc3c1a3547dbb1ea6b1cb19c23e12a94b823d5f Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Mon, 15 Jun 2020 09:36:44 +0200 Subject: [PATCH] GeometryTestUtils should always generate valid polygons (#58034) (#58091) Make sure we always generate legal polygons, e.g they have area --- .../org/elasticsearch/geo/GeometryTestUtils.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java b/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java index 33983e8e390..3b4ad5eca73 100644 --- a/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java @@ -40,6 +40,8 @@ import java.util.Collections; import java.util.List; import java.util.function.Function; +import static org.elasticsearch.test.ESTestCase.randomValueOtherThanMany; + public class GeometryTestUtils { public static double randomLat() { @@ -96,7 +98,7 @@ public class GeometryTestUtils { } public static Polygon randomPolygon(boolean hasAlt) { - org.apache.lucene.geo.Polygon lucenePolygon = GeoTestUtil.nextPolygon(); + org.apache.lucene.geo.Polygon lucenePolygon = randomValueOtherThanMany(p -> area(p) == 0, GeoTestUtil::nextPolygon); if (lucenePolygon.numHoles() > 0) { org.apache.lucene.geo.Polygon[] luceneHoles = lucenePolygon.getHoles(); List holes = new ArrayList<>(); @@ -109,6 +111,17 @@ public class GeometryTestUtils { return new Polygon(linearRing(lucenePolygon.getPolyLons(), lucenePolygon.getPolyLats(), hasAlt)); } + private static double area(org.apache.lucene.geo.Polygon lucenePolygon) { + double windingSum = 0; + final int numPts = lucenePolygon.numPoints() - 1; + for (int i = 0; i < numPts; i++) { + // compute signed area + windingSum += lucenePolygon.getPolyLon(i) * lucenePolygon.getPolyLat(i + 1) - + lucenePolygon.getPolyLat(i) * lucenePolygon.getPolyLon(i + 1); + } + return Math.abs(windingSum / 2); + } + private static double[] randomAltRing(int size) { double[] alts = new double[size];