Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-7970: Add test suite for GeoExactCircle. Committed on behalf of Ignacio Vera.

Karl Wrightcc16055efe69
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCircleFactory.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCircleFactory.java
index 7b56e587134..8feecced9c5 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCircleFactory.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoCircleFactory.java
@@ -44,8 +44,8 @@ public class GeoCircleFactory {
* @param planetModel is the planet model.
* @param latitude is the center latitude.
* @param longitude is the center longitude.
- * @param radius is the radius angle.
- * @param accuracy is the maximum difference between the circle approximation and the real circle, as computed using
+ * @param radius is the radius surface distance.
+ * @param accuracy is the maximum linear distance between the circle approximation and the real circle, as computed using
* the Vincenty formula.
* @return a GeoCircle corresponding to what was specified.
*/
diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoExactCircle.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoExactCircle.java
index 03eb5ce8444..e64fd6ea27b 100644
--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoExactCircle.java
+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoExactCircle.java
@@ -52,8 +52,8 @@ class GeoExactCircle extends GeoBaseCircle {
*@param planetModel is the planet model.
*@param lat is the center latitude.
*@param lon is the center longitude.
- *@param cutoffAngle is the cutoff angle for the circle.
- *@param accuracy is the allowed error value.
+ *@param cutoffAngle is the surface radius for the circle.
+ *@param accuracy is the allowed error value (linear distance).
*/
public GeoExactCircle(final PlanetModel planetModel, final double lat, final double lon, final double cutoffAngle, final double accuracy) {
super(planetModel);
@@ -75,119 +75,76 @@ class GeoExactCircle extends GeoBaseCircle {
actualAccuracy = accuracy;
}
- if (Math.abs(cutoffAngle - Math.PI) < Vector.MINIMUM_RESOLUTION) {
- // Circle is the whole world
- this.circlePlanes = null;
- this.eitherBounds = null;
- this.edgePoints = new GeoPoint[0];
- this.notableEdgePoints = null;
- } else if (planetModel.c == planetModel.ab) {
- // Sphere
- this.eitherBounds = null;
- this.circlePlanes = new ArrayList<>(1);
- // Compute two points on the circle, with the right angle from the center. We'll use these
- // to obtain the perpendicular plane to the circle.
- double upperLat = lat + cutoffAngle;
- double upperLon = lon;
- if (upperLat > Math.PI * 0.5) {
- upperLon += Math.PI;
- if (upperLon > Math.PI)
- upperLon -= 2.0 * Math.PI;
- upperLat = Math.PI - upperLat;
- }
- double lowerLat = lat - cutoffAngle;
- double lowerLon = lon;
- if (lowerLat < -Math.PI * 0.5) {
- lowerLon += Math.PI;
- if (lowerLon > Math.PI)
- lowerLon -= 2.0 * Math.PI;
- lowerLat = -Math.PI - lowerLat;
- }
- final GeoPoint upperPoint = new GeoPoint(planetModel, upperLat, upperLon);
- final GeoPoint lowerPoint = new GeoPoint(planetModel, lowerLat, lowerLon);
- // Construct normal plane
- final Plane normalPlane = Plane.constructNormalizedZPlane(upperPoint, lowerPoint, center);
- // Construct a sided plane that goes through the two points and whose normal is in the normalPlane.
- final SidedPlane circlePlane = SidedPlane.constructNormalizedPerpendicularSidedPlane(center, normalPlane, upperPoint, lowerPoint);
- if (circlePlane == null)
- throw new IllegalArgumentException("Couldn't construct circle plane, probably too small? Cutoff angle = "+cutoffAngle+"; upperPoint = "+upperPoint+"; lowerPoint = "+lowerPoint);
- circlePlanes.add(circlePlane);
- final GeoPoint recomputedIntersectionPoint = circlePlane.getSampleIntersectionPoint(planetModel, normalPlane);
- if (recomputedIntersectionPoint == null)
- throw new IllegalArgumentException("Couldn't construct intersection point, probably circle too small? Plane = "+circlePlane);
- this.edgePoints = new GeoPoint[]{recomputedIntersectionPoint};
- this.notableEdgePoints = null;
- } else {
- this.circlePlanes = new ArrayList<>();
- // If it turns out that there's only one circle plane, this array will be populated but unused
- final List<GeoPoint[]> notableEdgePoints = new ArrayList<>();
-
- // We construct approximation planes until we have a low enough error estimate
- final List<ApproximationSlice> slices = new ArrayList<>(100);
- // Construct four cardinal points, and then we'll build the first two planes
- final GeoPoint northPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, 0.0);
- final GeoPoint southPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, Math.PI);
- final GeoPoint eastPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, Math.PI * 0.5);
- final GeoPoint westPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, Math.PI * 1.5);
+ // Since the provide cutoff angle is really a surface distance, we need to use the point-on-bearing even for spheres.
+ final List<SidedPlane> circlePlanes = new ArrayList<>();
+ // If it turns out that there's only one circle plane, this array will be populated but unused
+ final List<GeoPoint[]> notableEdgePoints = new ArrayList<>();
- this.edgePoints = new GeoPoint[]{northPoint};
+ // We construct approximation planes until we have a low enough error estimate
+ final List<ApproximationSlice> slices = new ArrayList<>(100);
+ // Construct four cardinal points, and then we'll build the first two planes
+ final GeoPoint northPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, 0.0);
+ final GeoPoint southPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, Math.PI);
+ final GeoPoint eastPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, Math.PI * 0.5);
+ final GeoPoint westPoint = planetModel.surfacePointOnBearing(center, cutoffAngle, Math.PI * 1.5);
- if (planetModel.c > planetModel.ab) {
- // z can be greater than x or y, so ellipse is longer in height than width
- slices.add(new ApproximationSlice(center, eastPoint, Math.PI * 0.5, westPoint, Math.PI * -0.5, northPoint, 0.0));
- slices.add(new ApproximationSlice(center, westPoint, Math.PI * 1.5, eastPoint, Math.PI * 0.5, southPoint, Math.PI));
- } else {
- // z will be less than x or y, so ellipse is shorter than it is tall
- slices.add(new ApproximationSlice(center, northPoint, Math.PI * 2.0, southPoint, Math.PI, eastPoint, Math.PI * 0.5));
- slices.add(new ApproximationSlice(center, southPoint, Math.PI, northPoint, 0.0, westPoint, Math.PI * 1.5));
- }
+ if (planetModel.c > planetModel.ab) {
+ // z can be greater than x or y, so ellipse is longer in height than width
+ slices.add(new ApproximationSlice(center, eastPoint, Math.PI * 0.5, westPoint, Math.PI * -0.5, northPoint, 0.0));
+ slices.add(new ApproximationSlice(center, westPoint, Math.PI * 1.5, eastPoint, Math.PI * 0.5, southPoint, Math.PI));
+ } else {
+ // z will be less than x or y, so ellipse is shorter than it is tall
+ slices.add(new ApproximationSlice(center, northPoint, Math.PI * 2.0, southPoint, Math.PI, eastPoint, Math.PI * 0.5));
+ slices.add(new ApproximationSlice(center, southPoint, Math.PI, northPoint, 0.0, westPoint, Math.PI * 1.5));
+ }
- // Now, iterate over slices until we have converted all of them into safe SidedPlanes.
- while (slices.size() > 0) {
- // Peel off a slice from the back
- final ApproximationSlice thisSlice = slices.remove(slices.size()-1);
- // Assess it to see if it is OK as it is, or needs to be split.
- // To do this, we need to look at the part of the circle that will have the greatest error.
- // We will need to compute bearing points for these.
- final double interpPoint1Bearing = (thisSlice.point1Bearing + thisSlice.middlePointBearing) * 0.5;
- final GeoPoint interpPoint1 = planetModel.surfacePointOnBearing(center, cutoffAngle, interpPoint1Bearing);
- final double interpPoint2Bearing = (thisSlice.point2Bearing + thisSlice.middlePointBearing) * 0.5;
- final GeoPoint interpPoint2 = planetModel.surfacePointOnBearing(center, cutoffAngle, interpPoint2Bearing);
- // Is this point on the plane? (that is, is the approximation good enough?)
- if (Math.abs(thisSlice.plane.evaluate(interpPoint1)) < actualAccuracy && Math.abs(thisSlice.plane.evaluate(interpPoint2)) < actualAccuracy) {
- // Good enough; add it to the list of planes, unless it was identical to the previous plane
- if (circlePlanes.size() == 0 || !circlePlanes.get(circlePlanes.size()-1).isNumericallyIdentical(thisSlice.plane)) {
- circlePlanes.add(thisSlice.plane);
- notableEdgePoints.add(new GeoPoint[]{thisSlice.endPoint1, thisSlice.endPoint2});
- }
- } else {
- // Split the plane into two, and add it back to the end
- slices.add(new ApproximationSlice(center,
- thisSlice.endPoint1, thisSlice.point1Bearing,
- thisSlice.middlePoint, thisSlice.middlePointBearing,
- interpPoint1, interpPoint1Bearing));
- slices.add(new ApproximationSlice(center,
- thisSlice.middlePoint, thisSlice.middlePointBearing,
- thisSlice.endPoint2, thisSlice.point2Bearing,
- interpPoint2, interpPoint2Bearing));
+ // Now, iterate over slices until we have converted all of them into safe SidedPlanes.
+ while (slices.size() > 0) {
+ // Peel off a slice from the back
+ final ApproximationSlice thisSlice = slices.remove(slices.size()-1);
+ // Assess it to see if it is OK as it is, or needs to be split.
+ // To do this, we need to look at the part of the circle that will have the greatest error.
+ // We will need to compute bearing points for these.
+ final double interpPoint1Bearing = (thisSlice.point1Bearing + thisSlice.middlePointBearing) * 0.5;
+ final GeoPoint interpPoint1 = planetModel.surfacePointOnBearing(center, cutoffAngle, interpPoint1Bearing);
+ final double interpPoint2Bearing = (thisSlice.point2Bearing + thisSlice.middlePointBearing) * 0.5;
+ final GeoPoint interpPoint2 = planetModel.surfacePointOnBearing(center, cutoffAngle, interpPoint2Bearing);
+ // Is this point on the plane? (that is, is the approximation good enough?)
+ if (Math.abs(thisSlice.plane.evaluate(interpPoint1)) < actualAccuracy && Math.abs(thisSlice.plane.evaluate(interpPoint2)) < actualAccuracy) {
+ // Good enough; add it to the list of planes, unless it was identical to the previous plane
+ if (circlePlanes.size() == 0 || !circlePlanes.get(circlePlanes.size()-1).isNumericallyIdentical(thisSlice.plane)) {
+ circlePlanes.add(thisSlice.plane);
+ notableEdgePoints.add(new GeoPoint[]{thisSlice.endPoint1, thisSlice.endPoint2});
}
+ } else {
+ // Split the plane into two, and add it back to the end
+ slices.add(new ApproximationSlice(center,
+ thisSlice.endPoint1, thisSlice.point1Bearing,
+ thisSlice.middlePoint, thisSlice.middlePointBearing,
+ interpPoint1, interpPoint1Bearing));
+ slices.add(new ApproximationSlice(center,
+ thisSlice.middlePoint, thisSlice.middlePointBearing,
+ thisSlice.endPoint2, thisSlice.point2Bearing,
+ interpPoint2, interpPoint2Bearing));
}
+ }
- //System.out.println("Number of planes needed: "+circlePlanes.size());
+ //System.out.println("Number of planes needed: "+circlePlanes.size());
- // Compute bounds
- if (circlePlanes.size() == 1) {
- this.eitherBounds = null;
- this.notableEdgePoints = null;
- } else {
- this.notableEdgePoints = notableEdgePoints;
- this.eitherBounds = new HashMap<>(circlePlanes.size());
- for (int i = 0; i < circlePlanes.size(); i++) {
- final SidedPlane thisPlane = circlePlanes.get(i);
- final SidedPlane previousPlane = (i == 0)?circlePlanes.get(circlePlanes.size()-1):circlePlanes.get(i-1);
- final SidedPlane nextPlane = (i == circlePlanes.size()-1)?circlePlanes.get(0):circlePlanes.get(i+1);
- eitherBounds.put(thisPlane, new EitherBound(previousPlane, nextPlane));
- }
+ this.edgePoints = new GeoPoint[]{northPoint};
+ this.circlePlanes = circlePlanes;
+ // Compute bounds
+ if (circlePlanes.size() == 1) {
+ this.eitherBounds = null;
+ this.notableEdgePoints = null;
+ } else {
+ this.notableEdgePoints = notableEdgePoints;
+ this.eitherBounds = new HashMap<>(circlePlanes.size());
+ for (int i = 0; i < circlePlanes.size(); i++) {
+ final SidedPlane thisPlane = circlePlanes.get(i);
+ final SidedPlane previousPlane = (i == 0)?circlePlanes.get(circlePlanes.size()-1):circlePlanes.get(i-1);
+ final SidedPlane nextPlane = (i == circlePlanes.size()-1)?circlePlanes.get(0):circlePlanes.get(i+1);
+ eitherBounds.put(thisPlane, new EitherBound(previousPlane, nextPlane));
}
}
}
@@ -406,6 +363,9 @@ class GeoExactCircle extends GeoBaseCircle {
this.middlePointBearing = middlePointBearing;
// Construct the plane going through the three given points
this.plane = SidedPlane.constructNormalizedThreePointSidedPlane(center, endPoint1, endPoint2, middlePoint);
+ if (this.plane == null) {
+ throw new IllegalArgumentException("Either circle is too large to fit on ellipsoid or accuracy is too high; could not construct a plane with endPoint1="+endPoint1+" bearing "+point1Bearing+", endPoint2="+endPoint2+" bearing "+point2Bearing+", middle="+middlePoint+" bearing "+middlePointBearing);
+ }
}
}
diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoCircleTest.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoCircleTest.java
index 21f8d6aacd4..ad21edad8a4 100755
--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoCircleTest.java
+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoCircleTest.java
@@ -481,5 +481,25 @@ public class GeoCircleTest extends LuceneTestCase {
double surfaceDistance = planetModel.surfaceDistance(center, point);
assertTrue(planetModel.toString() + " " + Double.toString(surfaceDistance - radius) + " " + Double.toString(radius), surfaceDistance - radius < Vector.MINIMUM_ANGULAR_RESOLUTION);
}
+
+ @Test
+ public void exactCircleLargeTest(){
+ boolean success = true;
+ try {
+ GeoCircle circle = GeoCircleFactory.makeExactGeoCircle(new PlanetModel(0.5, 0.7), 0.25 * Math.PI, 0,0.35 * Math.PI, 1e-12);
+ } catch (IllegalArgumentException e) {
+ success = false;
+ }
+ assertTrue(success);
+ success = false;
+ try {
+ GeoCircle circle = GeoCircleFactory.makeExactGeoCircle(PlanetModel.WGS84, 0.25 * Math.PI, 0,0.9996 * Math.PI, 1e-12);
+ } catch (IllegalArgumentException e) {
+ success = true;
+ }
+ assertTrue(success);
+ }
+
+
}
diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeo3dShapeGenerator.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeo3dShapeGenerator.java
index d41bf1b10de..2c29528b80f 100644
--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeo3dShapeGenerator.java
+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeo3dShapeGenerator.java
@@ -55,6 +55,7 @@ public class RandomGeo3dShapeGenerator extends LuceneTestCase {
final protected static int COLLECTION = 8;
final protected static int POINT = 9;
final protected static int LINE = 10;
+ final protected static int EXACT_CIRCLE = 11;
/* Helper shapes for generating constraints whch are just three sided polygons */
final protected static int CONVEX_SIMPLE_POLYGON = 500;
@@ -87,7 +88,7 @@ public class RandomGeo3dShapeGenerator extends LuceneTestCase {
* @return a random generated shape code
*/
public int randomShapeType(){
- return random().nextInt(11);
+ return random().nextInt(12);
}
/**
@@ -100,7 +101,7 @@ public class RandomGeo3dShapeGenerator extends LuceneTestCase {
* @return a random generated polygon code
*/
public int randomGeoAreaShapeType(){
- return random().nextInt(11);
+ return random().nextInt(12);
}
/**
@@ -284,6 +285,9 @@ public class RandomGeo3dShapeGenerator extends LuceneTestCase {
case CONCAVE_SIMPLE_POLYGON: {
return concaveSimplePolygon(planetModel, constraints);
}
+ case EXACT_CIRCLE: {
+ return exactCircle(planetModel, constraints);
+ }
default:
throw new IllegalStateException("Unexpected shape type");
}
@@ -350,6 +354,38 @@ public class RandomGeo3dShapeGenerator extends LuceneTestCase {
return null;
}
+ /**
+ * Method that returns a random generated a GeoCircle under given constraints. Returns
+ * NULL if it cannot build the GeoCircle under the given constraints.
+ *
+ * @param planetModel The planet model.
+ * @param constraints The given constraints.
+ * @return The random generated GeoCircle.
+ */
+ private GeoCircle exactCircle(PlanetModel planetModel , Constraints constraints) {
+ int iterations=0;
+ while (iterations < MAX_SHAPE_ITERATIONS) {
+ iterations++;
+ final GeoPoint center = randomGeoPoint(planetModel, constraints);
+ if (center == null){
+ continue;
+ }
+ final double radius = randomCutoffAngle();
+ final int pow = random().nextInt(10) +3;
+ final double accuracy = random().nextDouble() * Math.pow(10, (-1) * pow);
+ try {
+ GeoCircle circle = GeoCircleFactory.makeExactGeoCircle(planetModel, center.getLatitude(), center.getLongitude(), radius, accuracy);
+ if (!constraints.valid(circle)) {
+ continue;
+ }
+ return circle;
+ } catch (IllegalArgumentException e) {
+ continue;
+ }
+ }
+ return null;
+ }
+
/**
* Method that returns a random generated a GeoBBox under given constraints. Returns
* NULL if it cannot build the GeoBBox under the given constraints.