1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use cubic_spline::spline;
use get_input_index;
use primitive::InterpolationPrimitive;

/// Catmull-Rom spline interpolation
///
/// `f(t) = (2d^3 + 3d^2 + 1)p0 + (d^3 - 2d^2 + d)m0 + (-2d^3 + 3d^2)p1 + (d^3 - d^2)m1`
/// `d = (t - t0) / (t1 - t0)`
/// `p0 = position at left keyframe`
/// `p1 = position at right keyframe`
/// `k = left keyframe index`
/// `k+1 = right keyframe index`
/// `m0 = (p_k+1 - p_k-1) / (t_k+1 - t_k-1)`
/// `m1 = (p_k+2 - p_k) / (t_k+2 - t_k)`
/// `t0 = input at left keyframe`
/// `t1 = input at right keyframe`
///
/// ## Parameters:
///
/// - `input`: the input value to the function
/// - `inputs`: list of discrete input values for each keyframe
/// - `outputs`: list of output values to interpolate between, for catmull rom spline interpolation
///             this should be the size of `inputs` + 2
///             `[ in_tangent_0, position_0, position_1, .., position_n, out_tangent_n ]`
/// - `normalize`: if true, normalize the interpolated value before returning it
pub fn catmull_rom_spline_interpolate<T>(
    input: f32,
    inputs: &[f32],
    outputs: &[T],
    normalize: bool,
) -> T
where
    T: InterpolationPrimitive + Copy,
{
    let input_index = match get_input_index(input, inputs) {
        Some(index) => index,
        None => return outputs[1],
    };
    if input_index >= (inputs.len() - 1) {
        outputs[outputs.len() - 2]
    } else {
        let t_diff = inputs[input_index + 1] - inputs[input_index];
        let v = spline(
            input,
            inputs[input_index],
            t_diff,
            &outputs[input_index + 1],
            &outputs[input_index + 2],
            &catmull_tangent(input_index, inputs, outputs),
            &catmull_tangent(input_index + 1, inputs, outputs),
        );
        if normalize {
            v.normalize()
        } else {
            v
        }
    }
}

fn catmull_tangent<D>(index: usize, inputs: &[f32], outputs: &[D]) -> D
where
    D: InterpolationPrimitive + Copy,
{
    let output_index = index + 1;
    if index == 0 {
        outputs[0]
    } else if index == inputs.len() - 1 {
        outputs[outputs.len() - 1]
    } else {
        outputs[output_index + 1]
            .sub(&outputs[output_index - 1])
            .mul(1. / (inputs[index + 1] - inputs[index - 1]))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mint::{Quaternion, Vector3};

    #[test]
    fn test_catmull_arr3() {
        let input = vec![0., 1., 2., 3., 4.];
        let output = vec![
            [1., 0., 0.],
            [0., 0., 0.],
            [1., 0., 0.],
            [0., 0., 0.],
            [-1., 0., 0.],
            [0., 0., 0.],
            [-1., 0., 0.],
        ];
        assert_eq!(
            [0.625, 0., 0.],
            catmull_rom_spline_interpolate(0.5, &input, &output, false)
        );
    }

    #[test]
    fn test_catmull_arr4() {
        let input = vec![0., 1., 2., 3., 4.];
        let output = vec![
            [1., 0., 0., 0.],
            [0., 0., 0., 0.],
            [1., 0., 0., 0.],
            [0., 0., 0., 0.],
            [-1., 0., 0., 0.],
            [0., 0., 0., 0.],
            [-1., 0., 0., 0.],
        ];
        assert_eq!(
            [1., 0., 0., 0.],
            catmull_rom_spline_interpolate(0.5, &input, &output, true)
        );
    }

    #[test]
    fn test_catmull_vec3() {
        let input = vec![0., 1., 2., 3., 4.];
        let output = vec![
            Vector3::from([1., 0., 0.]),
            Vector3::from([0., 0., 0.]),
            Vector3::from([1., 0., 0.]),
            Vector3::from([0., 0., 0.]),
            Vector3::from([-1., 0., 0.]),
            Vector3::from([0., 0., 0.]),
            Vector3::from([-1., 0., 0.]),
        ];
        assert_eq!(
            Vector3::from([0.625, 0., 0.]),
            catmull_rom_spline_interpolate(0.5, &input, &output, false)
        );
    }

    #[test]
    fn test_catmull_quat() {
        let input = vec![0., 1., 2., 3., 4.];
        let output = vec![
            Quaternion::from([1., 0., 0., 0.]),
            Quaternion::from([0., 0., 0., 0.]),
            Quaternion::from([1., 0., 0., 0.]),
            Quaternion::from([0., 0., 0., 0.]),
            Quaternion::from([-1., 0., 0., 0.]),
            Quaternion::from([0., 0., 0., 0.]),
            Quaternion::from([-1., 0., 0., 0.]),
        ];
        assert_eq!(
            Quaternion::from([1., 0., 0., 0.]),
            catmull_rom_spline_interpolate(0.5, &input, &output, true)
        );
    }
}