This commit is contained in:
Dimitri Sabadie 2019-09-23 20:56:56 +02:00
parent 7dbc85a312
commit 4fdbfa6189
No known key found for this signature in database
GPG Key ID: DE58C386A8DB2883
8 changed files with 148 additions and 37 deletions

View File

@ -1,8 +1,11 @@
# 1.1 # 1.1.1
> Mon Sep 22rd 2019 > Mon Sep 22rd 2019
- Yanked. - Add support for [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve). This is
normally a breaking change so its currently disabled by default and available via the
`"bezier"` feature-gate.
- Add `Spline::get`, `Spline::get_mut` and `Spline::replace`.
# 1.0 # 1.0

View File

@ -1,6 +1,6 @@
[package] [package]
name = "splines" name = "splines"
version = "1.2.0" version = "1.1.1"
license = "BSD-3-Clause" license = "BSD-3-Clause"
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"] authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
description = "Spline interpolation made easy" description = "Spline interpolation made easy"

View File

@ -2,7 +2,9 @@ use cgmath::{
BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace
}; };
use crate::interpolate::{Additive, Interpolate, Linear, One, cubic_hermite_def}; use crate::interpolate::{
Additive, Interpolate, Linear, One, cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def
};
macro_rules! impl_interpolate_vec { macro_rules! impl_interpolate_vec {
($($t:tt)*) => { ($($t:tt)*) => {
@ -29,6 +31,18 @@ macro_rules! impl_interpolate_vec {
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) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
#[inline(always)]
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
quadratic_bezier_def(a, u, b, t)
}
#[cfg(feature = "bezier")]
#[inline(always)]
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
cubic_bezier_def(a, u, v, b, t)
}
} }
} }
} }
@ -61,4 +75,16 @@ where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
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) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
#[inline(always)]
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
quadratic_bezier_def(a, u, b, t)
}
#[cfg(feature = "bezier")]
#[inline(always)]
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
cubic_bezier_def(a, u, v, b, t)
}
} }

View File

