feat: Fix misleading doc.

This commit is contained in:
Thorben Höhne 2025-05-04 16:30:02 +02:00
parent 77d39b6254
commit 81ef16cc99
Signed by: thoehne
GPG Key ID: 60D202D915B81DEC

View File

@ -7,18 +7,18 @@
#include "Core/Math/Vector2.hpp"
#ifndef MATRIX2_H
#define MATRIX2_H
# define MATRIX2_H
namespace Phanes::Core::Math {
namespace Phanes::Core::Math
{
// 2x2 Matrix defined in column-major order.
// Accessed by M[Row][Col].
// Accessed by M[Col][Row].
template<RealType T>
template <RealType T>
struct TMatrix2
{
public:
union
{
struct
@ -38,7 +38,6 @@ namespace Phanes::Core::Math {
};
public:
TMatrix2() = default;
/**
@ -59,8 +58,10 @@ namespace Phanes::Core::Math {
TMatrix2(T fields[2][2])
{
this->data[0][0] = fields[0][0]; this->data[1][0] = fields[1][0];
this->data[0][1] = fields[0][1]; this->data[1][1] = fields[1][1];
this->data[0][0] = fields[0][0];
this->data[1][0] = fields[1][0];
this->data[0][1] = fields[0][1];
this->data[1][1] = fields[1][1];
}
/**
@ -76,8 +77,10 @@ namespace Phanes::Core::Math {
TMatrix2(T n00, T n01, T n10, T n11)
{
this->data[0][0] = n00; this->data[1][0] = n01;
this->data[0][1] = n10; this->data[1][1] = n11;
this->data[0][0] = n00;
this->data[1][0] = n01;
this->data[0][1] = n10;
this->data[1][1] = n11;
}
/**
@ -94,21 +97,19 @@ namespace Phanes::Core::Math {
}
public:
T& operator() (int n, int m)
T& operator()(int n, int m)
{
return this->data[m][n];
}
T operator() (int n, int m) const
T operator()(int n, int m) const
{
return this->data[m][n];
}
TVector2<T, false>& operator[] (int m)
TVector2<T, false>& operator[](int m)
{
switch (m)
switch(m)
{
case 0:
return this->c0;
@ -119,9 +120,9 @@ namespace Phanes::Core::Math {
throw std::invalid_argument("m is outside valid range.");
}
TVector2<T, false> operator[] (int m) const
TVector2<T, false> operator[](int m) const
{
switch (m)
switch(m)
{
case 0:
return this->c0;
@ -131,15 +132,14 @@ namespace Phanes::Core::Math {
throw std::invalid_argument("m is outside valid range.");
}
};
// ====================== //
// TMatrix2 operator //
// ====================== //
template<RealType T>
TMatrix2<T>& operator+= (TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T>& operator+=(TMatrix2<T>& m1, T s)
{
m1(0, 0) += s;
m1(0, 1) += s;
@ -149,8 +149,8 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T>& operator+= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
TMatrix2<T>& operator+=(TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
m1(0, 0) += m2(0, 0);
m1(0, 1) += m2(0, 1);
@ -160,8 +160,8 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T>& operator-= (TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T>& operator-=(TMatrix2<T>& m1, T s)
{
m1(0, 0) -= s;
m1(0, 1) -= s;
@ -171,8 +171,8 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T>& operator-= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
TMatrix2<T>& operator-=(TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
m1(0, 0) -= m2(0, 0);
m1(0, 1) -= m2(0, 1);
@ -182,8 +182,8 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T>& operator*= (TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T>& operator*=(TMatrix2<T>& m1, T s)
{
m1.data[0][0] *= s;
m1.data[0][1] *= s;
@ -193,8 +193,8 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T>& operator*= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
TMatrix2<T>& operator*=(TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
TMatrix2<T> c = m1;
@ -207,8 +207,8 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T>& operator/= (TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T>& operator/=(TMatrix2<T>& m1, T s)
{
s = (T)1.0 / s;
m1.data[0][0] *= s;
@ -219,87 +219,83 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
TMatrix2<T> operator+ (const TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T> operator+(const TMatrix2<T>& m1, T s)
{
return TMatrix2<T>(m1(0, 0) + s, m1(0, 1) + s,
m1(1, 0) + s, m1(1, 1) + s);
return TMatrix2<T>(m1(0, 0) + s, m1(0, 1) + s, m1(1, 0) + s, m1(1, 1) + s);
}
template<RealType T>
TMatrix2<T> operator+ (const TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
TMatrix2<T> operator+(const TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
return TMatrix2<T>(m1(0, 0) + m2(0, 0), m1(0, 1) + m2(0, 1),
m1(1, 0) + m2(1, 0), m1(1, 1) + m2(1, 1));
return TMatrix2<T>(
m1(0, 0) + m2(0, 0), m1(0, 1) + m2(0, 1), m1(1, 0) + m2(1, 0), m1(1, 1) + m2(1, 1));
}
template<RealType T>
TMatrix2<T> operator- (const TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T> operator-(const TMatrix2<T>& m1, T s)
{
return TMatrix2<T>(m1(0, 0) - s, m1(0, 1) - s,
m1(1, 0) - s, m1(1, 1) - s);
return TMatrix2<T>(m1(0, 0) - s, m1(0, 1) - s, m1(1, 0) - s, m1(1, 1) - s);
}
template<RealType T>
TMatrix2<T> operator- (const TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
TMatrix2<T> operator-(const TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
return TMatrix2<T>(m1(0, 0) - m2(0, 0), m1(0, 1) - m2(0, 1),
m1(1, 0) - m2(1, 0), m1(1, 1) - m2(1, 1));
return TMatrix2<T>(
m1(0, 0) - m2(0, 0), m1(0, 1) - m2(0, 1), m1(1, 0) - m2(1, 0), m1(1, 1) - m2(1, 1));
}
template<RealType T>
TMatrix2<T> operator* (const TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T> operator*(const TMatrix2<T>& m1, T s)
{
return TMatrix2<T>(m1(0, 0) * s, m1(0, 1) * s,
m1(1, 0) * s, m1(1, 1) * s);
return TMatrix2<T>(m1(0, 0) * s, m1(0, 1) * s, m1(1, 0) * s, m1(1, 1) * s);
}
template<RealType T>
TMatrix2<T> operator/ (const TMatrix2<T>& m1, T s)
template <RealType T>
TMatrix2<T> operator/(const TMatrix2<T>& m1, T s)
{
s = (T)1.0 / s;
return TMatrix2<T>(m1(0, 0) * s, m1(0, 1) * s,
m1(1, 0) * s, m1(1, 1) * s);
return TMatrix2<T>(m1(0, 0) * s, m1(0, 1) * s, m1(1, 0) * s, m1(1, 1) * s);
}
template<RealType T>
TMatrix2<T> operator* (const TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
TMatrix2<T> operator*(const TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
return TMatrix2<T>(m1(0, 0) * m2(0, 0) + m1(0, 1) * m2(1, 0), m1(0, 0) * m2(0, 1) + m1(0, 1) * m2(1, 1),
m1(1, 0) * m2(0, 0) + m1(1, 1) * m2(1, 0), m1(1, 0) * m2(0, 1) + m1(1, 1) * m2(1, 1));
return TMatrix2<T>(m1(0, 0) * m2(0, 0) + m1(0, 1) * m2(1, 0),
m1(0, 0) * m2(0, 1) + m1(0, 1) * m2(1, 1),
m1(1, 0) * m2(0, 0) + m1(1, 1) * m2(1, 0),
m1(1, 0) * m2(0, 1) + m1(1, 1) * m2(1, 1));
}
template<RealType T>
TVector2<T, false> operator* (const TMatrix2<T>& m1, const TVector2<T, false>& v)
template <RealType T>
TVector2<T, false> operator*(const TMatrix2<T>& m1, const TVector2<T, false>& v)
{
return TVector2<T, false>(m1(0, 0) * v.x + m1(0, 1) * v.y,
m1(1, 0) * v.x + m1(1, 1) * v.y);
return TVector2<T, false>(m1(0, 0) * v.x + m1(0, 1) * v.y, m1(1, 0) * v.x + m1(1, 1) * v.y);
}
template<RealType T>
bool operator== (const TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
bool operator==(const TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
return m1[0] == m2[0] && m1[1] == m2[1];
}
template<RealType T>
bool operator!= (const TMatrix2<T>& m1, const TMatrix2<T>& m2)
template <RealType T>
bool operator!=(const TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
return m1[0] != m2[0] || m1[1] != m2[1];
}
// ============================== //
// Matrix function definition //
// ============================== //
template<RealType T>
template <RealType T>
T Determinant(const TMatrix2<T>& m1)
{
return m1(0, 0) * m1(1, 1) - m1(0, 1) * m1(1, 0);
}
template<RealType T>
template <RealType T>
TMatrix2<T>& InverseV(TMatrix2<T>& m1)
{
float _1_det = 1.0f / Determinant(m1);
@ -314,7 +310,7 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T>
template <RealType T>
TMatrix2<T>& TransposeV(TMatrix2<T>& m1)
{
Swap(m1(0, 1), m1(1, 0));
@ -326,33 +322,30 @@ namespace Phanes::Core::Math {
// WITH RETURN //
// =============== //
template<RealType T>
template <RealType T>
TMatrix2<T> Inverse(TMatrix2<T>& m1)
{
float _1_det = 1.0f / Determinant(m1);
return TMatrix2<T>( m1(1, 1) * _1_det, -m1(0, 1) * _1_det,
-m1(1, 0) * _1_det, m1(0, 0) * _1_det);
return TMatrix2<T>(
m1(1, 1) * _1_det, -m1(0, 1) * _1_det, -m1(1, 0) * _1_det, m1(0, 0) * _1_det);
}
template<RealType T>
template <RealType T>
TMatrix2<T> Transpose(const TMatrix2<T>& m1)
{
return TMatrix2<T>(m1(0, 0), m1(1, 0),
m1(0, 1), m1(1, 1));
return TMatrix2<T>(m1(0, 0), m1(1, 0), m1(0, 1), m1(1, 1));
}
template<RealType T>
template <RealType T>
bool IsIdentityMatrix(const TMatrix2<T>& m1, T threshold = P_FLT_INAC)
{
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1)) < P_FLT_INAC &&
abs(m1(1, 0)) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC);
}
} // Phanes::Core::Math
} // namespace Phanes::Core::Math
#endif // !MATRIX2_H
#include "Core/Math/SIMD/SIMDIntrinsics.h"