mint/
matrix.rs

1use crate::vector::{Vector2, Vector3, Vector4};
2use crate::IntoMint;
3
4macro_rules! matrix {
5    ($name:ident : $vec:ident[ $($field:ident[$($sub:ident),*] = $index:expr),* ] = ($inner:expr, $outer:expr)) => {
6        #[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
7        #[repr(C)]
8        #[allow(missing_docs)] //TODO: actually have docs
9        pub struct $name<T> {
10            $( pub $field : $vec<T>, )*
11        }
12
13        impl<T> IntoMint for $name<T> {
14            type MintType = $name<T>;
15        }
16
17        impl<T> From<[[T; $inner]; $outer]> for $name<T> {
18            fn from([$($field),*]: [[T; $inner]; $outer]) -> Self {
19                $name {
20                    $(
21                        $field: From::from($field),
22                    )*
23                }
24            }
25        }
26
27        impl<T> From<$name<T>> for [[T; $inner]; $outer] {
28            fn from(name: $name<T>) -> [[T; $inner]; $outer] {
29                [$( name.$field.into() ),*]
30            }
31        }
32
33        impl<T> AsRef<[[T; $inner]; $outer]> for $name<T> {
34            fn as_ref(&self) -> &[[T; $inner]; $outer] { unsafe { ::core::mem::transmute(self) } }
35        }
36
37        impl<T> AsMut<[[T; $inner]; $outer]> for $name<T> {
38            fn as_mut(&mut self) -> &mut [[T; $inner]; $outer] { unsafe { ::core::mem::transmute(self) } }
39        }
40
41        impl<T: Clone> From<[T; $inner * $outer]> for $name<T> {
42            fn from(m: [T; $inner * $outer]) -> Self {
43                $name {
44                    $(
45                        $field: $vec::from_slice(&m[$index*$inner..($index+1)*$inner]),
46                    )*
47                }
48            }
49        }
50
51        impl<T> From<$name<T>> for [T; $inner * $outer] {
52            fn from(name: $name<T>) -> [T; $inner * $outer] {
53                let $name { $($field),* } = name;
54                [
55                    $( $( $field.$sub ),* ),*
56                ]
57            }
58        }
59
60        impl<T> AsRef<[T; $inner * $outer]> for $name<T> {
61            fn as_ref(&self) -> &[T; $inner * $outer] { unsafe { ::core::mem::transmute(self) } }
62        }
63
64        impl<T> AsMut<[T; $inner * $outer]> for $name<T> {
65            fn as_mut(&mut self) -> &mut [T; $inner * $outer] { unsafe { ::core::mem::transmute(self) } }
66        }
67
68        #[cfg(feature = "serde")]
69        impl<T> ::serde::Serialize for $name<T>
70            where T: ::serde::Serialize
71        {
72            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
73                where S: ::serde::Serializer
74            {
75                AsRef::<[[T; $inner]; $outer]>::as_ref(self).serialize(serializer)
76            }
77        }
78
79        #[cfg(feature = "serde")]
80        impl<'de, T> ::serde::Deserialize<'de> for $name<T>
81            where T: ::serde::Deserialize<'de>
82        {
83            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
84                where D: ::serde::Deserializer<'de>
85            {
86                <[[T; $inner]; $outer]>::deserialize(deserializer).map($name::<T>::from)
87            }
88        }
89    };
90}
91
92macro_rules! turn {
93    ($name:ident : $vec:ident[$( $field:ident [ $($sub:ident),* ]  ),* ] = $transposed:ident) => {
94        impl<T> From<$transposed<T>> for $name<T> {
95            fn from(m: $transposed<T>) -> Self {
96                $name {
97                    $(
98                        $field: $vec {
99                            $(
100                                $sub: m.$sub.$field,
101                            )*
102                        },
103                    )*
104                }
105            }
106        }
107    }
108}
109
110// 2x2 row-major matrix.
111matrix!( RowMatrix2 : Vector2[x[x,y]=0,y[x,y]=1] = (2, 2));
112turn!( RowMatrix2 : Vector2[x[x,y],y[x,y]] = ColumnMatrix2 );
113// 2x3 row-major matrix.
114// Useful for combining rotation, scale, and translation in 2D space.
115matrix!( RowMatrix2x3 : Vector3[x[x,y,z]=0,y[x,y,z]=1] = (3, 2));
116turn!( RowMatrix2x3 : Vector3[x[x,y,z],y[x,y,z]] = ColumnMatrix2x3 );
117// 2x4 row-major matrix.
118matrix!( RowMatrix2x4 : Vector4[x[x,y,z,w]=0,y[x,y,z,w]=1] = (4, 2));
119turn!( RowMatrix2x4 : Vector4[x[x,y,z,w],y[x,y,z,w]] = ColumnMatrix2x4 );
120// 3x2 row-major matrix.
121// Useful for combining rotation, scale, and translation in 2D space.
122matrix!( RowMatrix3x2 : Vector2[x[x,y]=0,y[x,y]=1,z[x,y]=2] = (2, 3));
123turn!( RowMatrix3x2 : Vector2[x[x,y],y[x,y],z[x,y]] = ColumnMatrix3x2 );
124// 3x3 row-major matrix.
125// Useful for representing rotation and scale in 3D space.
126matrix!( RowMatrix3 : Vector3[x[x,y,z]=0,y[x,y,z]=1,z[x,y,z]=2] = (3, 3));
127turn!( RowMatrix3 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z]] = ColumnMatrix3 );
128// 3x4 row-major matrix.
129// Useful for combining rotation, scale, and translation in 3D space.
130matrix!( RowMatrix3x4 : Vector4[x[x,y,z,w]=0,y[x,y,z,w]=1,z[x,y,z,w]=2] = (4, 3));
131turn!( RowMatrix3x4 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w]] = ColumnMatrix3x4 );
132// 4x3 row-major matrix.
133// Useful for combining rotation, scale, and translation in 3D space.
134matrix!( RowMatrix4x3 : Vector3[x[x,y,z]=0,y[x,y,z]=1,z[x,y,z]=2,w[x,y,z]=3] = (3, 4));
135turn!( RowMatrix4x3 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z],w[x,y,z]] = ColumnMatrix4x3 );
136// 4x2 row-major matrix.
137matrix!( RowMatrix4x2 : Vector2[x[x,y]=0,y[x,y]=1,z[x,y]=2,w[x,y]=3] = (2, 4));
138turn!( RowMatrix4x2 : Vector2[x[x,y],y[x,y],z[x,y],w[x,y]] = ColumnMatrix4x2 );
139// 4x4 row-major matrix.
140matrix!( RowMatrix4 : Vector4[x[x,y,z,w]=0,y[x,y,z,w]=1,z[x,y,z,w]=2,w[x,y,z,w]=3] = (4, 4));
141turn!( RowMatrix4 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w],w[x,y,z,w]] = ColumnMatrix4 );
142
143// 2x2 column-major matrix.
144matrix!( ColumnMatrix2 : Vector2[x[x,y]=0,y[x,y]=1] = (2, 2));
145turn!( ColumnMatrix2 : Vector2[x[x,y],y[x,y]] = RowMatrix2 );
146// 2x3 column-major matrix.
147// Useful for combining rotation, scale, and translation in 2D space.
148matrix!( ColumnMatrix2x3 : Vector2[x[x,y]=0,y[x,y]=1,z[x,y]=2] = (2, 3));
149turn!( ColumnMatrix2x3 : Vector2[x[x,y],y[x,y],z[x,y]] = RowMatrix2x3 );
150// 2x4 column-major matrix.
151matrix!( ColumnMatrix2x4 : Vector2[x[x,y]=0,y[x,y]=1,z[x,y]=2,w[x,y]=3] = (2, 4));
152turn!( ColumnMatrix2x4 : Vector2[x[x,y],y[x,y],z[x,y],w[x,y]] = RowMatrix2x4 );
153// 3x2 column-major matrix.
154// Useful for combining rotation, scale, and translation in 2D space.
155matrix!( ColumnMatrix3x2 : Vector3[x[x,y,z]=0,y[x,y,z]=1] = (3, 2));
156turn!( ColumnMatrix3x2 : Vector3[x[x,y,z],y[x,y,z]] = RowMatrix3x2 );
157// 3x3 column-major matrix.
158// Useful for representing rotation and scale in 3D space.
159matrix!( ColumnMatrix3 : Vector3[x[x,y,z]=0,y[x,y,z]=1,z[x,y,z]=2] = (3, 3));
160turn!( ColumnMatrix3 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z]] = RowMatrix3 );
161// 3x4 column-major matrix.
162// Useful for combining rotation, scale, and translation in 3D space.
163matrix!( ColumnMatrix3x4 : Vector3[x[x,y,z]=0,y[x,y,z]=1,z[x,y,z]=2,w[x,y,z]=3] = (3, 4));
164turn!( ColumnMatrix3x4 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z],w[x,y,z]] = RowMatrix3x4 );
165// 4x2 column-major matrix.
166matrix!( ColumnMatrix4x2 : Vector4[x[x,y,z,w]=0,y[x,y,z,w]=1] = (4, 2));
167turn!( ColumnMatrix4x2 : Vector4[x[x,y,z,w],y[x,y,z,w]] = RowMatrix4x2 );
168// 4x3 column-major matrix.
169// Useful for combining rotation, scale, and translation in 3D space.
170matrix!( ColumnMatrix4x3 : Vector4[x[x,y,z,w]=0,y[x,y,z,w]=1,z[x,y,z,w]=2] = (4, 3));
171turn!( ColumnMatrix4x3 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w]] = RowMatrix4x3 );
172// 4x4 column-major matrix.
173matrix!( ColumnMatrix4 : Vector4[x[x,y,z,w]=0,y[x,y,z,w]=1,z[x,y,z,w]=2,w[x,y,z,w]=3] = (4, 4));
174turn!( ColumnMatrix4 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w],w[x,y,z,w]] = RowMatrix4 );