@ -59,9 +59,11 @@ pub trait Interpolate<T>: Sized + Copy {
} }
/// Quadratic Bézier interpolation. /// Quadratic Bézier interpolation.
#[cfg(feature = "bezier")]
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self; fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self;
/// Cubic Bézier interpolation. /// Cubic Bézier interpolation.
#[cfg(feature = "bezier")]
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self; fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self;
} }
@ -221,6 +223,7 @@ where V: Linear<T>,
/// Default implementation of [`Interpolate::quadratic_bezier`]. /// Default implementation of [`Interpolate::quadratic_bezier`].
/// ///
/// `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).
#[cfg(feature = "bezier")]
pub fn quadratic_bezier_def<V, T>(a: V, u: V, b: V, t: T) -> V pub fn quadratic_bezier_def<V, T>(a: V, u: V, b: V, t: T) -> V
where V: Linear<T>, where V: Linear<T>,
T: Additive + Mul<T, Output = T> + One { T: Additive + Mul<T, Output = T> + One {
@ -232,6 +235,7 @@ where V: Linear<T>,
/// Default implementation of [`Interpolate::cubic_bezier`]. /// Default implementation of [`Interpolate::cubic_bezier`].
/// ///
/// `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).
#[cfg(feature = "bezier")]
pub fn cubic_bezier_def<V, T>(a: V, u: V, v: V, b: V, t: T) -> V pub fn cubic_bezier_def<V, T>(a: V, u: V, v: V, b: V, t: T) -> V
where V: Linear<T>, where V: Linear<T>,
T: Additive + Mul<T, Output = T> + One { T: Additive + Mul<T, Output = T> + One {
@ -254,10 +258,12 @@ macro_rules! impl_interpolate_simple {
cubic_hermite_def(x, a, b, y, t) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
fn quadratic_bezier(a: Self, u: Self, b: Self, t: $t) -> Self { fn quadratic_bezier(a: Self, u: Self, b: Self, t: $t) -> Self {
quadratic_bezier_def(a, u, b, t) quadratic_bezier_def(a, u, b, t)
} }
#[cfg(feature = "bezier")]
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: $t) -> Self { fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: $t) -> Self {
cubic_bezier_def(a, u, v, b, t) cubic_bezier_def(a, u, v, b, t)
} }
@ -268,27 +274,29 @@ macro_rules! impl_interpolate_simple {
impl_interpolate_simple!(f32); impl_interpolate_simple!(f32);
impl_interpolate_simple!(f64); impl_interpolate_simple!(f64);
//macro_rules! impl_interpolate_via { macro_rules! impl_interpolate_via {
// ($t:ty, $v:ty) => { ($t:ty, $v:ty) => {
// impl Interpolate<$t> for $v { impl Interpolate<$t> for $v {
// fn lerp(a: Self, b: Self, t: $t) -> Self { fn lerp(a: Self, b: Self, t: $t) -> Self {
// a * (1. - t as $v) + b * t as $v a * (1. - t as $v) + b * t as $v
// } }
//
// fn cubic_hermite((x, xt): (Self, $t), (a, at): (Self, $t), (b, bt): (Self, $t), (y, yt): (Self, $t), t: $t) -> Self { fn cubic_hermite((x, xt): (Self, $t), (a, at): (Self, $t), (b, bt): (Self, $t), (y, yt): (Self, $t), t: $t) -> Self {
// cubic_hermite_def((x, xt as $v), (a, at as $v), (b, bt as $v), (y, yt as $v), t as $v) cubic_hermite_def((x, xt as $v), (a, at as $v), (b, bt as $v), (y, yt as $v), t as $v)
// } }
//
// fn quadratic_bezier(a: Self, u: Self, b: Self, t: $t) -> Self { #[cfg(feature = "bezier")]
// $t::quadratic_bezier(a as $t, u as $t, b as $t, t) fn quadratic_bezier(a: Self, u: Self, b: Self, t: $t) -> Self {
// } quadratic_bezier_def(a, u, b, t as $v)
// }
// fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: $t) -> Self {
// $t::cubic_bezier(a as $t, u as $t, v as $t, b as $t, t) #[cfg(feature = "bezier")]
// } fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: $t) -> Self {
// } cubic_bezier_def(a, u, v, b, t as $v)
// } }
//} }
// }
//impl_interpolate_via!(f32, f64); }
//impl_interpolate_via!(f64, f32);
impl_interpolate_via!(f32, f64);
impl_interpolate_via!(f64, f32);

View File

@ -5,11 +5,12 @@
/// Available kind of interpolations. /// Available kind of interpolations.
/// ///
/// Feel free to visit each variant for more documentation. /// Feel free to visit each variant for more documentation.
#[cfg(feature = "bezier")]
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))] #[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub enum Interpolation<T, V> { pub enum Interpolation<T, V> {
/// Hold a [`Key<T, _>`] until the sampling value passes the normalized step threshold, in which /// Hold a [`Key`] until the sampling value passes the normalized step threshold, in which
/// case the next key is used. /// case the next key is used.
/// ///
/// > Note: if you set the threshold to `0.5`, the first key will be used until half the time /// > Note: if you set the threshold to `0.5`, the first key will be used until half the time
@ -17,7 +18,7 @@ pub enum Interpolation<T, V> {
/// > first key will be kept until the next key. Set it to `0.` and the first key will never be /// > first key will be kept until the next key. Set it to `0.` and the first key will never be
/// > used. /// > used.
/// ///
/// [`Key<T, _>`]: crate::key::Key /// [`Key`]: crate::key::Key
Step(T), Step(T),
/// Linear interpolation between a key and the next one. /// Linear interpolation between a key and the next one.
Linear, Linear,
@ -41,11 +42,35 @@ pub enum Interpolation<T, V> {
/// interpolation_ and it kicks ass too, but a bit less than cubic. /// interpolation_ and it kicks ass too, but a bit less than cubic.
#[cfg(feature = "bezier")] #[cfg(feature = "bezier")]
Bezier(V), Bezier(V),
#[cfg(not(any(feature = "bezier")))]
#[doc(hidden)]
_V(std::marker::PhantomData<V>),
} }
/// Available kind of interpolations.
///
/// Feel free to visit each variant for more documentation.
#[cfg(not(feature = "bezier"))]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub enum Interpolation<T> {
/// Hold a [`Key`] until the sampling value passes the normalized step threshold, in which
/// case the next key is used.
///
/// > Note: if you set the threshold to `0.5`, the first key will be used until half the time
/// > between the two keys; the second key will be in used afterwards. If you set it to `1.0`, the
/// > first key will be kept until the next key. Set it to `0.` and the first key will never be
/// > used.
///
/// [`Key`]: crate::key::Key
Step(T),
/// Linear interpolation between a key and the next one.
Linear,
/// Cosine interpolation between a key and the next one.
Cosine,
/// Catmull-Rom interpolation, performing a cubic Hermite interpolation using four keys.
CatmullRom,
}
#[cfg(feature = "bezier")]
impl<T, V> Default for Interpolation<T, V> { impl<T, V> Default for Interpolation<T, V> {
/// [`Interpolation::Linear`] is the default. /// [`Interpolation::Linear`] is the default.
fn default() -> Self { fn default() -> Self {
@ -53,3 +78,10 @@ impl<T, V> Default for Interpolation<T, V> {
} }
} }
#[cfg(not(feature = "bezier"))]
impl<T> Default for Interpolation<T> {
/// [`Interpolation::Linear`] is the default.
fn default() -> Self {
Interpolation::Linear
}
}

View File

@ -17,6 +17,7 @@ use crate::interpolation::Interpolation;
/// key and the next one if existing. Have a look at [`Interpolation`] for further details. /// key and the next one if existing. Have a look at [`Interpolation`] for further details.
/// ///
/// [`Interpolation`]: crate::interpolation::Interpolation /// [`Interpolation`]: crate::interpolation::Interpolation
#[cfg(feature = "bezier")]
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))] #[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))] #[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
@ -29,9 +30,36 @@ pub struct Key<T, V> {
pub interpolation: Interpolation<T, V> pub interpolation: Interpolation<T, V>
} }
/// A spline control point.
///
/// This type associates a value at a given interpolation parameter value. It also contains an
/// interpolation mode used to determine how to interpolate values on the segment defined by this
/// key and the next one if existing. Have a look at [`Interpolation`] for further details.
///
/// [`Interpolation`]: crate::interpolation::Interpolation
#[cfg(not(feature = "bezier"))]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serialization", serde(rename_all = "snake_case"))]
pub struct Key<T, V> {
/// Interpolation parameter at which the [`Key`] should be reached.
pub t: T,
/// Carried value.
pub value: V,
/// Interpolation mode.
pub interpolation: Interpolation<T>
}
impl<T, V> Key<T, V> { impl<T, V> Key<T, V> {
/// Create a new key. /// Create a new key.
#[cfg(feature = "bezier")]
pub fn new(t: T, value: V, interpolation: Interpolation<T, V>) -> Self { pub fn new(t: T, value: V, interpolation: Interpolation<T, V>) -> Self {
Key { t, value, interpolation } Key { t, value, interpolation }
} }
/// Create a new key.
#[cfg(not(feature = "bezier"))]
pub fn new(t: T, value: V, interpolation: Interpolation<T>) -> Self {
Key { t, value, interpolation }
}
} }

