inire/examples/06_bend_collision_models.py

79 lines
3.1 KiB
Python
Raw Normal View History

2026-03-09 01:48:18 -07:00
from shapely.geometry import Polygon
2026-03-30 15:32:29 -07:00
from inire import CongestionOptions, NetSpec, ObjectiveWeights, RoutingOptions, RoutingProblem, RoutingResult, SearchOptions, route
2026-03-30 23:40:29 -07:00
from inire.geometry.components import BendCollisionModel, BendPhysicalGeometry
2026-03-09 01:48:18 -07:00
from inire.geometry.primitives import Port
from inire.utils.visualization import plot_routing_results
2026-03-29 18:27:03 -07:00
def _route_scenario(
bounds: tuple[float, float, float, float],
obstacles: list[Polygon],
netlist: dict[str, tuple[Port, Port]],
widths: dict[str, float],
2026-03-30 21:22:20 -07:00
*,
2026-03-30 23:40:29 -07:00
bend_collision_type: BendCollisionModel = "arc",
bend_proxy_geometry: BendCollisionModel | None = None,
bend_physical_geometry: BendPhysicalGeometry | None = None,
2026-03-30 21:22:20 -07:00
bend_clip_margin: float | None = None,
2026-03-30 15:32:29 -07:00
) -> dict[str, RoutingResult]:
problem = RoutingProblem(
bounds=bounds,
nets=tuple(NetSpec(net_id, start, target, width=widths[net_id]) for net_id, (start, target) in netlist.items()),
static_obstacles=tuple(obstacles),
2026-03-29 18:27:03 -07:00
)
2026-03-30 15:32:29 -07:00
options = RoutingOptions(
search=SearchOptions(
bend_radii=(10.0,),
bend_collision_type=bend_collision_type,
2026-03-30 23:40:29 -07:00
bend_proxy_geometry=bend_proxy_geometry,
bend_physical_geometry=bend_physical_geometry,
2026-03-30 21:22:20 -07:00
bend_clip_margin=bend_clip_margin,
2026-03-30 15:32:29 -07:00
),
objective=ObjectiveWeights(
bend_penalty=50.0,
sbend_penalty=150.0,
),
congestion=CongestionOptions(use_tiered_strategy=False),
)
return route(problem, options=options).results_by_net
2026-03-29 18:27:03 -07:00
2026-03-09 01:48:18 -07:00
def main() -> None:
print("Running Example 06: Bend Collision Models...")
bounds = (-20, -20, 170, 170)
obs_arc = Polygon([(40, 110), (60, 110), (60, 130), (40, 130)])
obs_bbox = Polygon([(40, 60), (60, 60), (60, 80), (40, 80)])
2026-03-30 23:40:29 -07:00
obs_custom = Polygon([(40, 10), (60, 10), (60, 30), (40, 30)])
custom_bend = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
2026-03-09 01:48:18 -07:00
2026-03-30 23:40:29 -07:00
obstacles = [obs_arc, obs_bbox, obs_custom]
2026-03-09 01:48:18 -07:00
netlist_arc = {"arc_model": (Port(10, 120, 0), Port(90, 140, 90))}
netlist_bbox = {"bbox_model": (Port(10, 70, 0), Port(90, 90, 90))}
2026-03-30 23:40:29 -07:00
netlist_custom = {"custom_geometry": (Port(10, 20, 0), Port(90, 40, 90))}
2026-03-09 01:48:18 -07:00
print("Routing Scenario 1 (Arc)...")
2026-03-30 23:40:29 -07:00
res_arc = _route_scenario(bounds, obstacles, netlist_arc, {"arc_model": 2.0}, bend_collision_type="arc")
2026-03-09 01:48:18 -07:00
print("Routing Scenario 2 (BBox)...")
2026-03-30 23:40:29 -07:00
res_bbox = _route_scenario(bounds, obstacles, netlist_bbox, {"bbox_model": 2.0}, bend_collision_type="bbox")
print("Routing Scenario 3 (Custom Manhattan Geometry With Matching Proxy)...")
res_custom = _route_scenario(
2026-03-30 21:22:20 -07:00
bounds,
obstacles,
2026-03-30 23:40:29 -07:00
netlist_custom,
{"custom_geometry": 2.0},
bend_physical_geometry=custom_bend,
bend_proxy_geometry=custom_bend,
2026-03-30 21:22:20 -07:00
)
2026-03-09 01:48:18 -07:00
2026-03-30 23:40:29 -07:00
all_results = {**res_arc, **res_bbox, **res_custom}
all_netlists = {**netlist_arc, **netlist_bbox, **netlist_custom}
2026-03-09 01:48:18 -07:00
2026-03-30 15:32:29 -07:00
fig, _ax = plot_routing_results(all_results, obstacles, bounds, netlist=all_netlists)
2026-03-09 01:48:18 -07:00
fig.savefig("examples/06_bend_collision_models.png")
print("Saved plot to examples/06_bend_collision_models.png")
if __name__ == "__main__":
main()