Update Matrix2.

This commit is contained in:
scorpioblood 2024-06-12 22:01:40 +02:00
parent 69be245e29
commit d99a318e53
3 changed files with 283 additions and 278 deletions

View File

@ -10,3 +10,5 @@
#include "Core/public/Math/IntVector2.hpp"
#include "Core/public/Math/IntVector3.hpp"
#include "Core/public/Math/IntVector4.hpp"
#include "Core/public/Math/Matrix2.hpp"

View File

@ -29,7 +29,6 @@ namespace Phanes::Core::Math {
template<RealType T> struct TRay;
template<RealType T> struct TLine;
template<RealType T> struct TPlane;
template<RealType T> struct TMatrix2;
template<RealType T> struct TMatrix3;
template<RealType T> struct TMatrix4;
template<RealType T> struct TQuaternion;
@ -40,39 +39,18 @@ namespace Phanes::Core::Math {
template<IntType T> struct TIntPoint2;
template<IntType T> struct TIntPoint3;
template<IntType T> struct TIntPoint4;
template<RealType T, bool A> struct TVector2;
template<RealType T, bool A> struct TVector3;
template<RealType T, bool A> struct TVector4;
template<IntType T, bool A> struct TIntVector2;
template<IntType T, bool A> struct TIntVector3;
template<IntType T, bool A> struct TIntVector4;
template<RealType T> struct TMatrix2;
template<RealType T, bool S> struct TVector2;
template<RealType T, bool S> struct TVector3;
template<RealType T, bool S> struct TVector4;
template<IntType T, bool S> struct TIntVector2;
template<IntType T, bool S> struct TIntVector3;
template<IntType T, bool S> struct TIntVector4;
/**
* Specific instantiation of forward declarations.
*/
// TMatrix2
typedef TMatrix2<float> Matrix2;
typedef TMatrix2<double> Matrix2d;
typedef std::vector<Matrix2> Matrix2List;
typedef std::vector<Matrix2d> Matrix2Listd;
// TMatrix3
typedef TMatrix3<float> Matrix3;
typedef TMatrix3<double> Matrix3d;
typedef std::vector<Matrix3> Matrix3List;
typedef std::vector<Matrix3d> Matrix3Listd;
// TPlane
typedef TPlane<float> Plane;
typedef TPlane<double> Planed;
typedef std::vector<Plane> PlaneList;
typedef std::vector<Planed> PlaneListd;
} // Phanes::Core::Math::coretypes

View File

@ -2,6 +2,8 @@
#include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathFwd.h"
#include "Core/public/Math/Vector2.hpp"
#ifndef MATRIX2_H
@ -17,7 +19,23 @@ namespace Phanes::Core::Math {
{
public:
T m[2][2];
union
{
struct
{
/// <summary>
/// Column one.
/// </summary>
TVector2<T, false> c0;
/// <summary>
/// Column two
/// </summary>
TVector2<T, false> c1;
};
T data[2][2];
};
public:
@ -29,17 +47,8 @@ namespace Phanes::Core::Math {
TMatrix2(const TMatrix2<T>& m1)
{
memcpy(this->m, m1.m, sizeof(T) * 4);
}
/**
* Move constructor.
*/
TMatrix2(TMatrix2<T>&& m)
{
this->m = m.m;
m.m = nullptr;
this->c0 = m1.c0;
this->c1 = m1.c1;
}
/**
@ -50,8 +59,8 @@ namespace Phanes::Core::Math {
TMatrix2(T fields[2][2])
{
this->m[0][0] = fields[0][0]; this->m[1][0] = fields[1][0];
this->m[0][1] = fields[0][1]; this->m[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];
}
/**
@ -67,8 +76,8 @@ namespace Phanes::Core::Math {
TMatrix2(T n00, T n01, T n10, T n11)
{
this->m[0][0] = n00; this->m[1][0] = n01;
this->m[0][1] = n10; this->m[1][1] = n11;
this->data[0][0] = n00; this->data[1][0] = n01;
this->data[0][1] = n10; this->data[1][1] = n11;
}
/**
@ -78,28 +87,44 @@ namespace Phanes::Core::Math {
* @param(v2) Column one
*/
TMatrix2(const TVector2<T>& v1, const TVector2<T>& v2)
TMatrix2(const TVector2<T, false>& v1, const TVector2<T, false>& v2)
{
this->m[0][0] = v1.x; this->m[1][0] = v2.x;
this->m[0][1] = v1.y; this->m[1][1] = v2.y;
this->c0 = v1;
this->c1 = v2;
}
public:
FORCEINLINE T& operator() (int n, int m) const
constexpr GetCol(int n)
{
return this->m[m][n];
switch (n)
{
case 0:
return this->c0;
case 1:
return this->c1;
default:
break;
}
FORCEINLINE TVector2<T>& operator[] (int m) const
}
FORCEINLINE T operator() (int n, int m) const
{
return reinterpret_cast<TVector2*>(this->m[m]);
this->data[m][n];
}
FORCEINLINE TVector2<T, false>& operator[] (int m) const
{
static_assert(m > -1 && m < 2, "(PHANES_CORE::MATH [Matrix2.hpp]): m must be between 0 or 1.");
return GetCol(m);
}
};
// ===================== //
// ====================== //
// TMatrix2 operator //
// ===================== //
// ====================== //
template<RealType T>
TMatrix2<T> operator+= (TMatrix2<T>& m1, T s)
@ -159,7 +184,7 @@ namespace Phanes::Core::Math {
template<RealType T>
TMatrix2<T> operator*= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
{
Matrix2 c = m1;
TMatrix2<T> c = m1;
m1(0, 0) = c(0, 0) * m2(0, 0) + c(0, 1) * m2(1, 0);
m1(0, 1) = c(0, 0) * m2(0, 1) + c(0, 1) * m2(1, 1);
@ -213,9 +238,9 @@ namespace Phanes::Core::Math {
}
template<RealType T>
TVector2<T> operator* (const TMatrix2<T>& m1, const TVector2<T>& v)
TVector2<T, false> operator* (const TMatrix2<T>& m1, const TVector2<T, false>& v)
{
return TVector2<T>(m1(0, 0) * v.x + m1(0, 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);
}
@ -234,12 +259,12 @@ namespace Phanes::Core::Math {
}
// =============================== //
// ============================== //
// Matrix function definition //
// =============================== //
// ============================== //
template<RealType T>
T Determinant(const Matrix2& m1)
T Determinant(const TMatrix2<T>& m1)
{
return m1(0, 0) * m1(1, 1) - m1(0, 1) * m1(0, 1);
}