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 2019/03/12 08:20:00 UTC
[lucene-solr] branch branch_8x updated: LUCENE-8713: Add Line2D
tests
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 31809b3 LUCENE-8713: Add Line2D tests
31809b3 is described below
commit 31809b3e27eddf353d59f8b9820f85deb552ef96
Author: iverase <iv...@apache.org>
AuthorDate: Tue Mar 12 09:18:54 2019 +0100
LUCENE-8713: Add Line2D tests
---
lucene/CHANGES.txt | 2 +
.../src/java/org/apache/lucene/geo/Line2D.java | 19 +++--
.../lucene/document/BaseLatLonShapeTestCase.java | 2 +-
.../src/test/org/apache/lucene/geo/TestLine2D.java | 85 ++++++++++++++++++++++
4 files changed, 100 insertions(+), 8 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index b7dd1f5..d9c2413 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -33,6 +33,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 7aff1aa..10c692e 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 1631373..d15248d 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 0000000..da25cba
--- /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));
+ }
+ }
+ }
+}