use polyzonotope::{Polytope, SparsePolyZonotope};
use sampling_planning::{Euclidean, Steering, StraightLine};
use crate::Reach;
pub trait Steer<const N: usize, C> {
fn steer(&self, from: &[f64; N], toward: &[f64; N]) -> (C, [f64; N]);
}
impl<const N: usize> Steer<N, [f64; N]> for StraightLine {
fn steer(&self, from: &[f64; N], toward: &[f64; N]) -> ([f64; N], [f64; N]) {
let end = Steering::steer(self, &Euclidean::<N>, from, toward);
(end, end)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct WithSteering<R, S> {
pub reach: R,
pub steering: S,
}
impl<R, S> WithSteering<R, S> {
#[must_use]
pub const fn new(reach: R, steering: S) -> Self {
Self { reach, steering }
}
}
impl<R: Reach<N>, S, const N: usize> Reach<N> for WithSteering<R, S> {
type Control = R::Control;
fn propagate(
&self,
initial: &SparsePolyZonotope,
control: &Self::Control,
cover: &mut Vec<SparsePolyZonotope>,
) -> Option<SparsePolyZonotope> {
self.reach.propagate(initial, control, cover)
}
fn certify_collision(
&self,
initial: &SparsePolyZonotope,
control: &Self::Control,
set: &SparsePolyZonotope,
obstacle: &Polytope,
) -> Option<Vec<f64>> {
self.reach
.certify_collision(initial, control, set, obstacle)
}
}
impl<R, S: Steer<N, C>, C, const N: usize> Steer<N, C> for WithSteering<R, S> {
fn steer(&self, from: &[f64; N], toward: &[f64; N]) -> (C, [f64; N]) {
self.steering.steer(from, toward)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn straight_line_reaches_near_targets_exactly_and_clamps_far_ones() {
let steering = StraightLine::new(1.0);
assert_eq!(
Steer::steer(&steering, &[0.0, 0.0], &[0.6, 0.8]),
([0.6, 0.8], [0.6, 0.8])
);
let (control, end) = Steer::steer(&steering, &[0.0, 0.0], &[30.0, 40.0]);
assert_eq!(control, end);
assert!((end[0] - 0.6).abs() < 1e-12 && (end[1] - 0.8).abs() < 1e-12);
}
}