1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Component access for vectors and constructors from components.

use core::ops::{Deref, DerefMut};

use crate::{Matrix, RowVector, Vector};

////////////////////////////////////////////////////////////////////////////////
// Accessors
////////////////////////////////////////////////////////////////////////////////

macro_rules! struct_coord {
    ($Coord:ident: $($comp:ident),*) => {
        #[allow(clippy::upper_case_acronyms)]
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[repr(C)]
        pub struct $Coord<T> {
            $(pub $comp: T),*
        }
    };
}

macro_rules! impl_deref {
    (($M:literal, $N:literal) -> $Target:ident) => {
        impl<T> Deref for Matrix<T, $M, $N> {
            type Target = $Target<T>;

            #[inline]
            fn deref(&self) -> &Self::Target {
                let ptr = self.as_ptr() as *const $Target<T>;
                unsafe { &*ptr }
            }
        }

        impl<T> DerefMut for Matrix<T, $M, $N> {
            #[inline]
            fn deref_mut(&mut self) -> &mut Self::Target {
                let ptr = self.as_mut_ptr() as *mut $Target<T>;
                unsafe { &mut *ptr }
            }
        }
    };
}

struct_coord! { X: x }
struct_coord! { XY: x, y }
struct_coord! { XYZ: x, y, z }
struct_coord! { XYZW: x, y, z, w }
struct_coord! { XYZWA: x, y, z, w, a }
struct_coord! { XYZWAB: x, y, z, w, a, b }

// SAFETY: given ($M, $N) -> $Target
//         - $Target should be marked #[repr(C)].
//         - $Target<T> should be the same size as [T; $N].
// row vectors
impl_deref! { (1, 1) -> X }
impl_deref! { (1, 2) -> XY }
impl_deref! { (1, 3) -> XYZ }
impl_deref! { (1, 4) -> XYZW }
impl_deref! { (1, 5) -> XYZWA }
impl_deref! { (1, 6) -> XYZWAB }
// column vectors
impl_deref! { (2, 1) -> XY }
impl_deref! { (3, 1) -> XYZ }
impl_deref! { (4, 1) -> XYZW }
impl_deref! { (5, 1) -> XYZWA }
impl_deref! { (6, 1) -> XYZWAB }

////////////////////////////////////////////////////////////////////////////////
// Constructors
////////////////////////////////////////////////////////////////////////////////

/// A macro for composing row vectors.
#[macro_export]
macro_rules! row_vector {
    ($repeat:expr; $n:expr) => {
        $crate::RowVector::from_column_major_order([[$repeat]; $n])
    };
    ($($value:expr),* $(,)?) => {
        $crate::RowVector::from_column_major_order([$([$value]),*])
    };
}

/// A macro for composing vectors.
#[macro_export]
macro_rules! vector {
    ($repeat:expr; $n:expr) => {
        $crate::Vector::from_column_major_order([[$repeat; $n]])
    };
    ($($value:expr),* $(,)?) => {
        $crate::Vector::from_column_major_order([[$($value),*]])
    };
}

macro_rules! impl_row_vector {
    ($N:literal: $($comp:ident),+) => {
        impl<T> RowVector<T, $N> {
            /// Creates a new vector from the given components.
            pub const fn new($($comp: T),+) -> Self {
                Self { data: [$([$comp]),+]}
            }
        }

        impl<T> From<[T; $N]> for RowVector<T, $N>  {
            fn from([$($comp),*]: [T; $N]) -> Self {
                Self { data: [$([$comp]),+] }
            }
        }
    }
}

macro_rules! impl_vector {
    ($M:literal: $($comp:ident),+) => {
        impl<T> Vector<T, $M> {
            /// Creates a new vector from the given components.
            pub const fn new($($comp: T),+) -> Self {
                Self { data: [[$($comp),+]]}
            }
        }

        impl<T> From<[T; $M]> for Vector<T, $M> {
            fn from(data: [T; $M]) -> Self {
                Self { data: [data] }
            }
        }
    }
}

impl_row_vector! { 1: x }
impl_row_vector! { 2: x, y }
impl_row_vector! { 3: x, y, z }
impl_row_vector! { 4: x, y, z, w }
impl_row_vector! { 5: x, y, z, w, a }
impl_row_vector! { 6: x, y, z, w, a, b }

impl_vector! { 2: x, y }
impl_vector! { 3: x, y, z }
impl_vector! { 4: x, y, z, w }
impl_vector! { 5: x, y, z, w, a }
impl_vector! { 6: x, y, z, w, a, b }