oa/anim/curve.rs
1//! Immutable sparse animation curve.
2//!
3//! Donor: `oa/animation/animCurve.h`.
4
5use crate::{Result, core::Duration};
6
7use super::{AnimInterpolation, AnimWrapMode, Keyframe, keyframe::AnimCurveValue};
8
9/// Immutable sparse animation curve over keyframe times.
10///
11/// Construction is validated through [`super::create_curve`]; keyframe times
12/// are strictly increasing and every value and tangent is finite.
13///
14/// Donor: `oa::AnimCurve<T>`.
15#[derive(Clone, Debug, PartialEq)]
16pub struct AnimCurve<T: AnimCurveValue> {
17 pub(crate) interpolation: AnimInterpolation,
18 pub(crate) keyframes: Vec<Keyframe<T>>,
19}
20
21impl<T: AnimCurveValue> AnimCurve<T> {
22 /// Interpolation mode between adjacent keys.
23 pub fn interpolation(&self) -> AnimInterpolation {
24 self.interpolation
25 }
26
27 /// Number of stored keyframes.
28 pub fn keyframe_count(&self) -> u32 {
29 self.keyframes.len() as u32
30 }
31
32 /// Borrowed keyframes in strictly increasing time order.
33 pub fn keyframes(&self) -> &[Keyframe<T>] {
34 &self.keyframes
35 }
36
37 /// Time of the first keyframe.
38 ///
39 /// # Panics
40 ///
41 /// Validated curves always hold at least one keyframe.
42 pub fn start_time(&self) -> Duration {
43 self.keyframes[0].time
44 }
45
46 /// Time of the last keyframe.
47 ///
48 /// # Panics
49 ///
50 /// Validated curves always hold at least one keyframe.
51 pub fn end_time(&self) -> Duration {
52 self.keyframes[self.keyframes.len() - 1].time
53 }
54
55 /// Span between the last and first keyframe times.
56 ///
57 /// The span is guaranteed representable by the construction-time
58 /// validation of [`super::create_curve`].
59 pub fn duration(&self) -> Duration {
60 let first = self.start_time().nanoseconds();
61 let last = self.end_time().nanoseconds();
62 Duration::from_nanoseconds(last - first)
63 }
64
65 /// Sample the curve at `time` under `wrap_mode`.
66 ///
67 /// Convenience delegate of [`super::sample_curve`].
68 ///
69 /// # Errors
70 ///
71 /// Returns an error when interpolation produces a non-finite or
72 /// non-normalizable value.
73 pub fn sample(&self, time: Duration, wrap_mode: AnimWrapMode) -> Result<T> {
74 super::sample_curve(self, time, wrap_mode)
75 }
76}