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

#[no_panic] math #16925

Closed
wants to merge 12 commits into from
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ jobs:
uses: ./.github/actions/install-linux-deps
- name: Check Compile
run: cargo run -p ci -- compile-check-no-std
check-no-panic:
runs-on: ubuntu-latest
timeout-minutes: 30
needs: ci
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
crates/bevy_ecs_compile_fail_tests/target/
crates/bevy_reflect_compile_fail_tests/target/
key: ${{ runner.os }}-cargo-check-no-panic-${{ hashFiles('**/Cargo.toml') }}
- uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-none
- name: Install Linux dependencies
uses: ./.github/actions/install-linux-deps
- name: Check no_panic
env:
RUSTFLAGS: "-D warnings"
run: cargo check -p bevy_math
BenjaminBrienen marked this conversation as resolved.
Show resolved Hide resolved

build-wasm:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", default-featu
"glam",
], optional = true }
variadics_please = "1.1"
no-panic = "0.1"
BenjaminBrienen marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
approx = "0.5"
Expand Down Expand Up @@ -79,6 +80,8 @@ rand = ["dep:rand", "dep:rand_distr", "glam/rand"]
curve = []
# Enable bevy_reflect (requires alloc)
bevy_reflect = ["dep:bevy_reflect", "alloc"]
# Enable to turn on checking no_panic functionality
check_no_panic = []

