Skip to main content

robust_planning/
node.rs

1//! Tree nodes that carry a reachable set, shared by [`RobustRrt`](crate::RobustRrt) and
2//! [`ReachSearch`](crate::ReachSearch).
3
4use polyzonotope::SparsePolyZonotope;
5use sampling_planning::{Euclidean, Path, StateSpace};
6
7/// A node of a planner tree: a nominal state, the reachable set there and the edge from its
8/// parent.
9#[derive(Debug, Clone)]
10pub struct Node<C, const N: usize> {
11    /// The nominal state the node's control drives toward.
12    pub nominal: [f64; N],
13    /// Every state the system can be in at this node.
14    pub set: SparsePolyZonotope,
15    /// The parent node id; `None` only for the root.
16    pub parent: Option<usize>,
17    /// The control from the parent to this node; `None` only for the root.
18    pub control: Option<C>,
19}
20
21/// The node ids from the root to `node`, inclusive.
22///
23/// # Panics
24///
25/// Panics if `node` or one of its ancestors is not an index into `nodes`.
26pub(crate) fn branch<C, const N: usize>(nodes: &[Node<C, N>], node: usize) -> Vec<usize> {
27    let mut ids = vec![node];
28    let mut cursor = node;
29
30    while let Some(parent) = nodes[cursor].parent {
31        ids.push(parent);
32        cursor = parent;
33    }
34
35    ids.reverse();
36    ids
37}
38
39/// The nominal path from the root to `node`, with its Euclidean length as the cost.
40///
41/// # Panics
42///
43/// Panics if `node` or one of its ancestors is not an index into `nodes`.
44pub(crate) fn nominal_path<C, const N: usize>(nodes: &[Node<C, N>], node: usize) -> Path<[f64; N]> {
45    let states: Vec<[f64; N]> = branch(nodes, node)
46        .into_iter()
47        .map(|id| nodes[id].nominal)
48        .collect();
49    let cost = states
50        .windows(2)
51        .map(|pair| Euclidean::<N>.distance(&pair[0], &pair[1]))
52        .sum();
53
54    Path { states, cost }
55}