#[repr(C)]pub struct Vec4<T> {
pub x: T,
pub y: T,
pub z: T,
pub w: T,
}
Expand description
Vec4 vector.
Fields§
§x: T
§y: T
§z: T
§w: T
Implementations§
Source§impl<T> Vec4<T>
impl<T> Vec4<T>
Sourcepub fn cast<U>(self) -> Vec4<U>where
T: CastTo<U>,
pub fn cast<U>(self) -> Vec4<U>where
T: CastTo<U>,
Casts to a vector of different type with the same dimensions.
Sourcepub fn map<U, F>(self, f: F) -> Vec4<U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> Vec4<U>where
F: FnMut(T) -> U,
Maps a callable over the components.
Source§impl<T: Scalar> Vec4<T>
Operations on vectors of scalars.
impl<T: Scalar> Vec4<T>
Operations on vectors of scalars.
Sourcepub fn sqr(self) -> Vec4<T>
pub fn sqr(self) -> Vec4<T>
Squares the components.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3, y: 4 };
assert_eq!(Vec2(9, 16), this.sqr());
let this = Vec3 { x: 2, y: 3, z: -6 };
assert_eq!(Vec3(4, 9, 36), this.sqr());
Sourcepub fn len_sqr(self) -> T
pub fn len_sqr(self) -> T
Calculates the squared length of the vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3, y: 4 };
assert_eq!(25, this.len_sqr());
let this = Vec3 { x: 2, y: -3, z: 6 };
assert_eq!(49, this.len_sqr());
Sourcepub fn len(self) -> Twhere
T: Float,
pub fn len(self) -> Twhere
T: Float,
Calculates the length of the vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3.0, y: 4.0 };
assert_eq!(5.0, this.len());
let this = Vec3 { x: -2.0, y: 3.0, z: -6.0 };
assert_eq!(7.0, this.len());
Sourcepub fn len_hat(self) -> T
pub fn len_hat(self) -> T
Calculates the manhattan length of the vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 3, y: 4 };
assert_eq!(7, this.len_hat());
let this = Vec3 { x: 2, y: -3, z: -6 };
assert_eq!(11, this.len_hat());
Sourcepub fn distance_sqr(self, to: Vec4<T>) -> T
pub fn distance_sqr(self, to: Vec4<T>) -> T
Calculates the squared euclidean distance to another vector.
use cvmath::Vec2;
let this = Vec2 { x: 1, y: 1 };
let to = Vec2 { x: 2, y: 2 };
assert_eq!(2, this.distance_sqr(to));
Sourcepub fn distance(self, to: Vec4<T>) -> Twhere
T: Float,
pub fn distance(self, to: Vec4<T>) -> Twhere
T: Float,
Calculates the euclidean distance to another vector.
use cvmath::Vec2;
let this = Vec2 { x: 10.0, y: 10.0 };
let to = Vec2 { x: 13.0, y: 14.0 };
assert_eq!(5.0, this.distance(to));
Sourcepub fn distance_hat(self, to: Vec4<T>) -> T
pub fn distance_hat(self, to: Vec4<T>) -> T
Calculates the manhattan distance to another vector.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 1.0, y: 5.0 };
let to = Vec2 { x: 5.0, y: 2.0 };
assert_eq!(7.0, this.distance_hat(to));
let this = Vec3 { x: 1.0, y: 5.0, z: -1.0 };
let to = Vec3 { x: 2.0, y: 3.0, z: 1.0 };
assert_eq!(5.0, this.distance_hat(to));
Sourcepub fn normalize(self) -> Vec4<T>where
T: Float,
pub fn normalize(self) -> Vec4<T>where
T: Float,
Normalizes the vector.
After normalizing the vector has the length 1.0
except the zero vector remains zero.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 3.0, y: -4.0 };
assert_eq!(Vec2(0.6, -0.8), this.normalize());
let this = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
assert_eq!(this, this.normalize());
Sourcepub fn normalize_len(self) -> (Vec4<T>, T)where
T: Float,
pub fn normalize_len(self) -> (Vec4<T>, T)where
T: Float,
Calculates the normalized vector and its length.
After normalizing the vector has the length 1.0
except the zero vector remains zero.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 3.0, y: -4.0 };
assert_eq!((Vec2(0.6, -0.8), 5.0), this.normalize_len());
let this = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
assert_eq!((this, 0.0), this.normalize_len());
Sourcepub fn resize(self, len: T) -> Vec4<T>where
T: Float,
pub fn resize(self, len: T) -> Vec4<T>where
T: Float,
Resizes the vector to the given length.
The zero vector remains zero.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -3.0, y: -4.0 };
assert_eq!(Vec2(-1.5, -2.0), this.resize(2.5));
let this = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
assert_eq!(Vec3(0.0, 0.0, 0.0), this.resize(2.0));
Sourcepub fn project_scalar(self, v: Vec4<T>) -> Twhere
T: Float,
pub fn project_scalar(self, v: Vec4<T>) -> Twhere
T: Float,
Calculates the length of self
projected onto v
.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: 1.0, y: 2.0 };
let v = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(2.2, this.project_scalar(v));
let this = Vec3 { x: 1.0, y: 4.0, z: 0.0 };
let v = Vec3 { x: 4.0, y: 2.0, z: 4.0 };
assert_eq!(2.0, this.project_scalar(v));
Sourcepub fn project(self, v: Vec4<T>) -> Vec4<T>where
T: Float,
pub fn project(self, v: Vec4<T>) -> Vec4<T>where
T: Float,
Projection of self
onto v
.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -5.0, y: -2.5 };
let v = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(Vec2(-3.0, -4.0), this.project(v));
let this = Vec3 { x: -5.0, y: -2.5, z: 0.0 };
let v = Vec3 { x: 3.0, y: 4.0, z: 0.0 };
assert_eq!(Vec3(-3.0, -4.0, 0.0), this.project(v));
Sourcepub fn project_sat(self, v: Vec4<T>) -> Vec4<T>
pub fn project_sat(self, v: Vec4<T>) -> Vec4<T>
Projection of self
onto v
clamped to v
.
use cvmath::Vec2;
let this = Vec2 { x: -5.0, y: -2.5 };
let v = Vec2 { x: 3.0, y: 4.0 };
assert_eq!(Vec2(0.0, 0.0), this.project_sat(v));
Sourcepub fn reflect(self, v: Vec4<T>) -> Vec4<T>where
T: Float,
pub fn reflect(self, v: Vec4<T>) -> Vec4<T>where
T: Float,
Reflects self
around v
.
use cvmath::Vec2;
let this = Vec2 { x: 1.0, y: 3.0 };
let v = Vec2 { x: 4.0, y: 4.0 };
assert_eq!(Vec2(3.0, 1.0), this.reflect(v));
Sourcepub fn dot(self, rhs: Vec4<T>) -> T
pub fn dot(self, rhs: Vec4<T>) -> T
Calculates the dot product.
use cvmath::Vec3;
let lhs = Vec3 { x: 1, y: 2, z: 3 };
let rhs = Vec3 { x: 4, y: -5, z: 6 };
assert_eq!(12, Vec3::dot(lhs, rhs));
Sourcepub fn cos_angle(self, rhs: Vec4<T>) -> Twhere
T: Float,
pub fn cos_angle(self, rhs: Vec4<T>) -> Twhere
T: Float,
Calculates the cosine of the angle between two vectors.
use cvmath::Vec2;
let lhs = Vec2 { x: 1.0, y: 1.0 };
let rhs = Vec2 { x: 1.0, y: 0.0 };
let sqrt_2_div_2 = 1.0 / 2_f32.sqrt(); // √2 ÷ 2
assert_eq!(sqrt_2_div_2, lhs.cos_angle(rhs));
Sourcepub fn angle(self, rhs: Vec4<T>) -> Rad<T>where
T: Float,
pub fn angle(self, rhs: Vec4<T>) -> Rad<T>where
T: Float,
Calculates the angle between two vectors.
use cvmath::{Deg, Vec2};
let lhs = Vec2 { x: 1.0, y: 1.0 };
let rhs = Vec2 { x: 1.0, y: 0.0 };
assert_eq!(Deg(45_f32), lhs.angle(rhs).to_deg());
Sourcepub fn hadd(self) -> T
pub fn hadd(self) -> T
Horizontal adds all components.
use cvmath::{Vec2, Vec3};
let this = Vec2 { x: -2, y: 7 };
assert_eq!(5, this.hadd());
let this = Vec3 { x: 3, y: 4, z: 5 };
assert_eq!(12, this.hadd());
Sourcepub fn abs(self) -> Vec4<T>
pub fn abs(self) -> Vec4<T>
Component-wise absolute value.
use cvmath::Vec2;
let this = Vec2 { x: -3, y: 5 };
assert_eq!(Vec2(3, 5), this.abs());
Sourcepub fn min(self, rhs: Vec4<T>) -> Vec4<T>
pub fn min(self, rhs: Vec4<T>) -> Vec4<T>
Component-wise minimum value.
use cvmath::Vec2;
let lhs = Vec2 { x: -3, y: 5 };
let rhs = Vec2 { x: 0, y: 2 };
assert_eq!(Vec2(-3, 2), lhs.min(rhs));
Sourcepub fn max(self, rhs: Vec4<T>) -> Vec4<T>
pub fn max(self, rhs: Vec4<T>) -> Vec4<T>
Component-wise maximum value.
use cvmath::Vec2;
let lhs = Vec2 { x: -3, y: 5 };
let rhs = Vec2 { x: 0, y: 2 };
assert_eq!(Vec2(0, 5), lhs.max(rhs));
Sourcepub fn mul_add(self, vec: Vec4<T>, scale: T) -> Vec4<T>
pub fn mul_add(self, vec: Vec4<T>, scale: T) -> Vec4<T>
Adds the scaled vector.
Equivalent to self + (vec * scale)
with less rounding errors.
Sourcepub fn slerp(self, rhs: Vec4<T>, t: T) -> Vec4<T>where
T: Float,
pub fn slerp(self, rhs: Vec4<T>, t: T) -> Vec4<T>where
T: Float,
Spherical interpolation between the vectors with constant velocity.
The result is linear interpolation of the angles between the vectors and their lengths.
This is fairly expensive to calculate requiring trigonometric functions. If constant velocity isn’t required, see the less expensive nlerp.
Sourcepub fn nlerp(self, rhs: Vec4<T>, t: T) -> Vec4<T>where
T: Float,
pub fn nlerp(self, rhs: Vec4<T>, t: T) -> Vec4<T>where
T: Float,
Cheap spherical interpolation between the vectors without constant velocity.
Sourcepub fn exp_decay(self, rhs: Vec4<T>, decay: T, dt: T) -> Vec4<T>where
T: Float,
pub fn exp_decay(self, rhs: Vec4<T>, decay: T, dt: T) -> Vec4<T>where
T: Float,
Exponential decay smoothing.
Also known as lerp smoothing. Useful decay values range from approx 1.0 to 25.0, slow to fast.
use cvmath::Vec2;
struct Entity {
pos: Vec2<f32>,
target: Vec2<f32>,
}
impl Entity {
fn update(&mut self, dt: f32) {
// Smoothly move towards the target.
self.pos = self.pos.exp_decay(self.target, 5.0, dt);
}
}
Source§impl<T> Vec4<T>
impl<T> Vec4<T>
Sourcepub fn is_infinite(self) -> Bool4where
T: Float,
pub fn is_infinite(self) -> Bool4where
T: Float,
Creates a mask for infinite components.
Sourcepub fn ne(self, rhs: Vec4<T>) -> Bool4where
T: PartialEq,
pub fn ne(self, rhs: Vec4<T>) -> Bool4where
T: PartialEq,
Creates a mask for inequal components.
Sourcepub fn lt(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
pub fn lt(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
Creates a mask for left-hand side components are less than the right-hand side.
Sourcepub fn le(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
pub fn le(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
Creates a mask for left-hand side components are less than or equal the right-hand side.
Sourcepub fn gt(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
pub fn gt(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
Creates a mask for left-hand side components are greater than the right-hand side.
Sourcepub fn ge(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
pub fn ge(self, rhs: Vec4<T>) -> Bool4where
T: PartialOrd,
Creates a mask for left-hand side components are greater than or equal the right-hand side.
Source§impl Vec4<u16>
impl Vec4<u16>
Source§impl Vec4<u8>
impl Vec4<u8>
Sourcepub const fn unpack8(v: u32) -> Vec4<u8>
pub const fn unpack8(v: u32) -> Vec4<u8>
Unpacks u32
into u8 u8 u8 u8
.
use cvmath::Vec4;
assert_eq!(
Vec4 { x: 0x01, y: 0x56, z: 0x9A, w: 0xFE },
Vec4::unpack8(0xFE_9A_56_01)
);
Unpacks an RGBA color value into Vec4<f32>
where x
: red, y
: green, z
: blue and w
: alpha.
use cvmath::Vec4;
// 0xAABBGGRR in little endian results in RR GG BB AA bytes in memory.
let rgba = 0xFF_C0_80_40;
let color = Vec4::unpack8(rgba).cast::<f32>() / 255_f32;
assert_eq!(Vec4 { x: 64.0/255.0, y: 128.0/255.0, z: 192.0/255.0, w: 1.0 }, color);
Sourcepub const fn pack(self) -> u32
pub const fn pack(self) -> u32
Packs into u32
.
use cvmath::Vec4;
let this: Vec4<u8> = Vec4 { x: 0x01, y: 0x56, z: 0x9A, w: 0xFE };
assert_eq!(0xFE_9A_56_01, this.pack());
Packs Vec4<f32>
color components into an RGBA color value.
use cvmath::Vec4;
let color = Vec4 { x: 64.0/255.0, y: 128.0/255.0, z: 192.0/255.0, w: 1.0 };
let rgba = (color * 255_f32).cast::<u8>().pack();
assert_eq!(0xFF_C0_80_40, rgba);
Trait Implementations§
Source§impl<U, T: AddAssign<U>> AddAssign<(U, U, U, U)> for Vec4<T>
impl<U, T: AddAssign<U>> AddAssign<(U, U, U, U)> for Vec4<T>
Source§fn add_assign(&mut self, rhs: (U, U, U, U))
fn add_assign(&mut self, rhs: (U, U, U, U))
+=
operation. Read moreSource§impl<U, T: AddAssign<U>> AddAssign<Vec4<U>> for Vec4<T>
impl<U, T: AddAssign<U>> AddAssign<Vec4<U>> for Vec4<T>
Source§fn add_assign(&mut self, rhs: Vec4<U>)
fn add_assign(&mut self, rhs: Vec4<U>)
+=
operation. Read moreSource§impl<U, T: DivAssign<U>> DivAssign<(U, U, U, U)> for Vec4<T>
impl<U, T: DivAssign<U>> DivAssign<(U, U, U, U)> for Vec4<T>
Source§fn div_assign(&mut self, rhs: (U, U, U, U))
fn div_assign(&mut self, rhs: (U, U, U, U))
/=
operation. Read moreSource§impl<U: Scalar, T: DivAssign<U>> DivAssign<U> for Vec4<T>
impl<U: Scalar, T: DivAssign<U>> DivAssign<U> for Vec4<T>
Source§fn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
/=
operation. Read moreSource§impl<U, T: DivAssign<U>> DivAssign<Vec4<U>> for Vec4<T>
impl<U, T: DivAssign<U>> DivAssign<Vec4<U>> for Vec4<T>
Source§fn div_assign(&mut self, rhs: Vec4<U>)
fn div_assign(&mut self, rhs: Vec4<U>)
/=
operation. Read moreSource§impl<T> From<(T, T, T, T)> for Vec4<T>
impl<T> From<(T, T, T, T)> for Vec4<T>
Source§fn from(val: (T, T, T, T)) -> Vec4<T>
fn from(val: (T, T, T, T)) -> Vec4<T>
Source§impl<T> Into<(T, T, T, T)> for Vec4<T>
impl<T> Into<(T, T, T, T)> for Vec4<T>
Source§fn into(self) -> (T, T, T, T)
fn into(self) -> (T, T, T, T)
Source§impl<U, T: MulAssign<U>> MulAssign<(U, U, U, U)> for Vec4<T>
impl<U, T: MulAssign<U>> MulAssign<(U, U, U, U)> for Vec4<T>
Source§fn mul_assign(&mut self, rhs: (U, U, U, U))
fn mul_assign(&mut self, rhs: (U, U, U, U))
*=
operation. Read moreSource§impl<U: Scalar, T: MulAssign<U>> MulAssign<U> for Vec4<T>
impl<U: Scalar, T: MulAssign<U>> MulAssign<U> for Vec4<T>
Source§fn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
*=
operation. Read moreSource§impl<U, T: MulAssign<U>> MulAssign<Vec4<U>> for Vec4<T>
impl<U, T: MulAssign<U>> MulAssign<Vec4<U>> for Vec4<T>
Source§fn mul_assign(&mut self, rhs: Vec4<U>)
fn mul_assign(&mut self, rhs: Vec4<U>)
*=
operation. Read moreSource§impl<T: Ord> Ord for Vec4<T>
impl<T: Ord> Ord for Vec4<T>
Source§impl<T: PartialOrd> PartialOrd for Vec4<T>
impl<T: PartialOrd> PartialOrd for Vec4<T>
Source§impl<U, T: RemAssign<U>> RemAssign<(U, U, U, U)> for Vec4<T>
impl<U, T: RemAssign<U>> RemAssign<(U, U, U, U)> for Vec4<T>
Source§fn rem_assign(&mut self, rhs: (U, U, U, U))
fn rem_assign(&mut self, rhs: (U, U, U, U))
%=
operation. Read moreSource§impl<U: Scalar, T: RemAssign<U>> RemAssign<U> for Vec4<T>
impl<U: Scalar, T: RemAssign<U>> RemAssign<U> for Vec4<T>
Source§fn rem_assign(&mut self, rhs: U)
fn rem_assign(&mut self, rhs: U)
%=
operation. Read moreSource§impl<U, T: RemAssign<U>> RemAssign<Vec4<U>> for Vec4<T>
impl<U, T: RemAssign<U>> RemAssign<Vec4<U>> for Vec4<T>
Source§fn rem_assign(&mut self, rhs: Vec4<U>)
fn rem_assign(&mut self, rhs: Vec4<U>)
%=
operation. Read moreSource§impl<U, T: SubAssign<U>> SubAssign<(U, U, U, U)> for Vec4<T>
impl<U, T: SubAssign<U>> SubAssign<(U, U, U, U)> for Vec4<T>
Source§fn sub_assign(&mut self, rhs: (U, U, U, U))
fn sub_assign(&mut self, rhs: (U, U, U, U))
-=
operation. Read moreSource§impl<U, T: SubAssign<U>> SubAssign<Vec4<U>> for Vec4<T>
impl<U, T: SubAssign<U>> SubAssign<Vec4<U>> for Vec4<T>
Source§fn sub_assign(&mut self, rhs: Vec4<U>)
fn sub_assign(&mut self, rhs: Vec4<U>)
-=
operation. Read more