Apache Lucene open-source search software
LUCENE-9197: fix wrong implementation on Point2D#withinTriangle (#1228)
Ignacio Vera641680fbf106
diff --git a/lucene/core/src/java/org/apache/lucene/geo/Point2D.java b/lucene/core/src/java/org/apache/lucene/geo/Point2D.javaindex 4547a22c727..d9865d4ce63 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 {@Overridepublic 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 {@Overridepublic 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.javaindex e6609ad31af..bc4c1350bdf 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()));}}}