-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Feature/glam cross constants #21561
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
base: main
Are you sure you want to change the base?
Feature/glam cross constants #21561
Changes from 9 commits
cf4b1dd
9b2e9e1
c10d08f
35ab172
a913e2b
003b605
99986cc
4be20e8
ddf0d5a
91452e2
f6c1789
621263b
a8f891d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ use thiserror::Error; | |
| /// An error indicating that a direction is invalid. | ||
| #[derive(Debug, PartialEq, Error)] | ||
| pub enum InvalidDirectionError { | ||
| /// The length of the direction vector is zero or very close to zero. | ||
| /// The length of the direction vector is zero or very close to zero.XES | ||
| #[error("The length of the direction vector is zero or very close to zero")] | ||
| Zero, | ||
| /// The length of the direction vector is `std::f32::INFINITY`. | ||
|
|
@@ -107,6 +107,8 @@ impl Dir2 { | |
| pub const NEG_Y: Self = Self(Vec2::NEG_Y); | ||
| /// The directional axes. | ||
| pub const AXES: [Self; 2] = [Self::X, Self::Y]; | ||
| /// The cardinal directions. | ||
| pub const CARDINALS: [Self; 4] = [Self::X, Self::NEG_X, Self::Y, Self::NEG_Y]; | ||
|
|
||
| /// The "north" direction, equivalent to [`Dir2::Y`]. | ||
| pub const NORTH: Self = Self(Vec2::Y); | ||
|
|
@@ -125,6 +127,25 @@ impl Dir2 { | |
| /// The "south-west" direction, between [`Dir2::SOUTH`] and [`Dir2::WEST`]. | ||
| pub const SOUTH_WEST: Self = Self(Vec2::new(-FRAC_1_SQRT_2, -FRAC_1_SQRT_2)); | ||
|
|
||
| /// The diagonals between the cardinal directions. | ||
| pub const DIAGONALS: [Self; 4] = [ | ||
| Self::NORTH_EAST, | ||
| Self::NORTH_WEST, | ||
| Self::SOUTH_EAST, | ||
| Self::SOUTH_WEST, | ||
| ]; | ||
| /// All neighboring directions on a grid. A combination of [`Self::CARDINALS`] and [`Self::DIAGONALS`] | ||
|
||
| pub const ALL_NEIGHBORS: [Self; 8] = [ | ||
| Self::X, | ||
| Self::NEG_X, | ||
| Self::Y, | ||
| Self::NEG_Y, | ||
| Self::NORTH_EAST, | ||
| Self::NORTH_WEST, | ||
| Self::SOUTH_EAST, | ||
| Self::SOUTH_WEST, | ||
| ]; | ||
|
|
||
| /// Create a direction from a finite, nonzero [`Vec2`], normalizing it. | ||
| /// | ||
| /// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length | ||
|
|
@@ -395,6 +416,116 @@ impl Dir3 { | |
| pub const NEG_Z: Self = Self(Vec3::NEG_Z); | ||
| /// The directional axes. | ||
| pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z]; | ||
| /// The cardinal directions. | ||
| pub const CARDINALS: [Self; 6] = [ | ||
| Self::X, | ||
| Self::NEG_X, | ||
| Self::Y, | ||
| Self::NEG_Y, | ||
| Self::Z, | ||
| Self::NEG_Z, | ||
| ]; | ||
|
|
||
| // Adding this allow here to make sure that the precision in FRAC_1_SQRT_2 | ||
| // and here is the same | ||
| #[expect( | ||
| clippy::excessive_precision, | ||
| reason = "compatibility with the unstable rust definition" | ||
| )] | ||
| /// Approximation of 1/sqrt(3) needed for the diagonals in 3D space | ||
| const FRAC_1_SQRT_3: f32 = 0.577350269189625764509148780501957456_f32; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a bit weird, since an f32 can't hold more than about 7 digits of precision, but I don't know if there's maybe another reason to want an extra-precise constant definition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these excessively long definitions are copied from the rust std::f32::consts, but I'm unsure why they would be there in the first place There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i propose to keep this definition for now for the sake of consistency with the rust stdlib, so that when the FRAC_1_SQRT_3 gets stabilized we can just switch over to that and be certain that there's no weird behavior from unexpected changes |
||
| /// The diagonals between the cardinal directions. | ||
|
||
| pub const DIAGONALS: [Self; 8] = [ | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| ]; | ||
| /// All neighboring directions on a grid. A combination of [`Self::CARDINALS`] and [`Self::DIAGONALS`] | ||
| pub const ALL_NEIGHBORS: [Self; 14] = [ | ||
|
||
| Self::X, | ||
| Self::NEG_X, | ||
| Self::Y, | ||
| Self::NEG_Y, | ||
| Self::Z, | ||
| Self::NEG_Z, | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| Self(Vec3::new( | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| -Self::FRAC_1_SQRT_3, | ||
| )), | ||
| ]; | ||
|
|
||
| /// Create a direction from a finite, nonzero [`Vec3`], normalizing it. | ||
| /// | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.