sprocket_engine 0.2.1

A vulkan game engine
Documentation
use super::vec3::Vec3;
use std::ops;
/// Representation of 3D vectors and points
#[derive(Copy, Clone, PartialEq)]
pub struct Vec4 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

impl Vec4 {
    /// Creates a vector given x,y,z
    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,
        }
    }

    /// Creates a vector with all components being zero
    pub fn zero() -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            z: 0.0,
            w: 0.0,
        }
    }

    /// Creates a vector with all components being one
    pub fn one() -> Self {
        Self {
            x: 1.0,
            y: 1.0,
            z: 1.0,
            w: 1.0,
        }
    }

    /// Returns the dot product of two vectors.
    /// The dot product is the cosine of the angle between a, b.
    ///
    /// # Example outputs assuming a, b are normalized
    /// 1: vectors are pointing in the same direction<br>
    /// 0: vectors are perpendicular<br>
    /// -1: vectors are opposite<br>
    #[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
    }

    /// Project a vector onto another
    #[inline]
    pub fn project(a: Self, b: Self) -> Self {
        let b = b.norm();
        b * Self::dot(&a, &b)
    }

    /// Projects a vector onto a plane
    #[inline]
    pub fn project_plane(a: Self, normal: Self) -> Self {
        let normal = normal.norm();
        a - normal * Self::dot(&a, &normal)
    }

    /// Linearly interpolates between two vectors with t.<br>
    /// When t > 1, lerp(a, b) = b<br>
    /// When t = 0, lerp(a, b) = t<br>
    /// Clamps t between 0, 1
    #[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
    }

    /// Linearly interpolates between two vectors with t.<br>
    /// When t > 1, lerp(a, b) = b<br>
    /// When t = 0, lerp(a, b) = t<br>
    /// Does not clamp t between 0, 1
    #[inline]
    pub fn lerp_unclamped(a: Self, b: Self, t: f32) -> Self {
        a * (1.0 - t) + b * t
    }

    // Instance method

    /// Returns the magnitude/length of the vector
    /// Note: for comparing two vectors, sqrmag is faster
    #[inline]
    pub fn mag(&self) -> f32 {
        (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
    }

    /// Returns the squared length of the vector
    /// Is faster than mag due to not using sqrt
    #[inline]
    pub fn sqrmag(&self) -> f32 {
        self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w
    }

    /// Returns the normal version the vector
    #[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)
    }
}

// Math operators

/// Adds two vectors component wise
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,
        }
    }
}

/// Compound addition to vector component wise
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;
    }
}

/// Subtracts two vectors component wise
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,
        }
    }
}

/// Compound subtraction to vector component wise
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;
    }
}

/// Multiplies two vectors component wise
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,
        }
    }
}

// Compound multiplies two vectors component wise
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;
    }
}

/// Negates the vector
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,
        }
    }
}

/// Multiplies the length of the vector by rhs
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,
        }
    }
}

/// Compound multiplies the length of the vector
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;
    }
}

/// Divides the length of the vector by 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,
        }
    }
}

/// Compound divides the length of the vector by 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()
        );
    }
}