From cf17460b77df6ba9e73a7ade63c636a2a9fc1707 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 4 Sep 2024 13:38:29 -0700 Subject: [PATCH] special case 3d vectors and matrices --- fixed_wide_vectors/src/macros/fixed_wide.rs | 96 +++++++++++++++++++++ fixed_wide_vectors/src/macros/matrix.rs | 9 ++ fixed_wide_vectors/src/macros/vector.rs | 9 ++ fixed_wide_vectors/src/matrix.rs | 3 + fixed_wide_vectors/src/vector.rs | 3 + 5 files changed, 120 insertions(+) diff --git a/fixed_wide_vectors/src/macros/fixed_wide.rs b/fixed_wide_vectors/src/macros/fixed_wide.rs index 98398c3..3812d86 100644 --- a/fixed_wide_vectors/src/macros/fixed_wide.rs +++ b/fixed_wide_vectors/src/macros/fixed_wide.rs @@ -83,6 +83,36 @@ macro_rules! impl_wide_vector_operations { }; } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_3_wide_cross { + ( + (), + ($lhs:expr, $rhs:expr) + )=>{ + impl Vector3>{ + paste::item!{ + #[inline] + pub fn [](self,rhs:Vector3>)->Vector3>{ + Vector3{ + 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) + } + } + } + } + } +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_wide_3 { + ()=>{ + $crate::do_macro_8x8!(impl_vector_3_wide_cross,()); + } +} + #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! impl_matrix_wide_dot_transpose_helper { @@ -263,6 +293,72 @@ macro_rules! impl_wide_matrix_operations_1arg_not_const_generic { }; } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! do_macro_4_dumb{ + ( + $macro:ident, + $any:tt + )=>{ + $crate::macro_repeated!($macro, $any, (1,2),(2,4),(3,6),(4,8)); + }; +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3_det_not_const_generic { + ( + $n: expr, + $_2n: expr + )=>{ + impl Matrix3>>{ + paste::item!{ + pub fn [](self)->fixed_wide::fixed::Fixed<{$n*3},{$n*3*32}>{ + //[] will not compile, so the doubles are hardcoded above + self.x_axis.[](self.y_axis.[](self.z_axis)) + } + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3_det_not_const_generic_shim { + ( + (),($n: expr,$_2n: expr) + )=>{ + $crate::impl_matrix_wide_3x3_det_not_const_generic!($n,$_2n); + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3_adjugate_not_const_generic { + ( + (), + $n: expr + )=>{ + impl Matrix3>>{ + paste::item!{ + pub fn [](self)->Matrix3>>{ + Matrix3{ + x_axis:Vector3{x:self.y_axis.y.[](self.z_axis.z)-self.y_axis.z.[](self.z_axis.y),y:self.x_axis.z.[](self.z_axis.y)-self.x_axis.y.[](self.z_axis.z),z:self.x_axis.y.[](self.y_axis.z)-self.x_axis.z.[](self.y_axis.y)}, + y_axis:Vector3{x:self.y_axis.z.[](self.z_axis.x)-self.y_axis.x.[](self.z_axis.z),y:self.x_axis.x.[](self.z_axis.z)-self.x_axis.z.[](self.z_axis.x),z:self.x_axis.z.[](self.y_axis.x)-self.x_axis.x.[](self.y_axis.z)}, + z_axis:Vector3{x:self.y_axis.x.[](self.z_axis.y)-self.y_axis.y.[](self.z_axis.x),y:self.x_axis.y.[](self.z_axis.x)-self.x_axis.x.[](self.z_axis.y),z:self.x_axis.x.[](self.y_axis.y)-self.x_axis.y.[](self.y_axis.x)}, + } + } + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_wide_3x3 { + ()=>{ + $crate::do_macro_4_dumb!(impl_matrix_wide_3x3_det_not_const_generic_shim,()); + $crate::do_macro_8!(impl_matrix_wide_3x3_adjugate_not_const_generic,()); + } +} + // HACK: Allows us to sum repeating tokens in macros. // See: https://stackoverflow.com/a/60187870/17452730 #[doc(hidden)] diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index eeed579..37b3b99 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -152,6 +152,15 @@ macro_rules! matrix_transpose_inner { } } +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_3x3 { + ()=>{ + #[cfg(feature="fixed_wide")] + $crate::impl_matrix_wide_3x3!(); + } +} + #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! impl_matrix_operator { diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index d3a69e7..ad7a72e 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -144,3 +144,12 @@ macro_rules! impl_vector_operator { } }; } + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_3 { + ()=>{ + #[cfg(feature="fixed_wide")] + $crate::impl_vector_wide_3!(); + } +} diff --git a/fixed_wide_vectors/src/matrix.rs b/fixed_wide_vectors/src/matrix.rs index 9acd5a1..4966f28 100644 --- a/fixed_wide_vectors/src/matrix.rs +++ b/fixed_wide_vectors/src/matrix.rs @@ -34,3 +34,6 @@ crate::impl_matrices!( (Vector4 { x, y, z, w }, Matrix4 { x_axis, y_axis, z_axis, w_axis }, 4) ) ); + +//Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases +crate::impl_matrix_3x3!(); diff --git a/fixed_wide_vectors/src/vector.rs b/fixed_wide_vectors/src/vector.rs index f7ff0a6..5016c97 100644 --- a/fixed_wide_vectors/src/vector.rs +++ b/fixed_wide_vectors/src/vector.rs @@ -79,3 +79,6 @@ crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector crate::impl_matrix_inner!((Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4), (Vector2 { x, y }, Vector2 { x, y }, 2) ); crate::impl_matrix_inner!((Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4), (Vector3 { x, y, z }, Vector3 { x, y, z }, 3) ); crate::impl_matrix_inner!((Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4), (Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4) ); + +//cross product +crate::impl_vector_3!();