[RectCollection] add a RectCollection shape
This commit is contained in:
parent
8b0eb2e83c
commit
c0f54fe585
5 changed files with 343 additions and 1 deletions
70
masque/test/test_rect_collection.py
Normal file
70
masque/test/test_rect_collection.py
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import copy
|
||||
|
||||
import numpy
|
||||
import pytest
|
||||
from numpy.testing import assert_allclose, assert_equal
|
||||
|
||||
from ..error import PatternError
|
||||
from ..shapes import Polygon, RectCollection
|
||||
|
||||
|
||||
def test_rect_collection_init_and_to_polygons() -> None:
|
||||
rects = RectCollection([[10, 10, 12, 12], [0, 0, 5, 5]])
|
||||
assert_equal(rects.rects, [[0, 0, 5, 5], [10, 10, 12, 12]])
|
||||
|
||||
polys = rects.to_polygons()
|
||||
assert len(polys) == 2
|
||||
assert all(isinstance(poly, Polygon) for poly in polys)
|
||||
assert_equal(polys[0].vertices, [[0, 0], [0, 5], [5, 5], [5, 0]])
|
||||
|
||||
|
||||
def test_rect_collection_rejects_invalid_rects() -> None:
|
||||
with pytest.raises(PatternError):
|
||||
RectCollection([[0, 0, 1]])
|
||||
with pytest.raises(PatternError):
|
||||
RectCollection([[5, 0, 1, 2]])
|
||||
with pytest.raises(PatternError):
|
||||
RectCollection([[0, 5, 1, 2]])
|
||||
|
||||
|
||||
def test_rect_collection_raw_constructor_matches_public() -> None:
|
||||
raw = RectCollection._from_raw(
|
||||
rects=numpy.array([[10.0, 10.0, 12.0, 12.0], [0.0, 0.0, 5.0, 5.0]]),
|
||||
annotations={'1': ['rects']},
|
||||
)
|
||||
public = RectCollection(
|
||||
[[0, 0, 5, 5], [10, 10, 12, 12]],
|
||||
annotations={'1': ['rects']},
|
||||
)
|
||||
assert raw == public
|
||||
assert_equal(raw.get_bounds_single(), [[0, 0], [12, 12]])
|
||||
|
||||
|
||||
def test_rect_collection_manhattan_transforms() -> None:
|
||||
rects = RectCollection([[0, 0, 2, 4], [10, 20, 12, 22]])
|
||||
|
||||
mirrored = copy.deepcopy(rects).mirror(1)
|
||||
assert_equal(mirrored.rects, [[-2, 0, 0, 4], [-12, 20, -10, 22]])
|
||||
|
||||
scaled = copy.deepcopy(rects).scale_by(-2)
|
||||
assert_equal(scaled.rects, [[-4, -8, 0, 0], [-24, -44, -20, -40]])
|
||||
|
||||
rotated = copy.deepcopy(rects).rotate(numpy.pi / 2)
|
||||
assert_equal(rotated.rects, [[-4, 0, 0, 2], [-22, 10, -20, 12]])
|
||||
|
||||
|
||||
def test_rect_collection_non_manhattan_rotation_raises() -> None:
|
||||
rects = RectCollection([[0, 0, 2, 4]])
|
||||
with pytest.raises(PatternError, match='Manhattan rotations'):
|
||||
rects.rotate(numpy.pi / 4)
|
||||
|
||||
|
||||
def test_rect_collection_normalized_form_rebuild_is_independent() -> None:
|
||||
rects = RectCollection([[0, 0, 2, 4], [10, 20, 12, 22]])
|
||||
_intrinsic, extrinsic, rebuild = rects.normalized_form(2)
|
||||
|
||||
clone = rebuild()
|
||||
clone.rects[:] = [[1, 1, 2, 2], [3, 3, 4, 4]]
|
||||
|
||||
assert_allclose(extrinsic[0], [6, 11.5])
|
||||
assert_equal(rects.rects, [[0, 0, 2, 4], [10, 20, 12, 22]])
|
||||
Loading…
Add table
Add a link
Reference in a new issue