inire/inire/geometry/component_overlap.py
2026-03-30 23:54:30 -07:00

52 lines
1.7 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
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,
) -> 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 = (
min(polygon.bounds[0] for polygon in polygons_a),
min(polygon.bounds[1] for polygon in polygons_a),
max(polygon.bounds[2] for polygon in polygons_a),
max(polygon.bounds[3] for polygon in polygons_a),
)
bounds_b = (
min(polygon.bounds[0] for polygon in polygons_b),
min(polygon.bounds[1] for polygon in polygons_b),
max(polygon.bounds[2] for polygon in polygons_b),
max(polygon.bounds[3] for polygon in polygons_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 polygon_a.intersects(polygon_b) and not polygon_a.touches(polygon_b):
return True
return False