spatial::Vec3
On this page
54absangle_betweenany_orthonormalapprox_eqceilclampcrossdistancedistance_squareddotelement_productelement_sumextendfloorfrom_arrayis_finiteis_normalizedlengthlength_reciplength_squaredlerpmapmaxmax_elementmidpointminmin_elementnewnormalizenormalize_or_zerooneproject_ontorecipreflectreject_fromroundsplatto_arraytry_normalizeunit_xunit_yunit_zxyxzxzyyxyxzyzyzxzerozxzxyzyzyx
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.
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);
The vector of its components.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).z == 3.0);Every component
v.nabiuse spatial::Vec3; assert(Vec3::splat(2.0) == Vec3::new(2.0, 2.0, 2.0));All zero.
nabiuse spatial::Vec3; assert(Vec3::<f32>::zero() == Vec3::splat(0.0));All one.
nabiuse spatial::Vec3; assert(Vec3::<f32>::one() == Vec3::splat(1.0));The unit vector along
x.nabiuse spatial::Vec3; assert(Vec3::<f32>::unit_x().x == 1.0);The unit vector along
y.nabiuse spatial::Vec3; assert(Vec3::<f32>::unit_y().y == 1.0);The unit vector along
z.nabiuse spatial::Vec3; assert(Vec3::<f32>::unit_z().z == 1.0);From
[x, y, z].nabiuse spatial::Vec3; assert(Vec3::from_array([1.0, 2.0, 3.0]) == Vec3::new(1.0, 2.0, 3.0));[x, y, z].nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).to_array() == [1.0, 2.0, 3.0]);The dot product.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).dot(Vec3::new(4.0, 5.0, 6.0)) == 32.0);self.dot(self).nabiuse 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)));The squared distance to
o.nabiuse spatial::Vec3; assert(Vec3::<f32>::zero().distance_squared(Vec3::splat(1.0)) == 3.0);The smaller of each pair of components.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).min(Vec3::splat(1.5)).z == 1.5);The larger of each pair of components.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).max(Vec3::splat(1.5)).x == 1.5);Each component limited to
lo..=hi.nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).clamp(Vec3::splat(1.5), Vec3::splat(1.5)) == Vec3::splat(1.5));The smallest component.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).min_element() == 1.0);The largest component.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).max_element() == 3.0);The sum of the components.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).element_sum() == 6.0);The product of the components.
nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).element_product() == 6.0);Each component through
f.nabiuse 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);(x, y).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).xy().x == 1.0);(x, z).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).xz().x == 1.0);(y, z).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).yz().x == 2.0);(y, x).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).yx().x == 2.0);(z, x).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).zx().x == 3.0);(z, y).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).zy().x == 3.0);(z, y, x).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).zyx().x == 3.0);(x, z, y).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).xzy().x == 1.0);(y, x, z).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).yxz().x == 2.0);(y, z, x).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).yzx().x == 2.0);(z, x, y).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).zxy().x == 3.0);(x, y, z, w).nabiuse spatial::Vec3; assert(Vec3::new(1.0, 2.0, 3.0).extend(1.0).w == 1.0);The length.
nabiuse spatial::Vec3; assert(Vec3::new(3.0, 4.0, 0.0).length() == 5.0);1 / length.nabiuse spatial::Vec3; assert(Vec3::new(0.0, 4.0, 0.0).length_recip() == 0.25);The distance to
o.nabiuse spatial::Vec3; assert(Vec3::<f32>::zero().distance(Vec3::new(3.0, 4.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::Vec3; assert(Vec3::new(0.0, 2.0, 0.0).normalize() == Vec3::new(0.0, 1.0, 0.0));The unit vector, or
nonefor a zero, tiny or non-finite one.nabiuse spatial::Vec3; assert(Vec3::<f32>::zero().try_normalize() == none);The unit vector, or zero where
try_normalizegivesnone.nabiuse spatial::Vec3; assert(Vec3::<f32>::zero().normalize_or_zero() == Vec3::zero());Whether the length is 1, within
1e-4of the squared length.nabiuse spatial::Vec3; assert(Vec3::<f32>::unit_x().is_normalized());self + (o - self) * t:t = 0givesself,t = 1giveso.nabiuse spatial::Vec3; assert(Vec3::<f32>::zero().lerp(Vec3::splat(2.0), 0.5) == Vec3::one());The midpoint of
selfando.nabiuse spatial::Vec3; assert(Vec3::<f32>::zero().midpoint(Vec3::splat(2.0)) == Vec3::one());Whether every component is within
epsofo's.nabiuse 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));Each component's absolute value.
nabiuse spatial::Vec3; assert((-Vec3::<f32>::one()).abs() == Vec3::one());Each component rounded down.
nabiuse spatial::Vec3; assert(Vec3::splat(1.5).floor() == Vec3::one());Each component rounded up.
nabiuse spatial::Vec3; assert(Vec3::splat(0.5).ceil() == Vec3::one());Each component rounded to the nearest integer, half away from zero.
nabiuse spatial::Vec3; assert(Vec3::splat(0.5).round() == Vec3::one());1 / cof each component.nabiuse spatial::Vec3; assert(Vec3::splat(4.0).recip() == Vec3::splat(0.25));Whether no component is NaN or infinite.
nabiuse spatial::Vec3; assert(!Vec3::splat(1.0 / 0.0).is_finite());The part of
selfalongonto(which need not be unit length).nabiuse spatial::Vec3; assert(Vec3::splat(2.0).project_onto(Vec3::unit_x()) == Vec3::unit_x() * 2.0);The part of
selfat right angles tofrom.nabiuse spatial::Vec3; assert(Vec3::splat(2.0).reject_from(Vec3::unit_x()).x == 0.0);selfmirrored by the plane through 0 with unit normaln.nabiuse spatial::Vec3; assert(Vec3::splat(1.0).reflect(Vec3::unit_x()).x == -1.0);The angle to
oin radians, in0..=pi.nabiuse spatial::Vec3; assert((Vec3::<f64>::unit_x().angle_between(Vec3::unit_y()) - f64::PI / 2.0).abs() < 1e-12);The cross product, right-handed:
x.cross(y) == z.nabiuse spatial::Vec3; assert(Vec3::<f32>::unit_x().cross(Vec3::unit_y()) == Vec3::unit_z());A unit vector at right angles to the unit vector
self.nabiuse 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>