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/collision.rs | 4 +- engine/src/data_types/matrix.rs | 8 +- engine/src/data_types/vector.rs | 380 +++++++++------------------------------- engine/src/math.rs | 6 +- engine/src/mesh/cube.rs | 12 +- engine/src/shader/default.rs | 7 +- 6 files changed, 100 insertions(+), 317 deletions(-) diff --git a/engine/src/collision.rs b/engine/src/collision.rs index 139b924..c053a7f 100644 --- a/engine/src/collision.rs +++ b/engine/src/collision.rs @@ -100,7 +100,7 @@ impl Collider for SphereCollider { fn intersects(&self, other: &SphereCollider) -> bool { - (&self.center - &other.center).length() <= self.radius + other.radius + (self.center - other.center).length() <= self.radius + other.radius } } @@ -136,6 +136,6 @@ impl Collider> for SphereCollider { fn intersects(&self, other: &Vec3) -> bool { - (&self.center - other).length() <= self.radius + (self.center - *other).length() <= self.radius } } 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 { diff --git a/engine/src/math.rs b/engine/src/math.rs index 0340de8..eeed61e 100644 --- a/engine/src/math.rs +++ b/engine/src/math.rs @@ -5,9 +5,9 @@ use crate::vector::Vec3; /// Calculates the surface normal of a triangle. #[must_use] pub fn calc_triangle_surface_normal( - egde_a: &Vec3, - edge_b: &Vec3, - edge_c: &Vec3, + egde_a: Vec3, + edge_b: Vec3, + edge_c: Vec3, ) -> Vec3 { let v1 = edge_b - egde_a; diff --git a/engine/src/mesh/cube.rs b/engine/src/mesh/cube.rs index b30449f..61d1e26 100644 --- a/engine/src/mesh/cube.rs +++ b/engine/src/mesh/cube.rs @@ -365,14 +365,14 @@ fn create_side(side_positions: &SidePositions, data: &mut Data) { let normal = match side_positions.normal_calc_order { NormalCalcOrder::Clockwise => calc_triangle_surface_normal( - &side_positions.up_left, - &side_positions.up_right, - &side_positions.down_left, + side_positions.up_left, + side_positions.up_right, + side_positions.down_left, ), NormalCalcOrder::CounterClockwise => calc_triangle_surface_normal( - &side_positions.up_left, - &side_positions.down_left, - &side_positions.up_right, + side_positions.up_left, + side_positions.down_left, + side_positions.up_right, ), }; diff --git a/engine/src/shader/default.rs b/engine/src/shader/default.rs index 1dc85fc..5b360ec 100644 --- a/engine/src/shader/default.rs +++ b/engine/src/shader/default.rs @@ -142,7 +142,7 @@ pub fn enqueue_set_shader_bindings( ), ( model_3d_shader_cursor.field("view"), - create_view_matrix(&camera, &camera_world_pos.position).into(), + create_view_matrix(&camera, camera_world_pos.position).into(), ), ( lighting_shader_cursor.field("view_pos"), @@ -325,14 +325,13 @@ fn create_model_matrix(transform: Transform) -> Matrix matrix } -fn create_view_matrix(camera: &Camera, camera_world_pos: &Vec3) - -> Matrix +fn create_view_matrix(camera: &Camera, camera_world_pos: Vec3) -> Matrix { let mut view = Matrix::new(); // tracing::debug!("Camera target: {:?}", camera.target); - view.look_at(camera_world_pos, &camera.target, &camera.global_up); + view.look_at(camera_world_pos, camera.target, camera.global_up); view } -- cgit v1.2.3-18-g5258