use rand_core::Rng;
use super::{Error, tree::Tree};
use crate::{
LinearScan, NearestNeighbors, Path, Problem, Sampler, StateSpace, Steering, ValidityChecker,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Side {
Start,
Goal,
}
impl Side {
const fn other(self) -> Self {
match self {
Self::Start => Self::Goal,
Self::Goal => Self::Start,
}
}
}
#[derive(Clone)]
struct Half<S, N> {
tree: Tree<S>,
nn: N,
}
#[derive(Clone)]
pub struct RrtConnect<Sp: StateSpace, Sa, C, St, N = LinearScan<<Sp as StateSpace>::State>> {
problem: Problem<Sp, Sa, C, St>,
start: Half<Sp::State, N>,
goal: Half<Sp::State, N>,
active: Side,
solution: Option<Path<Sp::State>>,
}
impl<Sp, Sa, C, St> RrtConnect<Sp, Sa, C, St>
where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
C: ValidityChecker<Sp>,
St: Steering<Sp>,
{
pub fn new(problem: Problem<Sp, Sa, C, St>) -> Result<Self, Error> {
Self::with_nearest_neighbors(problem, LinearScan::default())
}
}
impl<Sp, Sa, C, St, N> RrtConnect<Sp, Sa, C, St, N>
where
Sp: StateSpace,
Sa: Sampler<Sp::State>,
C: ValidityChecker<Sp>,
St: Steering<Sp>,
N: NearestNeighbors<Sp> + Clone,
{
pub fn with_nearest_neighbors(problem: Problem<Sp, Sa, C, St>, nn: N) -> Result<Self, Error> {
let start_validity = problem.checker.check_state(&problem.start);
if !start_validity.is_valid() {
return Err(Error::InvalidStart(start_validity));
}
let goal_validity = problem.checker.check_state(problem.goal.center());
if !goal_validity.is_valid() {
return Err(Error::InvalidGoal(goal_validity));
}
let solution = problem
.goal
.contains(&problem.space, &problem.start)
.then(|| Path {
states: alloc::vec![problem.start.clone()],
cost: 0.0,
});
Ok(Self {
start: Half::rooted(problem.start.clone(), nn.clone()),
goal: Half::rooted(problem.goal.center().clone(), nn),
problem,
active: Side::Start,
solution,
})
}
pub fn step<R: Rng + ?Sized>(&mut self, rng: &mut R) -> bool {
if self.solution.is_some() {
return true;
}
let side = self.active;
self.active = side.other();
let target = self.problem.sampler.sample(rng);
if let Some(new) = self.extend(side, &target) {
self.check_direct_goal(side, new);
if self.solution.is_none() {
self.connect(side.other(), new, side);
}
}
self.solution.is_some()
}
pub fn solve<R: Rng + ?Sized>(
&mut self,
max_iterations: usize,
rng: &mut R,
) -> Option<Path<Sp::State>> {
for _ in 0..max_iterations {
if self.step(rng) {
break;
}
}
self.solution.clone()
}
#[must_use]
pub const fn path(&self) -> Option<&Path<Sp::State>> {
self.solution.as_ref()
}
#[must_use]
pub fn node_count(&self) -> usize {
self.start.tree.len() + self.goal.tree.len()
}
const fn half(&self, side: Side) -> &Half<Sp::State, N> {
match side {
Side::Start => &self.start,
Side::Goal => &self.goal,
}
}
fn extend(&mut self, side: Side, target: &Sp::State) -> Option<usize> {
let nearest = self.nearest(side, target);
self.step_from(side, nearest, target)
}
fn step_from(&mut self, side: Side, from: usize, target: &Sp::State) -> Option<usize> {
let space = &self.problem.space;
let tree = &self.half(side).tree;
let new = self.problem.steering.steer(space, tree.state(from), target);
if !self.is_edge_valid(side, from, &new) {
return None;
}
let cost = tree.cost(from) + space.distance(tree.state(from), &new);
let half = match side {
Side::Start => &mut self.start,
Side::Goal => &mut self.goal,
};
let id = half.tree.add(new.clone(), from, cost);
half.nn.insert(id, new);
Some(id)
}
fn connect(&mut self, side: Side, target: usize, target_side: Side) {
let goal = self.half(target_side).tree.state(target).clone();
let mut current = self.nearest(side, &goal);
while let Some(next) = self.step_from(side, current, &goal) {
let reached = self.half(side).tree.state(next);
if self.problem.space.distance(reached, &goal) <= 0.0 {
self.record_meeting(side, next, target);
return;
}
current = next;
}
}
fn check_direct_goal(&mut self, side: Side, id: usize) {
let tree = &self.start.tree;
if side == Side::Start
&& self
.problem
.goal
.contains(&self.problem.space, tree.state(id))
{
self.solution = Some(Path {
states: tree.path_to(id),
cost: tree.cost(id),
});
}
}
fn record_meeting(&mut self, side: Side, id: usize, other: usize) {
let (start_id, goal_id) = match side {
Side::Start => (id, other),
Side::Goal => (other, id),
};
let mut states = self.start.tree.path_to(start_id);
let mut to_goal = self.goal.tree.path_to(goal_id);
to_goal.reverse();
states.extend(to_goal.into_iter().skip(1));
self.solution = Some(Path {
states,
cost: self.start.tree.cost(start_id) + self.goal.tree.cost(goal_id),
});
}
fn nearest(&self, side: Side, target: &Sp::State) -> usize {
self.half(side)
.nn
.nearest(&self.problem.space, target)
.expect("each tree always holds its root")
}
fn is_edge_valid(&self, side: Side, parent: usize, new: &Sp::State) -> bool {
let checker = &self.problem.checker;
let space = &self.problem.space;
let parent_state = self.half(side).tree.state(parent);
match side {
Side::Start => checker.check_motion(space, parent_state, new).is_valid(),
Side::Goal => {
checker.check_state(new).is_valid()
&& checker.check_motion(space, new, parent_state).is_valid()
}
}
}
}
impl<S: Clone, N> Half<S, N> {
fn rooted<Sp>(root: S, mut nn: N) -> Self
where
Sp: StateSpace<State = S>,
N: NearestNeighbors<Sp>,
{
nn.insert(0, root.clone());
Self {
tree: Tree::new(root),
nn,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Euclidean, GoalRegion, StraightLine, UniformBox, Validity, discretized_motion};
use rand_core::SeedableRng;
struct OneWay;
impl ValidityChecker<Euclidean<1>> for OneWay {
fn check_state(&self, _: &[f64; 1]) -> Validity {
Validity::Valid
}
fn check_motion(&self, space: &Euclidean<1>, from: &[f64; 1], to: &[f64; 1]) -> Validity {
let crosses = (from[0] < 5.0) != (to[0] < 5.0);
if crosses && to[0] < from[0] {
Validity::Invalid
} else {
discretized_motion(space, from, to, 0.1, |_| Validity::Valid)
}
}
}
#[test]
fn paths_are_valid_in_travel_direction() {
let problem = Problem {
space: Euclidean::<1>,
sampler: UniformBox::new([0.0], [10.0]),
checker: OneWay,
steering: StraightLine::new(0.7),
start: [1.0],
goal: GoalRegion::new([9.0], 0.1),
};
let mut planner = RrtConnect::new(problem).expect("valid");
let path = planner
.solve(10_000, &mut rand_pcg::Pcg64Mcg::seed_from_u64(4))
.expect("the gate is passable left to right");
for pair in path.states.windows(2) {
assert_eq!(
OneWay.check_motion(&Euclidean::<1>, &pair[0], &pair[1]),
Validity::Valid
);
}
assert!((path.cost - 8.0).abs() < 1e-9, "{}", path.cost);
}
#[test]
fn one_way_gate_blocks_the_reverse_direction() {
let problem = Problem {
space: Euclidean::<1>,
sampler: UniformBox::new([0.0], [10.0]),
checker: OneWay,
steering: StraightLine::new(0.7),
start: [9.0],
goal: GoalRegion::new([1.0], 0.1),
};
let mut planner = RrtConnect::new(problem).expect("valid");
assert_eq!(
planner.solve(3000, &mut rand_pcg::Pcg64Mcg::seed_from_u64(4)),
None
);
}
}