delete everything and start over

This commit is contained in:
Quaternions 2024-09-05 12:49:20 -07:00
parent 103697fbdd
commit c3026c67e9
9 changed files with 14 additions and 953 deletions

View File

@ -11,4 +11,3 @@ zeroes=["ratio","dep:arrayvec"]
[dependencies] [dependencies]
bnum = "0.11.0" bnum = "0.11.0"
arrayvec = { version = "0.7.6", optional = true } arrayvec = { version = "0.7.6", optional = true }
paste = "1.0.15"

View File

@ -4,9 +4,9 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[features] [features]
default=["fixed_wide"] default=["fixed_wide","named-fields"]
named-fields=[]
fixed_wide=["dep:fixed_wide"] fixed_wide=["dep:fixed_wide"]
[dependencies] [dependencies]
fixed_wide = { version = "0.1.0", path = "../fixed_wide", optional = true } fixed_wide = { version = "0.1.0", path = "../fixed_wide", optional = true }
paste = "1.0.15"

View File

@ -1,138 +1 @@
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_common {
( $struct: ident { $($field: ident), + }, $size: expr ) => {
impl<T> $struct<T> {
/// Constructs a new vector with the specified values for each field.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector2;
///
/// let vec2 = Vector2::new(0, 0);
///
/// assert_eq!(vec2.x, 0);
/// assert_eq!(vec2.y, 0);
/// ```
#[inline(always)]
pub const fn new( $($field: T), + ) -> Self {
Self {
$( $field ), +
}
}
/// Consumes the vector and returns its values as an array.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector2;
///
/// let vec2 = Vector2::new(0, 0);
/// let array = vec2.to_array();
///
/// assert_eq!(array, [0, 0]);
/// ```
#[inline(always)]
pub fn to_array(self) -> [T; $size] {
[ $(self.$field), + ]
}
/// Consumes the vector and returns a new vector with the given function applied on each field.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector2;
///
/// let vec2 = Vector2::new(1, 2)
/// .map(|i| i * 2);
///
/// assert_eq!(vec2, Vector2::new(2, 4));
/// ```
#[inline]
pub fn map<F, U>(self, f: F) -> $struct<U>
where
F: Fn(T) -> U
{
$struct {
$( $field: f(self.$field) ), +
}
}
}
impl<T: Copy> $struct<T> {
/// Constructs a vector using the given `value` as the value for all of its fields.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector2;
///
/// let vec2 = Vector2::from_value(0);
///
/// assert_eq!(vec2, Vector2::new(0, 0));
/// ```
#[inline(always)]
pub const fn from_value(value: T) -> Self {
Self {
$( $field: value ), +
}
}
}
impl<T> From<[T; $size]> for $struct<T> {
fn from(from: [T; $size]) -> Self {
let mut iterator = from.into_iter();
Self {
// SAFETY: We know the size of `from` so `iterator.next()` is always `Some(..)`
$( $field: unsafe { iterator.next().unwrap_unchecked() } ), +
}
}
}
impl<T: core::fmt::Debug> core::fmt::Debug for $struct<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let identifier = core::stringify!($struct);
f.debug_struct(identifier)
$( .field( core::stringify!($field), &self.$field ) ) +
.finish()
}
}
impl<T: PartialEq> PartialEq for $struct<T> {
fn eq(&self, other: &Self) -> bool {
$( self.$field == other.$field ) && +
}
}
impl<T: Eq> Eq for $struct<T> { }
impl<T: core::hash::Hash> core::hash::Hash for $struct<T> {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
$( self.$field.hash(state); ) +
}
}
impl<T: Clone> Clone for $struct<T> {
fn clone(&self) -> Self {
Self {
$( $field: self.$field.clone() ), +
}
}
}
impl<T: Copy> Copy for $struct<T> { }
impl<T: Default> Default for $struct<T> {
fn default() -> Self {
Self {
$( $field: T::default() ), +
}
}
}
}
}

View File

