VECTOR IS MATRIX
This commit is contained in:
parent
f531e8d8ee
commit
8e1807b4b7
171
fixed_wide_vectors/src/macros/matrix.rs
Normal file
171
fixed_wide_vectors/src/macros/matrix.rs
Normal file
@ -0,0 +1,171 @@
|
||||
// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license)
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix {
|
||||
(
|
||||
($struct_outer: ident { $($field_outer: ident), + }, ( $($generic_outer: tt), + ), $size_outer: expr),
|
||||
($struct_inner: ident, $size_inner: expr), $fields_inner:tt
|
||||
) => {
|
||||
impl<T> $struct_outer<$struct_inner<T>> {
|
||||
/// Consumes the matrix and returns its values as an array.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let mat2 = Vector2::new(
|
||||
/// Vector2::new(0, 0),
|
||||
/// Vector2::new(0, 0)
|
||||
/// );
|
||||
/// let array = mat2.to_array_2d();
|
||||
///
|
||||
/// assert_eq!(array, [[0, 0], [0, 0]]);
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn to_array_2d(self) -> [[T; $size_inner]; $size_outer] {
|
||||
[ $(self.$field_outer.to_array()), + ]
|
||||
}
|
||||
|
||||
/// Consumes the matrix and returns its values as a tuple.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let mat2 = Vector2::new(
|
||||
/// Vector2::new(0, 0),
|
||||
/// Vector2::new(0, 0)
|
||||
/// );
|
||||
/// let tuple = mat2.to_tuple_2d();
|
||||
///
|
||||
/// assert_eq!(tuple, ((0, 0), (0, 0)));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub fn to_tuple_2d(self) -> ( $($generic_outer), + ) {
|
||||
( $(self.$field_outer.to_tuple()), + )
|
||||
}
|
||||
|
||||
/// Consumes the matrix and returns a new matrix with the given function applied on each field.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let mat2 = Vector2::new(
|
||||
/// Vector2::new(1, 2),
|
||||
/// Vector2::new(3, 4)
|
||||
/// )
|
||||
/// .map_2d(|i| i * 2);
|
||||
///
|
||||
/// assert_eq!(mat2, Vector2::new(Vector2::new(2, 4), Vector2::new(6, 8)));
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn map_2d<F, U>(self, f: F) -> $struct_outer<$struct_inner<U>>
|
||||
where
|
||||
F: Fn(T) -> U
|
||||
{
|
||||
$struct_outer {
|
||||
$(
|
||||
$field_outer: $crate::matrix_map2d_inner!{f,self,$field_outer,$fields_inner}
|
||||
), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy> $struct_outer<$struct_inner<T>> {
|
||||
/// Constructs a matrix using the given `value` as the value for all of its fields.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use fixed_wide_vectors::Vector2;
|
||||
///
|
||||
/// let mat2 = Vector2::<Vector2<_>>::from_value_2d(0);
|
||||
///
|
||||
/// assert_eq!(mat2, Vector2::new(Vector2::new(0, 0), Vector2::new(0, 0)));
|
||||
/// ```
|
||||
#[inline(always)]
|
||||
pub const fn from_value_2d(value: T) -> Self {
|
||||
Self {
|
||||
$( $field_outer: $struct_inner::from_value(value) ), +
|
||||
}
|
||||
}
|
||||
//TODO: diagonal
|
||||
}
|
||||
|
||||
// Impl floating-point based methods
|
||||
//#[cfg(feature="fixed_wide_traits")]
|
||||
//$crate::impl_wide_matrix_operations!( ($struct_outer { $($field_outer), + }, $size_outer), ($struct_inner, $size_inner), $fields_inner );
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! matrix_map2d_inner {
|
||||
( $f:ident, $value:ident, $field_outer:ident, ($struct_inner: ident { $($field_inner: ident), + }) ) => {
|
||||
$struct_inner {
|
||||
$(
|
||||
$field_inner: $f($value.$field_outer.$field_inner)
|
||||
), +
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
macro_rules! nested {
|
||||
(($($f:ident),*) $args:tt) => {
|
||||
$(nested!(@call $f $args);)*
|
||||
};
|
||||
(@call $f:ident ($($arg:expr),*)) => {
|
||||
$f($($arg),*);
|
||||
};
|
||||
}
|
||||
|
||||
nested! {
|
||||
(show1, show2)
|
||||
(a, b, c)
|
||||
}
|
||||
*/
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export(local_inner_macros)]
|
||||
macro_rules! impl_matrix_operator {
|
||||
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident, $output: ty ) => {
|
||||
impl<T:core::ops::$trait<Output=T>> core::ops::$trait for $struct<T> {
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: Self) -> Self::Output {
|
||||
Self {
|
||||
$( $field: self.$field.$method(other.$field) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T:core::ops::$trait<Output=T>+Copy> core::ops::$trait<T> for $struct<T>{
|
||||
type Output = $output;
|
||||
|
||||
fn $method(self, other: T) -> Self::Output {
|
||||
$struct {
|
||||
$( $field: self.$field.$method(other) ), +
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
( $struct: ident { $($field: ident), + }, $trait: ident, $method: ident ) => {
|
||||
impl<T: core::ops::$trait> core::ops::$trait for $struct<T> {
|
||||
fn $method(&mut self, other: Self) {
|
||||
$( self.$field.$method(other.$field) ); +
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: core::ops::$trait + Copy> core::ops::$trait<T> for $struct<T> {
|
||||
fn $method(&mut self, other: T) {
|
||||
$( self.$field.$method(other) ); +
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -2,3 +2,4 @@
|
||||
pub mod wide;
|
||||
|
||||
pub mod vector;
|
||||
pub mod matrix;
|
||||
|
@ -66,3 +66,13 @@ pub struct Vector4<T> {
|
||||
crate::impl_vector!(Vector2 { x, y }, (T, T), 2);
|
||||
crate::impl_vector!(Vector3 { x, y, z }, (T, T, T), 3);
|
||||
crate::impl_vector!(Vector4 { x, y, z, w }, (T, T, T, T), 4);
|
||||
|
||||
crate::impl_matrix!((Vector2 { x, y }, ((T, T), (T, T)), 2), (Vector2, 2), (Vector2 { x, y }) );
|
||||
crate::impl_matrix!((Vector2 { x, y }, ((T, T, T), (T, T, T)), 2), (Vector3, 3), (Vector3 { x, y, z }) );
|
||||
crate::impl_matrix!((Vector2 { x, y }, ((T, T, T, T), (T, T, T, T)), 2), (Vector4, 4), (Vector4 { x, y, z, w }) );
|
||||
crate::impl_matrix!((Vector3 { x, y, z }, ((T, T), (T, T), (T, T)), 3), (Vector2, 2), (Vector2 { x, y }) );
|
||||
crate::impl_matrix!((Vector3 { x, y, z }, ((T, T, T), (T, T, T), (T, T, T)), 3), (Vector3, 3), (Vector3 { x, y, z }) );
|
||||
crate::impl_matrix!((Vector3 { x, y, z }, ((T, T, T, T), (T, T, T, T), (T, T, T, T)), 3), (Vector4, 4), (Vector4 { x, y, z, w }) );
|
||||
crate::impl_matrix!((Vector4 { x, y, z, w }, ((T, T), (T, T), (T, T), (T, T)), 4), (Vector2, 2), (Vector2 { x, y }) );
|
||||
crate::impl_matrix!((Vector4 { x, y, z, w }, ((T, T, T), (T, T, T), (T, T, T), (T, T, T)), 4), (Vector3, 3), (Vector3 { x, y, z }) );
|
||||
crate::impl_matrix!((Vector4 { x, y, z, w }, ((T, T, T, T), (T, T, T, T), (T, T, T, T), (T, T, T, T)), 4), (Vector4, 4), (Vector4 { x, y, z, w }) );
|
||||
|
Loading…
Reference in New Issue
Block a user