use super::vec3::Vec3;
use std::ops;
#[derive(Copy, Clone, PartialEq)]
pub struct Vec4 {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
impl Vec4 {
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
Vec4 { x, y, z, w }
}
pub fn from_vec3(xyz: Vec3, w: f32) -> Self {
Vec4 {
x: xyz.x,
y: xyz.y,
z: xyz.z,
w,
}
}
pub fn zero() -> Self {
Self {
x: 0.0,
y: 0.0,
z: 0.0,
w: 0.0,
}
}
pub fn one() -> Self {
Self {
x: 1.0,
y: 1.0,
z: 1.0,
w: 1.0,
}
}
#[inline]
pub fn dot(a: &Self, b: &Self) -> f32 {
a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
}
#[inline]
pub fn project(a: Self, b: Self) -> Self {
let b = b.norm();
b * Self::dot(&a, &b)
}
#[inline]
pub fn project_plane(a: Self, normal: Self) -> Self {
let normal = normal.norm();
a - normal * Self::dot(&a, &normal)
}
#[inline]
pub fn lerp(a: Self, b: Self, t: f32) -> Self {
let t = if t < 0.0 {
0.0
} else if t > 1.0 {
1.0
} else {
t
};
a * (1.0 - t) + b * t
}
#[inline]
pub fn lerp_unclamped(a: Self, b: Self, t: f32) -> Self {
a * (1.0 - t) + b * t
}
#[inline]
pub fn mag(&self) -> f32 {
(self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
}
#[inline]
pub fn sqrmag(&self) -> f32 {
self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w
}
#[inline]
pub fn norm(&self) -> Vec4 {
*self / self.mag()
}
#[inline]
pub fn xyz(&self) -> Vec3 {
Vec3 {
x: self.x,
y: self.y,
z: self.z,
}
}
}
impl std::fmt::Display for Vec4 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {}, {}, {})", self.x, self.y, self.z, self.w)
}
}
impl std::fmt::Debug for Vec4 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {}, {}, {})", self.x, self.y, self.z, self.w)
}
}
impl ops::Add for Vec4 {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
w: self.w + other.w,
}
}
}
impl ops::AddAssign for Vec4 {
#[inline]
fn add_assign(&mut self, other: Self) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
self.w += other.w;
}
}
impl ops::Sub for Vec4 {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
w: self.w - other.w,
}
}
}
impl ops::SubAssign for Vec4 {
#[inline]
fn sub_assign(&mut self, other: Self) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
self.w -= other.w;
}
}
impl ops::Mul for Vec4 {
type Output = Vec4;
#[inline]
fn mul(self, other: Self) -> Self {
Vec4 {
x: self.x * other.x,
y: self.y * other.y,
z: self.z * other.z,
w: self.w * other.w,
}
}
}
impl ops::MulAssign for Vec4 {
#[inline]
fn mul_assign(&mut self, other: Self) {
self.x *= other.x;
self.y *= other.y;
self.z *= other.z;
self.w *= other.w;
}
}
impl ops::Neg for Vec4 {
type Output = Vec4;
#[inline]
fn neg(self) -> Vec4 {
Vec4 {
x: -self.x,
y: -self.y,
z: -self.z,
w: -self.w,
}
}
}
impl ops::Mul<f32> for Vec4 {
type Output = Vec4;
#[inline]
fn mul(self, rhs: f32) -> Vec4 {
Vec4 {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
w: self.w * rhs,
}
}
}
impl ops::MulAssign<f32> for Vec4 {
#[inline]
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
self.z *= rhs;
self.w *= rhs;
}
}
impl ops::Div<f32> for Vec4 {
type Output = Vec4;
#[inline]
fn div(self, rhs: f32) -> Self {
Vec4 {
x: self.x / rhs,
y: self.y / rhs,
z: self.z / rhs,
w: self.w / rhs,
}
}
}
impl ops::DivAssign<f32> for Vec4 {
#[inline]
fn div_assign(&mut self, rhs: f32) {
self.x /= rhs;
self.y /= rhs;
self.z /= rhs;
self.w /= rhs;
}
}
impl From<(f32, f32, f32, f32)> for Vec4 {
fn from(t: (f32, f32, f32, f32)) -> Self {
Vec4 {
x: t.0,
y: t.1,
z: t.2,
w: t.3,
}
}
}
impl From<(i32, i32, i32, i32)> for Vec4 {
fn from(t: (i32, i32, i32, i32)) -> Self {
Vec4 {
x: t.0 as f32,
y: t.1 as f32,
z: t.2 as f32,
w: t.3 as f32,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn ops() {
assert_eq!(
Vec4::new(1.0, 2.0, 3.0, 4.0) + Vec4::new(4.0, -3.0, 5.0, -3.0),
Vec4::new(5.0, -1.0, 8.0, 1.0)
);
assert_eq!(
Vec4::new(1.0, 2.0, 3.0, 4.0) - Vec4::new(4.0, -3.0, -0.0, -3.0),
Vec4::new(-3.0, 5.0, 3.0, 7.0)
);
assert_eq!(
Vec4::new(1.0, 2.0, 3.0, 4.0) * Vec4::new(4.0, -3.0, 0.5, -4.5),
Vec4::new(4.0, -6.0, 1.5, -18.0)
);
}
#[test]
fn dot() {
assert_eq!(
Vec4::dot(
&Vec4::new(4.0, 0.0, -2.0, 0.0).norm(),
&Vec4::new(0.0, 0.0, 2.0, 2.0)
),
(-2.0 / 20_f32.sqrt()) * 2.0
);
assert_eq!(
Vec4::dot(
&Vec4::new(4.0, 0.0, 0.0, 4.0).norm(),
&Vec4::new(1.0, 0.0, 0.0, 0.0)
),
1.0 / 2_f32.sqrt()
);
}
}