Implement impl-nalgebra feature.

This commit is contained in:
Dimitri Sabadie 2019-04-21 17:54:24 +02:00
parent 427895ab10
commit 65a713c51b
5 changed files with 214 additions and 49 deletions

View File

@ -21,14 +21,15 @@ maintenance = { status = "actively-developed" }
[features] [features]
default = ["std"] default = ["std"]
serialization = ["serde", "serde_derive"]
std = ["num-traits/std"]
impl-cgmath = ["cgmath"] impl-cgmath = ["cgmath"]
impl-nalgebra = ["nalgebra"] impl-nalgebra = ["alga", "nalgebra", "num-traits"]
serialization = ["serde", "serde_derive"]
std = []
[dependencies] [dependencies]
alga = { version = "0.9", optional = true }
cgmath = { version = "0.17", optional = true } cgmath = { version = "0.17", optional = true }
nalgebra = { version = ">=0.14, <0.19", optional = true } nalgebra = { version = ">=0.14, <0.19", optional = true }
num-traits = { version = "0.2", default-features = false } num-traits = { version = "0.2", optional = true }
serde = { version = "1", optional = true } serde = { version = "1", optional = true }
serde_derive = { version = "1", optional = true } serde_derive = { version = "1", optional = true }

View File

@ -1,7 +1,11 @@
#[cfg(feature = "std")] use std::ops::{Div, Mul}; #[cfg(feature = "std")] use std::f32;
#[cfg(not(feature = "std"))] use core::ops::{Div, Mul}; #[cfg(not(feature = "std"))] use core::f32;
#[cfg(not(feature = "std"))] use core::intrinsics::cosf32;
use num_traits::Float; #[cfg(feature = "std")] use std::f64;
#[cfg(not(feature = "std"))] use core::f64;
#[cfg(not(feature = "std"))] use core::intrinsics::cosf64;
#[cfg(feature = "std")] use std::ops::{Add, Mul, Sub};
#[cfg(not(feature = "std"))] use core::ops::{Add, Mul, Sub};
/// Keys that can be interpolated in between. Implementing this trait is required to perform /// Keys that can be interpolated in between. Implementing this trait is required to perform
/// sampling on splines. /// sampling on splines.
@ -20,15 +24,147 @@ 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> {
}
/// Linear combination.
pub trait Linear<T> {
/// Apply an outer multiplication law.
fn outer_mul(self, t: T) -> Self;
/// Apply an outer division law.
fn outer_div(self, t: T) -> Self;
}
macro_rules! impl_linear_simple {
($t:ty) => {
impl Linear<$t> for $t {
fn outer_mul(self, t: $t) -> Self {
self * t
}
/// Apply an outer division law.
fn outer_div(self, t: $t) -> Self {
self / t
}
}
}
}
impl_linear_simple!(f32);
impl_linear_simple!(f64);
macro_rules! impl_linear_cast {
($t:ty, $q:ty) => {
impl Linear<$t> for $q {
fn outer_mul(self, t: $t) -> Self {
self * t as $q
}
/// Apply an outer division law.
fn outer_div(self, t: $t) -> Self {
self / t as $q
}
}
}
}
impl_linear_cast!(f32, f64);
impl_linear_cast!(f64, f32);
/// Types with a neutral element for multiplication.
pub trait One {
/// Return the neutral element for the multiplicative monoid.
fn one() -> Self;
}
macro_rules! impl_one_float {
($t:ty) => {
impl One for $t {
#[inline(always)]
fn one() -> Self {
1.
}
}
}
}
impl_one_float!(f32);
impl_one_float!(f64);
/// Types with a sane definition of π and cosine.
pub trait Trigo {
/// π.
fn pi() -> Self;
/// Cosine of the argument.
fn cos(self) -> Self;
}
impl Trigo for f32 {
#[inline(always)]
fn pi() -> Self {
f32::consts::PI
}
#[inline(always)]
fn cos(self) -> Self {
#[cfg(feature = "std")]
{
self.cos()
}
#[cfg(not(feature = "std"))]
{
unsafe { cosf32(self) }
}
}
}
impl Trigo for f64 {
#[inline(always)]
fn pi() -> Self {
f64::consts::PI
}
#[inline(always)]
fn cos(self) -> Self {
#[cfg(feature = "std")]
{
self.cos()
}
#[cfg(not(feature = "std"))]
{
unsafe { cosf64(self) }
}
}
}
// Default implementation of Interpolate::cubic_hermite. // Default implementation of Interpolate::cubic_hermite.
// //
// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time). // `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
pub(crate) fn cubic_hermite_def<V, T>(x: (V, T), a: (V, T), b: (V, T), y: (V, T), t: T) -> V pub(crate) fn cubic_hermite_def<V, T>(x: (V, T), a: (V, T), b: (V, T), y: (V, T), t: T) -> V
where V: Float + Mul<T, Output = V> + Div<T, Output = V>, where V: Additive + Linear<T>,
T: Float { T: Additive + Mul<T, Output = T> + One {
// some stupid generic constants, because Rust doesnt have polymorphic literals… // some stupid generic constants, because Rust doesnt have polymorphic literals…
let two_t = T::one() + T::one(); // lolololol let one_t = T::one();
let three_t = two_t + T::one(); // megalol let two_t = one_t + one_t; // lolololol
let three_t = two_t + one_t; // megalol
// sampler stuff // sampler stuff
let t2 = t * t; let t2 = t * t;
@ -37,10 +173,10 @@ where V: Float + Mul<T, Output = V> + Div<T, Output = V>,
let three_t2 = t2 * three_t; let three_t2 = t2 * three_t;
// tangents // tangents
let m0 = (b.0 - x.0) / (b.1 - x.1); let m0 = (b.0 - x.0).outer_div(b.1 - x.1);
let m1 = (y.0 - a.0) / (y.1 - a.1); let m1 = (y.0 - a.0).outer_div(y.1 - a.1);
a.0 * (two_t3 - three_t2 + T::one()) + m0 * (t3 - t2 * two_t + t) + b.0 * (three_t2 - two_t3) + m1 * (t3 - t2) a.0.outer_mul(two_t3 - three_t2 + one_t) + m0.outer_mul(t3 - t2 * two_t + t) + b.0.outer_mul(three_t2 - two_t3) + m1.outer_mul(t3 - t2)
} }
macro_rules! impl_interpolate_simple { macro_rules! impl_interpolate_simple {
@ -76,4 +212,3 @@ macro_rules! impl_interpolate_via {
impl_interpolate_via!(f32, f64); impl_interpolate_via!(f32, f64);
impl_interpolate_via!(f64, f32); impl_interpolate_via!(f64, f32);

View File

@ -97,6 +97,8 @@
#![cfg_attr(not(feature = "std"), feature(alloc))] #![cfg_attr(not(feature = "std"), feature(alloc))]
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))] #![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
#[cfg(not(feature = "std"))] extern crate alloc;
#[cfg(feature = "impl-cgmath")] mod cgmath; #[cfg(feature = "impl-cgmath")] mod cgmath;
pub mod interpolate; pub mod interpolate;
pub mod interpolation; pub mod interpolation;

