use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use crate::geom::{Axis, Direction, Point};
use crate::graph::{RoutingGraph, VertexId};
use crate::nets::Net;
#[derive(Debug, Clone)]
pub(crate) struct Segment {
pub(crate) net: usize,
pub(crate) axis: Axis,
pub(crate) vertices: Vec<VertexId>,
pub(crate) fixed: bool,
}
impl Segment {
pub(crate) fn low(&self) -> VertexId {
self.vertices[0]
}
pub(crate) fn high(&self) -> VertexId {
*self.vertices.last().expect("a segment has two vertices")
}
pub(crate) fn forward(&self) -> Direction {
match self.axis {
Axis::X => Direction::Right,
Axis::Y => Direction::Up,
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct Segments {
pub(crate) list: Vec<Segment>,
pub(crate) line: Vec<i32>,
of_edge: BTreeMap<(usize, VertexId, Direction), usize>,
terminal: BTreeMap<(usize, VertexId, Axis), usize>,
}
impl Segments {
pub(crate) fn new(graph: &RoutingGraph, nets: &[Net]) -> Self {
let mut segments = Self {
list: Vec::new(),
line: Vec::new(),
of_edge: BTreeMap::new(),
terminal: BTreeMap::new(),
};
for (index, net) in nets.iter().enumerate() {
for (v, dir) in net.edges() {
if !segments.of_edge.contains_key(&(index, v, dir)) {
segments.add_run(graph, index, net, v, dir);
}
}
}
for (index, net) in nets.iter().enumerate() {
for &t in &net.terminals {
segments.add_terminal_points(graph, index, net, t);
}
}
segments
}
fn add_terminal_points(&mut self, graph: &RoutingGraph, index: usize, net: &Net, t: VertexId) {
let arms = net.arms_at(t);
for axis in [Axis::X, Axis::Y] {
let along = arms.iter().any(|d| d.axis() == axis);
if along || arms.is_empty() {
continue;
}
self.terminal.insert((index, t, axis), self.list.len());
self.line.push(graph.point(t).coordinate(axis.other()));
self.list.push(Segment {
net: index,
axis,
vertices: alloc::vec![t],
fixed: true,
});
}
}
pub(crate) fn terminal_arcs(&self, nets: &[Net], axis: Axis) -> Vec<(usize, usize)> {
let mut arcs = Vec::new();
for (p, point) in self.list.iter().enumerate() {
if point.axis != axis || point.vertices.len() != 1 {
continue;
}
let t = point.vertices[0];
let Some(arm) = nets[point.net].arms_at(t).iter().next() else {
continue;
};
for (s, segment) in self.list.iter().enumerate() {
if segment.axis == axis
&& segment.net != point.net
&& segment.vertices.len() > 1
&& segment.vertices.contains(&t)
{
arcs.push(if arm.is_positive() { (s, p) } else { (p, s) });
}
}
}
arcs
}
fn add_run(
&mut self,
graph: &RoutingGraph,
index: usize,
net: &Net,
v: VertexId,
dir: Direction,
) {
let mut start = v;
while let Some(prev) = graph.neighbor(start, dir.reverse()) {
if !net.has_edge(prev, dir) {
break;
}
start = prev;
}
let mut vertices = alloc::vec![start];
let mut at = start;
while net.has_edge(at, dir) {
at = graph.neighbor(at, dir).expect("net edges are graph edges");
vertices.push(at);
}
let id = self.list.len();
for &u in &vertices[..vertices.len() - 1] {
self.of_edge.insert((index, u, dir), id);
}
let fixed = vertices.iter().any(|u| net.terminals.contains(u));
let axis = dir.axis();
self.line.push(graph.point(start).coordinate(axis.other()));
self.list.push(Segment {
net: index,
axis,
vertices,
fixed,
});
}
pub(crate) fn of_edge(
&self,
graph: &RoutingGraph,
net: usize,
v: VertexId,
dir: Direction,
) -> Option<usize> {
let (low, forward) = if dir.is_positive() {
(v, dir)
} else {
(graph.neighbor(v, dir)?, dir.reverse())
};
self.of_edge.get(&(net, low, forward)).copied()
}
pub(crate) fn terminal_point(&self, net: usize, v: VertexId, axis: Axis) -> Option<usize> {
self.terminal.get(&(net, v, axis)).copied()
}
pub(crate) fn end_coordinate(
&self,
graph: &RoutingGraph,
nets: &[Net],
s: usize,
end: VertexId,
) -> i32 {
let segment = &self.list[s];
let net = &nets[segment.net];
let across = segment.forward().left();
for dir in [across, across.reverse()] {
if net.has_edge(end, dir) {
if let Some(t) = self.of_edge(graph, segment.net, end, dir) {
return self.line[t];
}
}
}
match self.terminal_point(segment.net, end, segment.axis.other()) {
Some(t) => self.line[t],
None => graph.point(end).coordinate(segment.axis),
}
}
pub(crate) fn extent(&self, graph: &RoutingGraph, nets: &[Net], s: usize) -> (i32, i32) {
let segment = &self.list[s];
let a = self.end_coordinate(graph, nets, s, segment.low());
let b = self.end_coordinate(graph, nets, s, segment.high());
(a.min(b), a.max(b))
}
pub(crate) fn position(
&self,
graph: &RoutingGraph,
net: usize,
v: VertexId,
nets: &[Net],
) -> Point {
let mut p = graph.point(v);
for axis in [Axis::X, Axis::Y] {
if let Some(s) = self.terminal_point(net, v, axis) {
match axis {
Axis::X => p.y = self.line[s],
Axis::Y => p.x = self.line[s],
}
}
}
let arms = nets[net].arms_at(v);
for dir in arms.iter() {
if let Some(s) = self.of_edge(graph, net, v, dir) {
match self.list[s].axis {
Axis::X => p.y = self.line[s],
Axis::Y => p.x = self.line[s],
}
}
}
p
}
}