GeometryTestUtils should always generate valid polygons (#58034) (#58091)

Make sure we always generate legal polygons, e.g they have area
This commit is contained in:
Ignacio Vera 2020-06-15 09:36:44 +02:00 committed by GitHub
parent a7a1b78882
commit 1cc3c1a354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -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<LinearRing> 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];