View File

@ -3,7 +3,9 @@ use nalgebra::{Scalar, Vector, Vector1, Vector2, Vector3, Vector4, Vector5, Vect
use num_traits as nt; use num_traits as nt;
use std::ops::Mul; use std::ops::Mul;
use crate::interpolate::{Interpolate, Linear, Additive, One, cubic_hermite_def}; use crate::interpolate::{
Interpolate, Linear, Additive, One, cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def
};
macro_rules! impl_interpolate_vector { macro_rules! impl_interpolate_vector {
($($t:tt)*) => { ($($t:tt)*) => {
@ -40,6 +42,18 @@ macro_rules! impl_interpolate_vector {
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) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
#[inline(always)]
fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
quadratic_bezier_def(a, u, b, t)
}
#[cfg(feature = "bezier")]
#[inline(always)]
fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
cubic_bezier_def(a, u, v, b, t)
}
} }
} }
} }

View File

@ -146,9 +146,6 @@ impl<T, V> Spline<T, V> {
Some(Interpolate::quadratic_bezier(cp0.value, u, cp1.value, nt)) Some(Interpolate::quadratic_bezier(cp0.value, u, cp1.value, nt))
} }
} }
#[cfg(not(any(feature = "bezier")))]
Interpolation::_V(_) => unreachable!()
} }
} }
@ -246,7 +243,10 @@ pub struct KeyMut<'a, T, V> {
/// Carried value. /// Carried value.
pub value: &'a mut V, pub value: &'a mut V,
/// Interpolation mode to use for that key. /// Interpolation mode to use for that key.
#[cfg(feature = "bezier")]
pub interpolation: &'a mut Interpolation<T, V>, pub interpolation: &'a mut Interpolation<T, V>,
#[cfg(not(feature = "bezier"))]
pub interpolation: &'a mut Interpolation<T>,
} }
// Normalize a time ([0;1]) given two control points. // Normalize a time ([0;1]) given two control points.