[pattern] speed up visualize()

This commit is contained in:
jan 2026-03-07 23:57:12 -08:00
commit 338c123fb1
2 changed files with 188 additions and 49 deletions

View file

@ -0,0 +1,55 @@
import numpy as np
import pytest
from masque.pattern import Pattern
from masque.ports import Port
from masque.repetition import Grid
try:
import matplotlib
HAS_MATPLOTLIB = True
except ImportError:
HAS_MATPLOTLIB = False
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_visualize_noninteractive(tmp_path) -> None:
"""
Test that visualize() runs and saves a file without error.
This covers the recursive transformation and collection logic.
"""
# Create a hierarchy
child = Pattern()
child.polygon('L1', [[0, 0], [1, 0], [1, 1], [0, 1]])
child.ports['P1'] = Port((0.5, 0.5), 0)
parent = Pattern()
# Add some refs with various transforms
parent.ref('child', offset=(10, 0), rotation=np.pi/4, mirrored=True, scale=2.0)
# Add a repetition
rep = Grid(a_vector=(5, 5), a_count=2)
parent.ref('child', offset=(0, 10), repetition=rep)
library = {'child': child}
output_file = tmp_path / "test_plot.png"
# Run visualize with filename to avoid showing window
parent.visualize(library=library, filename=str(output_file), ports=True)
assert output_file.exists()
assert output_file.stat().st_size > 0
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_visualize_empty() -> None:
""" Test visualizing an empty pattern. """
pat = Pattern()
# Should not raise
pat.visualize(overdraw=True)
@pytest.mark.skipif(not HAS_MATPLOTLIB, reason="matplotlib not installed")
def test_visualize_no_refs() -> None:
""" Test visualizing a pattern with only local shapes (no library needed). """
pat = Pattern()
pat.polygon('L1', [[0, 0], [1, 0], [0, 1]])
# Should not raise even if library is None
pat.visualize(overdraw=True)