1use std::ops::{Add, Mul};
6
7use crate::{
8 core::Duration,
9 vlm::{Quat, Vec3},
10};
11
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17pub enum AnimInterpolation {
18 Step,
20 #[default]
22 Linear,
23 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
34pub trait AnimCurveValue:
40 Copy + Default + Add<Output = Self> + Mul<f32, Output = Self> + sealed::Sealed
41{
42 fn curve_zero() -> Self;
44
45 fn curve_is_finite(self) -> bool;
47
48 fn curve_is_valid(self) -> bool;
50
51 fn curve_normalize(self) -> Self;
53
54 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 self.try_slerp(other, weight).unwrap_or(self)
123 }
124}
125
126#[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}