Skip to main content

Vec4

Struct Vec4 

Source
pub struct Vec4 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}
Expand description

A 4D vector of f32 components.

Useful for homogeneous coordinates (w=1 for a point, w=0 for a direction), RGBA colors, and quaternions (x, y, z, w).

Fields§

§x: f32

X component.

§y: f32

Y component.

§z: f32

Z component.

§w: f32

W component.

Implementations§

Source§

impl Vec4

Source

pub const ZERO: Self

Zero vector.

Source

pub const ONE: Self

Vector whose components are all 1.0.

Source

pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self

Creates a new vector.

Source

pub const fn as_array(&self) -> [f32; 4]

Returns the components as an array.

Source

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

Creates a vector from an array.

Source

pub fn dot(&self, rhs: &Self) -> f32

Dot product.

Source

pub fn norm_sq(&self) -> f32

Squared norm. Infallible, but may be inf or 0 for extreme components; use Self::norm in those cases.

Source

pub fn norm(&self) -> Result<f32, VecError>

Euclidean norm (robust for very large or very small values).

Returns VecError::NonFiniteValue if a component is NaN or infinite, or if the norm exceeds f32::MAX. The norm of the zero vector is Ok(0.0).

Source

pub fn normalize(&self) -> Result<Self, VecError>

Unit vector in the same direction.

Returns VecError::ZeroNorm for an exact zero vector and VecError::NonFiniteValue if a component is invalid.

Source

pub fn normalize_or_zero(&self) -> Self

Like Self::normalize, but returns Self::ZERO on failure.

Source

pub fn distance_sq(&self, rhs: &Self) -> f32

Squared distance (avoids the square root).

Source

pub fn distance(&self, rhs: &Self) -> Result<f32, VecError>

Distance euclidienne entre deux vecteurs.

Source

pub fn lerp(&self, rhs: &Self, t: f32) -> Self

Linear interpolation: self + t * (rhs - self).

Source

pub fn hadamard(&self, rhs: &Self) -> Self

Component-wise multiplication (Hadamard product).

Source

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

Component-wise minimum.

Source

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

Component-wise maximum.

Source

pub fn abs(&self) -> Self

Component-wise absolute value.

Source

pub fn checked_div(&self, s: f32) -> Result<Self, VecError>

Divides by a scalar without silently producing inf or NaN.

Returns VecError::DivisionByZero if s == 0.0 and VecError::NonFiniteValue if the result is not finite.

Source

pub fn project_onto(&self, rhs: &Self) -> Result<Self, VecError>

Projection de self sur rhs : (self·rhs / |rhs|²) * rhs.

Scale-independent for both inputs, avoiding overflow in intermediate dot products and squared norms. Returns VecError::ZeroNorm if rhs is zero, or VecError::NonFiniteValue for non-finite inputs or an unrepresentable result.

Source

pub fn reject_from(&self, rhs: &Self) -> Result<Self, VecError>

Component of self perpendicular to rhs: self - proj(self, rhs).

Source

pub fn reflect(&self, normal: &Self) -> Result<Self, VecError>

Reflects self across the plane or line with normal normal.

normal does not need to be a unit vector, but must be nonzero.

Source

pub fn is_finite(&self) -> bool

Returns true if all components are finite.

Source

pub fn is_approx_eq(&self, rhs: &Self, eps: f32) -> bool

Approximate equality: each component differs by at most eps.

Source§

impl Vec4

Source

pub const X: Self

Unit vector along X: [1, 0, 0, 0].

Source

pub const Y: Self

Unit vector along Y: [0, 1, 0, 0].

Source

pub const Z: Self

Unit vector along Z: [0, 0, 1, 0].

Source

pub const W: Self

Unit vector along W: [0, 0, 0, 1].

Source

pub const fn from_vec3(v: Vec3, w: f32) -> Self

Creates a Vec4 from a Vec3 by adding w.

Convention: w=1.0 for a point and w=0.0 for a direction.

Source

pub const fn xyz(&self) -> Vec3

Extracts the XYZ components as a Vec3, ignoring w.

Source

pub fn perspective_divide(&self) -> Result<Vec3, VecError>

Perspective divide: (x/w, y/w, z/w).

Returns VecError::DivisionByZero if w == 0 (a direction or point at infinity), and VecError::NonFiniteValue if the result is not finite.

Trait Implementations§

Source§

impl Add for Vec4

Source§

type Output = Vec4

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign for Vec4

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Vec4

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Vec4

Source§

impl Debug for Vec4

Source§

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

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

impl Default for Vec4

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Div<f32> for Vec4

Source§

type Output = Vec4

The resulting type after applying the / operator.
Source§

fn div(self, s: f32) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Vec4

Source§

fn div_assign(&mut self, s: f32)

Performs the /= operation. Read more
Source§

impl From<Vec4> for [f32; 4]

Source§

fn from(v: Vec4) -> Self

Converts to this type from the input type.
Source§

impl From<[f32; 4]> for Vec4

Source§

fn from(v: [f32; 4]) -> Self

Converts to this type from the input type.
Source§

impl Mul<Vec4> for f32

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vec4) -> Vec4

Performs the * operation. Read more
Source§

impl Mul<f32> for Vec4

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, s: f32) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Vec4

Source§

fn mul_assign(&mut self, s: f32)

Performs the *= operation. Read more
Source§

impl Neg for Vec4

Source§

type Output = Vec4

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Vec4

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Vec4

Source§

impl Sub for Vec4

Source§

type Output = Vec4

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign for Vec4

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Sum for Vec4

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.

Auto Trait Implementations§

§

impl Forget for Vec4

§

impl Freeze for Vec4

§

impl Move for Vec4

§

impl RefUnwindSafe for Vec4

§

impl Send for Vec4

§

impl Sync for Vec4

§

impl Unpin for Vec4

§

impl UnsafeUnpin 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.