2023-02-23 13:37:34 -08:00
|
|
|
from typing import Self
|
2018-08-30 23:07:14 -07:00
|
|
|
import copy
|
2022-02-23 15:47:38 -08:00
|
|
|
|
|
|
|
import numpy
|
|
|
|
from numpy.typing import ArrayLike, NDArray
|
2018-08-30 23:07:14 -07:00
|
|
|
|
2020-07-22 21:50:39 -07:00
|
|
|
from .repetition import Repetition
|
2022-02-23 15:47:38 -08:00
|
|
|
from .utils import rotation_matrix_2d, layer_t, AutoSlots, annotations_t
|
2022-07-07 11:27:29 -07:00
|
|
|
from .traits import PositionableImpl, LayerableImpl, Copyable, Pivotable, RepeatableImpl
|
2020-09-10 20:06:58 -07:00
|
|
|
from .traits import AnnotatableImpl
|
2018-08-30 23:07:14 -07:00
|
|
|
|
|
|
|
|
2022-07-07 11:27:29 -07:00
|
|
|
class Label(PositionableImpl, LayerableImpl, RepeatableImpl, AnnotatableImpl,
|
2020-07-22 21:50:39 -07:00
|
|
|
Pivotable, Copyable, metaclass=AutoSlots):
|
2018-08-30 23:07:14 -07:00
|
|
|
"""
|
2019-12-12 00:38:11 -08:00
|
|
|
A text annotation with a position and layer (but no size; it is not drawn)
|
2018-08-30 23:07:14 -07:00
|
|
|
"""
|
2023-01-18 17:14:35 -08:00
|
|
|
__slots__ = ( '_string', )
|
2018-08-30 23:07:14 -07:00
|
|
|
|
2019-05-17 00:37:56 -07:00
|
|
|
_string: str
|
2020-02-17 21:02:53 -08:00
|
|
|
""" Label string """
|
2018-08-30 23:07:14 -07:00
|
|
|
|
2020-07-22 02:45:16 -07:00
|
|
|
'''
|
|
|
|
---- Properties
|
|
|
|
'''
|
2018-08-30 23:07:14 -07:00
|
|
|
# string property
|
|
|
|
@property
|
|
|
|
def string(self) -> str:
|
|
|
|
"""
|
|
|
|
Label string (str)
|
|
|
|
"""
|
|
|
|
return self._string
|
|
|
|
|
|
|
|
@string.setter
|
2022-02-23 11:27:11 -08:00
|
|
|
def string(self, val: str) -> None:
|
2018-08-30 23:07:14 -07:00
|
|
|
self._string = val
|
|
|
|
|
2022-02-23 11:27:11 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
string: str,
|
|
|
|
*,
|
2022-02-23 15:47:38 -08:00
|
|
|
offset: ArrayLike = (0.0, 0.0),
|
2022-02-23 11:27:11 -08:00
|
|
|
layer: layer_t = 0,
|
2023-02-23 13:15:32 -08:00
|
|
|
repetition: Repetition | None = None,
|
|
|
|
annotations: annotations_t | None = None,
|
2022-02-23 11:27:11 -08:00
|
|
|
) -> None:
|
2018-08-30 23:07:14 -07:00
|
|
|
self.string = string
|
2020-05-11 18:49:30 -07:00
|
|
|
self.offset = numpy.array(offset, dtype=float, copy=True)
|
2018-08-30 23:07:14 -07:00
|
|
|
self.layer = layer
|
2020-07-22 21:50:39 -07:00
|
|
|
self.repetition = repetition
|
2020-09-10 20:06:58 -07:00
|
|
|
self.annotations = annotations if annotations is not None else {}
|
2019-12-12 00:38:11 -08:00
|
|
|
|
2023-02-23 13:37:34 -08:00
|
|
|
def __copy__(self) -> Self:
|
2022-07-07 11:27:29 -07:00
|
|
|
return type(self)(
|
|
|
|
string=self.string,
|
|
|
|
offset=self.offset.copy(),
|
|
|
|
layer=self.layer,
|
|
|
|
repetition=self.repetition,
|
|
|
|
)
|
2018-08-30 23:07:14 -07:00
|
|
|
|
2023-02-23 13:37:34 -08:00
|
|
|
def __deepcopy__(self, memo: dict | None = None) -> Self:
|
2019-05-17 00:31:07 -07:00
|
|
|
memo = {} if memo is None else memo
|
2022-06-08 20:58:04 -07:00
|
|
|
new = copy.copy(self)
|
2019-05-17 00:31:07 -07:00
|
|
|
new._offset = self._offset.copy()
|
|
|
|
return new
|
2018-08-30 23:07:14 -07:00
|
|
|
|
2023-02-23 13:37:34 -08:00
|
|
|
def rotate_around(self, pivot: ArrayLike, rotation: float) -> Self:
|
2018-08-30 23:07:14 -07:00
|
|
|
"""
|
2020-02-17 21:02:53 -08:00
|
|
|
Rotate the label around a point.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pivot: Point (x, y) to rotate around
|
|
|
|
rotation: Angle to rotate by (counterclockwise, radians)
|
2018-08-30 23:07:14 -07:00
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
self
|
2018-08-30 23:07:14 -07:00
|
|
|
"""
|
|
|
|
pivot = numpy.array(pivot, dtype=float)
|
|
|
|
self.translate(-pivot)
|
|
|
|
self.offset = numpy.dot(rotation_matrix_2d(rotation), self.offset)
|
|
|
|
self.translate(+pivot)
|
|
|
|
return self
|
|
|
|
|
2022-02-23 15:47:38 -08:00
|
|
|
def get_bounds(self) -> NDArray[numpy.float64]:
|
2018-08-30 23:07:14 -07:00
|
|
|
"""
|
|
|
|
Return the bounds of the label.
|
|
|
|
|
|
|
|
Labels are assumed to take up 0 area, i.e.
|
|
|
|
bounds = [self.offset,
|
|
|
|
self.offset]
|
|
|
|
|
2020-02-17 21:02:53 -08:00
|
|
|
Returns:
|
|
|
|
Bounds [[xmin, xmax], [ymin, ymax]]
|
2018-08-30 23:07:14 -07:00
|
|
|
"""
|
|
|
|
return numpy.array([self.offset, self.offset])
|