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 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 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 else: at_join = False 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 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 )