Make sure we always generate legal polygons, e.g they have area
This commit is contained in:
parent
a7a1b78882
commit
1cc3c1a354
|
@ -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];
|
||||
|
|
Loading…
Reference in New Issue