trim down
This commit is contained in:
parent
457451d3b2
commit
4c2d5051cd
1 changed files with 1789 additions and 1887 deletions
|
|
@ -1,77 +1,160 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterator
|
||||
from typing import Self
|
||||
|
||||
import numpy
|
||||
from numpy.typing import ArrayLike, NDArray
|
||||
|
||||
|
||||
# 1nm snap (0.001 µm)
|
||||
GRID_SNAP_UM = 0.001
|
||||
def _normalize_angle(angle_deg: int | float) -> int:
|
||||
angle = int(round(angle_deg)) % 360
|
||||
if angle % 90 != 0:
|
||||
raise ValueError(f"Port angle must be Manhattan (multiple of 90), got {angle_deg!r}")
|
||||
return angle
|
||||
|
||||
|
||||
def snap_nm(value: float) -> float:
|
||||
"""
|
||||
Snap a coordinate to the nearest 1nm (0.001 um).
|
||||
"""
|
||||
return round(value * 1000) / 1000
|
||||
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
|
||||
|
||||
|
||||
from inire.constants import TOLERANCE_LINEAR
|
||||
|
||||
class Port:
|
||||
"""
|
||||
A port defined by (x, y, orientation) in micrometers.
|
||||
Port represented as an ndarray-backed (x, y, r) triple with int32 storage.
|
||||
"""
|
||||
__slots__ = ('x', 'y', 'orientation')
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
x: float,
|
||||
y: float,
|
||||
orientation: float,
|
||||
snap: bool = True
|
||||
) -> None:
|
||||
if snap:
|
||||
self.x = round(x * 1000) / 1000
|
||||
self.y = round(y * 1000) / 1000
|
||||
# Faster orientation normalization for common cases
|
||||
if 0 <= orientation < 360:
|
||||
self.orientation = float(orientation)
|
||||
else:
|
||||
self.orientation = float(orientation % 360)
|
||||
else:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.orientation = float(orientation)
|
||||
__slots__ = ("_xyr",)
|
||||
|
||||
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}, orientation={self.orientation})'
|
||||
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 (abs(self.x - other.x) < TOLERANCE_LINEAR and
|
||||
abs(self.y - other.y) < TOLERANCE_LINEAR and
|
||||
abs(self.orientation - other.orientation) < TOLERANCE_LINEAR)
|
||||
return bool(numpy.array_equal(self._xyr, other._xyr))
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash((round(self.x, 6), round(self.y, 6), round(self.orientation, 6)))
|
||||
return hash(self.as_tuple())
|
||||
|
||||
def copy(self) -> Self:
|
||||
return type(self).from_array(self._xyr.copy())
|
||||
|
||||
def as_tuple(self) -> tuple[int, int, int]:
|
||||
return (self.x, self.y, 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 __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 translate_port(port: Port, dx: float, dy: float) -> Port:
|
||||
"""
|
||||
Translate a port by (dx, dy).
|
||||
"""
|
||||
return Port(port.x + dx, port.y + dy, port.orientation)
|
||||
ROT2_0 = numpy.array(((1, 0), (0, 1)), dtype=numpy.int32)
|
||||
ROT2_90 = numpy.array(((0, -1), (1, 0)), dtype=numpy.int32)
|
||||
ROT2_180 = numpy.array(((-1, 0), (0, -1)), dtype=numpy.int32)
|
||||
ROT2_270 = numpy.array(((0, 1), (-1, 0)), dtype=numpy.int32)
|
||||
|
||||
|
||||
def rotate_port(port: Port, angle: float, origin: tuple[float, float] = (0, 0)) -> Port:
|
||||
"""
|
||||
Rotate a port by a multiple of 90 degrees around an origin.
|
||||
"""
|
||||
ox, oy = origin
|
||||
px, py = port.x, port.y
|
||||
def rotation_matrix2(rotation_deg: int) -> NDArray[numpy.int32]:
|
||||
quadrant = (_normalize_angle(rotation_deg) // 90) % 4
|
||||
return (ROT2_0, ROT2_90, ROT2_180, ROT2_270)[quadrant]
|
||||
|
||||
rad = numpy.radians(angle)
|
||||
qx = snap_nm(ox + numpy.cos(rad) * (px - ox) - numpy.sin(rad) * (py - oy))
|
||||
qy = snap_nm(oy + numpy.sin(rad) * (px - ox) + numpy.cos(rad) * (py - oy))
|
||||
|
||||
return Port(qx, qy, port.orientation + angle)
|
||||
def rotation_matrix3(rotation_deg: int) -> NDArray[numpy.int32]:
|
||||
rot2 = rotation_matrix2(rotation_deg)
|
||||
rot3 = numpy.zeros((3, 3), dtype=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