Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
a3a2919eb4 | |||
37cf89b566 | |||
77ccf0a47b | |||
766066d9ed | |||
882b9e7b34 | |||
0dcfe48415 | |||
24cd0d7fca | |||
3cd65dce54 | |||
11791bed70 | |||
820839abc0 | |||
865c855ac5 |
30
CHANGELOG.md
30
CHANGELOG.md
@ -1,3 +1,33 @@
|
||||
## 0.2.3
|
||||
|
||||
> Sat 13th October 2018
|
||||
|
||||
- Add the `"impl-nalgebra"` feature gate. It gives access to some implementors for the `nalgebra`
|
||||
crate.
|
||||
- Enhance the documentation.
|
||||
|
||||
## 0.2.2
|
||||
|
||||
> Sun 30th September 2018
|
||||
|
||||
- Bump version numbers (`splines-0.2`) in examples.
|
||||
- Fix several typos in the documentation.
|
||||
|
||||
## 0.2.1
|
||||
|
||||
> Thu 20th September 2018
|
||||
|
||||
- Enhance the features documentation.
|
||||
|
||||
# 0.2
|
||||
|
||||
> Thu 6th September 2018
|
||||
|
||||
- Add the `"std"` feature gate, that can be used to compile with the standard library.
|
||||
- Add the `"impl-cgmath"` feature gate in order to make optional, if wanted, the `cgmath`
|
||||
dependency.
|
||||
- Enhance the documentation.
|
||||
|
||||
## 0.1.1
|
||||
|
||||
> Wed 8th August 2018
|
||||
|
15
Cargo.toml
15
Cargo.toml
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "splines"
|
||||
version = "0.1.1"
|
||||
version = "0.2.3"
|
||||
license = "BSD-3-Clause"
|
||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
||||
description = "Spline interpolation made easy"
|
||||
@ -18,12 +18,19 @@ is-it-maintained-open-issues = { repository = "phaazon/splines" }
|
||||
maintenance = { status = "actively-developed" }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
default = ["std", "impl-cgmath"]
|
||||
serialization = ["serde", "serde_derive"]
|
||||
std = []
|
||||
impl-cgmath = ["cgmath"]
|
||||
impl-nalgebra = ["nalgebra"]
|
||||
|
||||
[dependencies]
|
||||
cgmath = "0.16"
|
||||
[dependencies.nalgebra]
|
||||
version = ">=0.14, <0.17"
|
||||
optional = true
|
||||
|
||||
[dependencies.cgmath]
|
||||
version = "0.16"
|
||||
optional = true
|
||||
|
||||
[dependencies.serde]
|
||||
version = "1"
|
||||
|
15
README.md
15
README.md
@ -10,10 +10,17 @@ Feel free to dig in the [online documentation](https://docs.rs/splines) for furt
|
||||
This crate has features! Here’s a comprehensive list of what you can enable:
|
||||
|
||||
- **Serialization / deserialization.**
|
||||
+ This feature implements both the `Serialize` and `Deserialize` traits from `serde`.
|
||||
+ This feature implements both the `Serialize` and `Deserialize` traits from `serde` for all
|
||||
types exported by this crate.
|
||||
+ Enable with the `"serialization"` feature.
|
||||
- **Standard library / no stdandard library**
|
||||
- **[cgmath](https://crates.io/crates/cgmath) implementors.**
|
||||
+ Adds some useful implementations of `Interpolate` for some cgmath types.
|
||||
+ Enable with the `"impl-cgmath"` feature.
|
||||
- **[nalgebra](https://crates.io/crates/nalgebra) implementors.**
|
||||
+ Adds some useful 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.
|
||||
+ Use `defaut-features = []` in your `Cargo.toml` to disable.
|
||||
+ Enable explicitly with the `"std"` feataure.
|
||||
+ Use `default-features = []` in your `Cargo.toml` to disable.
|
||||
+ Enable explicitly with the `"std"` feature.
|
||||
|
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "hello-world"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
splines = "0.1"
|
||||
splines = "0.2"
|
||||
|
@ -1,11 +1,11 @@
|
||||
[package]
|
||||
name = "serialization"
|
||||
version = "0.1.0"
|
||||
version = "0.2.0"
|
||||
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1"
|
||||
|
||||
[dependencies.splines]
|
||||
version = "0.1"
|
||||
version = "0.2"
|
||||
features = ["serialization"]
|
||||
|
99
src/lib.rs
99
src/lib.rs
@ -77,26 +77,40 @@
|
||||
//! So here’s a list of currently supported features and how to enable them:
|
||||
//!
|
||||
//! - **Serialization / deserialization.**
|
||||
//! + This feature implements both the `Serialize` and `Deserialize` traits from `serde`.
|
||||
//! + This feature implements both the `Serialize` and `Deserialize` traits from `serde` for all
|
||||
//! types exported by this crate.
|
||||
//! + Enable with the `"serialization"` feature.
|
||||
//! - **Standard library / no stdandard library**
|
||||
//! - **[cgmath](https://crates.io/crates/cgmath) implementors.**
|
||||
//! + Adds some useful implementations of `Interpolate` for some cgmath types.
|
||||
//! + Enable with the `"impl-cgmath"` feature.
|
||||
//! - **[nalgebra](https://crates.io/crates/nalgebra) implementors.**
|
||||
//! + Adds some useful 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.
|
||||
//! + Use `defaut-features = []` in your `Cargo.toml` to disable.
|
||||
//! + Enable explicitly with the `"std"` feataure.
|
||||
//! + Use `default-features = []` in your `Cargo.toml` to disable.
|
||||
//! + Enable explicitly with the `"std"` feature.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![cfg_attr(not(feature = "std"), feature(alloc))]
|
||||
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
|
||||
|
||||
// on no_std, we also need the alloc crate for Vec
|
||||
#[cfg(not(feature = "std"))] extern crate alloc;
|
||||
|
||||
extern crate cgmath;
|
||||
#[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;
|
||||
|
||||
use cgmath::{InnerSpace, Quaternion, Vector2, Vector3, Vector4};
|
||||
#[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::{DimName, DefaultAllocator, Scalar};
|
||||
#[cfg(feature = "impl-nalgebra")] use nalgebra::core::allocator::Allocator;
|
||||
|
||||
#[cfg(feature = "std")] use std::cmp::Ordering;
|
||||
#[cfg(feature = "std")] use std::f32::consts;
|
||||
@ -227,7 +241,18 @@ impl<T> Spline<T> {
|
||||
Interpolation::Cosine => {
|
||||
let cp1 = &keys[i+1];
|
||||
let nt = normalize_time(t, cp0, cp1);
|
||||
let cos_nt = (1. - f32::cos(nt * consts::PI)) * 0.5;
|
||||
let cos_nt = {
|
||||
#[cfg(feature = "std")]
|
||||
{
|
||||
(1. - f32::cos(nt * consts::PI)) * 0.5
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
{
|
||||
use core::intrinsics::cosf32;
|
||||
unsafe { (1. - cosf32(nt * consts::PI)) * 0.5 }
|
||||
}
|
||||
};
|
||||
|
||||
Some(Interpolate::lerp(cp0.value, cp1.value, cos_nt))
|
||||
},
|
||||
@ -329,6 +354,7 @@ impl Interpolate for f32 {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-cgmath")]
|
||||
impl Interpolate for Vector2<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
a.lerp(b, t)
|
||||
@ -339,6 +365,7 @@ impl Interpolate for Vector2<f32> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-cgmath")]
|
||||
impl Interpolate for Vector3<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
a.lerp(b, t)
|
||||
@ -349,6 +376,7 @@ impl Interpolate for Vector3<f32> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-cgmath")]
|
||||
impl Interpolate for Vector4<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
a.lerp(b, t)
|
||||
@ -359,12 +387,69 @@ impl Interpolate for Vector4<f32> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-cgmath")]
|
||||
impl Interpolate for Quaternion<f32> {
|
||||
fn lerp(a: Self, b: Self, t: f32) -> Self {
|
||||
a.nlerp(b, t)
|
||||
}
|
||||
}
|
||||
|
||||
#[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> {
|
||||
|
29
tests/mod.rs
29
tests/mod.rs
@ -1,4 +1,8 @@
|
||||
extern crate splines;
|
||||
#[cfg(feature = "impl-nalgebra")] extern crate nalgebra;
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")] use nalgebra as na;
|
||||
#[cfg(feature = "impl-nalgebra")] use splines::Interpolate;
|
||||
|
||||
use splines::{Interpolation, Key, Spline};
|
||||
|
||||
@ -128,3 +132,28 @@ fn several_interpolations_several_keys() {
|
||||
assert_eq!(spline.sample(10.), Some(2.));
|
||||
assert_eq!(spline.clamped_sample(11.), 4.);
|
||||
}
|
||||
|
||||
#[cfg(feature = "impl-nalgebra")]
|
||||
#[test]
|
||||
fn nalgebra_point_interpolation() {
|
||||
let start = na::Point2::new(0.0, 0.0);
|
||||
let mid = na::Point2::new(0.5, 0.5);
|
||||
let end = na::Point2::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")]
|
||||
#[test]
|
||||
fn nalgebra_vector_interpolation() {
|
||||
let start = na::Vector2::new(0.0, 0.0);
|
||||
let mid = na::Vector2::new(0.5, 0.5);
|
||||
let end = na::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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user