Skip to content

Commit 0387331

Browse files
doonvtguichaouaJondolf
authored
Direction: Rename from_normalized to new_unchecked (#11425)
# Objective `Direction2d::from_normalized` & `Direction3d::from_normalized` don't emphasize that importance of the vector being normalized enough. ## Solution Rename `from_normalized` to `new_unchecked` and add more documentation. --- `Direction2d` and `Direction3d` were added somewhat recently in #10466 (after 0.12), so I don't think documenting the changelog and migration guide is necessary (Since there is no major previous version to migrate from). But here it is anyway in case it's needed: ## Changelog - Renamed `Direction2d::from_normalized` and `Direction3d::from_normalized` to `new_unchecked`. ## Migration Guide - Renamed `Direction2d::from_normalized` and `Direction3d::from_normalized` to `new_unchecked`. --------- Co-authored-by: Tristan Guichaoua <[email protected]> Co-authored-by: Joona Aalto <[email protected]>
1 parent c6f4583 commit 0387331

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ impl Bounded3d for Capsule {
150150
fn aabb_3d(&self, translation: Vec3, rotation: Quat) -> Aabb3d {
151151
// Get the line segment between the hemispheres of the rotated capsule
152152
let segment = Segment3d {
153-
direction: Direction3d::from_normalized(rotation * Vec3::Y),
153+
// Multiplying a normalized vector (Vec3::Y) with a rotation returns a normalized vector.
154+
direction: Direction3d::new_unchecked(rotation * Vec3::Y),
154155
half_length: self.half_length,
155156
};
156157
let (a, b) = (segment.point1(), segment.point2());

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ impl Direction2d {
2424
Self::new_and_length(value).map(|(dir, _)| dir)
2525
}
2626

27+
/// Create a [`Direction2d`] from a [`Vec2`] that is already normalized.
28+
///
29+
/// # Warning
30+
///
31+
/// `value` must be normalized, i.e it's length must be `1.0`.
32+
pub fn new_unchecked(value: Vec2) -> Self {
33+
debug_assert!(value.is_normalized());
34+
35+
Self(value)
36+
}
37+
2738
/// Create a direction from a finite, nonzero [`Vec2`], also returning its original length.
2839
///
2940
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
@@ -34,7 +45,7 @@ impl Direction2d {
3445

3546
direction
3647
.map(|dir| (Self(dir), length))
37-
.map_or(Err(InvalidDirectionError::from_length(length)), Ok)
48+
.ok_or(InvalidDirectionError::from_length(length))
3849
}
3950

4051
/// Create a direction from its `x` and `y` components.
@@ -44,12 +55,6 @@ impl Direction2d {
4455
pub fn from_xy(x: f32, y: f32) -> Result<Self, InvalidDirectionError> {
4556
Self::new(Vec2::new(x, y))
4657
}
47-
48-
/// Create a direction from a [`Vec2`] that is already normalized.
49-
pub fn from_normalized(value: Vec2) -> Self {
50-
debug_assert!(value.is_normalized());
51-
Self(value)
52-
}
5358
}
5459

5560
impl TryFrom<Vec2> for Direction2d {
@@ -187,8 +192,10 @@ impl Segment2d {
187192
pub fn from_points(point1: Vec2, point2: Vec2) -> (Self, Vec2) {
188193
let diff = point2 - point1;
189194
let length = diff.length();
195+
190196
(
191-
Self::new(Direction2d::from_normalized(diff / length), length),
197+
// We are dividing by the length here, so the vector is normalized.
198+
Self::new(Direction2d::new_unchecked(diff / length), length),
192199
(point1 + point2) / 2.,
193200
)
194201
}
@@ -477,7 +484,7 @@ mod tests {
477484
);
478485
assert_eq!(
479486
Direction2d::new_and_length(Vec2::X * 6.5),
480-
Ok((Direction2d::from_normalized(Vec2::X), 6.5))
487+
Ok((Direction2d::X, 6.5))
481488
);
482489
}
483490

crates/bevy_math/src/primitives/dim3.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ impl Direction3d {
2828
Self::new_and_length(value).map(|(dir, _)| dir)
2929
}
3030

31+
/// Create a [`Direction3d`] from a [`Vec3`] that is already normalized.
32+
///
33+
/// # Warning
34+
///
35+
/// `value` must be normalized, i.e it's length must be `1.0`.
36+
pub fn new_unchecked(value: Vec3) -> Self {
37+
debug_assert!(value.is_normalized());
38+
39+
Self(value)
40+
}
41+
3142
/// Create a direction from a finite, nonzero [`Vec3`], also returning its original length.
3243
///
3344
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length
@@ -38,7 +49,7 @@ impl Direction3d {
3849

3950
direction
4051
.map(|dir| (Self(dir), length))
41-
.map_or(Err(InvalidDirectionError::from_length(length)), Ok)
52+
.ok_or(InvalidDirectionError::from_length(length))
4253
}
4354

4455
/// Create a direction from its `x`, `y`, and `z` components.
@@ -48,12 +59,6 @@ impl Direction3d {
4859
pub fn from_xyz(x: f32, y: f32, z: f32) -> Result<Self, InvalidDirectionError> {
4960
Self::new(Vec3::new(x, y, z))
5061
}
51-
52-
/// Create a direction from a [`Vec3`] that is already normalized.
53-
pub fn from_normalized(value: Vec3) -> Self {
54-
debug_assert!(value.is_normalized());
55-
Self(value)
56-
}
5762
}
5863

5964
impl TryFrom<Vec3> for Direction3d {
@@ -146,8 +151,10 @@ impl Segment3d {
146151
pub fn from_points(point1: Vec3, point2: Vec3) -> (Self, Vec3) {
147152
let diff = point2 - point1;
148153
let length = diff.length();
154+
149155
(
150-
Self::new(Direction3d::from_normalized(diff / length), length),
156+
// We are dividing by the length here, so the vector is normalized.
157+
Self::new(Direction3d::new_unchecked(diff / length), length),
151158
(point1 + point2) / 2.,
152159
)
153160
}
@@ -423,7 +430,7 @@ mod test {
423430
);
424431
assert_eq!(
425432
Direction3d::new_and_length(Vec3::X * 6.5),
426-
Ok((Direction3d::from_normalized(Vec3::X), 6.5))
433+
Ok((Direction3d::X, 6.5))
427434
);
428435
}
429436
}

0 commit comments

Comments
 (0)