Template class for working with 2x2 matrices. If you use matrix for transforming vector pay attention on the fact that the matrix is designed to be used for such a purpose in the way that vector should be multiplied to the matrix. If you try to multiply, for example rotate, matrix to a vector, you will get wrong transformation. The matrix give you access to the entire elements data by returning pointer to it via data()
method. This can be used, for example, for direct data uploading into your shader program without any temporary storages(in you shader program you, as well, should multiply vector on matrix and not vise versa) and without transposing.
You can find more information in comments below.
template<class TYPE>
class TMat2
{
public:
// Indices
static const size_t _11{ 0 }, _21{ 1 };
static const size_t _12{ 2 }, _22{ 3 };
TMat2() = default;
TMat2(const TYPE diagonal); // Create diaginal matrix with all diagonal elements equal to the specified value
TMat2(const TYPE _11, const TYPE _22); // Create diaginal matrix with diagonal elements equal to the specified values
TMat2(const TVec2<TYPE> &row1, const TVec2<TYPE> &row2); // Create with specified rows
TMat2(const TMat2 &other) = default;
TMat2(TMat2 &&other) = default;
const TYPE &operator[](const size_t index) const; // Return constant reference to the element with specified index
TYPE &operator[](const size_t index); // Return reference to the element with specified index
bool operator==(const TMat2 &other) const; // Strict comparison for equality
bool operator!=(const TMat2 &other) const; // Strict comparison for inequality
TMat2 &operator=(const TMat2 &other) = default;
TMat2 &operator=(TMat2 &&other) = default;
TMat2 operator+(const TYPE value) const; // Add scalar value to all elements and return new matrix
TMat2 operator-(const TYPE value) const; // Subtract scalar value form all elements and return new matrix
TMat2 operator*(const TYPE value) const; // Multiply scalar value on all elements and return new matrix
TMat2 operator/(const TYPE value) const; // Divide all elements on scalar value and return new matrix
TMat2 &operator+=(const TYPE value); // Add scalar value to all elements of this matrix
TMat2 &operator-=(const TYPE value); // Subtract scalar value from all elements of this matrix
TMat2 &operator*=(const TYPE value); // Multiply scalar value on all elements of this matrix
TMat2 &operator/=(const TYPE value); // Divide all elements on scalar value of this matrix
TMat2 operator+(const TMat2 &other) const; // Add elements of another matrix to corresponding elements of this matrix and return new matrix
TMat2 operator-(const TMat2 &other) const; // Subtract elements of another matrix from corresponding elements of this matrix and return new matrix
TMat2 operator*(const TMat2 &other) const; // Multiply elements of another matrix on corresponding elements of this matrix and return new matrix
TMat2 operator/(const TMat2 &other) const; // Divide elements of this matrix on corresponding elements of another matrix and return new matrix
TMat2 operator^(const TMat2 &other) const; // Multiply matrices
TMat2 &operator+=(const TMat2 &other); // Add elements of another matrix to corresponding elements of this matrix
TMat2 &operator-=(const TMat2 &other); // Subtract elements of another matrix from corresponding elements of this matrix
TMat2 &operator*=(const TMat2 &other); // Multiply elements of another matrix on corresponding elements of this matrix
TMat2 &operator/=(const TMat2 &other); // Divide elements of this matrix on corresponding elements of another matrix
TMat2 &operator^=(const TMat2 &other); // Multiply matrices and store result in this one
const TYPE *data() const; // Return pointer to elements array
TYPE get(const size_t column, const size_t row) const; // Get specified element
void set(const size_t column, const size_t row, const TYPE value); // Set specified element
bool is_equal(const TMat2 &other, const TYPE tolerance) const; // Soft comparison for equality with another matrix
bool is_not_equal(const TMat2 &other, const TYPE tolerance) const; // Soft comparison for inequality with another matrix
TYPE minor(const size_t column, const size_t row) const; // Return minor
TYPE determinant() const; // Calculate determinant
void transpose(); // Transpose this matrix
TMat2 transposed() const; // Return matrix transposed to this one
void inverse(); // Inverse this matrix
TMat2 inversed() const; // Return matrix inversed to this one
void identity(); // Make this matrix identity
void diagonal(const TYPE value); // Make this matrix diagonal with all diagonal elements equal to specified value
void diagonal(const TYPE _11, const TYPE _22); // Make this matrix diagonal with diagonal elements equal to specified values
void scale(const TYPE x, const TYPE y); // Make scaling matrix
void rotate_z(const TYPE angle); // Make rotation around X-axis matrix
};
Additionally following operators exists and can be used for arithmetic operations with scalar value and 2x2 matrices and 2D vectors. So as a final result you may use the 2x2 matrix template with scalar/2D vector values in both directions(scalar * matrix
and matrix * scalar
). Pay attention on the -, /, and ^ operators. The result of operation like scalar - matrix
will be different from the result of operation like matrix - scalar
.
template<class TYPE> TMat2<TYPE> operator+(const TYPE value, const TMat2<TYPE> &m);
template<class TYPE> TMat2<TYPE> operator-(const TYPE value, const TMat2<TYPE> &m);
template<class TYPE> TMat2<TYPE> operator*(const TYPE value, const TMat2<TYPE> &m);
template<class TYPE> TMat2<TYPE> operator/(const TYPE value, const TMat2<TYPE> &m);
template<class TYPE> TVec2<TYPE> operator^(const TVec2<TYPE> &v, const TMat2<TYPE> &m);
template<class TYPE> TVec2<TYPE> &operator^=(TVec2<TYPE> &v, const TMat2<TYPE> &m);
template<class TYPE> TVec2<TYPE> operator^(const TMat2<TYPE> &m, const TVec2<TYPE> &v);
Namespace: | nitisa::math |
Include: | Nitisa/Modules/Math/Matrix.h |