packages

spatial::Vec3

On this page

Vec3Source

pub struct Vec3<T = f32>

A 3-component vector. Vec3 alone is Vec3<f32>; DVec3, IVec3 and UVec3 are the f64, i32 and u32 forms. #[repr(C)]: three Ts in order, no padding; Vec3 (f32) is 12 bytes, aligned to 4. A GPU uniform or storage buffer aligns a vec3 to 16 bytes: use Vec3A there, or follow each Vec3 with a scalar.

nabi
use spatial::Vec3;

let v = Vec3::new(1.0, 2.0, 3.0);
assert(v.x == 1.0 && v.y == 2.0);
assert(Vec3::<f32>::size() == 12);
  • pub x: T§

    Source
  • pub y: T§

    Source
  • pub z: T§

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

    Source

    The vector of its components.

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

    Source

    Every component v.

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

    Source

    All zero.

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

    Source

    All one.

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

    Source

    The unit vector along x.

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

    Source

    The unit vector along y.

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

    Source

    The unit vector along z.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::<f32>::unit_z().z == 1.0);
    
  • pub const fn from_array(a: [T; 3]) -> Vec3<T>§

    Source

    From [x, y, z].

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

    Source

    [x, y, z].

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

    Source

    The dot product.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).dot(Vec3::new(4.0, 5.0, 6.0)) == 32.0);
    
  • pub fn length_squared(self) -> T§

    Source

    self.dot(self).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).length_squared() == Vec3::new(1.0, 2.0, 3.0).dot(Vec3::new(1.0, 2.0, 3.0)));
    
  • pub fn distance_squared(self, o: Vec3<T>) -> T§

    Source

    The squared distance to o.

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

    Source

    The smaller of each pair of components.

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

    Source

    The larger of each pair of components.

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

    Source

    Each component limited to lo..=hi.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).clamp(Vec3::splat(1.5), Vec3::splat(1.5)) == Vec3::splat(1.5));
    
  • pub fn min_element(self) -> T§

    Source

    The smallest component.

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

    Source

    The largest component.

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

    Source

    The sum of the components.

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

    Source

    The product of the components.

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

    Source

    Each component through f.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).map(|c: f64| -> f64 { c * 2.0 }) == Vec3::new(1.0, 2.0, 3.0) * 2.0);
    
  • pub const fn xy(self) -> Vec2<T>§

    Source

    (x, y).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).xy().x == 1.0);
    
  • pub const fn xz(self) -> Vec2<T>§

    Source

    (x, z).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).xz().x == 1.0);
    
  • pub const fn yz(self) -> Vec2<T>§

    Source

    (y, z).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).yz().x == 2.0);
    
  • pub const fn yx(self) -> Vec2<T>§

    Source

    (y, x).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).yx().x == 2.0);
    
  • pub const fn zx(self) -> Vec2<T>§

    Source

    (z, x).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).zx().x == 3.0);
    
  • pub const fn zy(self) -> Vec2<T>§

    Source

    (z, y).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).zy().x == 3.0);
    
  • pub const fn zyx(self) -> Vec3<T>§

    Source

    (z, y, x).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).zyx().x == 3.0);
    
  • pub const fn xzy(self) -> Vec3<T>§

    Source

    (x, z, y).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).xzy().x == 1.0);
    
  • pub const fn yxz(self) -> Vec3<T>§

    Source

    (y, x, z).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).yxz().x == 2.0);
    
  • pub const fn yzx(self) -> Vec3<T>§

    Source

    (y, z, x).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).yzx().x == 2.0);
    
  • pub const fn zxy(self) -> Vec3<T>§

    Source

    (z, x, y).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).zxy().x == 3.0);
    
  • pub const fn extend(self, w: T) -> Vec4<T>§

    Source

    (x, y, z, w).

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).extend(1.0).w == 1.0);
    
  • pub fn length(self) -> T§

    Source

    The length.

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

    Source

    1 / length.

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

    Source

    The distance to o.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::<f32>::zero().distance(Vec3::new(3.0, 4.0, 0.0)) == 5.0);
    
  • pub fn normalize(self) -> Vec3<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::Vec3;
    
    assert(Vec3::new(0.0, 2.0, 0.0).normalize() == Vec3::new(0.0, 1.0, 0.0));
    
  • pub fn try_normalize(self) -> Vec3<T> | none§

    Source

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

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

    Source

    The unit vector, or zero where try_normalize gives none.

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

    Source

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

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

    Source

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

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

    Source

    The midpoint of self and o.

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

    Source

    Whether every component is within eps of o's.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::new(1.0, 2.0, 3.0).approx_eq(Vec3::new(1.0, 2.0, 3.0) + Vec3::splat(1e-7), 1e-6));
    
  • pub fn abs(self) -> Vec3<T>§

    Source

    Each component's absolute value.

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

    Source

    Each component rounded down.

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

    Source

    Each component rounded up.

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

    Source

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

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

    Source

    1 / c of each component.

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

    Source

    Whether no component is NaN or infinite.

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

    Source

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

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

    Source

    The part of self at right angles to from.

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

    Source

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

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

    Source

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

    nabi
    use spatial::Vec3;
    
    assert((Vec3::<f64>::unit_x().angle_between(Vec3::unit_y()) - f64::PI / 2.0).abs() < 1e-12);
    
  • pub fn cross(self, o: Vec3<T>) -> Vec3<T>§

    Source

    The cross product, right-handed: x.cross(y) == z.

    nabi
    use spatial::Vec3;
    
    assert(Vec3::<f32>::unit_x().cross(Vec3::unit_y()) == Vec3::unit_z());
    
  • pub fn any_orthonormal(self) -> Vec3<T>§

    Source

    A unit vector at right angles to the unit vector self.

    nabi
    use spatial::Vec3;
    
    let v = Vec3::<f32>::unit_z();
    assert(v.any_orthonormal().dot(v) == 0.0);
    
Trait implementations 19
  • impl<T: Scalar> Add for Vec3<T>
  • impl<T: Scalar> Add<T> for Vec3<T>
  • impl<T: Scalar> AddAssign for Vec3<T>
  • impl<T: Scalar> AddAssign<T> for Vec3<T>
  • impl<T: Scalar> Sub for Vec3<T>
  • impl<T: Scalar> Sub<T> for Vec3<T>
  • impl<T: Scalar> SubAssign for Vec3<T>
  • impl<T: Scalar> SubAssign<T> for Vec3<T>
  • impl<T: Scalar> Mul for Vec3<T>
  • impl<T: Scalar> Mul<T> for Vec3<T>
  • impl<T: Scalar> MulAssign for Vec3<T>
  • impl<T: Scalar> MulAssign<T> for Vec3<T>
  • impl<T: Scalar> Div for Vec3<T>
  • impl<T: Scalar> Div<T> for Vec3<T>
  • impl<T: Scalar> DivAssign for Vec3<T>
  • impl<T: Scalar> DivAssign<T> for Vec3<T>
  • impl<T: Scalar + Neg<Output = T>> Neg for Vec3<T>
  • impl<T: Scalar> Index<usize> for Vec3<T>
  • impl<T: Scalar> Display for Vec3<T>