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: f32X component.
y: f32Y component.
z: f32Z component.
w: f32W component.
Implementations§
Source§impl Vec4
impl Vec4
Sourcepub const fn from_array(v: [f32; 4]) -> Vec4
pub const fn from_array(v: [f32; 4]) -> Vec4
Creates a vector from an array.
Sourcepub fn norm_sq(&self) -> f32
pub fn norm_sq(&self) -> f32
Squared norm. Infallible, but may be inf or 0 for extreme
components; use Self::norm in those cases.
Sourcepub fn norm(&self) -> Result<f32, VecError>
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).
Sourcepub fn normalize(&self) -> Result<Vec4, VecError>
pub fn normalize(&self) -> Result<Vec4, VecError>
Unit vector in the same direction.
Returns VecError::ZeroNorm for an exact zero vector and
VecError::NonFiniteValue if a component is invalid.
Sourcepub fn normalize_or_zero(&self) -> Vec4
pub fn normalize_or_zero(&self) -> Vec4
Like Self::normalize, but returns Self::ZERO on failure.
Sourcepub fn distance_sq(&self, rhs: &Vec4) -> f32
pub fn distance_sq(&self, rhs: &Vec4) -> f32
Squared distance (avoids the square root).
Sourcepub fn distance(&self, rhs: &Vec4) -> Result<f32, VecError>
pub fn distance(&self, rhs: &Vec4) -> Result<f32, VecError>
Distance euclidienne entre deux vecteurs.
Sourcepub fn checked_div(&self, s: f32) -> Result<Vec4, VecError>
pub fn checked_div(&self, s: f32) -> Result<Vec4, 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.
Sourcepub fn project_onto(&self, rhs: &Vec4) -> Result<Vec4, VecError>
pub fn project_onto(&self, rhs: &Vec4) -> Result<Vec4, 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.
Sourcepub fn reject_from(&self, rhs: &Vec4) -> Result<Vec4, VecError>
pub fn reject_from(&self, rhs: &Vec4) -> Result<Vec4, VecError>
Component of self perpendicular to rhs: self - proj(self, rhs).
Sourcepub fn reflect(&self, normal: &Vec4) -> Result<Vec4, VecError>
pub fn reflect(&self, normal: &Vec4) -> Result<Vec4, VecError>
Reflects self across the plane or line with normal normal.
normal does not need to be a unit vector, but must be nonzero.
Sourcepub fn is_approx_eq(&self, rhs: &Vec4, eps: f32) -> bool
pub fn is_approx_eq(&self, rhs: &Vec4, eps: f32) -> bool
Approximate equality: each component differs by at most eps.
Source§impl Vec4
impl Vec4
Sourcepub const fn from_vec3(v: Vec3, w: f32) -> Vec4
pub const fn from_vec3(v: Vec3, w: f32) -> Vec4
Creates a Vec4 from a Vec3 by adding w.
Convention: w=1.0 for a point and w=0.0 for a direction.
Sourcepub fn perspective_divide(&self) -> Result<Vec3, VecError>
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 AddAssign for Vec4
impl AddAssign for Vec4
Source§fn add_assign(&mut self, rhs: Vec4)
fn add_assign(&mut self, rhs: Vec4)
+= operation. Read moreimpl Copy for Vec4
Source§impl DivAssign<f32> for Vec4
impl DivAssign<f32> for Vec4
Source§fn div_assign(&mut self, s: f32)
fn div_assign(&mut self, s: f32)
/= operation. Read moreSource§impl MulAssign<f32> for Vec4
impl MulAssign<f32> for Vec4
Source§fn mul_assign(&mut self, s: f32)
fn mul_assign(&mut self, s: f32)
*= operation. Read more