Implement impl-cgmath.
This commit is contained in:
parent
9d5971a5f7
commit
70d6cf2081
@ -28,7 +28,7 @@ std = []
|
||||
|
||||
[dependencies]
|
||||
alga = { version = "0.9", optional = true }
|
||||
cgmath = { version = "0.17", optional = true }
|
||||
cgmath = { version = "0.16", optional = true }
|
||||
nalgebra = { version = ">=0.14, <0.19", optional = true }
|
||||
num-traits = { version = "0.2", optional = true }
|
||||
serde = { version = "1", optional = true }
|
||||
|
@ -1,26 +1,52 @@
|
||||
use cgmath::{BaseNum, InnerSpace, Quaternion, VectorSpace, Vector2, Vector3, Vector4};
|
||||
use num_traits::Float;
|
||||
use cgmath::{BaseFloat, BaseNum, InnerSpace, Quaternion, VectorSpace, Vector1, Vector2, Vector3, Vector4};
|
||||
|
||||
use crate::interpolate::{Interpolate, cubic_hermite_def};
|
||||
use crate::interpolate::{Additive, Interpolate, Linear, One, cubic_hermite_def};
|
||||
|
||||
macro_rules! impl_interpolate_vec {
|
||||
($t:ty, $($q:tt)*) => {
|
||||
impl Interpolate<$t> for $($q)*<$t> {
|
||||
fn lerp(a: Self, b: Self, t: $t) -> Self {
|
||||
($($t:tt)*) => {
|
||||
impl<T> Linear<T> for $($t)*<T> where T: BaseNum {
|
||||
fn outer_mul(self, t: T) -> Self {
|
||||
self * t
|
||||
}
|
||||
|
||||
fn outer_div(self, t: T) -> Self {
|
||||
self / t
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Interpolate<T> for $($t)*<T> where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
|
||||
fn lerp(a: Self, b: Self, t: T) -> Self {
|
||||
a.lerp(b, t)
|
||||
}
|
||||
|
||||
fn cubic_hermite(x: (Self, $t), a: (Self, $t), b: (Self, $t), y: (Self, $t), t: $t) -> Self {
|
||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
||||
cubic_hermite_def(x, a, b, y, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_interpolate_vec!(f32, Vector2);
|
||||
impl_interpolate_vec!(Vector1);
|
||||
impl_interpolate_vec!(Vector2);
|
||||
impl_interpolate_vec!(Vector3);
|
||||
impl_interpolate_vec!(Vector4);
|
||||
|
||||
//impl Interpolate for Quaternion<f32> {
|
||||
// fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
// a.nlerp(b, t)
|
||||
// }
|
||||
//}
|
||||
impl<T> Linear<T> for Quaternion<T> where T: BaseFloat {
|
||||
fn outer_mul(self, t: T) -> Self {
|
||||
self * t
|
||||
}
|
||||
|
||||
fn outer_div(self, t: T) -> Self {
|
||||
self / t
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Interpolate<T> for Quaternion<T> where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
|
||||
fn lerp(a: Self, b: Self, t: T) -> Self {
|
||||
a.nlerp(b, t)
|
||||
}
|
||||
|
||||
fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
|
||||
cubic_hermite_def(x, a, b, y, t)
|
||||
}
|
||||
}
|
||||
|
@ -27,16 +27,12 @@ pub trait Interpolate<T>: Sized + Copy {
|
||||
/// A trait for anything that supports additions, subtraction, multiplication and division.
|
||||
pub trait Additive:
|
||||
Copy +
|
||||
PartialEq +
|
||||
PartialOrd +
|
||||
Add<Self, Output = Self> +
|
||||
Sub<Self, Output = Self> {
|
||||
}
|
||||
|
||||
impl<T> Additive for T
|
||||
where T: Copy +
|
||||
PartialEq +
|
||||
PartialOrd +
|
||||
Add<Self, Output = Self> +
|
||||
Sub<Self, Output = Self> {
|
||||
}
|
||||
|
@ -53,7 +53,9 @@ impl<T, V> Spline<T, V> {
|
||||
/// sampling impossible. For instance, `Interpolate::CatmullRom` requires *four* keys. If you’re
|
||||
/// near the beginning of the spline or its end, ensure you have enough keys around to make the
|
||||
/// sampling.
|
||||
pub fn sample(&self, t: T) -> Option<V> where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T>, V: Interpolate<T> {
|
||||
pub fn sample(&self, t: T) -> Option<V>
|
||||
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
||||
V: Interpolate<T> {
|
||||
let keys = &self.0;
|
||||
let i = search_lower_cp(keys, t)?;
|
||||
let cp0 = &keys[i];
|
||||
@ -108,7 +110,9 @@ impl<T, V> Spline<T, V> {
|
||||
/// # Error
|
||||
///
|
||||
/// This function returns `None` if you have no key.
|
||||
pub fn clamped_sample(&self, t: T) -> Option<V> where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T>, V: Interpolate<T> {
|
||||
pub fn clamped_sample(&self, t: T) -> Option<V>
|
||||
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
|
||||
V: Interpolate<T> {
|
||||
if self.0.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@ -136,7 +140,7 @@ pub(crate) fn normalize_time<T, V>(
|
||||
t: T,
|
||||
cp: &Key<T, V>,
|
||||
cp1: &Key<T, V>
|
||||
) -> T where T: Additive + Div<T, Output = T> {
|
||||
) -> T where T: Additive + Div<T, Output = T> + PartialEq {
|
||||
assert!(cp1.t != cp.t, "overlapping keys");
|
||||
(t - cp.t) / (cp1.t - cp.t)
|
||||
}
|
||||
|
18
tests/mod.rs
18
tests/mod.rs
@ -1,7 +1,7 @@
|
||||
use splines::{Interpolation, Key, Spline};
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
use nalgebra as na;
|
||||
#[cfg(feature = "impl-cgmath")] use cgmath as cg;
|
||||
#[cfg(feature = "impl-nalgebra")] use nalgebra as na;
|
||||
|
||||
#[test]
|
||||
fn step_interpolation_f32() {
|
||||
@ -145,6 +145,20 @@ fn several_interpolations_several_keys() {
|
||||
assert_eq!(spline.clamped_sample(11.), Some(4.));
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-cgmath")]
|
||||
#[test]
|
||||
fn cgmath_vector_interpolation() {
|
||||
use splines::Interpolate;
|
||||
|
||||
let start = cg::Vector2::new(0.0, 0.0);
|
||||
let mid = cg::Vector2::new(0.5, 0.5);
|
||||
let end = cg::Vector2::new(1.0, 1.0);
|
||||
|
||||
assert_eq!(Interpolate::lerp(start, end, 0.0), start);
|
||||
assert_eq!(Interpolate::lerp(start, end, 1.0), end);
|
||||
assert_eq!(Interpolate::lerp(start, end, 0.5), mid);
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
#[test]
|
||||
fn nalgebra_vector_interpolation() {
|
||||
|
Loading…
Reference in New Issue
Block a user