pub struct Vec3(/* private fields */);Expand description
A 3-component non-owning view of a Euclidean vector.
§Guarantees
This type has the strict requirement that it can only reference a
3-component slice of f32 values. It is guaranteed to never refer to
more or less than 3 entries.
§Relation to Vector3
Vec3 is a non-owning equivalent of Vector3. This enables non-vector
types to either Deref or provide conversion-related utilities into
Vec3 types to be able to access and benefit from vector operations.
A basic example of where this can be useful is in something like a Point2
type, where it may not be implemented directly in terms of a Vector3,
but can convert into a Vec3 to benefit from operations on vectors.
Implementations§
source§impl Vec3
impl Vec3
sourcepub const fn from_array(array: &[f32; 3]) -> &Self
pub const fn from_array(array: &[f32; 3]) -> &Self
Forms a reference to a Vec3 from a 3-component f32 array.
This function is identical to from_slice_unchecked, except it is not
marked unsafe.
§Parameters
array- an array containing 3f32values.
§Examples
Basic use:
let array: [f32; 3] = [0.1, 0.42, 3.14];
let vec = Vec3::from_array(&array);
assert_eq!(vec.as_ptr(), array.as_ptr());
assert_eq!(vec.x(), array[0]);
assert_eq!(vec.y(), array[1]);
assert_eq!(vec.z(), array[2]);sourcepub fn from_mut_array(array: &mut [f32; 3]) -> &mut Self
pub fn from_mut_array(array: &mut [f32; 3]) -> &mut Self
Forms a mutable reference to a Vec2 from a 3-component f32 array.
This function is identical to from_mut_slice_unchecked, except it is
not marked unsafe.
§Parameters
array- an array containing 3f32values.
§Examples
Basic use:
let mut array: [f32; 3] = [0.1, 0.42, 3.14];
let vec = Vec3::from_mut_array(&mut array);
assert_eq!(vec.as_ptr(), array.as_ptr());sourcepub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>
pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>
Forms a reference to a Vec3 from a slice of f32.
This requires that slice.len() == 3, otherwise this returns VecError.
§Parameters
slice- the slice off32s.
§Examples
Basic use:
let slice = &[0.1, 0.42, 3.14];
let vec = Vec3::from_slice(slice).unwrap();
assert_eq!(vec.as_ptr(), slice.as_ptr());
assert_eq!(vec.x(), slice[0]);
assert_eq!(vec.y(), slice[1]);
assert_eq!(vec.z(), slice[2]);Invalid size:
let slice = &[0.1];
let vec = Vec3::from_slice(slice);
assert!(vec.is_err());sourcepub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>
pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>
Forms a mutable reference to a Vec3 from a mutable slice of f32.
This requires that slice.len() == 3, otherwise this returns VecError.
§Parameters
slice- the mutable slice off32s.
§Examples
Basic use:
let slice = &mut [0.1, 0.42, 3.19];
let vec = Vec3::from_mut_slice(slice).unwrap();
assert_eq!(vec.as_ptr(), slice.as_ptr());Invalid size:
let slice = &mut [0.1];
let vec = Vec3::from_mut_slice(slice);
assert!(vec.is_err());sourcepub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self
pub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self
sourcepub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self
pub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self
sourcepub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec3
pub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec3
sourcepub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec3
pub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec3
sourcepub fn as_mut_slice(&mut self) -> &mut [f32]
pub fn as_mut_slice(&mut self) -> &mut [f32]
sourcepub fn as_mut_ptr(&mut self) -> *mut f32
pub fn as_mut_ptr(&mut self) -> *mut f32
Returns this vector as a mutable ptr of f32.
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>
Returns a mutable iterator over the vector.
sourcepub fn x_mut(&mut self) -> &mut f32
pub fn x_mut(&mut self) -> &mut f32
Returns a mutable reference to the X-coordinate of this 3-component vector.
sourcepub fn y_mut(&mut self) -> &mut f32
pub fn y_mut(&mut self) -> &mut f32
Returns a mutable reference to the Y-coordinate of this 3-component vector.
sourcepub fn z_mut(&mut self) -> &mut f32
pub fn z_mut(&mut self) -> &mut f32
Returns a mutable reference to the Z-coordinate of this 3-component vector.
sourcepub fn xy_mut(&mut self) -> &mut Vec2
pub fn xy_mut(&mut self) -> &mut Vec2
Returns a mutable reference to the xy coordinates of this vector.
sourcepub fn yz_mut(&mut self) -> &mut Vec2
pub fn yz_mut(&mut self) -> &mut Vec2
Returns a mutable reference to the yz coordinates of this vector.
sourcepub fn square_magnitude(&self) -> f32
pub fn square_magnitude(&self) -> f32
Computes the square magnitude of this two-component vector.
sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Computes the magnitude of this vector.
Where possible, consider using Vec3::square_magnitude as this will
avoid the need to compute the square-root.
sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Queries whether this vector is normalized (e.g. has a magnitude of 1).
sourcepub fn normalized(&self) -> Vector3
pub fn normalized(&self) -> Vector3
Returns normalized vector.
sourcepub fn is_finite(&self) -> bool
pub fn is_finite(&self) -> bool
Returns whether all components of this Vec are finite.
§Example
Basic use:
let finite = Vector3::UNIT_X;
let not_finite = Vector3::new(f32::INFINITY, 3.0, 0.0);
assert!(finite.is_finite());
assert!(!not_finite.is_finite());sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
Trait Implementations§
source§impl AddAssign<&Vec3> for Vec3
impl AddAssign<&Vec3> for Vec3
source§fn add_assign(&mut self, rhs: &Vec3)
fn add_assign(&mut self, rhs: &Vec3)
+= operation. Read moresource§impl AddAssign<&Vec3> for Vector3
impl AddAssign<&Vec3> for Vector3
source§fn add_assign(&mut self, rhs: &Vec3)
fn add_assign(&mut self, rhs: &Vec3)
+= operation. Read moresource§impl AsMut<Vec3> for EulerAngles
impl AsMut<Vec3> for EulerAngles
source§impl AsRef<Vec3> for EulerAngles
impl AsRef<Vec3> for EulerAngles
source§impl Borrow<Vec3> for EulerAngles
impl Borrow<Vec3> for EulerAngles
source§impl BorrowMut<[f32]> for Vec3
impl BorrowMut<[f32]> for Vec3
source§fn borrow_mut(&mut self) -> &mut [f32]
fn borrow_mut(&mut self) -> &mut [f32]
source§impl BorrowMut<Vec3> for EulerAngles
impl BorrowMut<Vec3> for EulerAngles
source§fn borrow_mut(&mut self) -> &mut Vec3
fn borrow_mut(&mut self) -> &mut Vec3
source§impl BorrowMut<Vec3> for Vector3
impl BorrowMut<Vec3> for Vector3
source§fn borrow_mut(&mut self) -> &mut Vec3
fn borrow_mut(&mut self) -> &mut Vec3
source§impl DivAssign<f32> for Vec3
impl DivAssign<f32> for Vec3
source§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/= operation. Read moresource§impl MulAssign<f32> for Vec3
impl MulAssign<f32> for Vec3
source§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*= operation. Read moresource§impl PartialOrd for Vec3
impl PartialOrd for Vec3
source§impl RemAssign<f32> for Vec3
impl RemAssign<f32> for Vec3
source§fn rem_assign(&mut self, rhs: f32)
fn rem_assign(&mut self, rhs: f32)
%= operation. Read moresource§impl SubAssign<&Vec3> for Vec3
impl SubAssign<&Vec3> for Vec3
source§fn sub_assign(&mut self, rhs: &Vec3)
fn sub_assign(&mut self, rhs: &Vec3)
-= operation. Read moresource§impl SubAssign<&Vec3> for Vector3
impl SubAssign<&Vec3> for Vector3
source§fn sub_assign(&mut self, rhs: &Vec3)
fn sub_assign(&mut self, rhs: &Vec3)
-= operation. Read more