Crate splines[−][src]
Spline interpolation made easy.
This crate exposes splines for which each sections can be interpolated independently of each other – i.e. it’s possible to interpolate with a linear interpolator on one section and then switch to a cubic Hermite interpolator for the next section.
Most of the crate consists of three types:
Key
, which represents the control points by which the spline must pass.Interpolation
, the type of possible interpolation for each segment.Spline
, a spline from which you can sample points by interpolation.
When adding control points, you add new sections. Two control points define a section – i.e. it’s not possible to define a spline without at least two control points. Every time you add a new control point, a new section is created. Each section is assigned an interpolation mode that is picked from its lower control point.
Quickly create splines
use splines::{Interpolation, Key, Spline}; let start = Key::new(0., 0., Interpolation::Linear); let end = Key::new(1., 10., Interpolation::default()); let spline = Spline::from_vec(vec![start, end]);
You will notice that we used Interpolation::Linear
for the first key. The first key start
’s
interpolation will be used for the whole segment defined by those two keys. The end
’s
interpolation won’t be used. You can in theory use any Interpolation
you want for the last
key. We use the default one because we don’t care.
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
(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, you’ll get no value.
assert_eq!(spline.sample(0.), Some(0.)); assert_eq!(spline.clamped_sample(1.), 10.); assert_eq!(spline.sample(1.1), None);
It’s possible that you want to get a value even if you’re out-of-bounds. This is especially
important for simulations / animations. Feel free to use the Spline::clamped_interpolation
for
that purpose.
assert_eq!(spline.clamped_sample(-0.9), 0.); // clamped to the first key assert_eq!(spline.clamped_sample(1.1), 10.); // clamped to the last key
Feel free to have a look at the rest of the documentation for advanced usage.
Structs
Iter |
Iterator over spline keys. |
Key |
A spline control point. |
Spline |
Spline curve used to provide interpolation between control points (keys). |
Enums
Interpolation |
Interpolation mode. |
Traits
Interpolate |
Keys that can be interpolated in between. Implementing this trait is required to perform sampling on splines. |