inire/examples/08_custom_bend_geometry.py

57 lines
2.1 KiB
Python
Raw Normal View History

2026-03-10 21:55:54 -07:00
from shapely.geometry import Polygon
2026-03-11 09:37:54 -07:00
2026-03-10 21:55:54 -07:00
from inire.geometry.collision import CollisionEngine
from inire.geometry.primitives import Port
from inire.router.astar import AStarContext, AStarMetrics, route_astar
2026-03-10 21:55:54 -07:00
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
2026-03-11 09:37:54 -07:00
def main() -> None:
print("Running Example 08: Custom Bend Geometry...")
# 1. Setup Environment
2026-03-10 21:55:54 -07:00
bounds = (0, 0, 150, 150)
engine = CollisionEngine(clearance=2.0)
danger_map = DangerMap(bounds=bounds)
2026-03-11 09:37:54 -07:00
danger_map.precompute([])
2026-03-10 21:55:54 -07:00
2026-03-21 12:05:06 -07:00
evaluator = CostEvaluator(engine, danger_map, bend_penalty=50.0, sbend_penalty=150.0)
context = AStarContext(evaluator, snap_size=1.0, bend_radii=[10.0])
metrics = AStarMetrics()
pf = PathFinder(context, metrics)
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
# 2. Define Netlist
2026-03-10 21:55:54 -07:00
netlist = {
2026-03-11 09:37:54 -07:00
"custom_bend": (Port(20, 20, 0), Port(100, 100, 90)),
2026-03-10 21:55:54 -07:00
}
2026-03-11 09:37:54 -07:00
net_widths = {"custom_bend": 2.0}
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
# 3. Route with standard arc first
print("Routing with standard arc...")
results_std = pf.route_all(netlist, net_widths)
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
# 4. Define a custom 'trapezoid' bend model
# (Just for demonstration - we override the collision model during search)
2026-03-21 01:30:51 -07:00
# Define a custom centered 20x20 box
custom_poly = Polygon([(-10, -10), (10, -10), (10, 10), (-10, 10)])
2026-03-11 09:37:54 -07:00
print("Routing with custom collision model...")
# Override bend_collision_type with a literal Polygon
context_custom = AStarContext(evaluator, snap_size=1.0, bend_radii=[10.0], bend_collision_type=custom_poly)
metrics_custom = AStarMetrics()
results_custom = PathFinder(context_custom, metrics_custom, use_tiered_strategy=False).route_all(
2026-03-11 09:37:54 -07:00
{"custom_model": netlist["custom_bend"]}, {"custom_model": 2.0}
)
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
# 5. Visualize
all_results = {**results_std, **results_custom}
fig, ax = plot_routing_results(all_results, [], bounds, netlist=netlist)
2026-03-10 21:55:54 -07:00
fig.savefig("examples/08_custom_bend_geometry.png")
print("Saved plot to examples/08_custom_bend_geometry.png")
2026-03-11 09:37:54 -07:00
2026-03-10 21:55:54 -07:00
if __name__ == "__main__":
main()