masque/masque/ref.py

184 lines
6.0 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.
"""
2023-07-17 21:28:42 -07:00
#TODO more top-level documentation for ref
2016-03-15 19:12:39 -07:00
from typing import Mapping, TYPE_CHECKING, 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 .utils import 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.
Note: Order is (mirror, rotate, scale, translate, repeat)
2016-03-15 19:12:39 -07:00
"""
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: bool
""" Whether to mirror the instance across the x axis (new_y = -old_y)ubefore rotating. """
# Mirrored property
@property
def mirrored(self) -> bool: # mypy#3004, setter should be SupportsBool
return self._mirrored
@mirrored.setter
def mirrored(self, val: bool) -> None:
self._mirrored = bool(val)
def __init__(
self,
*,
offset: ArrayLike = (0.0, 0.0),
rotation: float = 0.0,
mirrored: bool = False,
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
"""
Note: Order is (mirror, rotate, scale, translate, repeat)
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 axis before rotating.
2020-05-17 22:59:54 -07:00
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
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,
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
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 self.mirrored:
pattern.mirror()
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
def mirror(self, axis: int = 0) -> Self:
self.mirror_target(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 mirror_target(self, axis: int = 0) -> Self:
self.mirrored = not self.mirrored
self.rotation += axis * pi
return self
def mirror2d_target(self, across_x: bool = False, across_y: bool = False) -> Self:
self.mirrored = bool((self.mirrored + across_x + across_y) % 2)
if across_y:
self.rotation += pi
return self
def get_bounds_single(
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 __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' if self.mirrored else ''
2023-04-12 13:56:50 -07:00
return f'<Ref {self.offset}{rotation}{scale}{mirrored}>'