Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace TwoIterators with Either in bevy_animation #16036

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_animation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ derive_more = { version = "1", default-features = false, features = [
"from",
"display",
] }
either = "1.13"
thread_local = "1"
uuid = { version = "1.7", features = ["v4"] }
smallvec = "1"
Expand Down
33 changes: 7 additions & 26 deletions crates/bevy_animation/src/gltf_curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy_math::{
};
use bevy_reflect::Reflect;
use derive_more::derive::{Display, Error, From};
use either::Either;

/// A keyframe-defined curve that "interpolates" by stepping at `t = 1.0` to the next keyframe.
#[derive(Debug, Clone, Reflect)]
Expand Down Expand Up @@ -189,11 +190,11 @@ where
match self.core.sample_interp(t) {
InterpolationDatum::Exact(v)
| InterpolationDatum::LeftTail(v)
| InterpolationDatum::RightTail(v) => TwoIterators::Left(v.iter().copied()),
| InterpolationDatum::RightTail(v) => Either::Left(v.iter().copied()),

InterpolationDatum::Between(u, v, s) => {
let interpolated = u.iter().zip(v.iter()).map(move |(x, y)| x.lerp(*y, s));
TwoIterators::Right(interpolated)
Either::Right(interpolated)
}
}
}
Expand Down Expand Up @@ -243,14 +244,14 @@ where
match self.core.sample_interp(t) {
InterpolationDatum::Exact(v)
| InterpolationDatum::LeftTail(v)
| InterpolationDatum::RightTail(v) => TwoIterators::Left(v.iter().cloned()),
| InterpolationDatum::RightTail(v) => Either::Left(v.iter().cloned()),

InterpolationDatum::Between(u, v, s) => {
let interpolated =
u.iter()
.zip(v.iter())
.map(move |(x, y)| if s >= 1.0 { y.clone() } else { x.clone() });
TwoIterators::Right(interpolated)
Either::Right(interpolated)
}
}
}
Expand Down Expand Up @@ -302,10 +303,10 @@ where
// Pick out the part of this that actually represents the position (instead of tangents),
// which is the middle third.
let width = self.core.width();
TwoIterators::Left(v[width..(width * 2)].iter().copied())
Either::Left(v[width..(width * 2)].iter().copied())
}

InterpolationDatum::Between((t0, u), (t1, v), s) => TwoIterators::Right(
InterpolationDatum::Between((t0, u), (t1, v), s) => Either::Right(
cubic_spline_interpolate_slices(self.core.width() / 3, u, v, s, t1 - t0),
),
}
Expand Down Expand Up @@ -392,26 +393,6 @@ pub enum WeightsCurve {
// HELPERS //
//---------//

enum TwoIterators<A, B> {
Left(A),
Right(B),
}

impl<A, B, T> Iterator for TwoIterators<A, B>
where
A: Iterator<Item = T>,
B: Iterator<Item = T>,
{
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
match self {
TwoIterators::Left(a) => a.next(),
TwoIterators::Right(b) => b.next(),
}
}
}

/// Helper function for cubic spline interpolation.
fn cubic_spline_interpolation<T>(
value_start: T,
Expand Down