[lints]
workspace = true
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/src/affine3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use glam::{Affine3A, Mat3, Vec3, Vec3Swizzles, Vec4};
use no_panic::no_panic;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
Expand All @@ -17,6 +18,7 @@ pub struct Affine3 {
impl Affine3 {
/// Calculates the transpose of the affine 4x3 matrix to a 3x4 and formats it for packing into GPU buffers
#[inline]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn to_transpose(&self) -> [Vec4; 3] {
let transpose_3x3 = self.matrix3.transpose();
[
Expand All @@ -28,6 +30,7 @@ impl Affine3 {

/// Calculates the inverse transpose of the 3x3 matrix and formats it for packing into GPU buffers
#[inline]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn inverse_transpose_3x3(&self) -> ([Vec4; 2], f32) {
let inverse_transpose_3x3 = Affine3A::from(self).inverse().matrix3.transpose();
(
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/src/aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::Vec2;
use derive_more::derive::Into;
use no_panic::no_panic;
use thiserror::Error;

#[cfg(feature = "bevy_reflect")]
Expand Down Expand Up @@ -29,6 +30,7 @@ impl AspectRatio {
/// - Either width or height is infinite (`AspectRatioError::Infinite`)
/// - Either width or height is NaN (`AspectRatioError::NaN`)
#[inline]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn try_new(width: f32, height: f32) -> Result<Self, AspectRatioError> {
match (width, height) {
(w, h) if w == 0.0 || h == 0.0 => Err(AspectRatioError::Zero),
Expand All @@ -40,6 +42,7 @@ impl AspectRatio {

/// Attempts to create a new [`AspectRatio`] from a given amount of x pixels and y pixels.
#[inline]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn try_from_pixels(x: u32, y: u32) -> Result<Self, AspectRatioError> {
Self::try_new(x as f32, y as f32)
}
Expand Down
11 changes: 10 additions & 1 deletion crates/bevy_math/src/bounding/bounded2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::{
prelude::{Mat2, Rot2, Vec2},
FloatPow, Isometry2d,
};

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use no_panic::no_panic;

/// Computes the geometric center of the given set of points.
#[inline(always)]
Expand Down Expand Up @@ -44,6 +44,7 @@ pub struct Aabb2d {
impl Aabb2d {
/// Constructs an AABB from its center and half-size.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn new(center: Vec2, half_size: Vec2) -> Self {
debug_assert!(half_size.x >= 0.0 && half_size.y >= 0.0);
Self {
Expand All @@ -59,6 +60,7 @@ impl Aabb2d {
///
/// Panics if the given set of points is empty.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn from_point_cloud(isometry: impl Into<Isometry2d>, points: &[Vec2]) -> Aabb2d {
let isometry = isometry.into();

Expand All @@ -81,6 +83,7 @@ impl Aabb2d {

/// Computes the smallest [`BoundingCircle`] containing this [`Aabb2d`].
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn bounding_circle(&self) -> BoundingCircle {
let radius = self.min.distance(self.max) / 2.0;
BoundingCircle::new(self.center(), radius)
Expand All @@ -91,6 +94,7 @@ impl Aabb2d {
/// If the point is outside the AABB, the returned point will be on the perimeter of the AABB.
/// Otherwise, it will be inside the AABB and returned as is.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn closest_point(&self, point: Vec2) -> Vec2 {
// Clamp point coordinates to the AABB
point.clamp(self.min, self.max)
Expand Down Expand Up @@ -462,6 +466,7 @@ pub struct BoundingCircle {
impl BoundingCircle {
/// Constructs a bounding circle from its center and radius.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn new(center: Vec2, radius: f32) -> Self {
debug_assert!(radius >= 0.);
Self {
Expand All @@ -475,6 +480,7 @@ impl BoundingCircle {
///
/// The bounding circle is not guaranteed to be the smallest possible.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn from_point_cloud(isometry: impl Into<Isometry2d>, points: &[Vec2]) -> BoundingCircle {
let isometry = isometry.into();

Expand All @@ -494,12 +500,14 @@ impl BoundingCircle {

/// Get the radius of the bounding circle
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn radius(&self) -> f32 {
self.circle.radius
}

/// Computes the smallest [`Aabb2d`] containing this [`BoundingCircle`].
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn aabb_2d(&self) -> Aabb2d {
Aabb2d {
min: self.center - Vec2::splat(self.radius()),
Expand All @@ -512,6 +520,7 @@ impl BoundingCircle {
/// If the point is outside the circle, the returned point will be on the perimeter of the circle.
/// Otherwise, it will be inside the circle and returned as is.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn closest_point(&self, point: Vec2) -> Vec2 {
self.circle.closest_point(point - self.center) + self.center
}
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_math/src/bounding/bounded3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
ops::{self, FloatPow},
Isometry3d, Quat, Vec3A,
};
use no_panic::no_panic;

#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
Expand Down Expand Up @@ -48,6 +49,7 @@ pub struct Aabb3d {
impl Aabb3d {
/// Constructs an AABB from its center and half-size.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn new(center: impl Into<Vec3A>, half_size: impl Into<Vec3A>) -> Self {
let (center, half_size) = (center.into(), half_size.into());
debug_assert!(half_size.x >= 0.0 && half_size.y >= 0.0 && half_size.z >= 0.0);
Expand All @@ -64,6 +66,7 @@ impl Aabb3d {
///
/// Panics if the given set of points is empty.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn from_point_cloud(
isometry: impl Into<Isometry3d>,
points: impl Iterator<Item = impl Into<Vec3A>>,
Expand All @@ -89,6 +92,7 @@ impl Aabb3d {

/// Computes the smallest [`BoundingSphere`] containing this [`Aabb3d`].
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn bounding_sphere(&self) -> BoundingSphere {
let radius = self.min.distance(self.max) / 2.0;
BoundingSphere::new(self.center(), radius)
Expand All @@ -99,6 +103,7 @@ impl Aabb3d {
/// If the point is outside the AABB, the returned point will be on the surface of the AABB.
/// Otherwise, it will be inside the AABB and returned as is.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn closest_point(&self, point: impl Into<Vec3A>) -> Vec3A {
// Clamp point coordinates to the AABB
point.into().clamp(self.min, self.max)
Expand Down Expand Up @@ -480,6 +485,7 @@ impl BoundingSphere {
///
/// The bounding sphere is not guaranteed to be the smallest possible.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn from_point_cloud(
isometry: impl Into<Isometry3d>,
points: &[impl Copy + Into<Vec3A>],
Expand All @@ -502,12 +508,14 @@ impl BoundingSphere {

/// Get the radius of the bounding sphere
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn radius(&self) -> f32 {
self.sphere.radius
}

/// Computes the smallest [`Aabb3d`] containing this [`BoundingSphere`].
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn aabb_3d(&self) -> Aabb3d {
Aabb3d {
min: self.center - self.radius(),
Expand All @@ -520,6 +528,7 @@ impl BoundingSphere {
/// If the point is outside the sphere, the returned point will be on the surface of the sphere.
/// Otherwise, it will be inside the sphere and returned as is.
#[inline(always)]
#[cfg_attr(feature = "check_no_panic", no_panic)]
pub fn closest_point(&self, point: impl Into<Vec3A>) -> Vec3A {
let point = point.into();
let radius = self.radius();
Expand Down
Loading
Loading