examples work

This commit is contained in:
jan 2026-03-10 21:55:54 -07:00
commit 7b0dddfe45

View file

@ -31,14 +31,20 @@ class ComponentResult:
"""
The result of a component generation: geometry, final port, and physical length.
"""
__slots__ = ('geometry', 'dilated_geometry', 'end_port', 'length', 'bounds', 'dilated_bounds')
__slots__ = ('geometry', 'dilated_geometry', 'proxy_geometry', 'actual_geometry', 'end_port', 'length', 'bounds', 'dilated_bounds', '_t_cache')
geometry: list[Polygon]
""" List of polygons representing the component geometry """
""" List of polygons representing the component geometry (could be proxy or arc) """
dilated_geometry: list[Polygon] | None
""" Optional list of pre-dilated polygons for collision optimization """
proxy_geometry: list[Polygon] | None
""" Simplified conservative proxy for tiered collision checks """
actual_geometry: list[Polygon] | None
""" High-fidelity 'actual' geometry for visualization (always the arc) """
end_port: Port
""" The final port after the component """
@ -51,40 +57,76 @@ class ComponentResult:
dilated_bounds: numpy.ndarray | None
""" Pre-calculated bounds for each polygon in dilated_geometry """
_t_cache: dict[tuple[float, float], ComponentResult]
""" Cache for translated versions of this result """
def __init__(
self,
geometry: list[Polygon],
end_port: Port,
length: float,
dilated_geometry: list[Polygon] | None = None,
proxy_geometry: list[Polygon] | None = None,
actual_geometry: list[Polygon] | None = None,
skip_bounds: bool = False,
) -> None:
self.geometry = geometry
self.dilated_geometry = dilated_geometry
self.proxy_geometry = proxy_geometry
self.actual_geometry = actual_geometry
self.end_port = end_port
self.length = length
# Vectorized bounds calculation
self.bounds = shapely.bounds(geometry)
self.dilated_bounds = shapely.bounds(dilated_geometry) if dilated_geometry is not None else None
self._t_cache = {}
if not skip_bounds:
# Vectorized bounds calculation
self.bounds = shapely.bounds(geometry)
self.dilated_bounds = shapely.bounds(dilated_geometry) if dilated_geometry is not None else None
def translate(self, dx: float, dy: float) -> ComponentResult:
"""
Create a new ComponentResult translated by (dx, dy).
"""
dxr, dyr = round(dx, 3), round(dy, 3)
if (dxr, dyr) == (0.0, 0.0):
return self
if (dxr, dyr) in self._t_cache:
return self._t_cache[(dxr, dyr)]
# Vectorized translation if possible, else list comp
# Shapely 2.x affinity functions still work on single geometries efficiently
geoms = list(self.geometry)
num_geom = len(self.geometry)
offsets = [num_geom]
if self.dilated_geometry is not None:
geoms.extend(self.dilated_geometry)
offsets.append(len(geoms))
if self.proxy_geometry is not None:
geoms.extend(self.proxy_geometry)
offsets.append(len(geoms))
if self.actual_geometry is not None:
geoms.extend(self.actual_geometry)
offsets.append(len(geoms))
from shapely.affinity import translate
translated = [translate(p, dx, dy) for p in geoms]
new_geom = translated[:num_geom]
new_dil = translated[num_geom:] if self.dilated_geometry is not None else None
new_geom = translated[:offsets[0]]
new_dil = translated[offsets[0]:offsets[1]] if self.dilated_geometry is not None else None
new_proxy = translated[offsets[1]:offsets[2]] if self.proxy_geometry is not None else None
new_actual = translated[offsets[2]:offsets[3]] if self.actual_geometry is not None else None
new_port = Port(self.end_port.x + dx, self.end_port.y + dy, self.end_port.orientation)
return ComponentResult(new_geom, new_port, self.length, new_dil)
res = ComponentResult(new_geom, new_port, self.length, new_dil, new_proxy, new_actual, skip_bounds=True)
# Optimize: reuse and translate bounds
res.bounds = self.bounds + [dx, dy, dx, dy]
if self.dilated_bounds is not None:
res.dilated_bounds = self.dilated_bounds + [dx, dy, dx, dy]
self._t_cache[(dxr, dyr)] = res
return res
@ -159,7 +201,8 @@ class Straight:
poly_points_dil = (pts_dil @ rot_matrix.T) + [start_port.x, start_port.y]
dilated_geom = [Polygon(poly_points_dil)]
return ComponentResult(geometry=geom, end_port=end_port, length=actual_length, dilated_geometry=dilated_geom)
# For straight segments, geom IS the actual geometry
return ComponentResult(geometry=geom, end_port=end_port, length=actual_length, dilated_geometry=dilated_geom, actual_geometry=geom)
def _get_num_segments(radius: float, angle_deg: float, sagitta: float = 0.01) -> int:
@ -234,20 +277,31 @@ def _clip_bbox(
width: float,
clip_margin: float,
arc_poly: Polygon,
t_start: float | None = None,
t_end: float | None = None,
) -> Polygon:
"""
Clips corners of a bounding box for better collision modeling using direct vertex manipulation.
Clips corners of a bounding box for better collision modeling.
"""
# Determination of which corners to clip
ac = arc_poly.centroid
qsx = 1.0 if ac.x >= cx else -1.0
qsy = 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
# 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)
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
minx, miny, maxx, maxy = bbox.bounds
# Initial vertices: [minx,miny], [maxx,miny], [maxx,maxy], [minx,maxy]
verts = [
numpy.array([minx, miny]),
numpy.array([maxx, miny]),
@ -259,36 +313,41 @@ def _clip_bbox(
for p in verts:
dx, dy = p[0] - cx, p[1] - cy
dist = numpy.sqrt(dx**2 + dy**2)
# Normal vector components from center to corner
sx = 1.0 if dx > 1e-6 else (-1.0 if dx < -1e-6 else qsx)
sy = 1.0 if dy > 1e-6 else (-1.0 if dy < -1e-6 else qsy)
angle = numpy.arctan2(dy, dx)
# Check if corner angle is within the arc's angular sweep
angle_rel = (angle - ts_norm) % (2 * numpy.pi)
is_in_sweep = angle_rel <= sweep + 1e-6
d_line = -1.0
if dist > r_out_cut:
d_line = r_out_cut * numpy.sqrt(2)
elif r_in_cut > 0 and dist < r_in_cut:
d_line = r_in_cut
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:
# This corner needs clipping. Replace one vertex with two at intersection of line and edges.
# Line: sx*(x-cx) + sy*(y-cy) = d_line
# Edge x=px: y = cy + (d_line - sx*(px-cx))/sy
# Edge y=py: x = cx + (d_line - sy*(py-cy))/sx
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]])
# Order matters for polygon winding.
# If we are at [minx, miny] and moving CCW towards [maxx, miny]:
# If we clip this corner, we should add p_edge_y then p_edge_x (or vice versa depending on orientation)
# For simplicity, we can just add both and let Polygon sort it out if it's convex,
# but better to be precise.
# Since we know the bounding box orientation, we can determine order.
# BUT: Difference was safer. Let's try a simpler approach:
# Just collect all possible vertices and use convex_hull if it's guaranteed convex.
# A clipped bbox is always convex.
new_verts.append(p_edge_x)
new_verts.append(p_edge_y)
# 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:
@ -305,6 +364,8 @@ def _apply_collision_model(
cx: float = 0.0,
cy: float = 0.0,
clip_margin: float = 10.0,
t_start: float | None = None,
t_end: float | None = None,
) -> list[Polygon]:
"""
Applies the specified collision model to an arc geometry.
@ -316,6 +377,7 @@ def _apply_collision_model(
width: Waveguide width.
cx, cy: Arc center.
clip_margin: Safety margin for clipping.
t_start, t_end: Arc angles.
Returns:
List of polygons representing the collision model.
@ -334,7 +396,7 @@ def _apply_collision_model(
return [bbox]
if collision_type == "clipped_bbox":
return [_clip_bbox(bbox, cx, cy, radius, width, clip_margin, arc_poly)]
return [_clip_bbox(bbox, cx, cy, radius, width, clip_margin, arc_poly, t_start, t_end)]
return [arc_poly]
@ -356,50 +418,74 @@ class Bend90:
) -> ComponentResult:
"""
Generate a 90-degree bend.
Args:
start_port: Port to start from.
radius: Bend radius.
width: Waveguide width.
direction: "CW" or "CCW".
sagitta: Geometric fidelity.
collision_type: Collision model.
clip_margin: Margin for clipped_bbox.
dilation: Optional dilation distance for pre-calculating collision geometry.
Returns:
A ComponentResult containing the bend.
"""
turn_angle = -90 if direction == "CW" else 90
rad_start = numpy.radians(start_port.orientation)
c_angle = rad_start + (numpy.pi / 2 if direction == "CCW" else -numpy.pi / 2)
cx = start_port.x + radius * numpy.cos(c_angle)
cy = start_port.y + radius * numpy.sin(c_angle)
t_start = c_angle + numpy.pi
t_end = t_start + (numpy.pi / 2 if direction == "CCW" else -numpy.pi / 2)
# Initial guess for center
cx_init = start_port.x + radius * numpy.cos(c_angle)
cy_init = start_port.y + radius * numpy.sin(c_angle)
t_start_init = c_angle + numpy.pi
t_end_init = t_start_init + (numpy.pi / 2 if direction == "CCW" else -numpy.pi / 2)
ex = snap_search_grid(cx + radius * numpy.cos(t_end))
ey = snap_search_grid(cy + radius * numpy.sin(t_end))
# Snap the target point
ex = snap_search_grid(cx_init + radius * numpy.cos(t_end_init))
ey = snap_search_grid(cy_init + radius * numpy.sin(t_end_init))
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)
# Adjust geometry to perfectly hit snapped port
dx = ex - start_port.x
dy = ey - start_port.y
dist = numpy.sqrt(dx**2 + dy**2)
# New radius for the right triangle connecting start to end with 90 deg
actual_radius = dist / numpy.sqrt(2)
# Vector from start to end
mid_x, mid_y = (start_port.x + ex)/2, (start_port.y + ey)/2
# Normal vector (orthogonal to start->end)
# Flip direction based on CW/CCW
dir_sign = 1 if direction == "CCW" else -1
cx = mid_x - dir_sign * (ey - start_port.y) / 2
cy = mid_y + dir_sign * (ex - start_port.x) / 2
# Update angles based on new center
t_start = numpy.arctan2(start_port.y - cy, start_port.x - cx)
t_end = numpy.arctan2(ey - cy, ex - cx)
# Maintain directionality and angular span near pi/2
if direction == "CCW":
while t_end < t_start: t_end += 2 * numpy.pi
else:
while t_end > t_start: t_end -= 2 * numpy.pi
arc_polys = _get_arc_polygons(cx, cy, actual_radius, width, t_start, t_end, sagitta)
collision_polys = _apply_collision_model(
arc_polys[0], collision_type, radius, width, cx, cy, clip_margin
arc_polys[0], collision_type, actual_radius, width, cx, cy, clip_margin, t_start, t_end
)
proxy_geom = None
if collision_type == "arc":
# Auto-generate a clipped_bbox proxy for tiered collision checks
proxy_geom = _apply_collision_model(
arc_polys[0], "clipped_bbox", actual_radius, width, cx, cy, clip_margin, t_start, t_end
)
dilated_geom = None
if dilation > 0:
if collision_type == "arc":
dilated_geom = _get_arc_polygons(cx, cy, radius, width, t_start, t_end, sagitta, dilation=dilation)
dilated_geom = _get_arc_polygons(cx, cy, actual_radius, width, t_start, t_end, sagitta, dilation=dilation)
else:
# For bbox or clipped_bbox, buffer the model itself (which is simpler than buffering the high-fidelity arc)
dilated_geom = [p.buffer(dilation) for p in collision_polys]
return ComponentResult(
geometry=collision_polys,
end_port=end_port,
length=radius * numpy.pi / 2.0,
dilated_geometry=dilated_geom
length=actual_radius * numpy.abs(t_end - t_start),
dilated_geometry=dilated_geom,
proxy_geometry=proxy_geom,
actual_geometry=arc_polys
)
@ -420,65 +506,61 @@ class SBend:
) -> ComponentResult:
"""
Generate a parametric S-bend (two tangent arcs).
Args:
start_port: Port to start from.
offset: Lateral offset.
radius: Arc radii.
width: Waveguide width.
sagitta: Geometric fidelity.
collision_type: Collision model.
clip_margin: Margin for clipped_bbox.
dilation: Optional dilation distance for pre-calculating collision geometry.
Returns:
A ComponentResult containing the S-bend.
"""
if abs(offset) >= 2 * radius:
raise ValueError(f"SBend offset {offset} must be less than 2*radius {2 * radius}")
theta = numpy.arccos(1 - abs(offset) / (2 * radius))
dx = 2 * radius * numpy.sin(theta)
dy = offset
theta_init = numpy.arccos(1 - abs(offset) / (2 * radius))
dx_init = 2 * radius * numpy.sin(theta_init)
rad_start = numpy.radians(start_port.orientation)
ex = snap_search_grid(start_port.x + dx * numpy.cos(rad_start) - dy * numpy.sin(rad_start))
ey = snap_search_grid(start_port.y + dx * numpy.sin(rad_start) + dy * numpy.cos(rad_start))
# Snap the target point
ex = snap_search_grid(start_port.x + dx_init * numpy.cos(rad_start) - offset * numpy.sin(rad_start))
ey = snap_search_grid(start_port.y + dx_init * numpy.sin(rad_start) + offset * numpy.cos(rad_start))
end_port = Port(ex, ey, start_port.orientation)
direction = 1 if offset > 0 else -1
# Solve for theta and radius that hit (ex, ey) exactly
local_dx = (ex - start_port.x) * numpy.cos(rad_start) + (ey - start_port.y) * numpy.sin(rad_start)
local_dy = -(ex - start_port.x) * numpy.sin(rad_start) + (ey - start_port.y) * numpy.cos(rad_start)
# tan(theta / 2) = local_dy / local_dx
theta = 2 * numpy.arctan2(abs(local_dy), local_dx)
# Avoid division by zero if theta is 0 (though unlikely due to offset check)
actual_radius = abs(local_dy) / (2 * (1 - numpy.cos(theta))) if theta > 1e-9 else radius
direction = 1 if local_dy > 0 else -1
c1_angle = rad_start + direction * numpy.pi / 2
cx1 = start_port.x + radius * numpy.cos(c1_angle)
cy1 = start_port.y + radius * numpy.sin(c1_angle)
cx1 = start_port.x + actual_radius * numpy.cos(c1_angle)
cy1 = start_port.y + actual_radius * numpy.sin(c1_angle)
ts1, te1 = c1_angle + numpy.pi, c1_angle + numpy.pi + direction * theta
ex_raw = start_port.x + dx * numpy.cos(rad_start) - dy * numpy.sin(rad_start)
ey_raw = start_port.y + dx * numpy.sin(rad_start) + dy * numpy.cos(rad_start)
c2_angle = rad_start - direction * numpy.pi / 2
cx2 = ex_raw + radius * numpy.cos(c2_angle)
cy2 = ey_raw + radius * numpy.sin(c2_angle)
cx2 = ex + actual_radius * numpy.cos(c2_angle)
cy2 = ey + actual_radius * numpy.sin(c2_angle)
te2 = c2_angle + numpy.pi
ts2 = te2 + direction * theta
arc1 = _get_arc_polygons(cx1, cy1, radius, width, ts1, te1, sagitta)[0]
arc2 = _get_arc_polygons(cx2, cy2, radius, width, ts2, te2, sagitta)[0]
arc1 = _get_arc_polygons(cx1, cy1, actual_radius, width, ts1, te1, sagitta)[0]
arc2 = _get_arc_polygons(cx2, cy2, actual_radius, width, ts2, te2, sagitta)[0]
arc_polys = [arc1, arc2]
if collision_type == "clipped_bbox":
col1 = _apply_collision_model(arc1, collision_type, radius, width, cx1, cy1, clip_margin)[0]
col2 = _apply_collision_model(arc2, collision_type, radius, width, cx2, cy2, clip_margin)[0]
# Optimization: keep as list instead of unary_union for search efficiency
collision_polys = [col1, col2]
else:
# For other models, we can either combine or keep separate.
# Keeping separate is generally better for CollisionEngine.
col1 = _apply_collision_model(arc1, collision_type, radius, width, cx1, cy1, clip_margin)[0]
col2 = _apply_collision_model(arc2, collision_type, radius, width, cx2, cy2, clip_margin)[0]
collision_polys = [col1, col2]
# Use the provided collision model for primary geometry
col1 = _apply_collision_model(arc1, collision_type, actual_radius, width, cx1, cy1, clip_margin, ts1, te1)[0]
col2 = _apply_collision_model(arc2, collision_type, actual_radius, width, cx2, cy2, clip_margin, ts2, te2)[0]
collision_polys = [col1, col2]
proxy_geom = None
if collision_type == "arc":
# Auto-generate proxies
p1 = _apply_collision_model(arc1, "clipped_bbox", actual_radius, width, cx1, cy1, clip_margin, ts1, te1)[0]
p2 = _apply_collision_model(arc2, "clipped_bbox", actual_radius, width, cx2, cy2, clip_margin, ts2, te2)[0]
proxy_geom = [p1, p2]
dilated_geom = None
if dilation > 0:
if collision_type == "arc":
d1 = _get_arc_polygons(cx1, cy1, radius, width, ts1, te1, sagitta, dilation=dilation)[0]
d2 = _get_arc_polygons(cx2, cy2, radius, width, ts2, te2, sagitta, dilation=dilation)[0]
d1 = _get_arc_polygons(cx1, cy1, actual_radius, width, ts1, te1, sagitta, dilation=dilation)[0]
d2 = _get_arc_polygons(cx2, cy2, actual_radius, width, ts2, te2, sagitta, dilation=dilation)[0]
dilated_geom = [d1, d2]
else:
dilated_geom = [p.buffer(dilation) for p in collision_polys]
@ -486,6 +568,8 @@ class SBend:
return ComponentResult(
geometry=collision_polys,
end_port=end_port,
length=2 * radius * theta,
dilated_geometry=dilated_geom
length=2 * actual_radius * theta,
dilated_geometry=dilated_geom,
proxy_geometry=proxy_geom,
actual_geometry=arc_polys
)