2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
Repetitions provides support for efficiently nesting multiple identical
|
|
|
|
instances of a Pattern in the same parent Pattern.
|
|
|
|
"""
|
|
|
|
|
2020-05-17 14:12:27 -07:00
|
|
|
from typing import Union, List, Dict, Tuple, Optional, Sequence, TYPE_CHECKING, Any
|
2019-03-31 20:57:10 -07:00
|
|
|
import copy
|
|
|
|
|
|
|
|
import numpy
|
|
|
|
from numpy import pi
|
|
|
|
|
2019-12-12 00:38:11 -08:00
|
|
|
from .error import PatternError, PatternLockedError
|
2019-03-31 20:57:10 -07:00
|
|
|
from .utils import is_scalar, rotation_matrix_2d, vector2
|
|
|
|
|
2020-05-11 19:09:35 -07:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from . import Pattern
|
2019-03-31 20:57:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
# TODO need top-level comment about what order rotation/scale/offset/mirror/array are applied
|
|
|
|
|
|
|
|
class GridRepetition:
|
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
GridRepetition provides support for efficiently embedding multiple copies of a `Pattern`
|
|
|
|
into another `Pattern` at regularly-spaced offsets.
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
2020-05-11 18:52:17 -07:00
|
|
|
__slots__ = ('_pattern',
|
2019-05-17 00:37:56 -07:00
|
|
|
'_offset',
|
|
|
|
'_rotation',
|
|
|
|
'_dose',
|
|
|
|
'_scale',
|
|
|
|
'_mirrored',
|
|
|
|
'_a_vector',
|
|
|
|
'_b_vector',
|
2019-12-06 22:28:11 -08:00
|
|
|
'_a_count',
|
|
|
|
'_b_count',
|
2019-12-12 00:38:11 -08:00
|
|
|
'identifier',
|
|
|
|
'locked')
|
2019-05-17 00:37:56 -07:00
|
|
|
|
2020-05-11 18:52:17 -07:00
|
|
|
_pattern: Optional['Pattern']
|
2020-02-17 21:02:53 -08:00
|
|
|
""" The `Pattern` being instanced """
|
2019-05-17 00:37:56 -07:00
|
|
|
|
|
|
|
_offset: numpy.ndarray
|
2020-02-17 21:02:53 -08:00
|
|
|
""" (x, y) offset for the base instance """
|
|
|
|
|
2019-05-17 00:37:56 -07:00
|
|
|
_dose: float
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Dose factor """
|
2020-02-07 23:01:14 -08:00
|
|
|
|
|
|
|
_rotation: float
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Rotation of the individual instances in the grid (not the grid vectors).
|
|
|
|
Radians, counterclockwise.
|
|
|
|
"""
|
|
|
|
|
2019-05-17 00:37:56 -07:00
|
|
|
_scale: float
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Scaling factor applied to individual instances in the grid (not the grid vectors) """
|
|
|
|
|
2020-05-11 19:09:35 -07:00
|
|
|
_mirrored: numpy.ndarray # ndarray[bool]
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Whether to mirror individual instances across the x and y axes
|
|
|
|
(Applies to individual instances in the grid, not the grid vectors)
|
|
|
|
"""
|
2019-05-17 00:37:56 -07:00
|
|
|
|
|
|
|
_a_vector: numpy.ndarray
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Vector `[x, y]` specifying the first lattice vector of the grid.
|
|
|
|
Specifies center-to-center spacing between adjacent elements.
|
|
|
|
"""
|
|
|
|
|
2019-12-06 22:28:11 -08:00
|
|
|
_a_count: int
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Number of instances along the direction specified by the `a_vector` """
|
|
|
|
|
2020-05-11 19:09:35 -07:00
|
|
|
_b_vector: Optional[numpy.ndarray]
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Vector `[x, y]` specifying a second lattice vector for the grid.
|
|
|
|
Specifies center-to-center spacing between adjacent elements.
|
|
|
|
Can be `None` for a 1D array.
|
|
|
|
"""
|
|
|
|
|
2019-12-06 22:28:11 -08:00
|
|
|
_b_count: int
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Number of instances along the direction specified by the `b_vector` """
|
2019-03-31 20:57:10 -07:00
|
|
|
|
2020-05-17 14:12:27 -07:00
|
|
|
identifier: Tuple[Any, ...]
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Arbitrary identifier """
|
|
|
|
|
2019-12-12 00:38:11 -08:00
|
|
|
locked: bool
|
2020-02-17 21:02:53 -08:00
|
|
|
""" If `True`, disallows changes to the GridRepetition """
|
2019-05-17 00:34:01 -07:00
|
|
|
|
2019-03-31 20:57:10 -07:00
|
|
|
def __init__(self,
|
2020-05-11 19:09:35 -07:00
|
|
|
pattern: Optional['Pattern'],
|
2019-03-31 20:57:10 -07:00
|
|
|
a_vector: numpy.ndarray,
|
|
|
|
a_count: int,
|
2020-05-11 19:09:35 -07:00
|
|
|
b_vector: Optional[numpy.ndarray] = None,
|
2019-03-31 20:57:10 -07:00
|
|
|
b_count: int = 1,
|
|
|
|
offset: vector2 = (0.0, 0.0),
|
|
|
|
rotation: float = 0.0,
|
2020-05-11 19:09:35 -07:00
|
|
|
mirrored: Optional[Sequence[bool]] = None,
|
2019-03-31 20:57:10 -07:00
|
|
|
dose: float = 1.0,
|
2019-12-12 00:38:11 -08:00
|
|
|
scale: float = 1.0,
|
2020-05-17 14:12:27 -07:00
|
|
|
locked: bool = False,
|
|
|
|
identifier: Tuple[Any, ...] = ()):
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
a_vector: First lattice vector, of the form `[x, y]`.
|
|
|
|
Specifies center-to-center spacing between adjacent elements.
|
|
|
|
a_count: Number of elements in the a_vector direction.
|
|
|
|
b_vector: Second lattice vector, of the form `[x, y]`.
|
|
|
|
Specifies center-to-center spacing between adjacent elements.
|
|
|
|
Can be omitted when specifying a 1D array.
|
|
|
|
b_count: Number of elements in the `b_vector` direction.
|
|
|
|
Should be omitted if `b_vector` was omitted.
|
|
|
|
locked: Whether the `GridRepetition` is locked after initialization.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
PatternError if `b_*` inputs conflict with each other
|
|
|
|
or `a_count < 1`.
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
if b_vector is None:
|
|
|
|
if b_count > 1:
|
|
|
|
raise PatternError('Repetition has b_count > 1 but no b_vector')
|
|
|
|
else:
|
|
|
|
b_vector = numpy.array([0.0, 0.0])
|
|
|
|
|
|
|
|
if a_count < 1:
|
2020-02-16 18:17:28 -08:00
|
|
|
raise PatternError('Repetition has too-small a_count: '
|
|
|
|
'{}'.format(a_count))
|
2019-03-31 20:57:10 -07:00
|
|
|
if b_count < 1:
|
2020-02-16 18:17:28 -08:00
|
|
|
raise PatternError('Repetition has too-small b_count: '
|
|
|
|
'{}'.format(b_count))
|
2020-05-11 19:29:00 -07:00
|
|
|
|
|
|
|
object.__setattr__(self, 'locked', False)
|
2019-03-31 20:57:10 -07:00
|
|
|
self.a_vector = a_vector
|
|
|
|
self.b_vector = b_vector
|
|
|
|
self.a_count = a_count
|
|
|
|
self.b_count = b_count
|
|
|
|
|
2020-05-17 14:12:27 -07:00
|
|
|
self.identifier = identifier
|
2019-03-31 20:57:10 -07:00
|
|
|
self.pattern = pattern
|
|
|
|
self.offset = offset
|
|
|
|
self.rotation = rotation
|
|
|
|
self.dose = dose
|
|
|
|
self.scale = scale
|
|
|
|
if mirrored is None:
|
|
|
|
mirrored = [False, False]
|
|
|
|
self.mirrored = mirrored
|
2019-12-12 00:38:11 -08:00
|
|
|
self.locked = locked
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
if self.locked and name != 'locked':
|
|
|
|
raise PatternLockedError()
|
|
|
|
object.__setattr__(self, name, value)
|
2019-03-31 20:57:10 -07:00
|
|
|
|
2019-12-04 23:34:57 -08:00
|
|
|
def __copy__(self) -> 'GridRepetition':
|
|
|
|
new = GridRepetition(pattern=self.pattern,
|
|
|
|
a_vector=self.a_vector.copy(),
|
|
|
|
b_vector=copy.copy(self.b_vector),
|
|
|
|
a_count=self.a_count,
|
|
|
|
b_count=self.b_count,
|
|
|
|
offset=self.offset.copy(),
|
|
|
|
rotation=self.rotation,
|
|
|
|
dose=self.dose,
|
|
|
|
scale=self.scale,
|
2019-12-12 00:38:11 -08:00
|
|
|
mirrored=self.mirrored.copy(),
|
|
|
|
locked=self.locked)
|
2019-12-04 23:34:57 -08:00
|
|
|
return new
|
|
|
|
|
2020-05-11 19:09:35 -07:00
|
|
|
def __deepcopy__(self, memo: Dict = None) -> 'GridRepetition':
|
2019-05-15 00:19:37 -07:00
|
|
|
memo = {} if memo is None else memo
|
2019-12-13 01:25:38 -08:00
|
|
|
new = copy.copy(self).unlock()
|
2019-05-15 00:19:37 -07:00
|
|
|
new.pattern = copy.deepcopy(self.pattern, memo)
|
2019-12-13 01:25:38 -08:00
|
|
|
new.locked = self.locked
|
2019-05-15 00:19:37 -07:00
|
|
|
return new
|
|
|
|
|
2020-05-11 18:52:17 -07:00
|
|
|
# pattern property
|
|
|
|
@property
|
|
|
|
def pattern(self) -> Optional['Pattern']:
|
|
|
|
return self._pattern
|
|
|
|
|
|
|
|
@pattern.setter
|
|
|
|
def pattern(self, val: Optional['Pattern']):
|
|
|
|
from .pattern import Pattern
|
|
|
|
if val is not None and not isinstance(val, Pattern):
|
|
|
|
raise PatternError('Provided pattern {} is not a Pattern object or None!'.format(val))
|
|
|
|
self._pattern = val
|
|
|
|
|
2019-03-31 20:57:10 -07:00
|
|
|
# offset property
|
|
|
|
@property
|
|
|
|
def offset(self) -> numpy.ndarray:
|
|
|
|
return self._offset
|
|
|
|
|
|
|
|
@offset.setter
|
|
|
|
def offset(self, val: vector2):
|
2019-12-12 00:38:11 -08:00
|
|
|
if self.locked:
|
|
|
|
raise PatternLockedError()
|
|
|
|
|
2019-03-31 20:57:10 -07:00
|
|
|
if not isinstance(val, numpy.ndarray):
|
|
|
|
val = numpy.array(val, dtype=float)
|
|
|
|
|
|
|
|
if val.size != 2:
|
|
|
|
raise PatternError('Offset must be convertible to size-2 ndarray')
|
2019-04-18 01:12:51 -07:00
|
|
|
self._offset = val.flatten().astype(float)
|
2019-03-31 20:57:10 -07:00
|
|
|
|
|
|
|
# dose property
|
|
|
|
@property
|
|
|
|
def dose(self) -> float:
|
|
|
|
return self._dose
|
|
|
|
|
|
|
|
@dose.setter
|
|
|
|
def dose(self, val: float):
|
|
|
|
if not is_scalar(val):
|
|
|
|
raise PatternError('Dose must be a scalar')
|
|
|
|
if not val >= 0:
|
|
|
|
raise PatternError('Dose must be non-negative')
|
|
|
|
self._dose = val
|
|
|
|
|
|
|
|
# scale property
|
|
|
|
@property
|
|
|
|
def scale(self) -> float:
|
|
|
|
return self._scale
|
|
|
|
|
|
|
|
@scale.setter
|
|
|
|
def scale(self, val: float):
|
|
|
|
if not is_scalar(val):
|
|
|
|
raise PatternError('Scale must be a scalar')
|
|
|
|
if not val > 0:
|
|
|
|
raise PatternError('Scale must be positive')
|
|
|
|
self._scale = val
|
|
|
|
|
|
|
|
# Rotation property [ccw]
|
|
|
|
@property
|
|
|
|
def rotation(self) -> float:
|
|
|
|
return self._rotation
|
|
|
|
|
|
|
|
@rotation.setter
|
|
|
|
def rotation(self, val: float):
|
|
|
|
if not is_scalar(val):
|
|
|
|
raise PatternError('Rotation must be a scalar')
|
|
|
|
self._rotation = val % (2 * pi)
|
|
|
|
|
|
|
|
# Mirrored property
|
|
|
|
@property
|
2020-05-11 19:09:35 -07:00
|
|
|
def mirrored(self) -> numpy.ndarray: # ndarray[bool]
|
2019-03-31 20:57:10 -07:00
|
|
|
return self._mirrored
|
|
|
|
|
|
|
|
@mirrored.setter
|
2020-05-11 19:09:35 -07:00
|
|
|
def mirrored(self, val: Sequence[bool]):
|
2019-03-31 20:57:10 -07:00
|
|
|
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)
|
2019-03-31 20:57:10 -07:00
|
|
|
|
|
|
|
# a_vector property
|
|
|
|
@property
|
|
|
|
def a_vector(self) -> numpy.ndarray:
|
|
|
|
return self._a_vector
|
|
|
|
|
|
|
|
@a_vector.setter
|
|
|
|
def a_vector(self, val: vector2):
|
|
|
|
if not isinstance(val, numpy.ndarray):
|
|
|
|
val = numpy.array(val, dtype=float)
|
|
|
|
|
|
|
|
if val.size != 2:
|
|
|
|
raise PatternError('a_vector must be convertible to size-2 ndarray')
|
2020-05-11 18:49:30 -07:00
|
|
|
self._a_vector = val.flatten().astype(float)
|
2019-03-31 20:57:10 -07:00
|
|
|
|
|
|
|
# b_vector property
|
|
|
|
@property
|
|
|
|
def b_vector(self) -> numpy.ndarray:
|
|
|
|
return self._b_vector
|
|
|
|
|
|
|
|
@b_vector.setter
|
|
|
|
def b_vector(self, val: vector2):
|
|
|
|
if not isinstance(val, numpy.ndarray):
|
2020-05-11 18:49:30 -07:00
|
|
|
val = numpy.array(val, dtype=float, copy=True)
|
2019-03-31 20:57:10 -07:00
|
|
|
|
|
|
|
if val.size != 2:
|
|
|
|
raise PatternError('b_vector must be convertible to size-2 ndarray')
|
|
|
|
self._b_vector = val.flatten()
|
|
|
|
|
2019-12-06 22:28:11 -08:00
|
|
|
# a_count property
|
|
|
|
@property
|
|
|
|
def a_count(self) -> int:
|
|
|
|
return self._a_count
|
|
|
|
|
|
|
|
@a_count.setter
|
|
|
|
def a_count(self, val: int):
|
|
|
|
if val != int(val):
|
|
|
|
raise PatternError('a_count must be convertable to an int!')
|
|
|
|
self._a_count = int(val)
|
|
|
|
|
|
|
|
# b_count property
|
|
|
|
@property
|
|
|
|
def b_count(self) -> int:
|
|
|
|
return self._b_count
|
|
|
|
|
|
|
|
@b_count.setter
|
|
|
|
def b_count(self, val: int):
|
|
|
|
if val != int(val):
|
|
|
|
raise PatternError('b_count must be convertable to an int!')
|
|
|
|
self._b_count = int(val)
|
2019-03-31 20:57:10 -07:00
|
|
|
|
|
|
|
def as_pattern(self) -> 'Pattern':
|
|
|
|
"""
|
2019-05-15 00:12:34 -07:00
|
|
|
Returns a copy of self.pattern which has been scaled, rotated, repeated, etc.
|
2020-02-17 21:02:53 -08:00
|
|
|
etc. according to this `GridRepetition`'s properties.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A copy of self.pattern which has been scaled, rotated, repeated, etc.
|
|
|
|
etc. according to this `GridRepetition`'s properties.
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
2020-05-11 18:58:57 -07:00
|
|
|
assert(self.pattern is not None)
|
2019-03-31 20:57:10 -07:00
|
|
|
patterns = []
|
|
|
|
|
|
|
|
for a in range(self.a_count):
|
|
|
|
for b in range(self.b_count):
|
|
|
|
offset = a * self.a_vector + b * self.b_vector
|
2019-12-12 00:38:11 -08:00
|
|
|
newPat = self.pattern.deepcopy().deepunlock()
|
2019-03-31 20:57:10 -07:00
|
|
|
newPat.translate_elements(offset)
|
|
|
|
patterns.append(newPat)
|
|
|
|
|
|
|
|
combined = patterns[0]
|
|
|
|
for p in patterns[1:]:
|
|
|
|
combined.append(p)
|
|
|
|
|
|
|
|
combined.scale_by(self.scale)
|
|
|
|
[combined.mirror(ax) for ax, do in enumerate(self.mirrored) if do]
|
|
|
|
combined.rotate_around((0.0, 0.0), self.rotation)
|
|
|
|
combined.translate_elements(self.offset)
|
|
|
|
combined.scale_element_doses(self.dose)
|
|
|
|
|
|
|
|
return combined
|
|
|
|
|
|
|
|
def translate(self, offset: vector2) -> 'GridRepetition':
|
|
|
|
"""
|
|
|
|
Translate by the given offset
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
offset: `[x, y]` to translate by
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
self.offset += offset
|
|
|
|
return self
|
|
|
|
|
|
|
|
def rotate_around(self, pivot: vector2, rotation: float) -> 'GridRepetition':
|
|
|
|
"""
|
2020-02-07 23:01:14 -08:00
|
|
|
Rotate the array around a point
|
2019-03-31 20:57:10 -07:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
pivot: Point `[x, y]` to rotate around
|
|
|
|
rotation: Angle to rotate by (counterclockwise, radians)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
pivot = numpy.array(pivot, dtype=float)
|
|
|
|
self.translate(-pivot)
|
|
|
|
self.offset = numpy.dot(rotation_matrix_2d(rotation), self.offset)
|
|
|
|
self.rotate(rotation)
|
|
|
|
self.translate(+pivot)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def rotate(self, rotation: float) -> 'GridRepetition':
|
|
|
|
"""
|
|
|
|
Rotate around (0, 0)
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
rotation: Angle to rotate by (counterclockwise, radians)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2020-02-07 23:01:14 -08:00
|
|
|
"""
|
|
|
|
self.rotate_elements(rotation)
|
|
|
|
self.a_vector = numpy.dot(rotation_matrix_2d(rotation), self.a_vector)
|
|
|
|
if self.b_vector is not None:
|
|
|
|
self.b_vector = numpy.dot(rotation_matrix_2d(rotation), self.b_vector)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def rotate_elements(self, rotation: float) -> 'GridRepetition':
|
|
|
|
"""
|
|
|
|
Rotate each element around its origin
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
rotation: Angle to rotate by (counterclockwise, radians)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
self.rotation += rotation
|
|
|
|
return self
|
|
|
|
|
|
|
|
def mirror(self, axis: int) -> 'GridRepetition':
|
|
|
|
"""
|
2019-05-15 00:12:34 -07:00
|
|
|
Mirror the GridRepetition across an axis.
|
2019-03-31 20:57:10 -07:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
axis: Axis to mirror across.
|
|
|
|
(0: mirror across x-axis, 1: mirror across y-axis)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
2020-02-07 23:01:14 -08:00
|
|
|
self.mirror_elements(axis)
|
2020-02-10 10:09:07 -08:00
|
|
|
self.a_vector[1-axis] *= -1
|
2019-12-05 23:18:18 -08:00
|
|
|
if self.b_vector is not None:
|
2020-02-10 10:09:07 -08:00
|
|
|
self.b_vector[1-axis] *= -1
|
2019-03-31 20:57:10 -07:00
|
|
|
return self
|
|
|
|
|
2020-02-07 23:01:14 -08:00
|
|
|
def mirror_elements(self, axis: int) -> 'GridRepetition':
|
|
|
|
"""
|
|
|
|
Mirror each element across an axis relative to its origin.
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
axis: Axis to mirror across.
|
|
|
|
(0: mirror across x-axis, 1: mirror across y-axis)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2020-02-07 23:01:14 -08:00
|
|
|
"""
|
|
|
|
self.mirrored[axis] = not self.mirrored[axis]
|
|
|
|
self.rotation *= -1
|
|
|
|
return self
|
|
|
|
|
2020-05-11 18:58:57 -07:00
|
|
|
def get_bounds(self) -> Optional[numpy.ndarray]:
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Return a `numpy.ndarray` containing `[[x_min, y_min], [x_max, y_max]]`, corresponding to the
|
|
|
|
extent of the `GridRepetition` in each dimension.
|
|
|
|
Returns `None` if the contained `Pattern` is empty.
|
2019-03-31 20:57:10 -07:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
`[[x_min, y_min], [x_max, y_max]]` or `None`
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
2020-05-11 18:58:57 -07:00
|
|
|
if self.pattern is None:
|
|
|
|
return None
|
2019-03-31 20:57:10 -07:00
|
|
|
return self.as_pattern().get_bounds()
|
|
|
|
|
|
|
|
def scale_by(self, c: float) -> 'GridRepetition':
|
|
|
|
"""
|
2019-05-15 00:12:34 -07:00
|
|
|
Scale the GridRepetition by a factor
|
2019-03-31 20:57:10 -07:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
c: scaling factor
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2020-02-07 23:01:14 -08:00
|
|
|
"""
|
|
|
|
self.scale_elements_by(c)
|
|
|
|
self.a_vector *= c
|
|
|
|
if self.b_vector is not None:
|
|
|
|
self.b_vector *= c
|
|
|
|
return self
|
|
|
|
|
|
|
|
def scale_elements_by(self, c: float) -> 'GridRepetition':
|
|
|
|
"""
|
|
|
|
Scale each element by a factor
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Args:
|
|
|
|
c: scaling factor
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
self.scale *= c
|
|
|
|
return self
|
|
|
|
|
|
|
|
def copy(self) -> 'GridRepetition':
|
|
|
|
"""
|
|
|
|
Return a shallow copy of the repetition.
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
`copy.copy(self)`
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
return copy.copy(self)
|
|
|
|
|
2019-05-15 00:12:34 -07:00
|
|
|
def deepcopy(self) -> 'GridRepetition':
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
Return a deep copy of the repetition.
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
`copy.deepcopy(self)`
|
2019-03-31 20:57:10 -07:00
|
|
|
"""
|
|
|
|
return copy.deepcopy(self)
|
|
|
|
|
2019-12-12 00:38:11 -08:00
|
|
|
def lock(self) -> 'GridRepetition':
|
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Lock the `GridRepetition`, disallowing changes.
|
2019-12-12 00:38:11 -08:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
self
|
2019-12-12 00:38:11 -08:00
|
|
|
"""
|
2020-05-11 19:29:00 -07:00
|
|
|
self.offset.flags.writeable = False
|
|
|
|
self.a_vector.flags.writeable = False
|
|
|
|
self.mirrored.flags.writeable = False
|
|
|
|
if self.b_vector is not None:
|
|
|
|
self.b_vector.flags.writeable = False
|
2019-12-12 00:38:11 -08:00
|
|
|
object.__setattr__(self, 'locked', True)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def unlock(self) -> 'GridRepetition':
|
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Unlock the `GridRepetition`
|
2019-12-12 00:38:11 -08:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
self
|
2019-12-12 00:38:11 -08:00
|
|
|
"""
|
2020-05-11 19:29:00 -07:00
|
|
|
self.offset.flags.writeable = True
|
|
|
|
self.a_vector.flags.writeable = True
|
|
|
|
self.mirrored.flags.writeable = True
|
|
|
|
if self.b_vector is not None:
|
|
|
|
self.b_vector.flags.writeable = True
|
2019-12-12 00:38:11 -08:00
|
|
|
object.__setattr__(self, 'locked', False)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def deeplock(self) -> 'GridRepetition':
|
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Recursively lock the `GridRepetition` and its contained pattern
|
2019-12-12 00:38:11 -08:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
self
|
2019-12-12 00:38:11 -08:00
|
|
|
"""
|
2020-05-11 18:58:57 -07:00
|
|
|
assert(self.pattern is not None)
|
2019-12-12 00:38:11 -08:00
|
|
|
self.lock()
|
|
|
|
self.pattern.deeplock()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def deepunlock(self) -> 'GridRepetition':
|
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Recursively unlock the `GridRepetition` and its contained pattern
|
2019-12-12 00:38:11 -08:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
This is dangerous unless you have just performed a deepcopy, since
|
|
|
|
the component parts may be reused elsewhere.
|
2019-12-12 00:38:11 -08:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
self
|
2019-12-12 00:38:11 -08:00
|
|
|
"""
|
2020-05-11 18:58:57 -07:00
|
|
|
assert(self.pattern is not None)
|
2019-12-12 00:38:11 -08:00
|
|
|
self.unlock()
|
|
|
|
self.pattern.deepunlock()
|
|
|
|
return self
|
2020-05-11 20:31:07 -07:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
name = self.pattern.name if self.pattern is not None else None
|
|
|
|
rotation = f' r{self.rotation*180/pi: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 ''
|
|
|
|
dose = f' d{self.dose:g}' if self.dose != 1 else ''
|
|
|
|
locked = ' L' if self.locked else ''
|
2020-05-12 14:17:50 -07:00
|
|
|
bv = f', {self.b_vector}' if self.b_vector is not None else ''
|
2020-05-11 20:31:07 -07:00
|
|
|
return (f'<GridRepetition "{name}" at {self.offset} {rotation}{scale}{mirrored}{dose}'
|
|
|
|
f' {self.a_count}x{self.b_count} ({self.a_vector}{bv}){locked}>')
|