matrix fix

v1
Brett 2024-09-19 15:12:38 -04:00
parent ba10bd633e
commit e19a88c454
4 changed files with 93 additions and 28 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) 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_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -27,8 +27,10 @@ namespace blt
type_string += tstr[0]; type_string += tstr[0];
// for unsigned long / unsigned int // for unsigned long / unsigned int
auto split = blt::string::split_sv(tstr, ' '); auto split = blt::string::split_sv(tstr, ' ');
if (tstr[0] == 'u'){ if (tstr[0] == 'u')
if (split.size() > 1){ {
if (split.size() > 1)
{
type_string += split[1][0]; type_string += split[1][0];
} else } else
type_string += tstr[1]; type_string += tstr[1];
@ -54,6 +56,21 @@ namespace blt
return out; return out;
} }
template<typename Writer = std::ostream, typename T, blt::u32 rows, blt::u32 columns>
inline Writer& operator<<(Writer& out, const generalized_matrix<T, rows, columns>& 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 #endif //BLT_TESTS_LOG_UTIL_H

View File

@ -10,6 +10,8 @@
#include <blt/math/vectors.h> #include <blt/math/vectors.h>
#include <cstring> #include <cstring>
#include <type_traits> #include <type_traits>
#include <array>
#include <initializer_list>
#ifndef M_PI #ifndef M_PI
// MSVC does not have M_PI // MSVC does not have M_PI
@ -62,16 +64,43 @@ namespace blt
return *this; return *this;
} }
generalized_matrix(std::initializer_list<T> list)
{
blt::size_t index = 0;
for (const auto& v : list)
{
data[(index / rows)][(index % rows)] = v;
index++;
}
}
generalized_matrix(std::initializer_list<blt::vec<float, rows>> list)
{
blt::size_t index = 0;
for (const auto& v : list)
{
data[index] = v;
index++;
}
}
explicit generalized_matrix(const std::array<T, rows * columns>& 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]) explicit generalized_matrix(const T dat[rows * columns])
{ {
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++)
data[i][j] = dat[j + i * 4]; data[i][j] = dat[j + i * columns];
} }
explicit generalized_matrix(const blt::vec<T, rows> dat[columns]) explicit generalized_matrix(const blt::vec<T, rows> dat[columns])
{ {
for (int i = 0; i < columns; i++) for (u32 i = 0; i < columns; i++)
data[i] = dat[i]; data[i] = dat[i];
} }
@ -106,22 +135,22 @@ namespace blt
return mat; return mat;
} }
inline const blt::vec<T, rows>& operator[](int column) const inline const blt::vec<T, rows>& operator[](u32 column) const
{ {
return data[column]; return data[column];
} }
inline blt::vec<T, rows>& operator[](int column) inline blt::vec<T, rows>& operator[](u32 column)
{ {
return data[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]; 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; return data[column][row] = value;
}; };
@ -130,7 +159,7 @@ namespace blt
inline friend matrix_t operator+(const matrix_t& left, const matrix_t& right) inline friend matrix_t operator+(const matrix_t& left, const matrix_t& right)
{ {
matrix_t ret = left; matrix_t ret = left;
for (int i = 0; i < columns; i++) for (u32 i = 0; i < columns; i++)
ret[i] += right.data[i]; ret[i] += right.data[i];
return ret; return ret;
} }
@ -139,7 +168,7 @@ namespace blt
inline friend matrix_t operator-(const matrix_t& left, const matrix_t& right) inline friend matrix_t operator-(const matrix_t& left, const matrix_t& right)
{ {
matrix_t ret = left; matrix_t ret = left;
for (int i = 0; i < columns; i++) for (u32 i = 0; i < columns; i++)
ret[i] -= right.data[i]; ret[i] -= right.data[i];
return ret; return ret;
} }
@ -150,11 +179,11 @@ namespace blt
{ {
Ret mat = Ret::make_empty(); 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)); mat.m(i, j, mat.m(i, j) + left.m(i, k) * right.m(k, j));
} }
} }
@ -162,13 +191,13 @@ namespace blt
return mat; return mat;
} }
inline friend vec <T, rows> operator*(const matrix_t& left, const vec <T, columns>& right) inline friend vec<T, rows> operator*(const matrix_t& left, const vec<T, columns>& right)
{ {
vec < T, rows > ret; vec<T, rows> 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]; ret[r] = ret[r] + left.m(r, c) * right[c];
} }
@ -180,7 +209,7 @@ namespace blt
{ {
matrix_t mat = make_empty(); 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]; mat.data[i] = c * v.data[i];
} }
@ -193,7 +222,7 @@ namespace blt
{ {
matrix_t mat = make_empty(); 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; mat.data[i] = v.data[i] * c;
} }
@ -206,7 +235,7 @@ namespace blt
{ {
matrix_t mat = make_empty(); 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; mat.data[i] = v.data[i] / c;
} }
@ -219,17 +248,32 @@ namespace blt
{ {
matrix_t mat = make_empty(); 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]; mat.data[i][j] = c / v.data[i][j];
} }
return mat; 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: private:
blt::vec<T, columns> data[rows]; blt::vec<T, rows> data[columns];
}; };
class mat4x4 class mat4x4

View File

@ -20,7 +20,7 @@ namespace blt
#define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__)) #define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__))
constexpr float EPSILON = 0.0001f; constexpr float EPSILON = std::numeric_limits<float>::epsilon();
static inline constexpr bool f_equal(float v1, float v2) static inline constexpr bool f_equal(float v1, float v2)
{ {
@ -381,9 +381,13 @@ namespace blt
template<typename T, blt::u32 size> template<typename T, blt::u32 size>
inline constexpr bool operator==(const vec<T, size>& left, const vec<T, size>& right) inline constexpr bool operator==(const vec<T, size>& left, const vec<T, size>& right)
{ {
constexpr double E = std::numeric_limits<T>::epsilon();
for (blt::u32 i = 0; i < size; i++) 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 false;
}
return true; return true;
} }