Skip to content

Apache Lucene open-source search software

LUCENE-8713: Add Line2D tests

iverasefd8d9d51999a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c3956662cc2..af23f0b6afc 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -45,6 +45,8 @@ Other
* LUCENE-8685: Refactor LatLonShape tests. (Ignacio Vera)
+* LUCENE-8713: Add Line2D tests. (Ignacio Vera)
+
======================= Lucene 8.0.0 =======================
API Changes
diff --git a/lucene/sandbox/src/java/org/apache/lucene/geo/Line2D.java b/lucene/sandbox/src/java/org/apache/lucene/geo/Line2D.java
index 7aff1aaae73..10c692e05d4 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/geo/Line2D.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/geo/Line2D.java
@@ -16,7 +16,7 @@
*/
package org.apache.lucene.geo;
-import org.apache.lucene.index.PointValues;
+import org.apache.lucene.index.PointValues.Relation;
/**
* 2D line implementation represented as a balanced interval tree of edges.
@@ -41,18 +41,23 @@ public final class Line2D extends EdgeTree {
}
@Override
- protected PointValues.Relation componentRelate(double minLat, double maxLat, double minLon, double maxLon) {
+ protected Relation componentRelate(double minLat, double maxLat, double minLon, double maxLon) {
if (tree.crosses(minLat, maxLat, minLon, maxLon)) {
- return PointValues.Relation.CELL_CROSSES_QUERY;
+ return Relation.CELL_CROSSES_QUERY;
}
- return PointValues.Relation.CELL_OUTSIDE_QUERY;
+
+ return Relation.CELL_OUTSIDE_QUERY;
}
@Override
- protected PointValues.Relation componentRelateTriangle(double ax, double ay, double bx, double by, double cx, double cy) {
+ protected Relation componentRelateTriangle(double ax, double ay, double bx, double by, double cx, double cy) {
if (tree.crossesTriangle(ax, ay, bx, by, cx, cy)) {
- return PointValues.Relation.CELL_CROSSES_QUERY;
+ return Relation.CELL_CROSSES_QUERY;
+ }
+ //check if line is inside triangle
+ if (pointInTriangle(tree.lon1, tree.lat1, ax, ay, bx, by, cx, cy)) {
+ return Relation.CELL_CROSSES_QUERY;
}
- return PointValues.Relation.CELL_OUTSIDE_QUERY;
+ return Relation.CELL_OUTSIDE_QUERY;
}
}
\ No newline at end of file
diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/BaseLatLonShapeTestCase.java b/lucene/sandbox/src/test/org/apache/lucene/document/BaseLatLonShapeTestCase.java
index 1631373ee5d..d15248df8b2 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/document/BaseLatLonShapeTestCase.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/document/BaseLatLonShapeTestCase.java
@@ -110,7 +110,7 @@ public abstract class BaseLatLonShapeTestCase extends LuceneTestCase {
}
/** use {@link GeoTestUtil#nextPolygon()} to create a random line; TODO: move to GeoTestUtil */
- public Line nextLine() {
+ public static Line nextLine() {
Polygon poly = GeoTestUtil.nextPolygon();
double[] lats = new double[poly.numPoints() - 1];
double[] lons = new double[lats.length];
diff --git a/lucene/sandbox/src/test/org/apache/lucene/geo/TestLine2D.java b/lucene/sandbox/src/test/org/apache/lucene/geo/TestLine2D.java
new file mode 100644
index 00000000000..da25cba59f4
--- /dev/null
+++ b/lucene/sandbox/src/test/org/apache/lucene/geo/TestLine2D.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.geo;
+
+import org.apache.lucene.document.TestLatLonLineShapeQueries;
+import org.apache.lucene.index.PointValues.Relation;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestLine2D extends LuceneTestCase {
+
+ public void testTriangleDisjoint() {
+ Line line = new Line(new double[] {0, 1, 2, 3}, new double[] {0, 0, 2, 2});
+ Line2D line2D = Line2D.create(line);
+ int ax = GeoEncodingUtils.encodeLongitude(4);
+ int ay = GeoEncodingUtils.encodeLatitude(4);
+ int bx = GeoEncodingUtils.encodeLongitude(5);
+ int by = GeoEncodingUtils.encodeLatitude(5);
+ int cx = GeoEncodingUtils.encodeLongitude(5);
+ int cy = GeoEncodingUtils.encodeLatitude(4);
+ assertEquals(Relation.CELL_OUTSIDE_QUERY, line2D.componentRelateTriangle(ax, ay, bx, by , cx, cy));;
+ }
+
+ public void testTriangleIntersects() {
+ Line line = new Line(new double[] {0.5, 0, 1, 2, 3}, new double[] {0.5, 0, 0, 2, 2});
+ Line2D line2D = Line2D.create(line);
+ int ax = GeoEncodingUtils.encodeLongitude(0.0);
+ int ay = GeoEncodingUtils.encodeLatitude(0.0);
+ int bx = GeoEncodingUtils.encodeLongitude(1);
+ int by = GeoEncodingUtils.encodeLatitude(0);
+ int cx = GeoEncodingUtils.encodeLongitude(0);
+ int cy = GeoEncodingUtils.encodeLatitude(1);
+ assertEquals(Relation.CELL_CROSSES_QUERY, line2D.componentRelateTriangle(ax, ay, bx, by , cx, cy));
+ }
+
+ public void testTriangleContains() {
+ Line line = new Line(new double[] {0.5, 0, 1, 2, 3}, new double[] {0.5, 0, 0, 2, 2});
+ Line2D line2D = Line2D.create(line);
+ int ax = GeoEncodingUtils.encodeLongitude(-10);
+ int ay = GeoEncodingUtils.encodeLatitude(-10);
+ int bx = GeoEncodingUtils.encodeLongitude(4);
+ int by = GeoEncodingUtils.encodeLatitude(-10);
+ int cx = GeoEncodingUtils.encodeLongitude(4);
+ int cy = GeoEncodingUtils.encodeLatitude(30);
+ assertEquals(Relation.CELL_CROSSES_QUERY, line2D.componentRelateTriangle(ax, ay, bx, by , cx, cy));
+ }
+
+ public void testRandomTriangles() {
+ Line line = TestLatLonLineShapeQueries.nextLine();
+ Line2D line2D = Line2D.create(line);
+
+ for (int i =0; i < 100; i++) {
+ double ax = GeoTestUtil.nextLongitude();
+ double ay = GeoTestUtil.nextLatitude();
+ double bx = GeoTestUtil.nextLongitude();
+ double by = GeoTestUtil.nextLatitude();
+ double cx = GeoTestUtil.nextLongitude();
+ double cy = GeoTestUtil.nextLatitude();
+
+ double tMinX = StrictMath.min(StrictMath.min(ax, bx), cx);
+ double tMaxX = StrictMath.max(StrictMath.max(ax, bx), cx);
+ double tMinY = StrictMath.min(StrictMath.min(ay, by), cy);
+ double tMaxY = StrictMath.max(StrictMath.max(ay, by), cy);
+
+ Relation r = line2D.relate(tMinY, tMaxY, tMinX, tMaxX);
+ if (r == Relation.CELL_OUTSIDE_QUERY) {
+ assertEquals(Relation.CELL_OUTSIDE_QUERY, line2D.componentRelateTriangle(ax, ay, bx, by, cx, cy));
+ }
+ }
+ }
+}