From ccc9057ea0bd68ecadce25dec920deef248c2e10 Mon Sep 17 00:00:00 2001 From: Karl Wright Date: Sun, 1 May 2016 09:35:00 -0400 Subject: [PATCH] LUCENE-7241: Various changes towards making GeoComplexPolygon fully testable. --- .../spatial3d/geom/GeoComplexPolygon.java | 28 ++++++++++++++++++- .../spatial3d/geom/GeoPolygonFactory.java | 6 +++- .../lucene/spatial3d/TestGeo3DPoint.java | 25 +++++++++-------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java index ffd39e9f3b2..f3bc115ab27 100644 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoComplexPolygon.java @@ -1225,7 +1225,33 @@ class GeoComplexPolygon extends GeoBasePolygon { @Override public String toString() { - return "GeoComplexPolygon: {planetmodel=" + planetModel + ", number of shapes="+shapeStartEdges.length+", address="+ Integer.toHexString(hashCode())+"}"; + final StringBuilder edgeDescription = new StringBuilder(); + for (final Edge shapeStartEdge : shapeStartEdges) { + fillInEdgeDescription(edgeDescription, shapeStartEdge); + } + return "GeoComplexPolygon: {planetmodel=" + planetModel + ", number of shapes="+shapeStartEdges.length+", address="+ Integer.toHexString(hashCode())+", testPoint="+testPoint+", testPointInSet="+testPointInSet+", shapes={"+edgeDescription+"}}"; } + + private static void fillInEdgeDescription(final StringBuilder description, final Edge startEdge) { + description.append(" {"); + Edge currentEdge = startEdge; + int edgeCounter = 0; + while (true) { + if (edgeCounter > 0) { + description.append(", "); + } + if (edgeCounter >= 20) { + description.append("..."); + break; + } + description.append(currentEdge.startPoint); + currentEdge = currentEdge.next; + if (currentEdge == startEdge) { + break; + } + edgeCounter++; + } + } + } diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoPolygonFactory.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoPolygonFactory.java index 91eb3392e10..a4499eea7a3 100755 --- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoPolygonFactory.java +++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/geom/GeoPolygonFactory.java @@ -502,7 +502,11 @@ public class GeoPolygonFactory { final GeoPoint closePoint = points.get(pointIndex); // We pick a random angle and random arc distance, then generate a point based on closePoint final double angle = generator.nextDouble() * Math.PI * 2.0 - Math.PI; - final double maxArcDistance = points.get(0).arcDistance(points.get(1)); + double maxArcDistance = points.get(0).arcDistance(points.get(1)); + double trialArcDistance = points.get(0).arcDistance(points.get(2)); + if (trialArcDistance > maxArcDistance) { + maxArcDistance = trialArcDistance; + } final double arcDistance = maxArcDistance - generator.nextDouble() * maxArcDistance; // We come up with a unit circle (x,y,z) coordinate given the random angle and arc distance. The point is centered around the positive x axis. final double x = Math.cos(arcDistance); diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java index 2028c368025..c977c9ee2a8 100644 --- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java +++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java @@ -925,8 +925,8 @@ public class TestGeo3DPoint extends LuceneTestCase { angles[i] = angle; accumulatedAngle += angle; } - // Pick the arc distance randomly - arcDistance[i] = random().nextDouble() * (Math.PI - MINIMUM_ARC_ANGLE) + MINIMUM_ARC_ANGLE; + // Pick the arc distance randomly; not quite the full range though + arcDistance[i] = random().nextDouble() * (Math.PI * 0.5 - MINIMUM_ARC_ANGLE) + MINIMUM_ARC_ANGLE; } if (clockwiseDesired) { // Reverse the signs @@ -938,6 +938,15 @@ public class TestGeo3DPoint extends LuceneTestCase { // Now, use the pole's information plus angles and arcs to create GeoPoints in the right order. final List polyPoints = convertToPoints(pm, pole, angles, arcDistance); + // Next, do some holes. No more than 2 of these. The poles for holes must always be within the polygon, so we're + // going to use Geo3D to help us select those given the points we just made. + + final int holeCount = createHoles?TestUtil.nextInt(random(), 0, 2):0; + + final List holeList = new ArrayList<>(); + + /* Hole logic is broken and needs rethinking + // Create the geo3d polygon, so we can test out our poles. final GeoPolygon poly; try { @@ -946,14 +955,7 @@ public class TestGeo3DPoint extends LuceneTestCase { // This is what happens when three adjacent points are colinear, so try again. continue; } - - // Next, do some holes. No more than 2 of these. The poles for holes must always be within the polygon, so we're - // going to use Geo3D to help us select those given the points we just made. - - final int holeCount = createHoles?TestUtil.nextInt(random(), 0, 2):0; - - final List holeList = new ArrayList<>(); - + for (int i = 0; i < holeCount; i++) { // Choose a pole. The poly has to be within the polygon, but it also cannot be on the polygon edge. // If we can't find a good pole we have to give it up and not do the hole. @@ -979,7 +981,8 @@ public class TestGeo3DPoint extends LuceneTestCase { } } } - + */ + final Polygon[] holes = holeList.toArray(new Polygon[0]); // Finally, build the polygon and return it