inire/examples/08_custom_bend_geometry.py

62 lines
2.4 KiB
Python

from shapely.geometry import Polygon
from inire.geometry.collision import CollisionEngine
from inire.geometry.primitives import Port
from inire.router.astar import AStarContext, AStarMetrics
from inire.router.cost import CostEvaluator
from inire.router.danger_map import DangerMap
from inire.router.pathfinder import PathFinder
from inire.utils.visualization import plot_routing_results
def _route_with_context(
context: AStarContext,
metrics: AStarMetrics,
netlist: dict[str, tuple[Port, Port]],
net_widths: dict[str, float],
) -> dict[str, object]:
return PathFinder(context, metrics, use_tiered_strategy=False).route_all(netlist, net_widths)
def main() -> None:
print("Running Example 08: Custom Bend Geometry...")
# 1. Setup Environment
bounds = (0, 0, 150, 150)
# 2. Define Netlist
netlist = {
"custom_bend": (Port(20, 20, 0), Port(100, 100, 90)),
}
net_widths = {"custom_bend": 2.0}
def build_context(bend_collision_type: object = "arc") -> tuple[AStarContext, AStarMetrics]:
engine = CollisionEngine(clearance=2.0)
danger_map = DangerMap(bounds=bounds)
danger_map.precompute([])
evaluator = CostEvaluator(engine, danger_map, bend_penalty=50.0, sbend_penalty=150.0)
return AStarContext(evaluator, bend_radii=[10.0], bend_collision_type=bend_collision_type, sbend_radii=[]), AStarMetrics()
# 3. Route with standard arc first
print("Routing with standard arc...")
context_std, metrics_std = build_context()
results_std = _route_with_context(context_std, metrics_std, netlist, net_widths)
# 4. Define a custom Manhattan 90-degree bend proxy in bend-local coordinates.
# The polygon origin is the bend center. It is mirrored for CW bends and
# rotated with the bend orientation before being translated into place.
custom_poly = Polygon([(0, -11), (11, -11), (11, 0), (9, 0), (9, -9), (0, -9)])
print("Routing with custom bend geometry...")
context_custom, metrics_custom = build_context(custom_poly)
results_custom = _route_with_context(context_custom, metrics_custom, {"custom_model": netlist["custom_bend"]}, {"custom_model": 2.0})
# 5. Visualize
all_results = {**results_std, **results_custom}
fig, ax = plot_routing_results(all_results, [], bounds, netlist=netlist)
fig.savefig("examples/08_custom_bend_geometry.png")
print("Saved plot to examples/08_custom_bend_geometry.png")
if __name__ == "__main__":
main()