spatial::Vec4
On this page
46absangle_betweenapprox_eqceilclampdistancedistance_squareddotelement_productelement_sumfloorfrom_arrayfrom_simdis_finiteis_normalizedlengthlength_reciplength_squaredlerpmapmaxmax_elementmidpointminmin_elementnewnormalizenormalize_or_zerooneproject_ontorecipreflectreject_fromroundsimdsplatto_arraytry_normalizeunit_wunit_xunit_yunit_zwzyxxyxyzzero
Vec4Source
pub struct Vec4<T = f32>A 4-component vector. Vec4 alone is Vec4<f32>; DVec4,
IVec4 and UVec4 are the f64, i32 and u32 forms.
#[repr(C)]: four Ts in order, no padding; Vec4 (f32) is 16
bytes, aligned to 4; a GPU aligns vec4<f32> to 16, so start a
buffer struct with it or pad before it.
use spatial::Vec4;
let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
assert(v.x == 1.0 && v.y == 2.0);
assert(Vec4::<f32>::size() == 16);
The vector of its components.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).w == 4.0);Every component
v.nabiuse spatial::Vec4; assert(Vec4::splat(2.0) == Vec4::new(2.0, 2.0, 2.0, 2.0));All zero.
nabiuse spatial::Vec4; assert(Vec4::<f32>::zero() == Vec4::splat(0.0));All one.
nabiuse spatial::Vec4; assert(Vec4::<f32>::one() == Vec4::splat(1.0));The unit vector along
x.nabiuse spatial::Vec4; assert(Vec4::<f32>::unit_x().x == 1.0);The unit vector along
y.nabiuse spatial::Vec4; assert(Vec4::<f32>::unit_y().y == 1.0);The unit vector along
z.nabiuse spatial::Vec4; assert(Vec4::<f32>::unit_z().z == 1.0);The unit vector along
w.nabiuse spatial::Vec4; assert(Vec4::<f32>::unit_w().w == 1.0);From
[x, y, z, w].nabiuse spatial::Vec4; assert(Vec4::from_array([1.0, 2.0, 3.0, 4.0]) == Vec4::new(1.0, 2.0, 3.0, 4.0));[x, y, z, w].nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).to_array() == [1.0, 2.0, 3.0, 4.0]);The dot product.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).dot(Vec4::new(5.0, 6.0, 7.0, 8.0)) == 70.0);self.dot(self).nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).length_squared() == Vec4::new(1.0, 2.0, 3.0, 4.0).dot(Vec4::new(1.0, 2.0, 3.0, 4.0)));The squared distance to
o.nabiuse spatial::Vec4; assert(Vec4::<f32>::zero().distance_squared(Vec4::splat(1.0)) == 4.0);The smaller of each pair of components.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).min(Vec4::splat(1.5)).w == 1.5);The larger of each pair of components.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).max(Vec4::splat(1.5)).x == 1.5);Each component limited to
lo..=hi.nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).clamp(Vec4::splat(1.5), Vec4::splat(1.5)) == Vec4::splat(1.5));The smallest component.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).min_element() == 1.0);The largest component.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).max_element() == 4.0);The sum of the components.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).element_sum() == 10.0);The product of the components.
nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).element_product() == 24.0);Each component through
f.nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).map(|c: f64| -> f64 { c * 2.0 }) == Vec4::new(1.0, 2.0, 3.0, 4.0) * 2.0);The four lanes as a
Simd<T, 4>.nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).simd().reduce_sum() == 10.0);The vector of four lanes.
nabiuse spatial::Vec4; use core::simd::Simd; assert(Vec4::from_simd(Simd::<f32, 4>::splat(1.0)) == Vec4::one());(x, y).nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).xy().y == 2.0);(x, y, z), droppingw.nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).xyz().z == 3.0);(w, z, y, x).nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).wzyx().x == 4.0);The length.
nabiuse spatial::Vec4; assert(Vec4::new(3.0, 4.0, 0.0, 0.0).length() == 5.0);1 / length.nabiuse spatial::Vec4; assert(Vec4::new(0.0, 4.0, 0.0, 0.0).length_recip() == 0.25);The distance to
o.nabiuse spatial::Vec4; assert(Vec4::<f32>::zero().distance(Vec4::new(3.0, 4.0, 0.0, 0.0)) == 5.0);The vector of length 1 in the same direction. A zero vector gives NaN components;
try_normalizeornormalize_or_zerohandle it.nabiuse spatial::Vec4; assert(Vec4::new(0.0, 2.0, 0.0, 0.0).normalize() == Vec4::new(0.0, 1.0, 0.0, 0.0));The unit vector, or
nonefor a zero, tiny or non-finite one.nabiuse spatial::Vec4; assert(Vec4::<f32>::zero().try_normalize() == none);The unit vector, or zero where
try_normalizegivesnone.nabiuse spatial::Vec4; assert(Vec4::<f32>::zero().normalize_or_zero() == Vec4::zero());Whether the length is 1, within
1e-4of the squared length.nabiuse spatial::Vec4; assert(Vec4::<f32>::unit_x().is_normalized());self + (o - self) * t:t = 0givesself,t = 1giveso.nabiuse spatial::Vec4; assert(Vec4::<f32>::zero().lerp(Vec4::splat(2.0), 0.5) == Vec4::one());The midpoint of
selfando.nabiuse spatial::Vec4; assert(Vec4::<f32>::zero().midpoint(Vec4::splat(2.0)) == Vec4::one());Whether every component is within
epsofo's.nabiuse spatial::Vec4; assert(Vec4::new(1.0, 2.0, 3.0, 4.0).approx_eq(Vec4::new(1.0, 2.0, 3.0, 4.0) + Vec4::splat(1e-7), 1e-6));Each component's absolute value.
nabiuse spatial::Vec4; assert((-Vec4::<f32>::one()).abs() == Vec4::one());Each component rounded down.
nabiuse spatial::Vec4; assert(Vec4::splat(1.5).floor() == Vec4::one());Each component rounded up.
nabiuse spatial::Vec4; assert(Vec4::splat(0.5).ceil() == Vec4::one());Each component rounded to the nearest integer, half away from zero.
nabiuse spatial::Vec4; assert(Vec4::splat(0.5).round() == Vec4::one());1 / cof each component.nabiuse spatial::Vec4; assert(Vec4::splat(4.0).recip() == Vec4::splat(0.25));Whether no component is NaN or infinite.
nabiuse spatial::Vec4; assert(!Vec4::splat(1.0 / 0.0).is_finite());The part of
selfalongonto(which need not be unit length).nabiuse spatial::Vec4; assert(Vec4::splat(2.0).project_onto(Vec4::unit_x()) == Vec4::unit_x() * 2.0);The part of
selfat right angles tofrom.nabiuse spatial::Vec4; assert(Vec4::splat(2.0).reject_from(Vec4::unit_x()).x == 0.0);selfmirrored by the plane through 0 with unit normaln.nabiuse spatial::Vec4; assert(Vec4::splat(1.0).reflect(Vec4::unit_x()).x == -1.0);The angle to
oin radians, in0..=pi.nabiuse spatial::Vec4; assert((Vec4::<f64>::unit_x().angle_between(Vec4::unit_y()) - f64::PI / 2.0).abs() < 1e-12);
Trait implementations 19
impl<T: Scalar> Add for Vec4<T>impl<T: Scalar> Add<T> for Vec4<T>impl<T: Scalar> AddAssign for Vec4<T>impl<T: Scalar> AddAssign<T> for Vec4<T>impl<T: Scalar> Sub for Vec4<T>impl<T: Scalar> Sub<T> for Vec4<T>impl<T: Scalar> SubAssign for Vec4<T>impl<T: Scalar> SubAssign<T> for Vec4<T>impl<T: Scalar> Mul for Vec4<T>impl<T: Scalar> Mul<T> for Vec4<T>impl<T: Scalar> MulAssign for Vec4<T>impl<T: Scalar> MulAssign<T> for Vec4<T>impl<T: Scalar> Div for Vec4<T>impl<T: Scalar> Div<T> for Vec4<T>impl<T: Scalar> DivAssign for Vec4<T>impl<T: Scalar> DivAssign<T> for Vec4<T>impl<T: Scalar + Neg<Output = T>> Neg for Vec4<T>impl<T: Scalar> Index<usize> for Vec4<T>impl<T: Scalar> Display for Vec4<T>