alloy::math::vec

Struct Vec4

source
pub struct Vec4(/* private fields */);
Expand description

A 4-component non-owning view of a Euclidean vector.

§Guarantees

This type has the strict requirement that it can only reference a 4-component slice of f32 values. It is guaranteed to never refer to more or less than 4 entries.

§Relation to Vector4

Vec4 is a non-owning equivalent of [Vector2]. This enables non-vector types to either Deref or provide conversion-related utilities into Vec4 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 Vector4, but can convert into a Vec4 to benefit from operations on vectors.

Implementations§

source§

impl Vec4

source

pub const fn from_array(array: &[f32; 4]) -> &Self

Forms a reference to a Vec4 from a 4-component f32 array.

This function is identical to from_slice_unchecked, except it is not marked unsafe.

§Parameters
  • array - an array containing 4 f32 values.
§Examples

Basic use:

let array: [f32; 4] = [0.1, 0.42, 3.14, 0.9];

let vec = Vec4::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]);
assert_eq!(vec.w(), array[3]);
source

pub fn from_mut_array(array: &mut [f32; 4]) -> &mut Self

Forms a mutable reference to a Vec4 from a 4-component f32 array.

This function is identical to from_mut_slice_unchecked, except it is not marked unsafe.

§Parameters
  • array - an array containing 4 f32 values.
§Examples

Basic use:

let mut array: [f32; 4] = [0.1, 42.0, 3.14, 0.9];

let vec = Vec4::from_mut_array(&mut array);

assert_eq!(vec.as_ptr(), array.as_ptr());
source

pub const fn from_slice(slice: &[f32]) -> Result<&Self, VecError>

Forms a reference to a Vec4 from a slice of f32.

This requires that slice.len() == 4, otherwise this returns VecError.

§Parameters
  • slice - the slice of f32s.
§Examples

Basic use:

let slice = &[0.1, 0.42, 3.14, 0.9];

let vec = Vec4::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]);
assert_eq!(vec.w(), slice[3]);

Invalid size:

let slice = &[0.1];

let vec = Vec4::from_slice(slice);

assert!(vec.is_err());
source

pub fn from_mut_slice(slice: &mut [f32]) -> Result<&mut Self, VecError>

Forms a mutable reference to a Vec4 from a mutable slice of f32.

This requires that slice.len() == 4, otherwise this returns VecError.

§Parameters
  • slice - the mutable slice of f32s.
§Examples

Basic use:

let slice = &mut [0.1, 0.42, 3.14, 0.9];

let vec = Vec4::from_mut_slice(slice).unwrap();

assert_eq!(vec.as_ptr(), slice.as_ptr());

Invalid size:

let slice = &mut [0.1];

let vec = Vec4::from_mut_slice(slice);

assert!(vec.is_err());
source

pub const unsafe fn from_slice_unchecked(slice: &[f32]) -> &Self

Forms a reference to a Vec4 from a slice of f32 that is assumed to contain two values.

§Parameters
  • slice - the slice of f32s.
§Safety

slice.len() must be equal to 2.

source

pub unsafe fn from_mut_slice_unchecked(slice: &mut [f32]) -> &mut Self

Forms a mutable reference to a Vec4 from a slice of f32 that is assumed to contain two values.

§Parameters
  • slice - the mutable slice of f32s.
§Safety

slice.len() must be equal to 2.

source

pub const unsafe fn from_ptr_unchecked<'a>(ptr: *const f32) -> &'a Vec4

Forms a reference to a Vec2 from a pointer to a contiguous sequence of at least two f32s.

§Parameters
  • ptr - the pointer to a sequence of f32 values
§Safety

ptr must point to an allocated object that references at least two entries

source§

impl Vec4

source

pub unsafe fn from_mut_ptr_unchecked<'a>(ptr: *mut f32) -> &'a mut Vec4

Forms a mutable reference to a Vec3 from a pointer to a contiguous sequence of at least two f32s.

§Parameters
  • ptr - the pointer to a sequence of f32 values
§Safety

ptr must point to an allocated object that references at least two entries

source

pub const fn as_slice(&self) -> &[f32]

Returns this Vec4 as a slice of f32.

source

pub fn as_mut_slice(&mut self) -> &mut [f32]

Returns this Vec4 as a mutable slice of f32.

source

pub const fn as_ptr(&self) -> *const f32

Returns this vector as a ptr of f32.

source

pub fn as_mut_ptr(&mut self) -> *mut f32

Returns this vector as a mutable ptr of f32.

source§

impl Vec4

source

pub fn iter(&self) -> impl Iterator<Item = &f32>

Returns an iterator over the vector.

source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut f32>

Returns a mutable iterator over the vector.

source

pub const fn x(&self) -> f32

