not going to merge 4x4 into generalized
parent
d1aaf4db94
commit
ba10bd633e
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
include(cmake/color.cmake)
|
||||
set(BLT_VERSION 0.20.7)
|
||||
set(BLT_VERSION 0.20.8)
|
||||
set(BLT_TEST_VERSION 0.0.1)
|
||||
|
||||
set(BLT_TARGET BLT)
|
||||
|
|
|
@ -23,22 +23,109 @@ namespace blt
|
|||
class generalized_matrix
|
||||
{
|
||||
using matrix_t = generalized_matrix<T, rows, columns>;
|
||||
enum class init_type
|
||||
{
|
||||
EMPTY,
|
||||
IDENTITY
|
||||
};
|
||||
|
||||
explicit generalized_matrix(init_type type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case init_type::EMPTY:
|
||||
break;
|
||||
case init_type::IDENTITY:
|
||||
set_identity();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
generalized_matrix() = default;
|
||||
|
||||
generalized_matrix(const matrix_t& copy)
|
||||
{
|
||||
std::memcpy(data, copy.data, sizeof(matrix_t));
|
||||
}
|
||||
|
||||
generalized_matrix(matrix_t&& move) noexcept
|
||||
{
|
||||
std::memcpy(data, move.data, sizeof(matrix_t));
|
||||
}
|
||||
|
||||
matrix_t& operator=(const matrix_t& copy)
|
||||
{
|
||||
if (© == this)
|
||||
return *this;
|
||||
std::memcpy(data, copy.data, sizeof(matrix_t));
|
||||
return *this;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
explicit generalized_matrix(const blt::vec<T, rows> dat[columns])
|
||||
{
|
||||
for (int i = 0; i < columns; i++)
|
||||
data[i] = dat[i];
|
||||
}
|
||||
|
||||
static matrix_t make_empty()
|
||||
{
|
||||
matrix_t mat;
|
||||
return mat;
|
||||
return matrix_t{init_type::EMPTY};
|
||||
}
|
||||
|
||||
static matrix_t make_identity()
|
||||
{
|
||||
static_assert(rows == columns && "Identity matrix must be square!");
|
||||
matrix_t mat = make_empty();
|
||||
return matrix_t{init_type::IDENTITY};
|
||||
}
|
||||
|
||||
auto& set_identity()
|
||||
{
|
||||
for (blt::u32 i = 0; i < rows; i++)
|
||||
mat[i][i] = 1;
|
||||
data[i][i] = 1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
generalized_matrix<T, columns, rows> transpose()
|
||||
{
|
||||
generalized_matrix<T, columns, rows> mat;
|
||||
|
||||
for (blt::u32 i = 0; i < columns; i++)
|
||||
{
|
||||
for (blt::u32 j = 0; j < rows; j++)
|
||||
mat[j][i] = data[i][j];
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
inline const blt::vec<T, rows>& operator[](int column) const
|
||||
{
|
||||
return data[column];
|
||||
}
|
||||
|
||||
inline blt::vec<T, rows>& operator[](int column)
|
||||
{
|
||||
return data[column];
|
||||
}
|
||||
|
||||
[[nodiscard]] inline T m(int row, int column) const
|
||||
{
|
||||
return data[column][row];
|
||||
};
|
||||
|
||||
inline T m(int row, int column, T value)
|
||||
{
|
||||
return data[column][row] = value;
|
||||
};
|
||||
|
||||
// adds the two mat4x4 left and right
|
||||
inline friend matrix_t operator+(const matrix_t& left, const matrix_t& right)
|
||||
{
|
||||
|
@ -91,7 +178,7 @@ namespace blt
|
|||
// 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{};
|
||||
matrix_t mat = make_empty();
|
||||
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
|
@ -104,7 +191,7 @@ namespace blt
|
|||
// same as above but for right sided constants
|
||||
inline friend matrix_t operator*(const matrix_t& v, float c)
|
||||
{
|
||||
matrix_t mat{};
|
||||
matrix_t mat = make_empty();
|
||||
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
|
@ -117,7 +204,7 @@ namespace blt
|
|||
// divides the mat4x4 by the constant c
|
||||
inline friend matrix_t operator/(const matrix_t& v, float c)
|
||||
{
|
||||
matrix_t mat{};
|
||||
matrix_t mat = make_empty();
|
||||
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
|
@ -128,13 +215,13 @@ namespace blt
|
|||
}
|
||||
|
||||
// divides each element in the mat4x4 by over the constant
|
||||
inline friend matrix_t operator/(float c, const mat4x4& v)
|
||||
inline friend matrix_t operator/(float c, const matrix_t& v)
|
||||
{
|
||||
matrix_t mat{};
|
||||
matrix_t mat = make_empty();
|
||||
|
||||
for (int i = 0; i < columns; i++)
|
||||
{
|
||||
for (int j = 0; j < 4; j++)
|
||||
for (int j = 0; j < rows; j++)
|
||||
mat.data[i][j] = c / v.data[i][j];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue