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

Impl TryFrom vector for directions and add InvalidDirectionError #10884

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Primitive2d, WindingOrder};
use super::{InvalidDirectionError, Primitive2d, WindingOrder};
use crate::Vec2;

/// A normalized vector pointing in a direction in 2D space
Expand All @@ -20,6 +20,14 @@ impl Direction2d {
}
}

impl TryFrom<Vec2> for Direction2d {
type Error = InvalidDirectionError;

fn try_from(value: Vec2) -> Result<Self, Self::Error> {
Self::new(value).map_or(Err(InvalidDirectionError), Ok)
}
}

impl std::ops::Deref for Direction2d {
type Target = Vec2;
fn deref(&self) -> &Self::Target {
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Primitive3d;
use super::{InvalidDirectionError, Primitive3d};
use crate::Vec3;

/// A normalized vector pointing in a direction in 3D space
Expand All @@ -20,6 +20,14 @@ impl Direction3d {
}
}

impl TryFrom<Vec3> for Direction3d {
type Error = InvalidDirectionError;

fn try_from(value: Vec3) -> Result<Self, Self::Error> {
Self::new(value).map_or(Err(InvalidDirectionError), Ok)
}
}

impl std::ops::Deref for Direction3d {
type Target = Vec3;
fn deref(&self) -> &Self::Target {
Expand Down
13 changes: 13 additions & 0 deletions crates/bevy_math/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ pub trait Primitive2d {}
/// A marker trait for 3D primitives
pub trait Primitive3d {}

/// An error indicating that a direction is invalid because it is zero or not finite.
#[derive(Debug)]
pub struct InvalidDirectionError;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably more useful as an enum: the different failure modes are useful to bubble up.

Copy link
Contributor Author

@Jondolf Jondolf Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to do a custom try_normalize for this, but it should be easy since it's just an .is_finite() and a comparison against zero

Copy link
Contributor Author

@Jondolf Jondolf Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or alternatively, do the checks again, but only in error cases. I think this could be better since we can also check for NaN separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to an enum with Zero, Infinite and NaN variants and added tests to verify that it gives the correct errors.


impl std::fmt::Display for InvalidDirectionError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why thiserror is not used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bevy_math doesn't have thiserror as a dependency currently. Could add it though, especially if we add more error types

fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Direction can not be zero (or very close to zero), or non-finite."
)
}
}

/// The winding order for a set of points
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WindingOrder {
Expand Down