use polyzonotope::Intersection;
use sampling_planning::Validity;
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub enum SafetyVerdict {
Safe,
Unsafe(Witness),
Indeterminate,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Witness {
pub point: Vec<f64>,
pub set: usize,
pub obstacle: usize,
}
impl SafetyVerdict {
pub const fn from_intersection(intersection: Intersection) -> Self {
match intersection {
Intersection::Disjoint => Self::Safe,
Intersection::Intersects | Intersection::Unknown => Self::Indeterminate,
}
}
#[must_use]
pub const fn is_safe(&self) -> bool {
matches!(self, Self::Safe)
}
pub fn and(self, other: Self) -> Self {
match (self, other) {
(Self::Unsafe(witness), _) | (_, Self::Unsafe(witness)) => Self::Unsafe(witness),
(Self::Indeterminate, _) | (_, Self::Indeterminate) => Self::Indeterminate,
(Self::Safe, Self::Safe) => Self::Safe,
}
}
}
impl From<SafetyVerdict> for Validity {
fn from(verdict: SafetyVerdict) -> Self {
match verdict {
SafetyVerdict::Safe => Self::Valid,
SafetyVerdict::Unsafe(_) => Self::Invalid,
SafetyVerdict::Indeterminate => Self::Unknown,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_disjoint_is_safe() {
assert_eq!(
SafetyVerdict::from_intersection(Intersection::Disjoint),
SafetyVerdict::Safe
);
assert_eq!(
SafetyVerdict::from_intersection(Intersection::Intersects),
SafetyVerdict::Indeterminate
);
assert_eq!(
SafetyVerdict::from_intersection(Intersection::Unknown),
SafetyVerdict::Indeterminate
);
}
#[test]
fn unsafe_dominates_indeterminate_dominates_safe() {
use SafetyVerdict::{Indeterminate, Safe, Unsafe};
let unsafe_at = |obstacle| {
Unsafe(Witness {
point: vec![0.0, 0.0],
set: 0,
obstacle,
})
};
assert_eq!(Safe.and(Safe), Safe);
assert_eq!(Safe.and(Indeterminate), Indeterminate);
assert_eq!(Indeterminate.and(unsafe_at(1)), unsafe_at(1));
assert_eq!(unsafe_at(1).and(Safe), unsafe_at(1));
assert_eq!(unsafe_at(1).and(unsafe_at(2)), unsafe_at(1));
}
#[test]
fn maps_to_validity() {
assert_eq!(Validity::from(SafetyVerdict::Safe), Validity::Valid);
let witness = Witness {
point: vec![0.0],
set: 0,
obstacle: 0,
};
assert_eq!(
Validity::from(SafetyVerdict::Unsafe(witness)),
Validity::Invalid
);
assert_eq!(
Validity::from(SafetyVerdict::Indeterminate),
Validity::Unknown
);
}
}