rework structure of everything
This commit is contained in:
parent
dcc4d6436c
commit
941d3e01df
1 changed files with 3819 additions and 3559 deletions
|
|
@ -1,10 +1,10 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
from dataclasses import dataclass
|
||||
from typing import Self
|
||||
|
||||
import numpy
|
||||
from numpy.typing import ArrayLike, NDArray
|
||||
from numpy.typing import NDArray
|
||||
|
||||
|
||||
def _normalize_angle(angle_deg: int | float) -> int:
|
||||
|
|
@ -13,119 +13,43 @@ def _normalize_angle(angle_deg: int | float) -> int:
|
|||
raise ValueError(f"Port angle must be Manhattan (multiple of 90), got {angle_deg!r}")
|
||||
return angle
|
||||
|
||||
|
||||
def _as_int32_triplet(value: ArrayLike) -> NDArray[numpy.int32]:
|
||||
arr = numpy.asarray(value, dtype=numpy.int32)
|
||||
if arr.shape != (3,):
|
||||
raise ValueError(f"Port array must have shape (3,), got {arr.shape}")
|
||||
arr = arr.copy()
|
||||
arr[2] = _normalize_angle(int(arr[2]))
|
||||
return arr
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class Port:
|
||||
"""
|
||||
Port represented as an ndarray-backed (x, y, r) triple with int32 storage.
|
||||
Port represented as a normalized integer (x, y, r) triple.
|
||||
"""
|
||||
|
||||
__slots__ = ("_xyr",)
|
||||
x: int | float
|
||||
y: int | float
|
||||
r: int | float
|
||||
|
||||
def __init__(self, x: int | float, y: int | float, r: int | float) -> None:
|
||||
self._xyr = numpy.array(
|
||||
(int(round(x)), int(round(y)), _normalize_angle(r)),
|
||||
dtype=numpy.int32,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_array(cls, xyr: ArrayLike) -> Self:
|
||||
obj = cls.__new__(cls)
|
||||
obj._xyr = _as_int32_triplet(xyr)
|
||||
return obj
|
||||
|
||||
@property
|
||||
def x(self) -> int:
|
||||
return int(self._xyr[0])
|
||||
|
||||
@x.setter
|
||||
def x(self, val: int | float) -> None:
|
||||
self._xyr[0] = int(round(val))
|
||||
|
||||
@property
|
||||
def y(self) -> int:
|
||||
return int(self._xyr[1])
|
||||
|
||||
@y.setter
|
||||
def y(self, val: int | float) -> None:
|
||||
self._xyr[1] = int(round(val))
|
||||
|
||||
@property
|
||||
def r(self) -> int:
|
||||
return int(self._xyr[2])
|
||||
|
||||
@r.setter
|
||||
def r(self, val: int | float) -> None:
|
||||
self._xyr[2] = _normalize_angle(val)
|
||||
|
||||
@property
|
||||
def orientation(self) -> int:
|
||||
return self.r
|
||||
|
||||
@orientation.setter
|
||||
def orientation(self, val: int | float) -> None:
|
||||
self.r = val
|
||||
|
||||
@property
|
||||
def xyr(self) -> NDArray[numpy.int32]:
|
||||
return self._xyr
|
||||
|
||||
@xyr.setter
|
||||
def xyr(self, val: ArrayLike) -> None:
|
||||
self._xyr = _as_int32_triplet(val)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Port(x={self.x}, y={self.y}, r={self.r})"
|
||||
|
||||
def __iter__(self) -> Iterator[int]:
|
||||
return iter((self.x, self.y, self.r))
|
||||
|
||||
def __len__(self) -> int:
|
||||
return 3
|
||||
|
||||
def __getitem__(self, item: int | slice) -> int | NDArray[numpy.int32]:
|
||||
return self._xyr[item]
|
||||
|
||||
def __array__(self, dtype: numpy.dtype | None = None) -> NDArray[numpy.int32]:
|
||||
return numpy.asarray(self._xyr, dtype=dtype)
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, Port):
|
||||
return False
|
||||
return bool(numpy.array_equal(self._xyr, other._xyr))
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash(self.as_tuple())
|
||||
|
||||
def copy(self) -> Self:
|
||||
return type(self).from_array(self._xyr.copy())
|
||||
def __post_init__(self) -> None:
|
||||
object.__setattr__(self, "x", int(round(self.x)))
|
||||
object.__setattr__(self, "y", int(round(self.y)))
|
||||
object.__setattr__(self, "r", _normalize_angle(self.r))
|
||||
|
||||
def as_tuple(self) -> tuple[int, int, int]:
|
||||
return (self.x, self.y, self.r)
|
||||
return (int(self.x), int(self.y), int(self.r))
|
||||
|
||||
def translate(self, dxy: ArrayLike) -> Self:
|
||||
dxy_arr = numpy.asarray(dxy, dtype=numpy.int32)
|
||||
if dxy_arr.shape == (2,):
|
||||
return type(self)(self.x + int(dxy_arr[0]), self.y + int(dxy_arr[1]), self.r)
|
||||
if dxy_arr.shape == (3,):
|
||||
return type(self)(self.x + int(dxy_arr[0]), self.y + int(dxy_arr[1]), self.r + int(dxy_arr[2]))
|
||||
raise ValueError(f"Translation must have shape (2,) or (3,), got {dxy_arr.shape}")
|
||||
def translate(
|
||||
self,
|
||||
dx: int | float = 0,
|
||||
dy: int | float = 0,
|
||||
rotation: int | float = 0,
|
||||
) -> Self:
|
||||
return type(self)(self.x + dx, self.y + dy, self.r + rotation)
|
||||
|
||||
def __add__(self, other: ArrayLike) -> Self:
|
||||
return self.translate(other)
|
||||
|
||||
def __sub__(self, other: ArrayLike | Self) -> NDArray[numpy.int32]:
|
||||
if isinstance(other, Port):
|
||||
return self._xyr - other._xyr
|
||||
return self._xyr - numpy.asarray(other, dtype=numpy.int32)
|
||||
def rotated(
|
||||
self,
|
||||
angle: int | float,
|
||||
origin: tuple[int | float, int | float] = (0, 0),
|
||||
) -> Self:
|
||||
angle_i = _normalize_angle(angle)
|
||||
rot = rotation_matrix2(angle_i)
|
||||
origin_xy = numpy.array((int(round(origin[0])), int(round(origin[1]))), dtype=numpy.int32)
|
||||
rel = numpy.array((self.x, self.y), dtype=numpy.int32) - origin_xy
|
||||
rotated = origin_xy + rot @ rel
|
||||
return type(self)(int(rotated[0]), int(rotated[1]), self.r + angle_i)
|
||||
|
||||
|
||||
ROT2_0 = numpy.array(((1, 0), (0, 1)), dtype=numpy.int32)
|
||||
|
|
@ -145,16 +69,3 @@ def rotation_matrix3(rotation_deg: int) -> NDArray[numpy.int32]:
|
|||
rot3[:2, :2] = rot2
|
||||
rot3[2, 2] = 1
|
||||
return rot3
|
||||
|
||||
|
||||
def translate_port(port: Port, dx: int | float, dy: int | float) -> Port:
|
||||
return Port(port.x + dx, port.y + dy, port.r)
|
||||
|
||||
|
||||
def rotate_port(port: Port, angle: int | float, origin: tuple[int | float, int | float] = (0, 0)) -> Port:
|
||||
angle_i = _normalize_angle(angle)
|
||||
rot = rotation_matrix2(angle_i)
|
||||
origin_xy = numpy.array((int(round(origin[0])), int(round(origin[1]))), dtype=numpy.int32)
|
||||
rel = numpy.array((port.x, port.y), dtype=numpy.int32) - origin_xy
|
||||
rotated = origin_xy + rot @ rel
|
||||
return Port(int(rotated[0]), int(rotated[1]), port.r + angle_i)
|
||||
|
|
|
|||
Loading…
Reference in a new issue