#[repr(C)]pub struct Vector3D<T> {
pub x: T,
pub y: T,
pub z: T,
}
Expand description
A 3d Vector tagged with a unit.
Fields§
§x: T
The x
(traditionally, horizontal) coordinate.
y: T
The y
(traditionally, vertical) coordinate.
z: T
The z
(traditionally, depth) coordinate.
Implementations§
Source§impl<T> Vector3D<T>
impl<T> Vector3D<T>
Sourcepub const fn new(x: T, y: T, z: T) -> Self
pub const fn new(x: T, y: T, z: T) -> Self
Constructor, setting all components to zero. Constructor taking scalar values directly.
Sourcepub fn splat(v: T) -> Selfwhere
T: Clone,
pub fn splat(v: T) -> Selfwhere
T: Clone,
Constructor setting all components to the same value.
Sourcepub fn from_untyped(p: Vector3D<T>) -> Self
pub fn from_untyped(p: Vector3D<T>) -> Self
Tag a unitless value with units.
Sourcepub fn abs(self) -> Selfwhere
T: Signed,
pub fn abs(self) -> Selfwhere
T: Signed,
Computes the vector with absolute values of each component.
§Example
assert_eq!(Vector3D::new(-1, 0, 2).abs(), Vector3D::new(1, 0, 2));
let vec = Vector3D::new(f32::NAN, 0.0, -f32::MAX).abs();
assert!(vec.x.is_nan());
assert_eq!(vec.y, 0.0);
assert_eq!(vec.z, f32::MAX);
§Panics
The behavior for each component follows the scalar type’s implementation of
num_traits::Signed::abs
.
Source§impl<T: Copy> Vector3D<T>
impl<T: Copy> Vector3D<T>
Sourcepub fn component_mul(self, other: Self) -> Selfwhere
T: Mul<Output = T>,
pub fn component_mul(self, other: Self) -> Selfwhere
T: Mul<Output = T>,
Returns the component-wise multiplication of the two vectors.
Sourcepub fn component_div(self, other: Self) -> Selfwhere
T: Div<Output = T>,
pub fn component_div(self, other: Self) -> Selfwhere
T: Div<Output = T>,
Returns the component-wise division of the two vectors.
Sourcepub fn to_untyped(self) -> Vector3D<T>
pub fn to_untyped(self) -> Vector3D<T>
Drop the units, preserving only the numeric value.
Source§impl<T> Vector3D<T>
impl<T> Vector3D<T>
Sourcepub fn square_length(self) -> T
pub fn square_length(self) -> T
Returns the vector’s length squared.
Sourcepub fn project_onto_vector(self, onto: Self) -> Self
pub fn project_onto_vector(self, onto: Self) -> Self
Returns this vector projected onto another one.
Projecting onto a nil vector will cause a division by zero.
Source§impl<T: Float> Vector3D<T>
impl<T: Float> Vector3D<T>
Sourcepub fn try_normalize(self) -> Option<Self>
pub fn try_normalize(self) -> Option<Self>
Returns the vector with length of one unit.
Unlike Vector2D::normalize
, this returns None in the case that the
length of the vector is zero.
Sourcepub fn robust_normalize(self) -> Self
pub fn robust_normalize(self) -> Self
Return the normalized vector even if the length is larger than the max value of Float.
Sourcepub fn with_max_length(self, max_length: T) -> Self
pub fn with_max_length(self, max_length: T) -> Self
Return this vector capped to a maximum length.
Sourcepub fn with_min_length(self, min_length: T) -> Self
pub fn with_min_length(self, min_length: T) -> Self
Return this vector with a minimum length applied.
Sourcepub fn clamp_length(self, min: T, max: T) -> Self
pub fn clamp_length(self, min: T, max: T) -> Self
Return this vector with minimum and maximum lengths applied.
Source§impl<T: NumCast + Copy> Vector3D<T>
impl<T: NumCast + Copy> Vector3D<T>
Sourcepub fn cast<NewT: NumCast>(self) -> Vector3D<NewT>
pub fn cast<NewT: NumCast>(self) -> Vector3D<NewT>
Cast from one numeric representation to another, preserving the units.
When casting from floating vector to integer coordinates, the decimals are truncated
as one would expect from a simple cast, but this behavior does not always make sense
geometrically. Consider using round()
, ceil()
or floor()
before casting.
Sourcepub fn try_cast<NewT: NumCast>(self) -> Option<Vector3D<NewT>>
pub fn try_cast<NewT: NumCast>(self) -> Option<Vector3D<NewT>>
Fallible cast from one numeric representation to another, preserving the units.
When casting from floating vector to integer coordinates, the decimals are truncated
as one would expect from a simple cast, but this behavior does not always make sense
geometrically. Consider using round()
, ceil()
or floor()
before casting.
Sourcepub fn to_usize(self) -> Vector3D<usize>
pub fn to_usize(self) -> Vector3D<usize>
Cast into an usize
vector, truncating decimals if any.
When casting from floating vector vectors, it is worth considering whether
to round()
, ceil()
or floor()
before the cast in order to obtain
the desired conversion behavior.
Sourcepub fn to_u32(self) -> Vector3D<u32>
pub fn to_u32(self) -> Vector3D<u32>
Cast into an u32
vector, truncating decimals if any.
When casting from floating vector vectors, it is worth considering whether
to round()
, ceil()
or floor()
before the cast in order to obtain
the desired conversion behavior.
Trait Implementations§
Source§impl<T: Copy + Add<T, Output = T>> AddAssign for Vector3D<T>
impl<T: Copy + Add<T, Output = T>> AddAssign for Vector3D<T>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moreSource§impl<T: Copy + Div<T, Output = T>> DivAssign<T> for Vector3D<T>
impl<T: Copy + Div<T, Output = T>> DivAssign<T> for Vector3D<T>
Source§fn div_assign(&mut self, scale: T)
fn div_assign(&mut self, scale: T)
/=
operation. Read moreSource§impl<T: Copy + Mul<T, Output = T>> MulAssign<T> for Vector3D<T>
impl<T: Copy + Mul<T, Output = T>> MulAssign<T> for Vector3D<T>
Source§fn mul_assign(&mut self, scale: T)
fn mul_assign(&mut self, scale: T)
*=
operation. Read moreSource§impl<T: Copy + Sub<T, Output = T>> SubAssign for Vector3D<T>
impl<T: Copy + Sub<T, Output = T>> SubAssign for Vector3D<T>
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-=
operation. Read more