Skip to main content

oa/anim/
keyframe.rs

1//! Sparse animation-curve sample values and interpolation modes.
2//!
3//! Donor: `oa/animation/keyframe.h`.
4
5use std::ops::{Add, Mul};
6
7use crate::{
8	core::Duration,
9	vlm::{Quat, Vec3},
10};
11
12/// Interpolation mode applied between two adjacent keyframes.
13///
14/// Donor: `oa::AnimInterpolation`. The Rust enum cannot hold the donor's
15/// castable invalid values, so no separate validity predicate exists.
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17pub enum AnimInterpolation {
18	/// Hold the left keyframe value until the next key.
19	Step,
20	/// Component-wise linear interpolation; quaternions use `slerp`.
21	#[default]
22	Linear,
23	/// Hermite spline using per-second in/out tangents.
24	CubicSpline,
25}
26
27mod sealed {
28	pub trait Sealed {}
29	impl Sealed for f32 {}
30	impl Sealed for crate::vlm::Vec3 {}
31	impl Sealed for crate::vlm::Quat {}
32}
33
34/// Value type admissible in [`Keyframe`] and [`AnimCurve`](super::AnimCurve).
35///
36/// Sealed: implemented by `f32`, [`Vec3`], and [`Quat`]. The trait spells the
37/// donor's `isAnimCurveValueV` bound plus the value behaviors curve sampling
38/// requires; it is not a public extension point.
39pub trait AnimCurveValue:
40	Copy + Default + Add<Output = Self> + Mul<f32, Output = Self> + sealed::Sealed
41{
42	/// Zero value used for absent tangents (`{0,0,0,0}` for quaternions).
43	fn curve_zero() -> Self;
44
45	/// All components finite.
46	fn curve_is_finite(self) -> bool;
47
48	/// Finite and, for quaternions, non-zero in at least one component.
49	fn curve_is_valid(self) -> bool;
50
51	/// Normalize where the type admits it (quaternions); otherwise identity.
52	fn curve_normalize(self) -> Self;
53
54	/// Component interpolation (`slerp` for quaternions).
55	fn curve_lerp(self, other: Self, weight: f32) -> Self;
56}
57
58impl AnimCurveValue for f32 {
59	fn curve_zero() -> Self {
60		0.0
61	}
62	fn curve_is_finite(self) -> bool {
63		self.is_finite()
64	}
65	fn curve_is_valid(self) -> bool {
66		self.is_finite()
67	}
68	fn curve_normalize(self) -> Self {
69		self
70	}
71	fn curve_lerp(self, other: Self, weight: f32) -> Self {
72		self * (1.0 - weight) + other * weight
73	}
74}
75
76impl AnimCurveValue for Vec3 {
77	fn curve_zero() -> Self {
78		Self::default()
79	}
80	fn curve_is_finite(self) -> bool {
81		self.is_finite()
82	}
83	fn curve_is_valid(self) -> bool {
84		self.is_finite()
85	}
86	fn curve_normalize(self) -> Self {
87		self
88	}
89	fn curve_lerp(self, other: Self, weight: f32) -> Self {
90		self.lerp(other, weight)
91	}
92}
93
94impl AnimCurveValue for Quat {
95	fn curve_zero() -> Self {
96		Self {
97			x: 0.0,
98			y: 0.0,
99			z: 0.0,
100			w: 0.0,
101		}
102	}
103	fn curve_is_finite(self) -> bool {
104		self.is_finite()
105	}
106	fn curve_is_valid(self) -> bool {
107		self.is_finite()
108			&& self
109				.x
110				.abs()
111				.max(self.y.abs())
112				.max(self.z.abs())
113				.max(self.w.abs())
114				> 0.0
115	}
116	fn curve_normalize(self) -> Self {
117		self.try_normalized().unwrap_or(self)
118	}
119	fn curve_lerp(self, other: Self, weight: f32) -> Self {
120		// Keyframe values are validated finite and non-zero before sampling,
121		// so the checked path always succeeds here.
122		self.try_slerp(other, weight).unwrap_or(self)
123	}
124}
125
126/// One sparse animation-curve sample.
127///
128/// Tangents are expressed per second and default to the value type's zero.
129/// For quaternion curves the zero tangent is the all-zero quaternion, while
130/// `value` defaults to identity.
131///
132/// Donor: `oa::Keyframe<T>`.
133#[derive(Clone, Copy, Debug, PartialEq)]
134pub struct Keyframe<T: AnimCurveValue> {
135	pub time: Duration,
136	pub value: T,
137	pub in_tangent: T,
138	pub out_tangent: T,
139}
140
141impl<T: AnimCurveValue> Default for Keyframe<T> {
142	fn default() -> Self {
143		Self {
144			time: Duration::ZERO,
145			value: T::default(),
146			in_tangent: T::curve_zero(),
147			out_tangent: T::curve_zero(),
148		}
149	}
150}