143 lines
5.8 KiB
Python
143 lines
5.8 KiB
Python
|
|
"""Fixtures with user-placed crossing devices and fixed access ports."""
|
||
|
|
|
||
|
|
from itertools import product
|
||
|
|
|
||
|
|
from shapely.geometry import Polygon, box
|
||
|
|
|
||
|
|
from inire import NetSpec, PathSeed, Port, RoutingProblem, SBendSeed, StraightSeed
|
||
|
|
from inire.router._waypoints import compile_waypoints
|
||
|
|
from inire.tests.routing_benchmark_case import BenchmarkCase, benchmark_options
|
||
|
|
|
||
|
|
|
||
|
|
_CROSSING_RADIUS = 25
|
||
|
|
_CROSSING_PORT_OFFSET = 14
|
||
|
|
_CROSSING_CLEARANCE = 4
|
||
|
|
|
||
|
|
|
||
|
|
def _crossing_keepout(x: int, y: int) -> Polygon:
|
||
|
|
# Recessed ports sit at the ends of access slots in a fixed 40-unit device.
|
||
|
|
# The remaining polygon reserves the crossing's core for the placed cell.
|
||
|
|
face, port = 20, _CROSSING_PORT_OFFSET
|
||
|
|
slot = _CROSSING_CLEARANCE + 2 # Unit guide half-width and access margin.
|
||
|
|
body = box(x - face, y - face, x + face, y + face)
|
||
|
|
for access in (
|
||
|
|
box(x - face, y - slot, x - port + slot, y + slot),
|
||
|
|
box(x + port - slot, y - slot, x + face, y + slot),
|
||
|
|
box(x - slot, y - face, x + slot, y - port + slot),
|
||
|
|
box(x - slot, y + port - slot, x + slot, y + face),
|
||
|
|
):
|
||
|
|
body = body.difference(access)
|
||
|
|
return body
|
||
|
|
|
||
|
|
|
||
|
|
def _crossing_leg_seed(net: NetSpec) -> PathSeed:
|
||
|
|
dx, dy = net.target.x - net.start.x, net.target.y - net.start.y
|
||
|
|
forward, offset = ((dx, dy), (dy, -dx), (-dx, -dy), (-dy, dx))[int(net.start.r) // 90]
|
||
|
|
if not offset:
|
||
|
|
return PathSeed((StraightSeed(forward),))
|
||
|
|
# Each stagger in the crossing grids uses a 20-unit offset. At radius
|
||
|
|
# 25 its forward span is exactly 40, leaving clearance at both ports.
|
||
|
|
assert abs(offset) == 20
|
||
|
|
assert forward > 40
|
||
|
|
first = (forward - 40) // 2
|
||
|
|
return PathSeed((StraightSeed(first), SBendSeed(offset, _CROSSING_RADIUS), StraightSeed(forward - first - 40)))
|
||
|
|
|
||
|
|
|
||
|
|
def _crossing_grid(
|
||
|
|
name: str,
|
||
|
|
description: str,
|
||
|
|
xs: tuple[int, ...],
|
||
|
|
ys: tuple[int, ...],
|
||
|
|
*,
|
||
|
|
staggered: bool = False,
|
||
|
|
) -> BenchmarkCase:
|
||
|
|
width, height = xs[-1] + 180, ys[-1] + 180
|
||
|
|
offset = _CROSSING_PORT_OFFSET
|
||
|
|
stagger = 20 if staggered else 0
|
||
|
|
cells = {
|
||
|
|
(row, column): (x + stagger * (row % 2), y + stagger * (column % 2)) for (row, y), (column, x) in product(enumerate(ys), enumerate(xs))
|
||
|
|
}
|
||
|
|
nets = []
|
||
|
|
|
||
|
|
def add_bank(label: str, ports: list[tuple[str, Port]], *, reverse: bool) -> None:
|
||
|
|
for (source, start), (sink, target) in zip(ports[::2], ports[1::2], strict=True):
|
||
|
|
if reverse:
|
||
|
|
nets.append(NetSpec(f"{label}:{sink}->{source}", target.translate(0, 0, 180), start.translate(0, 0, 180)))
|
||
|
|
else:
|
||
|
|
nets.append(NetSpec(f"{label}:{source}->{sink}", start, target))
|
||
|
|
|
||
|
|
for row in range(len(ys)):
|
||
|
|
left_y = cells[row, 0][1] - stagger
|
||
|
|
right_y = cells[row, len(xs) - 1][1] + stagger
|
||
|
|
ports = [("west", Port(30, left_y, 0))]
|
||
|
|
for column in range(len(xs)):
|
||
|
|
x, y = cells[row, column]
|
||
|
|
cell = f"x{row}_{column}"
|
||
|
|
ports.extend(((f"{cell}.w", Port(x - offset, y, 0)), (f"{cell}.e", Port(x + offset, y, 0))))
|
||
|
|
ports.append(("east", Port(width - 30, right_y, 0)))
|
||
|
|
add_bank(f"h{row}", ports, reverse=bool(row % 2))
|
||
|
|
|
||
|
|
for column in range(len(xs)):
|
||
|
|
bottom_x = cells[0, column][0] + stagger
|
||
|
|
top_x = cells[len(ys) - 1, column][0] - stagger
|
||
|
|
ports = [("south", Port(bottom_x, 30, 90))]
|
||
|
|
for row in range(len(ys)):
|
||
|
|
x, y = cells[row, column]
|
||
|
|
cell = f"x{row}_{column}"
|
||
|
|
ports.extend(((f"{cell}.s", Port(x, y - offset, 90)), (f"{cell}.n", Port(x, y + offset, 90))))
|
||
|
|
ports.append(("north", Port(top_x, height - 30, 90)))
|
||
|
|
add_bank(f"v{column}", ports, reverse=bool(column % 2))
|
||
|
|
|
||
|
|
problem = RoutingProblem(
|
||
|
|
(0, 0, width, height), tuple(nets), tuple(_crossing_keepout(*center) for center in cells.values()), clearance=_CROSSING_CLEARANCE
|
||
|
|
)
|
||
|
|
reference = tuple((net.net_id, _crossing_leg_seed(net)) for net in nets)
|
||
|
|
return BenchmarkCase(name, description, problem, benchmark_options(_CROSSING_RADIUS), reference)
|
||
|
|
|
||
|
|
|
||
|
|
def _crossing_obstructed_approaches() -> BenchmarkCase:
|
||
|
|
offset = _CROSSING_PORT_OFFSET
|
||
|
|
nets = (
|
||
|
|
NetSpec("west->x0.w", Port(30, 300, 0), Port(300 - offset, 300, 0)),
|
||
|
|
NetSpec("x0.e->south-east", Port(300 + offset, 300, 0), Port(530, 204, 270)),
|
||
|
|
NetSpec("south->x0.s", Port(280, 35, 90), Port(300, 300 - offset, 90)),
|
||
|
|
NetSpec("x0.n->north", Port(300, 300 + offset, 90), Port(300, 570, 90)),
|
||
|
|
)
|
||
|
|
west, east, south, north = nets
|
||
|
|
west_seed, _ = compile_waypoints(
|
||
|
|
((30, 300), (66, 300), (66, 215), (220, 215), (220, 300), (west.target.x, 300)), west.start, _CROSSING_RADIUS
|
||
|
|
)
|
||
|
|
east_seed, _ = compile_waypoints(((east.start.x, 300), (530, 300), (530, 204)), east.start, _CROSSING_RADIUS)
|
||
|
|
problem = RoutingProblem(
|
||
|
|
(0, 0, 600, 600),
|
||
|
|
nets,
|
||
|
|
(_crossing_keepout(300, 300), box(98, 265, 153, 335), box(402, 222, 468, 280)),
|
||
|
|
clearance=_CROSSING_CLEARANCE,
|
||
|
|
)
|
||
|
|
reference = (
|
||
|
|
(west.net_id, west_seed),
|
||
|
|
(east.net_id, east_seed),
|
||
|
|
(south.net_id, _crossing_leg_seed(south)),
|
||
|
|
(north.net_id, _crossing_leg_seed(north)),
|
||
|
|
)
|
||
|
|
return BenchmarkCase(
|
||
|
|
"crossing-obstructed-approaches",
|
||
|
|
"A fixed crossing with a blocked approach, offset launch, and turning exit",
|
||
|
|
problem,
|
||
|
|
benchmark_options(_CROSSING_RADIUS),
|
||
|
|
reference,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
CROSSING_BENCHMARK_CASES = (
|
||
|
|
_crossing_grid("crossing-single", "Four straight legs at one placed crossing", (160,), (160,)),
|
||
|
|
_crossing_obstructed_approaches(),
|
||
|
|
_crossing_grid("crossing-aligned-grid", "Six placed crossings linked between opposite port banks", (160, 263, 366), (165, 285)),
|
||
|
|
_crossing_grid(
|
||
|
|
"crossing-staggered-grid",
|
||
|
|
"Nine staggered crossings with short S-bend links and offset terminal banks",
|
||
|
|
(175, 280, 395),
|
||
|
|
(160, 266, 388),
|
||
|
|
staggered=True,
|
||
|
|
),
|
||
|
|
)
|