2026-03-29 20:35:58 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-11-28 20:16:29 -08:00
|
|
|
from itertools import accumulate, combinations
|
2026-03-29 20:35:58 -07:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
2026-11-30 19:29:51 -08:00
|
|
|
from shapely import box
|
2026-11-27 19:12:09 -08:00
|
|
|
from shapely.strtree import STRtree
|
|
|
|
|
|
2026-11-27 15:51:53 -08:00
|
|
|
from inire.constants import TOLERANCE_LINEAR
|
|
|
|
|
|
2026-03-29 20:35:58 -07:00
|
|
|
if TYPE_CHECKING:
|
2026-11-30 15:24:14 -08:00
|
|
|
from collections.abc import Iterable, Sequence
|
2026-03-29 20:35:58 -07:00
|
|
|
from shapely.geometry import Polygon
|
|
|
|
|
|
|
|
|
|
from inire.geometry.components import ComponentResult
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def components_overlap(
|
|
|
|
|
component_a: ComponentResult,
|
|
|
|
|
component_b: ComponentResult,
|
|
|
|
|
prefer_actual: bool = False,
|
2026-11-27 15:51:53 -08:00
|
|
|
*,
|
|
|
|
|
at_join: bool = False,
|
2026-11-28 20:16:29 -08:00
|
|
|
clearance: float = 0.0,
|
2026-03-29 20:35:58 -07:00
|
|
|
) -> bool:
|
2026-03-30 23:54:30 -07:00
|
|
|
polygons_a: Sequence[Polygon]
|
|
|
|
|
polygons_b: Sequence[Polygon]
|
2026-03-30 19:51:37 -07:00
|
|
|
if prefer_actual:
|
|
|
|
|
polygons_a = component_a.physical_geometry
|
|
|
|
|
polygons_b = component_b.physical_geometry
|
2026-11-27 19:12:09 -08:00
|
|
|
bounds_a = component_a.total_physical_bounds
|
|
|
|
|
bounds_b = component_b.total_physical_bounds
|
2026-03-30 19:51:37 -07:00
|
|
|
else:
|
|
|
|
|
polygons_a = component_a.collision_geometry
|
|
|
|
|
polygons_b = component_b.collision_geometry
|
|
|
|
|
bounds_a = component_a.total_bounds
|
|
|
|
|
bounds_b = component_b.total_bounds
|
|
|
|
|
|
2026-11-28 00:09:11 -08:00
|
|
|
if (
|
2026-11-28 20:16:29 -08:00
|
|
|
bounds_a[0] > bounds_b[2] + clearance
|
|
|
|
|
or bounds_a[2] + clearance < bounds_b[0]
|
|
|
|
|
or bounds_a[1] > bounds_b[3] + clearance
|
|
|
|
|
or bounds_a[3] + clearance < bounds_b[1]
|
2026-03-29 20:35:58 -07:00
|
|
|
):
|
|
|
|
|
return False
|
|
|
|
|
|
2026-11-29 23:12:28 -08:00
|
|
|
at_join = at_join and component_a.end_port == component_b.start_port
|
2026-11-28 20:01:29 -08:00
|
|
|
if at_join:
|
2026-11-29 23:12:28 -08:00
|
|
|
port = component_a.end_port
|
|
|
|
|
if not (component_a.standard_faces and component_b.standard_faces):
|
|
|
|
|
# Custom neighbors may abut along any part of their boundaries.
|
|
|
|
|
return any(polygon_a.relate_pattern(polygon_b, "2********") for polygon_a in polygons_a for polygon_b in polygons_b)
|
|
|
|
|
width_a, width_b = component_a.width, component_b.width
|
|
|
|
|
if width_a is not None and width_b is not None:
|
|
|
|
|
axis = 0 if port.r % 180 == 0 else 1
|
|
|
|
|
normal, lateral = (port.x, port.y) if axis == 0 else (port.y, port.x)
|
|
|
|
|
half_width = min(width_a, width_b) / 2
|
|
|
|
|
lo, hi = lateral - half_width, lateral + half_width
|
2026-11-30 15:24:18 -08:00
|
|
|
# An intersection contained in the shared face cannot count as overlap.
|
|
|
|
|
if (
|
|
|
|
|
max(bounds_a[axis], bounds_b[axis]) >= normal - TOLERANCE_LINEAR
|
|
|
|
|
and min(bounds_a[axis + 2], bounds_b[axis + 2]) <= normal + TOLERANCE_LINEAR
|
|
|
|
|
and max(bounds_a[1 - axis], bounds_b[1 - axis]) >= lo - TOLERANCE_LINEAR
|
|
|
|
|
and min(bounds_a[3 - axis], bounds_b[3 - axis]) <= hi + TOLERANCE_LINEAR
|
|
|
|
|
):
|
|
|
|
|
return False
|
2026-11-28 20:01:29 -08:00
|
|
|
else:
|
2026-11-29 23:12:28 -08:00
|
|
|
at_join = False
|
2026-11-28 20:01:29 -08:00
|
|
|
|
2026-03-29 20:35:58 -07:00
|
|
|
for polygon_a in polygons_a:
|
|
|
|
|
for polygon_b in polygons_b:
|
2026-11-27 15:51:53 -08:00
|
|
|
if at_join:
|
|
|
|
|
# Sampling a bend can leave sub-tolerance slivers at its shared face.
|
|
|
|
|
overlap = polygon_a.intersection(polygon_b)
|
2026-11-28 00:09:11 -08:00
|
|
|
if not overlap.is_empty:
|
2026-11-27 15:51:53 -08:00
|
|
|
bounds = overlap.bounds
|
2026-11-28 20:01:29 -08:00
|
|
|
if (
|
|
|
|
|
bounds[axis] < normal - TOLERANCE_LINEAR
|
|
|
|
|
or bounds[axis + 2] > normal + TOLERANCE_LINEAR
|
|
|
|
|
or bounds[1 - axis] < lo - TOLERANCE_LINEAR
|
|
|
|
|
or bounds[3 - axis] > hi + TOLERANCE_LINEAR
|
|
|
|
|
):
|
2026-11-27 15:51:53 -08:00
|
|
|
return True
|
2026-11-28 20:16:29 -08:00
|
|
|
elif (
|
|
|
|
|
(polygon_a.intersects(polygon_b) and (prefer_actual or not polygon_a.touches(polygon_b)))
|
|
|
|
|
or (clearance and polygon_a.distance(polygon_b) < clearance - TOLERANCE_LINEAR * min(1.0, clearance))
|
|
|
|
|
):
|
2026-03-29 20:35:58 -07:00
|
|
|
return True
|
|
|
|
|
return False
|
2026-11-27 19:12:09 -08:00
|
|
|
|
|
|
|
|
|
2026-11-28 20:16:29 -08:00
|
|
|
def count_path_self_collisions(components: Sequence[ComponentResult], clearance: float = 0.0) -> int:
|
2026-11-27 21:40:53 -08:00
|
|
|
internal_collisions = 0
|
|
|
|
|
for component in components:
|
|
|
|
|
for left, right in combinations(component.physical_geometry, 2):
|
|
|
|
|
overlap = left.intersection(right)
|
|
|
|
|
# A reflected S-bend seam can contain a rounding sliver.
|
|
|
|
|
if overlap.area and not overlap.buffer(-TOLERANCE_LINEAR / 2).is_empty:
|
|
|
|
|
internal_collisions += 1
|
2026-11-27 19:12:09 -08:00
|
|
|
if len(components) < 2:
|
2026-11-27 21:40:53 -08:00
|
|
|
return internal_collisions
|
2026-11-28 20:16:29 -08:00
|
|
|
distances = list(accumulate((component.length for component in components), initial=0.0)) if clearance else ()
|
|
|
|
|
|
|
|
|
|
def spacing(i: int, other: int) -> float:
|
|
|
|
|
if not distances or distances[other] - distances[i + 1] < clearance - TOLERANCE_LINEAR:
|
|
|
|
|
return 0.0
|
|
|
|
|
if other == i + 2 and components[i + 1].move_type != "straight":
|
|
|
|
|
return 0.0 # Keep the geometry local to one continuous bend together.
|
|
|
|
|
return clearance
|
|
|
|
|
|
2026-11-30 15:24:14 -08:00
|
|
|
pairs: Iterable[tuple[int, int]]
|
|
|
|
|
if len(components) <= 3:
|
|
|
|
|
# At most three pairs, each with its own bounds check.
|
|
|
|
|
pairs = combinations(range(len(components)), 2)
|
|
|
|
|
else:
|
|
|
|
|
envelopes = [
|
|
|
|
|
component.physical_geometry[0] if len(component.physical_geometry) == 1 else box(*component.total_physical_bounds)
|
|
|
|
|
for component in components
|
|
|
|
|
]
|
|
|
|
|
index = STRtree(envelopes)
|
|
|
|
|
|
|
|
|
|
def envelope_with_clearance(i: int) -> Polygon:
|
|
|
|
|
if not clearance:
|
|
|
|
|
return envelopes[i]
|
|
|
|
|
x0, y0, x1, y1 = components[i].total_physical_bounds
|
|
|
|
|
return box(x0 - clearance, y0 - clearance, x1 + clearance, y1 + clearance)
|
|
|
|
|
|
2026-11-30 19:29:51 -08:00
|
|
|
if len(components) <= 64:
|
|
|
|
|
queries = [envelope_with_clearance(i) for i in range(len(components))] if clearance else envelopes
|
|
|
|
|
left, right = index.query(queries)
|
|
|
|
|
pairs = ((i, other) for i, other in zip(left, right, strict=True) if other > i)
|
|
|
|
|
else:
|
|
|
|
|
pairs = ((i, other) for i in range(len(components)) for other in index.query(envelope_with_clearance(i)) if other > i)
|
2026-11-27 21:40:53 -08:00
|
|
|
return internal_collisions + sum(
|
2026-11-30 15:24:14 -08:00
|
|
|
components_overlap(components[i], components[other], prefer_actual=True, at_join=other == i + 1, clearance=spacing(i, other))
|
|
|
|
|
for i, other in pairs
|
2026-11-27 19:12:09 -08:00
|
|
|
)
|