92 lines
3.6 KiB
Python
92 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from itertools import pairwise
|
|
from typing import TYPE_CHECKING
|
|
|
|
from inire.geometry.primitives import Port
|
|
from inire.geometry.sbend import sbend_grid_span
|
|
from inire.seeds import Bend90Seed, PathSeed, SBendSeed, StraightSeed
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
|
|
from inire.geometry.components import ComponentResult
|
|
from inire.seeds import PathSegmentSeed
|
|
|
|
|
|
XY = tuple[int, int]
|
|
_HEADINGS = ((1, 0), (0, 1), (-1, 0), (0, -1))
|
|
|
|
|
|
def _direction(a: XY, b: XY) -> int | None:
|
|
dx, dy = b[0] - a[0], b[1] - a[1]
|
|
if not dx and not dy:
|
|
raise ValueError("Consecutive waypoints must be distinct")
|
|
if not dy:
|
|
return 0 if dx > 0 else 180
|
|
if not dx:
|
|
return 90 if dy > 0 else 270
|
|
return None
|
|
|
|
|
|
def compile_waypoints(points: tuple[XY, ...], start: Port, radius: float) -> tuple[PathSeed, Port]:
|
|
"""Replace sharp Manhattan intersections with tangential arc components."""
|
|
if len(points) < 2 or points[0] != (start.x, start.y):
|
|
raise ValueError("The waypoint path must start at the port and have at least two points")
|
|
directions = [_direction(a, b) for a, b in pairwise(points)]
|
|
corners = tuple(a is not None and b is not None and a != b for a, b in pairwise(directions))
|
|
segments: list[PathSegmentSeed] = []
|
|
heading = int(start.r)
|
|
for index, (a, b) in enumerate(pairwise(points)):
|
|
direction = directions[index]
|
|
dx, dy = b[0] - a[0], b[1] - a[1]
|
|
if direction is None:
|
|
ux, uy = _HEADINGS[heading // 90]
|
|
offset = dy * ux - dx * uy
|
|
span = dx * ux + dy * uy
|
|
if not 0 < abs(offset) < 2 * radius or span <= 0 or span != sbend_grid_span(offset, radius):
|
|
raise ValueError(f"Invalid S-bend from {a} to {b}")
|
|
segments.append(SBendSeed(offset, radius))
|
|
continue
|
|
if direction != heading:
|
|
raise ValueError(f"Inconsistent segment heading from {a} to {b}")
|
|
entry = index > 0 and corners[index - 1]
|
|
exit_corner = index < len(corners) and corners[index]
|
|
length = abs(dx) + abs(dy) - radius * (entry + exit_corner)
|
|
if length < 0:
|
|
raise ValueError(f"Overlapping bend tangencies from {a} to {b}")
|
|
if length:
|
|
segments.append(StraightSeed(length))
|
|
if exit_corner:
|
|
next_heading = directions[index + 1]
|
|
assert next_heading is not None
|
|
sweep = (next_heading - heading) % 360
|
|
if sweep not in (90, 270):
|
|
raise ValueError(f"Unsupported turn at {b}")
|
|
segments.append(Bend90Seed(radius, "CCW" if sweep == 90 else "CW"))
|
|
heading = next_heading
|
|
return PathSeed(tuple(segments)), Port(*points[-1], heading)
|
|
|
|
|
|
def export_waypoints(path: Sequence[ComponentResult]) -> tuple[tuple[float, float], ...]:
|
|
"""Export bend intersections and S-bend tangencies from a realized path."""
|
|
points: list[tuple[float, float]] = []
|
|
|
|
def append(x: float, y: float) -> None:
|
|
if not points or points[-1] != (x, y):
|
|
points.append((x, y))
|
|
|
|
if not path:
|
|
return ()
|
|
append(path[0].start_port.x, path[0].start_port.y)
|
|
for component in path:
|
|
start = component.start_port
|
|
move = component.move_spec
|
|
if isinstance(move, Bend90Seed):
|
|
ux, uy = _HEADINGS[int(start.r) // 90]
|
|
append(start.x + ux * move.radius, start.y + uy * move.radius)
|
|
elif isinstance(move, SBendSeed):
|
|
append(start.x, start.y)
|
|
append(component.end_port.x, component.end_port.y)
|
|
append(path[-1].end_port.x, path[-1].end_port.y)
|
|
return tuple(points)
|