jpountz commented on a change in pull request #546: LUCENE-8620: LatLonShape contains URL: https://github.com/apache/lucene-solr/pull/546#discussion_r250132342
########## File path: lucene/sandbox/src/java/org/apache/lucene/geo/Rectangle2D.java ########## @@ -148,6 +148,90 @@ public boolean intersectsTriangle(int aX, int aY, int bX, int bY, int cX, int cY return false; } + /** + * Checks if the shape is within the provided triangle. + * + * @param ax longitude of point a of the triangle + * @param ay latitude of point a of the triangle + * @param ab if edge ab belongs to the original shape + * @param bx longitude of point b of the triangle + * @param by latitude of point b of the triangle + * @param bc if edge bc belongs to the original shape + * @param cx longitude of point c of the triangle + * @param cy latitude of point c of the triangle + * @param ca if edge ca belongs to the original shape + * @return the {@link EdgeTree.WithinRelation} + */ + public EdgeTree.WithinRelation withinTriangle(int ax, int ay, boolean ab, int bx, int by, boolean bc, int cx, int cy, boolean ca) { + if (this.crossesDateline() == true) { + //Triangles cannot cross the date line so it is always false + return EdgeTree.WithinRelation.CROSSES; + } + // compute bounding box of triangle + int tMinX = StrictMath.min(StrictMath.min(ax, bx), cx); + int tMaxX = StrictMath.max(StrictMath.max(ax, bx), cx); + int tMinY = StrictMath.min(StrictMath.min(ay, by), cy); + int tMaxY = StrictMath.max(StrictMath.max(ay, by), cy); + if (boxesAreDisjoint(tMinX, tMaxX, tMinY, tMaxY, minX, maxX, minY, maxY)) { + return EdgeTree.WithinRelation.DISJOINT; + } + + return bboxWithinTriangle(ax, ay, ab, bx, by, bc, cx, cy, ca, minX, maxX, minY, maxY); + } + public EdgeTree.WithinRelation bboxWithinTriangle(int ax, int ay, boolean ab, int bx, int by, boolean bc, int cx, int cy, boolean ca, int minLon, int maxLon, int minLat, int maxLat) { + //points belong to the shape so if points are inside the rectangle then it cannot be within. It is valid if the point + //is on the edge. + if (bboxContainsPoint(ax, ay, minLon, maxLon, minLat, maxLat) || + bboxContainsPoint(bx, by, minLon, maxLon, minLat, maxLat) || + bboxContainsPoint(cx, cy, minLon, maxLon, minLat, maxLat)) { + return EdgeTree.WithinRelation.CROSSES; + } + //if any of the edges crosses and edge belonging to the polygon then it cannot be within. + //Note that crosses is different to crosses as it needs to have points at both sides. + boolean inside = false; + if (edgeIntersectsBox(ax, ay, bx, by, minLon, maxLon, minLat, maxLat) == true) { + if (ab == true) { + return EdgeTree.WithinRelation.CROSSES; + } else { + inside = true; + } + } + if (edgeIntersectsBox(bx, by, cx, cy, minLon, maxLon, minLat, maxLat) == true) { + if (bc == true) { + return EdgeTree.WithinRelation.CROSSES; + } else { + inside = true; + } + } + if (edgeIntersectsBox(cx, cy, ax, ay, minLon, maxLon, minLat, maxLat) == true) { + if (ca == true) { + return EdgeTree.WithinRelation.CROSSES; + } else { + inside = true; + } + } + //if any of the edges crosses and edge that does not belong to the polygon + // then it is a candidate for within + if (inside == true) { + return EdgeTree.WithinRelation.CANDIDATE; + } + + // Check if the rectangle is fully within the triangle, if not then the relationship is undefined. + // compute bounding box of triangle + int tMinX = StrictMath.min(StrictMath.min(ax, bx), cx); + int tMaxX = StrictMath.max(StrictMath.max(ax, bx), cx); + int tMinY = StrictMath.min(StrictMath.min(ay, by), cy); + int tMaxY = StrictMath.max(StrictMath.max(ay, by), cy); + if ((tMinX <= minLon && tMaxX >= maxLon && tMinY <= minLat && tMaxY >= maxLat) == false) { + return EdgeTree.WithinRelation.DISJOINT; + } + //We check the min and max points, if true it is inside Review comment: we only check the min point? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org For additional commands, e-mail: dev-h...@lucene.apache.org