This commit is contained in:
Dimitri Sabadie 2019-09-24 10:42:03 +02:00
parent 5d0ebc0777
commit 6f65be125b
No known key found for this signature in database
GPG Key ID: 784B10173D70584F
10 changed files with 15 additions and 102 deletions

View File

@ -1,10 +1,15 @@
# 1.1.1 # 2.0.0
> Mon Sep 22rd 2019 > Mon Sep 24th 2019
## Major changes
- Add support for [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
- Because of Bézier curves, the `Interpolation` type now has one more type variable to know how we
should interpolate with Bézier.
## Minor changes
- 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`. - 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.1.1" version = "2.0.0"
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"
@ -21,7 +21,6 @@ maintenance = { status = "actively-developed" }
[features] [features]
default = ["std"] default = ["std"]
bezier = []
impl-cgmath = ["cgmath"] impl-cgmath = ["cgmath"]
impl-nalgebra = ["alga", "nalgebra", "num-traits"] impl-nalgebra = ["alga", "nalgebra", "num-traits"]
serialization = ["serde", "serde_derive"] serialization = ["serde", "serde_derive"]

View File

@ -98,12 +98,6 @@ So heres a list of currently supported features and how to enable them:
- Compiling with the standard library is enabled by default. - Compiling with the standard library is enabled by default.
- Use `default-features = []` in your `Cargo.toml` to disable. - Use `default-features = []` in your `Cargo.toml` to disable.
- Enable explicitly with the `"std"` feature. - Enable explicitly with the `"std"` feature.
- **Extra interpolation modes.**
- In order not to introduce breaking changes, some feature-gates are added to augment the
[`Interpolation`] enum.
- Those feature-gates will disappear on the next major release of the crate.
- The following lists all currently available:
- `"bezier"`: [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
[`Interpolation`]: crate::interpolation::Interpolation [`Interpolation`]: crate::interpolation::Interpolation

View File

@ -32,13 +32,11 @@ macro_rules! impl_interpolate_vec {
cubic_hermite_def(x, a, b, y, t) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
#[inline(always)] #[inline(always)]
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")]
#[inline(always)] #[inline(always)]
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)
@ -76,13 +74,11 @@ where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
cubic_hermite_def(x, a, b, y, t) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
#[inline(always)] #[inline(always)]
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")]
#[inline(always)] #[inline(always)]
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)

View File

@ -59,11 +59,9 @@ 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;
} }
@ -223,7 +221,6 @@ 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 {
@ -235,7 +232,6 @@ 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 {
@ -258,12 +254,10 @@ 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)
} }
@ -285,12 +279,10 @@ macro_rules! impl_interpolate_via {
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)
} }
#[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 as $v) quadratic_bezier_def(a, u, b, t as $v)
} }
#[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 as $v) cubic_bezier_def(a, u, v, b, t as $v)
} }

View File

@ -5,7 +5,6 @@
/// 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"))]
@ -40,48 +39,14 @@ pub enum Interpolation<T, V> {
/// tangent used for the next control point is defined as the segment connecting that control /// tangent used for the next control point is defined as the segment connecting that control
/// point and the current control points associated point. This is called _quadratic Bézer /// point and the current control points associated point. This is called _quadratic Bézer
/// 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")]
Bezier(V), Bezier(V),
#[doc(hidden)]
__NonExhaustive
} }
/// 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 {
Interpolation::Linear Interpolation::Linear
} }
} }
#[cfg(not(feature = "bezier"))]
impl<T> Default for Interpolation<T> {
/// [`Interpolation::Linear`] is the default.
fn default() -> Self {
Interpolation::Linear
}
}

View File

@ -17,7 +17,6 @@ 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"))]
@ -30,36 +29,9 @@ 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

@ -99,12 +99,6 @@
//! - Compiling with the standard library is enabled by default. //! - Compiling with the standard library is enabled by default.
//! - Use `default-features = []` in your `Cargo.toml` to disable. //! - Use `default-features = []` in your `Cargo.toml` to disable.
//! - Enable explicitly with the `"std"` feature. //! - Enable explicitly with the `"std"` feature.
//! - **Extra interpolation modes.**
//! - In order not to introduce breaking changes, some feature-gates are added to augment the
//! [`Interpolation`] enum.
//! - Those feature-gates will disappear on the next major release of the crate.
//! - The following lists all currently available:
//! - `"bezier"`: [Bézier curves](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
//! //!
//! [`Interpolation`]: crate::interpolation::Interpolation //! [`Interpolation`]: crate::interpolation::Interpolation

View File

@ -43,13 +43,11 @@ macro_rules! impl_interpolate_vector {
cubic_hermite_def(x, a, b, y, t) cubic_hermite_def(x, a, b, y, t)
} }
#[cfg(feature = "bezier")]
#[inline(always)] #[inline(always)]
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")]
#[inline(always)] #[inline(always)]
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)

View File

@ -129,7 +129,6 @@ impl<T, V> Spline<T, V> {
} }
} }
#[cfg(feature = "bezier")]
Interpolation::Bezier(u) => { Interpolation::Bezier(u) => {
// We need to check the next control point to see whether we want quadratic or cubic Bezier. // We need to check the next control point to see whether we want quadratic or cubic Bezier.
let cp1 = &keys[i + 1]; let cp1 = &keys[i + 1];
@ -146,6 +145,8 @@ 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))
} }
} }
Interpolation::__NonExhaustive => unreachable!(),
} }
} }
@ -243,10 +244,7 @@ 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.