Skip to content

Commit

Permalink
Add generic cubic splines to bevy_math (#7683)
Browse files Browse the repository at this point in the history
# Objective

- Make cubic splines more flexible and more performant
- Remove the existing spline implementation that is generic over many degrees
  - This is a potential performance footgun and adds type complexity for negligible gain.
- Add implementations of:
  - Bezier splines
  - Cardinal splines (inc. Catmull-Rom)
  - B-Splines
  - Hermite splines

https://user-images.githubusercontent.com/2632925/221780519-495d1b20-ab46-45b4-92a3-32c46da66034.mp4


https://user-images.githubusercontent.com/2632925/221780524-2b154016-699f-404f-9c18-02092f589b04.mp4


https://user-images.githubusercontent.com/2632925/221780525-f934f99d-9ad4-4999-bae2-75d675f5644f.mp4


## Solution

- Implements the concept that splines are curve generators (e.g. https://youtu.be/jvPPXbo87ds?t=3488) via the `CubicGenerator` trait.
- Common splines are bespoke data types that implement this trait. This gives us flexibility to add custom spline-specific methods on these types, while ultimately all generating a `CubicCurve`.
- All splines generate `CubicCurve`s, which are a chain of precomputed polynomial coefficients. This means that all splines have the same evaluation cost, as the calculations for determining position, velocity, and acceleration are all identical. In addition, `CubicCurve`s are simply a list of `CubicSegment`s, which are evaluated from t=0 to t=1. This also means cubic splines of different type can be chained together, as ultimately they all are simply a collection of `CubicSegment`s.
- Because easing is an operation on a singe segment of a Bezier curve, we can simply implement easing on `Beziers` that use the `Vec2` type for points. Higher level crates such as `bevy_ui` can wrap this in a more ergonomic interface as needed.

### Performance
Measured on a desktop i5 8600K (6-year-old CPU):
- easing: 2.7x faster (19ns)
- cubic vec2 position sample: 1.5x faster (1.8ns)
- cubic vec3 position sample: 1.5x faster (2.6ns)
- cubic vec3a position sample: 1.9x faster (1.4ns)

On a laptop i7 11800H:
- easing: 16ns
- cubic vec2 position sample: 1.6ns
- cubic vec3 position sample: 2.3ns
- cubic vec3a position sample: 1.2ns

---

## Changelog

- Added a generic cubic curve trait, and implementation for Cardinal splines (including Catmull-Rom), B-Splines, Beziers, and Hermite Splines. 2D cubic curve segments also implement easing functionality for animation.
  • Loading branch information
aevyrie committed Mar 3, 2023
1 parent 47ddd4a commit 2ea0061
Show file tree
Hide file tree
Showing 4 changed files with 650 additions and 548 deletions.
127 changes: 44 additions & 83 deletions benches/benches/bevy_math/bezier.rs
Original file line number Diff line number Diff line change
@@ -1,128 +1,89 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use bevy_math::*;
use bevy_math::{prelude::*, *};

fn easing(c: &mut Criterion) {
let cubic_bezier = CubicBezierEasing::new(vec2(0.25, 0.1), vec2(0.25, 1.0));
let cubic_bezier = CubicSegment::new_bezier(vec2(0.25, 0.1), vec2(0.25, 1.0));
c.bench_function("easing_1000", |b| {
b.iter(|| {
(0..1000).map(|i| i as f32 / 1000.0).for_each(|t| {
cubic_bezier.ease(black_box(t));
black_box(cubic_bezier.ease(black_box(t)));
})
});
});
}

fn fifteen_degree(c: &mut Criterion) {
let bezier = Bezier::<Vec3A, 16>::new([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
]);
c.bench_function("fifteen_degree_position", |b| {
b.iter(|| bezier.position(black_box(0.5)));
});
}

fn quadratic_2d(c: &mut Criterion) {
let bezier = QuadraticBezier2d::new([[0.0, 0.0], [0.0, 1.0], [1.0, 1.0]]);
c.bench_function("quadratic_position_Vec2", |b| {
b.iter(|| bezier.position(black_box(0.5)));
});
}

fn quadratic(c: &mut Criterion) {
let bezier = QuadraticBezier3d::new([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 1.0]]);
c.bench_function("quadratic_position_Vec3A", |b| {
b.iter(|| bezier.position(black_box(0.5)));
});
}

fn quadratic_vec3(c: &mut Criterion) {
let bezier = Bezier::<Vec3, 3>::new([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 1.0, 1.0]]);
c.bench_function("quadratic_position_Vec3", |b| {
b.iter(|| bezier.position(black_box(0.5)));
});
}

fn cubic_2d(c: &mut Criterion) {
let bezier = CubicBezier2d::new([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]);
let bezier = Bezier::new([[
vec2(0.0, 0.0),
vec2(0.0, 1.0),
vec2(1.0, 0.0),
vec2(1.0, 1.0),
]])
.to_curve();
c.bench_function("cubic_position_Vec2", |b| {
b.iter(|| bezier.position(black_box(0.5)));
b.iter(|| black_box(bezier.position(black_box(0.5))));
});
}

fn cubic(c: &mut Criterion) {
let bezier = CubicBezier3d::new([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
]);
let bezier = Bezier::new([[
vec3a(0.0, 0.0, 0.0),
vec3a(0.0, 1.0, 0.0),
vec3a(1.0, 0.0, 0.0),
vec3a(1.0, 1.0, 1.0),
]])
.to_curve();
c.bench_function("cubic_position_Vec3A", |b| {
b.iter(|| bezier.position(black_box(0.5)));
b.iter(|| black_box(bezier.position(black_box(0.5))));
});
}

fn cubic_vec3(c: &mut Criterion) {
let bezier = Bezier::<Vec3, 4>::new([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
]);
let bezier = Bezier::new([[
vec3(0.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(1.0, 0.0, 0.0),
vec3(1.0, 1.0, 1.0),
]])
.to_curve();
c.bench_function("cubic_position_Vec3", |b| {
b.iter(|| bezier.position(black_box(0.5)));
b.iter(|| black_box(bezier.position(black_box(0.5))));
});
}

fn build_pos_cubic(c: &mut Criterion) {
let bezier = CubicBezier3d::new([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
]);
let bezier = Bezier::new([[
vec3a(0.0, 0.0, 0.0),
vec3a(0.0, 1.0, 0.0),
vec3a(1.0, 0.0, 0.0),
vec3a(1.0, 1.0, 1.0),
]])
.to_curve();
c.bench_function("build_pos_cubic_100_points", |b| {
b.iter(|| bezier.iter_positions(black_box(100)).collect::<Vec<_>>());
b.iter(|| black_box(bezier.iter_positions(black_box(100)).collect::<Vec<_>>()));
});
}

fn build_accel_cubic(c: &mut Criterion) {
let bezier = CubicBezier3d::new([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 1.0],
]);
let bezier = Bezier::new([[
vec3a(0.0, 0.0, 0.0),
vec3a(0.0, 1.0, 0.0),
vec3a(1.0, 0.0, 0.0),
vec3a(1.0, 1.0, 1.0),
]])
.to_curve();
c.bench_function("build_accel_cubic_100_points", |b| {
b.iter(|| bezier.iter_positions(black_box(100)).collect::<Vec<_>>());
b.iter(|| black_box(bezier.iter_positions(black_box(100)).collect::<Vec<_>>()));
});
}

criterion_group!(
benches,
easing,
fifteen_degree,
quadratic_2d,
quadratic,
quadratic_vec3,
cubic_2d,
cubic,
cubic_vec3,
cubic,
build_pos_cubic,
build_accel_cubic,
);
Expand Down
Loading

0 comments on commit 2ea0061

Please sign in to comment.