robust_planning/checker.rs
1//! A [`ValidityChecker`] that proves motions safe with reachable sets.
2
3use polyzonotope::{Containment, Interval, IntervalBox, Polytope, SparsePolyZonotope, Zonotope};
4use sampling_planning::{Euclidean, Validity, ValidityChecker};
5
6use crate::{Obstacles, Reach, SafetyVerdict, Steer, check_edge};
7
8/// Checks states and motions of `sampling-planning`'s planners with reachable sets.
9///
10/// `sampling-planning` hands a checker only two bare states per motion, so this adapter
11/// assumes a fixed uncertainty *tube*: the real state at every tree node lies in the box
12/// `state ± half_widths`. It then proves each motion against that assumption:
13///
14/// 1. the model's nominal steering from `from` toward `to` ends exactly at `to`;
15/// 2. every set covering the motion, propagated from the box at `from`, is proven disjoint
16/// from every obstacle, at every intermediate time step;
17/// 3. the end set is proven inside the box at `to`, so the assumption holds at the next node
18/// as well and edges chain soundly.
19///
20/// Only then is the motion [`Validity::Valid`]. Anything unproven is [`Validity::Unknown`]
21/// and rejected by the planners. For uncertainty that grows along a branch, use
22/// [`RobustRrt`](crate::RobustRrt), which keeps the propagated set per node.
23#[derive(Debug, Clone)]
24pub struct ReachChecker<R, const N: usize> {
25 /// The obstacles and their split budget.
26 obstacles: Obstacles,
27 /// The dynamics model that steers and propagates.
28 reach: R,
29 /// Half widths of the uncertainty box around every node state. Finite and non-negative.
30 half_widths: [f64; N],
31}
32
33impl<R: Reach<N>, const N: usize> ReachChecker<R, N> {
34 /// Creates a checker with the box `state ± half_widths` as the uncertainty at each node.
35 ///
36 /// The split budget of `obstacles` also bounds the containment check of end sets.
37 ///
38 /// # Panics
39 ///
40 /// Panics if a half width is negative or not finite.
41 #[must_use]
42 pub fn new(obstacles: Obstacles, reach: R, half_widths: [f64; N]) -> Self {
43 assert!(
44 half_widths.iter().all(|r| r.is_finite() && *r >= 0.0),
45 "half widths must be non-negative and finite"
46 );
47
48 Self {
49 obstacles,
50 reach,
51 half_widths,
52 }
53 }
54
55 /// The obstacles.
56 #[must_use]
57 pub const fn obstacles(&self) -> &Obstacles {
58 &self.obstacles
59 }
60
61 /// The dynamics model.
62 #[must_use]
63 pub const fn reach(&self) -> &R {
64 &self.reach
65 }
66
67 /// The uncertainty box around `state`.
68 ///
69 /// # Panics
70 ///
71 /// Panics if `state` has a NaN coordinate.
72 #[must_use]
73 pub fn tube(&self, state: &[f64; N]) -> IntervalBox {
74 IntervalBox(
75 state
76 .iter()
77 .zip(&self.half_widths)
78 .map(|(x, r)| Interval::new(x - r, x + r))
79 .collect(),
80 )
81 }
82
83 /// Whether `end` is proven inside the uncertainty box around `to`.
84 fn returns_into_tube(&self, end: &SparsePolyZonotope, to: &[f64; N]) -> Validity {
85 let tube = Polytope::from_box(&self.tube(to));
86
87 match end.containment(&tube, self.obstacles.max_splits()) {
88 Containment::Inside => Validity::Valid,
89 Containment::NotInside | Containment::Unknown => Validity::Unknown,
90 }
91 }
92}
93
94impl<R: Reach<N> + Steer<N, R::Control>, const N: usize> ValidityChecker<Euclidean<N>>
95 for ReachChecker<R, N>
96{
97 /// Valid only if the uncertainty box around `state` is proven disjoint from every obstacle.
98 fn check_state(&self, state: &[f64; N]) -> Validity {
99 let tube = SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&self.tube(state)));
100
101 self.obstacles.check_set(&tube).into()
102 }
103
104 /// Valid only if the motion is proven safe and returns into the tube at `to`; see the
105 /// type docs.
106 fn check_motion(&self, _: &Euclidean<N>, from: &[f64; N], to: &[f64; N]) -> Validity {
107 let (control, end) = self.reach.steer(from, to);
108
109 #[expect(
110 clippy::float_cmp,
111 reason = "the motion must end exactly at `to`, where the next tube is centered"
112 )]
113 let misses_target = end != *to;
114
115 if misses_target {
116 return Validity::Unknown;
117 }
118
119 let initial = SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&self.tube(from)));
120 let mut cover = Vec::new();
121 let (verdict, end_set) =
122 check_edge(&self.reach, &self.obstacles, &initial, &control, &mut cover);
123
124 if verdict != SafetyVerdict::Safe {
125 return verdict.into();
126 }
127
128 self.returns_into_tube(&end_set, to)
129 }
130}