Returns the X-coordinate of this 4-component vector.

source

pub const fn y(&self) -> f32

Returns the Y-coordinate of this 4-component vector.

source

pub const fn z(&self) -> f32

Returns the Z-coordinate of this 4-component vector.

source

pub const fn w(&self) -> f32

Returns the W-coordinate of this 4-component vector.

source

pub const fn xy(&self) -> &Vec2

Returns the xy coordinates of this vector as a Vec2.

source

pub const fn yz(&self) -> &Vec2

Returns the yz coordinates of this vector as a Vec2.

source

pub const fn zw(&self) -> &Vec2

Returns the zw coordinates of this vector as a Vec2.

source

pub const fn xyz(&self) -> &Vec3

Returns the xyz coordinates of this vector as a Vec2.

source

pub const fn yzw(&self) -> &Vec3

Returns the yz coordinates of this vector as a Vec2.

source

pub fn x_mut(&mut self) -> &mut f32

Returns a mutable reference to the X-coordinate of this 3-component vector.

source

pub fn y_mut(&mut self) -> &mut f32

Returns a mutable reference to the Y-coordinate of this 3-component vector.

source

pub fn z_mut(&mut self) -> &mut f32

Returns a mutable reference to the Z-coordinate of this 3-component vector.

source

pub fn w_mut(&mut self) -> &mut f32

Returns a mutable reference to the W-coordinate of this 3-component vector.

source

pub fn xy_mut(&mut self) -> &mut Vec2

Returns a mutable reference to the xy coordinates of this vector.

source

pub fn yz_mut(&mut self) -> &mut Vec2

Returns a mutable reference to the yz coordinates of this vector.

source

pub fn zw_mut(&mut self) -> &mut Vec2

Returns a mutable reference to the zw coordinates of this vector.

source

pub fn xyz_mut(&mut self) -> &mut Vec3

Returns a mutable reference to the yz coordinates of this vector.

source

pub fn yzw_mut(&mut self) -> &mut Vec3

Returns a mutable reference to the zw coordinates of this vector.

source

pub fn square_magnitude(&self) -> f32

Computes the square magnitude of this two-component vector.

source

pub fn magnitude(&self) -> f32

Computes the magnitude of this vector.

Where possible, consider using Vec4::square_magnitude as this will avoid the need to compute the square-root.

source

pub fn is_normalized(&self) -> bool

Queries whether this vector is normalized (e.g. has a magnitude of 1).

source

pub fn normalize(&mut self)

Normalizes this vector.

source

pub fn normalized(&self) -> Vector4

Returns normalized vector.

source

pub fn is_finite(&self) -> bool

Returns whether all components of this Vec are finite.

§Example

Basic use:


let finite = Vector4::UNIT_X;
let not_finite = Vector4::new(f32::INFINITY, 3.0, 0.0, -1.0);

assert!(finite.is_finite());
assert!(!not_finite.is_finite());
source

pub fn is_infinite(&self) -> bool

Returns whether any component of this vec are infinite.

This is the inverse operation of is_finite.

§Example

Basic use:


let finite = Vector4::UNIT_X;
let not_finite = Vector4::new(f32::INFINITY, 3.0, 0.0, -1.0);

assert!(!finite.is_infinite());
assert!(not_finite.is_infinite());
source

pub fn is_nan(&self) -> bool

Returns whether any component of this vec are nan.

source

pub fn abs(&self) -> Vector4

Computes the absolute value of self

source

pub fn min(&self, rhs: &Vec4) -> Vector4

Computes the minimum of each vector.

§Parameters
  • lhs - the left vector
  • rhs - the right vector
source

pub fn max(&self, rhs: &Vec4) -> Vector4

Computes the maximum of each vector.

§Parameters
  • lhs - the left vector
  • rhs - the right vector
source§

impl Vec4

source

pub fn set_x(&mut self, x: f32)

Sets the x-component

§Parameters
  • x - the X-component
source

pub fn set_y(&mut self, y: f32)

Sets the y-component

§Parameters
  • y - the Y-component
source

pub fn set_z(&mut self, z: f32)

Sets the z-component

§Parameters
  • z - theZ-component
source

pub fn set_w(&mut self, w: f32)

Sets the w-component

§Parameters
  • w - the W-component
source

pub fn set_xy(&mut self, xy: &Vec2)

Sets the X and Y components of this vector

§Parameters
  • xy - the X and Y components of the Vec4
source

pub fn set_yz(&mut self, yz: &Vec2)

Sets the Y and Z components of this vector

§Parameters
  • yz - the Y and Z components of the Vec4
source

pub fn set_zw(&mut self, zw: &Vec2)

Sets the Z and W components of this vector

§Parameters
  • zw - the Z and W components of the Vec4
source

