1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
use super::Vec4; #[inline] pub fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 { Vec4::new(x, y, z, w) } impl Vec4 { /// Returns a new `Vec4` with elements representing the sign of `self`. /// /// - `1.0` if the number is positive, `+0.0` or `INFINITY` /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` #[inline] pub fn sign(self) -> Self { let mask = self.cmpge(Self::zero()); mask.select(Self::splat(1.0), Self::splat(-1.0)) } /// Computes the reciprocal `1.0/n` of each element, returning the /// results in a new `Vec4`. #[inline] pub fn reciprocal(self) -> Self { // TODO: Optimize Self::one() / self } /// Performs a linear interpolation between `self` and `other` based on /// the value `s`. /// /// When `s` is `0.0`, the result will be equal to `self`. When `s` /// is `1.0`, the result will be equal to `other`. #[inline] pub fn lerp(self, other: Self, s: f32) -> Self { self + ((other - self) * s) } /// Returns whether `self` is length `1.0` or not. /// /// Uses a precision threshold of `1e-6`. #[inline] pub fn is_normalized(self) -> bool { is_normalized!(self) } /// Returns true if the absolute difference of all elements between `self` /// and `other` is less than or equal to `max_abs_diff`. /// /// This can be used to compare if two `Vec4`'s contain similar elements. It /// works best when comparing with a known value. The `max_abs_diff` that /// should be used used depends on the values being compared against. /// /// For more on floating point comparisons see /// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ #[inline] pub fn abs_diff_eq(self, other: Self, max_abs_diff: f32) -> bool { abs_diff_eq!(self, other, max_abs_diff) } } impl AsRef<[f32; 4]> for Vec4 { #[inline] fn as_ref(&self) -> &[f32; 4] { unsafe { &*(self as *const Self as *const [f32; 4]) } } } impl AsMut<[f32; 4]> for Vec4 { #[inline] fn as_mut(&mut self) -> &mut [f32; 4] { unsafe { &mut *(self as *mut Self as *mut [f32; 4]) } } }