Try verified perimeter construction early and reuse probe routes as movable hints during lookahead. Batch collision checks and cache component generation for repeated placement and refinement candidates.
103 lines
4.4 KiB
Python
103 lines
4.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from inire.geometry.components import (
|
|
ComponentPreparationError, build_component, component_endpoint, prepare_component_sampling,
|
|
)
|
|
from inire.model import _AmbiguousSBendOutlineError, 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_preparation_error(seed: PathSeed, *, start: Port, net_width: float, search: SearchOptions, clearance: float) -> str | None:
|
|
proxy, physical = resolve_bend_geometry(search)
|
|
for index, segment in enumerate(seed.segments, start=1):
|
|
kind, parameters = seed_move(segment)
|
|
try:
|
|
template = search.sbend_template(segment.offset, segment.radius, net_width) if isinstance(segment, SBendSeed) else None
|
|
start = component_endpoint(start, kind, parameters)
|
|
prepare_component_sampling(
|
|
kind, parameters, net_width, collision_type=proxy, physical_geometry_type=physical,
|
|
dilation=clearance / 2, sbend_outline=template.polygon if template is not None else None,
|
|
)
|
|
except (ComponentPreparationError, OverflowError, _AmbiguousSBendOutlineError) as exc:
|
|
return f"segment {index} ({type(segment).__name__}) {exc}"
|
|
return None
|
|
|
|
|
|
class SeedMaterializationError(ValueError):
|
|
"""A supplied seed cannot be materialized."""
|
|
|
|
|
|
def materialize_path_seed(
|
|
seed: PathSeed,
|
|
*,
|
|
start: Port,
|
|
net_width: float,
|
|
search: SearchOptions,
|
|
clearance: float,
|
|
bounds: tuple[float, float, float, float] | None = None,
|
|
diagnostic: bool = False,
|
|
component_cache: dict[tuple[Port, PathSegmentSeed], ComponentResult] | None = None,
|
|
) -> tuple[ComponentResult, ...] | None:
|
|
"""Materialize a seed, optionally reusing moves with the same geometry settings."""
|
|
if diagnostic:
|
|
error = seed_preparation_error(seed, start=start, net_width=net_width, search=search, clearance=clearance)
|
|
if error is not None:
|
|
raise SeedMaterializationError(error)
|
|
|
|
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 index, segment in enumerate(seed.segments, start=1):
|
|
if component_cache is not None:
|
|
key = (current, segment)
|
|
cached = component_cache.get(key)
|
|
if cached is not None:
|
|
path.append(cached)
|
|
current = cached.end_port
|
|
continue
|
|
kind, parameters = seed_move(segment)
|
|
try:
|
|
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,
|
|
bounds=None if diagnostic else bounds,
|
|
physical_length=search.bend_physical_length if kind == "bend90" else (template.length if template else None),
|
|
)
|
|
except (ComponentPreparationError, OverflowError, _AmbiguousSBendOutlineError) as exc:
|
|
if diagnostic:
|
|
raise SeedMaterializationError(f"segment {index} ({type(segment).__name__}) {exc}") from exc
|
|
return None
|
|
if component_cache is not None and len(component_cache) < 256:
|
|
component_cache[key] = component
|
|
path.append(component)
|
|
current = component.end_port
|
|
return tuple(path)
|