You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by kw...@apache.org on 2017/10/24 19:46:37 UTC

lucene-solr:branch_6x: LUCENE-7990: Fix polygon generator to not generate crossing polygons. Committed on behalf of Ignacio Vera.

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_6x deaff03d5 -> 3a95ccf32


LUCENE-7990: Fix polygon generator to not generate crossing polygons.  Committed on behalf of Ignacio Vera.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/3a95ccf3
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/3a95ccf3
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/3a95ccf3

Branch: refs/heads/branch_6x
Commit: 3a95ccf32ee732d904dd544634567ee169465bac
Parents: deaff03
Author: Karl Wright <Da...@gmail.com>
Authored: Tue Oct 24 15:42:50 2017 -0400
Committer: Karl Wright <Da...@gmail.com>
Committed: Tue Oct 24 15:46:24 2017 -0400

----------------------------------------------------------------------
 .../geom/RandomGeo3dShapeGenerator.java         | 108 ++++++-------------
 1 file changed, 31 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/3a95ccf3/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/RandomGeo3dShapeGenerator.java
----------------------------------------------------------------------
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 2c29528..cbefcad 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
@@ -19,9 +19,9 @@ package org.apache.lucene.spatial3d.geom;
 
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.lucene.util.LuceneTestCase;
 
@@ -852,84 +852,38 @@ public class RandomGeo3dShapeGenerator extends LuceneTestCase {
   /**
    * Method that orders a lit of points anti-clock-wise to prevent crossing edges.
    *
-   * @param originalPoints The points to order.
+   * @param points The points to order.
    * @return The list of ordered points anti-clockwise.
    */
-  private List<GeoPoint> orderPoints(List<GeoPoint> originalPoints){
-    List<GeoPoint> points = new ArrayList<>(originalPoints.size());
-    points.addAll(originalPoints); //make a copy
-    GeoPoint lPoint = getPointLefLon(points);
-    points.remove(lPoint);
-    GeoPoint rPoint = getPointRigthLon(points);
-    points.remove(rPoint);
-    List<GeoPoint> APoints = getPointsBelowAndSort(points, lPoint);
-    List<GeoPoint> BPoints = getPointsAboveAndsort(points, lPoint);
-    List<GeoPoint> result = new ArrayList<>();
-    result.add(lPoint);
-    result.addAll(APoints);
-    result.add(rPoint);
-    result.addAll(BPoints);
-    return result;
-  }
-
-  private List<GeoPoint> getPointsAboveAndsort(List<GeoPoint> points,GeoPoint lPoint) {
-    List<GeoPoint> BPoints = new ArrayList<>();
-    for (GeoPoint point : points){
-      if(point.getLatitude() > lPoint.getLatitude()){
-        BPoints.add(point);
-      }
-    }
-    Collections.sort(BPoints, new Comparator<GeoPoint>() {
-      public int compare(GeoPoint idx1, GeoPoint idx2) {
-        return Double.compare(idx1.getLongitude(), idx2.getLongitude());
-      }
-    });
-    return BPoints;
-  }
-
-  private List<GeoPoint> getPointsBelowAndSort(List<GeoPoint> points,GeoPoint lPoint) {
-    List<GeoPoint> APoints = new ArrayList<>();
-    for (GeoPoint point : points){
-      if(point.getLatitude() < lPoint.getLatitude()){
-        APoints.add(point);
-      }
-    }
-    Collections.sort(APoints, new Comparator<GeoPoint>() {
-      public int compare(GeoPoint idx1, GeoPoint idx2) {
-        return Double.compare(idx1.getLongitude(), idx2.getLongitude());
-      }
-    });
-    return APoints;
-  }
-
-  private GeoPoint getPointLefLon(List<GeoPoint> points)  {
-    GeoPoint lPoint = null;
-    for (GeoPoint point : points){
-      if(lPoint == null ){
-        lPoint = point;
-      }
-      else{
-        if (lPoint.getLongitude() > point.getLongitude()){
-          lPoint = point;
-        }
-      }
-    }
-    return lPoint;
-  }
-
-  private GeoPoint getPointRigthLon(List<GeoPoint> points) {
-    GeoPoint rPoint = null;
-    for (GeoPoint point : points){
-      if(rPoint == null ){
-        rPoint = point;
-      }
-      else{
-        if (rPoint.getLongitude() < point.getLongitude()){
-          rPoint = point;
-        }
-      }
-    }
-    return rPoint;
+  private List<GeoPoint> orderPoints(List<GeoPoint> points) {
+    double x = 0;
+    double y = 0;
+    double z = 0;
+    //get center of mass
+    for (GeoPoint point : points) {
+      x += point.x;
+      y += point.y;
+      z += point.z;
+    }
+    Map<Double, GeoPoint> pointWithAngle = new HashMap<>();
+    //get angle respect center of mass
+    for (GeoPoint point : points) {
+      GeoPoint center = new GeoPoint(x / points.size(), y / points.size(), z / points.size());
+      double cs = Math.sin(center.getLatitude()) * Math.sin(point.getLatitude())
+          + Math.cos(center.getLatitude()) * Math.cos(point.getLatitude())  * Math.cos(point.getLongitude() - center.getLongitude());
+      double posAng = Math.atan2(Math.cos(center.getLatitude()) * Math.cos(point.getLatitude()) * Math.sin(point.getLongitude() - center.getLongitude()),
+          Math.sin(point.getLatitude()) - Math.sin(center.getLatitude())*cs);
+      pointWithAngle.put(posAng, point);
+    }
+    //order points
+    List<Double> angles = new ArrayList<>(pointWithAngle.keySet());
+    Collections.sort(angles);
+    Collections.reverse(angles);
+    List<GeoPoint> orderedPoints = new ArrayList<>();
+    for (Double d : angles) {
+      orderedPoints.add(pointWithAngle.get(d));
+    }
+    return orderedPoints;
   }
 
   /**