Skip to content

Direction: Rename from_normalized to new_unchecked #11425

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

Merged
merged 6 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ impl Bounded3d for Capsule {
fn aabb_3d(&self, translation: Vec3, rotation: Quat) -> Aabb3d {
// Get the line segment between the hemispheres of the rotated capsule
let segment = Segment3d {
direction: Direction3d::from_normalized(rotation * Vec3::Y),
// Multiplying a normalized vector (Vec3::Y) with a rotation returns a normalized vector.
direction: Direction3d::new_unchecked(rotation * Vec3::Y),
half_length: self.half_length,
};
let (a, b) = (segment.point1(), segment.point2());
Expand Down
25 changes: 16 additions & 9 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ impl Direction2d {
Self::new_and_length(value).map(|(dir, _)| dir)
}

/// Create a [`Direction2d`] from a [`Vec2`] that is already normalized.
///
/// # Warning
///
/// `value` must be normalized, i.e it's length must be `1.0`.
pub fn new_unchecked(value: Vec2) -> Self {
debug_assert!(value.is_normalized());

Self(value)
}

/// Create a direction from a finite, nonzero [`Vec2`], also returning its original length.
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
Expand All @@ -34,7 +45,7 @@ impl Direction2d {

direction
.map(|dir| (Self(dir), length))
.map_or(Err(InvalidDirectionError::from_length(length)), Ok)
.ok_or(InvalidDirectionError::from_length(length))
}

/// Create a direction from its `x` and `y` components.
Expand All @@ -44,12 +55,6 @@ impl Direction2d {
pub fn from_xy(x: f32, y: f32) -> Result<Self, InvalidDirectionError> {
Self::new(Vec2::new(x, y))
}

/// Create a direction from a [`Vec2`] that is already normalized.
pub fn from_normalized(value: Vec2) -> Self {
debug_assert!(value.is_normalized());
Self(value)
}
}

impl TryFrom<Vec2> for Direction2d {
Expand Down Expand Up @@ -163,8 +168,10 @@ impl Segment2d {
pub fn from_points(point1: Vec2, point2: Vec2) -> (Self, Vec2) {
let diff = point2 - point1;
let length = diff.length();

(
Self::new(Direction2d::from_normalized(diff / length), length),
// We are dividing by the length here, so the vector is normalized.
Self::new(Direction2d::new_unchecked(diff / length), length),
(point1 + point2) / 2.,
)
}
Expand Down Expand Up @@ -457,7 +464,7 @@ mod tests {
);
assert_eq!(
Direction2d::new_and_length(Vec2::X * 6.5),
Ok((Direction2d::from_normalized(Vec2::X), 6.5))
Ok((Direction2d::X, 6.5))
);
}

Expand Down
25 changes: 16 additions & 9 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ impl Direction3d {
Self::new_and_length(value).map(|(dir, _)| dir)
}

/// Create a [`Direction3d`] from a [`Vec3`] that is already normalized.
///
/// # Warning
///
/// `value` must be normalized, i.e it's length must be `1.0`.
pub fn new_unchecked(value: Vec3) -> Self {
debug_assert!(value.is_normalized());

Self(value)
}

/// Create a direction from a finite, nonzero [`Vec3`], also returning its original length.
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
Expand All @@ -38,7 +49,7 @@ impl Direction3d {

direction
.map(|dir| (Self(dir), length))
.map_or(Err(InvalidDirectionError::from_length(length)), Ok)
.ok_or(InvalidDirectionError::from_length(length))
}

/// Create a direction from its `x`, `y`, and `z` components.
Expand All @@ -48,12 +59,6 @@ impl Direction3d {
pub fn from_xyz(x: f32, y: f32, z: f32) -> Result<Self, InvalidDirectionError> {
Self::new(Vec3::new(x, y, z))
}

/// Create a direction from a [`Vec3`] that is already normalized.
pub fn from_normalized(value: Vec3) -> Self {
debug_assert!(value.is_normalized());
Self(value)
}
}

impl TryFrom<Vec3> for Direction3d {
Expand Down Expand Up @@ -146,8 +151,10 @@ impl Segment3d {
pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) {
let diff = point2 - point1;
let length = diff.length();

(
Self::new(Direction3d::from_normalized(diff / length), length),
// We are dividing by the length here, so the vector is normalized.
Self::new(Direction3d::new_unchecked(diff / length), length),
(point1 + point2) / 2.,
)
}
Expand Down Expand Up @@ -423,7 +430,7 @@ mod test {
);
assert_eq!(
Direction3d::new_and_length(Vec3::X * 6.5),
Ok((Direction3d::from_normalized(Vec3::X), 6.5))
Ok((Direction3d::X, 6.5))
);
}
}