From 52462205323c20feb219dc8215a9db4ca551bd84 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 2 Jun 2026 22:00:14 +0200 Subject: refactor(engine): implement vector math op traits with macro --- engine/src/data_types/matrix.rs | 8 +- engine/src/data_types/vector.rs | 380 +++++++++------------------------------- 2 files changed, 86 insertions(+), 302 deletions(-) (limited to 'engine/src/data_types') diff --git a/engine/src/data_types/matrix.rs b/engine/src/data_types/matrix.rs index 486ab24..cc43e7b 100644 --- a/engine/src/data_types/matrix.rs +++ b/engine/src/data_types/matrix.rs @@ -94,7 +94,7 @@ impl Matrix self.set_cell(3, 3, 1.0); } - pub fn look_at(&mut self, eye: &Vec3, target: &Vec3, up: &Vec3) + pub fn look_at(&mut self, eye: Vec3, target: Vec3, up: Vec3) { let rev_target_direction = (eye - target).normalize(); @@ -117,9 +117,9 @@ impl Matrix // The vector is negated since we want the world to be translated in the opposite // direction of where we want the camera to move. let camera_pos = -Vec3 { - x: camera_right.dot(eye), - y: camera_up.dot(eye), - z: rev_target_direction.dot(eye), + x: camera_right.dot(&eye), + y: camera_up.dot(&eye), + z: rev_target_direction.dot(&eye), }; self.set_cell(0, 3, camera_pos.x); diff --git a/engine/src/data_types/vector.rs b/engine/src/data_types/vector.rs index 77173d6..980ca59 100644 --- a/engine/src/data_types/vector.rs +++ b/engine/src/data_types/vector.rs @@ -1,7 +1,69 @@ -use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use crate::color::Color; +macro_rules! impl_math_op_traits { + (impl $op_trait: ident for $vector: ident, ($($vec_field: ident),+)) => { + paste::paste! { + impl $op_trait for $vector + where + Value: $op_trait, + { + type Output = Self; + + fn [<$op_trait:lower>](self, rhs: Self) -> Self::Output + { + Self::Output {$( + $vec_field: self.$vec_field.[<$op_trait:lower>](rhs.$vec_field) + ),+} + } + } + + impl [<$op_trait Assign>] for $vector + where + Value: [<$op_trait Assign>], + { + fn [<$op_trait:lower _assign>](&mut self, rhs: Self) + { + $( + self.$vec_field.[<$op_trait:lower _assign>](rhs.$vec_field); + )+ + } + } + } + }; + + (impl $op_trait: ident for $vector: ident, ($($vec_field: ident),+)) => { + paste::paste! { + impl $op_trait for $vector + where + Value: $op_trait + Clone, + { + type Output = Self; + + fn [<$op_trait:lower>](self, rhs: Value) -> Self::Output + { + Self {$( + $vec_field: self.$vec_field.[<$op_trait:lower>](rhs.clone()) + ),+} + } + } + + impl [<$op_trait Assign>] for $vector + where + Value: [<$op_trait Assign>] + Clone, + { + fn [<$op_trait:lower _assign>](&mut self, rhs: Value) + { + $( + self.$vec_field.[<$op_trait:lower _assign>](rhs.clone()); + )+ + } + } + } + }; +} + #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct Vec2 { @@ -22,88 +84,13 @@ impl Vec2 pub const ZERO: Self = Self { x: 0, y: 0 }; } -impl Add for Vec2 -where - Value: Add, -{ - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output - { - Self::Output { x: self.x + rhs.x, y: self.y + rhs.y } - } -} - -impl AddAssign for Vec2 -where - Value: Add + Clone, -{ - fn add_assign(&mut self, rhs: Self) - { - self.x = self.x.clone() + rhs.x; - self.y = self.y.clone() + rhs.y; - } -} - -impl Add for Vec2 -where - Value: Add + Clone, -{ - type Output = Self; - - fn add(self, rhs: Value) -> Self::Output - { - Self { - x: self.x + rhs.clone(), - y: self.y + rhs, - } - } -} - -impl Sub for Vec2 -where - Value: Sub + Clone, -{ - type Output = Self; - - fn sub(self, rhs: Value) -> Self::Output - { - Self { - x: self.x - rhs.clone(), - y: self.y - rhs, - } - } -} +impl_math_op_traits!(impl Add for Vec2, (x, y)); +impl_math_op_traits!(impl Sub for Vec2, (x, y)); -impl Mul for Vec2 -where - Value: Mul + Clone, -{ - type Output = Self; - - fn mul(self, rhs: Value) -> Self::Output - { - Self { - x: self.x * rhs.clone(), - y: self.y * rhs, - } - } -} - -impl Div for Vec2 -where - Value: Div + Clone, -{ - type Output = Self; - - fn div(self, rhs: Value) -> Self::Output - { - Self { - x: self.x / rhs.clone(), - y: self.y / rhs, - } - } -} +impl_math_op_traits!(impl Add for Vec2, (x, y)); +impl_math_op_traits!(impl Sub for Vec2, (x, y)); +impl_math_op_traits!(impl Mul for Vec2, (x, y)); +impl_math_op_traits!(impl Div for Vec2, (x, y)); impl From<[Value; 2]> for Vec2 { @@ -204,85 +191,13 @@ impl Vec3 } } -impl Sub for Vec3 -where - Value: Sub, -{ - 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 Sub for &Vec3 -where - for<'a, 'b> &'a Value: Sub<&'b Value, Output = Value>, -{ - type Output = Vec3; - - 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 Add for Vec3 -where - Value: Add, -{ - 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 Add for &Vec3 -where - for<'a, 'b> &'a Value: Add<&'b Value, Output = Value>, -{ - type Output = Vec3; - - 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 Mul for Vec3 -where - Value: Mul, -{ - type Output = Self; +impl_math_op_traits!(impl Add for Vec3, (x, y, z)); +impl_math_op_traits!(impl Sub for Vec3, (x, y, z)); - fn mul(self, rhs: Self) -> Self::Output - { - Self::Output { - x: self.x * rhs.x, - y: self.y * rhs.y, - z: self.z * rhs.z, - } - } -} +impl_math_op_traits!(impl Add for Vec3, (x, y, z)); +impl_math_op_traits!(impl Sub for Vec3, (x, y, z)); +impl_math_op_traits!(impl Mul for Vec3, (x, y, z)); +impl_math_op_traits!(impl Div for Vec3, (x, y, z)); impl Neg for Vec3 where @@ -300,78 +215,6 @@ where } } -impl Add for Vec3 -where - Value: Add + 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 Sub for Vec3 -where - Value: Sub + 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 Mul for Vec3 -where - Value: Mul + 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 AddAssign for Vec3 -where - Value: AddAssign, -{ - fn add_assign(&mut self, rhs: Self) - { - self.x += rhs.x; - self.y += rhs.y; - self.z += rhs.z; - } -} - -impl SubAssign for Vec3 -where - Value: SubAssign, -{ - fn sub_assign(&mut self, rhs: Self) - { - self.x -= rhs.x; - self.y -= rhs.y; - self.z -= rhs.z; - } -} - impl From for Vec3 { fn from(value: f32) -> Self @@ -417,73 +260,14 @@ pub struct Vec4 pub w: Value, } -impl Mul for Vec4 -where - Value: Mul, -{ - type Output = Self; - - fn mul(self, rhs: Self) -> Self::Output - { - Self::Output { - x: self.x * rhs.x, - y: self.y * rhs.y, - z: self.z * rhs.z, - w: self.w * rhs.w, - } - } -} - -impl Add for Vec4 -where - Value: Add, -{ - type Output = Self; +impl_math_op_traits!(impl Add for Vec4, (x, y, z, w)); +impl_math_op_traits!(impl Sub for Vec4, (x, y, z, w)); +impl_math_op_traits!(impl Mul for Vec4, (x, y, z, w)); - fn add(self, rhs: Self) -> Self::Output - { - Self::Output { - x: self.x + rhs.x, - y: self.y + rhs.y, - z: self.z + rhs.z, - w: self.w + rhs.w, - } - } -} - -impl Sub for Vec4 -where - Value: Sub, -{ - 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, - w: self.w - rhs.w, - } - } -} - -impl Mul for Vec4 -where - Value: Mul + 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.w = self.w * rhs.clone(); - - self - } -} +impl_math_op_traits!(impl Add for Vec4, (x, y, z, w)); +impl_math_op_traits!(impl Sub for Vec4, (x, y, z, w)); +impl_math_op_traits!(impl Mul for Vec4, (x, y, z, w)); +impl_math_op_traits!(impl Div for Vec4, (x, y, z, w)); impl From for Vec4 { -- cgit v1.2.3-18-g5258