57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
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,
|
|
) -> bool:
|
|
polygons_a: Sequence[Polygon]
|
|
polygons_b: Sequence[Polygon]
|
|
if prefer_actual:
|
|
polygons_a = component_a.physical_geometry
|
|
polygons_b = component_b.physical_geometry
|
|
extents_a = tuple(polygon.bounds for polygon in polygons_a)
|
|
extents_b = tuple(polygon.bounds for polygon in polygons_b)
|
|
bounds_a = (min(b[0] for b in extents_a), min(b[1] for b in extents_a), max(b[2] for b in extents_a), max(b[3] for b in extents_a))
|
|
bounds_b = (min(b[0] for b in extents_b), min(b[1] for b in extents_b), max(b[2] for b in extents_b), max(b[3] for b in extents_b))
|
|
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 not (
|
|
bounds_a[0] < bounds_b[2]
|
|
and bounds_a[2] > bounds_b[0]
|
|
and bounds_a[1] < bounds_b[3]
|
|
and bounds_a[3] > bounds_b[1]
|
|
):
|
|
return 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 overlap.area > 0:
|
|
port = component_a.end_port
|
|
axis, position = (0, port.x) if port.r % 180 == 0 else (1, port.y)
|
|
bounds = overlap.bounds
|
|
if bounds[axis] < position - TOLERANCE_LINEAR or bounds[axis + 2] > position + TOLERANCE_LINEAR:
|
|
return True
|
|
elif polygon_a.intersects(polygon_b) and not polygon_a.touches(polygon_b):
|
|
return True
|
|
return False
|