use alloc::{collections::BinaryHeap, vec::Vec};
use crate::graph::Entry;
use crate::radius::{dimension_f64, shrinking_radius};
use crate::{NearestNeighbors, StateSpace, ValidityChecker};
pub(super) struct Batch<'a, Sp: StateSpace, C, N> {
pub(super) space: &'a Sp,
pub(super) checker: &'a C,
pub(super) states: Vec<Sp::State>,
nn: N,
radius: f64,
near: Vec<Option<Vec<usize>>>,
}
impl<'a, Sp, C, N> Batch<'a, Sp, C, N>
where
Sp: StateSpace,
C: ValidityChecker<Sp>,
N: NearestNeighbors<Sp>,
{
pub(super) fn new(
space: &'a Sp,
checker: &'a C,
gamma: f64,
states: Vec<Sp::State>,
mut nn: N,
) -> Self {
let n = states.len();
for (id, state) in states.iter().enumerate() {
nn.insert(id, state.clone());
}
Self {
space,
checker,
radius: shrinking_radius(gamma, n, dimension_f64(space.dimension())),
states,
nn,
near: alloc::vec![None; n],
}
}
pub(super) fn len(&self) -> usize {
self.states.len()
}
fn neighbors(&mut self, node: usize) -> &[usize] {
if self.near[node].is_none() {
let mut near = Vec::new();
self.nn
.within_radius(self.space, &self.states[node], self.radius, &mut near);
near.retain(|&other| other != node);
self.near[node] = Some(near);
}
self.near[node].as_deref().unwrap_or_default()
}
fn is_motion_valid(&self, from: usize, to: usize) -> bool {
self.checker
.check_motion(self.space, &self.states[from], &self.states[to])
.is_valid()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Direction {
Outward,
Inward,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Status {
Unvisited,
Fresh,
Open,
Closed,
}
pub(super) struct Wave {
direction: Direction,
pub(super) status: Vec<Status>,
pub(super) cost: Vec<f64>,
pub(super) parent: Vec<Option<usize>>,
open: BinaryHeap<Entry>,
}
impl Wave {
pub(super) fn new(n: usize, root: usize, direction: Direction) -> Self {
let mut wave = Self {
direction,
status: alloc::vec![Status::Unvisited; n],
cost: alloc::vec![f64::INFINITY; n],
parent: alloc::vec![None; n],
open: BinaryHeap::new(),
};
wave.status[root] = Status::Open;
wave.cost[root] = 0.0;
wave.open.push(Entry {
cost: 0.0,
node: root,
});
wave
}
pub(super) fn peek_open(&mut self) -> Option<usize> {
while let Some(&Entry { node, .. }) = self.open.peek() {
if self.status[node] == Status::Open {
return Some(node);
}
self.open.pop();
}
None
}
pub(super) fn expand<Sp, C, N>(
&mut self,
batch: &mut Batch<'_, Sp, C, N>,
z: usize,
) -> Vec<usize>
where
Sp: StateSpace,
C: ValidityChecker<Sp>,
N: NearestNeighbors<Sp>,
{
let mut fresh = Vec::new();
let near_z = batch.neighbors(z).to_vec();
for x in near_z {
if self.status[x] != Status::Unvisited {
continue;
}
let Some((y, cost)) = self.cheapest_open_neighbor(batch, x) else {
continue;
};
if self.is_edge_valid(batch, y, x) {
self.status[x] = Status::Fresh;
self.cost[x] = cost;
self.parent[x] = Some(y);
fresh.push(x);
}
}
for &x in &fresh {
self.status[x] = Status::Open;
self.open.push(Entry {
cost: self.cost[x],
node: x,
});
}
self.status[z] = Status::Closed;
fresh
}
fn cheapest_open_neighbor<Sp, C, N>(
&self,
batch: &mut Batch<'_, Sp, C, N>,
x: usize,
) -> Option<(usize, f64)>
where
Sp: StateSpace,
C: ValidityChecker<Sp>,
N: NearestNeighbors<Sp>,
{
let mut best: Option<(usize, f64)> = None;
let near_x = batch.neighbors(x).to_vec();
for y in near_x {
if self.status[y] != Status::Open {
continue;
}
let cost = self.cost[y] + self.edge_cost(batch, y, x);
if best.is_none_or(|(_, best_cost)| cost < best_cost) {
best = Some((y, cost));
}
}
best
}
fn edge_cost<Sp, C, N>(&self, batch: &Batch<'_, Sp, C, N>, parent: usize, child: usize) -> f64
where
Sp: StateSpace,
{
let (from, to) = self.travel(parent, child);
batch.space.distance(&batch.states[from], &batch.states[to])
}
fn is_edge_valid<Sp, C, N>(
&self,
batch: &Batch<'_, Sp, C, N>,
parent: usize,
child: usize,
) -> bool
where
Sp: StateSpace,
C: ValidityChecker<Sp>,
N: NearestNeighbors<Sp>,
{
let (from, to) = self.travel(parent, child);
batch.is_motion_valid(from, to)
}
const fn travel(&self, parent: usize, child: usize) -> (usize, usize) {
match self.direction {
Direction::Outward => (parent, child),
Direction::Inward => (child, parent),
}
}
pub(super) fn branch(&self, id: usize) -> Vec<usize> {
let mut nodes = Vec::new();
let mut current = Some(id);
while let Some(node) = current {
nodes.push(node);
current = self.parent[node];
}
nodes
}
}