72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
from typing import cast, TYPE_CHECKING
|
|
from numpy.testing import assert_equal, assert_allclose
|
|
from numpy import pi
|
|
|
|
from ..pattern import Pattern
|
|
from ..ref import Ref
|
|
from ..repetition import Grid
|
|
|
|
if TYPE_CHECKING:
|
|
from ..shapes import Polygon
|
|
|
|
|
|
def test_ref_init() -> None:
|
|
ref = Ref(offset=(10, 20), rotation=pi / 4, mirrored=True, scale=2.0)
|
|
assert_equal(ref.offset, [10, 20])
|
|
assert ref.rotation == pi / 4
|
|
assert ref.mirrored is True
|
|
assert ref.scale == 2.0
|
|
|
|
|
|
def test_ref_as_pattern() -> None:
|
|
sub_pat = Pattern()
|
|
sub_pat.polygon((1, 0), vertices=[[0, 0], [1, 0], [0, 1]])
|
|
|
|
ref = Ref(offset=(10, 10), rotation=pi / 2, scale=2.0)
|
|
transformed_pat = ref.as_pattern(sub_pat)
|
|
|
|
# Check transformed shape
|
|
shape = cast("Polygon", transformed_pat.shapes[(1, 0)][0])
|
|
# ref.as_pattern deepcopies sub_pat then applies transformations:
|
|
# 1. pattern.scale_by(2) -> vertices [[0,0], [2,0], [0,2]]
|
|
# 2. pattern.rotate_around((0,0), pi/2) -> vertices [[0,0], [0,2], [-2,0]]
|
|
# 3. pattern.translate_elements((10,10)) -> vertices [[10,10], [10,12], [8,10]]
|
|
|
|
assert_allclose(shape.vertices, [[10, 10], [10, 12], [8, 10]], atol=1e-10)
|
|
|
|
|
|
def test_ref_with_repetition() -> None:
|
|
sub_pat = Pattern()
|
|
sub_pat.polygon((1, 0), vertices=[[0, 0], [1, 0], [0, 1]])
|
|
|
|
rep = Grid(a_vector=(10, 0), b_vector=(0, 10), a_count=2, b_count=2)
|
|
ref = Ref(repetition=rep)
|
|
|
|
repeated_pat = ref.as_pattern(sub_pat)
|
|
# Should have 4 shapes
|
|
assert len(repeated_pat.shapes[(1, 0)]) == 4
|
|
|
|
first_verts = sorted([tuple(cast("Polygon", s).vertices[0]) for s in repeated_pat.shapes[(1, 0)]])
|
|
assert first_verts == [(0.0, 0.0), (0.0, 10.0), (10.0, 0.0), (10.0, 10.0)]
|
|
|
|
|
|
def test_ref_get_bounds() -> None:
|
|
sub_pat = Pattern()
|
|
sub_pat.polygon((1, 0), vertices=[[0, 0], [5, 0], [0, 5]])
|
|
|
|
ref = Ref(offset=(10, 10), scale=2.0)
|
|
bounds = ref.get_bounds_single(sub_pat)
|
|
# sub_pat bounds [[0,0], [5,5]]
|
|
# scaled [[0,0], [10,10]]
|
|
# translated [[10,10], [20,20]]
|
|
assert_equal(bounds, [[10, 10], [20, 20]])
|
|
|
|
|
|
def test_ref_copy() -> None:
|
|
ref1 = Ref(offset=(1, 2), rotation=0.5, annotations={"a": [1]})
|
|
ref2 = ref1.copy()
|
|
assert ref1 == ref2
|
|
assert ref1 is not ref2
|
|
|
|
ref2.offset[0] = 100
|
|
assert ref1.offset[0] == 1
|