use vector::{Vector2, Vector3, Vector4};
use std::{mem, ptr};
macro_rules! matrix {
($name:ident : $vec:ident[ $($field:ident = $index:expr),* ] = ($inner:expr, $outer:expr)) => {
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Eq, Ord)]
#[repr(C)]
#[allow(missing_docs)]
pub struct $name<T> {
$( pub $field : $vec<T>, )*
}
impl<T: Copy> From<[[T; $inner]; $outer]> for $name<T> {
fn from(m: [[T; $inner]; $outer]) -> Self {
$name {
$(
$field: m[$index].into(),
)*
}
}
}
impl<T> Into<[[T; $inner]; $outer]> for $name<T> {
fn into(self) -> [[T; $inner]; $outer] {
[$( self.$field.into() ),*]
}
}
impl<T> AsRef<[[T; $inner]; $outer]> for $name<T> {
fn as_ref(&self) -> &[[T; $inner]; $outer] { unsafe { mem::transmute(self) } }
}
impl<T: Clone> From<[T; $inner * $outer]> for $name<T> {
fn from(m: [T; $inner * $outer]) -> Self {
$name {
$(
$field: $vec::from_slice(&m[$index*$inner..($index+1)*$inner]),
)*
}
}
}
impl<T: Copy> Into<[T; $inner * $outer]> for $name<T> {
fn into(self) -> [T; $inner * $outer] {
unsafe {
let mut out: [T; $inner * $outer] = mem::uninitialized();
$(
ptr::copy_nonoverlapping(self.$field.as_ref().as_ptr(), out[$index*$inner..($index+1)*$inner].as_mut_ptr(), $inner);
)*
out
}
}
}
impl<T> AsRef<[T; $inner * $outer]> for $name<T> {
fn as_ref(&self) -> &[T; $inner * $outer] { unsafe { mem::transmute(self) } }
}
};
}
macro_rules! turn {
($name:ident : $vec:ident[$( $field:ident [ $($sub:ident),* ] ),* ] = $transposed:ident) => {
impl<T> From<$transposed<T>> for $name<T> {
fn from(m: $transposed<T>) -> Self {
$name {
$(
$field: $vec {
$(
$sub: m.$sub.$field,
)*
},
)*
}
}
}
}
}
matrix!( RowMatrix2 : Vector2[x=0,y=1] = (2, 2));
turn!( RowMatrix2 : Vector2[x[x,y],y[x,y]] = ColumnMatrix2 );
matrix!( RowMatrix2x3 : Vector3[x=0,y=1] = (3, 2));
turn!( RowMatrix2x3 : Vector3[x[x,y,z],y[x,y,z]] = ColumnMatrix2x3 );
matrix!( RowMatrix3x2 : Vector2[x=0,y=1,z=2] = (2, 3));
turn!( RowMatrix3x2 : Vector2[x[x,y],y[x,y],z[x,y]] = ColumnMatrix3x2 );
matrix!( RowMatrix3 : Vector3[x=0,y=1,z=2] = (3, 3));
turn!( RowMatrix3 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z]] = ColumnMatrix3 );
matrix!( RowMatrix3x4 : Vector4[x=0,y=1,z=2] = (4, 3));
turn!( RowMatrix3x4 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w]] = ColumnMatrix3x4 );
matrix!( RowMatrix4x3 : Vector3[x=0,y=1,z=2,w=3] = (3, 4));
turn!( RowMatrix4x3 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z],w[x,y,z]] = ColumnMatrix4x3 );
matrix!( RowMatrix4 : Vector4[x=0,y=1,z=2,w=3] = (4, 4));
turn!( RowMatrix4 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w],w[x,y,z,w]] = ColumnMatrix4 );
matrix!( ColumnMatrix2 : Vector2[x=0,y=1] = (2, 2));
turn!( ColumnMatrix2 : Vector2[x[x,y],y[x,y]] = RowMatrix2 );
matrix!( ColumnMatrix2x3 : Vector2[x=0,y=1,z=2] = (2, 3));
turn!( ColumnMatrix2x3 : Vector2[x[x,y],y[x,y],z[x,y]] = RowMatrix2x3 );
matrix!( ColumnMatrix3x2 : Vector3[x=0,y=1] = (3, 2));
turn!( ColumnMatrix3x2 : Vector3[x[x,y,z],y[x,y,z]] = RowMatrix3x2 );
matrix!( ColumnMatrix3 : Vector3[x=0,y=1,z=2] = (3, 3));
turn!( ColumnMatrix3 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z]] = RowMatrix3 );
matrix!( ColumnMatrix3x4 : Vector3[x=0,y=1,z=2,w=3] = (3, 4));
turn!( ColumnMatrix3x4 : Vector3[x[x,y,z],y[x,y,z],z[x,y,z],w[x,y,z]] = RowMatrix3x4 );
matrix!( ColumnMatrix4x3 : Vector4[x=0,y=1,z=2] = (4, 3));
turn!( ColumnMatrix4x3 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w]] = RowMatrix4x3 );
matrix!( ColumnMatrix4 : Vector4[x=0,y=1,z=2,w=3] = (4, 4));
turn!( ColumnMatrix4 : Vector4[x[x,y,z,w],y[x,y,z,w],z[x,y,z,w],w[x,y,z,w]] = RowMatrix4 );