View File

@ -1,36 +1,63 @@
use crate::Interpolate; use alga::general::{ClosedAdd, ClosedDiv, ClosedMul, ClosedSub};
use nalgebra::{DefaultAllocator, DimName, Point, Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vector6};
use nalgebra::allocator::Allocator;
use num_traits as nt;
use std::ops::Mul;
use nalgebra as na; use crate::interpolate::{Interpolate, Linear, Additive, One, cubic_hermite_def};
use num_traits::Float; macro_rules! impl_interpolate_vector {
macro_rules! impl_interpolate_na_vector {
($($t:tt)*) => { ($($t:tt)*) => {
impl<T, V> Interpolate<T> for $($t)*<V> where T: Float, V: na::Scalar + Interpolate<T> { // implement Linear
impl<T> Linear<T> for $($t)*<T> where T: Scalar + ClosedMul + ClosedDiv {
fn outer_mul(self, t: T) -> Self {
self * t
}
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> {
fn lerp(a: Self, b: Self, t: T) -> Self { fn lerp(a: Self, b: Self, t: T) -> Self {
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t)) Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, 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)
} }
} }
} }
} }
impl_interpolate_na_vector!(na::Vector1); impl_interpolate_vector!(Vector1);
impl_interpolate_na_vector!(na::Vector2); impl_interpolate_vector!(Vector2);
impl_interpolate_na_vector!(na::Vector3); impl_interpolate_vector!(Vector3);
impl_interpolate_na_vector!(na::Vector4); impl_interpolate_vector!(Vector4);
impl_interpolate_na_vector!(na::Vector5); impl_interpolate_vector!(Vector5);
impl_interpolate_na_vector!(na::Vector6); impl_interpolate_vector!(Vector6);
impl<T, N, D> Interpolate<T> for na::Point<N, D> impl<T, D> Linear<T> for Point<T, D>
where D: na::DimName, where D: DimName,
na::DefaultAllocator: na::allocator::Allocator<N, D>, DefaultAllocator: Allocator<T, D>,
<na::DefaultAllocator as na::allocator::Allocator<N, D>>::Buffer: Copy, <DefaultAllocator as Allocator<T, D>>::Buffer: Copy,
N: na::Scalar + Interpolate<T>, T: Scalar + ClosedDiv + ClosedMul {
T: Float { fn outer_mul(self, t: T) -> Self {
fn lerp(a: Self, b: Self, t: T) -> Self { self * t
// The 'coords' of a point is just a vector, so we can interpolate component-wise }
// over these vectors.
let coords = na::Vector::zip_map(&a.coords, &b.coords, |c1, c2| Interpolate::lerp(c1, c2, t)); fn outer_div(self, t: T) -> Self {
na::Point::from(coords) self / t
} }
} }

View File

@ -1,14 +1,14 @@
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize}; #[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
#[cfg(feature = "std")] use std::cmp::Ordering;
#[cfg(not(feature = "std"))] use core::cmp::Ordering;
#[cfg(not(feature = "std"))] use alloc::vec::Vec; #[cfg(not(feature = "std"))] use alloc::vec::Vec;
#[cfg(feature = "std")] use std::cmp::Ordering;
#[cfg(feature = "std")] use std::ops::{Div, Mul};
#[cfg(not(feature = "std"))] use core::ops::{Div, Mul};
#[cfg(not(feature = "std"))] use core::cmp::Ordering;
use crate::interpolate::Interpolate; use crate::interpolate::{Interpolate, Additive, One, Trigo};
use crate::interpolation::Interpolation; use crate::interpolation::Interpolation;
use crate::key::Key; use crate::key::Key;
use num_traits::{Float, FloatConst};
/// Spline curve used to provide interpolation between control points (keys). /// Spline curve used to provide interpolation between control points (keys).
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
@ -53,7 +53,7 @@ impl<T, V> Spline<T, V> {
/// sampling impossible. For instance, `Interpolate::CatmullRom` requires *four* keys. If youre /// sampling impossible. For instance, `Interpolate::CatmullRom` requires *four* keys. If youre
/// 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: Float + FloatConst, V: Interpolate<T> { pub fn sample(&self, t: T) -> Option<V> where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T>, 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];
@ -76,7 +76,7 @@ impl<T, V> Spline<T, V> {
let two_t = T::one() + T::one(); let two_t = T::one() + T::one();
let cp1 = &keys[i+1]; let cp1 = &keys[i+1];
let nt = normalize_time(t, cp0, cp1); let nt = normalize_time(t, cp0, cp1);
let cos_nt = (T::one() - (nt * T::PI()).cos()) / two_t; let cos_nt = (T::one() - (nt * T::pi()).cos()) / two_t;
Some(Interpolate::lerp(cp0.value, cp1.value, cos_nt)) Some(Interpolate::lerp(cp0.value, cp1.value, cos_nt))
} }
@ -108,7 +108,7 @@ 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: Float + FloatConst, 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>, V: Interpolate<T> {
if self.0.is_empty() { if self.0.is_empty() {
return None; return None;
} }
@ -136,7 +136,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: Float { ) -> T where T: Additive + Div<T, Output = T> {
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)
} }