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)
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)

View File

@ -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<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

View File

@ -10,6 +10,8 @@
#include <blt/math/vectors.h>
#include <cstring>
#include <type_traits>
#include <array>
#include <initializer_list>
#ifndef M_PI
// MSVC does not have M_PI
@ -62,16 +64,43 @@ namespace blt
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])
{
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<T, rows> 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<T, rows>& operator[](int column) const
inline const blt::vec<T, rows>& operator[](u32 column) const
{
return data[column];
}
inline blt::vec<T, rows>& operator[](int column)
inline blt::vec<T, rows>& 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));
}
}
@ -166,9 +195,9 @@ namespace blt
{
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];
}
@ -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<T, columns> data[rows];
blt::vec<T, rows> data[columns];
};
class mat4x4

View File

@ -20,7 +20,7 @@ namespace blt
#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)
{
@ -381,9 +381,13 @@ namespace blt
template<typename T, blt::u32 size>
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++)
if (left[i] != right[i])
{
auto diff = left[i] - right[i];
if (diff > E || diff < -E)
return false;
}
return true;
}