splines/src/nalgebra.rs

53 lines
1.5 KiB
Rust
Raw Normal View History

2019-04-21 17:54:24 +02:00
use alga::general::{ClosedAdd, ClosedDiv, ClosedMul, ClosedSub};
use nalgebra::{Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6};
2019-04-21 17:54:24 +02:00
use num_traits as nt;
use std::ops::Mul;
2019-04-21 17:54:24 +02:00
use crate::interpolate::{Interpolate, Linear, Additive, One, cubic_hermite_def};
2019-04-21 17:54:24 +02:00
macro_rules! impl_interpolate_vector {
($($t:tt)*) => {
2019-04-21 17:54:24 +02:00
// implement Linear
impl<T> Linear<T> for $($t)*<T> where T: Scalar + ClosedAdd + ClosedSub + ClosedMul + ClosedDiv {
2019-04-21 18:17:01 +02:00
#[inline(always)]
2019-04-21 17:54:24 +02:00
fn outer_mul(self, t: T) -> Self {
self * t
}
2019-04-21 18:17:01 +02:00
#[inline(always)]
2019-04-21 17:54:24 +02:00
fn outer_div(self, t: T) -> Self {
self / t
}
}
impl<T, V> Interpolate<T> for $($t)*<V>
where Self: Linear<T>,
T: Additive + One + Mul<T, Output = T>,
V: nt::One +
nt::Zero +
Additive +
Scalar +
ClosedAdd +
ClosedMul +
ClosedSub +
Interpolate<T> {
2019-04-21 18:17:01 +02:00
#[inline(always)]
fn lerp(a: Self, b: Self, t: T) -> Self {
2019-04-21 17:54:24 +02:00
Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
}
2019-04-21 18:17:01 +02:00
#[inline(always)]
2019-04-21 17:54:24 +02:00
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)
}
}
}
}
2019-04-21 17:54:24 +02:00
impl_interpolate_vector!(Vector1);
impl_interpolate_vector!(Vector2);
impl_interpolate_vector!(Vector3);
impl_interpolate_vector!(Vector4);
impl_interpolate_vector!(Vector5);
impl_interpolate_vector!(Vector6);