Report coincident terminals and unusable seeds per net, update custom bend handling and verification, and cover the revised behavior with regression tests. Shorten the user and developer guides around reader tasks and keep the illustrated routing walkthrough and reference links easy to find.
368 lines
15 KiB
Python
368 lines
15 KiB
Python
import pytest
|
|
from dataclasses import FrozenInstanceError
|
|
from math import asin, cos, pi, sin, sqrt
|
|
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
|
|
|
|
import inire.geometry.components as components_module
|
|
from inire.geometry.components import Bend90, SBend, Straight
|
|
from inire.geometry.primitives import Port
|
|
from inire.geometry.sbend import sbend_grid_span
|
|
from inire.router.cost import CostEvaluator
|
|
from inire.geometry.collision import RoutingWorld
|
|
|
|
|
|
def test_straight_generation() -> None:
|
|
start = Port(0, 0, 0)
|
|
length = 10.0
|
|
width = 2.0
|
|
result = Straight.generate(start, length, width)
|
|
|
|
assert result.end_port.x == 10.0
|
|
assert result.end_port.y == 0.0
|
|
assert result.end_port.r == 0.0
|
|
assert len(result.collision_geometry) == 1
|
|
|
|
# Bounds of the polygon
|
|
minx, miny, maxx, maxy = result.collision_geometry[0].bounds
|
|
assert minx == 0.0
|
|
assert maxx == 10.0
|
|
assert miny == -1.0
|
|
assert maxy == 1.0
|
|
assert isinstance(result.collision_geometry, tuple)
|
|
|
|
|
|
def test_bend90_generation() -> None:
|
|
start = Port(0, 0, 0)
|
|
radius = 10.0
|
|
width = 2.0
|
|
|
|
# CW bend
|
|
result_cw = Bend90.generate(start, radius, width, direction="CW")
|
|
assert result_cw.end_port.x == 10.0
|
|
assert result_cw.end_port.y == -10.0
|
|
assert result_cw.end_port.r == 270.0
|
|
|
|
# CCW bend
|
|
result_ccw = Bend90.generate(start, radius, width, direction="CCW")
|
|
assert result_ccw.end_port.x == 10.0
|
|
assert result_ccw.end_port.y == 10.0
|
|
assert result_ccw.end_port.r == 90.0
|
|
|
|
|
|
@pytest.mark.parametrize("direction", ["CW", "CCW"])
|
|
@pytest.mark.parametrize("proxy", ["arc", "clipped_bbox"])
|
|
def test_fractional_bend_preserves_radius_and_extends_both_tangents(direction, proxy) -> None:
|
|
radius = 10.25
|
|
start = Port(20, 20, 90)
|
|
result = Bend90.generate(start, radius, 2, direction, collision_type=proxy, dilation=1)
|
|
end = Port(31, 31, 0) if direction == "CW" else Port(9, 31, 180)
|
|
assert result.end_port == end
|
|
bend, = result.physical_geometry
|
|
lead, = Straight.generate(start.translate(dy=-5), 5, 2).physical_geometry
|
|
tail, = Straight.generate(end, 5, 2).physical_geometry
|
|
assert bend.is_valid
|
|
assert lead.intersection(bend).length == pytest.approx(2)
|
|
assert bend.intersection(tail).length == pytest.approx(2)
|
|
assert result.collision_geometry[0].covers(result.physical_geometry[0])
|
|
assert result.dilated_physical_geometry[0].covers(result.physical_geometry[0])
|
|
assert result.length == pytest.approx(radius * pi / 2 + 1.5)
|
|
|
|
evaluator = CostEvaluator(RoutingWorld(clearance=0), bend_penalty=50)
|
|
assert evaluator.score_component(result) == pytest.approx(result.length + 50 * sqrt(10 / radius))
|
|
|
|
|
|
def test_sbend_generation() -> None:
|
|
start = Port(0, 0, 0)
|
|
offset = 5.0
|
|
radius = 10.0
|
|
width = 2.0
|
|
|
|
result = SBend.generate(start, offset, radius, width)
|
|
assert result.end_port.y == 5.0
|
|
assert result.end_port.r == 0.0
|
|
assert len(result.collision_geometry) == 2 # Optimization: returns individual arcs
|
|
first, second = result.physical_geometry
|
|
lead, = Straight.generate(Port(-5, 0, 0), 5, width).physical_geometry
|
|
tail, = Straight.generate(result.end_port, 5, width).physical_geometry
|
|
assert first.is_valid
|
|
assert second.is_valid
|
|
assert lead.intersection(first).length == pytest.approx(width)
|
|
assert first.intersection(second).length == pytest.approx(width)
|
|
assert second.intersection(tail).length == pytest.approx(width)
|
|
|
|
# Verify failure for large offset
|
|
with pytest.raises(ValueError, match="offset must be a nonzero integer with magnitude less than twice the radius"):
|
|
SBend.generate(start, 25.0, 10.0, 2.0)
|
|
|
|
|
|
def test_sbend_generation_negative_offset_keeps_second_arc_below_centerline() -> None:
|
|
start = Port(0, 0, 0)
|
|
offset = -5.0
|
|
radius = 10.0
|
|
width = 2.0
|
|
|
|
result = SBend.generate(start, offset, radius, width)
|
|
|
|
assert result.end_port.y == -5.0
|
|
second_arc_minx, second_arc_miny, second_arc_maxx, second_arc_maxy = result.collision_geometry[1].bounds
|
|
assert second_arc_maxy <= width / 2.0 + 1e-6
|
|
assert second_arc_miny < -width / 2.0
|
|
|
|
|
|
@pytest.mark.parametrize("collision_type", ["arc", "clipped_bbox"])
|
|
def test_translate_reuses_shared_polygons_and_preserves_each_geometry_view(
|
|
monkeypatch: pytest.MonkeyPatch, collision_type: str
|
|
) -> None:
|
|
component = SBend.generate(Port(0, 0, 90), -5.0, 10.0, 2.0, collision_type=collision_type, dilation=1.0)
|
|
calls = []
|
|
transform = components_module.transform
|
|
|
|
def record(polygons, operation):
|
|
calls.extend(map(id, polygons))
|
|
return transform(polygons, operation)
|
|
|
|
monkeypatch.setattr(components_module, "transform", record)
|
|
dx, dy = 81.25, -31.5
|
|
shifted = component.translate(dx, dy)
|
|
|
|
roles = ("collision_geometry", "physical_geometry", "dilated_collision_geometry", "dilated_physical_geometry")
|
|
for role in roles:
|
|
original = getattr(component, role)
|
|
assert tuple(poly.wkb for poly in getattr(shifted, role)) == tuple(shapely_translate(poly, dx, dy).wkb for poly in original)
|
|
assert set(calls) == {id(poly) for role in roles for poly in getattr(component, role)}
|
|
assert len(calls) == len(set(calls))
|
|
assert shifted.start_port == component.start_port.translate(dx, dy)
|
|
assert shifted.end_port == component.end_port.translate(dx, dy)
|
|
|
|
|
|
def test_bend_collision_models() -> None:
|
|
start = Port(0, 0, 0)
|
|
radius = 10.0
|
|
width = 2.0
|
|
|
|
# 1. BBox model
|
|
res_bbox = Bend90.generate(start, radius, width, direction="CCW", collision_type="bbox")
|
|
# Arc CCW R=10 from (0,0,0) ends at (10,10,90).
|
|
# Waveguide width is 2.0, so bbox will be slightly larger than (0,0,10,10)
|
|
minx, miny, maxx, maxy = res_bbox.collision_geometry[0].bounds
|
|
assert minx <= 0.0 + 1e-6
|
|
assert maxx >= 10.0 - 1e-6
|
|
assert miny <= 0.0 + 1e-6
|
|
assert maxy >= 10.0 - 1e-6
|
|
|
|
# 2. Clipped BBox model
|
|
res_clipped = Bend90.generate(start, radius, width, direction="CCW", collision_type="clipped_bbox")
|
|
# Conservative 8-point approximation should still be tighter than the full bbox.
|
|
assert len(res_clipped.collision_geometry[0].exterior.coords) - 1 == 8
|
|
assert res_clipped.collision_geometry[0].area < res_bbox.collision_geometry[0].area
|
|
|
|
# It should also conservatively contain the true arc.
|
|
res_arc = Bend90.generate(start, radius, width, direction="CCW", collision_type="arc")
|
|
assert res_clipped.collision_geometry[0].covers(res_arc.collision_geometry[0])
|
|
|
|
# 3. Legacy clip-margin mode should still be available when explicitly requested.
|
|
res_clipped_margin = Bend90.generate(
|
|
start,
|
|
radius,
|
|
width,
|
|
direction="CCW",
|
|
collision_type="clipped_bbox",
|
|
clip_margin=1.0,
|
|
)
|
|
assert len(res_clipped_margin.collision_geometry[0].exterior.coords) - 1 == 4
|
|
assert abs(res_clipped_margin.collision_geometry[0].area - 81.0) < 1e-6
|
|
assert res_clipped_margin.collision_geometry[0].area > res_clipped.collision_geometry[0].area
|
|
|
|
|
|
def test_custom_bend_collision_polygon_uses_local_transform() -> None:
|
|
custom_poly = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
|
|
|
|
cases = [
|
|
(Port(0, 0, 0), "CCW", (0.0, 10.0), 0.0, False),
|
|
(Port(0, 0, 0), "CW", (0.0, -10.0), 0.0, True),
|
|
(Port(0, 0, 90), "CCW", (-10.0, 0.0), 90.0, False),
|
|
]
|
|
|
|
for start, direction, center_xy, rotation_deg, mirror_y in cases:
|
|
result = Bend90.generate(start, 10.0, 2.0, direction=direction, collision_type=custom_poly)
|
|
expected = custom_poly
|
|
if mirror_y:
|
|
expected = shapely_scale(expected, xfact=1.0, yfact=-1.0, origin=(0.0, 0.0))
|
|
if rotation_deg:
|
|
expected = shapely_rotate(expected, rotation_deg, origin=(0.0, 0.0), use_radians=False)
|
|
expected = shapely_translate(expected, center_xy[0], center_xy[1])
|
|
|
|
assert result.collision_geometry[0].symmetric_difference(expected).area < 1e-6
|
|
|
|
|
|
def test_custom_bend_collision_polygon_is_true_geometry() -> None:
|
|
custom_poly = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
|
|
result = Bend90.generate(
|
|
Port(0, 0, 0),
|
|
10.0,
|
|
2.0,
|
|
direction="CCW",
|
|
collision_type=custom_poly,
|
|
physical_geometry_type=custom_poly,
|
|
dilation=1.0,
|
|
)
|
|
|
|
assert result.collision_geometry[0].symmetric_difference(result.physical_geometry[0]).area < 1e-6
|
|
assert result.dilated_collision_geometry is not None
|
|
assert result.dilated_physical_geometry is not None
|
|
assert result.dilated_collision_geometry[0].symmetric_difference(result.dilated_physical_geometry[0]).area < 1e-6
|
|
|
|
|
|
def test_custom_bend_collision_polygon_can_differ_from_physical_geometry() -> None:
|
|
custom_proxy = Polygon([(0, -11), (11, -11), (11, 0), (0, 0)])
|
|
result = Bend90.generate(Port(0, 0, 0), 10.0, 2.0, direction="CCW", collision_type=custom_proxy, dilation=1.0)
|
|
|
|
assert result.collision_geometry[0].symmetric_difference(result.physical_geometry[0]).area > 1e-6
|
|
assert result.dilated_collision_geometry[0].symmetric_difference(result.dilated_physical_geometry[0]).area > 1e-6
|
|
|
|
|
|
@pytest.mark.parametrize("dilation", [1, 10])
|
|
def test_arc_proxy_dilation_uses_the_circular_geometry(dilation) -> None:
|
|
outline = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
|
|
start = Port(20, 20, 0)
|
|
default = Bend90.generate(start, 10, 2, "CCW", dilation=dilation)
|
|
custom = Bend90.generate(start, 10, 2, "CCW", physical_geometry_type=outline, dilation=dilation)
|
|
|
|
assert custom.collision_geometry == default.collision_geometry
|
|
assert custom.dilated_collision_geometry == default.dilated_collision_geometry
|
|
assert custom.dilated_physical_geometry == tuple(shape.buffer(dilation) for shape in custom.physical_geometry)
|
|
assert custom.dilated_physical_geometry != custom.dilated_collision_geometry
|
|
|
|
|
|
@pytest.mark.parametrize("heading", [0, 90, 180, 270])
|
|
@pytest.mark.parametrize(("radius", "offset"), [(50, 40), (50, -40), (12.25, 11), (12.25, -11)])
|
|
def test_custom_sbend_uses_one_asymmetric_polygon_and_reaches_grid_face(heading, radius, offset) -> None:
|
|
span = sbend_grid_span(offset, radius)
|
|
profile = [(0, 0), (span // 3, offset * 0.15), (2 * span // 3, offset * 0.65), (span, offset)]
|
|
outline = Polygon([(x, y - 1) for x, y in profile] + [(x, y + 1) for x, y in reversed(profile)])
|
|
start = Port(30, 50, heading)
|
|
expected = SBend.generate(start, offset, radius, 2)
|
|
result = SBend.generate(start, offset, radius, 2, outline=outline, dilation=1)
|
|
|
|
assert result.end_port == expected.end_port
|
|
assert result.collision_geometry == result.physical_geometry
|
|
placed = shapely_translate(shapely_rotate(outline, heading, origin=(0, 0)), start.x, start.y)
|
|
assert len(result.physical_geometry) == 1
|
|
assert result.physical_geometry[0].equals(placed)
|
|
assert result.dilated_physical_geometry == result.dilated_collision_geometry
|
|
assert result.dilated_physical_geometry[0].equals(placed.buffer(1))
|
|
|
|
|
|
def test_sbend_collision_models() -> None:
|
|
start = Port(0, 0, 0)
|
|
offset = 5.0
|
|
radius = 10.0
|
|
width = 2.0
|
|
|
|
res_bbox = SBend.generate(start, offset, radius, width, collision_type="bbox")
|
|
# Geometry should be a list of individual bbox polygons for each arc
|
|
assert len(res_bbox.collision_geometry) == 2
|
|
|
|
res_arc = SBend.generate(start, offset, radius, width, collision_type="arc")
|
|
area_bbox = sum(p.area for p in res_bbox.collision_geometry)
|
|
area_arc = sum(p.area for p in res_arc.collision_geometry)
|
|
assert area_bbox > area_arc
|
|
|
|
|
|
def test_sbend_continuity() -> None:
|
|
# Verify SBend endpoints and continuity math
|
|
start = Port(10, 20, 90) # Starting facing up
|
|
offset = 4.0
|
|
radius = 20.0
|
|
width = 1.0
|
|
|
|
res = SBend.generate(start, offset, radius, width)
|
|
|
|
# Target orientation should be same as start
|
|
assert abs(res.end_port.r - 90.0) < 1e-6
|
|
|
|
# For a port at 90 deg, +offset is a shift in -x direction
|
|
assert abs(res.end_port.x - (10.0 - offset)) < 1e-6
|
|
|
|
# Geometry should be a list of valid polygons
|
|
assert len(res.collision_geometry) == 2
|
|
for p in res.collision_geometry:
|
|
assert p.is_valid
|
|
|
|
|
|
def test_arc_sagitta_precision() -> None:
|
|
# Verify that requested sagitta actually controls segment count
|
|
start = Port(0, 0, 0)
|
|
radius = 100.0 # Large radius to make sagitta significant
|
|
width = 2.0
|
|
|
|
# Coarse: 1um sagitta
|
|
res_coarse = Bend90.generate(start, radius, width, direction="CCW", sagitta=1.0)
|
|
# Fine: 0.01um (10nm) sagitta
|
|
res_fine = Bend90.generate(start, radius, width, direction="CCW", sagitta=0.01)
|
|
|
|
# Number of segments should be significantly higher for fine
|
|
# Exterior points = (segments + 1) * 2
|
|
pts_coarse = len(res_coarse.collision_geometry[0].exterior.coords)
|
|
pts_fine = len(res_fine.collision_geometry[0].exterior.coords)
|
|
|
|
assert pts_fine > pts_coarse * 2
|
|
|
|
|
|
@pytest.mark.parametrize("radius", [100, 79999, 80001, 100000])
|
|
@pytest.mark.parametrize("offset", [None, 0.5])
|
|
def test_circular_bends_detect_keepouts_inside_the_outer_edge(radius, offset) -> None:
|
|
width = radius / 200
|
|
start = Port(0, 0, 0)
|
|
if offset is None:
|
|
component = Bend90.generate(start, radius, width, direction="CCW")
|
|
centers = [(0, radius, -pi / 2 + pi / 64)]
|
|
else:
|
|
lateral = round(offset * radius)
|
|
component = SBend.generate(start, lateral, radius, width)
|
|
theta = 2 * asin(sqrt(lateral / radius) / 2)
|
|
span = sqrt(lateral * (4 * radius - lateral))
|
|
centers = [(0, radius, -pi / 2 + theta / 32), (span, lateral - radius, pi / 2 + theta / 32)]
|
|
# These points are between the old 16-segment chord and the physical
|
|
# outer edge at fine scales. The keepouts lie entirely inside the arc.
|
|
outer_probe_radius = radius + width / 2 - max(0.03, width * 0.12)
|
|
for cx, cy, angle in centers:
|
|
x, y = cx + outer_probe_radius * cos(angle), cy + outer_probe_radius * sin(angle)
|
|
world = RoutingWorld(0)
|
|
world.add_static_obstacle(box(x - 0.005, y - 0.005, x + 0.005, y + 0.005))
|
|
assert world.verify_path_report("bend", (component,)).static_collision_count == 1
|
|
|
|
|
|
def test_component_transform_invariance() -> None:
|
|
# Verify that generating at (0,0) then transforming
|
|
# is same as generating at the transformed port.
|
|
start0 = Port(0, 0, 0)
|
|
radius = 10.0
|
|
width = 2.0
|
|
|
|
res0 = Bend90.generate(start0, radius, width, direction="CCW")
|
|
|
|
# Transform: Translate (10, 10) then Rotate 90
|
|
dx, dy = 10.0, 5.0
|
|
angle = 90.0
|
|
|
|
# 1. Transform the generated geometry
|
|
p_end_transformed = res0.end_port.translate(dx, dy).rotated(angle)
|
|
|
|
# 2. Generate at transformed start
|
|
start_transformed = start0.translate(dx, dy).rotated(angle)
|
|
res_transformed = Bend90.generate(start_transformed, radius, width, direction="CCW")
|
|
|
|
assert abs(res_transformed.end_port.x - p_end_transformed.x) < 1e-6
|
|
assert abs(res_transformed.end_port.y - p_end_transformed.y) < 1e-6
|
|
assert abs(res_transformed.end_port.r - p_end_transformed.r) < 1e-6
|
|
|
|
|
|
def test_component_result_is_immutable_value_type() -> None:
|
|
result = Straight.generate(Port(0, 0, 0), 10.0, 2.0)
|
|
|
|
with pytest.raises(FrozenInstanceError):
|
|
result.length = 42.0
|