2023-02-23 13:37:34 -08:00
|
|
|
from typing import Self, cast, Any
|
2020-07-22 02:45:16 -07:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
|
2022-02-23 15:47:38 -08:00
|
|
|
import numpy
|
2020-07-22 02:45:16 -07:00
|
|
|
from numpy import pi
|
2023-01-23 22:27:26 -08:00
|
|
|
from numpy.typing import ArrayLike
|
2020-07-22 02:45:16 -07:00
|
|
|
|
2023-01-21 21:22:11 -08:00
|
|
|
from .positionable import Positionable
|
2020-11-09 21:59:28 -08:00
|
|
|
from ..error import MasqueError
|
2023-01-23 22:27:26 -08:00
|
|
|
from ..utils import rotation_matrix_2d
|
2020-07-22 02:45:16 -07:00
|
|
|
|
2023-01-21 21:22:11 -08:00
|
|
|
|
2023-01-22 22:16:09 -08:00
|
|
|
_empty_slots = () # Workaround to get mypy to ignore intentionally empty slots for superclass
|
|
|
|
|
|
|
|
|
2020-07-22 02:45:16 -07:00
|
|
|
class Rotatable(metaclass=ABCMeta):
|
|
|
|
"""
|
2023-04-07 23:19:55 -07:00
|
|
|
Trait class for all rotatable entities
|
2020-07-22 02:45:16 -07:00
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
|
2023-04-07 23:19:55 -07:00
|
|
|
#
|
|
|
|
# Methods
|
|
|
|
#
|
2020-07-22 02:45:16 -07:00
|
|
|
@abstractmethod
|
2023-02-23 13:37:34 -08:00
|
|
|
def rotate(self, val: float) -> Self:
|
2020-07-22 02:45:16 -07:00
|
|
|
"""
|
|
|
|
Rotate the shape around its origin (0, 0), ignoring its offset.
|
|
|
|
|
|
|
|
Args:
|
2020-11-09 22:06:44 -08:00
|
|
|
val: Angle to rotate by (counterclockwise, radians)
|
2020-07-22 02:45:16 -07:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class RotatableImpl(Rotatable, metaclass=ABCMeta):
|
|
|
|
"""
|
|
|
|
Simple implementation of `Rotatable`
|
|
|
|
"""
|
2023-01-22 22:16:09 -08:00
|
|
|
__slots__ = _empty_slots
|
2020-07-22 02:45:16 -07:00
|
|
|
|
|
|
|
_rotation: float
|
|
|
|
""" rotation for the object, radians counterclockwise """
|
|
|
|
|
2023-04-07 23:19:55 -07:00
|
|
|
#
|
|
|
|
# Properties
|
|
|
|
#
|
2020-07-22 02:45:16 -07:00
|
|
|
@property
|
|
|
|
def rotation(self) -> float:
|
|
|
|
""" Rotation, radians counterclockwise """
|
|
|
|
return self._rotation
|
|
|
|
|
|
|
|
@rotation.setter
|
|
|
|
def rotation(self, val: float):
|
2020-11-09 22:06:44 -08:00
|
|
|
if not numpy.size(val) == 1:
|
2020-11-09 21:59:28 -08:00
|
|
|
raise MasqueError('Rotation must be a scalar')
|
2020-07-22 02:45:16 -07:00
|
|
|
self._rotation = val % (2 * pi)
|
|
|
|
|
2023-04-07 23:19:55 -07:00
|
|
|
#
|
|
|
|
# Methods
|
|
|
|
#
|
2023-02-23 13:37:34 -08:00
|
|
|
def rotate(self, rotation: float) -> Self:
|
2020-07-22 02:45:16 -07:00
|
|
|
self.rotation += rotation
|
|
|
|
return self
|
|
|
|
|
2023-02-23 13:37:34 -08:00
|
|
|
def set_rotation(self, rotation: float) -> Self:
|
2020-07-22 02:45:16 -07:00
|
|
|
"""
|
|
|
|
Set the rotation to a value
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rotation: radians ccw
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
|
|
|
"""
|
|
|
|
self.rotation = rotation
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class Pivotable(metaclass=ABCMeta):
|
|
|
|
"""
|
2023-04-07 23:19:55 -07:00
|
|
|
Trait class for entites which can be rotated around a point.
|
2020-07-22 02:45:16 -07:00
|
|
|
This requires that they are `Positionable` but not necessarily `Rotatable` themselves.
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
|
2020-08-15 17:40:49 -07:00
|
|
|
@abstractmethod
|
2023-02-23 13:37:34 -08:00
|
|
|
def rotate_around(self, pivot: ArrayLike, rotation: float) -> Self:
|
2020-07-22 02:45:16 -07:00
|
|
|
"""
|
|
|
|
Rotate the object around a point.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
pivot: Point (x, y) to rotate around
|
|
|
|
rotation: Angle to rotate by (counterclockwise, radians)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
self
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PivotableImpl(Pivotable, metaclass=ABCMeta):
|
|
|
|
"""
|
|
|
|
Implementation of `Pivotable` for objects which are `Rotatable`
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
|
2023-01-25 23:57:02 -08:00
|
|
|
offset: Any # TODO see if we can get around defining `offset` in PivotableImpl
|
|
|
|
""" `[x_offset, y_offset]` """
|
|
|
|
|
2023-02-23 13:37:34 -08:00
|
|
|
def rotate_around(self, pivot: ArrayLike, rotation: float) -> Self:
|
2020-07-22 02:45:16 -07:00
|
|
|
pivot = numpy.array(pivot, dtype=float)
|
2023-01-21 21:22:11 -08:00
|
|
|
cast(Positionable, self).translate(-pivot)
|
|
|
|
cast(Rotatable, self).rotate(rotation)
|
2023-07-17 21:25:52 -07:00
|
|
|
self.offset = numpy.dot(rotation_matrix_2d(rotation), self.offset) # type: ignore # mypy#3004
|
2023-01-21 21:22:11 -08:00
|
|
|
cast(Positionable, self).translate(+pivot)
|
2020-07-22 02:45:16 -07:00
|
|
|
return self
|
|
|
|
|