splines/interpolation.rs
1//! Available interpolation modes.
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// Available kind of interpolations.
7///
8/// Feel free to visit each variant for more documentation.
9#[non_exhaustive]
10#[derive(Copy, Clone, Debug, Eq, PartialEq)]
11#[cfg_attr(
12 feature = "serde",
13 derive(Deserialize, Serialize),
14 serde(rename_all = "snake_case")
15)]
16pub enum Interpolation<T, V> {
17 /// Hold a [`Key`] until the sampling value passes the normalized step threshold, in which
18 /// case the next key is used.
19 ///
20 /// > Note: if you set the threshold to `0.5`, the first key will be used until half the time
21 /// > between the two keys; the second key will be in used afterwards. If you set it to `1.0`, the
22 /// > first key will be kept until the next key. Set it to `0.` and the first key will never be
23 /// > used.
24 ///
25 /// [`Key`]: crate::key::Key
26 Step(T),
27
28 /// Linear interpolation between a key and the next one.
29 Linear,
30
31 /// Cosine interpolation between a key and the next one.
32 Cosine,
33
34 /// Catmull-Rom interpolation, performing a cubic Hermite interpolation using four keys.
35 ///
36 /// # Notes
37 ///
38 /// Given four keys `P0`, `P1`, `P2` and `P3`, interpolated values are only possible between
39 /// `P1` and `P2`. Trying to sample before `P1` and after `P2` will result with no values, as
40 /// four keys (one before the beginning key, the beginning key, the end key and one after the
41 /// end key) are required to perform interpolation.
42 ///
43 /// This requires uniform spacing.
44 CatmullRom,
45
46 /// Bézier interpolation.
47 ///
48 /// A control point that uses such an interpolation is associated with an extra point. The segment
49 /// connecting both is called the _tangent_ at this point. The part of the spline defined between
50 /// this control point and the next one will be interpolated across with Bézier interpolation. Two
51 /// cases are possible:
52 ///
53 /// - The next control point also has a Bézier interpolation mode. In this case, its tangent is
54 /// used for the interpolation process. This is called _cubic Bézier interpolation_ and it
55 /// kicks ass.
56 /// - The next control point doesn’t have a Bézier interpolation mode set. In this case, the
57 /// tangent used for the next control point is defined as the segment connecting that control
58 /// point and the current control point’s associated point. This is called _quadratic Bézer
59 /// interpolation_ and it kicks ass too, but a bit less than cubic.
60 Bezier(V),
61
62 /// A special Bézier interpolation using an _input tangent_ and an _output tangent_.
63 ///
64 /// With this kind of interpolation, a control point has an input tangent, which has the same role
65 /// as the one defined by [`Interpolation::Bezier`], and an output tangent, which has the same
66 /// role defined by the next key’s [`Interpolation::Bezier`] if present, normally.
67 ///
68 /// What it means is that instead of setting the output tangent as the next key’s Bézier tangent,
69 /// this interpolation mode allows you to manually set the output tangent. That will yield more
70 /// control on the tangents but might generate discontinuities. Use with care.
71 ///
72 /// Stroke Bézier interpolation is always a cubic Bézier interpolation by default.
73 StrokeBezier(V, V),
74}
75
76impl<T, V> Default for Interpolation<T, V> {
77 /// [`Interpolation::Linear`] is the default.
78 fn default() -> Self {
79 Interpolation::Linear
80 }
81}