inire/inire/geometry/primitives.py

80 lines
2.4 KiB
Python
Raw Normal View History

2026-03-07 08:26:29 -08:00
from __future__ import annotations
2026-03-09 03:19:01 -07:00
2026-03-30 15:32:29 -07:00
from dataclasses import dataclass
2026-03-30 23:54:30 -07:00
from typing import TYPE_CHECKING, Self
2026-03-29 01:26:22 -07:00
2026-03-09 03:19:01 -07:00
import numpy
2026-03-30 23:54:30 -07:00
if TYPE_CHECKING:
from numpy.typing import NDArray
2026-03-09 03:19:01 -07:00
2026-03-07 08:26:29 -08:00
2026-03-29 01:26:22 -07:00
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
2026-03-07 08:26:29 -08:00
2026-03-30 15:32:29 -07:00
@dataclass(frozen=True, slots=True)
2026-03-09 03:19:01 -07:00
class Port:
"""
2026-03-30 15:32:29 -07:00
Port represented as a normalized integer (x, y, r) triple.
2026-03-09 03:19:01 -07:00
"""
2026-03-30 15:32:29 -07:00
x: int | float
y: int | float
r: int | float
2026-03-29 01:26:22 -07:00
2026-03-30 15:32:29 -07:00
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))
2026-03-29 01:26:22 -07:00
def as_tuple(self) -> tuple[int, int, int]:
2026-03-30 15:32:29 -07:00
return (int(self.x), int(self.y), int(self.r))
2026-03-29 01:26:22 -07:00
2026-03-30 15:32:29 -07:00
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)
2026-03-29 01:26:22 -07:00
2026-03-30 15:32:29 -07:00
def rotated(
self,
angle: int | float,
origin: tuple[int | float, int | float] = (0, 0),
) -> Self:
angle_i = _normalize_angle(angle)
ox, oy = round(origin[0]), round(origin[1])
dx, dy = rotate_cardinal(self.x - ox, self.y - oy, angle_i)
return type(self)(ox + dx, oy + dy, self.r + angle_i)
2026-03-07 08:26:29 -08:00
2026-03-29 01:26:22 -07:00
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)
2026-03-07 08:26:29 -08:00
2026-03-30 23:54:30 -07:00
def rotation_matrix2(rotation_deg: int | float) -> NDArray[numpy.int32]:
2026-03-29 01:26:22 -07:00
quadrant = (_normalize_angle(rotation_deg) // 90) % 4
return (ROT2_0, ROT2_90, ROT2_180, ROT2_270)[quadrant]
def rotate_cardinal(x: float, y: float, heading: int) -> tuple[float, float]:
"""Rotate by a multiple of 90 degrees without snapping the point."""
angle = heading % 360
if angle == 0:
return x, y
if angle == 90:
return -y, x
if angle == 180:
return -x, -y
return y, -x
def port_local_coordinates(origin: Port, x: float, y: float) -> tuple[float, float]:
"""Project a point into the forward and leftward axes of a port."""
return rotate_cardinal(x - origin.x, y - origin.y, -int(origin.r))