diff --git a/src/interpolation.rs b/src/interpolation.rs index 85f5860..dd3eaf8 100644 --- a/src/interpolation.rs +++ b/src/interpolation.rs @@ -5,7 +5,7 @@ /// Available kind of interpolations. /// /// Feel free to visit each variant for more documentation. -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))] pub enum Interpolation { diff --git a/src/key.rs b/src/key.rs index ced101e..bfba08a 100644 --- a/src/key.rs +++ b/src/key.rs @@ -17,7 +17,7 @@ use crate::interpolation::Interpolation; /// key and the next one – if existing. Have a look at [`Interpolation`] for further details. /// /// [`Interpolation`]: crate::interpolation::Interpolation -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))] pub struct Key { diff --git a/src/spline.rs b/src/spline.rs index a7e0379..58b5e02 100644 --- a/src/spline.rs +++ b/src/spline.rs @@ -52,6 +52,18 @@ impl Spline { &self.0 } + /// Number of keys. + #[inline(always)] + pub fn len(&self) -> usize { + self.0.len() + } + + /// Check whether the spline has no key. + #[inline(always)] + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + /// Sample a spline at a given time. /// /// The current implementation, based on immutability, cannot perform in constant time. This means @@ -146,6 +158,15 @@ impl Spline { } }) } + + /// Remove a ke from the spline. + pub fn remove(&mut self, index: usize) -> Option> { + if index >= self.0.len() { + None + } else { + Some(self.0.remove(index)) + } + } } // Normalize a time ([0;1]) given two control points. diff --git a/tests/mod.rs b/tests/mod.rs index a73b18b..f66f383 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -172,3 +172,27 @@ fn nalgebra_vector_interpolation() { assert_eq!(Interpolate::lerp(start, end, 1.0), end); assert_eq!(Interpolate::lerp(start, end, 0.5), mid); } + +#[test] +fn remove_element_empty() { + let mut spline: Spline = Spline::from_vec(vec![]); + let removed = spline.remove(0); + + assert_eq!(removed, None); + assert!(spline.is_empty()); +} + +#[test] +fn remove_element() { + let start = Key::new(0., 0., Interpolation::Step(0.5)); + let k1 = Key::new(1., 5., Interpolation::Linear); + let k2 = Key::new(2., 0., Interpolation::Step(0.1)); + let k3 = Key::new(3., 1., Interpolation::Linear); + let k4 = Key::new(10., 2., Interpolation::Linear); + let end = Key::new(11., 4., Interpolation::default()); + let mut spline = Spline::from_vec(vec![start, k1, k2.clone(), k3, k4, end]); + let removed = spline.remove(2); + + assert_eq!(removed, Some(k2)); + assert_eq!(spline.len(), 5); +}