robust_planning/obstacles.rs
1//! Polytope obstacles and the safety check of reachable sets against them.
2
3use polyzonotope::{Polytope, SparsePolyZonotope};
4
5use crate::SafetyVerdict;
6
7/// A set of convex polytope obstacles, checked against reachable sets.
8///
9/// Each check calls [`SparsePolyZonotope::intersection`] with a split budget of
10/// [`Obstacles::max_splits`]. Splitting refines the enclosure where it is too coarse to
11/// separate a set from an obstacle, so a set is only given up on after the budget is spent.
12///
13/// # Examples
14///
15/// ```
16/// use robust_planning::{Obstacles, SafetyVerdict};
17/// use robust_planning::polyzonotope::{
18/// Interval, IntervalBox, Polytope, SparsePolyZonotope, Zonotope,
19/// };
20///
21/// let square = |lo: f64, hi: f64| IntervalBox(vec![Interval::new(lo, hi); 2]);
22/// let obstacles = Obstacles::new(vec![Polytope::from_box(&square(2.0, 3.0))], 4);
23///
24/// let clear = SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&square(0.0, 1.0)));
25/// let touching = SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&square(1.5, 2.5)));
26///
27/// assert_eq!(obstacles.check_set(&clear), SafetyVerdict::Safe);
28/// assert_eq!(obstacles.check_set(&touching), SafetyVerdict::Indeterminate);
29/// ```
30#[derive(Debug, Clone, PartialEq)]
31pub struct Obstacles {
32 /// The obstacles. A state inside any of them is unsafe.
33 polytopes: Vec<Polytope>,
34 /// Split budget per set and obstacle; `0` disables refinement.
35 max_splits: usize,
36}
37
38impl Obstacles {
39 /// Creates an obstacle set with the given split budget per set and obstacle.
40 #[must_use]
41 pub const fn new(polytopes: Vec<Polytope>, max_splits: usize) -> Self {
42 Self {
43 polytopes,
44 max_splits,
45 }
46 }
47
48 /// The obstacles.
49 #[must_use]
50 pub fn polytopes(&self) -> &[Polytope] {
51 &self.polytopes
52 }
53
54 /// Split budget per set and obstacle.
55 #[must_use]
56 pub const fn max_splits(&self) -> usize {
57 self.max_splits
58 }
59
60 /// Checks one set against every obstacle.
61 ///
62 /// Returns [`SafetyVerdict::Safe`] only if the set is proven disjoint from every obstacle.
63 /// Stops at the first obstacle that is not proven disjoint and returns its verdict.
64 ///
65 /// # Panics
66 ///
67 /// Panics if an obstacle's facet normals do not have the set's dimension.
68 pub fn check_set(&self, set: &SparsePolyZonotope) -> SafetyVerdict {
69 for obstacle in &self.polytopes {
70 let verdict =
71 SafetyVerdict::from_intersection(set.intersection(obstacle, self.max_splits));
72
73 if !verdict.is_safe() {
74 return verdict;
75 }
76 }
77
78 SafetyVerdict::Safe
79 }
80
81 /// Checks every set, for example all sets covering one edge.
82 ///
83 /// Returns [`SafetyVerdict::Safe`] only if every set is proven disjoint from every
84 /// obstacle; an empty list is safe. Stops at the first set that is not proven safe.
85 ///
86 /// # Panics
87 ///
88 /// Panics if an obstacle's facet normals do not have a set's dimension.
89 pub fn check_sets(&self, sets: &[SparsePolyZonotope]) -> SafetyVerdict {
90 for set in sets {
91 let verdict = self.check_set(set);
92
93 if !verdict.is_safe() {
94 return verdict;
95 }
96 }
97
98 SafetyVerdict::Safe
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use nalgebra::{DMatrix, DVector};
105 use polyzonotope::{Interval, IntervalBox, Zonotope};
106
107 use super::*;
108
109 /// A diamond `|x| + |y| <= 1` near a box corner: disjoint, but no single facet of the box
110 /// separates it, so proving disjointness needs a split.
111 fn diamond_near_corner() -> (SparsePolyZonotope, Polytope) {
112 let generators = DMatrix::from_column_slice(2, 2, &[0.5, 0.5, 0.5, -0.5]);
113 let diamond = Zonotope::new(DVector::zeros(2), generators);
114 let corner = IntervalBox(vec![Interval::new(0.6, 2.0); 2]);
115
116 (
117 SparsePolyZonotope::from_zonotope(&diamond),
118 Polytope::from_box(&corner),
119 )
120 }
121
122 #[test]
123 fn unknown_without_splits_is_indeterminate() {
124 let (diamond, obstacle) = diamond_near_corner();
125 let obstacles = Obstacles::new(vec![obstacle], 0);
126
127 assert_eq!(obstacles.check_set(&diamond), SafetyVerdict::Indeterminate);
128 }
129
130 #[test]
131 fn splitting_proves_disjointness() {
132 let (diamond, obstacle) = diamond_near_corner();
133 let obstacles = Obstacles::new(vec![obstacle], 4);
134
135 assert_eq!(obstacles.check_set(&diamond), SafetyVerdict::Safe);
136 }
137
138 #[test]
139 fn overlapping_set_is_not_safe() {
140 let (diamond, _) = diamond_near_corner();
141 let overlap = Polytope::from_box(&IntervalBox(vec![Interval::new(0.2, 2.0); 2]));
142 let obstacles = Obstacles::new(vec![overlap], 8);
143
144 assert_eq!(obstacles.check_set(&diamond), SafetyVerdict::Indeterminate);
145 assert!(obstacles.check_sets(&[]).is_safe());
146 }
147}