Skip to content

Commit

Permalink
fix(polygon): Implement extra check in polygon_relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed May 30, 2023
1 parent 05a2589 commit ff73cd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions ladybug_geometry/geometry2d/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,9 +933,15 @@ def polygon_relationship(self, polygon, tolerance):
pt_rels1 = [self.point_relationship(pt, tolerance) for pt in polygon]
pt_rels2 = [polygon.point_relationship(pt, tolerance) for pt in self]
if all(r1 >= 0 for r1 in pt_rels1) and all(r2 <= 0 for r2 in pt_rels2):
return 1 # definitely inside the polygon
if 1 in pt_rels1 or 1 in pt_rels2 or all(r2 == 0 for r2 in pt_rels2):
poi = polygon.pole_of_inaccessibility(tolerance)
if self.point_relationship(poi, tolerance) == 1:
return 1 # definitely inside the polygon
if 1 in pt_rels1 or 1 in pt_rels2:
return 0 # definitely overlap in the polygons
if all(r2 == 0 for r2 in pt_rels2):
poi = self.pole_of_inaccessibility(tolerance)
if polygon.point_relationship(poi, tolerance) == 1:
return 0

# offset one of the polygons inward by the tolerance
off_poly = polygon.offset(tolerance)
Expand Down
16 changes: 15 additions & 1 deletion tests/polygon2d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ def test_polygon_relationship():
# check the polygon with partial overlaps extending outward
pts_1 = (
Point2D(-6.30, 0.00), Point2D(-6.30, -1.54), Point2D(-3.16, -1.54),
Point2D (-3.16, -6.24),Point2D (0.00, -6.24), Point2D(0.00, 0.00)
Point2D(-3.16, -6.24), Point2D(0.00, -6.24), Point2D(0.00, 0.00)
)
pts_2 = (
Point2D(-3.16, -1.54), Point2D(-6.55, -1.54), Point2D(-9.04, -1.54),
Expand All @@ -617,6 +617,20 @@ def test_polygon_relationship():
assert poly_2.polygon_relationship(poly_1, 0.01) == 0


def test_polygon_relationship_concave():
"""Test the polygon_relationship method."""
# check the case of polygon inside a concave hole
inside_pts = [Point2D(1, 0), Point2D(2, 0), Point2D(2, 1), Point2D(1, 1)]
outside_pts = [
Point2D(0, 0), Point2D(1, 0), Point2D(1, 1), Point2D(2, 1), Point2D(2, 0),
Point2D(3, 0), Point2D(3, 3), Point2D(0, 3)
]
inside_polygon = Polygon2D(inside_pts)
outside_polygon = Polygon2D(outside_pts)
assert inside_polygon.polygon_relationship(outside_polygon, 0.01) == -1
assert outside_polygon.polygon_relationship(inside_polygon, 0.01) == -1


def test_distance_to_point():
"""Test the distance_to_point method."""
pts = (Point2D(0, 0), Point2D(4, 0), Point2D(4, 2), Point2D(2, 2),
Expand Down

0 comments on commit ff73cd2

Please sign in to comment.