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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * librePvZ-animation: animation playing for librePvZ.
 * Copyright (c) 2022  Ruifeng Xie
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Concrete curves.

use std::borrow::Borrow;
use std::fmt::Debug;
use std::marker::PhantomData;
use bitvec::prelude::*;
use derivative::Derivative;
use optics::traits::{AffineFoldRef, AffineFoldMut};
use crate::curve::AnyComponent;
use crate::curve::blend::BlendMethod;
use super::{Curve, TypedCurve, Segment};
use super::animatable::Animatable;

/// The lifetime-irrelevant part of the [`CurveContent`] interface.
pub trait CurveContentStatic: Send + Sync + 'static {
    /// Keyframe content type.
    type Keyframe: 'static;
    /// Track length.
    fn curve_content_len(&self) -> usize;
}

/// Provided borrowed access to the keyframe contents.
pub trait CurveContentBorrow<'a>: CurveContentStatic {
    /// Borrow of keyframe contents.
    type KeyframeRef: Borrow<Self::Keyframe>;
    /// Get keyframe at specific index.
    fn curve_content_get(&'a self, k: usize) -> Self::KeyframeRef;
}

/// Provides linear random access to keyframe contents in a [`Curve`].
/// See also [`CurveContentStatic`] and [`CurveContentBorrow`].
pub trait CurveContent: for<'a> CurveContentBorrow<'a> {}

impl<T: for<'a> CurveContentBorrow<'a>> CurveContent for T {}

impl<T: Send + Sync + 'static> CurveContentStatic for Box<[T]> {
    type Keyframe = T;
    fn curve_content_len(&self) -> usize { self.as_ref().len() }
}

impl<'a, T: Send + Sync + 'static> CurveContentBorrow<'a> for Box<[T]> {
    type KeyframeRef = &'a T;
    fn curve_content_get(&'a self, k: usize) -> &'a T { &self[k] }
}

impl<T, O> CurveContentStatic for BitBox<T, O>
    where T: BitStore + Send + Sync + 'static,
          O: BitOrder + Send + Sync + 'static {
    type Keyframe = bool;
    fn curve_content_len(&self) -> usize { self.len() }
}

impl<'a, T, O> CurveContentBorrow<'a> for BitBox<T, O>
    where T: BitStore + Send + Sync + 'static,
          O: BitOrder + Send + Sync + 'static {
    type KeyframeRef = bool;
    fn curve_content_get(&'a self, k: usize) -> bool { self[k] }
}

/// Keyframe animation curve.
#[derive(Derivative)]
#[derivative(Debug(bound = "F: Debug"))]
pub struct KeyframeCurve<S, F, C> {
    /// Target component type.
    #[derivative(Debug = "ignore")]
    _component_type: PhantomData<fn() -> S>,
    /// Field accessor from `S`.
    field_accessor: F,
    /// Keyframe indices: the actual frame indices, including non-keyframes.
    keyframe_indices: Box<[u16]>,
    /// Keyframe contents.
    #[derivative(Debug = "ignore")]
    keyframes: C,
}

impl<S, F, C: CurveContent> KeyframeCurve<S, F, C> {
    /// Create a keyframe curve.
    pub fn new(field: F, indices: Box<[u16]>, keyframes: C) -> Self {
        assert!(!indices.is_empty(), "never create empty curves");
        assert_eq!(keyframes.curve_content_len(), indices.len(), "unaligned curve");
        KeyframeCurve {
            _component_type: PhantomData,
            field_accessor: field,
            keyframe_indices: indices,
            keyframes,
        }
    }

    fn next_keyframe(&self, frame: u16) -> usize {
        self.keyframe_indices.partition_point(|&ix| ix <= frame)
    }

    fn last_keyframe(&self, frame: u16) -> Option<usize> {
        let next = self.next_keyframe(frame);
        next.checked_sub(1)
    }
}

impl<S, F, C> Curve for KeyframeCurve<S, F, C>
    where S: 'static, C: CurveContent<Keyframe = F::View>,
          F::View: PartialEq + Animatable + Sized + Send + Sync + 'static,
          F: Send + Sync + 'static
          + for<'a> AffineFoldRef<'a, S, Error = String>
          + for<'a> AffineFoldMut<'a, S, Error = String> {
    type Component = S;
    fn frame_count(&self) -> usize { *self.keyframe_indices.last().unwrap() as usize }
    fn apply_sampled(
        &self, segment: Segment, frame: f32,
        blending: Option<(BlendMethod, f32)>,
        output: impl AnyComponent<S>,
    ) -> Result<(), String> {
        if let Some(val) = self.sample(segment, frame) {
            if let Some((blending, progress)) = blending {
                let old = self.read_field(output.component())?;
                let val = F::View::interpolate(old, &val, blending.factor(progress));
                self.update_field(output, val)?;
            } else {
                self.update_field(output, val)?;
            }
        }
        Ok(())
    }
}

impl<S, F, C> TypedCurve for KeyframeCurve<S, F, C>
    where S: 'static, C: CurveContent<Keyframe = F::View>,
          F::View: PartialEq + Animatable + Sized + Send + Sync + 'static,
          F: Send + Sync + 'static
          + for<'a> AffineFoldRef<'a, S, Error = String>
          + for<'a> AffineFoldMut<'a, S, Error = String> {
    type Value = F::View;
    type FieldAccessor = F;
    fn sample(&self, segment: Segment, frame: f32) -> Option<F::View> {
        let frame = frame + segment.start as f32;
        let (this, next, ratio) = if frame >= segment.end as f32 { // wrap back (looping)
            // l = the first keyframe
            let l = self.last_keyframe(segment.start)?;
            // r = the last keyframe
            // now l..=r range contains all frames in the required segment
            let r = self.last_keyframe(segment.end)?;
            // absent keyframe means "the same as the last one"
            // so interpolation starts from segment.end
            // over the one-frame interval from segment.end to segment.start
            (r, l, frame - segment.end as f32)
        } else { // normal in-range interpolation
            let n = self.keyframe_indices.len();
            // k = last keyframe before 'frame'
            let k = self.last_keyframe(frame as u16)?;
            if k + 1 >= n {
                (k, k, 0.0)
            } else {
                let elapsed = frame - self.keyframe_indices[k] as f32;
                let delta = self.keyframe_indices[k + 1] - self.keyframe_indices[k];
                (k, k + 1, elapsed / delta as f32)
            }
        };

        Some(C::Keyframe::interpolate(
            self.keyframes.curve_content_get(this).borrow(),
            self.keyframes.curve_content_get(next).borrow(),
            ratio,
        ))
    }
    fn field_accessor(&self) -> &Self::FieldAccessor {
        &self.field_accessor
    }
}