2026-03-08 14:40:36 -07:00
|
|
|
|
|
|
|
|
from inire.geometry.collision import CollisionEngine
|
|
|
|
|
from inire.geometry.primitives import Port
|
|
|
|
|
from inire.router.astar import AStarRouter
|
|
|
|
|
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 main() -> None:
|
|
|
|
|
print("Running Example 03: Locked Paths (Incremental Routing)...")
|
|
|
|
|
|
|
|
|
|
# 1. Setup Environment
|
|
|
|
|
bounds = (0, 0, 100, 100)
|
|
|
|
|
engine = CollisionEngine(clearance=2.0)
|
|
|
|
|
danger_map = DangerMap(bounds=bounds)
|
|
|
|
|
danger_map.precompute([]) # No initial obstacles
|
|
|
|
|
|
|
|
|
|
evaluator = CostEvaluator(engine, danger_map)
|
|
|
|
|
router = AStarRouter(evaluator)
|
|
|
|
|
pf = PathFinder(router, evaluator)
|
|
|
|
|
|
|
|
|
|
# 2. Phase 1: Route a "Critical" Net
|
|
|
|
|
# This net gets priority and takes the best path.
|
|
|
|
|
netlist_phase1 = {
|
|
|
|
|
"critical_net": (Port(10, 50, 0), Port(90, 50, 0)),
|
|
|
|
|
}
|
|
|
|
|
print("Phase 1: Routing critical_net...")
|
|
|
|
|
results1 = pf.route_all(netlist_phase1, {"critical_net": 3.0}) # Wider trace
|
|
|
|
|
|
|
|
|
|
if not results1["critical_net"].is_valid:
|
|
|
|
|
print("Error: Phase 1 failed.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 3. Lock the Critical Net
|
|
|
|
|
# This converts the dynamic path into a static obstacle in the collision engine.
|
|
|
|
|
print("Locking critical_net...")
|
|
|
|
|
engine.lock_net("critical_net")
|
|
|
|
|
|
|
|
|
|
# Update danger map to reflect the new obstacle (optional but recommended for heuristics)
|
|
|
|
|
# Extract polygons from result
|
|
|
|
|
path_polys = [p for comp in results1["critical_net"].path for p in comp.geometry]
|
|
|
|
|
danger_map.precompute(path_polys)
|
|
|
|
|
|
|
|
|
|
# 4. Phase 2: Route a Secondary Net
|
|
|
|
|
# This net must route *around* the locked critical_net.
|
|
|
|
|
# Start and end points force a crossing path if it were straight.
|
|
|
|
|
netlist_phase2 = {
|
|
|
|
|
"secondary_net": (Port(50, 10, 90), Port(50, 90, 90)),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print("Phase 2: Routing secondary_net around locked path...")
|
|
|
|
|
results2 = pf.route_all(netlist_phase2, {"secondary_net": 2.0})
|
|
|
|
|
|
|
|
|
|
if results2["secondary_net"].is_valid:
|
|
|
|
|
print("Success! Secondary net routed around locked path.")
|
|
|
|
|
else:
|
|
|
|
|
print("Failed to route secondary net.")
|
|
|
|
|
|
|
|
|
|
# 5. Visualize
|
2026-03-08 22:13:10 -07:00
|
|
|
# Combine results and netlists for plotting
|
2026-03-08 14:40:36 -07:00
|
|
|
all_results = {**results1, **results2}
|
2026-03-08 22:13:10 -07:00
|
|
|
all_netlists = {**netlist_phase1, **netlist_phase2}
|
2026-03-08 14:40:36 -07:00
|
|
|
|
2026-03-08 22:13:10 -07:00
|
|
|
# Note: 'critical_net' is now in engine.static_obstacles internally,
|
2026-03-08 14:40:36 -07:00
|
|
|
# but for visualization we plot it from the result object to see it clearly.
|
2026-03-08 22:13:10 -07:00
|
|
|
# We pass an empty list for 'static_obstacles' to plot_routing_results
|
2026-03-08 14:40:36 -07:00
|
|
|
# because we want to see the path colored, not grayed out as an obstacle.
|
|
|
|
|
|
2026-03-08 22:13:10 -07:00
|
|
|
fig, ax = plot_routing_results(all_results, [], bounds, netlist=all_netlists)
|
2026-03-08 14:40:36 -07:00
|
|
|
fig.savefig("examples/locked.png")
|
2026-03-08 22:13:10 -07:00
|
|
|
|
2026-03-08 14:40:36 -07:00
|
|
|
print("Saved plot to examples/locked.png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|