inire/inire/geometry/component_overlap.py

137 lines
5.8 KiB
Python
Raw Normal View History

2026-03-29 20:35:58 -07:00
from __future__ import annotations
from itertools import accumulate, combinations
2026-03-29 20:35:58 -07:00
from typing import TYPE_CHECKING
from shapely import box
from shapely.strtree import STRtree
from inire.constants import TOLERANCE_LINEAR
2026-03-29 20:35:58 -07:00
if TYPE_CHECKING:
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,
*,
at_join: bool = False,
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
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
if (
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
at_join = at_join and component_a.end_port == component_b.start_port
if at_join:
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
# 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
else:
at_join = False
2026-03-29 20:35:58 -07:00
for polygon_a in polygons_a:
for polygon_b in polygons_b:
if at_join:
# Sampling a bend can leave sub-tolerance slivers at its shared face.
overlap = polygon_a.intersection(polygon_b)
if not overlap.is_empty:
bounds = overlap.bounds
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
):
return True
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
def count_path_self_collisions(components: Sequence[ComponentResult], clearance: float = 0.0) -> int:
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
if len(components) < 2:
return internal_collisions
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
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)
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)
return internal_collisions + sum(
components_overlap(components[i], components[other], prefer_actual=True, at_join=other == i + 1, clearance=spacing(i, other))
for i, other in pairs
)