Skip to content

Commit

Permalink
- tidy up Mat43 type
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Mar 23, 2023
1 parent 088fcc3 commit 6863f6d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod num;
pub mod vec;

/// multi dimensional row-major matrix with generic mat2, mat3, mat34 and mat4 implementations
/// there is a column-major mat43 type which can be transposed from mat34 but it does not support the full functionality
/// there is a mat43 type for mat34 transposes but it does not support the full functionality
pub mod mat;

/// generic quaternion for varying floating point precision
Expand Down
25 changes: 4 additions & 21 deletions src/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ use std::fmt::Formatter;
/// 08 09 10 11
/// 12 13 14 15
/// Create a partial matrix class, this is the concreate matrix struct and indexing operations.
/// the partial implementation allows data only (no arithmetic or traits) to be created
/// to facilitate 3x4 to 4x3 trasposes for interoperation with other API's and shaders
macro_rules! mat_partial_impl {
/// creates the basic generic traits for row-major matrices
macro_rules! mat_impl {
($MatN:ident, $rows:expr, $cols:expr, $elems:expr,
$RowVecN:ident { $($row_field:ident, $row_field_index:expr),* },
$ColVecN:ident { $($col_field:ident, $col_field_index:expr),* } ) => {
Expand Down Expand Up @@ -147,14 +145,7 @@ macro_rules! mat_partial_impl {
true
}
}
}
}

/// creates the basic generic traits for row-major matrices
macro_rules! mat_impl {
($MatN:ident, $rows:expr, $cols:expr, $elems:expr,
$RowVecN:ident { $($row_field:ident, $row_field_index:expr),* },
$ColVecN:ident { $($col_field:ident, $col_field_index:expr),* } ) => {

impl<T> $MatN<T> where T: Number {
/// initialise matrix to all zero's
pub fn zero() -> $MatN<T> {
Expand Down Expand Up @@ -1626,16 +1617,8 @@ impl<T> MatNew4<T> for Mat4<T> where T: Number {
}
}

mat_partial_impl!(Mat2, 2, 2, 4, Vec2 {x, 0, y, 1}, Vec2 {x, 0, y, 1});
mat_impl!(Mat2, 2, 2, 4, Vec2 {x, 0, y, 1}, Vec2 {x, 0, y, 1});

mat_partial_impl!(Mat3, 3, 3, 9, Vec3 {x, 0, y, 1, z, 2}, Vec3 {x, 0, y, 1, z, 2});
mat_impl!(Mat3, 3, 3, 9, Vec3 {x, 0, y, 1, z, 2}, Vec3 {x, 0, y, 1, z, 2});

mat_partial_impl!(Mat4, 4, 4, 16, Vec4 {x, 0, y, 1, z, 2, w, 3}, Vec4 {x, 0, y, 1, z, 2, w, 3});
mat_impl!(Mat4, 4, 4, 16, Vec4 {x, 0, y, 1, z, 2, w, 3}, Vec4 {x, 0, y, 1, z, 2, w, 3});

mat_partial_impl!(Mat34, 3, 4, 12, Vec4 {x, 0, y, 1, z, 2, w, 3}, Vec3 {x, 0, y, 1, z, 2});
mat_impl!(Mat34, 3, 4, 12, Vec4 {x, 0, y, 1, z, 2, w, 3}, Vec3 {x, 0, y, 1, z, 2});

mat_partial_impl!(Mat43, 4, 3, 12, Vec4 {x, 0, y, 1, z, 2}, Vec3 {x, 0, y, 1, z, 2, w, 3});
mat_impl!(Mat43, 4, 3, 12, Vec3 {x, 0, y, 1, z, 2}, Vec4 {x, 0, y, 1, z, 2, w, 3});
6 changes: 6 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,12 @@ fn matrix_identity() {
assert_eq!(m34.get_row(0), vec4f(1.0, 0.0, 0.0, 0.0));
assert_eq!(m34.get_row(1), vec4f(0.0, 1.0, 0.0, 0.0));
assert_eq!(m34.get_row(2), vec4f(0.0, 0.0, 1.0, 0.0));

let m43 = Mat43f::identity();
assert_eq!(m43.get_row(0), vec3f(1.0, 0.0, 0.0));
assert_eq!(m43.get_row(1), vec3f(0.0, 1.0, 0.0));
assert_eq!(m43.get_row(2), vec3f(0.0, 0.0, 1.0));
assert_eq!(m43.get_row(3), vec3f(0.0, 0.0, 0.0));
}

#[test]
Expand Down

0 comments on commit 6863f6d

Please sign in to comment.