rework structure of everything
This commit is contained in:
parent
dcc4d6436c
commit
941d3e01df
1 changed files with 3819 additions and 3559 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Literal
|
||||
|
||||
import numpy
|
||||
|
|
@ -12,61 +13,52 @@ from inire.constants import TOLERANCE_ANGULAR
|
|||
from .primitives import Port, rotation_matrix2
|
||||
|
||||
|
||||
MoveKind = Literal["straight", "bend90", "sbend"]
|
||||
BendCollisionModelName = Literal["arc", "bbox", "clipped_bbox"]
|
||||
BendCollisionModel = BendCollisionModelName | Polygon
|
||||
|
||||
|
||||
def _normalize_length(value: float) -> float:
|
||||
return float(value)
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ComponentResult:
|
||||
__slots__ = (
|
||||
"start_port",
|
||||
"geometry",
|
||||
"dilated_geometry",
|
||||
"proxy_geometry",
|
||||
"actual_geometry",
|
||||
"dilated_actual_geometry",
|
||||
"end_port",
|
||||
"length",
|
||||
"move_type",
|
||||
"_bounds",
|
||||
"_total_bounds",
|
||||
"_dilated_bounds",
|
||||
"_total_dilated_bounds",
|
||||
)
|
||||
start_port: Port
|
||||
collision_geometry: tuple[Polygon, ...]
|
||||
end_port: Port
|
||||
length: float
|
||||
move_type: MoveKind
|
||||
physical_geometry: tuple[Polygon, ...]
|
||||
dilated_collision_geometry: tuple[Polygon, ...]
|
||||
dilated_physical_geometry: tuple[Polygon, ...]
|
||||
_bounds: tuple[tuple[float, float, float, float], ...] = field(init=False, repr=False)
|
||||
_total_bounds: tuple[float, float, float, float] = field(init=False, repr=False)
|
||||
_dilated_bounds: tuple[tuple[float, float, float, float], ...] = field(init=False, repr=False)
|
||||
_total_dilated_bounds: tuple[float, float, float, float] = field(init=False, repr=False)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
start_port: Port,
|
||||
geometry: list[Polygon],
|
||||
end_port: Port,
|
||||
length: float,
|
||||
move_type: str,
|
||||
dilated_geometry: list[Polygon] | None = None,
|
||||
proxy_geometry: list[Polygon] | None = None,
|
||||
actual_geometry: list[Polygon] | None = None,
|
||||
dilated_actual_geometry: list[Polygon] | None = None,
|
||||
) -> None:
|
||||
self.start_port = start_port
|
||||
self.geometry = geometry
|
||||
self.dilated_geometry = dilated_geometry
|
||||
self.proxy_geometry = proxy_geometry
|
||||
self.actual_geometry = actual_geometry
|
||||
self.dilated_actual_geometry = dilated_actual_geometry
|
||||
self.end_port = end_port
|
||||
self.length = float(length)
|
||||
self.move_type = move_type
|
||||
def __post_init__(self) -> None:
|
||||
collision_geometry = tuple(self.collision_geometry)
|
||||
physical_geometry = tuple(self.physical_geometry)
|
||||
dilated_collision_geometry = tuple(self.dilated_collision_geometry)
|
||||
dilated_physical_geometry = tuple(self.dilated_physical_geometry)
|
||||
|
||||
self._bounds = [poly.bounds for poly in self.geometry]
|
||||
self._total_bounds = _combine_bounds(self._bounds)
|
||||
object.__setattr__(self, "collision_geometry", collision_geometry)
|
||||
object.__setattr__(self, "physical_geometry", physical_geometry)
|
||||
object.__setattr__(self, "dilated_collision_geometry", dilated_collision_geometry)
|
||||
object.__setattr__(self, "dilated_physical_geometry", dilated_physical_geometry)
|
||||
object.__setattr__(self, "length", float(self.length))
|
||||
|
||||
if self.dilated_geometry is None:
|
||||
self._dilated_bounds = None
|
||||
self._total_dilated_bounds = None
|
||||
else:
|
||||
self._dilated_bounds = [poly.bounds for poly in self.dilated_geometry]
|
||||
self._total_dilated_bounds = _combine_bounds(self._dilated_bounds)
|
||||
bounds = tuple(poly.bounds for poly in collision_geometry)
|
||||
object.__setattr__(self, "_bounds", bounds)
|
||||
object.__setattr__(self, "_total_bounds", _combine_bounds(list(bounds)))
|
||||
|
||||
dilated_bounds = tuple(poly.bounds for poly in dilated_collision_geometry)
|
||||
object.__setattr__(self, "_dilated_bounds", dilated_bounds)
|
||||
object.__setattr__(self, "_total_dilated_bounds", _combine_bounds(list(dilated_bounds)))
|
||||
|
||||
@property
|
||||
def bounds(self) -> list[tuple[float, float, float, float]]:
|
||||
def bounds(self) -> tuple[tuple[float, float, float, float], ...]:
|
||||
return self._bounds
|
||||
|
||||
@property
|
||||
|
|
@ -74,28 +66,23 @@ class ComponentResult:
|
|||
return self._total_bounds
|
||||
|
||||
@property
|
||||
def dilated_bounds(self) -> list[tuple[float, float, float, float]] | None:
|
||||
def dilated_bounds(self) -> tuple[tuple[float, float, float, float], ...]:
|
||||
return self._dilated_bounds
|
||||
|
||||
@property
|
||||
def total_dilated_bounds(self) -> tuple[float, float, float, float] | None:
|
||||
def total_dilated_bounds(self) -> tuple[float, float, float, float]:
|
||||
return self._total_dilated_bounds
|
||||
|
||||
def translate(self, dx: int | float, dy: int | float) -> ComponentResult:
|
||||
return ComponentResult(
|
||||
start_port=self.start_port + [dx, dy, 0],
|
||||
geometry=[shapely_translate(poly, dx, dy) for poly in self.geometry],
|
||||
end_port=self.end_port + [dx, dy, 0],
|
||||
start_port=self.start_port.translate(dx, dy),
|
||||
collision_geometry=[shapely_translate(poly, dx, dy) for poly in self.collision_geometry],
|
||||
end_port=self.end_port.translate(dx, dy),
|
||||
length=self.length,
|
||||
move_type=self.move_type,
|
||||
dilated_geometry=None if self.dilated_geometry is None else [shapely_translate(poly, dx, dy) for poly in self.dilated_geometry],
|
||||
proxy_geometry=None if self.proxy_geometry is None else [shapely_translate(poly, dx, dy) for poly in self.proxy_geometry],
|
||||
actual_geometry=None if self.actual_geometry is None else [shapely_translate(poly, dx, dy) for poly in self.actual_geometry],
|
||||
dilated_actual_geometry=(
|
||||
None
|
||||
if self.dilated_actual_geometry is None
|
||||
else [shapely_translate(poly, dx, dy) for poly in self.dilated_actual_geometry]
|
||||
),
|
||||
physical_geometry=[shapely_translate(poly, dx, dy) for poly in self.physical_geometry],
|
||||
dilated_collision_geometry=[shapely_translate(poly, dx, dy) for poly in self.dilated_collision_geometry],
|
||||
dilated_physical_geometry=[shapely_translate(poly, dx, dy) for poly in self.dilated_physical_geometry],
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -144,16 +131,13 @@ def _get_arc_polygons(
|
|||
return [Polygon(numpy.concatenate((inner_points, outer_points), axis=0))]
|
||||
|
||||
|
||||
def _clip_bbox(cxy: tuple[float, float], radius: float, width: float, ts: tuple[float, float], clip_margin: float) -> Polygon:
|
||||
def _clip_bbox(cxy: tuple[float, float], radius: float, width: float, ts: tuple[float, float]) -> Polygon:
|
||||
"""Return a conservative 8-point polygonal proxy for the arc.
|
||||
|
||||
The polygon uses 4 points along the outer edge and 4 along the inner edge.
|
||||
The outer edge is a circumscribed polyline and the inner edge is an
|
||||
inscribed polyline, so the result conservatively contains the true arc.
|
||||
`clip_margin` is kept for API compatibility but is not used by this proxy.
|
||||
"""
|
||||
del clip_margin
|
||||
|
||||
cx, cy = cxy
|
||||
sample_count = 4
|
||||
angle_span = abs(float(ts[1]) - float(ts[0]))
|
||||
|
|
@ -194,11 +178,10 @@ def _transform_custom_collision_polygon(
|
|||
|
||||
def _apply_collision_model(
|
||||
arc_poly: Polygon,
|
||||
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon,
|
||||
collision_type: BendCollisionModel,
|
||||
radius: float,
|
||||
width: float,
|
||||
cxy: tuple[float, float],
|
||||
clip_margin: float,
|
||||
ts: tuple[float, float],
|
||||
rotation_deg: float = 0.0,
|
||||
mirror_y: bool = False,
|
||||
|
|
@ -208,7 +191,7 @@ def _apply_collision_model(
|
|||
if collision_type == "arc":
|
||||
return [arc_poly]
|
||||
if collision_type == "clipped_bbox":
|
||||
clipped = _clip_bbox(cxy, radius, width, ts, clip_margin)
|
||||
clipped = _clip_bbox(cxy, radius, width, ts)
|
||||
return [clipped if not clipped.is_empty else box(*arc_poly.bounds)]
|
||||
return [box(*arc_poly.bounds)]
|
||||
|
||||
|
|
@ -231,7 +214,6 @@ class Straight:
|
|||
poly_points = (pts @ rot2.T) + numpy.array((start_port.x, start_port.y))
|
||||
geometry = [Polygon(poly_points)]
|
||||
|
||||
dilated_geometry = None
|
||||
if dilation > 0:
|
||||
half_w_d = half_w + dilation
|
||||
pts_d = numpy.array(
|
||||
|
|
@ -244,16 +226,18 @@ class Straight:
|
|||
)
|
||||
poly_points_d = (pts_d @ rot2.T) + numpy.array((start_port.x, start_port.y))
|
||||
dilated_geometry = [Polygon(poly_points_d)]
|
||||
else:
|
||||
dilated_geometry = geometry
|
||||
|
||||
return ComponentResult(
|
||||
start_port=start_port,
|
||||
geometry=geometry,
|
||||
collision_geometry=geometry,
|
||||
end_port=end_port,
|
||||
length=abs(length_f),
|
||||
move_type="Straight",
|
||||
dilated_geometry=dilated_geometry,
|
||||
actual_geometry=geometry,
|
||||
dilated_actual_geometry=dilated_geometry,
|
||||
move_type="straight",
|
||||
physical_geometry=geometry,
|
||||
dilated_collision_geometry=dilated_geometry,
|
||||
dilated_physical_geometry=dilated_geometry,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -265,8 +249,7 @@ class Bend90:
|
|||
width: float,
|
||||
direction: Literal["CW", "CCW"],
|
||||
sagitta: float = 0.01,
|
||||
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon = "arc",
|
||||
clip_margin: float = 10.0,
|
||||
collision_type: BendCollisionModel = "arc",
|
||||
dilation: float = 0.0,
|
||||
) -> ComponentResult:
|
||||
rot2 = rotation_matrix2(start_port.r)
|
||||
|
|
@ -290,32 +273,18 @@ class Bend90:
|
|||
radius,
|
||||
width,
|
||||
(float(center_xy[0]), float(center_xy[1])),
|
||||
clip_margin,
|
||||
ts,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign < 0),
|
||||
)
|
||||
|
||||
proxy_geometry = None
|
||||
if collision_type == "arc":
|
||||
proxy_geometry = _apply_collision_model(
|
||||
arc_polys[0],
|
||||
"clipped_bbox",
|
||||
radius,
|
||||
width,
|
||||
(float(center_xy[0]), float(center_xy[1])),
|
||||
clip_margin,
|
||||
ts,
|
||||
)
|
||||
|
||||
dilated_actual_geometry = None
|
||||
dilated_geometry = None
|
||||
physical_geometry = collision_polys if uses_custom_geometry else arc_polys
|
||||
if dilation > 0:
|
||||
if uses_custom_geometry:
|
||||
dilated_actual_geometry = [poly.buffer(dilation) for poly in collision_polys]
|
||||
dilated_geometry = dilated_actual_geometry
|
||||
dilated_physical_geometry = [poly.buffer(dilation) for poly in collision_polys]
|
||||
dilated_collision_geometry = dilated_physical_geometry
|
||||
else:
|
||||
dilated_actual_geometry = _get_arc_polygons(
|
||||
dilated_physical_geometry = _get_arc_polygons(
|
||||
(float(center_xy[0]), float(center_xy[1])),
|
||||
radius,
|
||||
width,
|
||||
|
|
@ -323,18 +292,22 @@ class Bend90:
|
|||
sagitta,
|
||||
dilation=dilation,
|
||||
)
|
||||
dilated_geometry = dilated_actual_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in collision_polys]
|
||||
dilated_collision_geometry = (
|
||||
dilated_physical_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in collision_polys]
|
||||
)
|
||||
else:
|
||||
dilated_physical_geometry = physical_geometry
|
||||
dilated_collision_geometry = collision_polys
|
||||
|
||||
return ComponentResult(
|
||||
start_port=start_port,
|
||||
geometry=collision_polys,
|
||||
collision_geometry=collision_polys,
|
||||
end_port=end_port,
|
||||
length=abs(radius) * numpy.pi / 2.0,
|
||||
move_type="Bend90",
|
||||
dilated_geometry=dilated_geometry,
|
||||
proxy_geometry=proxy_geometry,
|
||||
actual_geometry=collision_polys if uses_custom_geometry else arc_polys,
|
||||
dilated_actual_geometry=dilated_actual_geometry,
|
||||
move_type="bend90",
|
||||
physical_geometry=physical_geometry,
|
||||
dilated_collision_geometry=dilated_collision_geometry,
|
||||
dilated_physical_geometry=dilated_physical_geometry,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -346,8 +319,7 @@ class SBend:
|
|||
radius: float,
|
||||
width: float,
|
||||
sagitta: float = 0.01,
|
||||
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon = "arc",
|
||||
clip_margin: float = 10.0,
|
||||
collision_type: BendCollisionModel = "arc",
|
||||
dilation: float = 0.0,
|
||||
) -> ComponentResult:
|
||||
if abs(offset) >= 2 * radius:
|
||||
|
|
@ -383,7 +355,6 @@ class SBend:
|
|||
radius,
|
||||
width,
|
||||
(float(c1_xy[0]), float(c1_xy[1])),
|
||||
clip_margin,
|
||||
ts1,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign < 0),
|
||||
|
|
@ -394,41 +365,36 @@ class SBend:
|
|||
radius,
|
||||
width,
|
||||
(float(c2_xy[0]), float(c2_xy[1])),
|
||||
clip_margin,
|
||||
ts2,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign > 0),
|
||||
)[0],
|
||||
]
|
||||
|
||||
proxy_geometry = None
|
||||
if collision_type == "arc":
|
||||
proxy_geometry = [
|
||||
_apply_collision_model(arc1, "clipped_bbox", radius, width, (float(c1_xy[0]), float(c1_xy[1])), clip_margin, ts1)[0],
|
||||
_apply_collision_model(arc2, "clipped_bbox", radius, width, (float(c2_xy[0]), float(c2_xy[1])), clip_margin, ts2)[0],
|
||||
]
|
||||
|
||||
dilated_actual_geometry = None
|
||||
dilated_geometry = None
|
||||
physical_geometry = geometry if uses_custom_geometry else actual_geometry
|
||||
if dilation > 0:
|
||||
if uses_custom_geometry:
|
||||
dilated_actual_geometry = [poly.buffer(dilation) for poly in geometry]
|
||||
dilated_geometry = dilated_actual_geometry
|
||||
dilated_physical_geometry = [poly.buffer(dilation) for poly in geometry]
|
||||
dilated_collision_geometry = dilated_physical_geometry
|
||||
else:
|
||||
dilated_actual_geometry = [
|
||||
dilated_physical_geometry = [
|
||||
_get_arc_polygons((float(c1_xy[0]), float(c1_xy[1])), radius, width, ts1, sagitta, dilation=dilation)[0],
|
||||
_get_arc_polygons((float(c2_xy[0]), float(c2_xy[1])), radius, width, ts2, sagitta, dilation=dilation)[0],
|
||||
]
|
||||
dilated_geometry = dilated_actual_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in geometry]
|
||||
dilated_collision_geometry = (
|
||||
dilated_physical_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in geometry]
|
||||
)
|
||||
else:
|
||||
dilated_physical_geometry = physical_geometry
|
||||
dilated_collision_geometry = geometry
|
||||
|
||||
return ComponentResult(
|
||||
start_port=start_port,
|
||||
geometry=geometry,
|
||||
collision_geometry=geometry,
|
||||
end_port=end_port,
|
||||
length=2.0 * radius * theta,
|
||||
move_type="SBend",
|
||||
dilated_geometry=dilated_geometry,
|
||||
proxy_geometry=proxy_geometry,
|
||||
actual_geometry=geometry if uses_custom_geometry else actual_geometry,
|
||||
dilated_actual_geometry=dilated_actual_geometry,
|
||||
move_type="sbend",
|
||||
physical_geometry=physical_geometry,
|
||||
dilated_collision_geometry=dilated_collision_geometry,
|
||||
dilated_physical_geometry=dilated_physical_geometry,
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue