inire/examples/03_locked_paths.py

76 lines
2.7 KiB
Python

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
# Combine results for plotting
all_results = {**results1, **results2}
# Note: 'critical_net' is now in engine.static_obstacles internally,
# but for visualization we plot it from the result object to see it clearly.
# We pass an empty list for 'static_obstacles' to plot_routing_results
# because we want to see the path colored, not grayed out as an obstacle.
fig, ax = plot_routing_results(all_results, [], bounds)
fig.savefig("examples/locked.png")
print("Saved plot to examples/locked.png")
if __name__ == "__main__":
main()