diff --git a/CMakeLists.txt b/CMakeLists.txt index 719298a..e8ad294 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.8) +set(BLT_VERSION 0.20.9) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/math/log_util.h b/include/blt/math/log_util.h index 85f013d..2214f26 100644 --- a/include/blt/math/log_util.h +++ b/include/blt/math/log_util.h @@ -27,8 +27,10 @@ namespace blt type_string += tstr[0]; // for unsigned long / unsigned int auto split = blt::string::split_sv(tstr, ' '); - if (tstr[0] == 'u'){ - if (split.size() > 1){ + if (tstr[0] == 'u') + { + if (split.size() > 1) + { type_string += split[1][0]; } else type_string += tstr[1]; @@ -54,6 +56,21 @@ namespace blt return out; } + template + inline Writer& operator<<(Writer& out, const generalized_matrix& mat) + { + out << "Mat" << rows << 'x' << columns << "(\n"; + for (blt::u32 c = 0; c < columns; c++) + { + out << "\t{"; + for (blt::u32 r = 0; r < rows; r++) + out << mat[c][r] << ((r < rows - 1) ? ", " : ""); + out << '}' << ((c < columns - 1) ? "," : "") << "\n"; + } + out << ")"; + return out; + } + } #endif //BLT_TESTS_LOG_UTIL_H diff --git a/include/blt/math/matrix.h b/include/blt/math/matrix.h index ffb7c9a..070e5cd 100644 --- a/include/blt/math/matrix.h +++ b/include/blt/math/matrix.h @@ -10,6 +10,8 @@ #include #include #include +#include +#include #ifndef M_PI // MSVC does not have M_PI @@ -62,16 +64,43 @@ namespace blt return *this; } + generalized_matrix(std::initializer_list list) + { + blt::size_t index = 0; + for (const auto& v : list) + { + data[(index / rows)][(index % rows)] = v; + index++; + } + } + + generalized_matrix(std::initializer_list> list) + { + blt::size_t index = 0; + for (const auto& v : list) + { + data[index] = v; + index++; + } + } + + explicit generalized_matrix(const std::array& dat) + { + for (u32 i = 0; i < columns; i++) + for (u32 j = 0; j < rows; j++) + data[i][j] = dat[j + i * columns]; + } + explicit generalized_matrix(const T dat[rows * columns]) { - for (int i = 0; i < columns; i++) - for (int j = 0; j < rows; j++) - data[i][j] = dat[j + i * 4]; + for (u32 i = 0; i < columns; i++) + for (u32 j = 0; j < rows; j++) + data[i][j] = dat[j + i * columns]; } explicit generalized_matrix(const blt::vec dat[columns]) { - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) data[i] = dat[i]; } @@ -106,22 +135,22 @@ namespace blt return mat; } - inline const blt::vec& operator[](int column) const + inline const blt::vec& operator[](u32 column) const { return data[column]; } - inline blt::vec& operator[](int column) + inline blt::vec& operator[](u32 column) { return data[column]; } - [[nodiscard]] inline T m(int row, int column) const + [[nodiscard]] inline T m(u32 row, u32 column) const { return data[column][row]; }; - inline T m(int row, int column, T value) + inline T m(u32 row, u32 column, T value) { return data[column][row] = value; }; @@ -130,7 +159,7 @@ namespace blt inline friend matrix_t operator+(const matrix_t& left, const matrix_t& right) { matrix_t ret = left; - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) ret[i] += right.data[i]; return ret; } @@ -139,7 +168,7 @@ namespace blt inline friend matrix_t operator-(const matrix_t& left, const matrix_t& right) { matrix_t ret = left; - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) ret[i] -= right.data[i]; return ret; } @@ -150,11 +179,11 @@ namespace blt { Ret mat = Ret::make_empty(); - for (int i = 0; i < rows; i++) + for (u32 i = 0; i < rows; i++) { - for (int j = 0; j < p; j++) + for (u32 j = 0; j < p; j++) { - for (int k = 0; k < columns; k++) + for (u32 k = 0; k < columns; k++) mat.m(i, j, mat.m(i, j) + left.m(i, k) * right.m(k, j)); } } @@ -162,13 +191,13 @@ namespace blt return mat; } - inline friend vec operator*(const matrix_t& left, const vec & right) + inline friend vec operator*(const matrix_t& left, const vec& right) { - vec < T, rows > ret; + vec ret; - for (int r = 0; r < rows; r++) + for (u32 r = 0; r < rows; r++) { - for (int c = 0; c < columns; c++) + for (u32 c = 0; c < columns; c++) ret[r] = ret[r] + left.m(r, c) * right[c]; } @@ -180,7 +209,7 @@ namespace blt { matrix_t mat = make_empty(); - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) { mat.data[i] = c * v.data[i]; } @@ -193,7 +222,7 @@ namespace blt { matrix_t mat = make_empty(); - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) { mat.data[i] = v.data[i] * c; } @@ -206,7 +235,7 @@ namespace blt { matrix_t mat = make_empty(); - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) { mat.data[i] = v.data[i] / c; } @@ -219,17 +248,32 @@ namespace blt { matrix_t mat = make_empty(); - for (int i = 0; i < columns; i++) + for (u32 i = 0; i < columns; i++) { - for (int j = 0; j < rows; j++) + for (u32 j = 0; j < rows; j++) mat.data[i][j] = c / v.data[i][j]; } return mat; } + + inline friend bool operator==(const matrix_t& left, const matrix_t& right) + { + for (blt::u32 i = 0; i < columns; i++) + { + if (left.data[i] != right.data[i]) + return false; + } + return true; + } + + inline friend bool operator!=(const matrix_t& left, const matrix_t& right) + { + return !(left == right); + } private: - blt::vec data[rows]; + blt::vec data[columns]; }; class mat4x4 diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index 08085bd..8de0a9c 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -20,7 +20,7 @@ namespace blt #define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__)) - constexpr float EPSILON = 0.0001f; + constexpr float EPSILON = std::numeric_limits::epsilon(); static inline constexpr bool f_equal(float v1, float v2) { @@ -381,9 +381,13 @@ namespace blt template inline constexpr bool operator==(const vec& left, const vec& right) { + constexpr double E = std::numeric_limits::epsilon(); for (blt::u32 i = 0; i < size; i++) - if (left[i] != right[i]) + { + auto diff = left[i] - right[i]; + if (diff > E || diff < -E) return false; + } return true; }