inire/inire/seeds.py

62 lines
2.1 KiB
Python

from __future__ import annotations
from dataclasses import dataclass
from math import isfinite
from typing import Literal
BendDirection = Literal["CW", "CCW"]
@dataclass(frozen=True, slots=True)
class StraightSeed:
length: float
def __post_init__(self) -> None:
length = float(self.length)
if not isfinite(length) or length <= 0:
raise ValueError("Straight seed length must be finite and positive")
object.__setattr__(self, "length", length)
@dataclass(frozen=True, slots=True)
class Bend90Seed:
radius: float
direction: BendDirection
def __post_init__(self) -> None:
radius = float(self.radius)
if not isfinite(radius) or radius <= 0:
raise ValueError("Bend seed radius must be finite and positive")
if self.direction not in ("CW", "CCW"):
raise ValueError("Bend seed direction must be 'CW' or 'CCW'")
object.__setattr__(self, "radius", radius)
@dataclass(frozen=True, slots=True)
class SBendSeed:
offset: float
radius: float
def __post_init__(self) -> None:
offset, radius = float(self.offset), float(self.radius)
if not isfinite(radius) or radius <= 0:
raise ValueError("S-bend seed radius must be finite and positive")
if not isfinite(offset) or not offset.is_integer() or not 0 < abs(offset) < 2 * radius:
raise ValueError("S-bend seed offset must be a nonzero integer with magnitude less than twice the radius")
object.__setattr__(self, "offset", offset)
object.__setattr__(self, "radius", radius)
PathSegmentSeed = StraightSeed | Bend90Seed | SBendSeed
@dataclass(frozen=True, slots=True)
class PathSeed:
segments: tuple[PathSegmentSeed, ...]
def __post_init__(self) -> None:
segments = tuple(self.segments)
if any(not isinstance(segment, StraightSeed | Bend90Seed | SBendSeed) for segment in segments):
raise TypeError("PathSeed segments must be StraightSeed, Bend90Seed, or SBendSeed instances")
object.__setattr__(self, "segments", segments)