Implement impl-cgmath.
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user