fix examples
This commit is contained in:
parent
bc218a416b
commit
e11132b51d
1 changed files with 406 additions and 101 deletions
|
|
@ -134,7 +134,21 @@ 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]) -> Polygon:
|
||||
def _clip_bbox_legacy(
|
||||
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
|
||||
|
||||
|
||||
def _clip_bbox_polygonal(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.
|
||||
|
|
@ -165,6 +179,18 @@ def _clip_bbox(cxy: tuple[float, float], radius: float, width: float, ts: tuple[
|
|||
return Polygon(numpy.concatenate((outer_points, inner_points), axis=0))
|
||||
|
||||
|
||||
def _clip_bbox(
|
||||
cxy: tuple[float, float],
|
||||
radius: float,
|
||||
width: float,
|
||||
ts: tuple[float, float],
|
||||
clip_margin: float | None,
|
||||
) -> Polygon:
|
||||
if clip_margin is not None:
|
||||
return _clip_bbox_legacy(cxy, radius, width, ts, clip_margin)
|
||||
return _clip_bbox_polygonal(cxy, radius, width, ts)
|
||||
|
||||
|
||||
def _transform_custom_collision_polygon(
|
||||
collision_poly: Polygon,
|
||||
cxy: tuple[float, float],
|
||||
|
|
@ -186,6 +212,7 @@ def _apply_collision_model(
|
|||
width: float,
|
||||
cxy: tuple[float, float],
|
||||
ts: tuple[float, float],
|
||||
clip_margin: float | None = None,
|
||||
rotation_deg: float = 0.0,
|
||||
mirror_y: bool = False,
|
||||
) -> list[Polygon]:
|
||||
|
|
@ -194,7 +221,7 @@ def _apply_collision_model(
|
|||
if collision_type == "arc":
|
||||
return [arc_poly]
|
||||
if collision_type == "clipped_bbox":
|
||||
clipped = _clip_bbox(cxy, radius, width, ts)
|
||||
clipped = _clip_bbox(cxy, radius, width, ts, clip_margin)
|
||||
return [clipped if not clipped.is_empty else box(*arc_poly.bounds)]
|
||||
return [box(*arc_poly.bounds)]
|
||||
|
||||
|
|
@ -254,11 +281,11 @@ class Bend90:
|
|||
direction: Literal["CW", "CCW"],
|
||||
sagitta: float = 0.01,
|
||||
collision_type: BendCollisionModel = "arc",
|
||||
clip_margin: float | None = None,
|
||||
dilation: float = 0.0,
|
||||
) -> 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))
|
||||
|
|
@ -278,27 +305,24 @@ class Bend90:
|
|||
width,
|
||||
(float(center_xy[0]), float(center_xy[1])),
|
||||
ts,
|
||||
clip_margin=clip_margin,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign < 0),
|
||||
)
|
||||
|
||||
physical_geometry = collision_polys if uses_custom_geometry else arc_polys
|
||||
physical_geometry = arc_polys
|
||||
if dilation > 0:
|
||||
if uses_custom_geometry:
|
||||
dilated_physical_geometry = [poly.buffer(dilation) for poly in collision_polys]
|
||||
dilated_collision_geometry = dilated_physical_geometry
|
||||
else:
|
||||
dilated_physical_geometry = _get_arc_polygons(
|
||||
(float(center_xy[0]), float(center_xy[1])),
|
||||
radius,
|
||||
width,
|
||||
ts,
|
||||
sagitta,
|
||||
dilation=dilation,
|
||||
)
|
||||
dilated_collision_geometry = (
|
||||
dilated_physical_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in collision_polys]
|
||||
)
|
||||
dilated_physical_geometry = _get_arc_polygons(
|
||||
(float(center_xy[0]), float(center_xy[1])),
|
||||
radius,
|
||||
width,
|
||||
ts,
|
||||
sagitta,
|
||||
dilation=dilation,
|
||||
)
|
||||
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
|
||||
|
|
@ -325,13 +349,13 @@ class SBend:
|
|||
width: float,
|
||||
sagitta: float = 0.01,
|
||||
collision_type: BendCollisionModel = "arc",
|
||||
clip_margin: float | None = None,
|
||||
dilation: float = 0.0,
|
||||
) -> ComponentResult:
|
||||
if abs(offset) >= 2 * radius:
|
||||
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))
|
||||
|
|
@ -361,6 +385,7 @@ class SBend:
|
|||
width,
|
||||
(float(c1_xy[0]), float(c1_xy[1])),
|
||||
ts1,
|
||||
clip_margin=clip_margin,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign < 0),
|
||||
)[0],
|
||||
|
|
@ -371,24 +396,21 @@ class SBend:
|
|||
width,
|
||||
(float(c2_xy[0]), float(c2_xy[1])),
|
||||
ts2,
|
||||
clip_margin=clip_margin,
|
||||
rotation_deg=float(start_port.r),
|
||||
mirror_y=(sign > 0),
|
||||
)[0],
|
||||
]
|
||||
|
||||
physical_geometry = geometry if uses_custom_geometry else actual_geometry
|
||||
physical_geometry = actual_geometry
|
||||
if dilation > 0:
|
||||
if uses_custom_geometry:
|
||||
dilated_physical_geometry = [poly.buffer(dilation) for poly in geometry]
|
||||
dilated_collision_geometry = dilated_physical_geometry
|
||||
else:
|
||||
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_collision_geometry = (
|
||||
dilated_physical_geometry if collision_type == "arc" else [poly.buffer(dilation) for poly in 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_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
|
||||
|
|
|
|||
Loading…
Reference in a new issue