128 lines
5.1 KiB
Python
128 lines
5.1 KiB
Python
from __future__ import annotations
|
|
|
|
from itertools import accumulate, combinations
|
|
from typing import TYPE_CHECKING
|
|
|
|
from shapely.geometry import box
|
|
from shapely.strtree import STRtree
|
|
|
|
from inire.constants import TOLERANCE_LINEAR
|
|
from inire.geometry.components import components_join
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
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,
|
|
) -> bool:
|
|
polygons_a: Sequence[Polygon]
|
|
polygons_b: Sequence[Polygon]
|
|
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
|
|
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]
|
|
):
|
|
return False
|
|
|
|
if at_join:
|
|
port_a, port_b = component_a.end_port, component_b.start_port
|
|
axis = 0 if port_a.r % 180 == 0 else 1
|
|
normal, lateral = (port_a.x, port_a.y) if axis == 0 else (port_a.y, port_a.x)
|
|
face_a = _face_span(component_a, arrival=True)
|
|
face_b = _face_span(component_b, arrival=False)
|
|
if port_a != port_b or face_a is None or face_b is None:
|
|
at_join = False
|
|
else:
|
|
lo = lateral + max(face_a[0], face_b[0])
|
|
hi = lateral + min(face_a[1], face_b[1])
|
|
|
|
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))
|
|
):
|
|
return True
|
|
return False
|
|
|
|
|
|
def _face_span(component: ComponentResult, *, arrival: bool) -> tuple[float, float] | None:
|
|
if component.standard_faces and component.width is not None:
|
|
return -component.width / 2, component.width / 2
|
|
face = component.port_faces[int(arrival)]
|
|
return (face[0], face[1]) if face is not None else None
|
|
|
|
|
|
def invalid_component_join(left: ComponentResult, right: ComponentResult) -> bool:
|
|
return not components_join(left, right) or components_overlap(left, right, prefer_actual=True, at_join=True)
|
|
|
|
|
|
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
|
|
envelopes = [
|
|
component.physical_geometry[0] if len(component.physical_geometry) == 1 else box(*component.total_physical_bounds)
|
|
for component in components
|
|
]
|
|
index = STRtree(envelopes)
|
|
distances = list(accumulate((component.length for component in components), initial=0.0)) if clearance else ()
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
return internal_collisions + sum(
|
|
components_overlap(component, components[other], prefer_actual=True, at_join=other == i + 1, clearance=spacing(i, other))
|
|
for i, component in enumerate(components)
|
|
for other in index.query(envelope_with_clearance(i))
|
|
if other > i
|
|
)
|