You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by iv...@apache.org on 2020/02/04 06:11:36 UTC

[lucene-solr] branch branch_8x updated: LUCENE-9197: fix wrong implementation on Point2D#withinTriangle (#1228)

This is an automated email from the ASF dual-hosted git repository.

ivera pushed a commit to branch branch_8x
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/branch_8x by this push:
     new 996945f  LUCENE-9197: fix wrong implementation on Point2D#withinTriangle (#1228)
996945f is described below

commit 996945fff758bbb636ba1273e70cd5442ea36089
Author: Ignacio Vera <iv...@apache.org>
AuthorDate: Tue Feb 4 07:10:08 2020 +0100

    LUCENE-9197: fix wrong implementation on Point2D#withinTriangle (#1228)
---
 .../src/java/org/apache/lucene/geo/Point2D.java    | 35 ++++------------------
 .../test/org/apache/lucene/geo/TestPoint2D.java    |  9 ++++++
 2 files changed, 14 insertions(+), 30 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/geo/Point2D.java b/lucene/core/src/java/org/apache/lucene/geo/Point2D.java
index 4547a22..d9865d4 100644
--- a/lucene/core/src/java/org/apache/lucene/geo/Point2D.java
+++ b/lucene/core/src/java/org/apache/lucene/geo/Point2D.java
@@ -19,8 +19,6 @@ package org.apache.lucene.geo;
 
 import org.apache.lucene.index.PointValues;
 
-import static org.apache.lucene.geo.GeoUtils.orient;
-
 /**
  * 2D point implementation containing geo spatial logic.
  *
@@ -72,31 +70,10 @@ public class Point2D implements Component2D {
   @Override
   public PointValues.Relation relateTriangle(double minX, double maxX, double minY, double maxY,
                                              double ax, double ay, double bx, double by, double cx, double cy) {
-    if (Component2D.containsPoint(x, y, minX, maxX, minY, maxY) == false) {
-      return PointValues.Relation.CELL_OUTSIDE_QUERY;
-    }
     if (ax == bx && bx == cx && ay == by && by == cy) {
-      return  PointValues.Relation.CELL_INSIDE_QUERY;
-    } else if (ax == cx && ay == cy) {
-      // indexed "triangle" is a line:
-      if (orient(ax, ay, bx, by, x, y) == 0) {
-        return PointValues.Relation.CELL_INSIDE_QUERY;
-      }
-      return PointValues.Relation.CELL_OUTSIDE_QUERY;
-    } else if (ax == bx && ay == by) {
-      // indexed "triangle" is a line:
-      if (orient(bx, by, cx, cy, x, y) == 0) {
-        return PointValues.Relation.CELL_INSIDE_QUERY;
-      }
-      return PointValues.Relation.CELL_OUTSIDE_QUERY;
-    } else if (bx == cx && by == cy) {
-      // indexed "triangle" is a line:
-      if (orient(cx, cy, ax, ay, x, y) == 0) {
-        return PointValues.Relation.CELL_INSIDE_QUERY;
-      }
-      return PointValues.Relation.CELL_OUTSIDE_QUERY;
-    } else if (Component2D.pointInTriangle(minX, maxX, minY, maxY, x, y, ax, ay, bx, by, cx, cy) == true) {
-      // indexed "triangle" is a triangle:
+      return contains(ax, ay) ? PointValues.Relation.CELL_INSIDE_QUERY : PointValues.Relation.CELL_OUTSIDE_QUERY;
+    }
+    if (Component2D.pointInTriangle(minX, maxX, minY, maxY, x, y, ax, ay, bx, by, cx, cy)) {
       return PointValues.Relation.CELL_INSIDE_QUERY;
     }
     return PointValues.Relation.CELL_OUTSIDE_QUERY;
@@ -105,10 +82,8 @@ public class Point2D implements Component2D {
   @Override
   public WithinRelation withinTriangle(double minX, double maxX, double minY, double maxY,
                                        double aX, double aY, boolean ab, double bX, double bY, boolean bc, double cX, double cY, boolean ca) {
-    if (aX == bX && aY == bY && aX == cX && aY == cY) {
-      if (contains(aX, aY)) {
-        return WithinRelation.CANDIDATE;
-      }
+    if (Component2D.pointInTriangle(minX, maxX, minY, maxY, x, y, aX, aY, bX, bY, cX, cY)) {
+      return WithinRelation.CANDIDATE;
     }
     return WithinRelation.DISJOINT;
   }
diff --git a/lucene/core/src/test/org/apache/lucene/geo/TestPoint2D.java b/lucene/core/src/test/org/apache/lucene/geo/TestPoint2D.java
index e6609ad..bc4c135 100644
--- a/lucene/core/src/test/org/apache/lucene/geo/TestPoint2D.java
+++ b/lucene/core/src/test/org/apache/lucene/geo/TestPoint2D.java
@@ -31,6 +31,8 @@ public class TestPoint2D extends LuceneTestCase {
     double cx = 5;
     double cy = 4;
     assertEquals(Relation.CELL_OUTSIDE_QUERY, point2D.relateTriangle(ax, ay, bx, by , cx, cy));
+    assertEquals(Component2D.WithinRelation.DISJOINT,
+        point2D.withinTriangle(ax, ay, random().nextBoolean(), bx, by, random().nextBoolean(), cx, cy, random().nextBoolean()));
   }
 
   public void testTriangleIntersects() {
@@ -42,6 +44,8 @@ public class TestPoint2D extends LuceneTestCase {
     double cx = 0;
     double cy = 1;
     assertEquals(Relation.CELL_INSIDE_QUERY, point2D.relateTriangle(ax, ay, bx, by , cx, cy));
+    assertEquals(Component2D.WithinRelation.CANDIDATE,
+        point2D.withinTriangle(ax, ay, random().nextBoolean(), bx, by, random().nextBoolean(), cx, cy, random().nextBoolean()));
   }
 
   public void testTriangleContains() {
@@ -53,8 +57,11 @@ public class TestPoint2D extends LuceneTestCase {
     double cx = 0;
     double cy = 0;
     assertEquals(Relation.CELL_INSIDE_QUERY, point2D.relateTriangle(ax, ay, bx, by , cx, cy));
+    assertEquals(Component2D.WithinRelation.CANDIDATE,
+        point2D.withinTriangle(ax, ay, random().nextBoolean(), bx, by, random().nextBoolean(), cx, cy, random().nextBoolean()));
   }
 
+
   public void testRandomTriangles() {
     Component2D point2D = Point2D.create(new double[] {GeoTestUtil.nextLatitude(), GeoTestUtil.nextLongitude()});
 
@@ -74,6 +81,8 @@ public class TestPoint2D extends LuceneTestCase {
       Relation r = point2D.relate(tMinX, tMaxX, tMinY, tMaxY);
       if (r == Relation.CELL_OUTSIDE_QUERY) {
         assertEquals(Relation.CELL_OUTSIDE_QUERY, point2D.relateTriangle(ax, ay, bx, by, cx, cy));
+        assertEquals(Component2D.WithinRelation.DISJOINT,
+            point2D.withinTriangle(ax, ay, random().nextBoolean(), bx, by, random().nextBoolean(), cx, cy, random().nextBoolean()));
       }
     }
   }