This commit is contained in:
Quaternions 2024-09-05 13:36:38 -07:00
parent e475da5fb4
commit e026f6efed
8 changed files with 164 additions and 61 deletions

View File

@ -1,14 +1,10 @@
mod macros; mod macros;
mod vector; pub mod types;
mod matrix; pub mod vector;
pub mod matrix;
pub use vector::Vector2; #[cfg(feature="named-fields")]
pub use vector::Vector3; mod named;
pub use vector::Vector4;
pub use matrix::Matrix2;
pub use matrix::Matrix3;
pub use matrix::Matrix4;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View File

@ -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<T> core::ops::Deref for Matrix<$size_outer,$size_inner,T>{
type Target=$struct_outer<$struct_inner<T>>;
fn deref(&self)->&Self::Target{
unsafe{core::mem::transmute(&self.array)}
}
}
impl<T> 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),+);
}
}

View File

@ -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<T> core::ops::Deref for Vector<$size,T>{
type Target=$struct<T>;
fn deref(&self)->&Self::Target{
unsafe{core::mem::transmute(&self.array)}
}
}
impl<T> core::ops::DerefMut for Vector<$size,T>{
fn deref_mut(&mut self)->&mut Self::Target{
unsafe{core::mem::transmute(&mut self.array)}
}
}
}
}

View File

@ -1,35 +1,8 @@
use crate::{Vector2,Vector3,Vector4}; pub struct Matrix<const X:usize,const Y:usize,T>{
pub(crate) array:[[T;Y];X],
pub struct Matrix2<T> {
pub x_axis: T,
pub y_axis: T,
}
pub struct Matrix3<T> {
pub x_axis: T,
pub y_axis: T,
pub z_axis: T,
}
pub struct Matrix4<T> {
pub x_axis: T,
pub y_axis: T,
pub z_axis: T,
pub w_axis: T,
} }
crate::impl_matrix_named_fields!( crate::impl_matrix!();
//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)
)
);
//Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases //Special case 3x3 matrix operations because I cba to write macros for the arbitrary cases
crate::impl_matrix_3x3!(); //crate::impl_matrix_3x3!();

View File

@ -0,0 +1,59 @@
use crate::vector::Vector;
use crate::matrix::Matrix;
#[repr(C)]
pub struct Vector2<T> {
pub x: T,
pub y: T,
}
#[repr(C)]
pub struct Vector3<T> {
pub x: T,
pub y: T,
pub z: T,
}
#[repr(C)]
pub struct Vector4<T> {
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<T> {
pub x_axis: T,
pub y_axis: T,
}
#[repr(C)]
pub struct Matrix3<T> {
pub x_axis: T,
pub y_axis: T,
pub z_axis: T,
}
#[repr(C)]
pub struct Matrix4<T> {
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)
)
);

View File

@ -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 Planar64=fixed_wide::types::I32F32;
type Planar64Wide1=fixed_wide::types::I64F64; type Planar64Wide1=fixed_wide::types::I64F64;

View File

@ -0,0 +1,18 @@
use crate::vector::Vector;
use crate::matrix::Matrix;
pub type Vector2<T>=Vector<2,T>;
pub type Vector3<T>=Vector<3,T>;
pub type Vector4<T>=Vector<4,T>;
pub type Matrix2<T>=Matrix<2,2,T>;
pub type Matrix2x3<T>=Matrix<2,3,T>;
pub type Matrix2x4<T>=Matrix<2,4,T>;
pub type Matrix3x2<T>=Matrix<3,2,T>;
pub type Matrix3<T>=Matrix<3,3,T>;
pub type Matrix3x4<T>=Matrix<3,4,T>;
pub type Matrix4x2<T>=Matrix<4,2,T>;
pub type Matrix4x3<T>=Matrix<4,3,T>;
pub type Matrix4<T>=Matrix<4,4,T>;

View File

@ -1,24 +1,8 @@
pub struct Vector2<T> { pub struct Vector<const N:usize,T>{
pub x: T, pub(crate) array:[T;N],
pub y: T,
} }
pub struct Vector3<T> { crate::impl_vector!();
pub x: T,
pub y: T,
pub z: T,
}
pub struct Vector4<T> {
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);
//cross product //cross product
crate::impl_vector_3!(); //crate::impl_vector_3!();