From d1aaf4db9468cfafe10cffcaea48ee2908f6b05f Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 19 Sep 2024 02:01:18 -0400 Subject: [PATCH] working on generalized matrix --- CMakeLists.txt | 2 +- include/blt/math/matrix.h | 126 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5be2b4..d0e23c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.20.6) +set(BLT_VERSION 0.20.7) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/matrix.h b/include/blt/math/matrix.h index 62fbf53..18b66e1 100644 --- a/include/blt/math/matrix.h +++ b/include/blt/math/matrix.h @@ -19,6 +19,132 @@ namespace blt { + template + class generalized_matrix + { + using matrix_t = generalized_matrix; + public: + static matrix_t make_empty() + { + matrix_t mat; + return mat; + } + + static matrix_t make_identity() + { + static_assert(rows == columns && "Identity matrix must be square!"); + matrix_t mat = make_empty(); + for (blt::u32 i = 0; i < rows; i++) + mat[i][i] = 1; + return mat; + } + + // adds the two mat4x4 left and right + inline friend matrix_t operator+(const matrix_t& left, const matrix_t& right) + { + matrix_t ret = left; + for (int i = 0; i < columns; i++) + ret[i] += right.data[i]; + return ret; + } + + // subtracts the right mat4x4 from the left. + inline friend matrix_t operator-(const matrix_t& left, const matrix_t& right) + { + matrix_t ret = left; + for (int i = 0; i < columns; i++) + ret[i] -= right.data[i]; + return ret; + } + + // multiples the left with the right + template> + inline friend Ret operator*(const matrix_t& left, const generalized_matrix& right) + { + Ret mat = Ret::make_empty(); + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < p; j++) + { + for (int k = 0; k < columns; k++) + mat.m(i, j, mat.m(i, j) + left.m(i, k) * right.m(k, j)); + } + } + + return mat; + } + + inline friend vec operator*(const matrix_t& left, const vec& right) + { + vec ret; + + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < columns; c++) + ret[r] = ret[r] + left.m(r, c) * right[c]; + } + + return ret; + } + + // multiplies the const c with each element in the mat4x4 v + inline friend matrix_t operator*(float c, const matrix_t & v) + { + matrix_t mat{}; + + for (int i = 0; i < columns; i++) + { + mat.data[i] = c * v.data[i]; + } + + return mat; + } + + // same as above but for right sided constants + inline friend matrix_t operator*(const matrix_t& v, float c) + { + matrix_t mat{}; + + for (int i = 0; i < columns; i++) + { + mat.data[i] = v.data[i] * c; + } + + return mat; + } + + // divides the mat4x4 by the constant c + inline friend matrix_t operator/(const matrix_t& v, float c) + { + matrix_t mat{}; + + for (int i = 0; i < columns; i++) + { + mat.data[i] = v.data[i] / c; + } + + return mat; + } + + // divides each element in the mat4x4 by over the constant + inline friend matrix_t operator/(float c, const mat4x4& v) + { + matrix_t mat{}; + + for (int i = 0; i < columns; i++) + { + for (int j = 0; j < 4; j++) + mat.data[i][j] = c / v.data[i][j]; + } + + return mat; + } + + private: + blt::vec data[rows]; + }; + class mat4x4 { static_assert(std::is_trivially_copyable_v && "Vector must be trivially copyable!");