From 677017e3381df6de0f7fe4c236d7185a500b033d Mon Sep 17 00:00:00 2001 From: memdmp Date: Thu, 16 Jan 2025 11:04:38 +0100 Subject: feat: oh god the horrors --- src/interpolation.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/interpolation.rs (limited to 'src/interpolation.rs') diff --git a/src/interpolation.rs b/src/interpolation.rs new file mode 100644 index 0000000..de6b6df --- /dev/null +++ b/src/interpolation.rs @@ -0,0 +1,50 @@ +use std::ops::{Add, Mul, Sub}; + +pub fn raw_lerp(a: T, b: T, t: f64) -> T +where + T: Copy + Add + Sub + Mul, +{ + a + (b - a) * t +} + +pub enum TimingFunction { + Lerp, +} +pub struct KeyFrame { + // We could make a TimeType generic (I initially did), however it's safe to assume we use a 64-bit float for this + pub time: f64, + pub val: ValueType, +} +impl KeyFrame { + pub fn new(time: f64, val: ValueType) -> KeyFrame { + KeyFrame { time, val } + } +} +impl< + ValueType: PartialOrd + + Copy + + Add + + Sub + + Mul, + > KeyFrame +{ + /** Simply passes the data to the timing function involved. Does not do bounding. */ + pub fn raw_value_at( + from: &KeyFrame, + to: &KeyFrame, + time: f64, + timing_function: TimingFunction, + ) -> ValueType { + // Order them so `from` is always the lower bound + let (from, to) = if from.time < to.time { + (from, to) + } else { + (to, from) + }; + let length = to.time - from.time; + let position = (time - from.time) / length; + match timing_function { + TimingFunction::Lerp => raw_lerp(from.val, to.val, position), + } + } +} -- cgit v1.2.3 From d8fe5abc51c708367bca57493d1fd38142581ef9 Mon Sep 17 00:00:00 2001 From: memdmp Date: Thu, 16 Jan 2025 12:21:04 +0100 Subject: feat: in theory this "works" --- src/interpolation.rs | 115 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 3 deletions(-) (limited to 'src/interpolation.rs') diff --git a/src/interpolation.rs b/src/interpolation.rs index de6b6df..d088bf8 100644 --- a/src/interpolation.rs +++ b/src/interpolation.rs @@ -1,4 +1,17 @@ -use std::ops::{Add, Mul, Sub}; +// NOTE: This entire file would need to be reworked to handle functions like bezier curves/any other fn involving multiple points. +// Someone should do this at some point. +// +// This likely would only be slightly breaking; ie the most commonly used portions of AnimationTrack would remain in-tact +// however, the internals of a KeyFrame would change immensely and be (more or less) impossible to keep + +use std::ops::{Add, Index, Mul, Sub}; + +// For when RFC1733 finally passes +// trait AcceptableValueType = PartialOrd +// + Copys +// + Add +// + Sub +// + Mul; pub fn raw_lerp(a: T, b: T, t: f64) -> T where @@ -7,11 +20,13 @@ where a + (b - a) * t } +#[derive(Clone, Copy)] pub enum TimingFunction { Lerp, } +#[derive(Clone, Copy, PartialEq, PartialOrd)] +// We could make a TimeType generic (I initially did), however it's safe to assume we use a 64-bit float for this pub struct KeyFrame { - // We could make a TimeType generic (I initially did), however it's safe to assume we use a 64-bit float for this pub time: f64, pub val: ValueType, } @@ -28,7 +43,9 @@ impl< + Mul, > KeyFrame { - /** Simply passes the data to the timing function involved. Does not do bounding. */ + /** + Simply passes the data to the timing function involved. Does not do bounding. + */ pub fn raw_value_at( from: &KeyFrame, to: &KeyFrame, @@ -48,3 +65,95 @@ impl< } } } + +#[derive(Clone)] +pub struct AnimationTrack { + pub keyframes: Vec>, + pub loop_count: u32, +} +impl AnimationTrack { + fn get_sorted_keyframes(&self) -> Vec> { + let mut kf = self.keyframes.clone(); + kf.sort_by(|a, b| a.time.total_cmp(&b.time)); + kf + } + fn get_first_last(&self) -> Option<(KeyFrame, KeyFrame)> { + let okf = self.get_sorted_keyframes(); + if okf.len() == 0 { + None + } else { + Some((*okf.first().unwrap(), *okf.last().unwrap())) + } + } + fn length(fl: (KeyFrame, KeyFrame)) -> f64 { + fl.1.time - fl.0.time + } +} +impl< + ValueType: PartialOrd + + Copy + + Add + + Sub + + Mul, + > AnimationTrack +{ + /** Get the 2 keyframes surrounding the current position in time. */ + fn get_current_keyframes( + &self, + // We have the user pass this, as to prevent needing to constantly re-calculate it. + sorted_keyframes: Vec>, + time: f64, + ) -> Option<(KeyFrame, KeyFrame)> { + // TODO: maybe refactor the internals of this to be faster + let idx = { + let mut idx = 0; + let mut found = false; + for f in &sorted_keyframes { + if f.time > time { + found = true; + break; + } else { + idx += 1; + } + } + if found { + Some(idx) + } else { + None + } + }; + match idx { + None => None, + Some(idx) => { + // If it's the last item, we don't have a 2nd + if idx + 1 == sorted_keyframes.len() { + None + } else { + Some(( + *sorted_keyframes.index(idx), + *sorted_keyframes.index(idx + 1), + )) + } + } + } + } + /** Gets the current value */ + pub fn get_current_value( + &self, + // We have the user pass this, as to prevent needing to constantly re-calculate it. + sorted_keyframes: Vec>, + time: f64, + timing_function: TimingFunction, + ) -> Option { + let frames = self.get_current_keyframes(sorted_keyframes, time); + match frames { + Some(frames) => Some(KeyFrame::raw_value_at( + &frames.0, + &frames.1, + time, + timing_function, + )), + None => None, + } + } +} -- cgit v1.2.3 From 53cf1e9fb257e3fa351277c98b70e5244718d0db Mon Sep 17 00:00:00 2001 From: memdmp Date: Thu, 16 Jan 2025 13:07:05 +0100 Subject: feat: what the fuck am i doing here --- src/interpolation.rs | 70 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 26 deletions(-) (limited to 'src/interpolation.rs') diff --git a/src/interpolation.rs b/src/interpolation.rs index d088bf8..17e4a48 100644 --- a/src/interpolation.rs +++ b/src/interpolation.rs @@ -104,35 +104,53 @@ impl< sorted_keyframes: Vec>, time: f64, ) -> Option<(KeyFrame, KeyFrame)> { - // TODO: maybe refactor the internals of this to be faster - let idx = { - let mut idx = 0; - let mut found = false; - for f in &sorted_keyframes { - if f.time > time { - found = true; - break; - } else { - idx += 1; - } - } - if found { - Some(idx) - } else { + let length = AnimationTrack::length(( + *sorted_keyframes.first().unwrap(), + *sorted_keyframes.last().unwrap(), + )); + // This can be removed if size restrictions call for it + match if time > length { + if time > length * f64::from(self.loop_count) { None + } else { + Some(time % length) } - }; - match idx { + } else { + Some(time) + } { None => None, - Some(idx) => { - // If it's the last item, we don't have a 2nd - if idx + 1 == sorted_keyframes.len() { - None - } else { - Some(( - *sorted_keyframes.index(idx), - *sorted_keyframes.index(idx + 1), - )) + Some(time) => { + // TODO: maybe refactor the internals of this to be faster + let idx = { + let mut idx = 0; + let mut found = false; + for f in &sorted_keyframes { + if f.time > time { + found = true; + break; + } else { + idx += 1; + } + } + if found { + Some(idx) + } else { + None + } + }; + match idx { + None => None, + Some(idx) => { + // If it's the last item, we don't have a 2nd + if idx + 1 == sorted_keyframes.len() { + None + } else { + Some(( + *sorted_keyframes.index(idx), + *sorted_keyframes.index(idx + 1), + )) + } + } } } } -- cgit v1.2.3 From dde982726272341a760cea068199b96c0e660f66 Mon Sep 17 00:00:00 2001 From: memdmp Date: Thu, 16 Jan 2025 13:38:48 +0100 Subject: fix: i now hate life --- src/interpolation.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/interpolation.rs') diff --git a/src/interpolation.rs b/src/interpolation.rs index 17e4a48..4543ffb 100644 --- a/src/interpolation.rs +++ b/src/interpolation.rs @@ -21,9 +21,14 @@ where } #[derive(Clone, Copy)] -pub enum TimingFunction { +pub enum TwoValueTimingFunction { Lerp, } +#[derive(Clone, Copy)] +pub enum ManyValueTimingFunction { + Bezier, +} + #[derive(Clone, Copy, PartialEq, PartialOrd)] // We could make a TimeType generic (I initially did), however it's safe to assume we use a 64-bit float for this pub struct KeyFrame { @@ -50,7 +55,7 @@ impl< from: &KeyFrame, to: &KeyFrame, time: f64, - timing_function: TimingFunction, + timing_function: TwoValueTimingFunction, ) -> ValueType { // Order them so `from` is always the lower bound let (from, to) = if from.time < to.time { @@ -61,7 +66,7 @@ impl< let length = to.time - from.time; let position = (time - from.time) / length; match timing_function { - TimingFunction::Lerp => raw_lerp(from.val, to.val, position), + TwoValueTimingFunction::Lerp => raw_lerp(from.val, to.val, position), } } } @@ -161,7 +166,7 @@ impl< // We have the user pass this, as to prevent needing to constantly re-calculate it. sorted_keyframes: Vec>, time: f64, - timing_function: TimingFunction, + timing_function: TwoValueTimingFunction, ) -> Option { let frames = self.get_current_keyframes(sorted_keyframes, time); match frames { -- cgit v1.2.3 From 7fd1c555d1008a3d131d4acd5e3f0f7ca5b0e38c Mon Sep 17 00:00:00 2001 From: memdmp Date: Thu, 16 Jan 2025 17:29:36 +0100 Subject: feat: temp --- src/interpolation.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/interpolation.rs') diff --git a/src/interpolation.rs b/src/interpolation.rs index 4543ffb..ecb2c85 100644 --- a/src/interpolation.rs +++ b/src/interpolation.rs @@ -19,6 +19,12 @@ where { a + (b - a) * t } +pub fn raw_bezier(v: Vec, t: f64) -> T +where + T: Copy + Add + Sub + Mul, +{ + todo!() +} #[derive(Clone, Copy)] pub enum TwoValueTimingFunction { -- cgit v1.2.3