Make Spline<T>::clamped_sample return Option<T> instead. #9
This commit is contained in:
parent
bc329fe736
commit
55e792a98b
28
src/lib.rs
28
src/lib.rs
@ -45,7 +45,7 @@
|
||||
//! # let end = Key::new(1., 10., Interpolation::Linear);
|
||||
//! # let spline = Spline::from_vec(vec![start, end]);
|
||||
//! assert_eq!(spline.sample(0.), Some(0.));
|
||||
//! assert_eq!(spline.clamped_sample(1.), 10.);
|
||||
//! assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||
//! assert_eq!(spline.sample(1.1), None);
|
||||
//! ```
|
||||
//!
|
||||
@ -58,8 +58,8 @@
|
||||
//! # let start = Key::new(0., 0., Interpolation::Linear);
|
||||
//! # let end = Key::new(1., 10., Interpolation::Linear);
|
||||
//! # let spline = Spline::from_vec(vec![start, end]);
|
||||
//! assert_eq!(spline.clamped_sample(-0.9), 0.); // clamped to the first key
|
||||
//! assert_eq!(spline.clamped_sample(1.1), 10.); // clamped to the last key
|
||||
//! assert_eq!(spline.clamped_sample(-0.9), Some(0.)); // clamped to the first key
|
||||
//! assert_eq!(spline.clamped_sample(1.1), Some(10.)); // clamped to the last key
|
||||
//! ```
|
||||
//!
|
||||
//! # Features and customization
|
||||
@ -284,20 +284,26 @@ impl<T> Spline<T> {
|
||||
/// If you sample before the first key or after the last one, return the first key or the last
|
||||
/// one, respectively. Otherwise, behave the same way as `Spline::sample`.
|
||||
///
|
||||
/// # Panic
|
||||
/// # Error
|
||||
///
|
||||
/// This function panics if you have no key.
|
||||
pub fn clamped_sample(&self, t: f32) -> T where T: Interpolate {
|
||||
/// This function returns `None` if you have no key.
|
||||
pub fn clamped_sample(&self, t: f32) -> Option<T> where T: Interpolate {
|
||||
if self.0.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let first = self.0.first().unwrap();
|
||||
let last = self.0.last().unwrap();
|
||||
|
||||
if t <= first.t {
|
||||
return first.value;
|
||||
let sampled = if t <= first.t {
|
||||
first.value
|
||||
} else if t >= last.t {
|
||||
return last.value;
|
||||
}
|
||||
|
||||
last.value
|
||||
} else {
|
||||
self.sample(t).unwrap()
|
||||
};
|
||||
|
||||
Some(sampled)
|
||||
}
|
||||
}
|
||||
|
||||
|
14
tests/mod.rs
14
tests/mod.rs
@ -18,7 +18,7 @@ fn step_interpolation_0() {
|
||||
assert_eq!(spline.sample(0.5), Some(10.));
|
||||
assert_eq!(spline.sample(0.9), Some(10.));
|
||||
assert_eq!(spline.sample(1.), None);
|
||||
assert_eq!(spline.clamped_sample(1.), 10.);
|
||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -33,7 +33,7 @@ fn step_interpolation_0_5() {
|
||||
assert_eq!(spline.sample(0.5), Some(10.));
|
||||
assert_eq!(spline.sample(0.9), Some(10.));
|
||||
assert_eq!(spline.sample(1.), None);
|
||||
assert_eq!(spline.clamped_sample(1.), 10.);
|
||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -48,7 +48,7 @@ fn step_interpolation_0_75() {
|
||||
assert_eq!(spline.sample(0.5), Some(0.));
|
||||
assert_eq!(spline.sample(0.9), Some(10.));
|
||||
assert_eq!(spline.sample(1.), None);
|
||||
assert_eq!(spline.clamped_sample(1.), 10.);
|
||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -63,7 +63,7 @@ fn step_interpolation_1() {
|
||||
assert_eq!(spline.sample(0.5), Some(0.));
|
||||
assert_eq!(spline.sample(0.9), Some(0.));
|
||||
assert_eq!(spline.sample(1.), None);
|
||||
assert_eq!(spline.clamped_sample(1.), 10.);
|
||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -78,7 +78,7 @@ fn linear_interpolation() {
|
||||
assert_eq!(spline.sample(0.5), Some(5.));
|
||||
assert_eq!(spline.sample(0.9), Some(9.));
|
||||
assert_eq!(spline.sample(1.), None);
|
||||
assert_eq!(spline.clamped_sample(1.), 10.);
|
||||
assert_eq!(spline.clamped_sample(1.), Some(10.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -103,7 +103,7 @@ fn linear_interpolation_several_keys() {
|
||||
assert_eq!(spline.sample(3.), Some(1.));
|
||||
assert_eq!(spline.sample(6.5), Some(1.5));
|
||||
assert_eq!(spline.sample(10.), Some(2.));
|
||||
assert_eq!(spline.clamped_sample(11.), 4.);
|
||||
assert_eq!(spline.clamped_sample(11.), Some(4.));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -130,7 +130,7 @@ fn several_interpolations_several_keys() {
|
||||
assert_eq!(spline.sample(3.), Some(1.));
|
||||
assert_eq!(spline.sample(6.5), Some(1.5));
|
||||
assert_eq!(spline.sample(10.), Some(2.));
|
||||
assert_eq!(spline.clamped_sample(11.), 4.);
|
||||
assert_eq!(spline.clamped_sample(11.), Some(4.));
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
|
Loading…
Reference in New Issue
Block a user