@ -1,370 +1 @@
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_vector_operations_2arg_not_const_generic {
(
($struct: ident { $($field: ident), + }, $size: expr),
($lhs:expr, $rhs:expr)
) => {
impl $struct<fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{
paste::item!{
#[inline]
pub fn [<wide_mul_ $lhs _ $rhs>](self,rhs:$struct<fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->$struct<fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{
$struct{
$( $field: self.$field.[<wide_mul_ $lhs _ $rhs>](rhs.$field) ), +
}
}
#[inline]
pub fn [<wide_dot_ $lhs _ $rhs>](self,rhs:$struct<fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>{
$crate::sum_repeating!(
$( + (self.$field.[<wide_mul_ $lhs _ $rhs>](rhs.$field)) ) +
)
}
}
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_vector_operations_1arg_not_const_generic {
(
($struct: ident { $($field: ident), + }, $size: expr),
$n:expr
) => {
impl $struct<fixed_wide::fixed::Fixed<{$n},{$n*32}>>{
paste::item!{
#[inline]
pub fn wide_length_squared(&self)->fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>{
$crate::sum_repeating!(
$( + self.$field.[<wide_mul_ $n _ $n>](self.$field) ) +
)
}
}
}
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! do_macro_8x8{
(
$macro:ident,
$any:tt
)=>{
$crate::macro_repeated!($macro, $any,
(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),
(1,2),(2,2),(3,2),(4,2),(5,2),(6,2),(7,2),(8,2),
(1,3),(2,3),(3,3),(4,3),(5,3),(6,3),(7,3),(8,3),
(1,4),(2,4),(3,4),(4,4),(5,4),(6,4),(7,4),(8,4),
(1,5),(2,5),(3,5),(4,5),(5,5),(6,5),(7,5),(8,5),
(1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6),
(1,7),(2,7),(3,7),(4,7),(5,7),(6,7),(7,7),(8,7),
(1,8),(2,8),(3,8),(4,8),(5,8),(6,8),(7,8),(8,8)
);
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! do_macro_8{
(
$macro:ident,
$any:tt
)=>{
$crate::macro_repeated!($macro, $any, 1,2,3,4,5,6,7,8);
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_vector_operations {
( $struct: ident { $($field: ident), + }, $size: expr ) => {
$crate::do_macro_8!(impl_wide_vector_operations_1arg_not_const_generic,($struct { $($field), + }, $size));
$crate::do_macro_8x8!(impl_wide_vector_operations_2arg_not_const_generic,($struct { $($field), + }, $size));
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_vector_3_wide_cross {
(
(),
($lhs:expr, $rhs:expr)
)=>{
impl Vector3<fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>{
paste::item!{
#[inline]
pub fn [<wide_cross_ $lhs _ $rhs>](self,rhs:Vector3<fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>)->Vector3<fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>{
Vector3{
x:self.y.[<wide_mul_ $lhs _ $rhs>](rhs.z)-self.z.[<wide_mul_ $lhs _ $rhs>](rhs.y),
y:self.z.[<wide_mul_ $lhs _ $rhs>](rhs.x)-self.x.[<wide_mul_ $lhs _ $rhs>](rhs.z),
z:self.x.[<wide_mul_ $lhs _ $rhs>](rhs.y)-self.y.[<wide_mul_ $lhs _ $rhs>](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 {
(
$lhs_axis:expr, $wide_mul:ident, $rhs:ident,
($struct: ident { $($field: ident), + }),
($from_struct: ident { $($from_field: ident), + }),
$static_field: ident
) => {
$crate::sum_repeating!(
$( + $lhs_axis.$field.$wide_mul($rhs.$from_field.$static_field) ) +
)
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_dot_inner {
(
// MatY<VecX>.MatX<VecZ> = MatY<VecZ>
$lhs:ident, $lhs_field_outer:ident, $wide_mul:ident, $rhs:ident,
$struct_inner_thru: tt, //VecX
($struct_inner: ident { $($field_inner: ident), + }), //VecX
($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }), //VecZ
$rhs_outer: tt //MatX
) => {
$rhs_struct_inner {
$(
//directly dot product to avoid a copy
$rhs_field_inner: $crate::impl_matrix_wide_dot_transpose_helper!{
//lhs.axis.wide_mul(rhs_t.axis)
$lhs.$lhs_field_outer,$wide_mul,$rhs,
$struct_inner_thru, //VecZ
$rhs_outer, //MatX
$rhs_field_inner
}
), +
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_dot_outer {
(
// MatY<VecX>.MatX<VecZ> = MatY<VecZ>
$lhs:ident, $wide_mul:ident, $rhs:ident,
//result matrix shape
($struct_outer: ident { $($field_outer: ident), + }),//MatY
$rhs_struct_inner: tt,//VecZ
//inner loop shape
$struct_inner: tt,//VecX
$rhs_matrix: tt//MatX
) => {
$struct_outer {
$(
$field_outer: $crate::impl_matrix_wide_dot_inner!{
$lhs, $field_outer, $wide_mul, $rhs,
$struct_inner, //VecX
$struct_inner, //VecX
$rhs_struct_inner, //VecZ
$rhs_matrix //MatX
}
), +
}
}
}
// Notes:
// Mat3<Vec2>.dot(Vec2) -> Vec3
// lhs.dot(rhs) -> out
// lhs = Mat3<Vec4>
// rhs = Mat4<Vec2>
// out = Mat3<Vec2>
// Mat3<Vec4>.dot(Mat4<Vec2>) -> Mat3<Vec2>
// how to matrix multiply:
// RHS TRANSPOSE
// Mat4<Vec2> -> Mat2<Vec4>
// rhs_t = Mat2<Vec4>
// inner loop:
// out[y][x] = lhs[y].dot(rhs_t[x])
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_dot {
(
($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr),
($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr),
($rhs_struct_inner: ident { $($rhs_field_inner: ident), + }, $rhs_matrix_inner: ident { $($rhs_matrix_field_inner: ident), + }, $rhs_size_inner: expr),
($lhs: expr, $rhs: expr)
) => {
impl $struct_outer<$struct_inner<fixed_wide::fixed::Fixed<{$lhs},{$lhs*32}>>>{
paste::item!{
#[inline]
pub fn [<wide_dot_ $size_outer x $size_inner _ $size_inner x $rhs_size_inner _ $lhs _ $rhs>](self,rhs:$matrix_inner<$rhs_struct_inner<fixed_wide::fixed::Fixed<{$rhs},{$rhs*32}>>>)->$struct_outer<$rhs_struct_inner<fixed_wide::fixed::Fixed<{$lhs+$rhs},{($lhs+$rhs)*32}>>>{
$crate::impl_matrix_wide_dot_outer!(
//constituent idents
self,[<wide_mul_ $lhs _ $rhs>],rhs,
//result matrix shape
($struct_outer { $($field_outer), + }),
($rhs_struct_inner { $($rhs_field_inner), + }),
//inner loop shape
($struct_inner { $($field_inner), + }),
($matrix_inner { $($matrix_field_inner), + })
)
}
}
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_dot_shim {
(
($outer_info:tt,$inner_info:tt,$rhs_info:tt),
($lhs: expr, $rhs: expr)
) => {
$crate::impl_matrix_wide_dot!($outer_info,$inner_info,$rhs_info,($lhs,$rhs));
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_dot_8x8 {
(
($outer_info:tt,$inner_info:tt),
$rhs_info:tt
) => {
$crate::do_macro_8x8!(impl_matrix_wide_dot_shim,($outer_info,$inner_info,$rhs_info));
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_wide_dot_repeat_rhs {
(
($outer_info:tt,($($rhs_info:tt),+)),
$inner_info:tt
) => {
$crate::macro_repeated!(impl_matrix_wide_dot_8x8,($outer_info,$inner_info),$($rhs_info),+);
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_matrix_operations_2arg_not_const_generic {
(
$lhs: expr, $rhs: expr,
($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr),
($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr)
) => {
/* TODO: nasty determinant macro
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> {
#[inline]
pub fn wide_dot(&self) -> U {
$crate::sum_repeating!(
$( + self.$field.wide_mul(self.$field) ) +
)
}
}
*/
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_wide_matrix_operations_1arg_not_const_generic {
(
$n: expr,
($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr),
) => {
/* TODO: nasty determinant macro
impl<U:std::ops::Add<Output=U>,T:Copy+fixed_wide_traits::wide::WideMul<Output=U>> $struct<T> {
#[inline]
pub fn wide_det(&self) -> U {
$crate::sum_repeating!(
$( + self.$field.wide_mul(self.$field) ) +
)
}
}
*/
};
}
#[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<Vector3<fixed_wide::fixed::Fixed<$n,{$n*32}>>>{
paste::item!{
pub fn [<wide_det_3x3_ $n>](self)->fixed_wide::fixed::Fixed<{$n*3},{$n*3*32}>{
//[<wide_dot_ $n _ $n*2>] will not compile, so the doubles are hardcoded above
self.x_axis.[<wide_dot_ $n _ $_2n>](self.y_axis.[<wide_cross_ $n _ $n>](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<Vector3<fixed_wide::fixed::Fixed<$n,{$n*32}>>>{
paste::item!{
pub fn [<wide_adjugate_3x3_ $n>](self)->Matrix3<Vector3<fixed_wide::fixed::Fixed<{$n*2},{$n*2*32}>>>{
Matrix3{
x_axis:Vector3{x:self.y_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.z)-self.y_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.y),y:self.x_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.y)-self.x_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.z),z:self.x_axis.y.[<wide_mul_ $n _ $n>](self.y_axis.z)-self.x_axis.z.[<wide_mul_ $n _ $n>](self.y_axis.y)},
y_axis:Vector3{x:self.y_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.x)-self.y_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.z),y:self.x_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.z)-self.x_axis.z.[<wide_mul_ $n _ $n>](self.z_axis.x),z:self.x_axis.z.[<wide_mul_ $n _ $n>](self.y_axis.x)-self.x_axis.x.[<wide_mul_ $n _ $n>](self.y_axis.z)},
z_axis:Vector3{x:self.y_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.y)-self.y_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.x),y:self.x_axis.y.[<wide_mul_ $n _ $n>](self.z_axis.x)-self.x_axis.x.[<wide_mul_ $n _ $n>](self.z_axis.y),z:self.x_axis.x.[<wide_mul_ $n _ $n>](self.y_axis.y)-self.x_axis.y.[<wide_mul_ $n _ $n>](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)]
#[macro_export(local_inner_macros)]
macro_rules! sum_repeating {
( + $($item: tt) * ) => {
$($item) *
};
}

View File

@ -1,201 +1 @@
// 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), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr)
) => {
$crate::impl_common!($struct_outer { $($field_outer), + }, $size_outer);
impl<U> $struct_outer<U> {
#[inline(always)]
pub fn to_vector(self) -> $vector_outer<U> {
$vector_outer {
$(
$vector_field_outer: self.$field_outer
), +
}
}
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_shim {
(
(),
$matrix_info:tt
) => {
$crate::impl_matrix!($matrix_info);
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrices {
(
($($matrix_info:tt),+),
$vector_infos:tt
) => {
$crate::macro_repeated!(impl_matrix_shim,(),$($matrix_info),+);
$crate::macro_repeated!(impl_matrix_inner_shim,$vector_infos,$($matrix_info),+);
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_inner_shim {
(
($($vector_info:tt),+),
$matrix_info:tt
) => {
$crate::macro_repeated!(impl_matrix_inner,$matrix_info,$($vector_info),+);
#[cfg(feature="fixed_wide")]
$crate::macro_repeated!(impl_matrix_wide_dot_repeat_rhs,($matrix_info,($($vector_info),+)),$($vector_info),+);
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_matrix_inner {
(
($struct_outer: ident { $($field_outer: ident), + }, $vector_outer: ident { $($vector_field_outer: ident), + }, $size_outer: expr),
($struct_inner: ident { $($field_inner: ident), + }, $matrix_inner: ident { $($matrix_field_inner: ident), + }, $size_inner: expr)
) => {
impl<T> $struct_outer<$struct_inner<T>> {
#[inline(always)]
pub fn to_array_2d(self) -> [[T; $size_inner]; $size_outer] {
[ $(self.$field_outer.to_array()), + ]
}
#[inline]
pub fn map_2d<F, U>(self, f: F) -> $struct_outer<$struct_inner<U>>
where
F: Fn(T) -> U
{
$crate::matrix_map2d_outer!{f,self,($struct_outer { $($field_outer), + }),($struct_inner { $($field_inner), + })}
}
#[inline]
pub fn transpose(self) -> $matrix_inner<$vector_outer<T>>{
$crate::matrix_transpose_outer!{self,
($matrix_inner { $($matrix_field_inner), + }),($struct_inner { $($field_inner), + }),
($vector_outer { $($vector_field_outer), + }),($struct_outer { $($field_outer), + })
}
}
}
impl<T: Copy> $struct_outer<$struct_inner<T>> {
#[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_outer {
( $f:ident, $value:ident, ($struct_outer: ident { $($field_outer: ident), + }), $unparsed_inner:tt ) => {
$struct_outer {
$(
$field_outer: $crate::matrix_map2d_inner!{$f,$value,$field_outer,$unparsed_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)
), +
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! matrix_transpose_outer {
(
$value:ident,
($struct_outer: ident { $($field_outer: ident), + }),
($old_outer: ident { $($old_field_outer: ident), + }),
$fields_inner:tt,
$old_fields_inner:tt
) => {
$struct_outer {
$(
$field_outer: $crate::matrix_transpose_inner!{$value,$old_field_outer,$fields_inner,$old_fields_inner}
), +
}
}
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! matrix_transpose_inner {
( $value:ident, $field_outer:ident,
($struct_inner: ident { $($field_inner: ident), + }),
($old_struct_inner: ident { $($old_field_inner: ident), + })
) => {
$struct_inner {
$(
$field_inner: $value.$old_field_inner.$field_outer
), +
}
}
}
#[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 {
( $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) ); +
}
}
};
}

View File

@ -4,17 +4,3 @@ pub mod fixed_wide;
pub mod common; pub mod common;
pub mod vector; pub mod vector;
pub mod matrix; pub mod matrix;
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! macro_repeated{
(
$macro:ident,
$any:tt,
$($repeated:tt),*
)=>{
$(
$crate::$macro!($any, $repeated);
)*
};
}

View File

@ -1,155 +1 @@
// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license)
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_vector {
( $struct: ident { $($field: ident), + }, $size: expr ) => {
$crate::impl_common!($struct { $($field), + }, $size);
impl<T: Ord> $struct<T> {
pub fn min(self, rhs: Self) -> $struct<T> {
$struct{
$( $field: self.$field.min(rhs.$field) ), +
}
}
pub fn max(self, rhs: Self) -> $struct<T> {
$struct{
$( $field: self.$field.max(rhs.$field) ), +
}
}
pub fn cmp(self, rhs: Self) -> $struct<core::cmp::Ordering> {
$struct{
$( $field: self.$field.cmp(&rhs.$field) ), +
}
}
pub fn lt(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.lt(&rhs.$field) ), +
}
}
pub fn gt(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.gt(&rhs.$field) ), +
}
}
pub fn ge(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.ge(&rhs.$field) ), +
}
}
pub fn le(self, rhs: Self) -> $struct<bool> {
$struct{
$( $field: self.$field.le(&rhs.$field) ), +
}
}
}
impl $struct<bool>{
pub fn all(&self)->bool{
const ALL:[bool;$size]=[true;$size];
core::matches!(self.to_array(),ALL)
}
pub fn any(&self)->bool{
$( self.$field )|| +
}
}
impl<T: core::ops::Neg<Output = T>> core::ops::Neg for $struct<T> {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
$( $field: -self.$field ), +
}
}
}
// Impl arithmetic pperators
$crate::impl_vector_operator!( $struct { $($field), + }, AddAssign, add_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, Add, add, Self );
$crate::impl_vector_operator!( $struct { $($field), + }, SubAssign, sub_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, Sub, sub, Self );
$crate::impl_vector_operator!( $struct { $($field), + }, MulAssign, mul_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, Mul, mul, Self );
$crate::impl_vector_operator!( $struct { $($field), + }, DivAssign, div_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, Div, div, Self );
$crate::impl_vector_operator!( $struct { $($field), + }, RemAssign, rem_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, Rem, rem, Self );
// Impl bitwise operators
$crate::impl_vector_operator!( $struct { $($field), + }, BitAndAssign, bitand_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, BitAnd, bitand, Self );
$crate::impl_vector_operator!( $struct { $($field), + }, BitOrAssign, bitor_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, BitOr, bitor, Self );
$crate::impl_vector_operator!( $struct { $($field), + }, BitXorAssign, bitxor_assign );
$crate::impl_vector_operator!( $struct { $($field), + }, BitXor, bitxor, Self );
// Impl floating-point based methods
#[cfg(feature="fixed_wide")]
$crate::impl_wide_vector_operations!( $struct { $($field), + }, $size );
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_extend {
( $struct: ident { $($field: ident), + }, $struct_extended: ident, $field_extended: ident ) => {
impl<T> $struct<T> {
#[inline(always)]
pub fn extend(self,value:T) -> $struct_extended<T> {
$struct_extended {
$( $field:self.$field, ) +
$field_extended:value
}
}
}
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_vector_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) ); +
}
}
};
}
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! impl_vector_3 {
()=>{
#[cfg(feature="fixed_wide")]
$crate::impl_vector_wide_3!();
}
}

View File

@ -16,22 +16,18 @@ pub struct Matrix4<T> {
pub w_axis: T, pub w_axis: T,
} }
crate::impl_extend!(Matrix2 { x_axis, y_axis }, Matrix3, z_axis); crate::impl_matrix_named_fields!(
crate::impl_extend!(Matrix3 { x_axis, y_axis, z_axis }, Matrix4, w_axis); //outer struct
//TODO: extend vertically
crate::impl_matrices!(
//outer struct and equivalent vector
( (
(Matrix2 { x_axis, y_axis }, Vector2 { x, y }, 2), (Matrix2 { x_axis, y_axis }, 2),
(Matrix3 { x_axis, y_axis, z_axis }, Vector3 { x, y, z }, 3), (Matrix3 { x_axis, y_axis, z_axis }, 3),
(Matrix4 { x_axis, y_axis, z_axis, w_axis }, Vector4 { x, y, z, w }, 4) (Matrix4 { x_axis, y_axis, z_axis, w_axis }, 4)
), ),
//inner struct and equivalent matrix //inner struct
( (
(Vector2 { x, y }, Matrix2 { x_axis, y_axis }, 2), (Vector2 { x, y }, 2),
(Vector3 { x, y, z }, Matrix3 { x_axis, y_axis, z_axis }, 3), (Vector3 { x, y, z }, 3),
(Vector4 { x, y, z, w }, Matrix4 { x_axis, y_axis, z_axis, w_axis }, 4) (Vector4 { x, y, z, w }, 4)
) )
); );

View File

@ -1,60 +1,14 @@
// Stolen from https://github.com/c1m50c/fixed-vectors (MIT license)
/// Vector for holding two-dimensional values.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector2;
///
/// let mut vec2 = Vector2::new(1, 2);
/// vec2 += Vector2::new(1, 2);
///
/// assert_eq!(vec2.x, 2);
/// assert_eq!(vec2.y, 4);
/// ```
pub struct Vector2<T> { pub struct Vector2<T> {
pub x: T, pub x: T,
pub y: T, pub y: T,
} }
/// Vector for holding three-dimensional values.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector3;
///
/// let mut vec3 = Vector3::new(1, 2, 3);
/// vec3 += Vector3::new(1, 2, 3);
///
/// assert_eq!(vec3.x, 2);
/// assert_eq!(vec3.y, 4);
/// assert_eq!(vec3.z, 6);
/// ```
pub struct Vector3<T> { pub struct Vector3<T> {
pub x: T, pub x: T,
pub y: T, pub y: T,
pub z: T, pub z: T,
} }
/// Vector for holding four-dimensional values.
///
/// # Example
///
/// ```
/// use fixed_wide_vectors::Vector4;
///
/// let mut vec4 = Vector4::new(1, 2, 3, 4);
/// vec4 += Vector4::new(1, 2, 3, 4);
///
/// assert_eq!(vec4.x, 2);
/// assert_eq!(vec4.y, 4);
/// assert_eq!(vec4.z, 6);
/// assert_eq!(vec4.w, 8);
/// ```
pub struct Vector4<T> { pub struct Vector4<T> {
pub x: T, pub x: T,
pub y: T, pub y: T,
@ -62,23 +16,9 @@ pub struct Vector4<T> {
pub w: T, pub w: T,
} }
crate::impl_vector_named_fields!(Vector2 { x, y }, 2);
crate::impl_vector!(Vector2 { x, y }, 2); crate::impl_vector_named_fields!(Vector3 { x, y, z }, 3);
crate::impl_vector!(Vector3 { x, y, z }, 3); crate::impl_vector_named_fields!(Vector4 { x, y, z, w }, 4);
crate::impl_vector!(Vector4 { x, y, z, w }, 4);
crate::impl_extend!(Vector2 { x, y }, Vector3, z);
crate::impl_extend!(Vector3 { x, y, z }, Vector4, w);
crate::impl_matrix_inner!((Vector2 { x, y }, Vector2 { x, y }, 2), (Vector2 { x, y }, Vector2 { x, y }, 2) );
crate::impl_matrix_inner!((Vector2 { x, y }, Vector2 { x, y }, 2), (Vector3 { x, y, z }, Vector3 { x, y, z }, 3) );
crate::impl_matrix_inner!((Vector2 { x, y }, Vector2 { x, y }, 2), (Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4) );
crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector2 { x, y }, Vector2 { x, y }, 2) );
crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector3 { x, y, z }, Vector3 { x, y, z }, 3) );
crate::impl_matrix_inner!((Vector3 { x, y, z }, Vector3 { x, y, z }, 3), (Vector4 { x, y, z, w }, Vector4 { x, y, z, w }, 4) );
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 //cross product
crate::impl_vector_3!(); crate::impl_vector_3!();