ex07 works
This commit is contained in:
parent
d438c5b7c7
commit
8833240755
1 changed files with 272 additions and 110 deletions
|
|
@ -242,88 +242,65 @@ def _get_arc_polygons(
|
|||
|
||||
|
||||
def _clip_bbox(
|
||||
bbox: Polygon,
|
||||
cx: float,
|
||||
cy: float,
|
||||
radius: float,
|
||||
width: float,
|
||||
clip_margin: float,
|
||||
arc_poly: Polygon,
|
||||
t_start: float | None = None,
|
||||
t_end: float | None = None,
|
||||
t_start: float,
|
||||
t_end: float,
|
||||
) -> Polygon:
|
||||
"""
|
||||
Clips corners of a bounding box for better collision modeling.
|
||||
Generates a rotationally invariant bounding polygon for an arc.
|
||||
"""
|
||||
r_out_cut = radius + width / 2.0 + clip_margin
|
||||
r_in_cut = radius - width / 2.0 - clip_margin
|
||||
|
||||
# Angular range of the arc
|
||||
if t_start is not None and t_end is not None:
|
||||
ts, te = t_start, t_end
|
||||
if ts > te:
|
||||
ts, te = te, ts
|
||||
# Sweep could cross 2pi boundary
|
||||
sweep = (te - ts) % (2 * numpy.pi)
|
||||
ts_norm = ts % (2 * numpy.pi)
|
||||
sweep = abs(t_end - t_start)
|
||||
if sweep > 2 * numpy.pi:
|
||||
sweep = sweep % (2 * numpy.pi)
|
||||
|
||||
mid_angle = (t_start + t_end) / 2.0
|
||||
# Handle wrap-around for mid_angle
|
||||
if abs(t_end - t_start) > numpy.pi:
|
||||
mid_angle += numpy.pi
|
||||
|
||||
r_out = radius + width / 2.0
|
||||
r_in = max(0.0, radius - width / 2.0)
|
||||
|
||||
half_sweep = sweep / 2.0
|
||||
|
||||
# Define vertices in local space (center at 0,0, symmetry axis along +X)
|
||||
# 1. Start Inner
|
||||
# 2. Start Outer
|
||||
# 3. Peak Outer (intersection of tangents at start/end)
|
||||
# 4. End Outer
|
||||
# 5. End Inner
|
||||
# 6. Peak Inner (ensures convexity and inner clipping)
|
||||
|
||||
# Outer tangent intersection point
|
||||
# Tangent at -hs: x*cos(hs) - y*sin(hs) = r_out
|
||||
# Tangent at +hs: x*cos(hs) + y*sin(hs) = r_out
|
||||
# Intersection: y=0, x = r_out / cos(hs)
|
||||
cos_hs = numpy.cos(half_sweep)
|
||||
if cos_hs < 1e-3: # Sweep near 180 deg
|
||||
peak_out_x = r_out * 2.0 # Fallback
|
||||
else:
|
||||
# Fallback: assume 90 deg based on centroid quadrant
|
||||
ac = arc_poly.centroid
|
||||
mid_angle = numpy.arctan2(ac.y - cy, ac.x - cx)
|
||||
ts_norm = (mid_angle - numpy.pi/4) % (2 * numpy.pi)
|
||||
sweep = numpy.pi/2
|
||||
peak_out_x = r_out / cos_hs
|
||||
|
||||
minx, miny, maxx, maxy = bbox.bounds
|
||||
verts = [
|
||||
numpy.array([minx, miny]),
|
||||
numpy.array([maxx, miny]),
|
||||
numpy.array([maxx, maxy]),
|
||||
numpy.array([minx, maxy])
|
||||
local_verts = [
|
||||
[r_in * numpy.cos(-half_sweep), r_in * numpy.sin(-half_sweep)],
|
||||
[r_out * numpy.cos(-half_sweep), r_out * numpy.sin(-half_sweep)],
|
||||
[peak_out_x, 0.0],
|
||||
[r_out * numpy.cos(half_sweep), r_out * numpy.sin(half_sweep)],
|
||||
[r_in * numpy.cos(half_sweep), r_in * numpy.sin(half_sweep)],
|
||||
[r_in, 0.0]
|
||||
]
|
||||
|
||||
new_verts = []
|
||||
for p in verts:
|
||||
dx, dy = p[0] - cx, p[1] - cy
|
||||
dist = numpy.sqrt(dx**2 + dy**2)
|
||||
angle = numpy.arctan2(dy, dx)
|
||||
angle_rel = (angle - ts_norm) % (2 * numpy.pi)
|
||||
is_in_sweep = angle_rel <= sweep + 1e-6
|
||||
|
||||
d_line = -1.0
|
||||
if is_in_sweep:
|
||||
# We can clip if outside R_out or inside R_in
|
||||
if dist > radius + width/2.0 - 1e-6:
|
||||
d_line = r_out_cut * numpy.sqrt(2)
|
||||
elif r_in_cut > 1e-3 and dist < radius - width/2.0 + 1e-6:
|
||||
d_line = r_in_cut
|
||||
else:
|
||||
# Corner is outside angular sweep.
|
||||
if dist > radius + width/2.0 - 1e-6:
|
||||
d_line = r_out_cut * numpy.sqrt(2)
|
||||
elif r_in_cut > 1e-3 and dist < radius - width/2.0 + 1e-6:
|
||||
d_line = r_in_cut
|
||||
|
||||
if d_line > 0:
|
||||
sx = 1.0 if dx > 0 else -1.0
|
||||
sy = 1.0 if dy > 0 else -1.0
|
||||
try:
|
||||
# Intersection of line sx*(x-cx) + sy*(y-cy) = d_line with box edges
|
||||
p_edge_x = numpy.array([p[0], cy + (d_line - sx * (p[0] - cx)) / sy])
|
||||
p_edge_y = numpy.array([cx + (d_line - sy * (p[1] - cy)) / sx, p[1]])
|
||||
|
||||
# Check if intersection points are on the box boundary
|
||||
if (minx - 1e-6 <= p_edge_y[0] <= maxx + 1e-6 and
|
||||
miny - 1e-6 <= p_edge_x[1] <= maxy + 1e-6):
|
||||
new_verts.append(p_edge_x)
|
||||
new_verts.append(p_edge_y)
|
||||
else:
|
||||
new_verts.append(p)
|
||||
except ZeroDivisionError:
|
||||
new_verts.append(p)
|
||||
else:
|
||||
new_verts.append(p)
|
||||
|
||||
return Polygon(new_verts).convex_hull
|
||||
# Rotate and translate to world space
|
||||
cos_m = numpy.cos(mid_angle)
|
||||
sin_m = numpy.sin(mid_angle)
|
||||
rot = numpy.array([[cos_m, -sin_m], [sin_m, cos_m]])
|
||||
|
||||
world_verts = (numpy.array(local_verts) @ rot.T) + [cx, cy]
|
||||
|
||||
return Polygon(world_verts)
|
||||
|
||||
|
||||
def _apply_collision_model(
|
||||
|
|
@ -346,16 +323,16 @@ def _apply_collision_model(
|
|||
if collision_type == "arc":
|
||||
return [arc_poly]
|
||||
|
||||
# Bounding box of the high-fidelity arc
|
||||
if collision_type == "clipped_bbox" and t_start is not None and t_end is not None:
|
||||
return [_clip_bbox(cx, cy, radius, width, t_start, t_end)]
|
||||
|
||||
# Bounding box of the high-fidelity arc (fallback for bbox or missing angles)
|
||||
minx, miny, maxx, maxy = arc_poly.bounds
|
||||
bbox_poly = box(minx, miny, maxx, maxy)
|
||||
|
||||
if collision_type == "bbox":
|
||||
return [bbox_poly]
|
||||
|
||||
if collision_type == "clipped_bbox":
|
||||
return [_clip_bbox(bbox_poly, cx, cy, radius, width, clip_margin, arc_poly, t_start, t_end)]
|
||||
|
||||
return [arc_poly]
|
||||
|
||||
|
||||
|
|
@ -405,8 +382,23 @@ class Bend90:
|
|||
else:
|
||||
ex, ey = ex_raw, ey_raw
|
||||
|
||||
# Slightly adjust radius to hit snapped point exactly
|
||||
# Slightly adjust radius and t_end to hit snapped point exactly
|
||||
actual_radius = numpy.sqrt((ex - cx)**2 + (ey - cy)**2)
|
||||
|
||||
t_end_snapped = numpy.arctan2(ey - cy, ex - cx)
|
||||
# Ensure directionality and approx 90 degree sweep
|
||||
if direction == "CCW":
|
||||
while t_end_snapped <= t_start:
|
||||
t_end_snapped += 2 * numpy.pi
|
||||
while t_end_snapped > t_start + numpy.pi:
|
||||
t_end_snapped -= 2 * numpy.pi
|
||||
else:
|
||||
while t_end_snapped >= t_start:
|
||||
t_end_snapped -= 2 * numpy.pi
|
||||
while t_end_snapped < t_start - numpy.pi:
|
||||
t_end_snapped += 2 * numpy.pi
|
||||
t_end = t_end_snapped
|
||||
|
||||
end_port = Port(ex, ey, new_ori)
|
||||
|
||||
arc_polys = _get_arc_polygons(cx, cy, actual_radius, width, t_start, t_end, sagitta)
|
||||
|
|
|
|||
Loading…
Reference in a new issue