Report coincident terminals and unusable seeds per net, update custom bend handling and verification, and cover the revised behavior with regression tests. Shorten the user and developer guides around reader tasks and keep the illustrated routing walkthrough and reference links easy to find.
393 lines
14 KiB
Python
393 lines
14 KiB
Python
from __future__ import annotations
|
|
|
|
import math
|
|
from typing import TYPE_CHECKING
|
|
|
|
from inire.constants import TOLERANCE_LINEAR
|
|
from inire.geometry.components import bend_fits_width, bend90_grid_span, bend90_uses_circular_geometry
|
|
from inire.geometry.sbend import sbend_grid_span
|
|
|
|
from ._astar_admission import process_move
|
|
from ._seed_materialization import seed_move
|
|
|
|
if TYPE_CHECKING:
|
|
from inire.geometry.primitives import Port
|
|
from inire.seeds import BendDirection
|
|
from ._astar_types import AStarContext, AStarMetrics, AStarNode, CongestionCaches, SearchRunConfig
|
|
|
|
|
|
def _quantized_lengths(values: list[float], max_reach: float) -> list[int]:
|
|
out = {int(round(v)) for v in values if v > 0 and v <= max_reach + 0.01}
|
|
return sorted((v for v in out if v > 0), reverse=True)
|
|
|
|
|
|
def _distance_to_bounds_in_heading(
|
|
current: Port,
|
|
bounds: tuple[float, float, float, float],
|
|
) -> float:
|
|
min_x, min_y, max_x, max_y = bounds
|
|
if current.r == 0:
|
|
return max(0.0, max_x - current.x)
|
|
if current.r == 90:
|
|
return max(0.0, max_y - current.y)
|
|
if current.r == 180:
|
|
return max(0.0, current.x - min_x)
|
|
return max(0.0, current.y - min_y)
|
|
|
|
|
|
def _should_cap_straights_to_bounds(context: AStarContext) -> bool:
|
|
return (
|
|
not context.options.congestion.warm_start_enabled
|
|
and len(context.problem.nets) >= 8
|
|
)
|
|
|
|
|
|
def _sbend_forward_span(offset: float, radius: float) -> float | None:
|
|
abs_offset = abs(offset)
|
|
if abs_offset <= TOLERANCE_LINEAR or radius <= 0 or abs_offset >= 2.0 * radius:
|
|
return None
|
|
try:
|
|
return sbend_grid_span(offset, radius)
|
|
except OverflowError:
|
|
return None
|
|
|
|
|
|
def _visible_straight_candidates(
|
|
current: Port,
|
|
context: AStarContext,
|
|
max_reach: float,
|
|
cos_v: float,
|
|
sin_v: float,
|
|
net_width: float,
|
|
) -> list[float]:
|
|
search_options = context.options.search
|
|
mode = search_options.visibility_guidance
|
|
if mode == "off":
|
|
return []
|
|
|
|
if mode == "exact_corner":
|
|
max_bend_radius = max(search_options.bend_radii, default=0.0)
|
|
visibility_reach = max_reach + max_bend_radius
|
|
visible_corners = sorted(
|
|
context.visibility_manager.get_corner_visibility(current, max_dist=visibility_reach),
|
|
key=lambda corner: corner[2],
|
|
)
|
|
if not visible_corners:
|
|
return []
|
|
|
|
candidates: set[int] = set()
|
|
for cx, cy, _ in visible_corners[:12]:
|
|
dx = cx - current.x
|
|
dy = cy - current.y
|
|
local_x = dx * cos_v + dy * sin_v
|
|
if local_x <= search_options.min_straight_length:
|
|
continue
|
|
candidates.add(int(round(local_x)))
|
|
return sorted(candidates, reverse=True)
|
|
|
|
if mode != "tangent_corner":
|
|
return []
|
|
|
|
visibility_manager = context.visibility_manager
|
|
visibility_manager.ensure_corner_index_current()
|
|
context.metrics.total_visibility_tangent_candidate_scans += 1
|
|
max_bend_radius = max(search_options.bend_radii, default=0.0)
|
|
if max_bend_radius <= 0 or not visibility_manager.corners:
|
|
return []
|
|
|
|
reach = max_reach + max_bend_radius
|
|
candidate_ids = visibility_manager.get_tangent_corner_candidates(
|
|
current,
|
|
min_forward=search_options.min_straight_length,
|
|
max_forward=reach,
|
|
radii=search_options.bend_radii,
|
|
)
|
|
if not candidate_ids:
|
|
return []
|
|
|
|
scored: list[tuple[float, float, float, float, float]] = []
|
|
for idx in candidate_ids:
|
|
context.metrics.total_visibility_tangent_candidate_corner_checks += 1
|
|
cx, cy = visibility_manager.corners[idx]
|
|
dx = cx - current.x
|
|
dy = cy - current.y
|
|
local_x = dx * cos_v + dy * sin_v
|
|
local_y = -dx * sin_v + dy * cos_v
|
|
if local_x <= search_options.min_straight_length or local_x > reach + 0.01:
|
|
continue
|
|
|
|
nearest_radius = min(search_options.bend_radii, key=lambda radius: abs(abs(local_y) - radius))
|
|
tangent_error = abs(abs(local_y) - nearest_radius)
|
|
if tangent_error > 2.0:
|
|
continue
|
|
|
|
length = local_x - bend90_grid_span(nearest_radius)
|
|
if length <= search_options.min_straight_length or length > max_reach + 0.01:
|
|
continue
|
|
|
|
scored.append((tangent_error, math.hypot(dx, dy), length, dx, dy))
|
|
|
|
if not scored:
|
|
return []
|
|
|
|
collision_engine = context.cost_evaluator.collision_engine
|
|
tangent_candidates: set[int] = set()
|
|
for _, dist, length, dx, dy in sorted(scored)[:4]:
|
|
context.metrics.total_visibility_tangent_candidate_ray_tests += 1
|
|
angle = math.degrees(math.atan2(dy, dx))
|
|
corner_reach = collision_engine.ray_cast(
|
|
current,
|
|
angle,
|
|
max_dist=dist + 0.05,
|
|
net_width=net_width,
|
|
caller="visibility_tangent",
|
|
)
|
|
if corner_reach < dist - 0.01:
|
|
continue
|
|
qlen = int(round(length))
|
|
if qlen > 0:
|
|
tangent_candidates.add(qlen)
|
|
|
|
return sorted(tangent_candidates, reverse=True)
|
|
|
|
|
|
def expand_moves(
|
|
current: AStarNode,
|
|
target: Port,
|
|
net_width: float,
|
|
net_id: str,
|
|
open_set: list[AStarNode],
|
|
closed_set: dict[tuple[int, int, int], float],
|
|
context: AStarContext,
|
|
metrics: AStarMetrics,
|
|
congestion: CongestionCaches,
|
|
config: SearchRunConfig,
|
|
deferred_turns: list[tuple[AStarNode, float, BendDirection]] | None,
|
|
) -> None:
|
|
search_options = context.options.search
|
|
scan_limit = search_options.max_straight_length
|
|
cp = current.port
|
|
previous = current.component_result
|
|
seed_index = current.seed_index if config.propose_guidance_moves else None
|
|
prev_straight_length = previous.length if seed_index is None and previous is not None and previous.move_type == "straight" else None
|
|
seed = config.guidance_seed
|
|
if seed is not None and seed_index is not None and seed_index < len(seed):
|
|
kind, parameters = seed_move(seed[seed_index])
|
|
process_move(current, target, net_width, net_id, open_set, closed_set, context, metrics, congestion, config, kind, parameters)
|
|
|
|
dx_t = target.x - cp.x
|
|
dy_t = target.y - cp.y
|
|
dist_sq = dx_t * dx_t + dy_t * dy_t
|
|
|
|
if cp.r == 0:
|
|
cos_v, sin_v = 1.0, 0.0
|
|
elif cp.r == 90:
|
|
cos_v, sin_v = 0.0, 1.0
|
|
elif cp.r == 180:
|
|
cos_v, sin_v = -1.0, 0.0
|
|
else:
|
|
cos_v, sin_v = 0.0, -1.0
|
|
|
|
proj_t = dx_t * cos_v + dy_t * sin_v
|
|
perp_t = -dx_t * sin_v + dy_t * cos_v
|
|
dx_local = proj_t
|
|
dy_local = perp_t
|
|
|
|
collision = context.cost_evaluator.collision_engine
|
|
if (
|
|
proj_t > 0 and abs(perp_t) < TOLERANCE_LINEAR and cp.r == target.r
|
|
and (
|
|
prev_straight_length is None
|
|
or proj_t < prev_straight_length - TOLERANCE_LINEAR
|
|
or prev_straight_length + proj_t > scan_limit + TOLERANCE_LINEAR
|
|
)
|
|
):
|
|
process_move(
|
|
current,
|
|
target,
|
|
net_width,
|
|
net_id,
|
|
open_set,
|
|
closed_set,
|
|
context,
|
|
metrics,
|
|
congestion,
|
|
config,
|
|
"straight",
|
|
(int(round(proj_t)),),
|
|
)
|
|
|
|
max_reach = collision.ray_cast(
|
|
cp,
|
|
cp.r,
|
|
scan_limit,
|
|
net_width=net_width,
|
|
caller="expand_forward",
|
|
)
|
|
short_launch = (previous is None or previous.move_type != "straight") and max_reach < max(1, math.ceil(search_options.min_straight_length))
|
|
sbend_only = not search_options.bend_radii and bool(search_options.sbend_radii)
|
|
if short_launch or (sbend_only and max_reach < scan_limit):
|
|
# Radius around the ray can block a launch at an otherwise clear port face.
|
|
reach = collision.straight_reach(cp, scan_limit, net_width)
|
|
max_reach = max(max_reach, math.floor(reach + TOLERANCE_LINEAR))
|
|
if _should_cap_straights_to_bounds(context):
|
|
max_reach = min(max_reach, _distance_to_bounds_in_heading(cp, context.problem.bounds))
|
|
candidate_lengths = [
|
|
search_options.min_straight_length,
|
|
max_reach,
|
|
max_reach / 2.0,
|
|
max_reach - 5.0,
|
|
]
|
|
|
|
axis_target_dist = abs(dx_t) if cp.r in (0, 180) else abs(dy_t)
|
|
candidate_lengths.append(axis_target_dist)
|
|
bend_spans = tuple(bend90_grid_span(radius) for radius in search_options.bend_radii)
|
|
for span in bend_spans:
|
|
candidate_lengths.extend((max_reach - span, axis_target_dist - span, axis_target_dist - 2 * span))
|
|
|
|
candidate_lengths.extend(
|
|
_visible_straight_candidates(
|
|
cp,
|
|
context,
|
|
max_reach,
|
|
cos_v,
|
|
sin_v,
|
|
net_width,
|
|
)
|
|
)
|
|
|
|
if cp.r == target.r and dx_local > 0 and abs(dy_local) > TOLERANCE_LINEAR and search_options.allows_sbend_offset(dy_local):
|
|
for radius in search_options.sbend_radii:
|
|
sbend_span = _sbend_forward_span(dy_local, radius)
|
|
if sbend_span is None:
|
|
continue
|
|
candidate_lengths.extend((dx_local - sbend_span, dx_local - 2.0 * sbend_span))
|
|
if not bend_spans:
|
|
candidate_lengths.append(max_reach - sbend_span)
|
|
|
|
guide_offsets: set[int] = set()
|
|
if config.corridor is not None:
|
|
max_radius = max((*bend_spans, *search_options.sbend_radii), default=0.0)
|
|
for forward, lateral in config.corridor.nearby_anchors(cp, max_reach + 2 * max_radius):
|
|
candidate_lengths.append(forward)
|
|
candidate_lengths.extend(forward - span for span in bend_spans)
|
|
if search_options.sbend_offsets is None and abs(lateral) >= 1 and search_options.sbend_radii:
|
|
for radius in search_options.sbend_radii:
|
|
span = _sbend_forward_span(lateral, radius)
|
|
if span is not None:
|
|
candidate_lengths.append(forward - span)
|
|
if forward <= span + max_radius:
|
|
guide_offsets.add(round(lateral))
|
|
|
|
for length in _quantized_lengths(candidate_lengths, max_reach):
|
|
if length < search_options.min_straight_length:
|
|
continue
|
|
if (
|
|
prev_straight_length is not None
|
|
and length >= prev_straight_length - TOLERANCE_LINEAR
|
|
and prev_straight_length + length <= scan_limit + TOLERANCE_LINEAR
|
|
):
|
|
continue
|
|
process_move(
|
|
current,
|
|
target,
|
|
net_width,
|
|
net_id,
|
|
open_set,
|
|
closed_set,
|
|
context,
|
|
metrics,
|
|
congestion,
|
|
config,
|
|
"straight",
|
|
(length,),
|
|
)
|
|
|
|
angle_to_target = 0.0
|
|
if dx_t != 0 or dy_t != 0:
|
|
angle_to_target = float((round((180.0 / math.pi) * math.atan2(dy_t, dx_t)) + 360.0) % 360.0)
|
|
prefer_toward_target = deferred_turns is not None and not config.restrict_to_corridor and dist_sq >= 150 * 150
|
|
|
|
circular_bend = bend90_uses_circular_geometry(config.bend_collision_type, config.bend_physical_geometry)
|
|
for radius in search_options.bend_radii:
|
|
if circular_bend and not bend_fits_width(radius, net_width):
|
|
continue
|
|
for direction in ("CW", "CCW"):
|
|
if prefer_toward_target and deferred_turns is not None:
|
|
heading = (cp.r + (90 if direction == "CCW" else -90)) % 360
|
|
difference = (angle_to_target - heading + 180.0) % 360.0 - 180.0
|
|
if abs(difference) > 135.0:
|
|
deferred_turns.append((current, radius, direction))
|
|
continue
|
|
process_move(
|
|
current,
|
|
target,
|
|
net_width,
|
|
net_id,
|
|
open_set,
|
|
closed_set,
|
|
context,
|
|
metrics,
|
|
congestion,
|
|
config,
|
|
"bend90",
|
|
(radius, direction),
|
|
)
|
|
|
|
max_sbend_r = max(search_options.sbend_radii) if search_options.sbend_radii else 0.0
|
|
custom_sbends = (
|
|
{(outline.offset, outline.radius) for outline in search_options.sbend_outlines_for_width(net_width)}
|
|
if search_options.sbend_outlines and search_options.active_sbend_outlines else ()
|
|
)
|
|
if not bend_fits_width(max_sbend_r, net_width) and not custom_sbends:
|
|
return
|
|
|
|
explicit_offsets = search_options.sbend_offsets
|
|
offsets = set(explicit_offsets) if explicit_offsets is not None else guide_offsets
|
|
if custom_sbends:
|
|
offsets.update(offset for offset, _ in custom_sbends)
|
|
if explicit_offsets is None and target.r == cp.r and dx_local > 0 and dy_local:
|
|
if dx_local <= 4 * max_sbend_r and abs(dy_local) < 2 * max_sbend_r:
|
|
offsets.add(round(dy_local))
|
|
elif abs(dy_local) >= 2 * max_sbend_r:
|
|
for radius in search_options.sbend_radii:
|
|
max_offset = math.ceil(2 * radius) - 1
|
|
if max_offset <= 0 or not bend_fits_width(radius, net_width):
|
|
continue
|
|
count = math.ceil(abs(dy_local) / max_offset)
|
|
if count <= 4:
|
|
offset = round(dy_local / count)
|
|
span = _sbend_forward_span(offset, radius)
|
|
if span is not None and span * count <= dx_local + count - 1:
|
|
offsets.add(offset)
|
|
elif explicit_offsets is None and dx_local > 0 and (target.r - cp.r) % 180 == 90:
|
|
# Position just before the final quarter turn, including its two tangent spans.
|
|
turn = 1 if (target.r - cp.r) % 360 == 90 else -1
|
|
for span in bend_spans:
|
|
offset = round(dy_local - turn * span)
|
|
for radius in search_options.sbend_radii:
|
|
forward = _sbend_forward_span(offset, radius)
|
|
if forward is not None and forward <= dx_local - span:
|
|
offsets.add(offset)
|
|
break
|
|
|
|
for offset in sorted(offsets):
|
|
if offset == 0:
|
|
continue
|
|
for radius in search_options.sbend_radii:
|
|
if abs(offset) >= 2 * radius or (not bend_fits_width(radius, net_width) and (offset, radius) not in custom_sbends):
|
|
continue
|
|
process_move(
|
|
current,
|
|
target,
|
|
net_width,
|
|
net_id,
|
|
open_set,
|
|
closed_set,
|
|
context,
|
|
metrics,
|
|
congestion,
|
|
config,
|
|
"sbend",
|
|
(offset, radius),
|
|
)
|