From e026f6efed0b78861da57f5f0f0767f0adffa14f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 5 Sep 2024 13:36:38 -0700 Subject: [PATCH] wip --- fixed_wide_vectors/src/lib.rs | 14 ++--- fixed_wide_vectors/src/macros/matrix.rs | 49 ++++++++++++++++++ fixed_wide_vectors/src/macros/vector.rs | 24 +++++++++ fixed_wide_vectors/src/matrix.rs | 35 ++----------- fixed_wide_vectors/src/named.rs | 59 ++++++++++++++++++++++ fixed_wide_vectors/src/tests/fixed_wide.rs | 2 +- fixed_wide_vectors/src/types.rs | 18 +++++++ fixed_wide_vectors/src/vector.rs | 24 ++------- 8 files changed, 164 insertions(+), 61 deletions(-) create mode 100644 fixed_wide_vectors/src/named.rs create mode 100644 fixed_wide_vectors/src/types.rs diff --git a/fixed_wide_vectors/src/lib.rs b/fixed_wide_vectors/src/lib.rs index a9c0b41..628ca4e 100644 --- a/fixed_wide_vectors/src/lib.rs +++ b/fixed_wide_vectors/src/lib.rs @@ -1,14 +1,10 @@ mod macros; -mod vector; -mod matrix; +pub mod types; +pub mod vector; +pub mod matrix; -pub use vector::Vector2; -pub use vector::Vector3; -pub use vector::Vector4; - -pub use matrix::Matrix2; -pub use matrix::Matrix3; -pub use matrix::Matrix4; +#[cfg(feature="named-fields")] +mod named; #[cfg(test)] mod tests; diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index 8b13789..518315b 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -1 +1,50 @@ +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix { + () => { + //$crate::impl_common!(); + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_named_fields_shape { + ( + ($struct_outer:ident, $size_outer: expr), + ($struct_inner:ident, $size_inner: expr) + ) => { + impl core::ops::Deref for Matrix<$size_outer,$size_inner,T>{ + type Target=$struct_outer<$struct_inner>; + fn deref(&self)->&Self::Target{ + unsafe{core::mem::transmute(&self.array)} + } + } + impl core::ops::DerefMut for Matrix<$size_outer,$size_inner,T>{ + fn deref_mut(&mut self)->&mut Self::Target{ + unsafe{core::mem::transmute(&mut self.array)} + } + } + } +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_named_fields_shape_shim { + ( + ($($vector_info:tt),+), + $matrix_info:tt + ) => { + $crate::macro_repeated!(impl_matrix_named_fields_shape,$matrix_info,$($vector_info),+); + } +} + +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_matrix_named_fields { + ( + ($($matrix_info:tt),+), + $vector_infos:tt + ) => { + $crate::macro_repeated!(impl_matrix_named_fields_shape_shim,$vector_infos,$($matrix_info),+); + } +} diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index 8b13789..fc28750 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -1 +1,25 @@ +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector { + () => { + //$crate::impl_common!(); + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_named_fields { + ( $struct:ident, $size: expr ) => { + impl core::ops::Deref for Vector<$size,T>{ + type Target=$struct; + fn deref(&self)->&Self::Target{ + unsafe{core::mem::transmute(&self.array)} + } + } + impl core::ops::DerefMut for Vector<$size,T>{ + fn deref_mut(&mut self)->&mut Self::Target{ + unsafe{core::mem::transmute(&mut self.array)} + } + } + } +} diff --git a/fixed_wide_vectors/src/matrix.rs b/fixed_wide_vectors/src/matrix.rs index 41579df..331424b 100644 --- a/fixed_wide_vectors/src/matrix.rs +++ b/fixed_wide_vectors/src/matrix.rs @@ -1,35 +1,8 @@ -use crate::{Vector2,Vector3,Vector4}; - -pub struct Matrix2 { - pub x_axis: T, - pub y_axis: T, -} -pub struct Matrix3 { - pub x_axis: T, - pub y_axis: T, - pub z_axis: T, -} -pub struct Matrix4 { - pub x_axis: T, - pub y_axis: T, - pub z_axis: T, - pub w_axis: T, +pub struct Matrix{ + pub(crate) array:[[T;Y];X], } -crate::impl_matrix_named_fields!( - //outer struct - ( - (Matrix2 { x_axis, y_axis }, 2), - (Matrix3 { x_axis, y_axis, z_axis }, 3), - (Matrix4 { x_axis, y_axis, z_axis, w_axis }, 4) - ), - //inner struct - ( - (Vector2 { x, y }, 2), - (Vector3 { x, y, z }, 3), - (Vector4 { x, y, z, w }, 4) - ) -); +crate::impl_matrix!(); //Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases -crate::impl_matrix_3x3!(); +//crate::impl_matrix_3x3!(); diff --git a/fixed_wide_vectors/src/named.rs b/fixed_wide_vectors/src/named.rs new file mode 100644 index 0000000..f490ee9 --- /dev/null +++ b/fixed_wide_vectors/src/named.rs @@ -0,0 +1,59 @@ +use crate::vector::Vector; +use crate::matrix::Matrix; + +#[repr(C)] +pub struct Vector2 { + pub x: T, + pub y: T, +} +#[repr(C)] +pub struct Vector3 { + pub x: T, + pub y: T, + pub z: T, +} +#[repr(C)] +pub struct Vector4 { + pub x: T, + pub y: T, + pub z: T, + pub w: T, +} + +crate::impl_vector_named_fields!(Vector2, 2); +crate::impl_vector_named_fields!(Vector3, 3); +crate::impl_vector_named_fields!(Vector4, 4); + +#[repr(C)] +pub struct Matrix2 { + pub x_axis: T, + pub y_axis: T, +} +#[repr(C)] +pub struct Matrix3 { + pub x_axis: T, + pub y_axis: T, + pub z_axis: T, +} +#[repr(C)] +pub struct Matrix4 { + pub x_axis: T, + pub y_axis: T, + pub z_axis: T, + pub w_axis: T, +} + +crate::impl_matrix_named_fields!( + //outer struct + ( + (Matrix2, 2), + (Matrix3, 3), + (Matrix4, 4) + ), + //inner struct + ( + (Vector2, 2), + (Vector3, 3), + (Vector4, 4) + ) +); diff --git a/fixed_wide_vectors/src/tests/fixed_wide.rs b/fixed_wide_vectors/src/tests/fixed_wide.rs index 1f87fb0..b5cecd3 100644 --- a/fixed_wide_vectors/src/tests/fixed_wide.rs +++ b/fixed_wide_vectors/src/tests/fixed_wide.rs @@ -1,4 +1,4 @@ -use crate::{Vector2,Vector3,Vector4,Matrix3,Matrix4}; +use crate::types::{Vector2,Vector3,Vector4,Matrix3,Matrix4}; type Planar64=fixed_wide::types::I32F32; type Planar64Wide1=fixed_wide::types::I64F64; diff --git a/fixed_wide_vectors/src/types.rs b/fixed_wide_vectors/src/types.rs new file mode 100644 index 0000000..6abf17f --- /dev/null +++ b/fixed_wide_vectors/src/types.rs @@ -0,0 +1,18 @@ +use crate::vector::Vector; +use crate::matrix::Matrix; + +pub type Vector2=Vector<2,T>; +pub type Vector3=Vector<3,T>; +pub type Vector4=Vector<4,T>; + +pub type Matrix2=Matrix<2,2,T>; +pub type Matrix2x3=Matrix<2,3,T>; +pub type Matrix2x4=Matrix<2,4,T>; + +pub type Matrix3x2=Matrix<3,2,T>; +pub type Matrix3=Matrix<3,3,T>; +pub type Matrix3x4=Matrix<3,4,T>; + +pub type Matrix4x2=Matrix<4,2,T>; +pub type Matrix4x3=Matrix<4,3,T>; +pub type Matrix4=Matrix<4,4,T>; diff --git a/fixed_wide_vectors/src/vector.rs b/fixed_wide_vectors/src/vector.rs index 12ed55a..3d3e956 100644 --- a/fixed_wide_vectors/src/vector.rs +++ b/fixed_wide_vectors/src/vector.rs @@ -1,24 +1,8 @@ -pub struct Vector2 { - pub x: T, - pub y: T, +pub struct Vector{ + pub(crate) array:[T;N], } -pub struct Vector3 { - pub x: T, - pub y: T, - pub z: T, -} - -pub struct Vector4 { - pub x: T, - pub y: T, - pub z: T, - pub w: T, -} - -crate::impl_vector_named_fields!(Vector2 { x, y }, 2); -crate::impl_vector_named_fields!(Vector3 { x, y, z }, 3); -crate::impl_vector_named_fields!(Vector4 { x, y, z, w }, 4); +crate::impl_vector!(); //cross product -crate::impl_vector_3!(); +//crate::impl_vector_3!();