5 Commits

Author SHA1 Message Date
bdb9a68c3b 1.0.0-rc.2. 2019-04-23 18:43:30 +02:00
e7ecc9819a Documentation, step 4. 2019-04-23 18:43:30 +02:00
e88da58a87 Step 3 of doc cleanup. 2019-04-23 18:43:30 +02:00
6ae3918eb1 Second pass of doc cleanup. 2019-04-23 18:43:30 +02:00
dcd82f7301 First doc cleanup. 2019-04-23 18:43:30 +02:00
9 changed files with 115 additions and 37 deletions

View File

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

View File

@ -4,4 +4,4 @@ version = "0.2.0"
authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
[dependencies]
splines = "0.2"
splines = "1.0.0-rc.2"

View File

@ -5,7 +5,4 @@ authors = ["Dimitri Sabadie <dimitri.sabadie@gmail.com>"]
[dependencies]
serde_json = "1"
[dependencies.splines]
version = "0.2"
features = ["serialization"]
splines = { version = "1.0.0-rc.2", features = ["serialization"] }

View File

@ -1,3 +1,33 @@
//! The [`Interpolate`] trait and associated symbols.
//!
//! The [`Interpolate`] trait is the central concept of the crate. It enables a spline to be
//! sampled at by interpolating in between control points.
//!
//! In order for a type to be used in [`Spline<K, V>`], some properties must be met about the `K`
//! type must implementing several traits:
//!
//! - [`One`], giving a neutral element for the multiplication monoid.
//! - [`Additive`], making the type additive (i.e. one can add or subtract with it).
//! - [`Linear`], unlocking linear combinations, required for interpolating.
//! - [`Trigo`], a trait giving *π* and *cosine*, required for e.g. cosine interpolation.
//!
//! Feel free to have a look at current implementors for further help.
//!
//! > *Why doesnt this crate use [num-traits] instead of
//! > defining its own traits?*
//!
//! The reason for this is quite simple: this crate provides a `no_std` support, which is not
//! currently available easily with [num-traits]. Also, if something changes in [num-traits] with
//! those traits, it would make this whole crate unstable.
//!
//! [`Interpolate`]: crate::interpolate::Interpolate
//! [`Spline<K, V>`]: crate::spline::Spline
//! [`One`]: crate::interpolate::One
//! [`Additive`]: crate::interpolate::Additive
//! [`Linear`]: crate::interpolate::Linear
//! [`Trigo`]: crate::interpolate::Trigo
//! [num-traits]: https://crates.io/crates/num-traits
#[cfg(feature = "std")] use std::f32;
#[cfg(not(feature = "std"))] use core::f32;
#[cfg(not(feature = "std"))] use core::intrinsics::cosf32;
@ -10,21 +40,28 @@
/// Keys that can be interpolated in between. Implementing this trait is required to perform
/// sampling on splines.
///
/// `T` is the variable used to sample with. Typical implementations use `f32` or `f64`, but youre
/// free to use the ones you like.
/// `T` is the variable used to sample with. Typical implementations use [`f32`] or [`f64`], but
/// youre free to use the ones you like. Feel free to have a look at [`Spline::sample`] for
/// instance to know which trait your type must implement to be usable.
///
/// [`Spline::sample`]: crate::spline::Spline::sample
pub trait Interpolate<T>: Sized + Copy {
/// Linear interpolation.
fn lerp(a: Self, b: Self, t: T) -> Self;
/// Cubic hermite interpolation.
///
/// Default to `Self::lerp`.
/// Default to [`lerp`].
///
/// [`lerp`]: Interpolate::lerp
fn cubic_hermite(_: (Self, T), a: (Self, T), b: (Self, T), _: (Self, T), t: T) -> Self {
Self::lerp(a.0, b.0, t)
}
}
/// A trait for anything that supports additions, subtraction, multiplication and division.
/// Set of types that support additions and subtraction.
///
/// The [`Copy`] trait is also a supertrait as its likely to be used everywhere.
pub trait Additive:
Copy +
Add<Self, Output = Self> +
@ -37,8 +74,8 @@ where T: Copy +
Sub<Self, Output = Self> {
}
/// Linear combination.
pub trait Linear<T> {
/// Set of additive types that support outer multiplication and division, making them linear.
pub trait Linear<T>: Additive {
/// Apply an outer multiplication law.
fn outer_mul(self, t: T) -> Self;
@ -84,7 +121,7 @@ impl_linear_cast!(f64, f32);
/// Types with a neutral element for multiplication.
pub trait One {
/// Return the neutral element for the multiplicative monoid.
/// The neutral element for the multiplicative monoid — typically called `1`.
fn one() -> Self;
}
@ -151,11 +188,11 @@ impl Trigo for f64 {
}
}
// Default implementation of Interpolate::cubic_hermite.
//
// `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
where V: Additive + Linear<T>,
/// Default implementation of [`Interpolate::cubic_hermite`].
///
/// `V` is the value being interpolated. `T` is the sampling value (also sometimes called time).
pub fn cubic_hermite_def<V, T>(x: (V, T), a: (V, T), b: (V, T), y: (V, T), t: T) -> V
where V: Linear<T>,
T: Additive + Mul<T, Output = T> + One {
// some stupid generic constants, because Rust doesnt have polymorphic literals…
let one_t = T::one();

View File

@ -1,17 +1,23 @@
//! Available interpolation modes.
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
/// Interpolation mode.
/// Available kind of interpolations.
///
/// Feel free to visit each variant for more documentation.
#[derive(Copy, Clone, Debug)]
#[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 interpolator value passes the normalized step threshold, in which
/// Hold a [`Key<T, _>`] 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<T, _>`]: crate::key::Key
Step(T),
/// Linear interpolation between a key and the next one.
Linear,
@ -22,7 +28,7 @@ pub enum Interpolation<T> {
}
impl<T> Default for Interpolation<T> {
/// `Interpolation::Linear` is the default.
/// [`Interpolation::Linear`] is the default.
fn default() -> Self {
Interpolation::Linear
}

View File

@ -1,10 +1,18 @@
//! Spline [`Iterator`], in a nutshell.
//!
//! You can iterate over a [`Spline<K, V>`]s keys with the [`IntoIterator`] trait on
//! `&Spline<K, V>`. This gives you iterated [`Key<K, V>`] keys.
//!
//! [`Spline<K, V>`]: crate::spline::Spline
//! [`Key<K, V>`]: crate::key::Key
use crate::{Key, Spline};
/// Iterator over spline keys.
///
/// This iterator type assures you to iterate over sorted keys.
/// This iterator type is guaranteed to iterate over sorted keys.
pub struct Iter<'a, T, V> where T: 'a, V: 'a {
anim_param: &'a Spline<T, V>,
spline: &'a Spline<T, V>,
i: usize
}
@ -12,7 +20,7 @@ impl<'a, T, V> Iterator for Iter<'a, T, V> {
type Item = &'a Key<T, V>;
fn next(&mut self) -> Option<Self::Item> {
let r = self.anim_param.0.get(self.i);
let r = self.spline.0.get(self.i);
if let Some(_) = r {
self.i += 1;
@ -28,7 +36,7 @@ impl<'a, T, V> IntoIterator for &'a Spline<T, V> {
fn into_iter(self) -> Self::IntoIter {
Iter {
anim_param: self,
spline: self,
i: 0
}
}

View File

@ -1,3 +1,11 @@
//! Spline control points.
//!
//! A control point associates to a “sampling value” (a.k.a. time) a carriede value that can be
//! interpolated along the curve made by the control points.
//!
//! Splines constructed with this crate have the property that its possible to change the
//! interpolation mode on a key-based way, allowing you to implement and encode complex curves.
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
use crate::interpolation::Interpolation;
@ -5,15 +13,17 @@ use crate::interpolation::Interpolation;
/// A spline control point.
///
/// This type associates a value at a given interpolation parameter value. It also contains an
/// interpolation hint used to determine how to interpolate values on the segment defined by this
/// key and the next one if existing.
/// 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
#[derive(Copy, Clone, Debug)]
#[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,
/// Held value.
/// Carried value.
pub value: V,
/// Interpolation mode.
pub interpolation: Interpolation<T>
@ -25,4 +35,3 @@ impl<T, V> Key<T, V> {
Key { t, value, interpolation }
}
}

View File

@ -33,11 +33,11 @@
//! # Interpolate values
//!
//! The whole purpose of splines is to interpolate discrete values to yield continuous ones. This is
//! usually done with the `Spline::sample` method. This method expects the interpolation parameter
//! usually done with the [`Spline::sample`] method. This method expects the sampling parameter
//! (often, this will be the time of your simulation) as argument and will yield an interpolated
//! value.
//!
//! If you try to sample in out-of-bounds interpolation parameter, youll get no value.
//! If you try to sample in out-of-bounds sampling parameter, youll get no value.
//!
//! ```
//! # use splines::{Interpolation, Key, Spline};
@ -62,6 +62,13 @@
//! assert_eq!(spline.clamped_sample(1.1), Some(10.)); // clamped to the last key
//! ```
//!
//! # Polymorphic sampling types
//!
//! [`Spline`] curves are parametered both by the carried value (being interpolated) but also the
//! sampling type. Its very typical to use `f32` or `f64` but really, you can in theory use any
//! kind of type; that type must, however, implement a contract defined by a set of traits to
//! implement. See [the documentation of this module](crate::interpolate) for further details.
//!
//! # Features and customization
//!
//! This crate was written with features baked in and hidden behind feature-gates. The idea is that

View File

@ -1,3 +1,5 @@
//! Spline curves and operations.
#[cfg(feature = "serialization")] use serde_derive::{Deserialize, Serialize};
#[cfg(not(feature = "std"))] use alloc::vec::Vec;
#[cfg(feature = "std")] use std::cmp::Ordering;
@ -10,6 +12,17 @@ use crate::interpolation::Interpolation;
use crate::key::Key;
/// Spline curve used to provide interpolation between control points (keys).
///
/// Splines are made out of control points ([`Key`]). When creating a [`Spline`] with
/// [`Spline::from_vec`] or [`Spline::from_iter`], the keys dont have to be sorted (they are sorted
/// automatically by the sampling value).
///
/// You can sample from a spline with several functions:
///
/// - [`Spline::sample`]: allows you to sample from a spline. If not enough keys are available
/// for the required interpolation mode, you get `None`.
/// - [`Spline::clamped_sample`]: behaves like [`Spline::sample`] but will return either the first
/// or last key if out of bound; it will return `None` if not enough key.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
pub struct Spline<T, V>(pub(crate) Vec<Key<T, V>>);
@ -29,7 +42,7 @@ impl<T, V> Spline<T, V> {
/// # Note on iterators
///
/// Its valid to use any iterator that implements `Iterator<Item = Key<T>>`. However, you should
/// use `Spline::from_vec` if you are passing a `Vec<_>`. This will remove dynamic allocations.
/// use [`Spline::from_vec`] if you are passing a [`Vec`]. This will remove dynamic allocations.
pub fn from_iter<I>(iter: I) -> Self where I: Iterator<Item = Key<T, V>>, T: PartialOrd {
Self::from_vec(iter.collect())
}
@ -50,9 +63,10 @@ impl<T, V> Spline<T, V> {
///
/// `None` if you try to sample a value at a time that has no key associated with. That can also
/// happen if you try to sample between two keys with a specific interpolation mode that makes the
/// 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
/// sampling.
/// sampling impossible. For instance, [`Interpolation::CatmullRom`] requires *four* keys. If
/// youre near the beginning of the spline or its end, ensure you have enough keys around to make
/// the sampling.
///
pub fn sample(&self, t: T) -> Option<V>
where T: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
V: Interpolate<T> {
@ -105,11 +119,11 @@ impl<T, V> Spline<T, V> {
/// # Return
///
/// If you sample before the first key or after the last one, return the first key or the last
/// one, respectively. Otherwise, behave the same way as `Spline::sample`.
/// one, respectively. Otherwise, behave the same way as [`Spline::sample`].
///
/// # 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: Additive + One + Trigo + Mul<T, Output = T> + Div<T, Output = T> + PartialOrd,
V: Interpolate<T> {