inire/inire/router/_seed_materialization.py

67 lines
2.5 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
from inire.geometry.components import bend_fits_width, build_component
from inire.model import resolve_bend_geometry
from inire.seeds import Bend90Seed, SBendSeed, StraightSeed
if TYPE_CHECKING:
from inire.geometry.components import ComponentResult, MoveKind
from inire.geometry.primitives import Port
from inire.model import SearchOptions
from inire.seeds import PathSeed, PathSegmentSeed
def seed_move(segment: PathSegmentSeed) -> tuple[MoveKind, tuple]:
if isinstance(segment, StraightSeed):
return "straight", (max(1, round(segment.length)),)
if isinstance(segment, Bend90Seed):
return "bend90", (segment.radius, segment.direction)
if isinstance(segment, SBendSeed):
return "sbend", (segment.offset, segment.radius)
raise TypeError(f"Unsupported seed segment: {type(segment)!r}")
def seed_width_error(seed: PathSeed, net_width: float) -> str | None:
for index, segment in enumerate(seed.segments, start=1):
if isinstance(segment, (Bend90Seed, SBendSeed)) and not bend_fits_width(segment.radius, net_width):
return f"segment {index} ({type(segment).__name__}) needs radius at least {net_width / 2:g}, got {segment.radius:g}"
return None
def materialize_path_seed(
seed: PathSeed,
*,
start: Port,
net_width: float,
search: SearchOptions,
clearance: float,
) -> tuple[ComponentResult, ...] | None:
if seed_width_error(seed, net_width) is not None:
return None
path: list[ComponentResult] = []
current = start
dilation = clearance / 2.0
bend_collision_type, bend_physical_geometry = resolve_bend_geometry(search)
bend_clip_margin = search.bend_clip_margin
for segment in seed.segments:
kind, parameters = seed_move(segment)
template = search.sbend_template(segment.offset, segment.radius, net_width) if isinstance(segment, SBendSeed) else None
component = build_component(
current,
net_width,
kind,
parameters,
collision_type=bend_collision_type,
physical_geometry_type=bend_physical_geometry,
clip_margin=bend_clip_margin,
dilation=dilation,
sbend_outline=template.polygon if template is not None else None,
physical_length=search.bend_physical_length if kind == "bend90" else (template.length if template else None),
)
path.append(component)
current = component.end_port
return tuple(path)