89 lines
3 KiB
Python
89 lines
3 KiB
Python
import pytest
|
|
from numpy.testing import assert_equal
|
|
|
|
from ..error import PatternError
|
|
from ..shapes import Circle, Ellipse, Polygon, PolyCollection
|
|
|
|
|
|
def test_poly_collection_init() -> None:
|
|
verts = [[0, 0], [1, 0], [1, 1], [0, 1], [10, 10], [11, 10], [11, 11], [10, 11]]
|
|
offsets = [0, 4]
|
|
pc = PolyCollection(vertex_lists=verts, vertex_offsets=offsets)
|
|
assert len(list(pc.polygon_vertices)) == 2
|
|
assert_equal(pc.get_bounds_single(), [[0, 0], [11, 11]])
|
|
|
|
def test_poly_collection_to_polygons() -> None:
|
|
verts = [[0, 0], [1, 0], [1, 1], [0, 1], [10, 10], [11, 10], [11, 11], [10, 11]]
|
|
offsets = [0, 4]
|
|
pc = PolyCollection(vertex_lists=verts, vertex_offsets=offsets)
|
|
polys = pc.to_polygons()
|
|
assert len(polys) == 2
|
|
assert_equal(polys[0].vertices, [[0, 0], [1, 0], [1, 1], [0, 1]])
|
|
assert_equal(polys[1].vertices, [[10, 10], [11, 10], [11, 11], [10, 11]])
|
|
|
|
def test_poly_collection_holes() -> None:
|
|
# PolyCollection represents separate polygon boundaries, including nested boundaries.
|
|
verts = [
|
|
[0, 0],
|
|
[10, 0],
|
|
[10, 10],
|
|
[0, 10], # Poly 1
|
|
[2, 2],
|
|
[2, 8],
|
|
[8, 8],
|
|
[8, 2], # Poly 2
|
|
]
|
|
offsets = [0, 4]
|
|
pc = PolyCollection(verts, offsets)
|
|
polys = pc.to_polygons()
|
|
assert len(polys) == 2
|
|
assert_equal(polys[0].vertices, [[0, 0], [10, 0], [10, 10], [0, 10]])
|
|
assert_equal(polys[1].vertices, [[2, 2], [2, 8], [8, 8], [8, 2]])
|
|
|
|
def test_poly_collection_constituent_empty() -> None:
|
|
# Duplicate offsets create an empty constituent slice between valid polygons.
|
|
verts = [
|
|
[0, 0],
|
|
[1, 0],
|
|
[0, 1], # Tri
|
|
[10, 10],
|
|
[11, 10],
|
|
[11, 11],
|
|
[10, 11], # Square
|
|
]
|
|
offsets = [0, 3, 3]
|
|
pc = PolyCollection(verts, offsets)
|
|
with pytest.raises(PatternError):
|
|
pc.to_polygons()
|
|
|
|
def test_poly_collection_valid() -> None:
|
|
verts = [[0, 0], [1, 0], [0, 1], [10, 10], [11, 10], [11, 11], [10, 11]]
|
|
offsets = [0, 3]
|
|
pc = PolyCollection(verts, offsets)
|
|
assert len(pc.to_polygons()) == 2
|
|
shapes = [Circle(radius=20), Circle(radius=10), Polygon([[0, 0], [10, 0], [10, 10]]), Ellipse(radii=(5, 5))]
|
|
sorted_shapes = sorted(shapes)
|
|
assert len(sorted_shapes) == 4
|
|
assert sorted(sorted_shapes) == sorted_shapes
|
|
|
|
def test_poly_collection_normalized_form_reconstruction_is_independent() -> None:
|
|
pc = PolyCollection([[0, 0], [1, 0], [0, 1]], [0])
|
|
_intrinsic, _extrinsic, rebuild = pc.normalized_form(1)
|
|
|
|
clone = rebuild()
|
|
clone.vertex_offsets[:] = [5]
|
|
|
|
assert_equal(pc.vertex_offsets, [0])
|
|
assert_equal(clone.vertex_offsets, [5])
|
|
|
|
def test_poly_collection_normalized_form_rebuilds_independent_clones() -> None:
|
|
pc = PolyCollection([[0, 0], [1, 0], [0, 1]], [0])
|
|
_intrinsic, _extrinsic, rebuild = pc.normalized_form(1)
|
|
|
|
first = rebuild()
|
|
second = rebuild()
|
|
first.vertex_offsets[:] = [7]
|
|
|
|
assert_equal(first.vertex_offsets, [7])
|
|
assert_equal(second.vertex_offsets, [0])
|
|
assert_equal(pc.vertex_offsets, [0])
|