Make math independent from engine.

This commit is contained in:
scorpioblood 2024-05-20 22:00:08 +02:00
parent e0f6cf1fa0
commit 122fba35db
18 changed files with 251 additions and 457 deletions

View File

@ -1,303 +0,0 @@
// ========================== //
// Matrix3 implementation //
// ========================== //
#include "Misc/CommonDefines.h"
#include PHANES_CORE_PCH_DEFAULT_PATH
#include "Math/Matrix3.h"
// ================================= //
// Class Methods for easy access //
// ================================= //
float& phanes::core::math::coretypes::Matrix3::operator()(int n, int m)
{
return this->fields[m][n];
}
phanes::core::math::coretypes::Vector3& phanes::core::math::coretypes::Matrix3::operator[](int m)
{
return (*reinterpret_cast<Vector3*>(this->fields[m]));
}
const float& phanes::core::math::coretypes::Matrix3::operator()(int n, int m) const
{
return this->fields[m][n];
}
const phanes::core::math::coretypes::Vector3& phanes::core::math::coretypes::Matrix3::operator[](int m) const
{
return (*reinterpret_cast<const Vector3*>(this->fields[m]));
}
// ================= //
// Constructors //
// ================= //
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::createMatrix(float fields[3][3])
{
Matrix3 a;
std::copy(&fields[0][0], &fields[3][3], &a.fields[0][0]);
return a;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::createMatrix(float f00, float f01, float f02, float f10, float f11, float f12, float f20, float f21, float f22)
{
Matrix3 a;
a.fields[0][0] = f00; a.fields[1][0] = f01; a.fields[2][0] = f02;
a.fields[0][1] = f10; a.fields[1][1] = f11; a.fields[2][1] = f12;
a.fields[0][2] = f20; a.fields[1][2] = f21; a.fields[2][2] = f22;
return a;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::createMatrix(const Vector3& a, const Vector3& b, const Vector3& c)
{
Matrix3 m;
m.fields[0][0] = a.x; m.fields[1][0] = b.x; m.fields[2][0] = c.x;
m.fields[0][1] = a.y; m.fields[1][1] = b.y; m.fields[2][1] = c.y;
m.fields[0][2] = a.z; m.fields[1][2] = b.z; m.fields[2][2] = c.z;
return m;
}
// ============= //
// Operators //
// ============= //
void phanes::core::math::coretypes::operator+=(Matrix3& a, float s)
{
a[0] += s;
a[1] += s;
a[2] += s;
}
void phanes::core::math::coretypes::operator+=(Matrix3& a, const Matrix3& b)
{
a[0] += b[0];
a[1] += b[1];
a[2] += b[2];
}
void phanes::core::math::coretypes::operator-=(Matrix3& a, float s)
{
a[0] -= s;
a[1] -= s;
a[2] -= s;
}
void phanes::core::math::coretypes::operator-=(Matrix3& a, const Matrix3& b)
{
a[0] -= b[0];
a[1] -= b[1];
a[2] -= b[2];
}
void phanes::core::math::coretypes::operator*=(Matrix3& a, float s)
{
a[0] *= s;
a[1] *= s;
a[2] *= s;
}
void phanes::core::math::coretypes::operator*=(Matrix3& a, const Matrix3& b)
{
Matrix3 c = a;
a(0, 0) = c(0, 0) * b(0, 0) + c(0, 1) * b(1, 0) + c(0, 2) * b(2, 0);
a(0, 1) = c(0, 0) * b(0, 1) + c(0, 1) * b(1, 1) + c(0, 2) * b(2, 1);
a(0, 2) = c(0, 0) * b(0, 2) + c(0, 1) * b(1, 2) + c(0, 2) * b(2, 2);
a(1, 0) = c(1, 0) * b(0, 0) + c(1, 1) * b(1, 0) + c(1, 2) * b(2, 0);
a(1, 1) = c(1, 0) * b(0, 1) + c(1, 1) * b(1, 1) + c(1, 2) * b(2, 1);
a(1, 2) = c(1, 0) * b(0, 2) + c(1, 1) * b(1, 2) + c(1, 2) * b(2, 2);
a(2, 0) = c(2, 0) * b(0, 0) + c(2, 1) * b(1, 0) + c(2, 2) * b(2, 0);
a(2, 1) = c(2, 0) * b(0, 1) + c(2, 1) * b(1, 1) + c(2, 2) * b(2, 1);
a(2, 2) = c(2, 0) * b(0, 2) + c(2, 1) * b(1, 2) + c(2, 2) * b(2, 2);
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::operator+(const Matrix3& a, float s)
{
Matrix3 m;
m[0] = a[0] + s;
m[1] = a[1] + s;
m[2] = a[2] + s;
return m;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::operator+(const Matrix3& a, const Matrix3& b)
{
Matrix3 m;
m[0] = a[0] + b[0];
m[1] = a[1] + b[1];
m[2] = a[2] + b[2];
return m;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::operator-(const Matrix3& a, float s)
{
Matrix3 m;
m[0] = a[0] - s;
m[1] = a[1] - s;
m[2] = a[2] - s;
return m;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::operator-(const Matrix3& a, const Matrix3& b)
{
Matrix3 m;
m[0] = a[0] - b[0];
m[1] = a[1] - b[1];
m[2] = a[2] - b[2];
return m;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::operator*(const Matrix3& a, float s)
{
Matrix3 m;
m[0] = a[0] * s;
m[1] = a[1] * s;
m[2] = a[2] * s;
return m;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::operator*(const Matrix3& a, const Matrix3& b)
{
Matrix3 m;
m(0, 0) = a(0, 0) * b(0, 0) + a(0, 1) * b(1, 0) + a(0, 2) * b(2, 0);
m(0, 1) = a(0, 0) * b(0, 1) + a(0, 1) * b(1, 1) + a(0, 2) * b(2, 1);
m(0, 2) = a(0, 0) * b(0, 2) + a(0, 1) * b(1, 2) + a(0, 2) * b(2, 2);
m(1, 0) = a(1, 0) * b(0, 0) + a(1, 1) * b(1, 0) + a(1, 2) * b(2, 0);
m(1, 1) = a(1, 0) * b(0, 1) + a(1, 1) * b(1, 1) + a(1, 2) * b(2, 1);
m(1, 2) = a(1, 0) * b(0, 2) + a(1, 1) * b(1, 2) + a(1, 2) * b(2, 2);
m(2, 0) = a(2, 0) * b(0, 0) + a(2, 1) * b(1, 0) + a(2, 2) * b(2, 0);
m(2, 1) = a(2, 0) * b(0, 1) + a(2, 1) * b(1, 1) + a(2, 2) * b(2, 1);
m(2, 2) = a(2, 0) * b(0, 2) + a(2, 1) * b(1, 2) + a(2, 2) * b(2, 2);
return m;
}
phanes::core::math::coretypes::Vector3 phanes::core::math::coretypes::operator*(const Matrix3& a, const Vector3& v)
{
Vector3 b = createVector(
a(0, 0) * v.x + a(0, 1) * v.y + a(0, 2) * v.z,
a(1, 0) * v.x + a(1, 1) * v.y + a(1, 2) * v.z,
a(2, 0) * v.x + a(2, 1) * v.y + a(2, 2) * v.z
);
return b;
}
bool phanes::core::math::coretypes::operator==(const Matrix3& a, const Matrix3& b)
{
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]) {
return true;
}
return false;
}
// =============================== //
// Matrix function definition //
// =============================== //
float phanes::core::math::coretypes::determinant(const Matrix3& a)
{
return (a(0, 0) * (a(1, 1) * a(2, 2) - a(1, 2) * a(2, 1))
-a(0, 1) * (a(1, 0) * a(2, 2) - a(1, 2) * a(2, 0))
+a(0, 2) * (a(1, 0) * a(2, 1) - a(1, 1) * a(2, 0)));
}
void phanes::core::math::coretypes::inverseNR(Matrix3& a)
{
const Vector3& v0 = a[0];
const Vector3& v1 = a[1];
const Vector3& v2 = a[2];
Vector3 r0 = crossP(v1, v2);
Vector3 r1 = crossP(v2, v0);
Vector3 r2 = crossP(v0, v1);
float _1_det = 1.0f / determinant(a);
a = createMatrix(
r0.x, r0.y, r0.z,
r1.x, r1.y, r1.z,
r2.x, r2.y, r2.z
);
a *= _1_det;
}
void phanes::core::math::coretypes::transposeNR(Matrix3& a)
{
swap(a(0, 1), a(1, 0));
swap(a(0, 2), a(2, 0));
swap(a(1, 2), a(2, 1));
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::inverse(Matrix3& a)
{
const Vector3& v0 = a[0];
const Vector3& v1 = a[1];
const Vector3& v2 = a[2];
Vector3 r0 = crossP(v1, v2);
Vector3 r1 = crossP(v2, v0);
Vector3 r2 = crossP(v0, v1);
float _1_det = 1.0f / determinant(a);
Matrix3 m = createMatrix(
r0.x, r0.y, r0.z,
r1.x, r1.y, r1.z,
r2.x, r2.y, r2.z
);
m *= _1_det;
return m;
}
phanes::core::math::coretypes::Matrix3 phanes::core::math::coretypes::transpose(const Matrix3& a)
{
Matrix3 m = createMatrix(
a(0, 0), a(1, 0), a(2, 0),
a(0, 1), a(1, 1), a(2, 1),
a(0, 2), a(1, 2), a(2, 2)
);
return m;
}
bool phanes::core::math::coretypes::isIndentityMatrix(const Matrix3& a)
{
if (a == phanes::core::math::coretypes::createMatrix(1, 0, 0, 0, 1, 0, 0, 0, 1)) {
return true;
}
return false;
}

View File

@ -0,0 +1,56 @@
// Math is independent from the rest of the library, to ensure seamless usage with other client.
#pragma once
#ifdef P_BUILD_LIB
#include "PhanesEnginePCH.h"
#else
#include <type_traits>
#endif
#ifdef P_WIN_BUILD
#ifdef P_DEBUG
#define P_DEBUGBREAK DebugBreak();
#else
#define P_DEBUGBREAK
#endif // P_DEBUG
#define FORCEINLINE __forceinline
#elif defined(P_UNIX_BUILD)
#error Only Windows is supported at the moment.
#elif defined(P_ARM_BUILD)
#error Only Windows is supported at the moment.
#else
#error The target system must be defined. (See https://github.com/scorpioblood/PhanesEngine for more information)
#endif // P_WIN_BUILD
// Typenames with RealType constrain have to be floating point numbers.
template<typename T>
concept RealType = std::is_floating_point_v<T>;
// Typenames with IntType constrain have to be integer number.
template<typename T>
concept IntType = std::is_integral_v<T>;

View File

@ -1,13 +1,13 @@
#pragma once #pragma once
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathCommon.h" #include "Core/public/Math/MathCommon.hpp"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"
#include "Core/public/Math/MathFwd.h" #include "Core/public/Math/MathFwd.h"
#include "Core/public/Math/IntVector2.h" #include "Core/public/Math/IntVector2.hpp"
#include "Core/public/Math/IntVector3.h" #include "Core/public/Math/IntVector3.hpp"
// #include "Core/public/Math/IntVector4.h" // #include "Core/public/Math/IntVector4.h"
#ifndef P_DEBUG #ifndef P_DEBUG

View File

@ -1,8 +1,6 @@
#pragma once #pragma once
#include "Core/Core.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Misc/Boilerplate.h"
#include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathCommon.hpp"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathCommon.hpp"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"

View File

@ -14,8 +14,6 @@
#define P_PI_FLT 3.1415926535897932f // PI #define P_PI_FLT 3.1415926535897932f // PI
#include "Core/Core.h"
#ifndef MATH_COMMON_H #ifndef MATH_COMMON_H
#define MATH_COMMON_H #define MATH_COMMON_H
@ -114,7 +112,7 @@ namespace Phanes::Core::Math {
template<typename T> template<typename T>
float FastInvSqrt(T n) float FastInvSqrt(T n)
{ {
Phanes::Core::Types::int32 i = *(int*)&n; int i = *(int*)&n;
float x2 = n * 0.5f; float x2 = n * 0.5f;
i = 0x5f3759df - (i >> 1); i = 0x5f3759df - (i >> 1);

View File

@ -1,13 +1,15 @@
#pragma once #pragma once
#include "Core/Core.h" #ifdef P_BUILD_LIB
#include "PhanesEnginePCH.h"
#include "Core/public/OSAL/PlatformTypes.h" #else
#include <vector>
#endif
#ifndef MATH_FWD_H #ifndef MATH_FWD_H
#define MATH_FWD_H #define MATH_FWD_H
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
/** /**
* Includes forward declarations, as well as certain useful typedefs. * Includes forward declarations, as well as certain useful typedefs.
@ -18,85 +20,92 @@
namespace Phanes::Core::Math { namespace Phanes::Core::Math {
/** /**
* Template forward declarations. * Template forward declarations.
*/ */
template<RealType T> struct TColor; template<RealType T> struct TColor;
template<RealType T> struct TLinearColor; template<RealType T> struct TLinearColor;
template<RealType T> struct TVector2; template<RealType T> struct TVector2;
template<RealType T> struct TVector3; template<RealType T> struct TVector3;
template<RealType T> struct TVector4; template<RealType T> struct TVector4;
template<RealType T> struct TRay; template<RealType T> struct TRay;
template<RealType T> struct TPlane; template<RealType T> struct TPlane;
template<RealType T> struct TMatrix2; template<RealType T> struct TMatrix2;
template<RealType T> struct TMatrix3; template<RealType T> struct TMatrix3;
template<RealType T> struct TMatrix4; template<RealType T> struct TMatrix4;
template<RealType T> struct TQuaternion; template<RealType T> struct TQuaternion;
template<RealType T> struct TTransform; template<RealType T> struct TTransform;
template<RealType T> struct TPoint2; template<RealType T> struct TPoint2;
template<RealType T> struct TPoint3; template<RealType T> struct TPoint3;
template<RealType T> struct TPoint4; template<RealType T> struct TPoint4;
template<IntType T> struct TIntVector2; template<IntType T> struct TIntVector2;
template<IntType T> struct TIntVector3; template<IntType T> struct TIntVector3;
template<IntType T> struct TIntVector4; template<IntType T> struct TIntVector4;
template<IntType T> struct TIntPoint2; template<IntType T> struct TIntPoint2;
template<IntType T> struct TIntPoint3; template<IntType T> struct TIntPoint3;
template<IntType T> struct TIntPoint4; template<IntType T> struct TIntPoint4;
/** /**
* Specific instantiation of forward declarations. * Specific instantiation of forward declarations.
*/ */
// TVector2 // TVector2
typedef TVector2<float> Vector2; typedef TVector2<float> Vector2;
typedef TVector2<double> Vector2d; typedef TVector2<double> Vector2d;
typedef std::vector<Vector2> Vector2List; typedef std::vector<Vector2> Vector2List;
typedef std::vector<Vector2d> Vector2Listd; typedef std::vector<Vector2d> Vector2Listd;
// TVector3 // TVector3
typedef TVector3<float> Vector3; typedef TVector3<float> Vector3;
typedef TVector3<double> Vector3d; typedef TVector3<double> Vector3d;
typedef std::vector<Vector3> Vector3List; typedef std::vector<Vector3> Vector3List;
typedef std::vector<Vector3d> Vector3Listd; typedef std::vector<Vector3d> Vector3Listd;
// TIntVector2 // TIntVector2
typedef TIntVector2<int> IntVector2; typedef TIntVector2<int> IntVector2;
typedef TIntVector2<long> IntVector2l; typedef TIntVector2<long> IntVector2l;
typedef std::vector<IntVector2> IntVector2List; typedef std::vector<IntVector2> IntVector2List;
typedef std::vector<IntVector2l> IntVector2Listl; typedef std::vector<IntVector2l> IntVector2Listl;
// TIntVector3 // TIntVector3
typedef TIntVector3<int> IntVector3; typedef TIntVector3<int> IntVector3;
typedef TIntVector3<long> IntVector3l; typedef TIntVector3<long> IntVector3l;
typedef std::vector<IntVector3> IntVector3List; typedef std::vector<IntVector3> IntVector3List;
typedef std::vector<IntVector3l> IntVector3Listl; typedef std::vector<IntVector3l> IntVector3Listl;
// TMatrix2 // TMatrix2
typedef TMatrix2<float> Matrix2; typedef TMatrix2<float> Matrix2;
typedef TMatrix2<double> Matrix2d; typedef TMatrix2<double> Matrix2d;
typedef std::vector<Matrix2> Matrix2List; typedef std::vector<Matrix2> Matrix2List;
typedef std::vector<Matrix2d> Matrix2Listd; typedef std::vector<Matrix2d> Matrix2Listd;
// TMatrix3
typedef TMatrix3<float> Matrix3;
typedef TMatrix3<double> Matrix3d;
typedef std::vector<Matrix3> Matrix3List;
typedef std::vector<Matrix3d> Matrix3Listd;
} // Phanes::Core::Math::coretypes } // Phanes::Core::Math::coretypes
namespace Phanes::Core::Math::Internal namespace Phanes::Core::Math::Internal
{ {
// Internal types // Internal types
template <typename T, unsigned int D> struct AVector; template <typename T, unsigned int D> struct AVector;
template <typename T, unsigned int n, unsigned int> struct AMatrix; template <typename T, unsigned int n, unsigned int> struct AMatrix;
} }

View File

@ -0,0 +1,20 @@
// Header file containing all files to be compiled to use core math.
// Should be included in PCH file of project to use.
#pragma once
#include "Point.hpp"
#include "Plane.hpp"
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "IntPoint.hpp"
#include "IntVector2.hpp"
#include "IntVector3.hpp"
#include "Matrix2.hpp"
#include "Matrix3.hpp"
#include "MathCommon.hpp"
#include "MathTypeConversion.hpp"
#include "MathUnitConversion.hpp"

View File

@ -6,8 +6,13 @@
// @ref [FILE]MathUnitConversion // // @ref [FILE]MathUnitConversion //
// ============================================= // // ============================================= //
#include "PhanesEnginePCH.h" #ifdef P_BUILD_LIB
#include "Core/public/Misc/Boilerplate.h" #include "PhanesEnginePCH.h"
#else
#include <string>
#endif
#include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"
#include "Core/public/Math/Vector2.hpp" #include "Core/public/Math/Vector2.hpp"
@ -16,7 +21,7 @@
#include "Core/public/Math/Matrix2.hpp" #include "Core/public/Math/Matrix2.hpp"
//#include "Core/public/Math/Matrix3.h" //#include "Core/public/Math/Matrix3.h"
#include "Core/public/Math/IntVector2.hpp" #include "Core/public/Math/IntVector2.hpp"
#include "Core/public/Math/IntVector3.h" #include "Core/public/Math/IntVector3.hpp"
#ifndef MATH_TYPE_CONVERSION_H #ifndef MATH_TYPE_CONVERSION_H
#define MATH_TYPE_CONVERSION_H #define MATH_TYPE_CONVERSION_H
@ -84,7 +89,7 @@ namespace Phanes::Core::Math {
template<RealType T> template<RealType T>
std::string toString(const TMatrix2<T>& m) std::string toString(const TMatrix2<T>& m)
{ {
return "[[" + ToString(m.m(0, 0)) + " " + ToString(m.m(0, 1)) + "], [" + ToString(m.m(0, 0)) + " " + ToString(m.m(0, 1)) + "]]" return "[[" + ToString(m.m(0, 0)) + " " + ToString(m.m(0, 1)) + "], [" + ToString(m.m(0, 0)) + " " + ToString(m.m(0, 1)) + "]]";
} }
//std::string toString(const Matrix3& v); //std::string toString(const Matrix3& v);

View File

@ -4,7 +4,7 @@
// Contains functions to convert units // // Contains functions to convert units //
// ======================================= // // ======================================= //
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathCommon.hpp"

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "Core/Core.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Misc/Boilerplate.h"
#include "Core/public/Math/Vector2.hpp" #include "Core/public/Math/Vector2.hpp"
@ -96,14 +95,6 @@ namespace Phanes::Core::Math {
return reinterpret_cast<TVector2*>(this->m[m]); return reinterpret_cast<TVector2*>(this->m[m]);
} }
FORCEINLINE const T& operator() (int n, int m) const
{
return this->m[m][n];
}
FORCEINLINE const TVector2<T>& operator[] (int m) const
{
return reinterpret_cast<const TVector2*>(this->m[m]);
}
}; };
// ===================== // // ===================== //
@ -190,7 +181,7 @@ namespace Phanes::Core::Math {
TMatrix2<T> operator+ (const TMatrix2<T>& m1, const TMatrix2<T>& m2) 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), 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); m1(1, 0) + m2(1, 0), m1(1, 1) + m2(1, 1));
} }
template<RealType T> template<RealType T>
@ -204,7 +195,7 @@ namespace Phanes::Core::Math {
TMatrix2<T> operator- (const TMatrix2<T>& m1, const TMatrix2<T>& m2) 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), 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); m1(1, 0) - m2(1, 0), m1(1, 1) - m2(1, 1));
} }
template<RealType T> template<RealType T>

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"
#include "Core/public/Math/Vector3.hpp" #include "Core/public/Math/Vector3.hpp"
@ -15,11 +15,11 @@ namespace Phanes::Core::Math {
// Accessed by M[Row][Col]. // Accessed by M[Row][Col].
template<RealType T> template<RealType T>
struct alignas(9) TMatrix3 struct TMatrix3
{ {
public: public:
alignas(9) T m[3][3]; T m[3][3];
public: public:
@ -35,16 +35,6 @@ namespace Phanes::Core::Math {
memcpy(this->m, m1.m, sizeof(T) * 9); memcpy(this->m, m1.m, sizeof(T) * 9);
} }
/**
* Move constructor.
*/
TMatrix3(TMatrix3<T>&& m)
{
this->m = m.m;
m.m = nullptr;
}
/** /**
* Construct Matrix from 2d array. * Construct Matrix from 2d array.
* *
@ -53,9 +43,9 @@ namespace Phanes::Core::Math {
TMatrix3(T fields[2][2]) TMatrix3(T fields[2][2])
{ {
this->m[0][0] = fields[0][0]; this->m[1][0] = fields[1][0]; this->[2][0] = fields[2][0]; this->m[0][0] = fields[0][0]; this->m[1][0] = fields[1][0]; this->m[2][0] = fields[2][0];
this->m[0][1] = fields[0][1]; this->m[1][1] = fields[1][1]; this->[2][1] = fields[2][1]; this->m[0][1] = fields[0][1]; this->m[1][1] = fields[1][1]; this->m[2][1] = fields[2][1];
this->m[0][2] = fields[0][2]; this->m[1][2] = fields[1][2]; this->[2][2] = fields[2][2]; this->m[0][2] = fields[0][2]; this->m[1][2] = fields[1][2]; this->m[2][2] = fields[2][2];
} }
/** /**
@ -73,9 +63,9 @@ namespace Phanes::Core::Math {
T n10, T n11, T n12, T n10, T n11, T n12,
T n20, T n21, T n22) T n20, T n21, T n22)
{ {
this->m[0][0] = n00; this->m[1][0] = n01; this->[2][0] = n02; this->m[0][0] = n00; this->m[1][0] = n01; this->m[2][0] = n02;
this->m[1][0] = n10; this->m[1][1] = n11; this->[2][1] = n12; this->m[1][0] = n10; this->m[1][1] = n11; this->m[2][1] = n12;
this->m[1][2] = n20; this->m[1][2] = n21; this->[2][2] = n22; this->m[1][2] = n20; this->m[1][2] = n21; this->m[2][2] = n22;
} }
/** /**
@ -87,31 +77,31 @@ namespace Phanes::Core::Math {
TMatrix3(const TVector3<T>& v1, const TVector3<T>& v2, const TVector3<T> v3) TMatrix3(const TVector3<T>& v1, const TVector3<T>& v2, const TVector3<T> v3)
{ {
this->m[0][0] = v1.x; this->m[1][0] = v2.x; this->[2][0] = v3.x; this->m[0][0] = v1.x; this->m[1][0] = v2.x; this->m[2][0] = v3.x;
this->m[0][1] = v1.y; this->m[1][1] = v2.y; this->[2][1] = v3.y; this->m[0][1] = v1.y; this->m[1][1] = v2.y; this->m[2][1] = v3.y;
this->m[0][2] = v1.z; this->m[1][2] = v2.z; this->[2][2] = v3.z; this->m[0][2] = v1.z; this->m[1][2] = v2.z; this->m[2][2] = v3.z;
} }
public: public:
FORCEINLINE T& operator() (int n, int m) FORCEINLINE T& operator() (int n, int m)
{ {
return this->[m][n]; return this->m[m][n];
} }
FORCEINLINE TVector3<T>& operator[] (int m) FORCEINLINE TVector3<T>& operator[] (int m)
{ {
return reinterpret_cast<TVector2<T>*>(this->m[m]); return (*reinterpret_cast<TVector3<T>*>(this->m[m]));
} }
FORCEINLINE const T& operator() (int n, int m) const FORCEINLINE const T& operator() (int n, int m) const
{ {
return this->[m][n]; return this->m[m][n];
} }
FORCEINLINE const TVector3<T>& operator[] (int m) const FORCEINLINE const TVector3<T>& operator[] (int m) const
{ {
return reinterpret_cast<const TVector2<T>*>(this->m[m]); return (*reinterpret_cast<TVector3<T>*>(this->m[m]));
} }
}; };
@ -129,11 +119,11 @@ namespace Phanes::Core::Math {
*/ */
template<RealType T> template<RealType T>
TMatrix3<T> operator+= (TMatrix3<T>& m, T s) TMatrix3<T> operator+= (TMatrix3<T>& m1, T s)
{ {
m(0, 0) += s; m(0, 1) += s; m(0, 2) += s; m1(0, 0) += s; m1(0, 1) += s; m1(0, 2) += s;
m(1, 0) += s; m(1, 1) += s; m(1, 2) += s; m1(1, 0) += s; m1(1, 1) += s; m1(1, 2) += s;
m(2, 0) += s; m(2, 1) += s; m(2, 2) += s; m1(2, 0) += s; m1(2, 1) += s; m1(2, 2) += s;
return m1; return m1;
} }
@ -163,11 +153,11 @@ namespace Phanes::Core::Math {
*/ */
template<RealType T> template<RealType T>
TMatrix3<T> operator-= (TMatrix3<T>& a, T s) TMatrix3<T> operator-= (TMatrix3<T>& m1, T s)
{ {
m(0, 0) -= s; m(0, 1) -= s; m(0, 2) -= s; m1(0, 0) -= s; m1(0, 1) -= s; m1(0, 2) -= s;
m(1, 0) -= s; m(1, 1) -= s; m(1, 2) -= s; m1(1, 0) -= s; m1(1, 1) -= s; m1(1, 2) -= s;
m(2, 0) -= s; m(2, 1) -= s; m(2, 2) -= s; m1(2, 0) -= s; m1(2, 1) -= s; m1(2, 2) -= s;
return m1; return m1;
} }
@ -197,11 +187,11 @@ namespace Phanes::Core::Math {
*/ */
template<RealType T> template<RealType T>
TMatrix3<T> operator*= (TMatrix3<T>& m, T s) TMatrix3<T> operator*= (TMatrix3<T>& m1, T s)
{ {
m(0, 0) *= s; m(0, 1) *= s; m(0, 2) *= s; m1(0, 0) *= s; m1(0, 1) *= s; m1(0, 2) *= s;
m(1, 0) *= s; m(1, 1) *= s; m(1, 2) *= s; m1(1, 0) *= s; m1(1, 1) *= s; m1(1, 2) *= s;
m(2, 0) *= s; m(2, 1) *= s; m(2, 2) *= s; m1(2, 0) *= s; m1(2, 1) *= s; m1(2, 2) *= s;
return m1; return m1;
} }
@ -216,7 +206,7 @@ namespace Phanes::Core::Math {
template<RealType T> template<RealType T>
TMatrix3<T> operator*= (TMatrix3<T>& m1, const TMatrix3<T>& m2) TMatrix3<T> operator*= (TMatrix3<T>& m1, const TMatrix3<T>& m2)
{ {
TMatrix3<T> c = a; TMatrix3<T> c = m1;
m1(0, 0) = c(0, 0) * m2(0, 0) + c(0, 1) * m2(1, 0) + c(0, 2) * m2(2, 0); m1(0, 0) = c(0, 0) * m2(0, 0) + c(0, 1) * m2(1, 0) + c(0, 2) * m2(2, 0);
m1(0, 1) = c(0, 0) * m2(0, 1) + c(0, 1) * m2(1, 1) + c(0, 2) * m2(2, 1); m1(0, 1) = c(0, 0) * m2(0, 1) + c(0, 1) * m2(1, 1) + c(0, 2) * m2(2, 1);
m1(0, 2) = c(0, 0) * m2(0, 2) + c(0, 1) * m2(1, 2) + c(0, 2) * m2(2, 2); m1(0, 2) = c(0, 0) * m2(0, 2) + c(0, 1) * m2(1, 2) + c(0, 2) * m2(2, 2);
@ -462,7 +452,7 @@ namespace Phanes::Core::Math {
TVector3<T> r1 = CrossP(v2, v0); TVector3<T> r1 = CrossP(v2, v0);
TVector3<T> r2 = CrossP(v0, v1); TVector3<T> r2 = CrossP(v0, v1);
T _1_det = (T)1.0 / determinant(m1); T _1_det = (T)1.0 / Determinant(m1);
TMatrix3<T> inverse(r0.x, r0.y, r0.z, TMatrix3<T> inverse(r0.x, r0.y, r0.z,
r1.x, r1.y, r1.z, r1.x, r1.y, r1.z,

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathFwd.h" #include "Core/public/Math/MathFwd.h"
#include "Core/public/Math/Vector3.hpp" #include "Core/public/Math/Vector3.hpp"

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathCommon.hpp"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "Core/Core.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Misc/Boilerplate.h"
#include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathCommon.hpp"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"

View File

@ -5,8 +5,8 @@
// TODO: ClampToCube // TODO: ClampToCube
// TODO: Slerp (using Quaternions) // TODO: Slerp (using Quaternions)
#include "Core/Core.h"
#include "Core/public/Misc/Boilerplate.h" #include "Core/public/Math/Boilerplate.h"
#include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathCommon.hpp"
#include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathAbstractTypes.h"

View File

@ -6,34 +6,38 @@
#define NOMAXMIN #define NOMAXMIN
#ifndef PHANES_CORE_PCH_H #ifndef PHANES_CORE_PCH_H
// STL // STL
#include <cmath> #include <cmath>
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <concepts> #include <concepts>
#include <type_traits> #include <type_traits>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#ifdef P_WIN_BUILD #ifdef P_WIN_BUILD
#include <windows.h> #include <windows.h>
#endif #endif
// spdlog
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h> // spdlog
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
// Local PCH
#include "Core/public/Math/MathPCH.h"
#endif // !PHANES_CORE_PCH_H #endif // !PHANES_CORE_PCH_H

View File

@ -1,11 +1,38 @@
#include "Core/public/Math/MathPCH.h"
#include <iostream> #include <iostream>
#include <chrono>
namespace PMath = Phanes::Core::Math;
int main() int main()
{ {
int x = 13;
x %= 7;
std::cout << x; PMath::TMatrix2<float> m1(7.3f,4.3f,
9.4f,2.5f);
for (int i = 0; i < 10; i++)
{
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 999999; i++)
{
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() << std::endl;
}
return 0; return 0;
} }