diff options
author | HampusM <hampus@hampusmat.com> | 2024-02-18 19:18:18 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-02-18 19:18:18 +0100 |
commit | e8c6c096b2068f4ea71b021bf02f56d266ed671c (patch) | |
tree | 0cc54c1c82bf7af9881da00fa4d1a882a8263fd1 /engine/src/vector.rs | |
parent | 12e0f5ffffcaa36cce3cd4fecc007b9a2955ff23 (diff) |
refactor(engine): move data types to a data types module
Diffstat (limited to 'engine/src/vector.rs')
-rw-r--r-- | engine/src/vector.rs | 324 |
1 files changed, 0 insertions, 324 deletions
diff --git a/engine/src/vector.rs b/engine/src/vector.rs deleted file mode 100644 index 5b7779c..0000000 --- a/engine/src/vector.rs +++ /dev/null @@ -1,324 +0,0 @@ -use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign}; - -use crate::color::Color; - -#[derive(Debug, Default, Clone)] -pub struct Vec2<Value> -{ - pub x: Value, - pub y: Value, -} - -impl Vec2<u32> -{ - pub const ZERO: Self = Self { x: 0, y: 0 }; -} - -impl<Value> Add<Value> for Vec2<Value> -where - Value: Add<Output = Value> + Clone, -{ - type Output = Self; - - fn add(self, rhs: Value) -> Self::Output - { - Self { - x: self.x + rhs.clone(), - y: self.y + rhs, - } - } -} - -impl<Value> Sub<Value> for Vec2<Value> -where - Value: Sub<Output = Value> + Clone, -{ - type Output = Self; - - fn sub(self, rhs: Value) -> Self::Output - { - Self { - x: self.x - rhs.clone(), - y: self.y - rhs, - } - } -} - -impl<Value> Mul<Value> for Vec2<Value> -where - Value: Mul<Output = Value> + Clone, -{ - type Output = Self; - - fn mul(self, rhs: Value) -> Self::Output - { - Self { - x: self.x * rhs.clone(), - y: self.y * rhs, - } - } -} - -impl<Value> Div<Value> for Vec2<Value> -where - Value: Div<Output = Value> + Clone, -{ - type Output = Self; - - fn div(self, rhs: Value) -> Self::Output - { - Self { - x: self.x / rhs.clone(), - y: self.y / rhs, - } - } -} - -#[derive(Debug, Default, Clone)] -#[repr(C)] -pub struct Vec3<Value> -{ - pub x: Value, - pub y: Value, - pub z: Value, -} - -impl Vec3<f32> -{ - pub const UP: Self = Self { x: 0.0, y: 1.0, z: 0.0 }; - - /// Returns the length of the vector. - #[must_use] - pub fn length(&self) -> f32 - { - (self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt() - } - - /// Normalizes the vector, returning a unit vector. - #[must_use] - pub fn normalize(&self) -> Self - { - let length = self.length(); - - Self { - x: self.x / length, - y: self.y / length, - z: self.z / length, - } - } - - /// Returns the cross product of this and another vector. - #[must_use] - pub fn cross(&self, rhs: &Self) -> Self - { - Self { - x: (self.y * rhs.z) - (self.z * rhs.y), - y: (self.z * rhs.x) - (self.x * rhs.z), - z: (self.x * rhs.y) - (self.y * rhs.x), - } - } - - /// Returns the dot product of this and another vector. - #[must_use] - pub fn dot(&self, rhs: &Self) -> f32 - { - (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) - } - - /// Returns a direction vector from the specified angle (in degrees). - #[must_use] - pub fn direction_from_angle(pitch_degs: f32, yaw_degs: f32) -> Self - { - Self { - x: yaw_degs.to_radians().cos() * pitch_degs.to_radians().cos(), - y: pitch_degs.to_radians().sin(), - z: yaw_degs.to_radians().sin() * pitch_degs.to_radians().cos(), - } - } -} - -impl<Value> Vec3<Value> -{ - pub fn as_ptr(&self) -> *const Value - { - &self.x - } -} - -impl<Value> Sub for Vec3<Value> -where - Value: Sub<Value, Output = Value>, -{ - type Output = Self; - - fn sub(self, rhs: Self) -> Self::Output - { - Self::Output { - x: self.x - rhs.x, - y: self.y - rhs.y, - z: self.z - rhs.z, - } - } -} - -impl<Value> Sub for &Vec3<Value> -where - for<'a, 'b> &'a Value: Sub<&'b Value, Output = Value>, -{ - type Output = Vec3<Value>; - - fn sub(self, rhs: Self) -> Self::Output - { - Self::Output { - x: &self.x - &rhs.x, - y: &self.y - &rhs.y, - z: &self.z - &rhs.z, - } - } -} - -impl<Value> Add for Vec3<Value> -where - Value: Add<Value, Output = Value>, -{ - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output - { - Self::Output { - x: self.x + rhs.x, - y: self.y + rhs.y, - z: self.z + rhs.z, - } - } -} - -impl<Value> Add for &Vec3<Value> -where - for<'a, 'b> &'a Value: Add<&'b Value, Output = Value>, -{ - type Output = Vec3<Value>; - - fn add(self, rhs: Self) -> Self::Output - { - Self::Output { - x: &self.x + &rhs.x, - y: &self.y + &rhs.y, - z: &self.z + &rhs.z, - } - } -} - -impl<Value> Neg for Vec3<Value> -where - Value: Neg<Output = Value>, -{ - type Output = Self; - - fn neg(mut self) -> Self::Output - { - self.x = -self.x; - self.y = -self.y; - self.z = -self.z; - - self - } -} - -impl<Value> Add<Value> for Vec3<Value> -where - Value: Add<Value, Output = Value> + Clone, -{ - type Output = Self; - - fn add(mut self, rhs: Value) -> Self::Output - { - self.x = self.x + rhs.clone(); - self.y = self.y + rhs.clone(); - self.z = self.z + rhs.clone(); - - self - } -} - -impl<Value> Sub<Value> for Vec3<Value> -where - Value: Sub<Value, Output = Value> + Clone, -{ - type Output = Self; - - fn sub(mut self, rhs: Value) -> Self::Output - { - self.x = self.x - rhs.clone(); - self.y = self.y - rhs.clone(); - self.z = self.z - rhs.clone(); - - self - } -} - -impl<Value> Mul<Value> for Vec3<Value> -where - Value: Mul<Value, Output = Value> + Clone, -{ - type Output = Self; - - fn mul(mut self, rhs: Value) -> Self::Output - { - self.x = self.x * rhs.clone(); - self.y = self.y * rhs.clone(); - self.z = self.z * rhs.clone(); - - self - } -} - -impl<Value> AddAssign for Vec3<Value> -where - Value: AddAssign<Value>, -{ - fn add_assign(&mut self, rhs: Self) - { - self.x += rhs.x; - self.y += rhs.y; - self.z += rhs.z; - } -} - -impl<Value> SubAssign for Vec3<Value> -where - Value: SubAssign<Value>, -{ - fn sub_assign(&mut self, rhs: Self) - { - self.x -= rhs.x; - self.y -= rhs.y; - self.z -= rhs.z; - } -} - -impl<Value> From<Value> for Vec3<Value> -where - Value: Clone, -{ - fn from(value: Value) -> Self - { - Self { - x: value.clone(), - y: value.clone(), - z: value, - } - } -} - -impl<Value> From<Color<Value>> for Vec3<Value> -{ - fn from(color: Color<Value>) -> Self - { - Self { - x: color.red, - y: color.green, - z: color.blue, - } - } -} |