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

Predicates2 new methods addition #1153

Open
wants to merge 181 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
181 commits
Select commit Hold shift + click to select a range
2ba8533
Adding reshape and unflatten to compas.utilities
romanarust Jul 26, 2021
ed2229b
small function add to glft
romanarust Jul 26, 2021
a10d0ad
Revert "small function add to glft"
romanarust Jul 28, 2021
99ec0cb
Update __init__.py
romanarust Jul 28, 2021
1fe82a0
Update __init__.py
romanarust Jul 28, 2021
4dc265e
Update src/compas/utilities/itertools.py
romanarust Jul 30, 2021
95c1e59
removing unflatten
romanarust Jul 30, 2021
55f2921
adding tests
romanarust Jul 31, 2021
2d569b6
remove unflatten from init
romanarust Jul 31, 2021
40accd6
update flatten and reduce
romanarust Jul 31, 2021
cd8e8e2
update flatten docstr
romanarust Jul 31, 2021
0f5a7f9
Update CHANGELOG.md
romanarust Aug 2, 2021
d2afaf5
small fix
romanarust Oct 28, 2022
bf59b37
add texture, normals and vertex color support if data available
romanarust Oct 28, 2022
2572319
remove note
romanarust Oct 28, 2022
78daeab
Merge branch 'main' into feature/support_gltf_texture_coordinates
romanarust Oct 28, 2022
15a2be9
Update CHANGELOG.md
romanarust Oct 28, 2022
6433bb7
Fixed unbound method usage of `.cross()`
Mar 1, 2023
fabde09
bug fix on `is_polygon_in_polygon_xy`
baehrjo Mar 23, 2023
8226ba7
add test to is_polygon_in_polygon_xy fix report
baehrjo Mar 23, 2023
b1d3ddf
Update release.yml
Licini Mar 25, 2023
6b7c3e8
comment test method
baehrjo Mar 28, 2023
6126fed
Merge branch 'main' into polygon-in-polygon-fix
tomvanmele Mar 28, 2023
05499b1
up to numerical
tomvanmele May 6, 2023
5c38118
all done
tomvanmele May 10, 2023
bc3714f
blender and rhino
tomvanmele May 20, 2023
ebecc85
plotters
tomvanmele May 25, 2023
fcd0051
lint
tomvanmele May 25, 2023
31fce1b
changelog
tomvanmele May 25, 2023
338400b
working config without api
tomvanmele May 25, 2023
53c9b4e
book theme and furo
tomvanmele May 29, 2023
900b7e9
further config of book theme
tomvanmele May 29, 2023
7a3eba9
small tweaks
tomvanmele May 29, 2023
d04e282
final adaptations of book theme
tomvanmele May 31, 2023
f32cb8c
switch to pydata
tomvanmele May 31, 2023
aaf385f
config and restructuring for pydata
tomvanmele May 31, 2023
d410c39
small tweaks
tomvanmele Jun 1, 2023
c41df18
update path
tomvanmele Jun 1, 2023
413534f
remove class attributes section
tomvanmele Jun 1, 2023
b46a68a
add blank line
tomvanmele Jun 1, 2023
a7172ca
remove inheritance
tomvanmele Jun 1, 2023
7361ff3
remove other conf
tomvanmele Jun 1, 2023
bb1a35e
remove inheritance extension
tomvanmele Jun 1, 2023
9ba5f18
remove explicit theme switcher
tomvanmele Jun 1, 2023
9c15e60
first attempt at extensions page
tomvanmele Jun 1, 2023
c57c46a
set height of the brand image
tomvanmele Jun 1, 2023
8500d85
stop using the title text
tomvanmele Jun 1, 2023
f5667d5
separate sections for core extensions
tomvanmele Jun 1, 2023
bf3782a
ignore generated
tomvanmele Jun 2, 2023
ca1283b
see also
tomvanmele Jun 2, 2023
fb4b358
devguide structure
tomvanmele Jun 2, 2023
1b56827
remove
tomvanmele Jun 2, 2023
aec4329
extensions
tomvanmele Jun 2, 2023
162d859
modify the html writers
tomvanmele Jun 2, 2023
b8b2025
styling
tomvanmele Jun 2, 2023
815f446
remove unused packages
tomvanmele Jun 2, 2023
f6acae3
general updates
tomvanmele Jun 2, 2023
564064b
muted color and line height
tomvanmele Jun 7, 2023
1cf75cd
add inline tabs
tomvanmele Jun 7, 2023
6c7929d
refer to versions.json on gh-pages
Licini Jun 8, 2023
73104f0
lint
tomvanmele Jun 8, 2023
be377c2
update stubs directory for api test
tomvanmele Jun 8, 2023
1b7178f
and skip api testing for now
tomvanmele Jun 8, 2023
0913eb8
skip the skip for ipy
tomvanmele Jun 8, 2023
787973a
little lint check again
baehrjo Mar 28, 2023
a23028c
added split methods to polyline
nmaslarinos Jun 8, 2023
59cff0d
new predicate methods added
baehrjo Jun 8, 2023
05139fa
change email
tomvanmele Jun 8, 2023
76e6f2e
change copyright year
tomvanmele Jun 8, 2023
e9faac1
copyright info
tomvanmele Jun 8, 2023
abd90b6
Merge branch 'main' into new-docs-theme
tomvanmele Jun 8, 2023
85d3837
fix for polyline split methods not returning correct results. Added t…
nmaslarinos Jun 10, 2023
d56da2c
run black
nmaslarinos Jun 10, 2023
d1c930b
removed testing safeguard
nmaslarinos Jun 10, 2023
312d922
Merge pull request #1134 from compas-dev/Licini-patch-1
tomvanmele Jun 13, 2023
7fa21a1
fix volmesh artists
gonzalocasas Jun 13, 2023
e07ff65
update changelog
gonzalocasas Jun 13, 2023
4f9ec40
Merge branch 'main' into feature/support_gltf_texture_coordinates
tomvanmele Jun 14, 2023
c8dcf25
Merge pull request #1158 from compas-dev/volmeshartist_bug
tomvanmele Jun 14, 2023
a3959c5
remove deprecations and update api tests
tomvanmele Jun 14, 2023
b80c5bc
update ipy api file
tomvanmele Jun 14, 2023
0aa6f52
ugh
tomvanmele Jun 14, 2023
3f579e7
not sure how to test this locally
tomvanmele Jun 14, 2023
ad47fe0
Merge pull request #1144 from compas-dev/init-cleanup
tomvanmele Jun 14, 2023
bc5d3a7
differentiate between data and json data (with respective schemas)
tomvanmele May 25, 2023
a4e7669
add schema wrapper
tomvanmele Jun 5, 2023
2e508d1
switch to in-code schemas for json
tomvanmele Jun 5, 2023
2c0964b
remove independent schema files
tomvanmele Jun 5, 2023
d5f0015
in-code json schema and live data for geometry objects
tomvanmele Jun 5, 2023
23a04c7
remove the wrapping
tomvanmele Jun 7, 2023
31980aa
data schema as class attribute
tomvanmele Jun 7, 2023
0f13949
switch to in-code value schemas, add document schema validation, upda…
tomvanmele Jun 8, 2023
f223f4e
lint
tomvanmele Jun 8, 2023
d80bfd6
ipy sring format
tomvanmele Jun 8, 2023
2796660
skip api completeness test for now
tomvanmele Jun 8, 2023
aeba839
skip the skip for ipy
tomvanmele Jun 8, 2023
2194404
log
tomvanmele Jun 13, 2023
9181386
add the tests back
tomvanmele Jun 14, 2023
ec6d0ab
Merge branch 'main' into feature/support_gltf_texture_coordinates
tomvanmele Jun 14, 2023
97de464
Merge pull request #1093 from compas-dev/feature/support_gltf_texture…
tomvanmele Jun 14, 2023
05bac88
Merge branch 'main' into live-data
tomvanmele Jun 14, 2023
39551af
differentiate between data and json data (with respective schemas)
tomvanmele May 25, 2023
3ac322b
add schema wrapper
tomvanmele Jun 5, 2023
b7c17d4
switch to in-code schemas for json
tomvanmele Jun 5, 2023
b896caf
remove independent schema files
tomvanmele Jun 5, 2023
a058de4
in-code json schema and live data for geometry objects
tomvanmele Jun 5, 2023
11125e7
remove the wrapping
tomvanmele Jun 7, 2023
6846a17
data schema as class attribute
tomvanmele Jun 7, 2023
885bcb2
switch to in-code value schemas, add document schema validation, upda…
tomvanmele Jun 8, 2023
f77cb16
lint
tomvanmele Jun 8, 2023
2ea9af2
ipy sring format
tomvanmele Jun 8, 2023
af3eea5
skip api completeness test for now
tomvanmele Jun 8, 2023
6ce2bd5
skip the skip for ipy
tomvanmele Jun 8, 2023
40c5384
use tuple as edge identifier everywhere
tomvanmele Jun 9, 2023
8710fe2
see also
tomvanmele Jun 11, 2023
97ad624
edge identifier as tuple
tomvanmele Jun 13, 2023
e2cbe58
see also
tomvanmele Jun 13, 2023
fb81bfa
remove deprecated
tomvanmele Jun 13, 2023
c96d6e1
see also
tomvanmele Jun 13, 2023
649a1fb
see also and geometry returns and a few small fixes
tomvanmele Jun 13, 2023
e536857
see also
tomvanmele Jun 13, 2023
a84b33b
log
tomvanmele Jun 13, 2023
ab62d64
lint
tomvanmele Jun 13, 2023
aac40f2
ipy doesn't like unpacking objects
tomvanmele Jun 13, 2023
7080f90
Update src/compas/datastructures/halfface/halfface.py
tomvanmele Jun 15, 2023
6d147bf
Merge pull request #1148 from compas-dev/live-data
tomvanmele Jun 15, 2023
93296b7
Merge branch 'main' into datastructure-upgrade
tomvanmele Jun 15, 2023
c58b67d
remove commented sections
tomvanmele Jun 15, 2023
6426c7e
removed public references to old map naming conventions
tomvanmele Jun 15, 2023
49b996b
log element index maps
tomvanmele Jun 15, 2023
897de99
Merge pull request #1157 from compas-dev/datastructure-upgrade
tomvanmele Jun 16, 2023
bf91e71
Merge branch 'main' into new-docs-theme
tomvanmele Jun 16, 2023
5080734
latest updates
tomvanmele Jun 16, 2023
aef5eab
include in generated docs
tomvanmele Jun 16, 2023
7a73089
small fixes
tomvanmele Jun 16, 2023
62346e0
use "latest" in dev docs
Licini Jun 16, 2023
5126faa
Merge remote-tracking branch 'origin/new-docs-theme' into new-docs-theme
tomvanmele Jun 16, 2023
d4e6d76
Merge branch 'main' into fix_cross_product
tomvanmele Jun 17, 2023
d3711ae
Merge pull request #1123 from katarametin/fix_cross_product
tomvanmele Jun 17, 2023
93fb7ee
added boundary test cases
nmaslarinos Jun 17, 2023
9db8a26
fixed docstring tests, added checks to input length
nmaslarinos Jun 17, 2023
4dca7bd
Merge branch 'main' into polyline-split-functions
nmaslarinos Jun 17, 2023
ef0a9fe
format with black
nmaslarinos Jun 19, 2023
98c1255
Merge pull request #1152 from nmaslarinos/polyline-split-functions
tomvanmele Jun 19, 2023
ce59008
ignore all generated files
tomvanmele Jun 19, 2023
a986c74
Merge remote-tracking branch 'origin/main' into main
tomvanmele Jun 19, 2023
316ea7e
Merge branch 'main' into new-docs-theme
tomvanmele Jun 19, 2023
904054f
restyled index page
tomvanmele Jun 19, 2023
c86a3de
remove flatten changes
romanavyzn Jun 19, 2023
86300f1
Merge branch 'main' into new_utility_functions
romanavyzn Jun 19, 2023
9483518
add warnings about version 2
tomvanmele Jun 19, 2023
1fda2d7
log
tomvanmele Jun 19, 2023
e4f1f8c
Merge pull request #1149 from compas-dev/new-docs-theme
tomvanmele Jun 19, 2023
35f0ea6
ipy is annoying
romanavyzn Jun 19, 2023
d885264
black
romanavyzn Jun 19, 2023
226bbb5
black
romanavyzn Jun 19, 2023
1b47ed3
black
romanavyzn Jun 19, 2023
bba6912
lock python 3.7
Licini Jun 19, 2023
727e4f4
all the way back to 3.7.9
Licini Jun 19, 2023
fcde961
skip 3.7
Licini Jun 19, 2023
a0d3204
Merge pull request #1160 from Licini/workflow
tomvanmele Jun 20, 2023
5d0823b
Merge branch 'main' into polygon-in-polygon-fix
tomvanmele Jun 20, 2023
f3e84e3
Remove lint in test_predicates_2.py
tomvanmele Jun 20, 2023
8ff9fa2
Update test_predicates_2.py
tomvanmele Jun 20, 2023
ad8c262
Merge pull request #1130 from baehrjo/polygon-in-polygon-fix
tomvanmele Jun 20, 2023
d298244
revert some customisations and fix sidebar scroll
tomvanmele Jun 21, 2023
c29734a
simplify (for now)
tomvanmele Jun 21, 2023
c0b5cdf
add warnings
tomvanmele Jun 21, 2023
b63a00b
revert customisation
tomvanmele Jun 21, 2023
5ba87c5
Merge remote-tracking branch 'origin/main' into main
tomvanmele Jun 21, 2023
cf127c2
Merge branch 'main' into new_utility_functions
romanavyzn Jun 21, 2023
09080ad
Update itertools.py
romanavyzn Jun 21, 2023
5531210
Merge pull request #881 from compas-dev/new_utility_functions
tomvanmele Jun 21, 2023
b4f8546
update title everywhere
tomvanmele Jun 21, 2023
e5653ee
simplify landing
tomvanmele Jun 21, 2023
3f39b23
Merge remote-tracking branch 'origin/main' into main
tomvanmele Jun 21, 2023
e7fb12b
lighter font, narrower sidebar, smaller sidebar font
tomvanmele Jun 21, 2023
a4da892
only add framework as external link
tomvanmele Jun 21, 2023
5b17181
theme tweaks
tomvanmele Jul 3, 2023
288b3b0
new predicate methods added
baehrjo Jun 8, 2023
56200e1
Merge branch 'predicates2-new-methods-addition' of https://github.com…
baehrjo Jul 6, 2023
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 AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
- Chen Kasirer <<[email protected]>> [@chenkasirer](https://github.com/chenkasirer)
- Nickolas Maslarinos <<[email protected]>> [@nmaslarinos](https://github.com/nmaslarinos)
- Katerina Toumpektsi <<[email protected]>> [@katarametin](https://github.com/katarametin)
- Joelle Baehr-Bruyere <<[email protected]>> [@baehrjo](https://github.com/baehrjo)
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* Added `create_id` to `compas_ghpython.utilities`. (moved from `compas_fab`)
* Added new predicates in `predicates_2` for points and polygons (XY) : `polygon_to_polygon_relationship_xy`, `point_to_polygon_relationship_xy`, `is_intersection_polygon_polygon_xy`

### Changed

* Fixed bug that caused a new-line at the end of the `compas.HERE` constant in IronPython for Mac.
* 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 uninstall post-process.

### Removed
Expand Down
117 changes: 116 additions & 1 deletion src/compas/geometry/predicates/predicates_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"is_point_in_convex_polygon_xy",
"is_point_in_circle_xy",
"is_polygon_in_polygon_xy",
"polygon_to_polygon_relationship_xy",
"point_to_polygon_relationship_xy",
"is_intersection_polygon_polygon_xy",
"is_intersection_line_line_xy",
"is_intersection_segment_segment_xy",
]
Expand Down Expand Up @@ -383,7 +386,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]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at this code now, would it not be much simpler (and more readable) to do the following:

p1 = len(polygon1)
p2 = len(polygon2)
for i in range(-1, p1 - 1):
    line1 = polygon[i], polygon[i + 1]
    for j in range(-1, p2 - 1):
        line2 = polygon[j], polygon[j + 1]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see #1130 )

if is_intersection_segment_segment_xy(line, line_):
return False
for pt in polygon2:
Expand All @@ -392,6 +395,118 @@ def is_polygon_in_polygon_xy(polygon1, polygon2):
return False


def polygon_to_polygon_relationship_xy(polygon1, polygon2):
"""Determine if a polygon (polygon1) is inside, intersects, is outside or contains another polygon (polygon2) on the XY-plane.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it required that the polygons are convex?

Copy link
Contributor Author

@baehrjo baehrjo Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not required, the methods works for any (XY) polygon.

In the is_polygon_in_polygon method, there's a convexity check at the beginning because if both polygons are convex then checking that all points of one polygon are contained in the other is enough and faster, while without that convexity, we have to check that both all points are inside and no edges intersect.

In polygon_to_polygon_relationship, we need the intersection check anyway as we deduct information from it for other relationship than containment, that's why I don't check convexity at the begenning. Polygon_to_polygon_relationship first checks for intersection, then for containment, and if not deducts that the polygon is outside instead of calling separate methods. Separate methods ('is_polygon_in_polygon_xy', 'is_intersection_polygon_polygon_xy', ...) are still useful when looking for a True/False answer only but calling them inside polygon_to_polygon_relationship would run several times the same pieces of code.


Parameters
----------
polygon1 : sequence[point] | :class:`~compas.geometry.Polygon`
List of XY(Z) coordinates of points representing the locations of the corners of the polygon to examine the position regarding the boundary polygon (Z will be ignored).
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.
polygon2 : sequence[point] | :class:`~compas.geometry.Polygon`
List of XY(Z) coordinates of points representing the locations of the corners of the boundary polygon (Z will be ignored).
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.

Returns
-------
int
1 if polygon1 is inside polygon2.
0 if polygon1 intersects polygon2.
-1 if polygon1 is outside polygon2.
-2 if polygon1 contains polygon2 (polygon2 is inside polygon1).
"""
for i in range(len(polygon1)):
line1 = [polygon1[i], polygon1[i - 1]]
for j in range(len(polygon2)):
line2 = [polygon2[j], polygon2[j - 1]]
if is_intersection_segment_segment_xy(line1, line2):
return 0
Comment on lines +400 to +405
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we not replace this by a call to is_intersection_polygon_polygon_xy?

for pt in polygon1:
if is_point_in_polygon_xy(pt, polygon2):
return 1
if is_point_in_polygon_xy(polygon2.centroid, polygon1):
return -2
Comment on lines +406 to +410
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the different approach here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean different because of calling other existing methods inside the loop here ?

return -1


def point_to_polygon_relationship_xy(point, polygon, tolerance=10**-6):
"""Determine if a point is inside, outside or on the boundary (with tolerance) of a polygon in the XY-plane.

Parameters
----------
point : [float, float, float] | :class:`~compas.geometry.Point`
XY(Z) coordinates of a point (Z will be ignored).
polygon : sequence[point] | :class:`~compas.geometry.Polygon`
List of XY(Z) coordinates of points representing the locations of the corners of the boundary polygon (Z will be ignored).
The vertices are assumed to be in order. The polygon is assumed to be closed:
the first and last vertex in the sequence should not be the same.

Returns
-------
int
1 if point is inside polygon.
0 if point is on the boundary of polygon.
-1 if point is outside polygon.
"""
x, y = point[0], point[1]
polygon = [(p[0], p[1]) for p in polygon] # make 2D
flag = -1
for i in range(-1, len(polygon) - 1):
x1, y1 = polygon[i]
x2, y2 = polygon[i + 1]
segment = ([x1, y1], [x2, y2])
if is_point_on_segment_xy(point, segment, tolerance):
return 0
elif y > min(y1, y2):
if y <= max(y1, y2):
if x <= max(x1, x2):
if y1 != y2:
xinters = (y - y1) * (x2 - x1) / (y2 - y1) + x1
if x1 == x2 or x <= xinters:
flag = -flag
return flag
Comment on lines +433 to +449
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be a combination of is_point_on_polyline_xy and is_point_in_polygon_xy? why not use those?



def is_intersection_polygon_polygon_xy(polygon1, polygon2):
"""Determines if two polygons intersect, assuming they lie in the XY plane.

Parameters
----------
polygon1 : sequence[point] | :class:`~compas.geometry.Polygon`
A Polygon lying in the XY.
polygon2 : sequence[point] | :class:`~compas.geometry.Polygon`
A Polygon lying in the XY.

Returns
-------
bool
True if the polygons intersect. False if they don't.

Examples
--------
>>> from compas.geometry import Polygon
>>> polygon1 = Polygon([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])
>>> polygon2 = Polygon([[0.5, 0.5], [1.5, 0.5], [1.5, 1.5], [0.5, 1.5]])
>>> is_intersection_polygon_polygon_xy(polygon1, polygon2)
True

>>> from compas.geometry import Polygon
>>> polygon1 = Polygon([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]])
>>> polygon2 = Polygon([[1.5, 1.5], [2.5, 1.5], [2.5, 2.5], [1.5, 2.5]])
>>> is_intersection_polygon_polygon_xy(polygon1, polygon2)
False
"""
for i in range(len(polygon1)):
line1 = [polygon1[i], polygon1[i - 1]]
for j in range(len(polygon2)):
line2 = [polygon2[j], polygon2[j - 1]]
if is_intersection_segment_segment_xy(line1, line2):
return True
return False


def is_intersection_line_line_xy(l1, l2, tol=1e-6):
"""Verifies if two lines intersect on the XY-plane.

Expand Down
55 changes: 54 additions & 1 deletion tests/compas/geometry/predicates/test_predicates_2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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
from compas.geometry.predicates.predicates_2 import polygon_to_polygon_relationship_xy, point_to_polygon_relationship_xy, is_intersection_polygon_polygon_xy


def test_is_point_in_circle_xy():
Expand All @@ -24,3 +26,54 @@ 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) is True

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

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

# 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 is_polygon_in_polygon_xy(polygon_contour, polygon_intersecting_shifted) is False


def test_polygon_to_polygon_relationship_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)])
polygon_intersecting = Polygon([(10, 10, 0), (10, 5, 0), (15, 5, 0), (15, 10, 0)])
polygon_outside = Polygon([(15, 5, 0), (20, 5, 0), (20, 10, 0), (15, 10, 0)])
polygon_containing = Polygon([(-5, -5, 0), (15, -5, 0), (15, 15, 0), (-5, 15, 0)])

assert polygon_to_polygon_relationship_xy(polygon_inside, polygon_contour) == 1
assert polygon_to_polygon_relationship_xy(polygon_intersecting, polygon_contour) == 0
assert polygon_to_polygon_relationship_xy(polygon_outside, polygon_contour) == -1
assert polygon_to_polygon_relationship_xy(polygon_containing, polygon_contour) == -2


def test_point_to_polygon_relationship_xy():
polygon = Polygon([(0, 0, 0), (4, 0, 0), (4, 4, 0), (0, 4, 0)])
pt_inside = Point(1, 2, 0)
pt_boundary = Point(2, 0, 0)
pt_outside = Point(5, 5, 0)

assert point_to_polygon_relationship_xy(pt_inside, polygon) == 1
assert point_to_polygon_relationship_xy(pt_boundary, polygon) == 0
assert point_to_polygon_relationship_xy(pt_outside, polygon) == -1


def test_is_intersection_polygon_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)])
polygon_intersecting = Polygon([(10, 10, 0), (10, 5, 0), (15, 5, 0), (15, 10, 0)])
polygon_outside = Polygon([(15, 5, 0), (20, 5, 0), (20, 10, 0), (15, 10, 0)])

assert is_intersection_polygon_polygon_xy(polygon_inside, polygon_contour) is False
assert is_intersection_polygon_polygon_xy(polygon_intersecting, polygon_contour) is True
assert is_intersection_polygon_polygon_xy(polygon_outside, polygon_contour) is False