style
This commit is contained in:
parent
c9bb8d6469
commit
8eb0dbf64a
1 changed files with 910 additions and 391 deletions
|
|
@ -1,50 +1,111 @@
|
|||
from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
import numpy as np
|
||||
|
||||
import numpy
|
||||
|
||||
|
||||
# 1nm snap (0.001 µm)
|
||||
GRID_SNAP_UM = 0.001
|
||||
|
||||
|
||||
def snap_nm(value: float) -> float:
|
||||
"""Snap a coordinate to the nearest 1nm (0.001 um)."""
|
||||
"""
|
||||
Snap a coordinate to the nearest 1nm (0.001 um).
|
||||
|
||||
Args:
|
||||
value: Coordinate value to snap.
|
||||
|
||||
Returns:
|
||||
Snapped coordinate value.
|
||||
"""
|
||||
return round(value / GRID_SNAP_UM) * GRID_SNAP_UM
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Port:
|
||||
"""A port defined by (x, y, orientation) in micrometers."""
|
||||
x: float
|
||||
y: float
|
||||
orientation: float # Degrees: 0, 90, 180, 270
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
class Port:
|
||||
"""
|
||||
A port defined by (x, y, orientation) in micrometers.
|
||||
"""
|
||||
__slots__ = ('x', 'y', 'orientation')
|
||||
|
||||
x: float
|
||||
""" x-coordinate in micrometers """
|
||||
|
||||
y: float
|
||||
""" y-coordinate in micrometers """
|
||||
|
||||
orientation: float
|
||||
""" Orientation in degrees: 0, 90, 180, 270 """
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
x: float,
|
||||
y: float,
|
||||
orientation: float,
|
||||
) -> None:
|
||||
"""
|
||||
Initialize and snap a Port.
|
||||
|
||||
Args:
|
||||
x: Initial x-coordinate.
|
||||
y: Initial y-coordinate.
|
||||
orientation: Initial orientation in degrees.
|
||||
"""
|
||||
# Snap x, y to 1nm
|
||||
# We need to use object.__setattr__ because the dataclass is frozen.
|
||||
snapped_x = snap_nm(self.x)
|
||||
snapped_y = snap_nm(self.y)
|
||||
self.x = snap_nm(x)
|
||||
self.y = snap_nm(y)
|
||||
|
||||
# Ensure orientation is one of {0, 90, 180, 270}
|
||||
norm_orientation = int(round(self.orientation)) % 360
|
||||
norm_orientation = int(round(orientation)) % 360
|
||||
if norm_orientation not in {0, 90, 180, 270}:
|
||||
norm_orientation = (round(norm_orientation / 90) * 90) % 360
|
||||
|
||||
object.__setattr__(self, "x", snapped_x)
|
||||
object.__setattr__(self, "y", snapped_y)
|
||||
object.__setattr__(self, "orientation", float(norm_orientation))
|
||||
self.orientation = float(norm_orientation)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f'Port(x={self.x}, y={self.y}, orientation={self.orientation})'
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if not isinstance(other, Port):
|
||||
return False
|
||||
return (self.x == other.x and
|
||||
self.y == other.y and
|
||||
self.orientation == other.orientation)
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash((self.x, self.y, self.orientation))
|
||||
|
||||
|
||||
def translate_port(port: Port, dx: float, dy: float) -> Port:
|
||||
"""Translate a port by (dx, dy)."""
|
||||
"""
|
||||
Translate a port by (dx, dy).
|
||||
|
||||
Args:
|
||||
port: Port to translate.
|
||||
dx: x-offset.
|
||||
dy: y-offset.
|
||||
|
||||
Returns:
|
||||
A new translated Port.
|
||||
"""
|
||||
return Port(port.x + dx, port.y + dy, port.orientation)
|
||||
|
||||
|
||||
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."""
|
||||
"""
|
||||
Rotate a port by a multiple of 90 degrees around an origin.
|
||||
|
||||
Args:
|
||||
port: Port to rotate.
|
||||
angle: Angle to rotate by (degrees).
|
||||
origin: (x, y) origin to rotate around.
|
||||
|
||||
Returns:
|
||||
A new rotated Port.
|
||||
"""
|
||||
ox, oy = origin
|
||||
px, py = port.x, port.y
|
||||
|
||||
rad = np.radians(angle)
|
||||
qx = ox + np.cos(rad) * (px - ox) - np.sin(rad) * (py - oy)
|
||||
qy = oy + np.sin(rad) * (px - ox) + np.cos(rad) * (py - oy)
|
||||
rad = numpy.radians(angle)
|
||||
qx = ox + numpy.cos(rad) * (px - ox) - numpy.sin(rad) * (py - oy)
|
||||
qy = oy + numpy.sin(rad) * (px - ox) + numpy.cos(rad) * (py - oy)
|
||||
|
||||
return Port(qx, qy, port.orientation + angle)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue