Skip to content

Commit 18cd93f

Browse files
authored
Fix bevy_math when no alloc is available (#21969)
# Objective `bevy_math` does not work when no `alloc` is available despite the existance of the `alloc` feature which should gate all logic that requires it. Support for no std + no alloc can be useful for some embedded usecases, for example using `bevy_math` types on the GPU using Rust GPU. ## Solution Swap `smallvec` out with `arrayvec`, and fix two minor uses of `alloc::slice::sort_by` (not available on `core` because they allocate `Vec`s internally) ## Testing - Unit tests still pass - I was able to compile a shader with Rust GPU while depending on bevy_math
1 parent b2f5212 commit 18cd93f

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

crates/bevy_math/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ libm = { version = "0.2", optional = true }
2424
approx = { version = "0.5", default-features = false, optional = true }
2525
rand = { version = "0.9", default-features = false, optional = true }
2626
rand_distr = { version = "0.5", optional = true }
27-
smallvec = { version = "1", default-features = false }
27+
arrayvec = { version = "0.7", default-features = false }
2828
bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev", default-features = false, features = [
2929
"glam",
3030
], optional = true }

crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::f32::consts::{FRAC_PI_2, PI, TAU};
1414
#[cfg(feature = "alloc")]
1515
use crate::primitives::{ConvexPolygon, Polygon, Polyline2d};
1616

17-
use smallvec::SmallVec;
17+
use arrayvec::ArrayVec;
1818

1919
use super::{Aabb2d, Bounded2d, BoundingCircle};
2020

@@ -33,10 +33,10 @@ impl Bounded2d for Circle {
3333
// Compute the axis-aligned bounding points of a rotated arc, used for computing the AABB of arcs and derived shapes.
3434
// The return type has room for 7 points so that the CircularSector code can add an additional point.
3535
#[inline]
36-
fn arc_bounding_points(arc: Arc2d, rotation: impl Into<Rot2>) -> SmallVec<[Vec2; 7]> {
36+
fn arc_bounding_points(arc: Arc2d, rotation: impl Into<Rot2>) -> ArrayVec<Vec2, 7> {
3737
// Otherwise, the extreme points will always be either the endpoints or the axis-aligned extrema of the arc's circle.
3838
// We need to compute which axis-aligned extrema are actually contained within the rotated arc.
39-
let mut bounds = SmallVec::<[Vec2; 7]>::new();
39+
let mut bounds = ArrayVec::<Vec2, 7>::new();
4040
let rotation = rotation.into();
4141
bounds.push(rotation * arc.left_endpoint());
4242
bounds.push(rotation * arc.right_endpoint());

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,13 +1727,14 @@ impl Triangle2d {
17271727
let ca = a - c;
17281728

17291729
// a^2 + b^2 < c^2 for an acute triangle
1730-
let mut side_lengths = [
1730+
let side_lengths = [
17311731
ab.length_squared(),
17321732
bc.length_squared(),
17331733
ca.length_squared(),
17341734
];
1735-
side_lengths.sort_by(|a, b| a.partial_cmp(b).unwrap());
1736-
side_lengths[0] + side_lengths[1] > side_lengths[2]
1735+
let sum = side_lengths[0] + side_lengths[1] + side_lengths[2];
1736+
let max = side_lengths[0].max(side_lengths[1]).max(side_lengths[2]);
1737+
sum - max > max
17371738
}
17381739

17391740
/// Checks if the triangle is obtuse, meaning one angle is greater than 90 degrees
@@ -1745,13 +1746,14 @@ impl Triangle2d {
17451746
let ca = a - c;
17461747

17471748
// a^2 + b^2 > c^2 for an obtuse triangle
1748-
let mut side_lengths = [
1749+
let side_lengths = [
17491750
ab.length_squared(),
17501751
bc.length_squared(),
17511752
ca.length_squared(),
17521753
];
1753-
side_lengths.sort_by(|a, b| a.partial_cmp(b).unwrap());
1754-
side_lengths[0] + side_lengths[1] < side_lengths[2]
1754+
let sum = side_lengths[0] + side_lengths[1] + side_lengths[2];
1755+
let max = side_lengths[0].max(side_lengths[1]).max(side_lengths[2]);
1756+
sum - max < max
17551757
}
17561758

17571759
/// Reverse the [`WindingOrder`] of the triangle

crates/bevy_math/src/primitives/dim3.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,14 @@ impl Triangle3d {
12351235
let ca = a - c;
12361236

12371237
// a^2 + b^2 < c^2 for an acute triangle
1238-
let mut side_lengths = [
1238+
let side_lengths = [
12391239
ab.length_squared(),
12401240
bc.length_squared(),
12411241
ca.length_squared(),
12421242
];
1243-
side_lengths.sort_by(|a, b| a.partial_cmp(b).unwrap());
1244-
side_lengths[0] + side_lengths[1] > side_lengths[2]
1243+
let sum = side_lengths[0] + side_lengths[1] + side_lengths[2];
1244+
let max = side_lengths[0].max(side_lengths[1]).max(side_lengths[2]);
1245+
sum - max > max
12451246
}
12461247

12471248
/// Checks if the triangle is obtuse, meaning one angle is greater than 90 degrees
@@ -1253,13 +1254,14 @@ impl Triangle3d {
12531254
let ca = a - c;
12541255

12551256
// a^2 + b^2 > c^2 for an obtuse triangle
1256-
let mut side_lengths = [
1257+
let side_lengths = [
12571258
ab.length_squared(),
12581259
bc.length_squared(),
12591260
ca.length_squared(),
12601261
];
1261-
side_lengths.sort_by(|a, b| a.partial_cmp(b).unwrap());
1262-
side_lengths[0] + side_lengths[1] < side_lengths[2]
1262+
let sum = side_lengths[0] + side_lengths[1] + side_lengths[2];
1263+
let max = side_lengths[0].max(side_lengths[1]).max(side_lengths[2]);
1264+
sum - max < max
12631265
}
12641266

12651267
/// Reverse the triangle by swapping the first and last vertices.

0 commit comments

Comments
 (0)