inire/examples/08_custom_bend_geometry.py

79 lines
2.5 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-30 21:22:20 -07:00
from inire import CongestionOptions, NetSpec, RoutingOptions, RoutingProblem, SearchOptions
from inire.geometry.collision import RoutingWorld
2026-03-10 21:55:54 -07:00
from inire.geometry.primitives import Port
2026-03-30 21:22:20 -07:00
from inire.router._astar_types import AStarContext, AStarMetrics
from inire.router._router import PathFinder
from inire.router.cost import CostEvaluator
from inire.router.danger_map import DangerMap
2026-03-10 21:55:54 -07:00
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...")
2026-03-10 21:55:54 -07:00
bounds = (0, 0, 150, 150)
2026-03-30 21:22:20 -07:00
engine = RoutingWorld(clearance=2.0)
danger_map = DangerMap(bounds=bounds)
danger_map.precompute([])
evaluator = CostEvaluator(engine, danger_map, bend_penalty=50.0, sbend_penalty=150.0)
metrics = AStarMetrics()
2026-03-30 15:32:29 -07:00
start = Port(20, 20, 0)
target = Port(100, 100, 90)
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
print("Routing with standard arc...")
2026-03-30 21:22:20 -07:00
results_std = PathFinder(
AStarContext(
evaluator,
RoutingProblem(
bounds=bounds,
nets=(NetSpec("custom_bend", start, target, width=2.0),),
),
RoutingOptions(
search=SearchOptions(bend_radii=(10.0,), sbend_radii=()),
congestion=CongestionOptions(max_iterations=1),
),
),
metrics=metrics,
).route_all()
2026-03-10 21:55:54 -07:00
2026-03-30 21:22:20 -07:00
custom_poly = Polygon([(-10, -10), (10, -10), (10, 10), (-10, 10)])
2026-03-30 21:22:20 -07:00
print("Routing with custom collision model...")
results_custom = PathFinder(
AStarContext(
evaluator,
RoutingProblem(
bounds=bounds,
nets=(NetSpec("custom_model", start, target, width=2.0),),
),
RoutingOptions(
search=SearchOptions(
bend_radii=(10.0,),
bend_collision_type=custom_poly,
sbend_radii=(),
),
congestion=CongestionOptions(max_iterations=1, use_tiered_strategy=False),
),
),
metrics=AStarMetrics(),
use_tiered_strategy=False,
).route_all()
2026-03-10 21:55:54 -07:00
2026-03-11 09:37:54 -07:00
all_results = {**results_std, **results_custom}
2026-03-30 15:32:29 -07:00
fig, _ax = plot_routing_results(
all_results,
[],
bounds,
netlist={
"custom_bend": (start, target),
"custom_model": (start, target),
},
)
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()