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