-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Improve cubic segment bezier functionality #17645
base: main
Are you sure you want to change the base?
Improve cubic segment bezier functionality #17645
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
#[inline] | ||
pub fn iter_samples<'a, 'b: 'a>( | ||
&'b self, | ||
subdivisions: usize, | ||
mut sample_function: impl FnMut(&Self, f32) -> P + 'a, | ||
) -> impl Iterator<Item = P> + 'a { | ||
self.iter_uniformly(subdivisions) | ||
.map(move |t| sample_function(self, t)) | ||
} | ||
|
||
/// An iterator that returns values of `t` uniformly spaced over `0..=subdivisions`. | ||
#[inline] | ||
fn iter_uniformly(&self, subdivisions: usize) -> impl Iterator<Item = f32> { | ||
let step = 1.0 / subdivisions as f32; | ||
(0..=subdivisions).map(move |i| i as f32 * step) | ||
} | ||
|
||
/// Iterate over the curve split into `subdivisions`, sampling the position at each step. | ||
pub fn iter_positions(&self, subdivisions: usize) -> impl Iterator<Item = P> + '_ { | ||
self.iter_samples(subdivisions, Self::position) | ||
} | ||
|
||
/// Iterate over the curve split into `subdivisions`, sampling the velocity at each step. | ||
pub fn iter_velocities(&self, subdivisions: usize) -> impl Iterator<Item = P> + '_ { | ||
self.iter_samples(subdivisions, Self::velocity) | ||
} | ||
|
||
/// Iterate over the curve split into `subdivisions`, sampling the acceleration at each step. | ||
pub fn iter_accelerations(&self, subdivisions: usize) -> impl Iterator<Item = P> + '_ { | ||
self.iter_samples(subdivisions, Self::acceleration) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of the code duplication, but I guess that's kind of unavoidable 🤔
I think there's a reasonable shot that all of these will be outmoded by the Curve
API implementations in the future, but for now it's more ergonomic, I guess.
(E.g. one right now can write something like
cubic_segment.with_two_derivatives().map(|j| j.second_derivative).samples(n)
to similar effect, and the intention is for this to become more ergonomic in the near future.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to move these functions to a separate trait, and implement it on both CubicCurve
and CubicSegment
. This can help avoid some duplication. What do you think?
You can delete the entire |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the code and doc comments look good. You might want to add some labels to your PR though if you can. Sometimes people skip over PR's if they don't have labels.
Objective
CubicSegment
lacks functionality ofCubicCurve
#17642Solution
new_bezier(points: [P; 4]) -> Self
forCubicSegment<P>
new_bezier
is nownew_bezier_easing(p1: impl Into<Vec2>, p2: impl Into<Vec2>) -> Self
(breaking change)added methodnew_bezier_with_anchor
, which can make a bezier curve between two points with one control anchoriter_positions
,iter_velocities
,iter_accelerations
, the same as inCubicCurve
(copied code, potentially can be reduced)CubicCurve
toCubicSegment
, removing the unneeded allocationTesting
crates/bevy_math/
cargo test
on the whole bevy directory because of OOMCubicCurve
withnew_bezier
andnew_bezier_easing
, but not testedCubicCurve::new_bezier
Showcase
Migration Guide
CubicCurve::new_bezier
withCubicCurve::new_bezier_easing