use alloc::vec::Vec;
use multiobjective_search::{Graph, heuristic_fn, namoa_star_dr};
use crate::error::Error;
use crate::geom::{Axis, Direction};
use crate::graph::{RoutingGraph, VertexId};
use crate::route::Route;
use crate::scene::{PortId, Scene};
use crate::search::{Endpoints, endpoints, min_bends};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParetoRoute {
pub route: Route,
pub cost: [u64; 3],
}
type State = (u32, u8);
struct Front<'a> {
graph: &'a RoutingGraph,
ends: Endpoints,
straight: Vec<[u64; 2]>,
}
impl Graph for Front<'_> {
type State = State;
type Weight = [u64; 3];
fn successors(&self, state: &State, out: &mut Vec<(State, [u64; 3])>) {
let v = VertexId(state.0);
let heading = (state.1 != 4).then(|| Direction::from_index(usize::from(state.1)));
let options: Vec<Direction> = match heading {
Some(h) => alloc::vec![h, h.right(), h.left()],
None => self.ends.source_directions.iter().collect(),
};
for dir in options {
let Some(w) = self.graph.neighbor(v, dir) else {
continue;
};
let length = self.graph.point(v).manhattan(self.graph.point(w));
let bend = u64::from(heading.is_some_and(|h| h != dir));
let crossings = if heading == Some(dir) {
match dir.axis() {
Axis::X => self.straight[v.index()][1],
Axis::Y => self.straight[v.index()][0],
}
} else {
0
};
let index = u8::try_from(dir.index()).expect("four directions");
out.push(((w.0, index), [length, bend, crossings]));
}
}
}
fn straight_passes(graph: &RoutingGraph, routes: &[Route]) -> Vec<[u64; 2]> {
let mut straight = alloc::vec![[0, 0]; graph.capacity()];
for route in routes {
for t in route.vertices.windows(3) {
let (a, b, c) = (graph.point(t[0]), graph.point(t[1]), graph.point(t[2]));
if Direction::between(a, b) == Direction::between(b, c) {
straight[t[1].index()][usize::from(a.y != b.y)] += 1;
}
}
}
straight
}
pub fn front(
scene: &Scene,
source: PortId,
target: PortId,
others: &[Route],
) -> Result<Vec<ParetoRoute>, Error> {
let graph = scene.graph();
let ends = endpoints(scene, source, target)?;
let search = Front {
graph,
ends,
straight: straight_passes(graph, others),
};
let target_point = graph.point(ends.target);
let arrivals = ends.target_directions.reversed();
let heuristic = heuristic_fn(|s: &State| {
let here = graph.point(VertexId(s.0));
let bends = if s.1 == 4 {
ends.source_directions
.iter()
.filter_map(|d| min_bends(here, d, target_point, arrivals))
.min()
.unwrap_or(0)
} else {
min_bends(
here,
Direction::from_index(usize::from(s.1)),
target_point,
arrivals,
)
.unwrap_or(0)
};
Some([here.manhattan(target_point), u64::from(bends), 0])
});
let target = ends.target.0;
let is_goal = |s: &State| {
s.0 == target && (s.1 == 4 || arrivals.contains(Direction::from_index(usize::from(s.1))))
};
let output = namoa_star_dr(&search, heuristic, (ends.source.0, 4), is_goal);
Ok(output
.solutions
.into_iter()
.map(|solution| {
let vertices = solution.path.iter().map(|s| VertexId(s.0)).collect();
ParetoRoute {
route: Route::from_vertices(graph, vertices, solution.cost[0]),
cost: solution.cost,
}
})
.collect())
}