pub fn set_xyz(&mut self, xyz: &Vec3)

Sets the X, Y, and Z components of this vector

§Parameters
  • xyz - the X, Y, and Z components of the Vec4`
source

pub fn set_yzw(&mut self, yzw: &Vec3)

Sets the Y, Z and W components of this vector

§Parameters
  • yzw - the Y, Z, and W components of the Vec4
source

pub fn set(&mut self, other: &Vec4)

Sets all the components of this vector the values from other.

§Parameters
  • other - the other Vec3 to set.

Trait Implementations§

source§

impl Add<&Vec4> for &Vector4

source§

type Output = Vector4

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Vec4) -> Self::Output

Performs the + operation. Read more
source§

impl Add<&Vec4> for Vector4

source§

type Output = Vector4

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Vec4) -> Self::Output

Performs the + operation. Read more
source§

impl Add<&Vector4> for &Vec4

source§

type Output = Vector4

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Vector4) -> Self::Output

Performs the + operation. Read more
source§

impl Add<Vector4> for &Vec4

source§

type Output = Vector4

The resulting type after applying the + operator.
source§

fn add(self, rhs: Vector4) -> Self::Output

Performs the + operation. Read more
source§

impl Add for &Vec4

source§

type Output = Vector4

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<&Vec4> for Vec4

source§

fn add_assign(&mut self, rhs: &Vec4)

Performs the += operation. Read more
source§

impl AddAssign<&Vec4> for Vector4

source§

fn add_assign(&mut self, rhs: &Vec4)

Performs the += operation. Read more
source§

impl AddAssign<&Vector4> for Vec4

source§

fn add_assign(&mut self, rhs: &Vector4)

Performs the += operation. Read more
source§

impl AddAssign<Vector4> for Vec4

source§

fn add_assign(&mut self, rhs: Vector4)

Performs the += operation. Read more
source§

impl AlmostEq for Vec4

source§

fn almost_eq(&self, other: &Self) -> bool

Tests whether self is almost equal to other Read more
source§

impl AsMut<[f32]> for Vec4

source§

fn as_mut(&mut self) -> &mut [f32]

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsMut<Vec4> for Color

source§

fn as_mut(&mut self) -> &mut Vec4

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsMut<Vec4> for Plane

source§

fn as_mut(&mut self) -> &mut Vec4

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsMut<Vec4> for Vector4

source§

fn as_mut(&mut self) -> &mut Vec4

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl AsRef<[f32]> for Vec4

source§

fn as_ref(&self) -> &[f32]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<Vec4> for Color

source§

fn as_ref(&self) -> &Vec4

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<Vec4> for Plane

source§

fn as_ref(&self) -> &Vec4

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<Vec4> for Vector4

source§

fn as_ref(&self) -> &Vec4

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<[f32]> for Vec4

source§

fn borrow(&self) -> &[f32]

Immutably borrows from an owned value. Read more
source§

impl Borrow<Vec4> for Color

source§

fn borrow(&self) -> &Vec4

Immutably borrows from an owned value. Read more
source§

impl Borrow<Vec4> for Vector4

source§

fn borrow(&self) -> &Vec4

Immutably borrows from an owned value. Read more
source§

impl BorrowMut<[f32]> for Vec4

source§

fn borrow_mut(&mut self) -> &mut [f32]

Mutably borrows from an owned value. Read more
source§

impl BorrowMut<Vec4> for Color

source§

fn borrow_mut(&mut self) -> &mut Vec4

Mutably borrows from an owned value. Read more
source§

impl BorrowMut<Vec4> for Vector4

source§

fn borrow_mut(&mut self) -> &mut Vec4

Mutably borrows from an owned value. Read more
source§

impl Cross for Vec4

source§

type Output = Vector4

The output type of the Cross operation
source§

fn cross(&self, other: &Self) -> Self::Output

Computes the cross-product of two values, and returns the result. Read more
source§

impl Debug for Vec4

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Vec4

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Div<f32> for &Vec4

source§

type Output = Vector4

The resulting type after applying the / operator.
source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
source§

impl DivAssign<f32> for Vec4

source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
source§

impl Dot<Col4> for Vec4

source§

type Output = f32

The output of this operation. Read more
source§

fn dot(&self, other: &Col4) -> Self::Output

Performs the dot-product between two values, and returns the output value. Read more
source§

impl Dot<Quaternion> for Vec4

source§

type Output = f32

The output of this operation. Read more
source§

fn dot(&self, other: &Quaternion) -> Self::Output

Performs the dot-product between two values, and returns the output value. Read more
source§

impl Dot<Vec4> for Col4

source§

type Output = f32

The output of this operation. Read more
source§

fn dot(&self, other: &Vec4) -> Self::Output

Performs the dot-product between two values, and returns the output value. Read more
source§

impl Dot<Vec4> for Quaternion

source§

type Output = f32

The output of this operation. Read more
source§

fn dot(&self, other: &Vec4) -> Self::Output

Performs the dot-product between two values, and returns the output value. Read more
source§

impl Dot for Vec4

source§

type Output = f32

The output of this operation. Read more
source§

fn dot(&self, other: &Self) -> Self::Output

Performs the dot-product between two values, and returns the output value. Read more
source§

impl From<&Vec4> for Color

source§

fn from(vec: &Vec4) -> Self

Converts to this type from the input type.
source§

impl From<&Vec4> for Quaternion

source§

fn from(vec: &Vec4) -> Self

Converts to this type from the input type.
source§

impl From<&Vec4> for Vector4

source§

fn from(value: &Vec4) -> Self

Converts to this type from the input type.
source§

impl<I> Index<I> for Vec4
where I: SliceIndex<[f32]>,

source§

type Output = <I as SliceIndex<[f32]>>::Output

The returned type after indexing.
source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<I> IndexMut<I> for Vec4
where I: SliceIndex<[f32]>,

source§

fn index_mut(&mut self, index: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl Lerp<Vec4> for Vector4

source§

type Output = Vector4

The output of this operation.
source§

fn lerp(&self, rhs: &Vec4, alpha: f32) -> Self::Output

Interpolates between two different states Read more
source§

impl Lerp<Vector4> for Vec4

source§

type Output = Vector4

The output of this operation.
source§

fn lerp(&self, rhs: &Vector4, alpha: f32) -> Self::Output

Interpolates between two different states Read more
source§

impl Lerp for Vec4

source§

type Output = Vector4

The output of this operation.
source§

fn lerp(&self, other: &Self, alpha: f32) -> Self::Output

Interpolates between two different states Read more
source§

impl Midpoint for Vec4

source§

type Output = Vector4

The output type
source§

fn midpoint(&self, other: &Self) -> Self::Output

Computes the midpoint of this type, returning the result Read more
source§

impl Mul<&Vec4> for &Mat4

source§

type Output = Vector4

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Vec4) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<&Vec4> for f32

source§

type Output = Vector4

The resulting type after applying the * operator.
source§

fn mul(self, rhs: &Vec4) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<f32> for &Vec4

source§

type Output = Vector4

The resulting type after applying the * operator.
source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
source§

impl MulAssign<f32> for Vec4

source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
source§

impl Near for Vec4

source§

fn near(&self, other: &Self, tolerance: &f32) -> bool

Tests whether self is “near” other relative top the specified tolerance Read more
source§

impl Neg for &Vec4

source§

type Output = Vector4

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl PartialEq for Vec4

source§

fn eq(&self, other: &Vec4) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Vec4

source§

fn partial_cmp(&self, other: &Vec4) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Rem<f32> for &Vec4

source§

type Output = Vector4

The resulting type after applying the % operator.
source§

fn rem(self, rhs: f32) -> Self::Output

Performs the % operation. Read more
source§

impl RemAssign<f32> for Vec4

source§

fn rem_assign(&mut self, rhs: f32)

Performs the %= operation. Read more
source§

impl Sub<&Vec4> for &Vector4

source§

type Output = Vector4

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Vec4) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<&Vec4> for Vector4

source§

type Output = Vector4

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Vec4) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<&Vector4> for &Vec4

source§

type Output = Vector4

The resulting type after applying the - operator.
source§

fn sub(self, rhs: &Vector4) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<Vector4> for &Vec4

source§

type Output = Vector4

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Vector4) -> Self::Output

Performs the - operation. Read more
source§

impl Sub for &Vec4

source§

type Output = Vector4

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<&Vec4> for Vec4

source§

fn sub_assign(&mut self, rhs: &Vec4)

Performs the -= operation. Read more
source§

impl SubAssign<&Vec4> for Vector4

source§

fn sub_assign(&mut self, rhs: &Vec4)

Performs the -= operation. Read more
source§

impl SubAssign<&Vector4> for Vec4

source§

fn sub_assign(&mut self, rhs: &Vector4)

Performs the -= operation. Read more
source§

impl SubAssign<Vector4> for Vec4

source§

fn sub_assign(&mut self, rhs: Vector4)

Performs the -= operation. Read more
source§

impl ToOwned for Vec4

source§

type Owned = Vector4

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> Self::Owned

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl StructuralPartialEq for Vec4

Auto Trait Implementations§

§

impl Freeze for Vec4

§

impl RefUnwindSafe for Vec4

§

impl Send for Vec4

§

impl !Sized for Vec4

§

impl Sync for Vec4

§

impl Unpin for Vec4

§

impl UnwindSafe for Vec4

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more