Add support for nalgebra along with some tests.
Feature-gated with impl-nalgebra.
This commit is contained in:
65
src/lib.rs
65
src/lib.rs
@ -83,6 +83,9 @@
|
||||
//! - **[cgmath](https://crates.io/crates/cgmath) implementors.**
|
||||
//! + Adds some usefull implementations of `Interpolate` for some cgmath types.
|
||||
//! + Enable with the `"impl-cgmath"` feature.
|
||||
//! - **[nalgebra](https://crates.io/crates/nalgebra) implementors.**
|
||||
//! + Adds some usefull implementations of `Interpolate` for some nalgebra types.
|
||||
//! + Enable with the `"impl-nalgebra"` feature.
|
||||
//! - **Standard library / no standard library.**
|
||||
//! + It’s possible to compile against the standard library or go on your own without it.
|
||||
//! + Compiling with the standard library is enabled by default.
|
||||
@ -98,11 +101,17 @@
|
||||
|
||||
#[cfg(feature = "impl-cgmath")] extern crate cgmath;
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")] extern crate nalgebra;
|
||||
|
||||
#[cfg(feature = "serialization")] extern crate serde;
|
||||
#[cfg(feature = "serialization")] #[macro_use] extern crate serde_derive;
|
||||
|
||||
#[cfg(feature = "impl-cgmath")] use cgmath::{InnerSpace, Quaternion, Vector2, Vector3, Vector4};
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")] use nalgebra as na;
|
||||
#[cfg(feature = "impl-nalgebra")] use nalgebra::core::allocator::Allocator;
|
||||
#[cfg(feature = "impl-nalgebra")] use nalgebra::core::{DimName, DefaultAllocator, Scalar};
|
||||
|
||||
#[cfg(feature = "std")] use std::cmp::Ordering;
|
||||
#[cfg(feature = "std")] use std::f32::consts;
|
||||
#[cfg(feature = "std")] use std::ops::{Add, Div, Mul, Sub};
|
||||
@ -385,6 +394,62 @@ impl Interpolate for Quaternion<f32> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl<N, D> Interpolate for na::Point<N, D>
|
||||
where D: DimName,
|
||||
DefaultAllocator: Allocator<N, D>,
|
||||
<DefaultAllocator as Allocator<N, D>>::Buffer: Copy,
|
||||
N: Scalar + Interpolate {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
// 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));
|
||||
na::Point::from_coordinates(coords)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl Interpolate for na::Vector1<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl Interpolate for na::Vector2<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl Interpolate for na::Vector3<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl Interpolate for na::Vector4<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl Interpolate for na::Vector5<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
impl Interpolate for na::Vector6<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
na::Vector::zip_map(&a, &b, |c1, c2| Interpolate::lerp(c1, c2, t))
|
||||
}
|
||||
}
|
||||
|
||||
// Default implementation of Interpolate::cubic_hermit.
|
||||
pub(crate) fn cubic_hermite<T>(x: (T, f32), a: (T, f32), b: (T, f32), y: (T, f32), t: f32) -> T
|
||||
where T: Copy + Add<Output = T> + Sub<Output = T> + Mul<f32, Output = T> + Div<f32, Output = T> {
|
||||
|
Reference in New Issue
Block a user