masque/masque/ref.py

190 lines
6.4 KiB
Python
Raw Normal View History

2016-03-15 19:12:39 -07:00
"""
2023-01-21 21:22:11 -08:00
Ref provides basic support for nesting Pattern objects within each other, by adding
2016-03-15 19:12:39 -07:00
offset, rotation, scaling, and other such properties to the reference.
"""
2020-05-17 22:59:54 -07:00
#TODO more top-level documentation
2016-03-15 19:12:39 -07:00
2023-04-06 17:03:31 -07:00
from typing import Sequence, Mapping, TYPE_CHECKING, Any, Self
import copy
2016-03-15 19:12:39 -07:00
import numpy
2016-03-15 19:12:39 -07:00
from numpy import pi
from numpy.typing import NDArray, ArrayLike
2016-03-15 19:12:39 -07:00
from .error import PatternError
2023-01-23 22:27:26 -08:00
from .utils import is_scalar, annotations_t
from .repetition import Repetition
from .traits import (
PositionableImpl, RotatableImpl, ScalableImpl,
Mirrorable, PivotableImpl, Copyable, RepeatableImpl, AnnotatableImpl,
)
2016-03-15 19:12:39 -07:00
if TYPE_CHECKING:
from . import Pattern
2016-03-15 19:12:39 -07:00
2023-01-21 21:22:11 -08:00
class Ref(
PositionableImpl, RotatableImpl, ScalableImpl, Mirrorable,
PivotableImpl, Copyable, RepeatableImpl, AnnotatableImpl,
):
2016-03-15 19:12:39 -07:00
"""
2023-01-21 21:22:11 -08:00
`Ref` provides basic support for nesting Pattern objects within each other, by adding
2016-03-15 19:12:39 -07:00
offset, rotation, scaling, and associated methods.
"""
2023-01-21 21:22:11 -08:00
__slots__ = (
2023-04-12 13:56:50 -07:00
'_mirrored',
2023-01-21 21:22:11 -08:00
# inherited
'_offset', '_rotation', 'scale', '_repetition', '_annotations',
)
_mirrored: NDArray[numpy.bool_]
""" Whether to mirror the instance across the x and/or y axes. """
def __init__(
self,
*,
offset: ArrayLike = (0.0, 0.0),
rotation: float = 0.0,
2023-02-23 13:15:32 -08:00
mirrored: Sequence[bool] | None = None,
scale: float = 1.0,
2023-02-23 13:15:32 -08:00
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
) -> None:
2020-05-17 22:59:54 -07:00
"""
Args:
offset: (x, y) offset applied to the referenced pattern. Not affected by rotation etc.
rotation: Rotation (radians, counterclockwise) relative to the referenced pattern's (0, 0).
mirrored: Whether to mirror the referenced pattern across its x and y axes.
scale: Scaling factor applied to the pattern's geometry.
repetition: `Repetition` object, default `None`
2020-05-17 22:59:54 -07:00
"""
2016-03-15 19:12:39 -07:00
self.offset = offset
self.rotation = rotation
self.scale = scale
if mirrored is None:
2020-11-09 22:04:04 -08:00
mirrored = (False, False)
self.mirrored = mirrored
self.repetition = repetition
self.annotations = annotations if annotations is not None else {}
2019-12-12 00:38:11 -08:00
2023-01-21 21:22:11 -08:00
def __copy__(self) -> 'Ref':
new = Ref(
offset=self.offset.copy(),
rotation=self.rotation,
scale=self.scale,
mirrored=self.mirrored.copy(),
repetition=copy.deepcopy(self.repetition),
annotations=copy.deepcopy(self.annotations),
)
return new
2023-02-23 13:15:32 -08:00
def __deepcopy__(self, memo: dict | None = None) -> 'Ref':
memo = {} if memo is None else memo
new = copy.copy(self)
2020-08-12 21:43:46 -07:00
new.repetition = copy.deepcopy(self.repetition, memo)
new.annotations = copy.deepcopy(self.annotations, memo)
return new
# Mirrored property
@property
2023-01-23 22:27:26 -08:00
def mirrored(self) -> Any: # TODO mypy#3004 NDArray[numpy.bool_]:
return self._mirrored
@mirrored.setter
def mirrored(self, val: ArrayLike) -> None:
if is_scalar(val):
raise PatternError('Mirrored must be a 2-element list of booleans')
2020-05-11 18:49:30 -07:00
self._mirrored = numpy.array(val, dtype=bool, copy=True)
def as_pattern(
self,
2023-04-12 13:56:50 -07:00
pattern: 'Pattern',
) -> 'Pattern':
2016-03-15 19:12:39 -07:00
"""
Args:
pattern: Pattern object to transform
Returns:
A copy of the referenced Pattern which has been scaled, rotated, etc.
2023-01-21 21:22:11 -08:00
according to this `Ref`'s properties.
2016-03-15 19:12:39 -07:00
"""
pattern = pattern.deepcopy()
if self.scale != 1:
pattern.scale_by(self.scale)
if numpy.any(self.mirrored):
pattern.mirror2d(self.mirrored)
if self.rotation % (2 * pi) != 0:
pattern.rotate_around((0.0, 0.0), self.rotation)
if numpy.any(self.offset):
pattern.translate_elements(self.offset)
2016-03-15 19:12:39 -07:00
2020-07-22 21:48:34 -07:00
if self.repetition is not None:
combined = type(pattern)()
2020-07-22 21:48:34 -07:00
for dd in self.repetition.displacements:
temp_pat = pattern.deepcopy()
2023-03-31 13:35:18 -07:00
temp_pat.ports = {}
temp_pat.translate_elements(dd)
combined.append(temp_pat)
pattern = combined
return pattern
2016-03-15 19:12:39 -07:00
2023-02-23 13:37:34 -08:00
def rotate(self, rotation: float) -> Self:
2020-08-12 21:43:46 -07:00
self.rotation += rotation
if self.repetition is not None:
self.repetition.rotate(rotation)
return self
2023-02-23 13:37:34 -08:00
def mirror(self, axis: int) -> Self:
self.mirrored[axis] = not self.mirrored[axis]
2019-12-05 23:18:18 -08:00
self.rotation *= -1
2020-08-12 21:43:46 -07:00
if self.repetition is not None:
2020-08-15 17:41:09 -07:00
self.repetition.mirror(axis)
return self
def get_bounds(
self,
2023-04-12 13:56:50 -07:00
pattern: 'Pattern',
*,
2023-02-23 13:15:32 -08:00
library: Mapping[str, 'Pattern'] | None = None,
) -> NDArray[numpy.float64] | None:
2016-03-15 19:12:39 -07:00
"""
Return a `numpy.ndarray` containing `[[x_min, y_min], [x_max, y_max]]`, corresponding to the
2023-01-21 21:22:11 -08:00
extent of the `Ref` in each dimension.
Returns `None` if the contained `Pattern` is empty.
2016-03-15 19:12:39 -07:00
Args:
library: Name-to-Pattern mapping for resul
Returns:
`[[x_min, y_min], [x_max, y_max]]` or `None`
2016-03-15 19:12:39 -07:00
"""
2023-04-12 13:56:50 -07:00
if pattern.is_empty():
2023-02-09 16:38:33 -08:00
# no need to run as_pattern()
return None
2023-04-12 13:56:50 -07:00
return self.as_pattern(pattern=pattern).get_bounds(library) # TODO can just take pattern's bounds and then transform those!
def get_bounds_nonempty(
self,
pattern: 'Pattern',
*,
library: Mapping[str, 'Pattern'] | None = None,
) -> NDArray[numpy.float64]:
"""
Returns `[[x_min, y_min], [x_max, y_max]]` which specify a minimal bounding box for the entity.
Asserts that the entity is non-empty (i.e., `get_bounds()` does not return None).
This is handy for destructuring like `xy_min, xy_max = entity.get_bounds_nonempty()`
"""
bounds = self.get_bounds(pattern, library=library)
assert bounds is not None
return bounds
def __repr__(self) -> str:
2023-04-12 13:56:50 -07:00
rotation = f' r{numpy.rad2deg(self.rotation):g}' if self.rotation != 0 else ''
scale = f' d{self.scale:g}' if self.scale != 1 else ''
mirrored = ' m{:d}{:d}'.format(*self.mirrored) if self.mirrored.any() else ''
2023-04-12 13:56:50 -07:00
return f'<Ref {self.offset}{rotation}{scale}{mirrored}>'