robust_planning/
checker.rs1use polyzonotope::{Containment, Interval, IntervalBox, Polytope, SparsePolyZonotope, Zonotope};
4use sampling_planning::{Euclidean, Validity, ValidityChecker};
5
6use crate::{Obstacles, Reach, SafetyVerdict, Steer, check_edge};
7
8#[derive(Debug, Clone)]
24pub struct ReachChecker<R, const N: usize> {
25 obstacles: Obstacles,
27 reach: R,
29 half_widths: [f64; N],
31}
32
33impl<R: Reach<N>, const N: usize> ReachChecker<R, N> {
34 #[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 #[must_use]
57 pub const fn obstacles(&self) -> &Obstacles {
58 &self.obstacles
59 }
60
61 #[must_use]
63 pub const fn reach(&self) -> &R {
64 &self.reach
65 }
66
67 #[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 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 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 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 let Some(end_set) = end_set.filter(|_| verdict == SafetyVerdict::Safe) else {
124 return verdict.into();
125 };
126
127 self.returns_into_tube(&end_set, to)
128 }
129}