more fixes and refactoring
This commit is contained in:
parent
f2b2bf22f9
commit
0c432bd229
1 changed files with 1207 additions and 611 deletions
|
|
@ -3,6 +3,8 @@ from __future__ import annotations
|
|||
from typing import Literal
|
||||
|
||||
import numpy
|
||||
from shapely.affinity import rotate as shapely_rotate
|
||||
from shapely.affinity import scale as shapely_scale
|
||||
from shapely.affinity import translate as shapely_translate
|
||||
from shapely.geometry import Polygon, box
|
||||
|
||||
|
|
@ -135,11 +137,51 @@ def _get_arc_polygons(
|
|||
|
||||
|
||||
def _clip_bbox(cxy: tuple[float, float], radius: float, width: float, ts: tuple[float, float], clip_margin: float) -> Polygon:
|
||||
arc_poly = _get_arc_polygons(cxy, radius, width, ts)[0]
|
||||
minx, miny, maxx, maxy = arc_poly.bounds
|
||||
bbox_poly = box(minx, miny, maxx, maxy)
|
||||
shrink = min(clip_margin, max(radius, width))
|
||||
return bbox_poly.buffer(-shrink, join_style=2) if shrink > 0 else bbox_poly
|
||||
"""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]))
|
||||
if angle_span < TOLERANCE_ANGULAR:
|
||||
return box(*_get_arc_polygons(cxy, radius, width, ts)[0].bounds)
|
||||
|
||||
segment_half_angle = numpy.radians(angle_span / (2.0 * (sample_count - 1)))
|
||||
cos_half = max(float(numpy.cos(segment_half_angle)), 1e-9)
|
||||
|
||||
inner_radius = max(0.0, radius - width / 2.0)
|
||||
outer_radius = radius + width / 2.0
|
||||
tolerance = max(1e-3, radius * 1e-4)
|
||||
conservative_inner_radius = max(0.0, inner_radius * cos_half - tolerance)
|
||||
conservative_outer_radius = outer_radius / cos_half + tolerance
|
||||
|
||||
angles = numpy.radians(numpy.linspace(ts[0], ts[1], sample_count))
|
||||
cos_a = numpy.cos(angles)
|
||||
sin_a = numpy.sin(angles)
|
||||
|
||||
outer_points = numpy.column_stack((cx + conservative_outer_radius * cos_a, cy + conservative_outer_radius * sin_a))
|
||||
inner_points = numpy.column_stack((cx + conservative_inner_radius * cos_a[::-1], cy + conservative_inner_radius * sin_a[::-1]))
|
||||
return Polygon(numpy.concatenate((outer_points, inner_points), axis=0))
|
||||
|
||||
|
||||
def _transform_custom_collision_polygon(
|
||||
collision_poly: Polygon,
|
||||
cxy: tuple[float, float],
|
||||
rotation_deg: float,
|
||||
mirror_y: bool,
|
||||
) -> Polygon:
|
||||
poly = collision_poly
|
||||
if mirror_y:
|
||||
poly = shapely_scale(poly, xfact=1.0, yfact=-1.0, origin=(0.0, 0.0))
|
||||
if rotation_deg % 360:
|
||||
poly = shapely_rotate(poly, rotation_deg, origin=(0.0, 0.0), use_radians=False)
|
||||
return shapely_translate(poly, cxy[0], cxy[1])
|
||||
|
||||
|
||||
def _apply_collision_model(
|
||||
|
|
@ -150,9 +192,11 @@ def _apply_collision_model(
|
|||
cxy: tuple[float, float],
|
||||
clip_margin: float,
|
||||
ts: tuple[float, float],
|
||||
rotation_deg: float = 0.0,
|
||||
mirror_y: bool = False,
|
||||
) -> list[Polygon]:
|
||||
if isinstance(collision_type, Polygon):
|
||||
return [shapely_translate(collision_type, cxy[0], cxy[1])]
|
||||
return [_transform_custom_collision_polygon(collision_type, cxy, rotation_deg, mirror_y)]
|
||||
if collision_type == "arc":
|
||||
return [arc_poly]
|
||||
if collision_type == "clipped_bbox":
|
||||
|
|
@ -211,6 +255,7 @@ class Bend90:
|
|||
) -> ComponentResult:
|
||||
rot2 = rotation_matrix2(start_port.r)
|
||||
sign = 1 if direction == "CCW" else -1
|
||||
uses_custom_geometry = isinstance(collision_type, Polygon)
|
||||
|
||||
center_local = numpy.array((0.0, sign * radius))
|
||||
end_local = numpy.array((radius, sign * radius))
|
||||
|
|
@ -231,6 +276,8 @@ class Bend90:
|
|||
(float(center_xy[0]), float(center_xy[1])),
|
||||
clip_margin,
|
||||
ts,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign < 0),
|
||||
)
|
||||
|
||||
proxy_geometry = None
|
||||
|
|
@ -248,8 +295,12 @@ class Bend90:
|
|||
dilated_actual_geometry = None
|
||||
dilated_geometry = None
|
||||
if dilation > 0:
|
||||
dilated_actual_geometry = _get_arc_polygons((float(center_xy[0]), float(center_xy[1])), radius, width, ts, sagitta, dilation=dilation)
|
||||
dilated_geometry = dilated_actual_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in collision_polys]
|
||||
if uses_custom_geometry:
|
||||
dilated_actual_geometry = [poly.buffer(dilation) for poly in collision_polys]
|
||||
dilated_geometry = dilated_actual_geometry
|
||||
else:
|
||||
dilated_actual_geometry = _get_arc_polygons((float(center_xy[0]), float(center_xy[1])), radius, width, ts, sagitta, dilation=dilation)
|
||||
dilated_geometry = dilated_actual_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in collision_polys]
|
||||
|
||||
return ComponentResult(
|
||||
geometry=collision_polys,
|
||||
|
|
@ -258,7 +309,7 @@ class Bend90:
|
|||
move_type="Bend90",
|
||||
dilated_geometry=dilated_geometry,
|
||||
proxy_geometry=proxy_geometry,
|
||||
actual_geometry=arc_polys,
|
||||
actual_geometry=collision_polys if uses_custom_geometry else arc_polys,
|
||||
dilated_actual_geometry=dilated_actual_geometry,
|
||||
)
|
||||
|
||||
|
|
@ -279,6 +330,7 @@ class SBend:
|
|||
raise ValueError(f"SBend offset {offset} must be less than 2*radius {2 * radius}")
|
||||
|
||||
sign = 1 if offset >= 0 else -1
|
||||
uses_custom_geometry = isinstance(collision_type, Polygon)
|
||||
theta = numpy.arccos(1.0 - abs(offset) / (2.0 * radius))
|
||||
dx = 2.0 * radius * numpy.sin(theta)
|
||||
theta_deg = float(numpy.degrees(theta))
|
||||
|
|
@ -301,8 +353,28 @@ class SBend:
|
|||
arc2 = _get_arc_polygons((float(c2_xy[0]), float(c2_xy[1])), radius, width, ts2, sagitta)[0]
|
||||
actual_geometry = [arc1, arc2]
|
||||
geometry = [
|
||||
_apply_collision_model(arc1, collision_type, radius, width, (float(c1_xy[0]), float(c1_xy[1])), clip_margin, ts1)[0],
|
||||
_apply_collision_model(arc2, collision_type, radius, width, (float(c2_xy[0]), float(c2_xy[1])), clip_margin, ts2)[0],
|
||||
_apply_collision_model(
|
||||
arc1,
|
||||
collision_type,
|
||||
radius,
|
||||
width,
|
||||
(float(c1_xy[0]), float(c1_xy[1])),
|
||||
clip_margin,
|
||||
ts1,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign < 0),
|
||||
)[0],
|
||||
_apply_collision_model(
|
||||
arc2,
|
||||
collision_type,
|
||||
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
|
||||
|
|
@ -315,11 +387,15 @@ class SBend:
|
|||
dilated_actual_geometry = None
|
||||
dilated_geometry = None
|
||||
if dilation > 0:
|
||||
dilated_actual_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]
|
||||
if uses_custom_geometry:
|
||||
dilated_actual_geometry = [poly.buffer(dilation) for poly in geometry]
|
||||
dilated_geometry = dilated_actual_geometry
|
||||
else:
|
||||
dilated_actual_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]
|
||||
|
||||
return ComponentResult(
|
||||
geometry=geometry,
|
||||
|
|
@ -328,6 +404,6 @@ class SBend:
|
|||
move_type="SBend",
|
||||
dilated_geometry=dilated_geometry,
|
||||
proxy_geometry=proxy_geometry,
|
||||
actual_geometry=actual_geometry,
|
||||
actual_geometry=geometry if uses_custom_geometry else actual_geometry,
|
||||
dilated_actual_geometry=dilated_actual_geometry,
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue