Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix on is_polygon_in_polygon_xy #1130

Merged
merged 8 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fixed bug that caused a new-line at the end of the `compas.HERE` constant in IronPython for Mac.
* Fixed unbound method usage of `.cross()` on `Plane`, `Vector` and `Frame`.
* Fixed Grasshopper `draw_polylines` method to return `PolylineCurve` instead of `Polyline` because the latter shows as only points.
* Fixed bug in the `is_polygon_in_polygon_xy` that was not correctly generating all the edges of the second polygon before checking for intersections.
* Fixed `area_polygon` that was, in some cases, returning a negative area.
* Fixed uninstall post-process.
* Fixed support for `System.Decimal` data type on json serialization.
Expand Down
2 changes: 1 addition & 1 deletion src/compas/geometry/predicates/predicates_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def is_polygon_in_polygon_xy(polygon1, polygon2):
for i in range(len(polygon1)):
line = [polygon1[-i], polygon1[-i - 1]]
for j in range(len(polygon2)):
line_ = [polygon2[-j], polygon2[j - 1]]
line_ = [polygon2[-j], polygon2[-j - 1]]
if is_intersection_segment_segment_xy(line, line_):
return False
for pt in polygon2:
Expand Down
19 changes: 18 additions & 1 deletion tests/compas/geometry/predicates/test_predicates_2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from compas.geometry import Circle
from compas.geometry import Plane
from compas.geometry import Point
from compas.geometry import Polygon
from compas.geometry import Vector
from compas.geometry import is_point_in_circle_xy
from compas.geometry import is_point_in_circle_xy, is_polygon_in_polygon_xy


def test_is_point_in_circle_xy():
Expand All @@ -24,3 +25,19 @@ def test_is_point_in_circle_xy_class_input():

pt_outside = Point(15, 15, 0)
assert is_point_in_circle_xy(pt_outside, circle) is False


def test_is_polygon_in_polygon_xy():
polygon_contour = Polygon([(0, 0, 0), (4, 2, 0), (10, 0, 0), (11, 10, 0), (8, 12, 0), (0, 10, 0)])
polygon_inside = Polygon([(5, 5, 0), (10, 5, 0), (10, 10, 0), (5, 10, 0)])
assert is_polygon_in_polygon_xy(polygon_contour, polygon_inside)

polygon_outside = Polygon([(15, 5, 0), (20, 5, 0), (20, 10, 0), (15, 10, 0)])
assert not is_polygon_in_polygon_xy(polygon_contour, polygon_outside)

polygon_intersecting = Polygon([(10, 10, 0), (10, 5, 0), (15, 5, 0), (15, 10, 0)])
assert not is_polygon_in_polygon_xy(polygon_contour, polygon_intersecting)

# shifting the vertices list of the same polygon shouldn't affect the containment check output anymore
polygon_intersecting_shifted = Polygon(polygon_intersecting[1:] + polygon_intersecting[:1])
assert not is_polygon_in_polygon_xy(polygon_contour, polygon_intersecting_shifted)