use alloc::vec;
use alloc::vec::Vec;
use nalgebra::DVector;
use crate::IntervalBox;
#[derive(Clone, Debug, PartialEq)]
pub struct Halfspace {
pub normal: DVector<f64>,
pub offset: f64,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Polytope {
pub facets: Vec<Halfspace>,
}
impl Polytope {
#[must_use]
pub fn from_box(b: &IntervalBox) -> Self {
let n = b.dim();
let axis = |i: usize, s: f64| DVector::from_fn(n, |k, _| if k == i { s } else { 0.0 });
let facets =
b.0.iter()
.enumerate()
.flat_map(|(i, iv)| {
vec![
Halfspace {
normal: axis(i, 1.0),
offset: iv.hi,
},
Halfspace {
normal: axis(i, -1.0),
offset: -iv.lo,
},
]
})
.collect();
Self { facets }
}
#[must_use]
pub fn contains(&self, p: &DVector<f64>, tol: f64) -> bool {
self.facets
.iter()
.all(|h| h.normal.dot(p) <= h.offset + tol)
}
}
impl From<Halfspace> for Polytope {
fn from(h: Halfspace) -> Self {
Self { facets: vec![h] }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Intersection {
Disjoint,
Intersects,
Unknown,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Containment {
Inside,
NotInside,
Unknown,
}