splines/lib.rs
1//! # Spline interpolation made easy.
2//!
3//! This crate exposes splines for which each sections can be interpolated independently of each
4//! other – i.e. it’s possible to interpolate with a linear interpolator on one section and then
5//! switch to a cubic Hermite interpolator for the next section.
6//!
7//! Most of the crate consists of three types:
8//!
9//! - [`Key`], which represents the control points by which the spline must pass.
10//! - [`Interpolation`], the type of possible interpolation for each segment.
11//! - [`Spline`], a spline from which you can *sample* points by interpolation.
12//!
13//! When adding control points, you add new sections. Two control points define a section – i.e.
14//! it’s not possible to define a spline without at least two control points. Every time you add a
15//! new control point, a new section is created. Each section is assigned an interpolation mode that
16//! is picked from its lower control point.
17//!
18//! # Quickly create splines
19//!
20//! ```
21//! use splines::{Interpolation, Key, Spline};
22//!
23//! let start = Key::new(0., 0., Interpolation::Linear);
24//! let end = Key::new(1., 10., Interpolation::default());
25//! let spline = Spline::from_vec(vec![start, end]);
26//! ```
27//!
28//! You will notice that we used `Interpolation::Linear` for the first key. The first key `start`’s
29//! interpolation will be used for the whole segment defined by those two keys. The `end`’s
30//! interpolation won’t be used. You can in theory use any [`Interpolation`] you want for the last
31//! key. We use the default one because we don’t care.
32//!
33//! # Interpolate values
34//!
35//! The whole purpose of splines is to interpolate discrete values to yield continuous ones. This is
36//! usually done with the [`Spline::sample`] method. This method expects the sampling parameter
37//! (often, this will be the time of your simulation) as argument and will yield an interpolated
38//! value.
39//!
40//! If you try to sample in out-of-bounds sampling parameter, you’ll get no value.
41//!
42//! ```
43//! # use splines::{Interpolation, Key, Spline};
44//! # let start = Key::new(0., 0., Interpolation::Linear);
45//! # let end = Key::new(1., 10., Interpolation::Linear);
46//! # let spline = Spline::from_vec(vec![start, end]);
47//! assert_eq!(spline.sample(0.), Some(0.));
48//! assert_eq!(spline.clamped_sample(1.), Some(10.));
49//! assert_eq!(spline.sample(1.1), None);
50//! ```
51//!
52//! It’s possible that you want to get a value even if you’re out-of-bounds. This is especially
53//! important for simulations / animations. Feel free to use the `Spline::clamped_interpolation` for
54//! that purpose.
55//!
56//! ```
57//! # use splines::{Interpolation, Key, Spline};
58//! # let start = Key::new(0., 0., Interpolation::Linear);
59//! # let end = Key::new(1., 10., Interpolation::Linear);
60//! # let spline = Spline::from_vec(vec![start, end]);
61//! assert_eq!(spline.clamped_sample(-0.9), Some(0.)); // clamped to the first key
62//! assert_eq!(spline.clamped_sample(1.1), Some(10.)); // clamped to the last key
63//! ```
64//!
65//! # Polymorphic sampling types
66//!
67//! [`Spline`] curves are parametered both by the carried value (being interpolated) but also the
68//! sampling type. It’s very typical to use `f32` or `f64` but really, you can in theory use any
69//! kind of type; that type must, however, implement a contract defined by a set of traits to
70//! implement. See [the documentation of this module](crate::interpolate) for further details.
71//!
72//! # Features and customization
73//!
74//! This crate was written with features baked in and hidden behind feature-gates. The idea is that
75//! the default configuration (i.e. you just add `"splines = …"` to your `Cargo.toml`) will always
76//! give you the minimal, core and raw concepts of what splines, keys / knots and interpolation
77//! modes are. However, you might want more. Instead of letting other people do the extra work to
78//! add implementations for very famous and useful traits – and do it in less efficient way, because
79//! they wouldn’t have access to the internals of this crate, it’s possible to enable features in an
80//! ad hoc way.
81//!
82//! This mechanism is not final and this is currently an experiment to see how people like it or
83//! not. It’s especially important to see how it copes with the documentation.
84//!
85//! So here’s a list of currently supported features and how to enable them:
86//!
87//! - **Serialization / deserialization.**
88//! - This feature implements both the `Serialize` and `Deserialize` traits from `serde` for all
89//! types exported by this crate.
90//! - Enable with the `"serialization"` feature.
91//! - **[cgmath](https://crates.io/crates/cgmath) implementors.**
92//! - Adds some useful implementations of `Interpolate` for some cgmath types.
93//! - Enable with the `"impl-cgmath"` feature.
94//! - **[nalgebra](https://crates.io/crates/nalgebra) implementors.**
95//! - Adds some useful implementations of `Interpolate` for some nalgebra types.
96//! - Enable with the `"impl-nalgebra"` feature.
97//! - **Standard library / no standard library.**
98//! - It’s possible to compile against the standard library or go on your own without it.
99//! - Compiling with the standard library is enabled by default.
100//! - Use `default-features = []` in your `Cargo.toml` to disable.
101//! - Enable explicitly with the `"std"` feature.
102//!
103//! [`Interpolation`]: crate::interpolation::Interpolation
104
105#![cfg_attr(not(feature = "std"), no_std)]
106#![cfg_attr(not(feature = "std"), feature(alloc))]
107#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
108
109#[cfg(not(feature = "std"))] extern crate alloc;
110
111#[cfg(feature = "impl-cgmath")] mod cgmath;
112pub mod interpolate;
113pub mod interpolation;
114pub mod iter;
115pub mod key;
116#[cfg(feature = "impl-nalgebra")] mod nalgebra;
117pub mod spline;
118
119pub use crate::interpolate::Interpolate;
120pub use crate::interpolation::Interpolation;
121pub use crate::key::Key;
122pub use crate::spline::Spline;