96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
from ..utils.pack2d import maxrects_bssf, guillotine_bssf_sas, pack_patterns
|
|
from ..library import Library
|
|
from ..pattern import Pattern
|
|
|
|
|
|
def test_maxrects_bssf_simple() -> None:
|
|
# Pack two 10x10 squares into one 20x10 container
|
|
rects = [[10, 10], [10, 10]]
|
|
containers = [[0, 0, 20, 10]]
|
|
|
|
locs, rejects = maxrects_bssf(rects, containers)
|
|
|
|
assert not rejects
|
|
# They should be at (0,0) and (10,0)
|
|
assert {tuple(loc) for loc in locs} == {(0.0, 0.0), (10.0, 0.0)}
|
|
|
|
|
|
def test_maxrects_bssf_reject() -> None:
|
|
# Try to pack a too-large rectangle
|
|
rects = [[10, 10], [30, 30]]
|
|
containers = [[0, 0, 20, 20]]
|
|
|
|
locs, rejects = maxrects_bssf(rects, containers, allow_rejects=True)
|
|
assert 1 in rejects # Second rect rejected
|
|
assert 0 not in rejects
|
|
|
|
|
|
def test_maxrects_bssf_exact_fill_rejects_remaining() -> None:
|
|
rects = [[20, 20], [1, 1]]
|
|
containers = [[0, 0, 20, 20]]
|
|
|
|
locs, rejects = maxrects_bssf(rects, containers, presort=False, allow_rejects=True)
|
|
|
|
assert tuple(locs[0]) == (0.0, 0.0)
|
|
assert rejects == {1}
|
|
|
|
|
|
def test_maxrects_bssf_presort_reject_mapping() -> None:
|
|
rects = [[10, 12], [19, 14], [13, 11]]
|
|
containers = [[0, 0, 20, 20]]
|
|
|
|
_locs, rejects = maxrects_bssf(rects, containers, presort=True, allow_rejects=True)
|
|
|
|
assert rejects == {0, 2}
|
|
|
|
|
|
def test_guillotine_bssf_sas_presort_reject_mapping() -> None:
|
|
rects = [[2, 1], [17, 15], [16, 11]]
|
|
containers = [[0, 0, 20, 20]]
|
|
|
|
_locs, rejects = guillotine_bssf_sas(rects, containers, presort=True, allow_rejects=True)
|
|
|
|
assert rejects == {2}
|
|
|
|
|
|
def test_pack_patterns() -> None:
|
|
lib = Library()
|
|
p1 = Pattern()
|
|
p1.polygon((1, 0), vertices=[[0, 0], [10, 0], [10, 10], [0, 10]])
|
|
lib["p1"] = p1
|
|
|
|
p2 = Pattern()
|
|
p2.polygon((1, 0), vertices=[[0, 0], [5, 0], [5, 5], [0, 5]])
|
|
lib["p2"] = p2
|
|
|
|
# Containers: one 20x20
|
|
containers = [[0, 0, 20, 20]]
|
|
# 2um spacing
|
|
pat, rejects = pack_patterns(lib, ["p1", "p2"], containers, spacing=(2, 2))
|
|
|
|
assert not rejects
|
|
assert len(pat.refs) == 2
|
|
assert "p1" in pat.refs
|
|
assert "p2" in pat.refs
|
|
|
|
# Check that they don't overlap (simple check via bounds)
|
|
# p1 size 10x10, effectively 12x12
|
|
# p2 size 5x5, effectively 7x7
|
|
# Both should fit in 20x20
|
|
|
|
|
|
def test_pack_patterns_reject_names_match_original_patterns() -> None:
|
|
lib = Library()
|
|
for name, (lx, ly) in {
|
|
"p0": (10, 12),
|
|
"p1": (19, 14),
|
|
"p2": (13, 11),
|
|
}.items():
|
|
pat = Pattern()
|
|
pat.rect((1, 0), xmin=0, xmax=lx, ymin=0, ymax=ly)
|
|
lib[name] = pat
|
|
|
|
pat, rejects = pack_patterns(lib, ["p0", "p1", "p2"], [[0, 0, 20, 20]], spacing=(0, 0))
|
|
|
|
assert set(rejects) == {"p0", "p2"}
|
|
assert set(pat.refs) == {"p1"}
|