packages

spatial::Vec4

On this page

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.

nabi
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);
  • pub x: T§

    Source
  • pub y: T§

    Source
  • pub z: T§

    Source
  • pub w: T§

    Source
  • pub const fn new(x: T, y: T, z: T, w: T) -> Vec4<T>§

    Source

    The vector of its components.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).w == 4.0);
    
  • pub const fn splat(v: T) -> Vec4<T>§

    Source

    Every component v.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(2.0) == Vec4::new(2.0, 2.0, 2.0, 2.0));
    
  • pub fn zero() -> Vec4<T>§

    Source

    All zero.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero() == Vec4::splat(0.0));
    
  • pub fn one() -> Vec4<T>§

    Source

    All one.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::one() == Vec4::splat(1.0));
    
  • pub fn unit_x() -> Vec4<T>§

    Source

    The unit vector along x.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::unit_x().x == 1.0);
    
  • pub fn unit_y() -> Vec4<T>§

    Source

    The unit vector along y.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::unit_y().y == 1.0);
    
  • pub fn unit_z() -> Vec4<T>§

    Source

    The unit vector along z.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::unit_z().z == 1.0);
    
  • pub fn unit_w() -> Vec4<T>§

    Source

    The unit vector along w.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::unit_w().w == 1.0);
    
  • pub const fn from_array(a: [T; 4]) -> Vec4<T>§

    Source

    From [x, y, z, w].

    nabi
    use spatial::Vec4;
    
    assert(Vec4::from_array([1.0, 2.0, 3.0, 4.0]) == Vec4::new(1.0, 2.0, 3.0, 4.0));
    
  • pub const fn to_array(self) -> [T; 4]§

    Source

    [x, y, z, w].

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).to_array() == [1.0, 2.0, 3.0, 4.0]);
    
  • pub fn dot(self, o: Vec4<T>) -> T§

    Source

    The dot product.

    nabi
    use 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);
    
  • pub fn length_squared(self) -> T§

    Source

    self.dot(self).

    nabi
    use 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)));
    
  • pub fn distance_squared(self, o: Vec4<T>) -> T§

    Source

    The squared distance to o.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero().distance_squared(Vec4::splat(1.0)) == 4.0);
    
  • pub fn min(self, o: Vec4<T>) -> Vec4<T>§

    Source

    The smaller of each pair of components.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).min(Vec4::splat(1.5)).w == 1.5);
    
  • pub fn max(self, o: Vec4<T>) -> Vec4<T>§

    Source

    The larger of each pair of components.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).max(Vec4::splat(1.5)).x == 1.5);
    
  • pub fn clamp(self, lo: Vec4<T>, hi: Vec4<T>) -> Vec4<T>§

    Source

    Each component limited to lo..=hi.

    nabi
    use 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));
    
  • pub fn min_element(self) -> T§

    Source

    The smallest component.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).min_element() == 1.0);
    
  • pub fn max_element(self) -> T§

    Source

    The largest component.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).max_element() == 4.0);
    
  • pub fn element_sum(self) -> T§

    Source

    The sum of the components.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).element_sum() == 10.0);
    
  • pub fn element_product(self) -> T§

    Source

    The product of the components.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).element_product() == 24.0);
    
  • pub fn map<U>(self, f: fn(T) -> U) -> Vec4<U>§

    Source

    Each component through f.

    nabi
    use 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);
    
  • pub fn simd(self) -> Simd<T, 4>§

    Source

    The four lanes as a Simd<T, 4>.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).simd().reduce_sum() == 10.0);
    
  • pub fn from_simd(s: Simd<T, 4>) -> Vec4<T>§

    Source

    The vector of four lanes.

    nabi
    use spatial::Vec4;
    use core::simd::Simd;
    
    assert(Vec4::from_simd(Simd::<f32, 4>::splat(1.0)) == Vec4::one());
    
  • pub const fn xy(self) -> Vec2<T>§

    Source

    (x, y).

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).xy().y == 2.0);
    
  • pub const fn xyz(self) -> Vec3<T>§

    Source

    (x, y, z), dropping w.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).xyz().z == 3.0);
    
  • pub const fn wzyx(self) -> Vec4<T>§

    Source

    (w, z, y, x).

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(1.0, 2.0, 3.0, 4.0).wzyx().x == 4.0);
    
  • pub fn length(self) -> T§

    Source

    The length.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(3.0, 4.0, 0.0, 0.0).length() == 5.0);
    
  • pub fn length_recip(self) -> T§

    Source

    1 / length.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(0.0, 4.0, 0.0, 0.0).length_recip() == 0.25);
    
  • pub fn distance(self, o: Vec4<T>) -> T§

    Source

    The distance to o.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero().distance(Vec4::new(3.0, 4.0, 0.0, 0.0)) == 5.0);
    
  • pub fn normalize(self) -> Vec4<T>§

    Source

    The vector of length 1 in the same direction. A zero vector gives NaN components; try_normalize or normalize_or_zero handle it.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::new(0.0, 2.0, 0.0, 0.0).normalize() == Vec4::new(0.0, 1.0, 0.0, 0.0));
    
  • pub fn try_normalize(self) -> Vec4<T> | none§

    Source

    The unit vector, or none for a zero, tiny or non-finite one.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero().try_normalize() == none);
    
  • pub fn normalize_or_zero(self) -> Vec4<T>§

    Source

    The unit vector, or zero where try_normalize gives none.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero().normalize_or_zero() == Vec4::zero());
    
  • pub fn is_normalized(self) -> bool§

    Source

    Whether the length is 1, within 1e-4 of the squared length.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::unit_x().is_normalized());
    
  • pub fn lerp(self, o: Vec4<T>, t: T) -> Vec4<T>§

    Source

    self + (o - self) * t: t = 0 gives self, t = 1 gives o.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero().lerp(Vec4::splat(2.0), 0.5) == Vec4::one());
    
  • pub fn midpoint(self, o: Vec4<T>) -> Vec4<T>§

    Source

    The midpoint of self and o.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::<f32>::zero().midpoint(Vec4::splat(2.0)) == Vec4::one());
    
  • pub fn approx_eq(self, o: Vec4<T>, eps: T) -> bool§

    Source

    Whether every component is within eps of o's.

    nabi
    use 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));
    
  • pub fn abs(self) -> Vec4<T>§

    Source

    Each component's absolute value.

    nabi
    use spatial::Vec4;
    
    assert((-Vec4::<f32>::one()).abs() == Vec4::one());
    
  • pub fn floor(self) -> Vec4<T>§

    Source

    Each component rounded down.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(1.5).floor() == Vec4::one());
    
  • pub fn ceil(self) -> Vec4<T>§

    Source

    Each component rounded up.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(0.5).ceil() == Vec4::one());
    
  • pub fn round(self) -> Vec4<T>§

    Source

    Each component rounded to the nearest integer, half away from zero.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(0.5).round() == Vec4::one());
    
  • pub fn recip(self) -> Vec4<T>§

    Source

    1 / c of each component.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(4.0).recip() == Vec4::splat(0.25));
    
  • pub fn is_finite(self) -> bool§

    Source

    Whether no component is NaN or infinite.

    nabi
    use spatial::Vec4;
    
    assert(!Vec4::splat(1.0 / 0.0).is_finite());
    
  • pub fn project_onto(self, onto: Vec4<T>) -> Vec4<T>§

    Source

    The part of self along onto (which need not be unit length).

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(2.0).project_onto(Vec4::unit_x()) == Vec4::unit_x() * 2.0);
    
  • pub fn reject_from(self, from: Vec4<T>) -> Vec4<T>§

    Source

    The part of self at right angles to from.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(2.0).reject_from(Vec4::unit_x()).x == 0.0);
    
  • pub fn reflect(self, n: Vec4<T>) -> Vec4<T>§

    Source

    self mirrored by the plane through 0 with unit normal n.

    nabi
    use spatial::Vec4;
    
    assert(Vec4::splat(1.0).reflect(Vec4::unit_x()).x == -1.0);
    
  • pub fn angle_between(self, o: Vec4<T>) -> T§

    Source

    The angle to o in radians, in 0..=pi.

    nabi
    use 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>