more bend work; bounds constrain edges
This commit is contained in:
parent
4714bed9a8
commit
58873692d6
1 changed files with 251 additions and 124 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import NamedTuple, Literal, Union
|
||||
from typing import NamedTuple, Literal, Any
|
||||
|
||||
import numpy as np
|
||||
from shapely.geometry import Polygon, box
|
||||
|
|
@ -35,7 +35,7 @@ class Straight:
|
|||
|
||||
ex = start_port.x + dx
|
||||
ey = start_port.y + dy
|
||||
|
||||
|
||||
if snap_to_grid:
|
||||
ex = snap_search_grid(ex)
|
||||
ey = snap_search_grid(ey)
|
||||
|
|
@ -84,39 +84,83 @@ def _get_arc_polygons(cx: float, cy: float, radius: float, width: float, t_start
|
|||
|
||||
|
||||
def _apply_collision_model(
|
||||
arc_poly: Polygon,
|
||||
arc_poly: Polygon,
|
||||
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon,
|
||||
radius: float,
|
||||
width: float,
|
||||
cx: float = 0.0,
|
||||
cy: float = 0.0,
|
||||
clip_margin: float = 10.0
|
||||
) -> list[Polygon]:
|
||||
"""Applies the specified collision model to an arc geometry."""
|
||||
if isinstance(collision_type, Polygon):
|
||||
return [collision_type]
|
||||
|
||||
|
||||
if collision_type == "arc":
|
||||
return [arc_poly]
|
||||
|
||||
|
||||
# Get bounding box
|
||||
minx, miny, maxx, maxy = arc_poly.bounds
|
||||
bbox = box(minx, miny, maxx, maxy)
|
||||
|
||||
|
||||
if collision_type == "bbox":
|
||||
return [bbox]
|
||||
|
||||
|
||||
if collision_type == "clipped_bbox":
|
||||
safe_zone = arc_poly.buffer(clip_margin)
|
||||
return [bbox.intersection(safe_zone)]
|
||||
|
||||
res_poly = bbox
|
||||
|
||||
# Determine quadrant signs from arc centroid relative to center
|
||||
# This ensures we always cut 'into' the box correctly
|
||||
ac = arc_poly.centroid
|
||||
sx = 1.0 if ac.x >= cx else -1.0
|
||||
sy = 1.0 if ac.y >= cy else -1.0
|
||||
|
||||
r_out_cut = radius + width / 2.0 + clip_margin
|
||||
r_in_cut = radius - width / 2.0 - clip_margin
|
||||
|
||||
corners = [(minx, miny), (minx, maxy), (maxx, miny), (maxx, maxy)]
|
||||
for px, py in corners:
|
||||
dx, dy = px - cx, py - cy
|
||||
dist = np.sqrt(dx**2 + dy**2)
|
||||
|
||||
if dist > r_out_cut:
|
||||
# Outer corner: remove part furthest from center
|
||||
# We want minimum distance to line to be r_out_cut
|
||||
d_cut = r_out_cut * np.sqrt(2)
|
||||
elif r_in_cut > 0 and dist < r_in_cut:
|
||||
# Inner corner: remove part closest to center
|
||||
# We want maximum distance to line to be r_in_cut
|
||||
d_cut = r_in_cut
|
||||
else:
|
||||
continue
|
||||
|
||||
# The cut line is sx*(x-cx) + sy*(y-cy) = d_cut
|
||||
# sx*x + sy*y = sx*cx + sy*cy + d_cut
|
||||
val = cx * sx + cy * sy + d_cut
|
||||
|
||||
try:
|
||||
p1 = (px, py)
|
||||
p2 = (px, (val - sx * px) / sy)
|
||||
p3 = ((val - sy * py) / sx, py)
|
||||
|
||||
triangle = Polygon([p1, p2, p3])
|
||||
if triangle.is_valid and triangle.area > 1e-9:
|
||||
res_poly = res_poly.difference(triangle)
|
||||
except ZeroDivisionError:
|
||||
continue
|
||||
|
||||
return [res_poly]
|
||||
|
||||
return [arc_poly]
|
||||
|
||||
|
||||
class Bend90:
|
||||
@staticmethod
|
||||
def generate(
|
||||
start_port: Port,
|
||||
radius: float,
|
||||
width: float,
|
||||
direction: str = "CW",
|
||||
start_port: Port,
|
||||
radius: float,
|
||||
width: float,
|
||||
direction: str = "CW",
|
||||
sagitta: float = 0.01,
|
||||
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon = "arc",
|
||||
clip_margin: float = 10.0
|
||||
|
|
@ -133,9 +177,11 @@ class Bend90:
|
|||
ex = snap_search_grid(cx + radius * np.cos(t_end))
|
||||
ey = snap_search_grid(cy + radius * np.sin(t_end))
|
||||
end_port = Port(ex, ey, float((start_port.orientation + turn_angle) % 360))
|
||||
|
||||
|
||||
arc_polys = _get_arc_polygons(cx, cy, radius, width, t_start, t_end, sagitta)
|
||||
collision_polys = _apply_collision_model(arc_polys[0], collision_type, radius, clip_margin)
|
||||
collision_polys = _apply_collision_model(
|
||||
arc_polys[0], collision_type, radius, width, cx, cy, clip_margin
|
||||
)
|
||||
|
||||
return ComponentResult(geometry=collision_polys, end_port=end_port, length=radius * np.pi / 2.0)
|
||||
|
||||
|
|
@ -143,10 +189,10 @@ class Bend90:
|
|||
class SBend:
|
||||
@staticmethod
|
||||
def generate(
|
||||
start_port: Port,
|
||||
offset: float,
|
||||
radius: float,
|
||||
width: float,
|
||||
start_port: Port,
|
||||
offset: float,
|
||||
radius: float,
|
||||
width: float,
|
||||
sagitta: float = 0.01,
|
||||
collision_type: Literal["arc", "bbox", "clipped_bbox"] | Polygon = "arc",
|
||||
clip_margin: float = 10.0
|
||||
|
|
@ -162,7 +208,7 @@ class SBend:
|
|||
ex = snap_search_grid(start_port.x + dx * np.cos(rad_start) - dy * np.sin(rad_start))
|
||||
ey = snap_search_grid(start_port.y + dx * np.sin(rad_start) + dy * np.cos(rad_start))
|
||||
end_port = Port(ex, ey, start_port.orientation)
|
||||
|
||||
|
||||
direction = 1 if offset > 0 else -1
|
||||
c1_angle = rad_start + direction * np.pi / 2
|
||||
cx1 = start_port.x + radius * np.cos(c1_angle)
|
||||
|
|
@ -180,6 +226,14 @@ class SBend:
|
|||
arc1 = _get_arc_polygons(cx1, cy1, radius, width, ts1, te1, sagitta)[0]
|
||||
arc2 = _get_arc_polygons(cx2, cy2, radius, width, ts2, te2, sagitta)[0]
|
||||
combined_arc = unary_union([arc1, arc2])
|
||||
|
||||
collision_polys = _apply_collision_model(combined_arc, collision_type, radius, clip_margin)
|
||||
|
||||
if collision_type == "clipped_bbox":
|
||||
col1 = _apply_collision_model(arc1, collision_type, radius, width, cx1, cy1, clip_margin)
|
||||
col2 = _apply_collision_model(arc2, collision_type, radius, width, cx2, cy2, clip_margin)
|
||||
collision_polys = [unary_union(col1 + col2)]
|
||||
else:
|
||||
collision_polys = _apply_collision_model(
|
||||
combined_arc, collision_type, radius, width, 0, 0, clip_margin
|
||||
)
|
||||
|
||||
return ComponentResult(geometry=collision_polys, end_port=end_port, length=2 * radius * theta)
|
||||
|
|
|
|||
Loading…
Reference in a new issue