Add Dir2::from_xy_unchecked and Dir3::from_xyz_unchecked (#14587)

# Objective

Bevy's direction types have `new` and `new_unchecked` constructors, but
no unchecked variant for the `Dir2::from_xy` and `Dir3::from_xyz`
methods.

For me, this has several times lead to constructing directions like
this, in cases where the components of the direction are already known
to be normalized:

```rust
let normal = Dir2::new_unchecked(Vec2::new(-ray.direction.x.signum(), 0.0));
```

```rust
segment.direction =
    Dir2::new_unchecked(Vec2::new(-segment.direction.x, segment.direction.y));
```

For consistency and ergonomics, it would be nice to have unchecked
variants of `Dir2::from_xy` and `Dir3::from_xyz`:

```rust
let normal = Dir2::from_xy_unchecked(-ray.direction.x.signum(), 0.0);
```

```rust
segment.direction = Dir2::from_xy_unchecked(-segment.direction.x, segment.direction.y);
```

## Solution

Add `Dir2::from_xy_unchecked` and `Dir3::from_xyz_unchecked`.
This commit is contained in:
Joona Aalto 2024-08-02 16:10:13 +03:00 committed by GitHub
parent b98d15f278
commit e6261b0f5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -169,6 +169,15 @@ impl Dir2 {
Self::new(Vec2::new(x, y))
}
/// Create a direction from its `x` and `y` components, assuming the resulting vector is normalized.
///
/// # Warning
///
/// The vector produced from `x` and `y` must be normalized, i.e its length must be `1.0`.
pub fn from_xy_unchecked(x: f32, y: f32) -> Self {
Self::new_unchecked(Vec2::new(x, y))
}
/// Returns the inner [`Vec2`]
pub const fn as_vec2(&self) -> Vec2 {
self.0
@ -419,6 +428,15 @@ impl Dir3 {
Self::new(Vec3::new(x, y, z))
}
/// Create a direction from its `x`, `y`, and `z` components, assuming the resulting vector is normalized.
///
/// # Warning
///
/// The vector produced from `x`, `y`, and `z` must be normalized, i.e its length must be `1.0`.
pub fn from_xyz_unchecked(x: f32, y: f32, z: f32) -> Self {
Self::new_unchecked(Vec3::new(x, y, z))
}
/// Returns the inner [`Vec3`]
pub const fn as_vec3(&self) -> Vec3 {
self.0
@ -675,6 +693,15 @@ impl Dir3A {
Self::new(Vec3A::new(x, y, z))
}
/// Create a direction from its `x`, `y`, and `z` components, assuming the resulting vector is normalized.
///
/// # Warning
///
/// The vector produced from `x`, `y`, and `z` must be normalized, i.e its length must be `1.0`.
pub fn from_xyz_unchecked(x: f32, y: f32, z: f32) -> Self {
Self::new_unchecked(Vec3A::new(x, y, z))
}
/// Returns the inner [`Vec3A`]
pub const fn as_vec3a(&self) -> Vec3A {
self.0