Change Engine/src to Engine/Source.
This commit is contained in:
74
Engine/Source/Runtime/Core/Core.h
Normal file
74
Engine/Source/Runtime/Core/Core.h
Normal file
@@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
// TODO: Refactor documentation
|
||||
|
||||
|
||||
#ifdef P_WIN_BUILD
|
||||
|
||||
#ifdef P_BUILD_LIB
|
||||
|
||||
#define PHANES_CORE __declspec(dllexport)
|
||||
|
||||
#else
|
||||
|
||||
#define PHANES_CORE __declspec(dllimport)
|
||||
|
||||
#endif
|
||||
|
||||
#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
|
||||
|
||||
|
||||
namespace Phanes
|
||||
{
|
||||
|
||||
// Alias for shared_ptr
|
||||
template<typename T>
|
||||
using Ref = std::shared_ptr<T>;
|
||||
|
||||
// Alias for make_shared
|
||||
template<typename T, typename ...Args>
|
||||
constexpr Ref<T> MakeRef(Args&& ...args)
|
||||
{
|
||||
return std::make_shared<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Alias for unique ptr
|
||||
template<typename T>
|
||||
using Scope = std::unique_ptr<T>;
|
||||
|
||||
// Alias for make_unique
|
||||
template<typename T, typename ...Args>
|
||||
constexpr Scope<T> MakeScope(Args&& ...args)
|
||||
{
|
||||
return std::make_unique<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int test()
|
||||
{
|
||||
|
||||
}
|
25
Engine/Source/Runtime/Core/Include.h
Normal file
25
Engine/Source/Runtime/Core/Include.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
// --- Logging -------------------------------------
|
||||
|
||||
#include "Core/public/Logging/Logging.h"
|
||||
|
||||
// --- Starting point ------------------------------
|
||||
#include "Core/public/StartingPoint/StartingPoint.h"
|
||||
#include "Core/public/StartingPoint/EntryPoint.h"
|
||||
|
||||
|
||||
|
||||
|
||||
// --- OSAL ----------------------------------------
|
||||
|
||||
#include "Core/public/OSAL/PlatformTypes.h"
|
||||
|
||||
|
||||
#ifdef P_USE_NAMESPACE_ALIAS
|
||||
|
||||
|
||||
// Starting point
|
||||
namespace PApp = Phanes::Core::Application;
|
||||
|
||||
#endif
|
14
Engine/Source/Runtime/Core/private/Logging/Logging.cpp
Normal file
14
Engine/Source/Runtime/Core/private/Logging/Logging.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "PhanesEnginePCH.h"
|
||||
|
||||
#include "Core/public/Logging/Logging.h"
|
||||
|
||||
void Phanes::Core::Logging::Init()
|
||||
{
|
||||
spdlog::set_pattern("%^[%n][%T][%l]:%$ %v");
|
||||
|
||||
_PEngineLogger = spdlog::stdout_color_mt("PHANES");
|
||||
_PEngineLogger->set_level(spdlog::level::trace);
|
||||
|
||||
_PAppLogger = spdlog::stdout_color_mt("GAME");
|
||||
_PAppLogger->set_level(spdlog::level::trace);
|
||||
}
|
347
Engine/Source/Runtime/Core/private/Math/Matrix4.cpp
Normal file
347
Engine/Source/Runtime/Core/private/Math/Matrix4.cpp
Normal file
@@ -0,0 +1,347 @@
|
||||
// ========================== //
|
||||
// Matrix4 implementation //
|
||||
// ========================== //
|
||||
|
||||
#include "Misc/CommonDefines.h"
|
||||
#include PHANES_CORE_PCH_DEFAULT_PATH
|
||||
|
||||
#include "Math/Matrix4.h"
|
||||
|
||||
#include "Math/Vector3.h"
|
||||
|
||||
|
||||
// ================================= //
|
||||
// Class Methods for easy access //
|
||||
// ================================= //
|
||||
|
||||
float& phanes::core::math::coretypes::Matrix4::operator()(int n, int m)
|
||||
{
|
||||
return this->fields[m][n];
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4& phanes::core::math::coretypes::Matrix4::operator[](int m)
|
||||
{
|
||||
return (*reinterpret_cast<Vector4*>(this->fields[m]));
|
||||
}
|
||||
|
||||
const float& phanes::core::math::coretypes::Matrix4::operator()(int n, int m) const
|
||||
{
|
||||
return this->fields[m][n];
|
||||
}
|
||||
|
||||
const phanes::core::math::coretypes::Vector4& phanes::core::math::coretypes::Matrix4::operator[](int m) const
|
||||
{
|
||||
return (*reinterpret_cast<const Vector4*>(this->fields[m]));
|
||||
}
|
||||
|
||||
|
||||
// ================= //
|
||||
// Constructors //
|
||||
// ================= //
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::createMatrix(float fields[4][4])
|
||||
{
|
||||
Matrix4 a;
|
||||
std::copy(&fields[0][0], &fields[4][4], &a.fields[0][0]);
|
||||
return a;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::createMatrix(float f00, float f01, float f02, float f03, float f10, float f11, float f12, float f13, float f20, float f21, float f22, float f23, float f30, float f31, float f32, float f33)
|
||||
{
|
||||
Matrix4 a;
|
||||
a.fields[0][0] = f00; a.fields[1][0] = f01; a.fields[2][0] = f02; a.fields[3][0] = f03;
|
||||
a.fields[0][1] = f10; a.fields[1][1] = f11; a.fields[2][1] = f12; a.fields[3][1] = f13;
|
||||
a.fields[0][2] = f20; a.fields[1][2] = f21; a.fields[2][2] = f22; a.fields[3][2] = f23;
|
||||
a.fields[0][3] = f30; a.fields[1][3] = f31; a.fields[2][3] = f32; a.fields[3][3] = f33;
|
||||
return a;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::createMatrix(const Vector4& a, const Vector4& b, const Vector4& c, const Vector4& d)
|
||||
{
|
||||
Matrix4 m;
|
||||
m.fields[0][0] = a.x; m.fields[1][0] = b.x; m.fields[2][0] = c.x; m.fields[3][0] = d.x;
|
||||
m.fields[0][1] = a.y; m.fields[1][1] = b.y; m.fields[2][1] = c.y; m.fields[3][1] = d.y;
|
||||
m.fields[0][2] = a.z; m.fields[1][2] = b.z; m.fields[2][2] = c.z; m.fields[3][2] = d.z;
|
||||
m.fields[0][3] = a.w; m.fields[1][3] = b.w; m.fields[2][3] = c.w; m.fields[3][3] = d.w;
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
// ============= //
|
||||
// Operators //
|
||||
// ============= //
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::operator+=(Matrix4& a, float s)
|
||||
{
|
||||
a[0] += s;
|
||||
a[1] += s;
|
||||
a[2] += s;
|
||||
a[3] += s;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator+=(Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
a[0] += b[0];
|
||||
a[1] += b[1];
|
||||
a[2] += b[2];
|
||||
a[3] += b[3];
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator-=(Matrix4& a, float s)
|
||||
{
|
||||
a[0] -= s;
|
||||
a[1] -= s;
|
||||
a[2] -= s;
|
||||
a[3] -= s;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator-=(Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
a[0] -= b[0];
|
||||
a[1] -= b[1];
|
||||
a[2] -= b[2];
|
||||
a[3] -= b[3];
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator*=(Matrix4& a, float s)
|
||||
{
|
||||
a[0] *= s;
|
||||
a[1] *= s;
|
||||
a[2] *= s;
|
||||
a[3] *= s;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator*=(Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
Matrix4 c = a;
|
||||
|
||||
a(0, 0) = c(0, 0) * b(0, 0) + c(0, 1) * b(1, 0) + c(0, 2) * b(2, 0) + c(0, 3) * b(3, 0);
|
||||
a(0, 1) = c(0, 0) * b(0, 1) + c(0, 1) * b(1, 1) + c(0, 2) * b(2, 1) + c(0, 3) * b(3, 1);
|
||||
a(0, 2) = c(0, 0) * b(0, 2) + c(0, 1) * b(1, 2) + c(0, 2) * b(2, 2) + c(0, 3) * b(3, 2);
|
||||
a(0, 3) = c(0, 0) * b(0, 3) + c(0, 1) * b(1, 3) + c(0, 2) * b(2, 3) + c(0, 3) * b(3, 3);
|
||||
|
||||
a(1, 0) = c(1, 0) * b(0, 0) + c(1, 1) * b(1, 0) + c(1, 2) * b(2, 0) + c(1, 3) * b(3, 0);
|
||||
a(1, 1) = c(1, 0) * b(0, 1) + c(1, 1) * b(1, 1) + c(1, 2) * b(2, 1) + c(1, 3) * b(3, 1);
|
||||
a(1, 2) = c(1, 0) * b(0, 2) + c(1, 1) * b(1, 2) + c(1, 2) * b(2, 2) + c(1, 3) * b(3, 2);
|
||||
a(1, 3) = c(1, 0) * b(0, 3) + c(1, 1) * b(1, 3) + c(1, 2) * b(2, 3) + c(1, 3) * b(3, 3);
|
||||
|
||||
a(2, 0) = c(2, 0) * b(0, 0) + c(2, 1) * b(1, 0) + c(2, 2) * b(2, 0) + c(2, 3) * b(3, 0);
|
||||
a(2, 1) = c(2, 0) * b(0, 1) + c(2, 1) * b(1, 1) + c(2, 2) * b(2, 1) + c(2, 3) * b(3, 1);
|
||||
a(2, 2) = c(2, 0) * b(0, 2) + c(2, 1) * b(1, 2) + c(2, 2) * b(2, 2) + c(2, 3) * b(3, 2);
|
||||
a(2, 3) = c(2, 0) * b(0, 3) + c(2, 1) * b(1, 3) + c(2, 2) * b(2, 3) + c(2, 3) * b(3, 3);
|
||||
|
||||
a(3, 0) = c(3, 0) * b(0, 0) + c(3, 1) * b(1, 0) + c(3, 2) * b(2, 0) + c(3, 3) * b(3, 0);
|
||||
a(3, 1) = c(3, 0) * b(0, 1) + c(3, 1) * b(1, 1) + c(3, 2) * b(2, 1) + c(3, 3) * b(3, 1);
|
||||
a(3, 2) = c(3, 0) * b(0, 2) + c(3, 1) * b(1, 2) + c(3, 2) * b(2, 2) + c(3, 3) * b(3, 2);
|
||||
a(3, 3) = c(3, 0) * b(0, 3) + c(3, 1) * b(1, 3) + c(3, 2) * b(2, 3) + c(3, 3) * b(3, 3);
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::operator+(const Matrix4& a, float s)
|
||||
{
|
||||
Matrix4 m;
|
||||
m[0] = a[0] + s;
|
||||
m[1] = a[1] + s;
|
||||
m[2] = a[2] + s;
|
||||
m[3] = a[3] + s;
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::operator+(const Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
Matrix4 m;
|
||||
m[0] = a[0] + b[0];
|
||||
m[1] = a[1] + b[1];
|
||||
m[2] = a[2] + b[2];
|
||||
m[3] = a[3] + b[3];
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::operator-(const Matrix4& a, float s)
|
||||
{
|
||||
Matrix4 m;
|
||||
m[0] = a[0] - s;
|
||||
m[1] = a[1] - s;
|
||||
m[2] = a[2] - s;
|
||||
m[3] = a[3] - s;
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::operator-(const Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
Matrix4 m;
|
||||
m[0] = a[0] - b[0];
|
||||
m[1] = a[1] - b[1];
|
||||
m[2] = a[2] - b[2];
|
||||
m[3] = a[3] - b[3];
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::operator*(const Matrix4& a, float s)
|
||||
{
|
||||
Matrix4 m;
|
||||
m[0] = a[0] * s;
|
||||
m[1] = a[1] * s;
|
||||
m[2] = a[2] * s;
|
||||
m[3] = a[3] * s;
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::operator*(const Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
Matrix4 m;
|
||||
|
||||
m(0, 0) = a(0, 0) * b(0, 0) + a(0, 1) * b(1, 0) + a(0, 2) * b(2, 0) + a(0, 3) * b(3, 0);
|
||||
m(0, 1) = a(0, 0) * b(0, 1) + a(0, 1) * b(1, 1) + a(0, 2) * b(2, 1) + a(0, 3) * b(3, 1);
|
||||
m(0, 2) = a(0, 0) * b(0, 2) + a(0, 1) * b(1, 2) + a(0, 2) * b(2, 2) + a(0, 3) * b(3, 2);
|
||||
m(0, 3) = a(0, 0) * b(0, 3) + a(0, 1) * b(1, 3) + a(0, 2) * b(2, 3) + a(0, 3) * b(3, 3);
|
||||
|
||||
m(1, 0) = a(1, 0) * b(0, 0) + a(1, 1) * b(1, 0) + a(1, 2) * b(2, 0) + a(1, 3) * b(3, 0);
|
||||
m(1, 1) = a(1, 0) * b(0, 1) + a(1, 1) * b(1, 1) + a(1, 2) * b(2, 1) + a(1, 3) * b(3, 1);
|
||||
m(1, 2) = a(1, 0) * b(0, 2) + a(1, 1) * b(1, 2) + a(1, 2) * b(2, 2) + a(1, 3) * b(3, 2);
|
||||
m(1, 3) = a(1, 0) * b(0, 3) + a(1, 1) * b(1, 3) + a(1, 2) * b(2, 3) + a(1, 3) * b(3, 3);
|
||||
|
||||
m(2, 0) = a(2, 0) * b(0, 0) + a(2, 1) * b(1, 0) + a(2, 2) * b(2, 0) + a(2, 3) * b(3, 0);
|
||||
m(2, 1) = a(2, 0) * b(0, 1) + a(2, 1) * b(1, 1) + a(2, 2) * b(2, 1) + a(2, 3) * b(3, 1);
|
||||
m(2, 2) = a(2, 0) * b(0, 2) + a(2, 1) * b(1, 2) + a(2, 2) * b(2, 2) + a(2, 3) * b(3, 2);
|
||||
m(2, 3) = a(2, 0) * b(0, 3) + a(2, 1) * b(1, 3) + a(2, 2) * b(2, 3) + a(2, 3) * b(3, 3);
|
||||
|
||||
m(3, 0) = a(3, 0) * b(0, 0) + a(3, 1) * b(1, 0) + a(3, 2) * b(2, 0) + a(3, 3) * b(3, 0);
|
||||
m(3, 1) = a(3, 0) * b(0, 1) + a(3, 1) * b(1, 1) + a(3, 2) * b(2, 1) + a(3, 3) * b(3, 1);
|
||||
m(3, 2) = a(3, 0) * b(0, 2) + a(3, 1) * b(1, 2) + a(3, 2) * b(2, 2) + a(3, 3) * b(3, 2);
|
||||
m(3, 3) = a(3, 0) * b(0, 3) + a(3, 1) * b(1, 3) + a(3, 2) * b(2, 3) + a(3, 3) * b(3, 3);
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator*(const Matrix4& a, const Vector4& v)
|
||||
{
|
||||
Vector4 b = createVector(
|
||||
a(0, 0) * v.x + a(0, 1) * v.y + a(0, 2) * v.z + a(0, 3) * v.w,
|
||||
a(1, 0) * v.x + a(1, 1) * v.y + a(1, 2) * v.z + a(1, 3) * v.w,
|
||||
a(2, 0) * v.x + a(2, 1) * v.y + a(2, 2) * v.z + a(2, 3) * v.w,
|
||||
a(3, 0) * v.x + a(3, 1) * v.y + a(3, 2) * v.z + a(3, 3) * v.w
|
||||
);
|
||||
return b;
|
||||
}
|
||||
|
||||
bool phanes::core::math::coretypes::operator==(const Matrix4& a, const Matrix4& b)
|
||||
{
|
||||
if (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// =============================== //
|
||||
// Matrix function definition //
|
||||
// =============================== //
|
||||
|
||||
|
||||
float phanes::core::math::coretypes::determinant(const Matrix4& a)
|
||||
{
|
||||
Vector3 v0 = reinterpret_cast<const Vector3&>(a[0]);
|
||||
Vector3 v1 = reinterpret_cast<const Vector3&>(a[1]);
|
||||
Vector3 v2 = reinterpret_cast<const Vector3&>(a[2]);
|
||||
Vector3 v3 = reinterpret_cast<const Vector3&>(a[3]);
|
||||
|
||||
Vector3 s = crossP(v0, v1);
|
||||
Vector3 t = crossP(v2, v3);
|
||||
Vector3 u = a(3, 1) * v0 + a(3, 0) * v1;
|
||||
Vector3 v = a(3, 3) * v2 + a(3, 2) * v3;
|
||||
|
||||
return s * v + t * u;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::inverseNR(Matrix4& a)
|
||||
{
|
||||
Vector3 v0 = reinterpret_cast<const Vector3&>(a[0]);
|
||||
Vector3 v1 = reinterpret_cast<const Vector3&>(a[1]);
|
||||
Vector3 v2 = reinterpret_cast<const Vector3&>(a[2]);
|
||||
Vector3 v3 = reinterpret_cast<const Vector3&>(a[3]);
|
||||
|
||||
Vector3 s = crossP(v0, v1);
|
||||
Vector3 t = crossP(v2, v3);
|
||||
Vector3 u = a(3, 1) * v0 + a(3, 0) * v1;
|
||||
Vector3 v = a(3, 3) * v2 + a(3, 2) * v3;
|
||||
|
||||
float _1_det = 1.0f / determinant(a);
|
||||
|
||||
Vector3 r0 = crossP(v1, v) + t * a(3, 1);
|
||||
Vector3 r1 = crossP(v, v0) - t * a(3, 0);
|
||||
Vector3 r2 = crossP(v3, u) + s * a(3, 3);
|
||||
Vector3 r3 = crossP(u, v2) - s * a(3, 2);
|
||||
|
||||
a = createMatrix(
|
||||
r0.x, r0.y, r0.z, -dotP(v1, t),
|
||||
r1.x, r1.y, r1.z, -dotP(v0, t),
|
||||
r2.x, r2.y, r2.z, -dotP(v3, s),
|
||||
r3.x, r3.y, r3.z, -dotP(v2, s)
|
||||
);
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::transposeNR(Matrix4& a)
|
||||
{
|
||||
swap(a(0, 1), a(1, 0));
|
||||
swap(a(0, 2), a(2, 0));
|
||||
swap(a(0, 3), a(3, 0));
|
||||
|
||||
swap(a(1, 2), a(2, 1));
|
||||
swap(a(1, 3), a(3, 1));
|
||||
|
||||
swap(a(2, 3), a(3, 2));
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::inverse(Matrix4& a)
|
||||
{
|
||||
Vector3 v0 = reinterpret_cast<const Vector3&>(a[0]);
|
||||
Vector3 v1 = reinterpret_cast<const Vector3&>(a[1]);
|
||||
Vector3 v2 = reinterpret_cast<const Vector3&>(a[2]);
|
||||
Vector3 v3 = reinterpret_cast<const Vector3&>(a[3]);
|
||||
|
||||
Vector3 s = crossP(v0, v1);
|
||||
Vector3 t = crossP(v2, v3);
|
||||
Vector3 u = a(3, 1) * v0 + a(3, 0) * v1;
|
||||
Vector3 v = a(3, 3) * v2 + a(3, 2) * v3;
|
||||
|
||||
float _1_det = 1.0f / determinant(a);
|
||||
|
||||
Vector3 r0 = crossP(v1, v) + t * a(3, 1);
|
||||
Vector3 r1 = crossP(v, v0) - t * a(3, 0);
|
||||
Vector3 r2 = crossP(v3, u) + s * a(3, 3);
|
||||
Vector3 r3 = crossP(u, v2) - s * a(3, 2);
|
||||
|
||||
Matrix4 m = createMatrix(
|
||||
r0.x, r0.y, r0.z, -dotP(v1, t),
|
||||
r1.x, r1.y, r1.z, -dotP(v0, t),
|
||||
r2.x, r2.y, r2.z, -dotP(v3, s),
|
||||
r3.x, r3.y, r3.z, -dotP(v2, s)
|
||||
);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Matrix4 phanes::core::math::coretypes::transpose(const Matrix4& a)
|
||||
{
|
||||
Matrix4 m = a;
|
||||
|
||||
swap(m(0, 1), m(1, 0));
|
||||
swap(m(0, 2), m(2, 0));
|
||||
swap(m(0, 3), m(3, 0));
|
||||
|
||||
swap(m(1, 2), m(2, 1));
|
||||
swap(m(1, 3), m(3, 1));
|
||||
|
||||
swap(m(2, 3), m(3, 2));
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
bool phanes::core::math::coretypes::isIndentityMatrix(const Matrix4& a)
|
||||
{
|
||||
if (a == phanes::core::math::coretypes::createMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
388
Engine/Source/Runtime/Core/private/Math/Vector4.cpp
Normal file
388
Engine/Source/Runtime/Core/private/Math/Vector4.cpp
Normal file
@@ -0,0 +1,388 @@
|
||||
// ========================== //
|
||||
// Vector4 implementation //
|
||||
// ========================== //
|
||||
|
||||
|
||||
#include "Misc/CommonDefines.h"
|
||||
#include PHANES_CORE_PCH_DEFAULT_PATH
|
||||
|
||||
#include "Math/Vector4.h"
|
||||
|
||||
#include "Math/Vector3.h"
|
||||
#include "Math/Vector2.h"
|
||||
|
||||
|
||||
// ===================== //
|
||||
// Vector4 operators //
|
||||
// ===================== //
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::createVector(float x, float y, float z, float w)
|
||||
{
|
||||
Vector4 _new{};
|
||||
_new.x = x;
|
||||
_new.y = y;
|
||||
_new.z = z;
|
||||
_new.w = w;
|
||||
return _new;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::operator+=(Vector4& a, float s)
|
||||
{
|
||||
a.x += s;
|
||||
a.y += s;
|
||||
a.z += s;
|
||||
a.w += s;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::operator+= (phanes::core::math::coretypes::Vector4& a, const phanes::core::math::coretypes::Vector4& b) {
|
||||
a.x += b.x;
|
||||
a.y += b.y;
|
||||
a.z += b.z;
|
||||
a.w += b.w;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator-=(Vector4& a, float s)
|
||||
{
|
||||
a.x -= s;
|
||||
a.y -= s;
|
||||
a.z -= s;
|
||||
a.w -= s;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::operator-= (phanes::core::math::coretypes::Vector4& a, const phanes::core::math::coretypes::Vector4& b) {
|
||||
a.x -= b.x;
|
||||
a.y -= b.y;
|
||||
a.z -= b.z;
|
||||
a.w -= b.w;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::operator*= (phanes::core::math::coretypes::Vector4& a, float s) {
|
||||
a.x *= s;
|
||||
a.y *= s;
|
||||
a.z *= s;
|
||||
a.w *= s;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::operator/= (phanes::core::math::coretypes::Vector4& a, float s) {
|
||||
s = 1.0f / s;
|
||||
a.x *= s;
|
||||
a.y *= s;
|
||||
a.z *= s;
|
||||
a.w *= s;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator*(const Vector4& a, float s)
|
||||
{
|
||||
Vector4 v;
|
||||
v.x = a.x * s;
|
||||
v.y = a.y * s;
|
||||
v.z = a.z * s;
|
||||
v.w = a.w * s;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator*(float s, const Vector4& a)
|
||||
{
|
||||
Vector4 v;
|
||||
v.x = a.x * s;
|
||||
v.y = a.y * s;
|
||||
v.z = a.z * s;
|
||||
v.w = a.w * s;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator/(const Vector4& a, float s)
|
||||
{
|
||||
Vector4 v;
|
||||
s = 1.0f / s;
|
||||
v.x = a.x * s;
|
||||
v.y = a.y * s;
|
||||
v.z = a.z * s;
|
||||
v.w = a.w * s;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator/(float s, const Vector4& a)
|
||||
{
|
||||
Vector4 v;
|
||||
s = 1.0f / s;
|
||||
v.x = a.x * s;
|
||||
v.y = a.y * s;
|
||||
v.z = a.z * s;
|
||||
v.w = a.w * s;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
float phanes::core::math::coretypes::operator*(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator+(const Vector4& a, float s)
|
||||
{
|
||||
Vector4 v;
|
||||
v.x = a.x + s;
|
||||
v.y = a.y + s;
|
||||
v.z = a.z + s;
|
||||
v.w = a.w + s;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator+(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
Vector4 v;
|
||||
v.x = a.x + b.x;
|
||||
v.y = a.y + b.y;
|
||||
v.z = a.z + b.z;
|
||||
v.w = a.w + b.w;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator-(const Vector4& a, float s)
|
||||
{
|
||||
Vector4 v;
|
||||
v.x = a.x - s;
|
||||
v.y = a.y - s;
|
||||
v.z = a.z - s;
|
||||
v.w = a.w - s;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::operator-(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
Vector4 v;
|
||||
v.x = a.x - b.x;
|
||||
v.y = a.y - b.y;
|
||||
v.z = a.z - b.z;
|
||||
v.w = a.w - b.w;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::operator-(Vector4& a)
|
||||
{
|
||||
a.x = -a.x;
|
||||
a.y = -a.y;
|
||||
a.z = -a.z;
|
||||
a.w = -a.w;
|
||||
}
|
||||
|
||||
|
||||
bool phanes::core::math::coretypes::operator==(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
if (abs(a.x - b.x) <= P_FLT_INAC and abs(a.y - b.y) <= P_FLT_INAC and abs(a.z - b.z) <= P_FLT_INAC and abs(a.w - b.w) <= P_FLT_INAC)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ====================================== //
|
||||
// Vector4 function implementation //
|
||||
// ====================================== //
|
||||
|
||||
|
||||
float phanes::core::math::coretypes::magnitude(const Vector4& a)
|
||||
{
|
||||
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
|
||||
}
|
||||
|
||||
float phanes::core::math::coretypes::sqrMagnitude(const Vector4& a)
|
||||
{
|
||||
return a.x * a.x + a.y * a.y + a.z * a.z;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::normalizeNR(Vector4& a)
|
||||
{
|
||||
a /= sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||
}
|
||||
|
||||
|
||||
float phanes::core::math::coretypes::angle(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
return acosf(dotP(a, b) / (magnitude(a) * magnitude(b)));
|
||||
}
|
||||
|
||||
|
||||
float phanes::core::math::coretypes::dotP(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::orthogonolize(Vector4& a, Vector4& b, Vector4& c)
|
||||
{
|
||||
Set(b, b - project(b, a));
|
||||
Set(c, c - project(c, a) - project(c, b));
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::orthoNormalize(Vector4& a, Vector4& b, Vector4& c)
|
||||
{
|
||||
Set(b, b - project(b, a));
|
||||
Set(c, c - project(c, a) - project(c, b));
|
||||
|
||||
a /= sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||
b /= sqrtf(b.x * b.x + b.y * b.y + b.z * b.z);
|
||||
c /= sqrtf(c.x * c.x + c.y * c.y + c.z * c.z);
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::scaleToMagnitude(Vector4& a, float size)
|
||||
{
|
||||
a /= sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||
a *= size;
|
||||
}
|
||||
|
||||
bool phanes::core::math::coretypes::equals(const Vector4& a, const Vector4& b, float threshold)
|
||||
{
|
||||
if (abs(a.x - b.x) <= threshold and abs(a.y - b.y) <= threshold and abs(a.z - b.z) <= threshold)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::perpspectiveDivideNR(Vector4& a)
|
||||
{
|
||||
float _z = 1.0f / a.z;
|
||||
a.x *= _z;
|
||||
a.y *= _z;
|
||||
a.z = 0.0f;
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::maxNR(Vector4& a, const Vector4& b)
|
||||
{
|
||||
a.x = phanes::core::math::max(a.x, b.x);
|
||||
a.y = phanes::core::math::max(a.y, b.y);
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::minNR(Vector4& a, const Vector4& b)
|
||||
{
|
||||
a.x = phanes::core::math::min(a.x, b.x);
|
||||
a.y = phanes::core::math::min(a.y, b.y);
|
||||
}
|
||||
|
||||
void phanes::core::math::coretypes::scaleNR(Vector4& a, const Vector4& b)
|
||||
{
|
||||
a.x *= b.x;
|
||||
a.y *= b.y;
|
||||
a.z *= b.z;
|
||||
}
|
||||
|
||||
// projects vector a onto vector b
|
||||
void phanes::core::math::coretypes::projectNR(Vector4& a, const Vector4& b)
|
||||
{
|
||||
float x = (a * b) / (b * b);
|
||||
a.x = x * b.x;
|
||||
a.y = x * b.y;
|
||||
a.z = x * b.z;
|
||||
}
|
||||
|
||||
// rejects vector a from vector b
|
||||
void phanes::core::math::coretypes::rejectNR(Vector4& a, const Vector4& b)
|
||||
{
|
||||
float x = ((a * b) / (b * b));
|
||||
a.x -= x * b.x;
|
||||
a.y -= x * b.y;
|
||||
a.z -= x * b.z;
|
||||
}
|
||||
|
||||
|
||||
void phanes::core::math::coretypes::Set(Vector4& a, const Vector4& b)
|
||||
{
|
||||
a.x = b.x;
|
||||
a.y = b.y;
|
||||
a.z = b.z;
|
||||
}
|
||||
|
||||
|
||||
// WITH RETURN: //
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::perpspectiveDivide(const Vector4& a)
|
||||
{
|
||||
float _z = 1.0f / a.z;
|
||||
return createVector(a.x * _z, a.y * _z, a.z * _z, 0.0f);
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::lerp(const Vector4& a, const Vector4& b, float t)
|
||||
{
|
||||
t = phanes::core::math::clamp(t, .0f, 1.0f);
|
||||
return a * (1 - t) + t * b;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::lerpUnclamped(const Vector4& a, const Vector4& b, float t)
|
||||
{
|
||||
return a * (1 - t) + t * b;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::slerp(const Vector4& a, const Vector4& b, float t)
|
||||
{
|
||||
t = phanes::core::math::clamp(t, 0.0f, 1.0f);
|
||||
Vector4 _a = normalize(a);
|
||||
Vector4 _b = normalize(b);
|
||||
float _angle = angle(_a, _b);
|
||||
return (((sinf(1.0f - t) * _angle) / sinf(_angle)) * _a) + ((sinf(t * _angle) / sinf(_angle)) * _b);
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::slerpUnclamped(const Vector4& a, const Vector4& b, float t)
|
||||
{
|
||||
Vector4 _a = normalize(a);
|
||||
Vector4 _b = normalize(b);
|
||||
float _angle = angle(_a, _b);
|
||||
return (((sinf(1.0f - t) * _angle) / sinf(_angle)) * _a) + ((sinf(t * _angle) / sinf(_angle)) * _b);
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::max(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
Vector4 _new{};
|
||||
_new.x = phanes::core::math::max(a.x, b.x);
|
||||
_new.y = phanes::core::math::max(a.y, b.y);
|
||||
_new.z = phanes::core::math::max(a.z, b.z);
|
||||
return _new;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::min(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
Vector4 _new{};
|
||||
_new.x = phanes::core::math::min(a.x, b.x);
|
||||
_new.y = phanes::core::math::min(a.y, b.y);
|
||||
_new.z = phanes::core::math::min(a.z, b.z);
|
||||
return _new;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::scale(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
return createVector(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::project(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
return ((a * b) / (b * b)) * b;
|
||||
}
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::reject(const Vector4& a, const Vector4& b)
|
||||
{
|
||||
return a - ((a * b) / (b * b)) * b;
|
||||
}
|
||||
|
||||
|
||||
phanes::core::math::coretypes::Vector4 phanes::core::math::coretypes::normalize(const Vector4& a)
|
||||
{
|
||||
return a / sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
#include "PhanesEnginePCH.h"
|
||||
|
||||
#define P_USE_NAMESPACE_ALIAS
|
||||
#define P_TEST
|
||||
|
||||
#include "Core/public/StartingPoint/StartingPoint.h"
|
||||
|
||||
static void IdleMsg()
|
||||
{
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
SetConsoleTextAttribute(hConsole, 12);
|
||||
|
||||
std::cout << "Welcome to PhanesEngine!" << std::endl << std::endl;
|
||||
|
||||
SetConsoleTextAttribute(hConsole, 15);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
|
||||
std::cout << "It's silent..." << std::endl << std::endl;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
|
||||
std::cout << "To silent." << std::endl;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
|
||||
std::cout << "\nI will go now" << std::endl;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(4));
|
||||
|
||||
std::cout << "\nGood by!" << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Phanes::Core::Application::PhanesProject::PhanesProject(std::string _ProjectName) : projectName(_ProjectName)
|
||||
{
|
||||
}
|
||||
|
||||
Phanes::Core::Application::PhanesProject::~PhanesProject()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string Phanes::Core::Application::PhanesProject::GetName()
|
||||
{
|
||||
return this->projectName;
|
||||
}
|
||||
|
||||
void Phanes::Core::Application::PhanesProject::Run()
|
||||
{
|
||||
std::cin.get();
|
||||
}
|
57
Engine/Source/Runtime/Core/public/Logging/Logging.h
Normal file
57
Engine/Source/Runtime/Core/public/Logging/Logging.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include "PhanesEnginePCH.h"
|
||||
|
||||
#include "Core/Core.h"
|
||||
|
||||
#ifndef P_DEBUG
|
||||
#pragma warning(disable : 4251) // Disable STL dll export warning
|
||||
#endif
|
||||
|
||||
namespace Phanes::Core::Logging
|
||||
{
|
||||
static std::shared_ptr<spdlog::logger> _PEngineLogger;
|
||||
static std::shared_ptr<spdlog::logger> _PAppLogger;
|
||||
|
||||
PHANES_CORE void Init();
|
||||
|
||||
PHANES_CORE inline std::shared_ptr<spdlog::logger>& PEngineLogger() { return _PEngineLogger; };
|
||||
PHANES_CORE inline std::shared_ptr<spdlog::logger>& PAppLogger() { return _PAppLogger; };
|
||||
}
|
||||
|
||||
|
||||
namespace PLog = Phanes::Core::Logging; // User Macros
|
||||
|
||||
#ifdef P_DEBUG
|
||||
|
||||
// Default logger
|
||||
|
||||
#define PENGINE_LOG_TRACE(...) ::Phanes::Core::Logging::PEngineLogger()->trace(__VA_ARGS__)
|
||||
#define PENGINE_LOG_INFO(...) ::Phanes::Core::Logging::PEngineLogger()->info(__VA_ARGS__)
|
||||
#define PENGINE_LOG_WARN(...) ::Phanes::Core::Logging::PEngineLogger()->warn(__VA_ARGS__)
|
||||
#define PENGINE_LOG_ERROR(...) ::Phanes::Core::Logging::PEngineLogger()->error(__VA_ARGS__)
|
||||
#define PENGINE_LOG_FATAL(...) ::Phanes::Core::Logging::PEngineLogger()->critical(__VA_ARGS__)
|
||||
|
||||
|
||||
#define PAPP_LOG_TRACE(...) ::Phanes::Core::Logging::PAppLogger()->trace(__VA_ARGS__)
|
||||
#define PAPP_LOG_INFO(...) ::Phanes::Core::Logging::PAppLogger()->info(__VA_ARGS__)
|
||||
#define PAPP_LOG_WARN(...) ::Phanes::Core::Logging::PAppLogger()->warn(__VA_ARGS__)
|
||||
#define PAPP_LOG_ERROR(...) ::Phanes::Core::Logging::PAppLogger()->error(__VA_ARGS__)
|
||||
#define PAPP_LOG_FATAL(...) ::Phanes::Core::Logging::PAppLogger()->critical(__VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
#define PENGINE_LOG_TRACE(...)
|
||||
#define PENGINE_LOG_INFO(...)
|
||||
#define PENGINE_LOG_WARN(...)
|
||||
#define PENGINE_LOG_ERROR(...)
|
||||
#define PENGINE_LOG_FATAL(...)
|
||||
|
||||
|
||||
#define PAPP_LOG_TRACE(...)
|
||||
#define PAPP_LOG_INFO(...)
|
||||
#define PAPP_LOG_WARN(...)
|
||||
#define PAPP_LOG_ERROR(...)
|
||||
#define PAPP_LOG_FATAL(...)
|
||||
|
||||
#endif
|
89
Engine/Source/Runtime/Core/public/Math/Boilerplate.h
Normal file
89
Engine/Source/Runtime/Core/public/Math/Boilerplate.h
Normal file
@@ -0,0 +1,89 @@
|
||||
// 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace Phanes::Core::Math
|
||||
{
|
||||
|
||||
// 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>;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Alias for shared_ptr
|
||||
template<typename T>
|
||||
using Ref = std::shared_ptr<T>;
|
||||
|
||||
// Alias for make_shared
|
||||
template<typename T, typename ...Args>
|
||||
constexpr Ref<T> MakeRef(Args&& ...args)
|
||||
{
|
||||
return std::make_shared<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Alias for unique ptr
|
||||
template<typename T>
|
||||
using Scope = std::unique_ptr<T>;
|
||||
|
||||
// Alias for make_unique
|
||||
template<typename T, typename ...Args>
|
||||
constexpr Scope<T> MakeScope(Args&& ...args)
|
||||
{
|
||||
return std::make_unique<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
160
Engine/Source/Runtime/Core/public/Math/IntPoint.hpp
Normal file
160
Engine/Source/Runtime/Core/public/Math/IntPoint.hpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathCommon.hpp"
|
||||
#include "Core/public/Math/MathAbstractTypes.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#include "Core/public/Math/IntVector2.hpp"
|
||||
#include "Core/public/Math/IntVector3.hpp"
|
||||
// #include "Core/public/Math/IntVector4.h"
|
||||
|
||||
#ifndef P_DEBUG
|
||||
#pragma warning(disable : 4244)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* General annonation: The Point is the same as a vector. The type exists, to ensure a
|
||||
* easy differentiation between the two.
|
||||
*/
|
||||
|
||||
#ifndef INTPOINT_H
|
||||
#define INTPOINT_H
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
/**
|
||||
* A 2D Point with components x and y with integer precision.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
struct TIntPoint2 : public TIntVector2<T> {
|
||||
|
||||
using TIntVector2<T>::TIntVector2;
|
||||
|
||||
/**
|
||||
* Creates IntPoint2 from IntPoint3's xy
|
||||
*
|
||||
* @param a IntPoint3 one
|
||||
*/
|
||||
|
||||
TIntPoint2(const TIntPoint3<T>& a)
|
||||
{
|
||||
this->x = a.x;
|
||||
this->y = a.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates IntPoint2 from IntPoint4's xy
|
||||
*
|
||||
* @param a IntPoint4 one
|
||||
*/
|
||||
|
||||
//TIntPoint2(const TIntPoint4<T>& a)
|
||||
//{
|
||||
// this->x = a.x;
|
||||
// this->y = a.y;
|
||||
|
||||
//}
|
||||
};
|
||||
|
||||
template<IntType T, RealType Rt>
|
||||
Rt Distance(const TIntPoint2<T>& p1, const TIntPoint2<T>& p2)
|
||||
{
|
||||
return Magnitude(p2 - p1);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* A 3D Point with components x and y with integer precision.
|
||||
*/
|
||||
|
||||
|
||||
template<IntType T>
|
||||
struct TIntPoint3 : public TIntVector3<T> {
|
||||
|
||||
using TIntVector3<T>::TIntVector3;
|
||||
|
||||
/**
|
||||
* Creates IntPoint3 from IntPoint2's xy and zero
|
||||
*
|
||||
* @param a IntPoint2 one
|
||||
*/
|
||||
|
||||
TIntPoint3(const TIntPoint2<T>& a)
|
||||
{
|
||||
this->x = a.x;
|
||||
this->y = a.y;
|
||||
this->z = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates IntPoint3 from IntPoint4's xyz
|
||||
*
|
||||
* @param a IntPoint4 one
|
||||
*/
|
||||
|
||||
//TIntPoint3(const TIntPoint4<T>& a)
|
||||
//{
|
||||
// this->components[0] = a.components[0];
|
||||
// this->components[1] = a.components[1];
|
||||
// this->components[2] = a.components[2];
|
||||
//}
|
||||
};
|
||||
|
||||
template<IntType T, RealType Rt>
|
||||
Rt Distance(const TIntPoint3<T>& p1, const TIntPoint3<T>& p2)
|
||||
{
|
||||
return Magnitude(p2 - p1);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* A 4D Point with components x and y with integer precision.
|
||||
*/
|
||||
|
||||
|
||||
//template<typename T>
|
||||
//struct TIntPoint4 : public TIntVector4<T> {
|
||||
// static_assert(std::is_integral_v(T), "T must be an integer type.");
|
||||
|
||||
// using IntVector4<T>::IntVector4;
|
||||
|
||||
// /**
|
||||
// * Creates IntPoint4 from IntPoint2's xy and the last two zero
|
||||
// *
|
||||
// * @param a IntPoint2 one
|
||||
// */
|
||||
|
||||
// PHANES_CORE_API IntPoint4(const IntPoint2<T>& a)
|
||||
// {
|
||||
// this->components[0] = a.components[0];
|
||||
// this->components[1] = a.components[1];
|
||||
// this->components[2] = 0;
|
||||
// this->components[3] = 0;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Creates IntPoint4 from IntPoint3's xyz and zero
|
||||
// *
|
||||
// * @param a IntPoint3 one
|
||||
// */
|
||||
|
||||
// PHANES_CORE_API IntPoint4(const IntPoint3<T>& a)
|
||||
// {
|
||||
// this->components[0] = a.components[0];
|
||||
// this->components[1] = a.components[1];
|
||||
// this->components[2] = a.components[2];
|
||||
// this->components[3] = 0;
|
||||
// }
|
||||
//};
|
||||
|
||||
|
||||
} // phanes::core::math::coretypes
|
||||
|
||||
#endif // !INTPOINT_H
|
718
Engine/Source/Runtime/Core/public/Math/IntVector2.hpp
Normal file
718
Engine/Source/Runtime/Core/public/Math/IntVector2.hpp
Normal file
@@ -0,0 +1,718 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathCommon.hpp"
|
||||
#include "Core/public/Math/MathAbstractTypes.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#ifndef P_DEBUG
|
||||
#pragma warning(disable : 4244)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Note: Some function are deleted, because, their unusable with int types, except very specific cases.
|
||||
* To keep the library verbose, these functions are explicitly marked as deleted.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef INTVECTOR2_H
|
||||
#define INTVECTOR2_H
|
||||
|
||||
#define PIntZeroVector2(type) TIntVector2<##type>(0,0)
|
||||
#define PIntVectorSouth2(type) TIntVector2<##type>(0,-1)
|
||||
#define PIntVectorNorth2(type) TIntVector2<##type>(0,1)
|
||||
#define PIntVectorEast2(type) TIntVector2<##type>(1,0)
|
||||
#define PIntVectorWest2(type) TIntVector2<##type>(-1,0)
|
||||
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
/**
|
||||
* A 2D Vector with components x and y with integer precision.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
struct TIntVector2 {
|
||||
|
||||
static_assert(std::is_integral_v<T>, "T must be an integer type.");
|
||||
|
||||
public:
|
||||
|
||||
// Using in combination with a struct and an array allows us the reflect changes of the x and y variables in the comp array and vise versa.
|
||||
|
||||
union
|
||||
{
|
||||
|
||||
struct
|
||||
{
|
||||
/** X component of Vector
|
||||
*
|
||||
* @ref [FIELD]components
|
||||
* @note x does not hold the component, but is a reference two the second item in the components array. The varibale exists wholly for convenience.
|
||||
*/
|
||||
T x;
|
||||
|
||||
/** Y component of Vector
|
||||
*
|
||||
* @ref [FIELD]components
|
||||
*
|
||||
* @note y does not hold the component, but is a reference two the second item in the components array. The varibale exists wholly for convenience.
|
||||
*/
|
||||
T y;
|
||||
};
|
||||
|
||||
/** Components array holding the data
|
||||
*
|
||||
* @ref [FIELD]x
|
||||
* @ref [FIELD]y
|
||||
*
|
||||
* @note Components are split into x and y. Access and manipulation is possible by these variables.
|
||||
*/
|
||||
|
||||
T comp[2];
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor without initialization
|
||||
*/
|
||||
|
||||
TIntVector2() = default;
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
*/
|
||||
|
||||
TIntVector2(const TIntVector2<T>& v)
|
||||
{
|
||||
memcpy(this->comp, comp, sizeof(T) * 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move constructor
|
||||
*/
|
||||
|
||||
TIntVector2(TIntVector2<T>&& v)
|
||||
{
|
||||
this->comp = v.comp;
|
||||
v.comp = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert other type of vector
|
||||
*/
|
||||
|
||||
template<IntType OtherIntType>
|
||||
explicit TIntVector2(const TIntVector2<OtherIntType>& v) : x((T)v.x), y((T)v.y) {};
|
||||
|
||||
/**
|
||||
* Construct Vector from xy components.
|
||||
*
|
||||
* @param x X component
|
||||
* @param y Y component
|
||||
*/
|
||||
|
||||
TIntVector2(const T x, const T y)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Vector from two component array.
|
||||
*
|
||||
* @param comp Array of components
|
||||
*/
|
||||
|
||||
TIntVector2(const T* comp)
|
||||
{
|
||||
memcpy(this->comp, comp, sizeof(T) * 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Vector from 3D integer Vector's xy.
|
||||
*
|
||||
* @param v 3D IntVector to copy from
|
||||
*/
|
||||
|
||||
TIntVector2(const TIntVector3<T>& v)
|
||||
{
|
||||
this->x = v.x;
|
||||
this->y = v.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a vector pointing from start to end.
|
||||
*
|
||||
* @param(start) Startingpoint
|
||||
* @param(end) Endpoint
|
||||
*/
|
||||
|
||||
TIntVector2(const TIntPoint2<T>& start, const TIntPoint2<T>& end)
|
||||
{
|
||||
this->x = end.x - start.x;
|
||||
this->y = end.y - start.y;
|
||||
}
|
||||
};
|
||||
|
||||
// ======================== //
|
||||
// IntVector2 operators //
|
||||
// ======================== //
|
||||
|
||||
|
||||
/**
|
||||
* Addition operation on same TIntVector2<T> (this) by a floating point value.
|
||||
*
|
||||
* @param(v1) Vector to add to
|
||||
* @param(s) Floating point to add
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator+= (TIntVector2<T>& v1, T s)
|
||||
{
|
||||
v1.x += s;
|
||||
v1.y += s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Addition operation on same TIntVector2<T> (this) by a another TIntVector2<T>.
|
||||
*
|
||||
* @param(v1) Vector to add to
|
||||
* @param(v2) Vector to add
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator+= (TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
v1.x += v2.x;
|
||||
v1.y += v2.y;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substraction operation on same TIntVector2<T> (this) by a floating point.
|
||||
*
|
||||
* @param(v1) Vector to substract from
|
||||
* @param(v2) Floating point to substract
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator-= (TIntVector2<T>& v1, T s)
|
||||
{
|
||||
v1.x -= s;
|
||||
v1.y -= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substraction operation on same TIntVector2<T> (this) by a another TIntVector2<T>.
|
||||
*
|
||||
* @param(v1) Vector to substract from
|
||||
* @param(v2) Vector to substract
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator-= (TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
v1.x -= v2.x;
|
||||
v1.y -= v2.y;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplication of TIntVector2<T> (this) with a floating point.
|
||||
*
|
||||
* @param(v1) Vector to multiply with
|
||||
* @param(s Floating point to multiply with
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator*= (TIntVector2<T>& v1, T s)
|
||||
{
|
||||
v1.x *= s;
|
||||
v1.y *= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devision of Vector
|
||||
*
|
||||
* @param(v1) Vector to divide with
|
||||
* @param(s) Scalar to divide with
|
||||
*
|
||||
* @note Result is rounded (obviously)
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator/= (TIntVector2<T>& v1, T s)
|
||||
{
|
||||
float _1_s = 1.0f;
|
||||
|
||||
v1.x *= _1_s;
|
||||
v1.y *= _1_s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the remainder of division by a scalar.
|
||||
*
|
||||
* @param(v1) Vector to divide with
|
||||
* @param(s) Scalar to divide with
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator%= (TIntVector2<T>& v1, T s)
|
||||
{
|
||||
v1.x %= s;
|
||||
v1.y %= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale of Vector by floating point. (> Creates a new TIntVector2<T>)
|
||||
*
|
||||
* @param(v1) Vector to multiply with
|
||||
* @param(s Floating point to multiply with
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator* (const TIntVector2<T>& v1, T s)
|
||||
{
|
||||
return TIntVector2<T>(v1.x * s, v1.y * s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Division of Vector by floating point. (> Creates another TIntVector2<T>)
|
||||
*
|
||||
* @see [FUNC]DivideFloat
|
||||
*
|
||||
* @param(v1) Vector to multiply with
|
||||
* @param(s Floating point to divide with
|
||||
*
|
||||
* @return Result Vector
|
||||
* @note Deleted, because the returntype might not implicitly be obvious, when using in code or reading.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator/ (const TIntVector2<T>& v1, T s)
|
||||
{
|
||||
float _1_s = 1.0f;
|
||||
return TIntVector2<T>(v1.x * _1_s, v1.y * _1_s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale of Vector by floating point. (> Creates a new TIntVector2<T>)
|
||||
*
|
||||
* @param(v1) Vector to multiply with
|
||||
* @param(s Floating point to multiply with
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
FORCEINLINE TIntVector2<T> operator* (T s, const TIntVector2<T>& v1)
|
||||
{
|
||||
return v1 * s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Division of Vector by floating point. (> For convenience not arithmethicaly correct. Works like overloaded counterpart.)
|
||||
*
|
||||
* @see [FUNC]DivideFloat
|
||||
*
|
||||
* @param(v1) Vector to multiply with
|
||||
* @param(s Floating point to divide with
|
||||
*
|
||||
* @return Result Vector
|
||||
*
|
||||
* @note Deleted, because the returntype might not implicitly be obvious, when using in code or reading.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
FORCEINLINE TIntVector2<T> operator/ (T s, const TIntVector2<T>& v1)
|
||||
{
|
||||
return v1 / s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dot product between two Vectors.
|
||||
*
|
||||
* @see [FUNC]DotP
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @result Dot product
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
T operator* (const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return v1.x * v2.x + v1.y * v2.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Componentwise addition of Vector with floating point.
|
||||
*
|
||||
* @param(v1) Vector to add to
|
||||
* @param(s Floating point to add
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator+ (const TIntVector2<T>& v1, T s)
|
||||
{
|
||||
return TIntVector2<T>(v1.x + s, v1.y + s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Componentwise addition of Vector with floating point.
|
||||
*
|
||||
* @param(v1) Vector to add to
|
||||
* @param(s Floating point to add
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator+ (const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return TIntVector2<T>(v1.x + v2.x, v1.y + v2.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Componentwise substraction of Vector with floating point.
|
||||
*
|
||||
* @param(v1) Vector to substract from
|
||||
* @param(s Floating point to substract
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator- (const TIntVector2<T>& v1, T s)
|
||||
{
|
||||
return TIntVector2<T>(v1.x - s, v1.y - s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Componentwise substraction of Vector with Vector.
|
||||
*
|
||||
* @param(v1) Vector to substract from
|
||||
* @param(s Floating point to substract
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> operator- (const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return TIntVector2<T>(v1.x - v2.x, v1.y - v2.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale of Vector by floating point. (> Creates a new TIntVector2<T>)
|
||||
*
|
||||
* @param(v1) Vector to multiply with
|
||||
* @param(s Floating point to multiply with
|
||||
*
|
||||
* @return Result Vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
FORCEINLINE TIntVector2<T> operator% (const TIntVector2<T>& v1, T s)
|
||||
{
|
||||
return TIntVector2<T>(v1.x % s, v1.y % s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negate Vector.
|
||||
*
|
||||
* @param(v1) Vector to negate
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
void operator- (TIntVector2<T>& v1)
|
||||
{
|
||||
v1.x = -v1.x;
|
||||
v1.y = -v1.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare Vector for equality.
|
||||
*
|
||||
* @see [FUNC]Equals
|
||||
*
|
||||
* @param(v1) Vector to negate
|
||||
*
|
||||
* @return true if equal, false if inequal
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
bool operator== (const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return (abs(v1.x - v1.x) < P_FLT_INAC && abs(v1.y - v1.y) < P_FLT_INAC);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compare Vector for inequality.
|
||||
*
|
||||
* @see [FUNC]Equals
|
||||
*
|
||||
* @param(v1) Vector to negate
|
||||
*
|
||||
* @return true if inequal, false if equal
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
bool operator!= (const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return (abs(v1.x - v1.x) > P_FLT_INAC || abs(v1.y - v1.y) > P_FLT_INAC);
|
||||
}
|
||||
|
||||
// ============================================== //
|
||||
// TIntVector2 static function implementation //
|
||||
// ============================================== //
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> MaxV(TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
v1.x = Phanes::Core::Math::Max(v1.x, v2.x);
|
||||
v1.y = Phanes::Core::Math::Max(v1.y, v2.y);
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Vector, with component wise smallest values.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @note Stores new Vector to v1
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> MinV(TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
v1.x = Phanes::Core::Math::Min(v1.x, v2.x);
|
||||
v1.y = Phanes::Core::Math::Min(v1.y, v2.y);
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> SignVectorV(TIntVector2<T>& v1)
|
||||
{
|
||||
v1.x = (v1.x >= 0) ? 1 : -1;
|
||||
v1.y = (v1.y >= 0) ? 1 : -1;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Component wise multiplication of Vector
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @note Stores new Vector to v1
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> ScaleV(TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
v1.x *= v2.x;
|
||||
v1.y *= v2.y;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies one Vector two another
|
||||
*
|
||||
* @param(v1) Vector to copy to
|
||||
* @param(v2) Vector to copy
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> Set(TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
v1 = v2;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets components of a vector.
|
||||
*
|
||||
* @param(v1) Vector to copy to
|
||||
* @param(v2) Vector to copy
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> Set(TIntVector2<T>& v1, T x, T y)
|
||||
{
|
||||
v1.x = x;
|
||||
v1.y = y;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Negates Vector
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> NegateV(TIntVector2<T>& v1)
|
||||
{
|
||||
v1.x = -v1.x;
|
||||
v1.y = -v1.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if 2 vectors are perpendicular to each other.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return true if perpendicular, false if not
|
||||
*
|
||||
* @note Requires v1 and v2 to be normal vectors.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsPerpendicular(const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return (abs(DotP(v1, v2)) = 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if 2 vectors are parallel to each other. (Angle is close to zero.)
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return true if parallel, false if not
|
||||
*
|
||||
* @note Requires v1 and v2 to be normal vectors.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsParallel(const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return (abs(DotP(v1, v2)) = 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if 2 vectors are coincident. (Are parallel and point in the same direction.)
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return true if coincident, false if not
|
||||
*
|
||||
* @note Requires v1 and v2 to be normal vectors.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsCoincident(const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return (DotP(v1, v2) > 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets outer product of to vectors.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Resulting matrix
|
||||
*/
|
||||
|
||||
//template<IntType T>
|
||||
//Matrix2<T> OuterProduct(const TIntVector2<T>& v1, const TIntVector2<T>& v2);
|
||||
|
||||
|
||||
// ================================================================ //
|
||||
// IntVector2 static function implementation with return values //
|
||||
// ================================================================ //
|
||||
|
||||
|
||||
/**
|
||||
* Negates Vector
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
*
|
||||
* @return Componentwise inverted vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> Negate(const TIntVector2<T>& v1)
|
||||
{
|
||||
return TIntVector2<T>(-v1.x, -v1.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Vector by the component wise minimals of both vectors
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Minimal vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> Min(const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return TIntVector2<T>(Phanes::Core::Math::Min(v1.x, v2.x), Phanes::Core::Math::Min(v1.y, v2.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Vector by the component wise maxima of both vectors
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Maximal vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> Max(const TIntVector2<T>& v1, const TIntVector2<T>& v2)
|
||||
{
|
||||
return TIntVector2<T>(Phanes::Core::Math::Max(v1.x, v2.x), Phanes::Core::Math::Max(v1.y, v2.y));
|
||||
}
|
||||
|
||||
|
||||
template<IntType T>
|
||||
TIntVector2<T> SignVector(const TIntVector2<T>& v1)
|
||||
{
|
||||
return TIntVector2<T>((v1.x >= 0) ? 1 : -1, (v1.y >= 0) ? 1 : -1);
|
||||
}
|
||||
|
||||
|
||||
} // phanes::core::math::coretypes
|
||||
|
||||
#endif // !INTVECTOR2_H
|
||||
|
855
Engine/Source/Runtime/Core/public/Math/IntVector3.hpp
Normal file
855
Engine/Source/Runtime/Core/public/Math/IntVector3.hpp
Normal file
@@ -0,0 +1,855 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathCommon.hpp"
|
||||
#include "Core/public/Math/MathAbstractTypes.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#ifndef P_DEBUG
|
||||
#pragma warning(disable : 4244)
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef INTVECTOR3_H
|
||||
#define INTVECTOR3_H
|
||||
|
||||
#define PIntZeroVector3(type) TIntVector3<##type>(0,0,0)
|
||||
#define PIntVectorForward3(type) TIntVector3<##type>(1,0,0)
|
||||
#define PIntVectorBackward3(type) TIntVector3<##type>(-1,0,0)
|
||||
#define PIntVectorEast3(type) TIntVector3<##type>(0,1,0)
|
||||
#define PIntVectorWest3(type) TIntVector3<##type>(0,-1,0)
|
||||
#define PIntVectorUp3(type) TIntVector3<##type>(0,0,1)
|
||||
#define PIntVectorDown3(type) TIntVector3<##type>(0,0,-1)
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
/**
|
||||
* A 3D Vector with components x, y and z with integer precision.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
struct TIntVector3 {
|
||||
|
||||
public:
|
||||
|
||||
// Using in combination with a struct and an array allows us the reflect changes of the x and y variables in the comp array and vise versa.
|
||||
|
||||
union
|
||||
{
|
||||
|
||||
struct
|
||||
{
|
||||
/** X component of Vector
|
||||
*
|
||||
* @ref [FIELD]components
|
||||
* @note x does not hold the component, but is a reference two the second item in the components array. The varibale exists wholly for convenience.
|
||||
*/
|
||||
T x;
|
||||
|
||||
/** Y component of Vector
|
||||
*
|
||||
* @ref [FIELD]components
|
||||
*
|
||||
* @note y does not hold the component, but is a reference two the second item in the components array. The varibale exists wholly for convenience.
|
||||
*/
|
||||
T y;
|
||||
|
||||
/** Z component of Vector
|
||||
*
|
||||
* @ref [FIELD]components
|
||||
*
|
||||
* @note Z does not hold the component, but is a reference two the second item in the components array. The varibale exists wholly for convenience.
|
||||
*/
|
||||
T z;
|
||||
};
|
||||
|
||||
/** Components array holding the data
|
||||
*
|
||||
* @ref [FIELD]x
|
||||
* @ref [FIELD]y
|
||||
* @ref [FIELD]z
|
||||
*
|
||||
* @note Components are split into x, y and z. Access and manipulation is possible by these variables.
|
||||
*/
|
||||
|
||||
T comp[3];
|
||||
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Default constructor without initialization
|
||||
*/
|
||||
|
||||
TIntVector3() = default;
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
*/
|
||||
|
||||
TIntVector3(const TIntVector3<T>& v)
|
||||
{
|
||||
memcpy(this->comp, comp, sizeof(T) * 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move constructor
|
||||
*/
|
||||
|
||||
TIntVector3(TIntVector3<T>&& v)
|
||||
{
|
||||
this->comp = v.comp;
|
||||
v.comp = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert other type of vector
|
||||
*/
|
||||
|
||||
template<IntType OtherIntType>
|
||||
explicit TIntVector3(const TIntVector3<OtherIntType>& v) : x((T)v.x), y((T)v.y) {};
|
||||
|
||||
/**
|
||||
* Construct Vector from xy components.
|
||||
*
|
||||
* @param(x) X component
|
||||
* @param(y) Y component
|
||||
* @param(z) Z component
|
||||
*/
|
||||
|
||||
TIntVector3(const T x, const T y, const T z) : x(x), y(y), z(z) {};
|
||||
|
||||
/**
|
||||
* Construct Vector from two component array.
|
||||
*
|
||||
* @param(comp) Array of components
|
||||
*/
|
||||
|
||||
TIntVector3(const T* comp)
|
||||
{
|
||||
memcpy(this->comp, comp, sizeof(T) * 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a vector pointing from start to end.
|
||||
*
|
||||
* @param(start) Startingpoint
|
||||
* @param(end) Endpoint
|
||||
*/
|
||||
|
||||
TIntVector3(const TIntPoint3<T>& start, const TIntPoint3<T>& end)
|
||||
{
|
||||
this->x = end.x - start.x;
|
||||
this->y = end.y - start.y;
|
||||
this->z = end.z - start.z;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
// ======================== //
|
||||
// IntVector3 operators //
|
||||
// ======================== //
|
||||
|
||||
|
||||
/**
|
||||
* Coponentwise addition of scalar to 3D vector
|
||||
*
|
||||
* @param(v1) vector to add to
|
||||
* @param(s) scalar to add
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator+= (TIntVector3<T>& v1, T s)
|
||||
{
|
||||
v1.x += s;
|
||||
v1.y += s;
|
||||
v1.z += s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise addition of 3D vector to 3D vector
|
||||
*
|
||||
* @param(v1) vector to add to
|
||||
* @param(v2) vector to add
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator+= (TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
v1.x += v2.x;
|
||||
v1.y += v2.y;
|
||||
v1.z += v2.z;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise substraction of scalar of 3D vector
|
||||
*
|
||||
* @param(v1) vector to substract from
|
||||
* @param(s) scalar to substract
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator-= (TIntVector3<T>& v1, T s)
|
||||
{
|
||||
v1.x -= s;
|
||||
v1.y -= s;
|
||||
v1.z -= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise substraction of 3D vector to 3D vector
|
||||
*
|
||||
* @param(v1) vector to substract from
|
||||
* @param(v2) vector to substract with
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator-= (TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
v1.x -= v2.x;
|
||||
v1.y -= v2.y;
|
||||
v1.z -= v2.z;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dot product between two 3D Vectors
|
||||
*
|
||||
* @param(v1) vector one
|
||||
* @param(s) scalar
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator*= (TIntVector3<T>& v1, T s)
|
||||
{
|
||||
v1.x *= s;
|
||||
v1.y *= s;
|
||||
v1.z *= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Division of vector by scalar
|
||||
*
|
||||
* @param(v1) vector one
|
||||
* @param(s) scalar
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator/= (TIntVector3<T>& v1, T s)
|
||||
{
|
||||
float _1_s = 1.0f / s;
|
||||
|
||||
v1.x *= s;
|
||||
v1.y *= s;
|
||||
v1.z *= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores remainder of division with scalar
|
||||
*
|
||||
* @param(v1) vector one
|
||||
* @param(s) scalar
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator%= (TIntVector3<T>& v1, T s)
|
||||
{
|
||||
v1.x %= s;
|
||||
v1.y %= s;
|
||||
v1.z %= s;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise multiplication of 3D Vectors with scalar
|
||||
*
|
||||
* @param(v1) vector one
|
||||
* @param(s) scalar
|
||||
*
|
||||
* @return Resulting vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> operator* (const TIntVector3<T>& v1, T s)
|
||||
{
|
||||
return TIntVector3<T>(v1.x * s, v1.y * s, v1.z * s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise multiplication of 3D Vectors with scalar
|
||||
*
|
||||
* @param(s) scalar
|
||||
* @param(v2) vector
|
||||
*
|
||||
* @return Solution vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
FORCEINLINE TIntVector3<T> operator* (T s, const TIntVector3<T>& v1)
|
||||
{
|
||||
return v1 * s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Division by scalar
|
||||
*
|
||||
* @param(s) scalar
|
||||
* @param(v2) vector
|
||||
*
|
||||
* @return Solution vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator/ (const TIntVector3<T>& v1, T s)
|
||||
{
|
||||
float _1_s = 1.0f / s;
|
||||
return TIntVector3<T>(v1.x * s, v1.y * s, v1.z * s);
|
||||
}
|
||||
|
||||
template<IntType T>
|
||||
FORCEINLINE TIntVector3<T> operator/ (T s, const TIntVector3<T>& v1)
|
||||
{
|
||||
return v1 / s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores remainder of division by scalar
|
||||
*
|
||||
* @param(s) scalar
|
||||
* @param(v2) vector
|
||||
*
|
||||
* @return Solution vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline TIntVector3<T> operator% (T s, const TIntVector3<T>& v1)
|
||||
{
|
||||
return TIntVector3<T>(v1.x % s, v1.y % s, v1.z % s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dot product between two 3D Vectors
|
||||
*
|
||||
* @param(v1) vector one
|
||||
* @param(v2) vector two
|
||||
*
|
||||
* @return Dot product of Vectors
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
T operator* (const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise addition of scalar to 3D vector
|
||||
*
|
||||
* @param(v1) vector to add to
|
||||
* @param(s) scalar to add
|
||||
*
|
||||
* @return Resulting vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> operator+ (const TIntVector3<T>& v1, T s)
|
||||
{
|
||||
return TIntVector3<T>(v1.x + s.v1.y + s, v1.z + s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise addition of 3D vector to 3D vector
|
||||
*
|
||||
* @param(v1) vector to add to
|
||||
* @param(v2) vector to add
|
||||
*
|
||||
* @return Resulting vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> operator+ (const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return TIntVector3<T>(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise substraction of scalar of 3D vector
|
||||
*
|
||||
* @param(v1) vector to substract from
|
||||
* @param(s) scalar to substract
|
||||
*
|
||||
* @return Resulting vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> operator- (const TIntVector3<T>& v1, T s)
|
||||
{
|
||||
return TIntVector3<T>(v1.x - s.v1.y - s, v1.z - s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coponentwise substraction of scalar of 3D vector
|
||||
*
|
||||
* @param(v1) vector to substract from
|
||||
* @param(v2) vector to substract with
|
||||
*
|
||||
* @return Resulting vector
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> operator- (const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return TIntVector3<T>(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates vector
|
||||
*
|
||||
* @param(v1) Vector to negate
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> operator- (TIntVector3<T>& v1)
|
||||
{
|
||||
v1.x = -v1.x;
|
||||
v1.y = -v1.y;
|
||||
v1.z = -v1.z;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests two 3D vectors for equality.
|
||||
*
|
||||
* @ref [FUNC]Equals
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return True if equal, false if not.
|
||||
*
|
||||
* @note Uses [MACRO]P_FLT_INAC
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool operator== (const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return (abs(v1.x - v2.x) < P_FLT_INAC && abs(v1.y - v2.y) < P_FLT_INAC && abs(v1.z - v2.z) < P_FLT_INAC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests two 3D vectors for inequality.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return True if inequal, false if not.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool operator!= (const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return (abs(v1.x - v2.x) > P_FLT_INAC || abs(v1.y - v2.y) > P_FLT_INAC || abs(v1.z - v2.z) > P_FLT_INAC);
|
||||
}
|
||||
|
||||
|
||||
// ============================================== //
|
||||
// IntVector3 static function implementation //
|
||||
// ============================================== //
|
||||
|
||||
/**
|
||||
* Dot product of two vectors
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Dot product of vectors
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
T DotP(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests two vectors for equality.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
* @param(threshold) Allowed T inaccuracy.
|
||||
*
|
||||
* @return True if equal, false if not.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool Equals(const TIntVector3<T>& v1, const TIntVector3<T>& v2, T threshold = P_FLT_INAC)
|
||||
{
|
||||
return (abs(v1.x - v2.x) < threshold && abs(v1.y - v2.y) < threshold && abs(v1.z - v2.z) < threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the cross product between two vectors.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> CrossPV(TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
float x = v1.x;
|
||||
float y = v1.y;
|
||||
float z = v1.z;
|
||||
|
||||
v1.x = (y * v2.z) - (z * v2.y);
|
||||
v1.y = (z * v2.x) - (x * v2.z);
|
||||
v1.z = (x * v2.y) - (y * v2.x);
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the componentwise max of both vectors.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> MaxV(TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
v1.x = Phanes::Core::Math::Max(v1.x, v2.x);
|
||||
v1.y = Phanes::Core::Math::Max(v1.y, v2.y);
|
||||
v1.z = Phanes::Core::Math::Max(v1.z, v2.z);
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the componentwise min of both vectors.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> MinV(TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
v1.x = Phanes::Core::Math::Min(v1.x, v2.x);
|
||||
v1.y = Phanes::Core::Math::Min(v1.y, v2.y);
|
||||
v1.z = Phanes::Core::Math::Min(v1.z, v2.z);
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets reversed vector.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> NegateV(TIntVector3<T>& v1)
|
||||
{
|
||||
v1.x = -v1.x;
|
||||
v1.y = -v1.y;
|
||||
v1.z = -v1.z;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performes componentwise multiplication of two vectors.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> ScaleV(TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
v1.x *= v2.x;
|
||||
v1.y *= v2.y;
|
||||
v1.z *= v2.z;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies v1 vector
|
||||
*
|
||||
* @param(v1) Vector to copy to
|
||||
* @param(v2) Vector to copy
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> Set(TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
v1 = v2;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets vector.
|
||||
*
|
||||
* @param(v1) Vector to copy to
|
||||
* @param(x) X component
|
||||
* @param(y) Y component
|
||||
* @param(z) Z component
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> Set(TIntVector3<T>& v1, T x, T y, T z)
|
||||
{
|
||||
v1.x = x;
|
||||
v1.y = y;
|
||||
v1.z = z;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns signs of components in vector: -1 / +1 / 0.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> SignVectorV(TIntVector3<T>& v1)
|
||||
{
|
||||
v1.x = (v1.x >= 0) ? 1 : -1;
|
||||
v1.y = (v1.y >= 0) ? 1 : -1;
|
||||
v1.z = (v1.z >= 0) ? 1 : -1;
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets scalar triple product ((v1 x v2) * v3). (Volume of parallelepiped.)
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
* @param(v3) Vector three
|
||||
*
|
||||
* @return Vector triple product
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
T ScalarTriple(const TIntVector3<T>& v1, const TIntVector3<T>& v2, const TIntVector3<T>& v3)
|
||||
{
|
||||
return CrossP(v1, v2) * v3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets vector triple product ((v1 x v2) x v3).
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
* @param(v3) Vector three
|
||||
*
|
||||
* @note result is stored in v1
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> VectorTripleV(TIntVector3<T>& v1, const TIntVector3<T>& v2, const TIntVector3<T>& v3)
|
||||
{
|
||||
CrossPV(CrossPV(v1, v2), v3);
|
||||
|
||||
return v1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two vectors are perpendicular.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return True if perpendicular, false if not.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsPerpendicular(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return (DotP(v1, v2) == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two vectors are parallel.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return True if parallel, false if not.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsParallel(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return (abs(DotP(v1, v2)) == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two vectors are coincident (Parallel and point in same direction).
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return True if coincident, false if not.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsCoincident(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return (DotP(v1, v2) == 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if three vectors are coplanar
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
* @param(v3) Vector three
|
||||
*
|
||||
* @return True if coplanar, false if not.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
inline bool IsCoplanar(const TIntVector3<T>& v1, const TIntVector3<T>& v2, const TIntVector3<T>& v3)
|
||||
{
|
||||
return (ScalarTriple(v1, v2, v3) == 0);
|
||||
}
|
||||
|
||||
|
||||
// ================================================================ //
|
||||
// IntVector3 static function implementation with return values //
|
||||
// ================================================================ //
|
||||
|
||||
/**
|
||||
* Gets cross product between two vectors.
|
||||
*
|
||||
* @param(v1) vector one
|
||||
* @param(v2) vector two
|
||||
*
|
||||
* @return Cross product of v1 and v2
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> CrossP(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return TIntVector3<T>((v1.y * v2.z) - (v1.z * v2.y),
|
||||
(v1.z * v2.x) - (v1.x * v2.z),
|
||||
(v1.x * v2.y) - (v1.y * v2.x));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Vector by the componentwise max of both vectors
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Vector of componentwise max
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> Max(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return TIntVector3<T>((v1.x > v2.x) ? v1.x : v2.x,
|
||||
(v1.y > v2.y) ? v1.y : v2.y,
|
||||
(v1.z > v2.z) ? v1.z : v2.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Vector by the componentwise min of both vectors
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Vector of componentwise min
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> Min(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return TIntVector3<T>((v1.x < v2.x) ? v1.x : v2.x,
|
||||
(v1.y < v2.y) ? v1.y : v2.y,
|
||||
(v1.z < v2.z) ? v1.z : v2.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets reversed vector.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> Negate(const TIntVector3<T>& v1)
|
||||
{
|
||||
return TIntVector3<T>(-v1.x, -v1.y, -v1.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies vector componentwise.
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
*
|
||||
* @return Vector with componentwise products
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> Scale(const TIntVector3<T>& v1, const TIntVector3<T>& v2)
|
||||
{
|
||||
return TIntVector3<T>(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets vector triple product ((v1 x v2) x v3).
|
||||
*
|
||||
* @param(v1) Vector one
|
||||
* @param(v2) Vector two
|
||||
* @param(v3) Vector three
|
||||
*
|
||||
* @return Vector triple product
|
||||
*/
|
||||
|
||||
template<IntType T>
|
||||
TIntVector3<T> VectorTriple(const TIntVector3<T>& v1, const TIntVector3<T>& v2, const TIntVector3<T>& v3)
|
||||
{
|
||||
return CrossP(CrossP(v1, v2), v3);
|
||||
}
|
||||
|
||||
} // phanes::core::math::coretypes
|
||||
|
||||
#endif // !INTVECTOR3_H
|
||||
|
72
Engine/Source/Runtime/Core/public/Math/Line.hpp
Normal file
72
Engine/Source/Runtime/Core/public/Math/Line.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#include "Core/public/Math/Vector3.hpp"
|
||||
|
||||
#include <any>
|
||||
|
||||
|
||||
namespace Phanes::Core::Math
|
||||
{
|
||||
|
||||
// Line with direction and moment
|
||||
|
||||
template<RealType T>
|
||||
struct TLine
|
||||
{
|
||||
public:
|
||||
using Real = T;
|
||||
|
||||
/** Direction of line */
|
||||
|
||||
TVector3<Real> direction;
|
||||
|
||||
/** Base point of line */
|
||||
|
||||
TVector3<Real> base;
|
||||
|
||||
public:
|
||||
|
||||
/** Construct line from base and direction
|
||||
*
|
||||
* @param(direction) Direction of line
|
||||
* @param(p) Base of line
|
||||
*/
|
||||
|
||||
TLine(const TVector3<T>& direction, const TVector3<T>& p) : direction(direction), base(p) {};
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalizes the direction of the line
|
||||
*
|
||||
* @param(l1) Line
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TLine<T> NormalizeV(TLine<T>& l1)
|
||||
{
|
||||
std::any
|
||||
|
||||
NormalizeV(l1.direction);
|
||||
return l1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Normalizes the direction of the line
|
||||
*
|
||||
* @param(l1) Line
|
||||
*
|
||||
* @return Line with normalized direction.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TLine<T> NormalizeV(const TLine<T>& l1)
|
||||
{
|
||||
return TLine<T>(Normalize(l1.direction), l1.base);
|
||||
}
|
||||
}
|
29
Engine/Source/Runtime/Core/public/Math/MathAbstractTypes.h
Normal file
29
Engine/Source/Runtime/Core/public/Math/MathAbstractTypes.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef ABSTRACT_TYPES_H
|
||||
#define ABSTRACT_TYPES_H
|
||||
|
||||
namespace Phanes::Core::Math::Internal {
|
||||
|
||||
template <typename T, unsigned int D = 3>
|
||||
struct AVector {
|
||||
public:
|
||||
|
||||
/**
|
||||
* List of n components of the vector
|
||||
*/
|
||||
|
||||
T comp[D];
|
||||
|
||||
};
|
||||
|
||||
template <typename T, unsigned int n = 3, unsigned int m = 3>
|
||||
struct AMatrix {
|
||||
public:
|
||||
T fields[n][m];
|
||||
|
||||
};
|
||||
|
||||
}; // Phanes::Core::Math::abstract::coretypes
|
||||
|
||||
#endif // !ABSTRACT_TYPES_H
|
129
Engine/Source/Runtime/Core/public/Math/MathCommon.hpp
Normal file
129
Engine/Source/Runtime/Core/public/Math/MathCommon.hpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#pragma once
|
||||
|
||||
#define P_FLT_INAC_LARGE 0.0001f // large float inaccuracy (1*10^-4);
|
||||
#define P_FLT_INAC 0.00001f // float inaccuracy (1*10^-5);
|
||||
#define P_FLT_INAC_SMALL 0.000001f // small float inaccuracy (1*10^-6);
|
||||
|
||||
#define P_180_PI 57.29577951308232 // (double) 180<38>/pi;
|
||||
#define P_180_PI_FLT 57.29577951308232f // (float) 180<38>/pi;
|
||||
|
||||
#define P_PI_180 0.0174532925199432 // double pi/180<38>
|
||||
#define P_PI_180_FLT 0.0174532925199432f // (float) pi/180<38>
|
||||
|
||||
#define P_PI 3.1415926535897932 // PI
|
||||
#define P_PI_FLT 3.1415926535897932f // PI
|
||||
|
||||
|
||||
#ifndef MATH_COMMON_H
|
||||
#define MATH_COMMON_H
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
/**
|
||||
* Clamps a value between minimum and maximum
|
||||
*
|
||||
* @param value Value to clamp
|
||||
* @param low Minimum
|
||||
* @param high Maximum
|
||||
*
|
||||
* @return Minimum, if value is to small / Maximum, if value is to large / value, if value is in range.
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
T Clamp(T value, T low, T high)
|
||||
{
|
||||
if (value < low) value = low;
|
||||
if (value > high) value = high;
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the larger of two values
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
*
|
||||
* @return Larger value
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
inline T Max(T x, T y)
|
||||
{
|
||||
return (x > y) ? x : y;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the smaller of two values
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
*
|
||||
* @return Smaller value
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
inline T Min(T x, T y)
|
||||
{
|
||||
return (x < y) ? x : y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps the values of two variables
|
||||
*
|
||||
* @param x
|
||||
* @param y
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
inline void Swap(T& x, T& y)
|
||||
{
|
||||
float z = x;
|
||||
x = y;
|
||||
y = z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test two numbers for equality
|
||||
*
|
||||
* @param(x)
|
||||
* @param(y)
|
||||
* @param(threshold) Allowed inaccuracy
|
||||
*
|
||||
* @return True, if equal, false if not
|
||||
*/
|
||||
template<typename T>
|
||||
bool Equals(T x, T y, T threshold = P_FLT_INAC)
|
||||
{
|
||||
return (abs(x - y) < threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the reciprocal of the square root of n using the algorithm of A Quake III
|
||||
*
|
||||
* @param n Number to calculate
|
||||
*
|
||||
* @return Inverse square root of n
|
||||
*
|
||||
* @note a simple 1.0f / sqrtf(x) is faster than this algorithm. Use for research purpose only.
|
||||
*/
|
||||
|
||||
|
||||
template<typename T>
|
||||
float FastInvSqrt(T n)
|
||||
{
|
||||
int i = *(int*)&n;
|
||||
float x2 = n * 0.5f;
|
||||
|
||||
i = 0x5f3759df - (i >> 1);
|
||||
n = *(float*)&i;
|
||||
n = n * (1.5f - (x2 * n * n));
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
} // phanes
|
||||
|
||||
#endif // !MATH_COMMON_H
|
122
Engine/Source/Runtime/Core/public/Math/MathFwd.h
Normal file
122
Engine/Source/Runtime/Core/public/Math/MathFwd.h
Normal file
@@ -0,0 +1,122 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef P_BUILD_LIB
|
||||
#include "PhanesEnginePCH.h"
|
||||
#else
|
||||
#include <vector>
|
||||
#endif
|
||||
|
||||
#ifndef MATH_FWD_H
|
||||
#define MATH_FWD_H
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
/**
|
||||
* Includes forward declarations, as well as certain useful typedefs.
|
||||
*
|
||||
* @ref OSAL/PlatformTypes.h
|
||||
*/
|
||||
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
/**
|
||||
* Template forward declarations.
|
||||
*/
|
||||
|
||||
template<RealType T> struct TColor;
|
||||
template<RealType T> struct TLinearColor;
|
||||
template<RealType T> struct TVector2;
|
||||
template<RealType T> struct TVector3;
|
||||
template<RealType T> struct TVector4;
|
||||
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;
|
||||
template<RealType T> struct TTransform;
|
||||
template<RealType T> struct TPoint2;
|
||||
template<RealType T> struct TPoint3;
|
||||
template<RealType T> struct TPoint4;
|
||||
template<IntType T> struct TIntVector2;
|
||||
template<IntType T> struct TIntVector3;
|
||||
template<IntType T> struct TIntVector4;
|
||||
template<IntType T> struct TIntPoint2;
|
||||
template<IntType T> struct TIntPoint3;
|
||||
template<IntType T> struct TIntPoint4;
|
||||
|
||||
/**
|
||||
* Specific instantiation of forward declarations.
|
||||
*/
|
||||
|
||||
// TVector2
|
||||
typedef TVector2<float> Vector2;
|
||||
typedef TVector2<double> Vector2d;
|
||||
|
||||
typedef std::vector<Vector2> Vector2List;
|
||||
typedef std::vector<Vector2d> Vector2Listd;
|
||||
|
||||
// TVector3
|
||||
typedef TVector3<float> Vector3;
|
||||
typedef TVector3<double> Vector3d;
|
||||
|
||||
typedef std::vector<Vector3> Vector3List;
|
||||
typedef std::vector<Vector3d> Vector3Listd;
|
||||
|
||||
|
||||
|
||||
// TIntVector2
|
||||
typedef TIntVector2<int> IntVector2;
|
||||
typedef TIntVector2<long> IntVector2l;
|
||||
|
||||
typedef std::vector<IntVector2> IntVector2List;
|
||||
typedef std::vector<IntVector2l> IntVector2Listl;
|
||||
|
||||
// TIntVector3
|
||||
typedef TIntVector3<int> IntVector3;
|
||||
typedef TIntVector3<long> IntVector3l;
|
||||
|
||||
typedef std::vector<IntVector3> IntVector3List;
|
||||
typedef std::vector<IntVector3l> IntVector3Listl;
|
||||
|
||||
|
||||
|
||||
// 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
|
||||
|
||||
namespace Phanes::Core::Math::Internal
|
||||
{
|
||||
|
||||
// Internal types
|
||||
|
||||
template <typename T, unsigned int D> struct AVector;
|
||||
|
||||
template <typename T, unsigned int n, unsigned int> struct AMatrix;
|
||||
}
|
||||
|
||||
|
||||
#endif // !MATH_FWD_H
|
20
Engine/Source/Runtime/Core/public/Math/MathPCH.h
Normal file
20
Engine/Source/Runtime/Core/public/Math/MathPCH.h
Normal 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"
|
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
// ============================================= //
|
||||
// Function to convert types into each other //
|
||||
// //
|
||||
// @ref [FILE]MathUnitConversion //
|
||||
// ============================================= //
|
||||
|
||||
#ifdef P_BUILD_LIB
|
||||
#include "PhanesEnginePCH.h"
|
||||
#else
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathAbstractTypes.h"
|
||||
#include "Core/public/Math/Vector2.hpp"
|
||||
#include "Core/public/Math/Vector3.hpp"
|
||||
//#include "Core/public/Math/Vector4.h"
|
||||
#include "Core/public/Math/Matrix2.hpp"
|
||||
//#include "Core/public/Math/Matrix3.h"
|
||||
#include "Core/public/Math/IntVector2.hpp"
|
||||
#include "Core/public/Math/IntVector3.hpp"
|
||||
|
||||
#ifndef MATH_TYPE_CONVERSION_H
|
||||
#define MATH_TYPE_CONVERSION_H
|
||||
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
// =================================================== //
|
||||
// std::to_string wrapper //
|
||||
// //
|
||||
// This is, to make using ToString more general //
|
||||
// and allow usage of one function instead of two, //
|
||||
// for converting a mathmatical type to a string. //
|
||||
// =================================================== //
|
||||
|
||||
FORCEINLINE std::string ToString(long long val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(double val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(float val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(int val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(long val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(long double val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(unsigned long long val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(unsigned int val) { return std::to_string(val); };
|
||||
|
||||
FORCEINLINE std::string ToString(unsigned long val) { return std::to_string(val); };
|
||||
|
||||
|
||||
// ============ //
|
||||
// ToString //
|
||||
// ============ //
|
||||
|
||||
template<RealType T>
|
||||
std::string ToString(const TVector2<T>& v)
|
||||
{
|
||||
return "(" + ToString(v.x) + ", " + ToString(v.y) + ")";
|
||||
}
|
||||
|
||||
template<IntType T>
|
||||
std::string ToString(const TIntVector2<T>& v)
|
||||
{
|
||||
return "(" + ToString(v.x) + ", " + ToString(v.y) + ")";
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
std::string ToString(const TVector3<T>& v)
|
||||
{
|
||||
return "(" + ToString(v.x) + ", " + ToString(v.y) + ", " + ToString(v.z) + ")";
|
||||
}
|
||||
|
||||
template<IntType T>
|
||||
std::string ToString(const TIntVector3<T>& v)
|
||||
{
|
||||
return "(" + ToString(v.x) + ", " + ToString(v.y) + ", " + ToString(v.z) + ")";
|
||||
}
|
||||
|
||||
//std::string toString(const Vector4& v);
|
||||
|
||||
template<RealType T>
|
||||
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)) + "]]";
|
||||
}
|
||||
|
||||
//std::string toString(const Matrix3& v);
|
||||
|
||||
}
|
||||
|
||||
#endif // !MATH_TYPE_CONVERSION_H
|
139
Engine/Source/Runtime/Core/public/Math/MathUnitConversion.hpp
Normal file
139
Engine/Source/Runtime/Core/public/Math/MathUnitConversion.hpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
|
||||
// ======================================= //
|
||||
// Contains functions to convert units //
|
||||
// ======================================= //
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathCommon.hpp"
|
||||
|
||||
namespace Phanes::Core::Math::UnitConversion
|
||||
{
|
||||
|
||||
/**
|
||||
* Converts degrees to radians.
|
||||
*
|
||||
* @param(deg) Angle in degress (<28>)
|
||||
*
|
||||
* @return Angle in radians
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline T DegToRad(T deg)
|
||||
{
|
||||
return deg * P_PI_180_FLT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts radians to degrees.
|
||||
*
|
||||
* @param(rad) Angle in radians (rad)
|
||||
*
|
||||
* @return Angle in degrees
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline T RadToDeg(T rad)
|
||||
{
|
||||
return rad * P_180_PI_FLT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts degrees to gradian.
|
||||
*
|
||||
* @param(deg) Angle in degress (<28>)
|
||||
*
|
||||
* @return Angle in gradian
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline T DegToGradian(T deg)
|
||||
{
|
||||
return deg * 1.111111f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts gradian to degrees.
|
||||
*
|
||||
* @param(rad) Angle in gradians (g)
|
||||
*
|
||||
* @return Angle in degrees
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline T GradianToDeg(T g)
|
||||
{
|
||||
return g * 0.9f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts radians to gradians.
|
||||
*
|
||||
* @param(deg) Angle in radians (rad)
|
||||
*
|
||||
* @return Angle in gradians
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline T RadToGradian(T rad)
|
||||
{
|
||||
return rad * 200 / P_PI_FLT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts gradian to radians.
|
||||
*
|
||||
* @param(rad) Angle in gradians (g)
|
||||
*
|
||||
* @return Angle in radians
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline T GradianToRad(T g)
|
||||
{
|
||||
return g * P_PI_FLT / 200;
|
||||
}
|
||||
|
||||
} // phanes::core::math::typeconversion
|
||||
|
||||
|
||||
namespace Phanes::Core::Math::UnitLiterals
|
||||
{
|
||||
// ============================================== //
|
||||
// unit conversion with user-defined literals //
|
||||
// ============================================== //
|
||||
|
||||
/**
|
||||
* Convert deg to rad.
|
||||
*
|
||||
* @param(_x) Angle in degress
|
||||
*/
|
||||
|
||||
double operator ""_deg(long double _x)
|
||||
{
|
||||
return _x * P_PI_180_FLT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert rad to rad.
|
||||
*
|
||||
* @param(_x) Angle in degress
|
||||
*/
|
||||
|
||||
double operator ""_rad(long double _x)
|
||||
{
|
||||
return _x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert gradian to rad.
|
||||
*
|
||||
* @param(_x) Angle in degress
|
||||
*/
|
||||
|
||||
double operator ""_g(long double _x)
|
||||
{
|
||||
return _x * P_PI_FLT / 200;
|
||||
}
|
||||
}
|
298
Engine/Source/Runtime/Core/public/Math/Matrix2.hpp
Normal file
298
Engine/Source/Runtime/Core/public/Math/Matrix2.hpp
Normal file
@@ -0,0 +1,298 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/Vector2.hpp"
|
||||
|
||||
#ifndef MATRIX2_H
|
||||
#define MATRIX2_H
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
// 2x2 Matrix defined in column-major order.
|
||||
// Accessed by M[Row][Col].
|
||||
|
||||
template<RealType T>
|
||||
struct TMatrix2
|
||||
{
|
||||
public:
|
||||
|
||||
T m[2][2];
|
||||
|
||||
public:
|
||||
|
||||
TMatrix2() = default;
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Matrix from 2d array.
|
||||
*
|
||||
* @param(fields) 2D Array with column major order.
|
||||
*/
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Matrix from parameters.
|
||||
*
|
||||
* @param(n00) M[0][0]
|
||||
* @param(n10) M[1][0]
|
||||
* @param(n01) M[0][1]
|
||||
* @param(n11) M[1][1]
|
||||
*
|
||||
* @note nXY = n[Row][Col]
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Matrix from two 2d vector columns.
|
||||
*
|
||||
* @param(v1) Column zero
|
||||
* @param(v2) Column one
|
||||
*/
|
||||
|
||||
TMatrix2(const TVector2<T>& v1, const TVector2<T>& 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;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
FORCEINLINE T& operator() (int n, int m) const
|
||||
{
|
||||
return this->m[m][n];
|
||||
}
|
||||
FORCEINLINE TVector2<T>& operator[] (int m) const
|
||||
{
|
||||
return reinterpret_cast<TVector2*>(this->m[m]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// ===================== //
|
||||
// TMatrix2 operator //
|
||||
// ===================== //
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> operator+= (TMatrix2<T>& m1, T s)
|
||||
{
|
||||
m1->m(0, 0) += s;
|
||||
m1->m(0, 1) += s;
|
||||
m1->m(1, 0) += s;
|
||||
m1->m(1, 1) += s;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> operator+= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
|
||||
{
|
||||
m1->m(0, 0) += m2.m(0, 0);
|
||||
m1->m(0, 1) += m2.m(0, 1);
|
||||
m1->m(1, 0) += m2.m(1, 0);
|
||||
m1->m(1, 1) += m2.m(1, 1);
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> operator-= (TMatrix2<T>& m1, T s)
|
||||
{
|
||||
m1->m(0, 0) -= s;
|
||||
m1->m(0, 1) -= s;
|
||||
m1->m(1, 0) -= s;
|
||||
m1->m(1, 1) -= s;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> operator-= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
|
||||
{
|
||||
m1->m(0, 0) -= m2.m(0, 0);
|
||||
m1->m(0, 1) -= m2.m(0, 1);
|
||||
m1->m(1, 0) -= m2.m(1, 0);
|
||||
m1->m(1, 1) -= m2.m(1, 1);
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> operator*= (TMatrix2<T>& m1, T s)
|
||||
{
|
||||
m1->m[0][0] *= s;
|
||||
m1->m[0][1] *= s;
|
||||
m1->m[1][0] *= s;
|
||||
m1->m[1][1] *= s;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> operator*= (TMatrix2<T>& m1, const TMatrix2<T>& m2)
|
||||
{
|
||||
Matrix2 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);
|
||||
|
||||
m1(1, 0) = c(1, 0) * m2(0, 0) + c(1, 1) * m2(1, 0);
|
||||
m1(1, 1) = c(1, 0) * m2(0, 1) + c(1, 1) * m2(1, 1);
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TVector2<T> operator* (const TMatrix2<T>& m1, const TVector2<T>& v)
|
||||
{
|
||||
return TVector2<T>(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)
|
||||
{
|
||||
return (abs(m1(0, 0) - m2(0, 0)) < P_FLT_INAC && abs(m1(0, 1) - m2(0, 1)) < P_FLT_INAC &&
|
||||
abs(m1(1, 0) - m2(1, 0)) < P_FLT_INAC && abs(m1(1, 1) - m2(1, 1)) < P_FLT_INAC);
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
bool operator!= (const TMatrix2<T>& m1, const TMatrix2<T>& m2)
|
||||
{
|
||||
return (abs(m1(0, 0) - m2(0, 0)) > P_FLT_INAC || abs(m1(0, 1) - m2(0, 1)) > P_FLT_INAC ||
|
||||
abs(m1(1, 0) - m2(1, 0)) > P_FLT_INAC || abs(m1(1, 1) - m2(1, 1)) > P_FLT_INAC);
|
||||
}
|
||||
|
||||
|
||||
// =============================== //
|
||||
// Matrix function definition //
|
||||
// =============================== //
|
||||
|
||||
template<RealType T>
|
||||
T Determinant(const Matrix2& m1)
|
||||
{
|
||||
return m1(0, 0) * m1(1, 1) - m1(0, 1) * m1(0, 1);
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> InverseV(TMatrix2<T>& m1)
|
||||
{
|
||||
float _1_det = 1.0f / Determinant(m1);
|
||||
float m00 = m1(0, 0);
|
||||
|
||||
m1(0, 0) = m1(1, 1);
|
||||
m1(0, 1) = -m1(0, 1);
|
||||
m1(1, 0) = -m1(1, 0);
|
||||
m1(1, 1) = m00;
|
||||
|
||||
m1 *= _1_det;
|
||||
return m1;
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
TMatrix2<T> TransposeV(TMatrix2<T>& m1)
|
||||
{
|
||||
Swap(m1(0, 1), m1(1, 0));
|
||||
}
|
||||
|
||||
// =============== //
|
||||
// WITH RETURN //
|
||||
// =============== //
|
||||
|
||||
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(1, 0) * _1_det,
|
||||
m1(0, 1) * _1_det, m1(0, 0) * _1_det);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
template<RealType T>
|
||||
bool IsIndentityMatrix(const TMatrix2<T>& m1, T threshold = P_FLT_INAC)
|
||||
{
|
||||
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1) - (T)1.0) < P_FLT_INAC &&
|
||||
abs(m1(1, 0) - (T)1.0) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC);
|
||||
}
|
||||
|
||||
} // Phanes::Core::Math
|
||||
|
||||
|
||||
#endif // !MATRIX2_H
|
497
Engine/Source/Runtime/Core/public/Math/Matrix3.hpp
Normal file
497
Engine/Source/Runtime/Core/public/Math/Matrix3.hpp
Normal file
@@ -0,0 +1,497 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathAbstractTypes.h"
|
||||
#include "Core/public/Math/Vector3.hpp"
|
||||
|
||||
#ifndef MATRIX3_H
|
||||
#define MATRIX3_H
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
|
||||
// 3x3 Matrix defined in column-major order.
|
||||
// Accessed by M[Row][Col].
|
||||
|
||||
template<RealType T>
|
||||
struct TMatrix3
|
||||
{
|
||||
public:
|
||||
|
||||
T m[3][3];
|
||||
|
||||
|
||||
public:
|
||||
|
||||
TMatrix3() = default;
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
*/
|
||||
|
||||
TMatrix3(const TMatrix3<T>& m1)
|
||||
{
|
||||
memcpy(this->m, m1.m, sizeof(T) * 9);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Matrix from 2d array.
|
||||
*
|
||||
* @param(fields) 2D Array with column major order.
|
||||
*/
|
||||
|
||||
TMatrix3(T fields[2][2])
|
||||
{
|
||||
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->m[2][1] = fields[2][1];
|
||||
this->m[0][2] = fields[0][2]; this->m[1][2] = fields[1][2]; this->m[2][2] = fields[2][2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Matrix from parameters.
|
||||
*
|
||||
* @param(n00) M[0][0]
|
||||
* @param(n10) M[1][0]
|
||||
* @param(n01) M[0][1]
|
||||
* @param(n11) M[1][1]
|
||||
*
|
||||
* @note nXY = n[Row][Col]
|
||||
*/
|
||||
|
||||
TMatrix3(T n00, T n01, T n02,
|
||||
T n10, T n11, T n12,
|
||||
T n20, T n21, T n22)
|
||||
{
|
||||
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->m[2][1] = n12;
|
||||
this->m[1][2] = n20; this->m[1][2] = n21; this->m[2][2] = n22;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct Matrix from two 2d vector columns.
|
||||
*
|
||||
* @param(v1) Column zero
|
||||
* @param(v2) Column one
|
||||
*/
|
||||
|
||||
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->m[2][0] = v3.x;
|
||||
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->m[2][2] = v3.z;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
FORCEINLINE T& operator() (int n, int m)
|
||||
{
|
||||
return this->m[m][n];
|
||||
}
|
||||
|
||||
FORCEINLINE TVector3<T>& operator[] (int m)
|
||||
{
|
||||
return (*reinterpret_cast<TVector3<T>*>(this->m[m]));
|
||||
}
|
||||
|
||||
FORCEINLINE const T& operator() (int n, int m) const
|
||||
{
|
||||
return this->m[m][n];
|
||||
}
|
||||
|
||||
FORCEINLINE const TVector3<T>& operator[] (int m) const
|
||||
{
|
||||
return (*reinterpret_cast<TVector3<T>*>(this->m[m]));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
// ==================== //
|
||||
// Matrix3 operator //
|
||||
// ==================== //
|
||||
|
||||
/**
|
||||
* Adds scalar to matrix componentwise
|
||||
*
|
||||
* @param(m) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator+= (TMatrix3<T>& m1, T s)
|
||||
{
|
||||
m1(0, 0) += s; m1(0, 1) += s; m1(0, 2) += s;
|
||||
m1(1, 0) += s; m1(1, 1) += s; m1(1, 2) += s;
|
||||
m1(2, 0) += s; m1(2, 1) += s; m1(2, 2) += s;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds matrix to matrix componentwise
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator+= (TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
m1(0, 0) += m2(0, 0); m1(0, 1) += m2(0, 1); m1(0, 2) += m2(0, 2);
|
||||
m1(1, 0) += m2(1, 0); m1(1, 1) += m2(1, 1); m1(1, 2) += m2(1, 2);
|
||||
m1(2, 0) += m2(2, 0); m1(2, 1) += m2(2, 1); m1(2, 2) += m2(2, 2);
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substract scalar from matrix componentwise
|
||||
*
|
||||
* @param(m) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator-= (TMatrix3<T>& m1, T s)
|
||||
{
|
||||
m1(0, 0) -= s; m1(0, 1) -= s; m1(0, 2) -= s;
|
||||
m1(1, 0) -= s; m1(1, 1) -= s; m1(1, 2) -= s;
|
||||
m1(2, 0) -= s; m1(2, 1) -= s; m1(2, 2) -= s;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substract matrix from matrix componentwise
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator-= (TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
m1(0, 0) -= m2(0, 0); m1(0, 1) -= m2(0, 1); m1(0, 2) -= m2(0, 2);
|
||||
m1(1, 0) -= m2(1, 0); m1(1, 1) -= m2(1, 1); m1(1, 2) -= m2(1, 2);
|
||||
m1(2, 0) -= m2(2, 0); m1(2, 1) -= m2(2, 1); m1(2, 2) -= m2(2, 2);
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply matrix with scalar
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator*= (TMatrix3<T>& m1, T s)
|
||||
{
|
||||
m1(0, 0) *= s; m1(0, 1) *= s; m1(0, 2) *= s;
|
||||
m1(1, 0) *= s; m1(1, 1) *= s; m1(1, 2) *= s;
|
||||
m1(2, 0) *= s; m1(2, 1) *= s; m1(2, 2) *= s;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matrix on matrix multiplication
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator*= (TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
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, 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(1, 0) = c(1, 0) * m2(0, 0) + c(1, 1) * m2(1, 0) + c(1, 2) * m2(2, 0);
|
||||
m1(1, 1) = c(1, 0) * m2(0, 1) + c(1, 1) * m2(1, 1) + c(1, 2) * m2(2, 1);
|
||||
m1(1, 2) = c(1, 0) * m2(0, 2) + c(1, 1) * m2(1, 2) + c(1, 2) * m2(2, 2);
|
||||
|
||||
m1(2, 0) = c(2, 0) * m2(0, 0) + c(2, 1) * m2(1, 0) + c(2, 2) * m2(2, 0);
|
||||
m1(2, 1) = c(2, 0) * m2(0, 1) + c(2, 1) * m2(1, 1) + c(2, 2) * m2(2, 1);
|
||||
m1(2, 2) = c(2, 0) * m2(0, 2) + c(2, 1) * m2(1, 2) + c(2, 2) * m2(2, 2);
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add scalar to matrix componentwise
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator+ (const TMatrix3<T>& m, T s)
|
||||
{
|
||||
return TMatrix3<T>(m(0, 0) + s, m(0, 1) + s, m(0, 2) + s,
|
||||
m(1, 0) + s, m(1, 1) + s, m(1, 2) + s,
|
||||
m(2, 0) + s, m(2, 1) + s, m(2, 2) + s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add matrix to matrix componentwise
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator+ (const TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
return TMatrix2<T>(m1(0, 0) + m2(0, 0), m1(0, 1) + m2(0, 1), m1(0, 2) + m2(0, 2),
|
||||
m1(1, 0) + m2(1, 0), m1(1, 1) + m2(1, 1), m1(1, 2) + m2(1, 2),
|
||||
m1(2, 0) + m2(2, 0), m1(2, 1) + m2(2, 1), m1(2, 2) + m2(2, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add scalar to matrix componentwise
|
||||
*
|
||||
* @param(m) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator- (const TMatrix3<T>& m, T s)
|
||||
{
|
||||
return TMatrix3<T>(m(0, 0) - s, m(0, 1) - s, m(0, 2) - s,
|
||||
m(1, 0) - s, m(1, 1) - s, m(1, 2) - s,
|
||||
m(2, 0) - s, m(2, 1) - s, m(2, 2) - s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add scalar to matrix componentwise
|
||||
*
|
||||
* @param(m) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator- (const TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
return TMatrix3<T>(m1(0, 0) - m2(0, 0), m1(0, 1) - m2(0, 1), m1(0, 2) - m2(0, 2),
|
||||
m1(1, 0) - m2(1, 0), m1(1, 1) - m2(1, 1), m1(1, 2) - m2(1, 2),
|
||||
m1(2, 0) - m2(2, 0), m1(2, 1) - m2(2, 1), m1(2, 2) - m2(2, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply scalar with matrix
|
||||
*
|
||||
* @param(m) Matrix
|
||||
* @param(s) Scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator* (const TMatrix3<T>& m, float s)
|
||||
{
|
||||
return TMatrix3<T>(m(0, 0) * s, m(0, 1) * s, m(0, 2) * s,
|
||||
m(1, 0) * s, m(1, 1) * s, m(1, 2) * s,
|
||||
m(2, 0) * s, m(2, 1) * s, m(2, 2) * s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplay matrix by matrix
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> operator* (const TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
return TMatrix3<T>(m1(0, 0) * m2(0, 0) + m1(0, 1) * m2(1, 0) + m1(0, 2) * m2(2, 0),
|
||||
m1(0, 0) * m2(0, 1) + m1(0, 1) * m2(1, 1) + m1(0, 2) * m2(2, 1),
|
||||
m1(0, 0) * m2(0, 2) + m1(0, 1) * m2(1, 2) + m1(0, 2) * m2(2, 2),
|
||||
|
||||
m1(1, 0) * m2(0, 0) + m1(1, 1) * m2(1, 0) + m1(1, 2) * m2(2, 0),
|
||||
m1(1, 0) * m2(0, 1) + m1(1, 1) * m2(1, 1) + m1(1, 2) * m2(2, 1),
|
||||
m1(1, 0) * m2(0, 2) + m1(1, 1) * m2(1, 2) + m1(1, 2) * m2(2, 2),
|
||||
|
||||
m1(2, 0) * m2(0, 0) + m1(2, 1) * m2(1, 0) + m1(2, 2) * m2(2, 0),
|
||||
m1(2, 0) * m2(0, 1) + m1(2, 1) * m2(1, 1) + m1(2, 2) * m2(2, 1),
|
||||
m1(2, 0) * m2(0, 2) + m1(2, 1) * m2(1, 2) + m1(2, 2) * m2(2, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add matrix to matrix componentwise
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TVector3<T> operator* (const TMatrix3<T>& m1, const TVector3<T>& v)
|
||||
{
|
||||
return TVector3<T>(m1(0, 0) * v.x + m1(0, 1) * v.y + m1(0, 2) * v.z,
|
||||
m1(1, 0) * v.x + m1(1, 1) * v.y + m1(1, 2) * v.z,
|
||||
m1(2, 0) * v.x + m1(2, 1) * v.y + m1(2, 2) * v.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare matrix with other matrix.
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool operator== (const TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
return (m1[0] == m2[0] && m1[1] == m2[1] && m1[2] == m2[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare matrix with other matrix.
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
* @param(m2) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool operator!= (const TMatrix3<T>& m1, const TMatrix3<T>& m2)
|
||||
{
|
||||
return (m1[0] != m2[0] || m1[1] != m2[1] || m1[2] != m2[2]);
|
||||
}
|
||||
|
||||
|
||||
// =============================== //
|
||||
// Matrix function definition //
|
||||
// =============================== //
|
||||
|
||||
/**
|
||||
* Calculate determinant of 3x3 Matrix
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
T Determinant(const TMatrix3<T>& m1)
|
||||
{
|
||||
return m1(0, 0) * (m1(1, 1) * m1(2, 2) - m1(1, 2) * m1(2, 1))
|
||||
- m1(0, 1) * (m1(1, 0) * m1(2, 2) - m1(1, 2) * m1(2, 0))
|
||||
+ m1(0, 2) * (m1(1, 0) * m1(2, 1) - m1(1, 1) * m1(2, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate inverse of 3x3 Matrix
|
||||
*
|
||||
* @see [FUNC]Inverse
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
*
|
||||
* @note Stores result in m1.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> InverseV(TMatrix3<T>& m1)
|
||||
{
|
||||
const TVector3<T>& v0 = m1[0];
|
||||
const TVector3<T>& v1 = m1[1];
|
||||
const TVector3<T>& v2 = m1[2];
|
||||
|
||||
TVector3<T> r0 = CrossP(v1, v2);
|
||||
TVector3<T> r1 = CrossP(v2, v0);
|
||||
TVector3<T> r2 = CrossP(v0, v1);
|
||||
|
||||
T _1_det = (T)1.0 / determinant(m1);
|
||||
|
||||
m1 = TMatrix3<T>(r0.x, r0.y, r0.z,
|
||||
r1.x, r1.y, r1.z,
|
||||
r2.x, r2.y, r2.z);
|
||||
|
||||
m1 *= _1_det;
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transpose of matrix.
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
*
|
||||
* @note Result is stored in m1;
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> TransposeV(TMatrix3<T>& m1)
|
||||
{
|
||||
Swap(m1(0, 1), m1(1, 0));
|
||||
Swap(m1(0, 2), m1(2, 0));
|
||||
Swap(m1(1, 2), m1(2, 1));
|
||||
|
||||
return m1;
|
||||
}
|
||||
|
||||
|
||||
// =============== //
|
||||
// WITH RETURN //
|
||||
// =============== //
|
||||
|
||||
/**
|
||||
* Calculate inverse of 3x3 Matrix
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> Inverse(TMatrix3<T>& m1)
|
||||
{
|
||||
const TVector3<T>& v0 = m1[0];
|
||||
const TVector3<T>& v1 = m1[1];
|
||||
const TVector3<T>& v2 = m1[2];
|
||||
|
||||
TVector3<T> r0 = CrossP(v1, v2);
|
||||
TVector3<T> r1 = CrossP(v2, v0);
|
||||
TVector3<T> r2 = CrossP(v0, v1);
|
||||
|
||||
T _1_det = (T)1.0 / Determinant(m1);
|
||||
|
||||
TMatrix3<T> inverse(r0.x, r0.y, r0.z,
|
||||
r1.x, r1.y, r1.z,
|
||||
r2.x, r2.y, r2.z);
|
||||
|
||||
inverse *= _1_det;
|
||||
|
||||
return inverse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get transpose of matrix.
|
||||
*
|
||||
* @param(m1) Matrix
|
||||
*
|
||||
* @note Result is stored in m1;
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TMatrix3<T> Transpose(const TMatrix3<T>& m1)
|
||||
{
|
||||
return TMatrix3<T>(m1(0, 0), m1(1, 0), m1(2, 0),
|
||||
m1(0, 1), m1(1, 1), m1(2, 1),
|
||||
m1(0, 2), m1(1, 2), m1(2, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if matrix is an identity matrix.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool IsIndentityMatrix(const TMatrix3<T>& m1)
|
||||
{
|
||||
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1) - (T)1.0) < P_FLT_INAC && abs(m1(0, 2) - (T)1.0) < P_FLT_INAC &&
|
||||
abs(m1(1, 0) - (T)1.0) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && abs(m1(1, 2) - (T)1.0) < P_FLT_INAC &&
|
||||
abs(m1(2, 0) - (T)1.0) < P_FLT_INAC && abs(m1(2, 1) - (T)1.0) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC);
|
||||
}
|
||||
|
||||
} // Phanes::Core::Math
|
||||
|
||||
|
||||
#endif // !MATRIX3_H
|
960
Engine/Source/Runtime/Core/public/Math/Plane.hpp
Normal file
960
Engine/Source/Runtime/Core/public/Math/Plane.hpp
Normal file
@@ -0,0 +1,960 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
// TODO: Transform
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#include "Core/public/Math/Line.hpp"
|
||||
#include "Core/public/Math/Ray.hpp"
|
||||
#include "Core/public/Math/Vector3.hpp"
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
// Plane in 3D space, defined as: P: ax + by + cz = d;
|
||||
|
||||
template<RealType T>
|
||||
struct TPlane
|
||||
{
|
||||
public:
|
||||
using Real = T;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
/** X Part of the normal. */
|
||||
Real x;
|
||||
|
||||
/** Y Part of the normal. */
|
||||
Real y;
|
||||
|
||||
/** Z Part of the normal. */
|
||||
Real z;
|
||||
};
|
||||
|
||||
TVector3<Real> normal;
|
||||
};
|
||||
|
||||
/** Scalar component of plane. */
|
||||
union
|
||||
{
|
||||
Real d;
|
||||
Real w;
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
|
||||
TPlane() = default;
|
||||
|
||||
/**
|
||||
* Copy constructor
|
||||
*/
|
||||
|
||||
TPlane(const TPlane<Real>& plane) : normal(plane.normal), d(plane.d) {};
|
||||
|
||||
/**
|
||||
* Move constructor
|
||||
*/
|
||||
|
||||
TPlane(TPlane<Real>&& plane) :
|
||||
normal(std::move(plane.normal)),
|
||||
d(std::move(plane.d))
|
||||
{}
|
||||
|
||||
|
||||
/**
|
||||
* Construct plane from normal and d
|
||||
*
|
||||
* @param(normal) Normal of plane
|
||||
* @param(d) Scalar component
|
||||
*
|
||||
* @note Normal is NOT normalized, make sure to normalize [PARAM]normal, or use [FUNC]CreateFromVector. Otherwise unexpected results may occur using the plane.
|
||||
*/
|
||||
|
||||
TPlane(const TVector3<Real>& normal, Real d) :
|
||||
normal(normal),
|
||||
d(d)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Construct plane from normal and base point.
|
||||
*
|
||||
* @param(normal) Normal of plane
|
||||
* @param(base) Base point
|
||||
*/
|
||||
|
||||
TPlane(const TVector3<Real>& normal, const TVector3<Real>& base) :
|
||||
normal(normal)
|
||||
{
|
||||
this->d = DotP(this->normal, base);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct plane from coefficients
|
||||
*
|
||||
* @param(x) X coefficient
|
||||
* @param(y) Y coefficient
|
||||
* @param(z) Z coefficient
|
||||
* @param(d) D coefficient
|
||||
*/
|
||||
|
||||
TPlane(Real x, Real y, Real z, Real d) :
|
||||
d(d)
|
||||
{
|
||||
this->normal = TVector3<Real>(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct plane from 3 points
|
||||
*
|
||||
* @param(p1) Point one
|
||||
* @param(p2) Point two
|
||||
* @param(p3) Point three
|
||||
*/
|
||||
|
||||
TPlane(const TVector3<Real>& p1, const TVector3<Real>& p2, const TVector3<Real>& p3)
|
||||
{
|
||||
this->normal = Normalize(CrossP(p1, p2));
|
||||
this->d = DotP(this->normal, p3);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// ======================== //
|
||||
// Operators for TPlane //
|
||||
// ======================== //
|
||||
|
||||
|
||||
/**
|
||||
* Adds pl2 to pl1.
|
||||
*
|
||||
* @param(pl1) Plane to add to
|
||||
* @param(pl2) Plane to add
|
||||
*
|
||||
* @note This leads to the plane not being normalized anymore. Use PlaneNormalizeV to normalize again.
|
||||
* @see [FUNC] PlaneNormalizeV
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator+= (TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
pl1.normal += pl2.normal; pl1.d += pl2.d;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substracts pl2 from pl1.
|
||||
*
|
||||
* @param(pl1) Plane to substract from
|
||||
* @param(pl2) Plane to substract
|
||||
*
|
||||
* @note This leads to the plane not being normalized anymore. Use PlaneNormalizeV to normalize again.
|
||||
* @see [FUNC] PlaneNormalizeV
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator-= (TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
pl1.normal -= pl2.normal; pl1.d -= pl2.d;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies pl1 with pl2.
|
||||
*
|
||||
* @param(pl1) Plane to multiply
|
||||
* @param(pl2) Plane to multiply with
|
||||
*
|
||||
* @note This leads to the plane not being normalized anymore. Use PlaneNormalizeV to normalize again.
|
||||
* @see [FUNC] PlaneNormalizeV
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator*= (TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
pl1.x *= pl2.x; pl1.y *= pl2.y; pl1.z *= pl2.z; pl1.d *= pl2.d;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies pl1 with a scalar
|
||||
*
|
||||
* @param(pl1) Plane to multiply
|
||||
* @param(s) Scalar to multiply with
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator*= (TPlane<T>& pl1, T s)
|
||||
{
|
||||
pl1.normal *= s; pl1 *= s;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides pl1 with a scalar
|
||||
*
|
||||
* @param(pl1) Plane to divide
|
||||
* @param(s) Scalar to divide with
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator/= (TPlane<T>& pl1, T s)
|
||||
{
|
||||
T _1_s = (T)1.0 / s;
|
||||
|
||||
pl1.normal *= _1_s; pl1 *= _1_s;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add two planes.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(pl2) Plane
|
||||
*
|
||||
* @return Sum of planes
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator+ (const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return TPlane<T>(pl1.normal + pl2.normal, pl1.d + pl2.d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Substracts two planes.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(pl2) Plane
|
||||
*
|
||||
* @return Difference of the planes
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator- (const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return TPlane<T>(pl1.normal - pl2.normal, pl1.d - pl2.d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies two planes.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(pl2) Plane
|
||||
*
|
||||
* @return Product of planes
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator* (const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return TPlane<T>(pl1.x * pl2.x, pl1.y * pl2.y, pl1.z * pl2.z, pl1.d * pl2.d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies pl1 with a scalar
|
||||
*
|
||||
* @param(pl1) Plane to multiply
|
||||
* @param(s) Scalar to multiply with
|
||||
*
|
||||
* @return Product of plane and scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator*= (const TPlane<T>& pl1, T s)
|
||||
{
|
||||
return TPlane<T>(pl1.normal * s, pl1.d * s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides pl1 with a scalar
|
||||
*
|
||||
* @param(pl1) Plane to divide
|
||||
* @param(s) Scalar to divide with
|
||||
*
|
||||
* @return Quotient of plane and scalar
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> operator/= (const TPlane<T>& pl1, T s)
|
||||
{
|
||||
T _1_s = (T)1.0 / s;
|
||||
|
||||
return TPlane<T>(pl1.normal * _1_s, pl1.d * _1_s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests two planes for equality
|
||||
*
|
||||
* @see [FUNC] Equals
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
*
|
||||
* @return True, if planes are equal, false, if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool operator== (const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return pl1.normal == pl2.normal && abs(pl1.d - pl2.d) < P_FLT_INAC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests two planes for inequality
|
||||
*
|
||||
* @see [FUNC] Equals
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
*
|
||||
* @return True, if planes are inequal, false, if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool operator!= (const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return pl1.normal != pl2.normal || abs(pl1.d - pl2.d) >= P_FLT_INAC;
|
||||
}
|
||||
|
||||
|
||||
// ======================= //
|
||||
// Functions of TPlane //
|
||||
// ======================= //
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Tests whether two planes are perpendicular.
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(threshold) Allowed T inaccuracy
|
||||
*
|
||||
* @return True if perpendicular, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool IsPerpendicular(const TPlane<T>& pl1, const TPlane<T>& pl2, T threshold = P_FLT_INAC)
|
||||
{
|
||||
return (abs(DotP(pl1.normal, pl2.normal)) < threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two planes are parallel.
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(threshold) Allowed T inaccuracy from one (e.g. 0.98f)
|
||||
*
|
||||
* @return True if parallel, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool IsParallel(const TPlane<T>& pl1, const TPlane<T>& pl2, T threshold = 1.0f - P_FLT_INAC)
|
||||
{
|
||||
return (abs(DotP(pl1.normal, pl2.normal)) > threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two planes are coincident (Parallel and point in same direction).
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(threshold) Allowed T inaccuracy from one (e.g. 0.98f)
|
||||
*
|
||||
* @return True if coincident, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool IsCoincident(const TPlane<T>& pl1, const TPlane<T>& pl2, T threshold = 1.0f - P_FLT_INAC)
|
||||
{
|
||||
return (DotP(pl1.normal, pl2.normal) > threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether pl1 plane is a unit vector.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(threshold) Allowed T inaccuracy
|
||||
*
|
||||
* @return True if unit vector, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool IsNormalized(const TPlane<T>& pl1, T threshold = P_FLT_INAC)
|
||||
{
|
||||
return (SqrMagnitude(pl1.normal) < threshold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two planes are the same
|
||||
*
|
||||
* @see [FUNC]Equals
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(threshold) Allowed T inaccuracy
|
||||
*
|
||||
* @return True if same, false if not.
|
||||
* @note Planes must be normalized.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool IsSame(const TPlane<T>& pl1, const TPlane<T>& pl2, T threshold = P_FLT_INAC)
|
||||
{
|
||||
|
||||
return DotP(pl1.normal, pl2.normal) > threshold && abs(pl1.d - pl2.d) < P_FLT_INAC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> PlaneNormalizeV(TPlane<T>& pl1)
|
||||
{
|
||||
T normVec = SqrMagnitude(pl1);
|
||||
|
||||
T scale = (normVec > P_FLT_INAC) ? (T)1.0 / sqrt(normVec) : 1.0f;
|
||||
|
||||
pl1.normal *= scale; pl1.d *= scale;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @return Normalized plane
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> PlaneNormalize(TPlane<T>& pl1)
|
||||
{
|
||||
T normVec = SqrMagnitude(pl1);
|
||||
|
||||
T scale = (normVec > P_FLT_INAC) ? (T)1.0 / sqrt(normVec) : 1.0f;
|
||||
|
||||
return TPlane<T>(pl1.normal * scale, pl1.d * scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @note Does not check for zero vector pl1.normal.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> PlaneUnsafeNormalizeV(TPlane<T>& pl1)
|
||||
{
|
||||
T scale = (T)1.0 / Magnitude(pl1);
|
||||
|
||||
pl1.normal *= scale; pl1.d *= scale;
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @return Normalized plane
|
||||
*
|
||||
* @note Does not check for zero vector pl1.normal.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> PlaneUnsafeNormalize(TPlane<T>& pl1)
|
||||
{
|
||||
T scale = (T)1.0 / Magnitude(pl1);
|
||||
|
||||
return TPlane<T>(pl1.normal * scale, pl1.d * scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dot product between two planes
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE T PlaneDotP(const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return DotP(pl1.normal, pl2.normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get angle between two planes
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE T PlaneAngle(const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return Angle(pl1.normal, pl2.normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cosine of angle between two planes
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE T PlaneCosAngle(const TPlane<T>& pl1, const TPlane<T>& pl2)
|
||||
{
|
||||
return CosineAngle(pl1.normal, pl2.normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flip plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> FlipV(const TPlane<T>& pl1)
|
||||
{
|
||||
pl1.normal = -pl1.normal;
|
||||
pl1.d = -pl1.d;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get flipped plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @return Flipped plane
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> Flip(const TPlane<T>& pl1)
|
||||
{
|
||||
return TPlane<T>(-pl1.normal, -pl1.d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform plane with Transform
|
||||
*
|
||||
* @param(pl) Plane
|
||||
* @param(tr) Transform
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TPlane<T> TransformV(TPlane<T>& pl, const TTransform<T>& tr)
|
||||
{
|
||||
// TODO: Do with operator*
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Transform plane with Transform
|
||||
*
|
||||
* @param(pl) Plane
|
||||
* @param(tr) Transform
|
||||
*
|
||||
* @return Transformed plane.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TPlane<T> Transform(const TPlane<T>& pl, const TTransform<T>& tr)
|
||||
{
|
||||
// TODO: Do with operator*
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates distance bewteen point and plane.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(p1) Point
|
||||
*
|
||||
* @return Distance from point to plane
|
||||
* @note Distance is 0 if point is on plane, >0 if it's in front and <0 if it's on the backside.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
T PointDistance(const TPlane<T>& pl1, const TVector3<T>& p1)
|
||||
{
|
||||
return (pl1.x * p1.x + pl1.y * p1.y + pl1.z * p1.z) - pl1.d;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the origin point (base) of the plane
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @return Base of plane
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TVector3<T> GetOrigin(const TPlane<T>& pl1)
|
||||
{
|
||||
return TVector3<T>(pl1.normal * d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates plane by vector
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(v1) Vector
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> TranslateV(TPlane<T>& pl1, const TVector3<T>& v1)
|
||||
{
|
||||
|
||||
pl1.d = DotP(this->normal, GetOrigin(pl1) + v1);
|
||||
|
||||
return pl1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates plane by vector
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(v1) Vector
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TPlane<T> Translate(TPlane<T>& pl1, const TVector3<T>& v1)
|
||||
{
|
||||
return TPlane<T>(pl1.normal, GetOrigin(pl1) + v1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the side a point is on.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(p1) Point
|
||||
*
|
||||
* @return True, if it's in the front and false, if it's on the back.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool GetSide(const TPlane<T>& pl1, const TVector3<T>& p1)
|
||||
{
|
||||
return (pl1.d <= DotP(pl1.normal, p1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects vector onto plane
|
||||
*
|
||||
* @param(v1) Vector to reject
|
||||
* @param(plane) Plane
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
* @note Simply rejects v1 from normal
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ProjectOntoPlaneV(TVector3<T>& v1, const TPlane<T>& plane)
|
||||
{
|
||||
return RejectV(v1, plane.normal);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Projects vector onto plane
|
||||
*
|
||||
* @param(v1) Vector to reject
|
||||
* @param(normal) Normal of the plane
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
* @note Simply rejects v1 from normal
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ProjectOntoPlaneV(TVector3<T>& v1, const TVector3<T>& normal)
|
||||
{
|
||||
return RejectV(v1, normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reflect from plane
|
||||
*
|
||||
* @param(v1) Vector to mirror
|
||||
* @param(plane) Plane to mirror on
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ReflectFromPlaneV(TVector3<T>& v1, const TPlane<T>& plane)
|
||||
{
|
||||
return ReflectV(v1, plane.normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reflect from plane
|
||||
*
|
||||
* @param(v1) Vector to mirror
|
||||
* @param(normal) Normal of plane
|
||||
*
|
||||
* @note result is stored in v1.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ReflectFromPlaneV(TVector3<T>& v1, const TVector3<T>& normal)
|
||||
{
|
||||
return ReflectV(v1, normal);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reflect from plane
|
||||
*
|
||||
* @param(v1) Vector to mirror
|
||||
* @param(plane) Plane to mirror on
|
||||
*
|
||||
* @return Reflected vector
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ReflectFromPlane(const TVector3<T>& v1, const TPlane<T>& plane)
|
||||
{
|
||||
return Reflect(v1, plane.normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reflect from plane
|
||||
*
|
||||
* @param(v1) Vector to mirror
|
||||
* @param(plane) Normal of plane
|
||||
*
|
||||
* @return Reflected vector
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ReflectFromPlane(const TVector3<T>& v1, const TVector3<T>& normal)
|
||||
{
|
||||
return Reflect(v1, normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects vector onto plane
|
||||
*
|
||||
* @see [FUNC]PointProjectOntoPlane
|
||||
*
|
||||
* @param(v1) Vector to reject
|
||||
* @param(normal) Normal of the plane
|
||||
*
|
||||
* @return Projected vector
|
||||
* @note Simply rejects the vector from normal
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ProjectOntoPlane(const TVector3<T>& v1, const TVector3<T>& normal)
|
||||
{
|
||||
return Reject(v1, normal);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Projects vector onto plane
|
||||
*
|
||||
* @see [FUNC]PointProjectOntoPlane
|
||||
*
|
||||
* @param(v1) Vector to reject
|
||||
* @param(plane) Plane
|
||||
*
|
||||
* @return Projected vector
|
||||
* @note Simply rejects the vector from normal
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE TVector3<T> ProjectOntoPlane(const TVector3<T>& v1, const TPlane<T>& plane)
|
||||
{
|
||||
return Reject(v1, plane.normal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests planes for equality
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(threshold) Allowed inaccuracy
|
||||
*
|
||||
* @return True, if equal, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool Equals(const TPlane<T>& pl1, const TPlane<T>& pl2, T threshold = P_FLT_INAC)
|
||||
{
|
||||
return Equals(pl1.normal, pl2.normal, threshold) && abs(pl1.d - pl2.d) < threshold;
|
||||
}
|
||||
|
||||
|
||||
/** Tests whether a point is on a plane
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(p1) Point
|
||||
*
|
||||
* @return True, if p1 on pl1, false if not.
|
||||
*/
|
||||
template<RealType T>
|
||||
FORCEINLINE bool IsPointOnPlane(const TPlane<T>& pl1, const TVector3<T>& p1)
|
||||
{
|
||||
return (Equals(DotP(pl1.normal, p1), d));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether two planes intersect. Sets line to intersection-line if true.
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(interLine) Line of intersection
|
||||
* @param(threshold) Threshold for parallel planes.
|
||||
*
|
||||
* @return True, if planes intersect, false, if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool PlanesIntersect2(const TPlane<T>& pl1, const TPlane<T>& pl2, Ref<TLine<T>> interLine, T threshold = P_FLT_INAC)
|
||||
{
|
||||
TVector3<T> dirLine = CrossP(pl1.normal, pl2.normal);
|
||||
T det = SqrMagnitude(dirLine);
|
||||
|
||||
if (abs(det) > P_FLT_INAC)
|
||||
{
|
||||
interLine = MakeRef<TLine<T>(dirLine, (CrossP(dirLine, pl2.normal) * pl1.d + CrossP(dirLine, pl1.normal) * pl2.d) / det);
|
||||
NormalizeV(interLine);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether three planes intersect. Sets line to intersection-line if true.
|
||||
*
|
||||
* @param(pl1) Plane one
|
||||
* @param(pl2) Plane two
|
||||
* @param(pl3) Plane three
|
||||
* @param(interPoint) Point of intersection
|
||||
* @param(threshold) Threshold for parallel planes.
|
||||
*
|
||||
* @return True, if all planes intersect, false, if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool PlanesIntersect3(const TPlane<T>& pl1, const TPlane<T>& pl2, const TPlane<T>& pl3, Ref<TVector3<T>> interPoint, T threshold = P_FLT_INAC)
|
||||
{
|
||||
T det = DotP(CrossP(pl1.normal, pl2.normal), pl3.normal);
|
||||
|
||||
if (abs(det) > P_FLT_INAC)
|
||||
{
|
||||
interPoint = MakeRef<TVector3<T>>((CrossP(pl3.normal, pl2.normal) * pl1.d + CrossP(pl1.normal, pl3.normal) * pl2.d) / det);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mirrors a point through plane
|
||||
*
|
||||
* @param(p1) Point to mirror
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @return Mirrored point.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TVector3<T> PlaneMirrorPoint(const TVector3<T>& p1, const TPlane<T>& pl1)
|
||||
{
|
||||
return p1 - pl1.normal * ((T)2.0 * PointDistance(pl1, p1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects point onto plane
|
||||
*
|
||||
* @param(p1) Point to project
|
||||
* @param(pl1) Plane
|
||||
*
|
||||
* @return Projected point.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TVector3<T> PointProjectOntoPlane(const TVector3<T>& p1, const TPlane<T>& pl1)
|
||||
{
|
||||
p1 - PointDistance(pl1, p1) * pl1.normal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the intersection point, of a line with a plane, if there is one
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(l1) Line
|
||||
* @param(p1) Point
|
||||
*
|
||||
* @return True, if they intersect, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool LineIntersect(const TPlane<T>& pl1, const TLine<T>& l1, Ref<TVector3<T>> p1)
|
||||
{
|
||||
T dotProduct = DotP(l1.normal, pl1.normal);
|
||||
|
||||
if (abs(dotProduct) > P_FLT_INAC)
|
||||
{
|
||||
p1 = MakeRef<TVector3<T>>(l1.base - l1.normal * (DotP(l1.normal * p1.base) / dotProduct));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates, the intersection point, of a plane and a ray.
|
||||
*
|
||||
* @param(pl1) Plane
|
||||
* @param(r1) Ray
|
||||
* @param(p1) Intersection point
|
||||
*
|
||||
* @return True, if they intersect, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
bool RayIntersect(const TPlane<T>& pl1, const TRay<T>& r1, Ref<TVector3<T>> p1)
|
||||
{
|
||||
T pr = DotP(pl1.normal, Normalize(r1.direction));
|
||||
T parameter = DotP((GetOrigin(pl1) - r1.origin), pl1.normal) / pr;
|
||||
|
||||
if (p1 > P_FLT_INAC && parameter >= 0)
|
||||
{
|
||||
p1 = MakeRef<TVector3<T>>(PointAt(r1, parameter));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // Phanes::Core::Math
|
||||
|
||||
|
193
Engine/Source/Runtime/Core/public/Math/Point.hpp
Normal file
193
Engine/Source/Runtime/Core/public/Math/Point.hpp
Normal file
@@ -0,0 +1,193 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
|
||||
#include "Core/public/Math/MathCommon.hpp"
|
||||
#include "Core/public/Math/MathAbstractTypes.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#include "Core/public/Math/Vector2.hpp"
|
||||
#include "Core/public/Math/Vector3.hpp"
|
||||
|
||||
#ifndef P_DEBUG
|
||||
#pragma warning(disable : 4244)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* The Point is the same as a vector. The type exists, to ensure
|
||||
* differentiation between the two types.
|
||||
*/
|
||||
|
||||
#ifndef POINT_H
|
||||
#define POINT_H
|
||||
|
||||
namespace Phanes::Core::Math {
|
||||
|
||||
/**
|
||||
* A 2D Point with components x and y with float precision.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
struct TPoint2 : public TVector2<T> {
|
||||
static_assert(std::is_floating_point_v<T>, "T must be a floating point");
|
||||
|
||||
using TVector2<T>::TVector2;
|
||||
|
||||
using Real = T;
|
||||
|
||||
/**
|
||||
* Creates Point2 from Point3's xy
|
||||
*
|
||||
* @param a Point3 one
|
||||
*/
|
||||
|
||||
TPoint2(const TPoint3<T>& p)
|
||||
{
|
||||
this->x = p.x;
|
||||
this->y = p.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Point2 from Point4's xy
|
||||
*
|
||||
* @param a Point4 one
|
||||
*/
|
||||
|
||||
TPoint2(const TPoint4<T>& p)
|
||||
{
|
||||
this->x = p.x;
|
||||
this->y = p.y;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculates distance between two points.
|
||||
*
|
||||
* @param(p1) Point one
|
||||
* @param(p2) Point two
|
||||
*
|
||||
* @return Distance between two points.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
T Distance(const TPoint2<T>& p1, const TPoint2<T>& p2)
|
||||
{
|
||||
return Magnitude(p2 - p1);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* A 3D Point with components x and y with float precision.
|
||||
*/
|
||||
|
||||
|
||||
template<RealType T>
|
||||
struct TPoint3 : public TVector3<T> {
|
||||
static_assert(std::is_floating_point_v(T), "T must be a floating point");
|
||||
|
||||
using TVector3<T>::TVector3;
|
||||
|
||||
using Real = T;
|
||||
|
||||
/**
|
||||
* Creates Point3 from Point2's xy and zero
|
||||
*
|
||||
* @param a Point2 one
|
||||
*/
|
||||
|
||||
TPoint3(const TPoint2<T>& p)
|
||||
{
|
||||
this->x = p.x;
|
||||
this->y = p.y;
|
||||
this->z = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates Point3 from Point4's xyz
|
||||
*
|
||||
* @param a Point4 one
|
||||
*/
|
||||
|
||||
TPoint3(const TPoint4<T>& p)
|
||||
{
|
||||
this->x = p.x;
|
||||
this->y = p.y;
|
||||
this->z = p.z;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculates distance between two points.
|
||||
*
|
||||
* @param(p1) Point one
|
||||
* @param(p2) Point two
|
||||
*
|
||||
* @return Distance between two points.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
T Distance(const TPoint3<T>& p1, const TPoint3<T>& p2)
|
||||
{
|
||||
return Magnitude(p2 - p1);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* A 4D Point with components x and y with float precision.
|
||||
*/
|
||||
|
||||
|
||||
//template<RealType T>
|
||||
//struct TPoint4 : public TVector4<T> {
|
||||
// static_assert(std::is_floating_point_v(T), "T must be a floating point");
|
||||
|
||||
// using TVector4<T>::TVector4;
|
||||
|
||||
// /**
|
||||
// * Creates Point4 from Point2's xy and the last two zero
|
||||
// *
|
||||
// * @param a Point2 one
|
||||
// */
|
||||
|
||||
// TPoint4(const TPoint2<T>& p)
|
||||
// {
|
||||
// this->x = p.x;
|
||||
// this->y = p.y;
|
||||
// this->z = 0;
|
||||
// this->w = 0;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Creates Point4 from Point3's xyz and zero
|
||||
// *
|
||||
// * @param a Point3 one
|
||||
// */
|
||||
|
||||
// TPoint4(const TPoint3<T>& p)
|
||||
// {
|
||||
// this->x = p.x;
|
||||
// this->y = p.y;
|
||||
// this->z = p.z;
|
||||
// this->w = 0;
|
||||
// }
|
||||
//};
|
||||
|
||||
///**
|
||||
// * Calculates distance between two points.
|
||||
// *
|
||||
// * @param(p1) Point one
|
||||
// * @param(p2) Point two
|
||||
// *
|
||||
// * @return Distance between two points.
|
||||
// */
|
||||
|
||||
//template<RealType T>
|
||||
//T Distance(const TPoint4<T>& p1, const TPoint4<T>& p2);
|
||||
|
||||
} // phanes::core::math::coretypes
|
||||
|
||||
#endif // !POINT_H
|
12
Engine/Source/Runtime/Core/public/Math/README.md
Normal file
12
Engine/Source/Runtime/Core/public/Math/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# PhanesCore
|
||||
|
||||
## Math
|
||||
|
||||
### Description
|
||||
|
||||
Math lib.
|
||||
|
||||
|
||||
### Notes
|
||||
|
||||
- Normals are called normals for a reason.
|
127
Engine/Source/Runtime/Core/public/Math/Ray.hpp
Normal file
127
Engine/Source/Runtime/Core/public/Math/Ray.hpp
Normal file
@@ -0,0 +1,127 @@
|
||||
#pragma once
|
||||
|
||||
#include "Core/public/Math/Boilerplate.h"
|
||||
#include "Core/public/Math/MathFwd.h"
|
||||
|
||||
#include "Core/public/Math/Vector3.hpp"
|
||||
|
||||
namespace Phanes::Core::Math
|
||||
{
|
||||
|
||||
// Ray with origin and direction (L = p + t * v)
|
||||
|
||||
template<RealType T>
|
||||
struct TRay
|
||||
{
|
||||
public:
|
||||
using Real = T;
|
||||
|
||||
TVector3<Real> origin;
|
||||
TVector3<Real> direction;
|
||||
|
||||
public:
|
||||
/** Default constructor */
|
||||
TRay() = default;
|
||||
|
||||
/** Copy constructor */
|
||||
TRay(const TRay<Real>& r) : direction(r.direction), origin(r.origin) {};
|
||||
|
||||
/** Move constructor */
|
||||
TRay(TRay<Real>&& r) : direction(std::move(r.direction)), origin(std::move(r.origin)) {};
|
||||
|
||||
/**
|
||||
* Construct ray from origin and direction.
|
||||
*
|
||||
* @param(direction) Direction
|
||||
* @param(origin) Origin
|
||||
*/
|
||||
|
||||
TRay(const TVector3<Real>& direction, const TVector3<Real>& origin) : direction(direction), origin(origin) {};
|
||||
|
||||
};
|
||||
|
||||
// ================== //
|
||||
// TRay operators //
|
||||
// ================== //
|
||||
|
||||
|
||||
/**
|
||||
* Tests two rays for equality
|
||||
*
|
||||
* @param(r1) Ray one
|
||||
* @param(r2) Ray two
|
||||
*
|
||||
* @return True, if same and false, if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE bool operator== (const TRay<T>& r1, const TRay<T>& r2)
|
||||
{
|
||||
return (r1.origin == r2.origin && r1.direction == r2.direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests two rays for inequality
|
||||
*
|
||||
* @param(r1) Ray one
|
||||
* @param(r2) Ray two
|
||||
*
|
||||
* @return True, if not same and false, if same.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
FORCEINLINE bool operator== (const TRay<T>& r1, const TRay<T>& r2)
|
||||
{
|
||||
return (r1.origin != r2.origin || r1.direction != r2.direction);
|
||||
}
|
||||
|
||||
// ================== //
|
||||
// TRay functions //
|
||||
// ================== //
|
||||
|
||||
|
||||
/**
|
||||
* Gets the point of the ray at a given parameter
|
||||
*
|
||||
* @param(r1) Ray
|
||||
* @param(t) Parameter
|
||||
*
|
||||
* @return Point at t
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TVector3<T> PointAt(const TRay<T>& r1, T t)
|
||||
{
|
||||
return r1.origin + r1.direction * t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets parameter necessary to travel to query point on line.
|
||||
*
|
||||
* @param(r1) Ray
|
||||
* @param(p1) Query point
|
||||
*
|
||||
* @return parameter t
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
TVector3<T> GetParameter(const TRay<T>& r1, const TVector3<T>& p1)
|
||||
{
|
||||
return DotP((p1 - r1.origin), r1.direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests wether two ray point in the same direction (Not if origin).
|
||||
*
|
||||
* @param(r1) Ray one
|
||||
* @param(r2) Ray two
|
||||
*
|
||||
* @return True, if both rays point in the same direction, false if not.
|
||||
*/
|
||||
|
||||
template<RealType T>
|
||||
inline bool SameDirection(const TRay<T>& r1, const TRay<T>& r2)
|
||||
{
|
||||
return (r1.direction == r1.direction);
|
||||
}
|
||||
}
|
82
Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMD.h
Normal file
82
Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMD.h
Normal file
@@ -0,0 +1,82 @@
|
||||
// This file includes the necessary header for vectorization intrinsics. If no specifics are defined SSE4.2 is used.
|
||||
//
|
||||
// ARM is not supported.
|
||||
|
||||
#include "Core/public/Math/SIMD/Platform.h"
|
||||
|
||||
|
||||
|
||||
#include <nmmintrin.h> // SSE4.2
|
||||
|
||||
#ifdef __AVX__
|
||||
# include <immintrin.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace Phanes::Core::Math::SIMD
|
||||
{
|
||||
|
||||
// XMM Register wrapper for 4x1 floats
|
||||
|
||||
struct VectorRegister4f
|
||||
{
|
||||
public:
|
||||
__m128 data;
|
||||
};
|
||||
|
||||
typedef VectorRegister4f VectorRegister4f32;
|
||||
|
||||
|
||||
|
||||
// XMM Register wrapper for 2x1 doubles
|
||||
struct VectorRegister2d
|
||||
{
|
||||
public:
|
||||
__m128d data;
|
||||
};
|
||||
|
||||
typedef VectorRegister2d VectorRegister2f64;
|
||||
|
||||
|
||||
|
||||
// XMM Register wrapper for 4x1 integers
|
||||
struct VectorRegister4i
|
||||
{
|
||||
public:
|
||||
__m128i data;
|
||||
};
|
||||
|
||||
typedef VectorRegister4i VectorRegister4i32;
|
||||
|
||||
|
||||
|
||||
# ifdef __AVX__
|
||||
|
||||
// AVX specific types
|
||||
|
||||
// XMM Register wrapper for 4x1 doubles
|
||||
struct VectorRegister4d
|
||||
{
|
||||
public:
|
||||
__m256d data;
|
||||
};
|
||||
|
||||
typedef VectorRegister4d VectorRegister4f64;
|
||||
|
||||
# endif
|
||||
|
||||
|
||||
# ifdef __AVX2__
|
||||
|
||||
// AVX2 specific types
|
||||
|
||||
// XMM Register wrapper for 4x1 doubles
|
||||
struct VectorRegister4i64
|
||||
{
|
||||
public:
|
||||
__m256i data;
|
||||
};
|
||||
|
||||
|
||||
# endif
|
||||
}
|
227
Engine/Source/Runtime/Core/public/Math/SIMD/Platform.h
Normal file
227
Engine/Source/Runtime/Core/public/Math/SIMD/Platform.h
Normal file
@@ -0,0 +1,227 @@
|
||||
// Platform / Compiler detection.
|
||||
|
||||
#pragma once
|
||||
|
||||
// Set platform MACRO depending on defined build
|
||||
// User defines build platform
|
||||
#ifdef P_WIN_BUILD
|
||||
#define P_PLATFORM 0
|
||||
#elif P_LINUX_BUILD
|
||||
#define P_PLATFORM 1
|
||||
#elif P_APPLE_BUILD
|
||||
#define P_PLATFORM 2
|
||||
#elif P_PS5_BUILD
|
||||
#define P_PLATFORM 3
|
||||
#else
|
||||
#error Your target system is either not supported, or you have yet to define it.
|
||||
#endif
|
||||
|
||||
// Set compiler depending on defined compiler
|
||||
|
||||
// Compiler macro definition
|
||||
|
||||
// ID's defines like [0-9][0-x]
|
||||
// First bracket is compiler, second is the version of the compiler.
|
||||
|
||||
// Visual C++
|
||||
#define P_COMPILER_VC22 001
|
||||
#define P_COMPILER_VC19 002
|
||||
#define P_COMPILER_VC17 003
|
||||
#define P_COMPILER_VC15 004
|
||||
#define P_COMPILER_VC13 005
|
||||
#define P_COMPILER_VC12 006
|
||||
#define P_COMPILER_VC10 007
|
||||
#define P_COMPILER_VC08 008
|
||||
#define P_COMPILER_VC05 009
|
||||
#define P_COMPILER_VC03 010
|
||||
#define P_COMPILER_VC02 011
|
||||
#define P_COMPILER_VCSP 012
|
||||
|
||||
// Clang
|
||||
#define P_COMPILER_CLANG34 101
|
||||
#define P_COMPILER_CLANG35 102
|
||||
#define P_COMPILER_CLANG36 103
|
||||
#define P_COMPILER_CLANG37 104
|
||||
#define P_COMPILER_CLANG38 105
|
||||
#define P_COMPILER_CLANG39 106
|
||||
#define P_COMPILER_CLANG4 107
|
||||
#define P_COMPILER_CLANG5 108
|
||||
#define P_COMPILER_CLANG6 109
|
||||
#define P_COMPILER_CLANG7 110
|
||||
#define P_COMPILER_CLANG8 111
|
||||
#define P_COMPILER_CLANG9 112
|
||||
#define P_COMPILER_CLANG10 113
|
||||
#define P_COMPILER_CLANG11 114
|
||||
#define P_COMPILER_CLANG12 115
|
||||
#define P_COMPILER_CLANG13 116
|
||||
#define P_COMPILER_CLANG14 117
|
||||
#define P_COMPILER_CLANG15 118
|
||||
#define P_COMPILER_CLANG16 119
|
||||
#define P_COMPILER_CLANG17 120
|
||||
#define P_COMPILER_CLANG18 121
|
||||
#define P_COMPILER_CLANG19 122
|
||||
|
||||
|
||||
// G++
|
||||
#define P_COMPILER_GCC46 201
|
||||
#define P_COMPILER_GCC47 202
|
||||
#define P_COMPILER_GCC48 203
|
||||
#define P_COMPILER_GCC49 204
|
||||
#define P_COMPILER_GCC5 205
|
||||
#define P_COMPILER_GCC6 206
|
||||
#define P_COMPILER_GCC61 207
|
||||
#define P_COMPILER_GCC7 208
|
||||
#define P_COMPILER_GCC8 209
|
||||
#define P_COMPILER_GCC9 210
|
||||
#define P_COMPILER_GCC10 211
|
||||
#define P_COMPILER_GCC11 212
|
||||
#define P_COMPILER_GCC12 213
|
||||
#define P_COMPILER_GCC13 214
|
||||
#define P_COMPILER_GCC14 215
|
||||
|
||||
|
||||
// Intel C++
|
||||
#define P_COMPILER_INTEL14 301
|
||||
#define P_COMPILER_INTEL15 302
|
||||
#define P_COMPILER_INTEL16 303
|
||||
#define P_COMPILER_INTEL17 304
|
||||
#define P_COMPILER_INTEL18 305
|
||||
#define P_COMPILER_INTEL19 306
|
||||
#define P_COMPILER_INTEL21 307
|
||||
|
||||
// Visual studio
|
||||
#ifdef _MSC_VER
|
||||
# if _MSC_VER >= 1930
|
||||
# define P_COMPILER P_COMPILER_VC22
|
||||
# elif _MSC_VER >= 1920
|
||||
# define P_COMPILER P_COMPILER_VC19
|
||||
# elif _MSC_VER >= 1910
|
||||
# define P_COMPILER P_COMPILER_VC17
|
||||
# elif _MSC_VER >= 1900
|
||||
# define P_COMPILER P_COMPILER_VC15
|
||||
# elif _MSC_VER >= 1800
|
||||
# define P_COMPILER P_COMPILER_VC13
|
||||
# elif _MSC_VER >= 1700
|
||||
# define P_COMPILER P_COMPILER_VC12
|
||||
# elif _MSC_VER >= 1600
|
||||
# define P_COMPILER P_COMPILER_VC10
|
||||
# elif _MSC_VER >= 1500
|
||||
# define P_COMPILER P_COMPILER_VC08
|
||||
# elif _MSC_VER >= 1400
|
||||
# define P_COMPILER P_COMPILER_VC05
|
||||
# elif _MSC_VER >= 1310
|
||||
# define P_COMPILER P_COMPILER_VC03
|
||||
# elif _MSC_VER >= 1300
|
||||
# define P_COMPILER P_COMPILER_VC02
|
||||
# elif _MSC_VER >= 1200
|
||||
# define P_COMPILER P_COMPILER_VCSP
|
||||
# endif
|
||||
|
||||
|
||||
|
||||
// Clang
|
||||
|
||||
#elif (defined(__clang__))
|
||||
|
||||
# if defined(__apple_build_version__)
|
||||
#
|
||||
# if (__clang_major__ < 6)
|
||||
# error "GLM requires Clang 3.4 / Apple Clang 6.0 or higher"
|
||||
# elif __clang_major__ == 6 && __clang_minor__ == 0
|
||||
# define P_COMPILER P_COMPILER_CLANG35
|
||||
# elif __clang_major__ == 6 && __clang_minor__ >= 1
|
||||
# define P_COMPILER P_COMPILER_CLANG36
|
||||
# elif __clang_major__ >= 7
|
||||
# define P_COMPILER P_COMPILER_CLANG37
|
||||
# endif
|
||||
# else
|
||||
# if ((__clang_major__ == 3) && (__clang_minor__ < 4)) || (__clang_major__ < 3)
|
||||
# error "GLM requires Clang 3.4 or higher"
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 4
|
||||
# define P_COMPILER P_COMPILER_CLANG34
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 5
|
||||
# define P_COMPILER P_COMPILER_CLANG35
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 6
|
||||
# define P_COMPILER P_COMPILER_CLANG36
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 7
|
||||
# define P_COMPILER P_COMPILER_CLANG37
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 8
|
||||
# define P_COMPILER P_COMPILER_CLANG38
|
||||
# elif __clang_major__ == 3 && __clang_minor__ >= 9
|
||||
# define P_COMPILER P_COMPILER_CLANG39
|
||||
# elif __clang_major__ == 4 && __clang_minor__ == 0
|
||||
# define P_COMPILER P_COMPILER_CLANG4
|
||||
# elif __clang_major__ == 5
|
||||
# define P_COMPILER P_COMPILER_CLANG5
|
||||
# elif __clang_major__ == 6
|
||||
# define P_COMPILER P_COMPILER_CLANG6
|
||||
# elif __clang_major__ == 7
|
||||
# define P_COMPILER P_COMPILER_CLANG7
|
||||
# elif __clang_major__ == 8
|
||||
# define P_COMPILER P_COMPILER_CLANG8
|
||||
# elif __clang_major__ == 9
|
||||
# define P_COMPILER P_COMPILER_CLANG9
|
||||
# elif __clang_major__ == 10
|
||||
# define P_COMPILER P_COMPILER_CLANG10
|
||||
# elif __clang_major__ == 11
|
||||
# define P_COMPILER P_COMPILER_CLANG11
|
||||
# elif __clang_major__ == 12
|
||||
# define P_COMPILER P_COMPILER_CLANG12
|
||||
# elif __clang_major__ == 13
|
||||
# define P_COMPILER P_COMPILER_CLANG13
|
||||
# elif __clang_major__ == 14
|
||||
# define P_COMPILER P_COMPILER_CLANG14
|
||||
# elif __clang_major__ == 15
|
||||
# define P_COMPILER P_COMPILER_CLANG15
|
||||
# elif __clang_major__ == 16
|
||||
# define P_COMPILER P_COMPILER_CLANG16
|
||||
# elif __clang_major__ == 17
|
||||
# define P_COMPILER P_COMPILER_CLANG17
|
||||
# elif __clang_major__ == 18
|
||||
# define P_COMPILER P_COMPILER_CLANG18
|
||||
# elif __clang_major__ >= 19
|
||||
# define P_COMPILER P_COMPILER_CLANG19
|
||||
# endif
|
||||
# endif
|
||||
|
||||
|
||||
|
||||
// G++
|
||||
#elif defined(__GNUC__) || defined(__MINGW32__)
|
||||
# if __GNUC__ >= 14
|
||||
# define P_COMPILER P_COMPILER_GCC14
|
||||
# elif __GNUC__ >= 13
|
||||
# define P_COMPILER P_COMPILER_GCC13
|
||||
# elif __GNUC__ >= 12
|
||||
# define P_COMPILER P_COMPILER_GCC12
|
||||
# elif __GNUC__ >= 11
|
||||
# define P_COMPILER P_COMPILER_GCC11
|
||||
# elif __GNUC__ >= 10
|
||||
# define P_COMPILER P_COMPILER_GCC10
|
||||
# elif __GNUC__ >= 9
|
||||
# define P_COMPILER P_COMPILER_GCC9
|
||||
# elif __GNUC__ >= 8
|
||||
# define P_COMPILER P_COMPILER_GCC8
|
||||
# elif __GNUC__ >= 7
|
||||
# define P_COMPILER P_COMPILER_GCC7
|
||||
# elif __GNUC__ >= 6
|
||||
# define P_COMPILER P_COMPILER_GCC6
|
||||
# elif __GNUC__ >= 5
|
||||
# define P_COMPILER P_COMPILER_GCC5
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
|
||||
# define P_COMPILER P_COMPILER_GCC49
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 8
|
||||
# define P_COMPILER P_COMPILER_GCC48
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 7
|
||||
# define P_COMPILER P_COMPILER_GCC47
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 6
|
||||
# define P_COMPILER P_COMPILER_GCC46
|
||||
# elif ((__GNUC__ == 4) && (__GNUC_MINOR__ < 6)) || (__GNUC__ < 4)
|
||||
# error PhanesEngine does not support your compiler.
|
||||
# endif
|
||||
|
||||
#elif defined(__CUDACC__)
|
||||
# error CUDA C++ is not supported by PhanesEngine
|
||||
|
||||
#endif
|
||||
|
1162
Engine/Source/Runtime/Core/public/Math/Vector2.hpp
Normal file
1162
Engine/Source/Runtime/Core/public/Math/Vector2.hpp
Normal file
File diff suppressed because it is too large
Load Diff
1439
Engine/Source/Runtime/Core/public/Math/Vector3.hpp
Normal file
1439
Engine/Source/Runtime/Core/public/Math/Vector3.hpp
Normal file
File diff suppressed because it is too large
Load Diff
11
Engine/Source/Runtime/Core/public/Misc/Boilerplate.h
Normal file
11
Engine/Source/Runtime/Core/public/Misc/Boilerplate.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "PhanesEnginePCH.h"
|
||||
|
||||
// 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>;
|
227
Engine/Source/Runtime/Core/public/OSAL/Platform.h
Normal file
227
Engine/Source/Runtime/Core/public/OSAL/Platform.h
Normal file
@@ -0,0 +1,227 @@
|
||||
// Platform / Compiler detection.
|
||||
|
||||
#pragma once
|
||||
|
||||
// Set platform MACRO depending on defined build
|
||||
// User defines build platform
|
||||
#ifdef P_WIN_BUILD
|
||||
#define P_PLATFORM 0
|
||||
#elif P_LINUX_BUILD
|
||||
#define P_PLATFORM 1
|
||||
#elif P_APPLE_BUILD
|
||||
#define P_PLATFORM 2
|
||||
#elif P_PS5_BUILD
|
||||
#define P_PLATFORM 3
|
||||
#else
|
||||
#error Your target system is either not supported, or you have yet to define it.
|
||||
#endif
|
||||
|
||||
// Set compiler depending on defined compiler
|
||||
|
||||
// Compiler macro definition
|
||||
|
||||
// ID's defines like [0-9][0-x]
|
||||
// First bracket is compiler, second is the version of the compiler.
|
||||
|
||||
// Visual C++
|
||||
#define P_COMPILER_VC22 001
|
||||
#define P_COMPILER_VC19 002
|
||||
#define P_COMPILER_VC17 003
|
||||
#define P_COMPILER_VC15 004
|
||||
#define P_COMPILER_VC13 005
|
||||
#define P_COMPILER_VC12 006
|
||||
#define P_COMPILER_VC10 007
|
||||
#define P_COMPILER_VC08 008
|
||||
#define P_COMPILER_VC05 009
|
||||
#define P_COMPILER_VC03 010
|
||||
#define P_COMPILER_VC02 011
|
||||
#define P_COMPILER_VCSP 012
|
||||
|
||||
// Clang
|
||||
#define P_COMPILER_CLANG34 101
|
||||
#define P_COMPILER_CLANG35 102
|
||||
#define P_COMPILER_CLANG36 103
|
||||
#define P_COMPILER_CLANG37 104
|
||||
#define P_COMPILER_CLANG38 105
|
||||
#define P_COMPILER_CLANG39 106
|
||||
#define P_COMPILER_CLANG4 107
|
||||
#define P_COMPILER_CLANG5 108
|
||||
#define P_COMPILER_CLANG6 109
|
||||
#define P_COMPILER_CLANG7 110
|
||||
#define P_COMPILER_CLANG8 111
|
||||
#define P_COMPILER_CLANG9 112
|
||||
#define P_COMPILER_CLANG10 113
|
||||
#define P_COMPILER_CLANG11 114
|
||||
#define P_COMPILER_CLANG12 115
|
||||
#define P_COMPILER_CLANG13 116
|
||||
#define P_COMPILER_CLANG14 117
|
||||
#define P_COMPILER_CLANG15 118
|
||||
#define P_COMPILER_CLANG16 119
|
||||
#define P_COMPILER_CLANG17 120
|
||||
#define P_COMPILER_CLANG18 121
|
||||
#define P_COMPILER_CLANG19 122
|
||||
|
||||
|
||||
// G++
|
||||
#define P_COMPILER_GCC46 201
|
||||
#define P_COMPILER_GCC47 202
|
||||
#define P_COMPILER_GCC48 203
|
||||
#define P_COMPILER_GCC49 204
|
||||
#define P_COMPILER_GCC5 205
|
||||
#define P_COMPILER_GCC6 206
|
||||
#define P_COMPILER_GCC61 207
|
||||
#define P_COMPILER_GCC7 208
|
||||
#define P_COMPILER_GCC8 209
|
||||
#define P_COMPILER_GCC9 210
|
||||
#define P_COMPILER_GCC10 211
|
||||
#define P_COMPILER_GCC11 212
|
||||
#define P_COMPILER_GCC12 213
|
||||
#define P_COMPILER_GCC13 214
|
||||
#define P_COMPILER_GCC14 215
|
||||
|
||||
|
||||
// Intel C++
|
||||
#define P_COMPILER_INTEL14 301
|
||||
#define P_COMPILER_INTEL15 302
|
||||
#define P_COMPILER_INTEL16 303
|
||||
#define P_COMPILER_INTEL17 304
|
||||
#define P_COMPILER_INTEL18 305
|
||||
#define P_COMPILER_INTEL19 306
|
||||
#define P_COMPILER_INTEL21 307
|
||||
|
||||
// Visual studio
|
||||
#ifdef _MSC_VER
|
||||
# if _MSC_VER >= 1930
|
||||
# define P_COMPILER P_COMPILER_VC22
|
||||
# elif _MSC_VER >= 1920
|
||||
# define P_COMPILER P_COMPILER_VC19
|
||||
# elif _MSC_VER >= 1910
|
||||
# define P_COMPILER P_COMPILER_VC17
|
||||
# elif _MSC_VER >= 1900
|
||||
# define P_COMPILER P_COMPILER_VC15
|
||||
# elif _MSC_VER >= 1800
|
||||
# define P_COMPILER P_COMPILER_VC13
|
||||
# elif _MSC_VER >= 1700
|
||||
# define P_COMPILER P_COMPILER_VC12
|
||||
# elif _MSC_VER >= 1600
|
||||
# define P_COMPILER P_COMPILER_VC10
|
||||
# elif _MSC_VER >= 1500
|
||||
# define P_COMPILER P_COMPILER_VC08
|
||||
# elif _MSC_VER >= 1400
|
||||
# define P_COMPILER P_COMPILER_VC05
|
||||
# elif _MSC_VER >= 1310
|
||||
# define P_COMPILER P_COMPILER_VC03
|
||||
# elif _MSC_VER >= 1300
|
||||
# define P_COMPILER P_COMPILER_VC02
|
||||
# elif _MSC_VER >= 1200
|
||||
# define P_COMPILER P_COMPILER_VCSP
|
||||
# endif
|
||||
|
||||
|
||||
|
||||
// Clang
|
||||
|
||||
#elif (defined(__clang__))
|
||||
|
||||
# if defined(__apple_build_version__)
|
||||
#
|
||||
# if (__clang_major__ < 6)
|
||||
# error "GLM requires Clang 3.4 / Apple Clang 6.0 or higher"
|
||||
# elif __clang_major__ == 6 && __clang_minor__ == 0
|
||||
# define P_COMPILER P_COMPILER_CLANG35
|
||||
# elif __clang_major__ == 6 && __clang_minor__ >= 1
|
||||
# define P_COMPILER P_COMPILER_CLANG36
|
||||
# elif __clang_major__ >= 7
|
||||
# define P_COMPILER P_COMPILER_CLANG37
|
||||
# endif
|
||||
# else
|
||||
# if ((__clang_major__ == 3) && (__clang_minor__ < 4)) || (__clang_major__ < 3)
|
||||
# error "GLM requires Clang 3.4 or higher"
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 4
|
||||
# define P_COMPILER P_COMPILER_CLANG34
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 5
|
||||
# define P_COMPILER P_COMPILER_CLANG35
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 6
|
||||
# define P_COMPILER P_COMPILER_CLANG36
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 7
|
||||
# define P_COMPILER P_COMPILER_CLANG37
|
||||
# elif __clang_major__ == 3 && __clang_minor__ == 8
|
||||
# define P_COMPILER P_COMPILER_CLANG38
|
||||
# elif __clang_major__ == 3 && __clang_minor__ >= 9
|
||||
# define P_COMPILER P_COMPILER_CLANG39
|
||||
# elif __clang_major__ == 4 && __clang_minor__ == 0
|
||||
# define P_COMPILER P_COMPILER_CLANG4
|
||||
# elif __clang_major__ == 5
|
||||
# define P_COMPILER P_COMPILER_CLANG5
|
||||
# elif __clang_major__ == 6
|
||||
# define P_COMPILER P_COMPILER_CLANG6
|
||||
# elif __clang_major__ == 7
|
||||
# define P_COMPILER P_COMPILER_CLANG7
|
||||
# elif __clang_major__ == 8
|
||||
# define P_COMPILER P_COMPILER_CLANG8
|
||||
# elif __clang_major__ == 9
|
||||
# define P_COMPILER P_COMPILER_CLANG9
|
||||
# elif __clang_major__ == 10
|
||||
# define P_COMPILER P_COMPILER_CLANG10
|
||||
# elif __clang_major__ == 11
|
||||
# define P_COMPILER P_COMPILER_CLANG11
|
||||
# elif __clang_major__ == 12
|
||||
# define P_COMPILER P_COMPILER_CLANG12
|
||||
# elif __clang_major__ == 13
|
||||
# define P_COMPILER P_COMPILER_CLANG13
|
||||
# elif __clang_major__ == 14
|
||||
# define P_COMPILER P_COMPILER_CLANG14
|
||||
# elif __clang_major__ == 15
|
||||
# define P_COMPILER P_COMPILER_CLANG15
|
||||
# elif __clang_major__ == 16
|
||||
# define P_COMPILER P_COMPILER_CLANG16
|
||||
# elif __clang_major__ == 17
|
||||
# define P_COMPILER P_COMPILER_CLANG17
|
||||
# elif __clang_major__ == 18
|
||||
# define P_COMPILER P_COMPILER_CLANG18
|
||||
# elif __clang_major__ >= 19
|
||||
# define P_COMPILER P_COMPILER_CLANG19
|
||||
# endif
|
||||
# endif
|
||||
|
||||
|
||||
|
||||
// G++
|
||||
#elif defined(__GNUC__) || defined(__MINGW32__)
|
||||
# if __GNUC__ >= 14
|
||||
# define P_COMPILER P_COMPILER_GCC14
|
||||
# elif __GNUC__ >= 13
|
||||
# define P_COMPILER P_COMPILER_GCC13
|
||||
# elif __GNUC__ >= 12
|
||||
# define P_COMPILER P_COMPILER_GCC12
|
||||
# elif __GNUC__ >= 11
|
||||
# define P_COMPILER P_COMPILER_GCC11
|
||||
# elif __GNUC__ >= 10
|
||||
# define P_COMPILER P_COMPILER_GCC10
|
||||
# elif __GNUC__ >= 9
|
||||
# define P_COMPILER P_COMPILER_GCC9
|
||||
# elif __GNUC__ >= 8
|
||||
# define P_COMPILER P_COMPILER_GCC8
|
||||
# elif __GNUC__ >= 7
|
||||
# define P_COMPILER P_COMPILER_GCC7
|
||||
# elif __GNUC__ >= 6
|
||||
# define P_COMPILER P_COMPILER_GCC6
|
||||
# elif __GNUC__ >= 5
|
||||
# define P_COMPILER P_COMPILER_GCC5
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
|
||||
# define P_COMPILER P_COMPILER_GCC49
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 8
|
||||
# define P_COMPILER P_COMPILER_GCC48
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 7
|
||||
# define P_COMPILER P_COMPILER_GCC47
|
||||
# elif __GNUC__ == 4 && __GNUC_MINOR__ >= 6
|
||||
# define P_COMPILER P_COMPILER_GCC46
|
||||
# elif ((__GNUC__ == 4) && (__GNUC_MINOR__ < 6)) || (__GNUC__ < 4)
|
||||
# error PhanesEngine does not support your compiler.
|
||||
# endif
|
||||
|
||||
#elif defined(__CUDACC__)
|
||||
# error CUDA C++ is not supported by PhanesEngine
|
||||
|
||||
#endif
|
||||
|
112
Engine/Source/Runtime/Core/public/OSAL/PlatformTypes.h
Normal file
112
Engine/Source/Runtime/Core/public/OSAL/PlatformTypes.h
Normal file
@@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
|
||||
#include "PhanesEnginePCH.h"
|
||||
|
||||
// ============================================= //
|
||||
// Turn os specific types into global types. //
|
||||
// ============================================= //
|
||||
|
||||
|
||||
// TODO: include int128
|
||||
|
||||
namespace Phanes::Core::Types
|
||||
{
|
||||
|
||||
#ifdef P_WIN_BUILD
|
||||
|
||||
// MSCV++ specific types
|
||||
|
||||
typedef FLOAT128 float128;
|
||||
|
||||
//#elif P_UNIX_BUILD
|
||||
//
|
||||
// // GCC specific types
|
||||
//
|
||||
// typedef __float128 float128;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
// Specific types size
|
||||
//
|
||||
// 8-Bit integer
|
||||
typedef int8_t int8;
|
||||
|
||||
// 16-Bit integer
|
||||
typedef int16_t int16;
|
||||
|
||||
// 32-Bit integer
|
||||
typedef int32_t int32;
|
||||
|
||||
// 64-Bit integer
|
||||
typedef int64_t int64;
|
||||
|
||||
// 8-Bit unsigned integer
|
||||
typedef uint8_t uint8;
|
||||
|
||||
// 16-Bit unsigned integer
|
||||
typedef uint16_t uint16;
|
||||
|
||||
// 32-Bit unsigned integer
|
||||
typedef uint32_t uint32;
|
||||
|
||||
// 64-Bit unsigned integer
|
||||
typedef uint64_t uint64;
|
||||
|
||||
|
||||
|
||||
// At least N bit types
|
||||
//
|
||||
// At least 8-Bit integer
|
||||
typedef int_least8_t lint8;
|
||||
|
||||
// At least 16-Bit integer
|
||||
typedef int_least16_t lint16;
|
||||
|
||||
// At least 32-Bit integer
|
||||
typedef int_least32_t lint32;
|
||||
|
||||
// At least 64-Bit integer
|
||||
typedef int_least64_t lint64;
|
||||
|
||||
// At least 8-Bit integer
|
||||
typedef uint_least8_t ulint8;
|
||||
|
||||
// At least 16-Bit integer
|
||||
typedef uint_least16_t ulint16;
|
||||
|
||||
// At least 32-Bit integer
|
||||
typedef uint_least32_t ulint32;
|
||||
|
||||
// At least 64-Bit integer
|
||||
typedef uint_least64_t ulint64;
|
||||
|
||||
|
||||
|
||||
// Fast N bit types
|
||||
//
|
||||
// Fast 8-bit integer
|
||||
typedef int_fast8_t fint8;
|
||||
|
||||
// At least 16-Bit integer
|
||||
typedef int_fast16_t fint16;
|
||||
|
||||
// At least 32-Bit integer
|
||||
typedef int_fast32_t fint32;
|
||||
|
||||
// At least 64-Bit integer
|
||||
typedef int_fast64_t fint64;
|
||||
|
||||
// At least 8-Bit integer
|
||||
typedef uint_fast8_t ufint8;
|
||||
|
||||
// At least 16-Bit integer
|
||||
typedef uint_fast16_t ufint16;
|
||||
|
||||
// At least 32-Bit integer
|
||||
typedef uint_fast32_t ufint32;
|
||||
|
||||
// At least 64-Bit integer
|
||||
typedef uint_fast64_t ufint64;
|
||||
|
||||
}
|
14
Engine/Source/Runtime/Core/public/OSAL/README.md
Normal file
14
Engine/Source/Runtime/Core/public/OSAL/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# PhanesCore
|
||||
## Operating System Abstraction Layer (OSAL)
|
||||
|
||||
### Descritpion
|
||||
|
||||
Contains abstraction necessary for cross platform compatability. Including but not limited to specific datatypes.
|
||||
|
||||
### Note
|
||||
|
||||
- The PhanesEngine will have no console support in the near future.
|
||||
|
||||
- Testing will only happen for Windows. GCC compatability will be partitally included, but by far not fully. Though support is planed for future versions.
|
||||
|
||||
- Minimum SSE4.1 is required to compile the engine.
|
26
Engine/Source/Runtime/Core/public/StartingPoint/EntryPoint.h
Normal file
26
Engine/Source/Runtime/Core/public/StartingPoint/EntryPoint.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
// Entry point for Phanes game
|
||||
|
||||
#if defined(P_WIN_BUILD)
|
||||
|
||||
extern Phanes::Core::Application::PhanesProject* Phanes::Core::Application::CreatePhanesGame();
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Phanes::Core::Logging::Init();
|
||||
PENGINE_LOG_INFO("Logger initialized!");
|
||||
PENGINE_LOG_INFO("Welcome to PhanesEngine!");
|
||||
|
||||
|
||||
auto phanes_game = Phanes::Core::Application::CreatePhanesGame();
|
||||
|
||||
PENGINE_LOG_INFO("Loading project {0}...", phanes_game->GetName());
|
||||
|
||||
phanes_game->Run();
|
||||
|
||||
delete phanes_game;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "PhanesEnginePCH.h"
|
||||
|
||||
#include "Core/Core.h"
|
||||
|
||||
// Entrypoint class for any Phanes game.
|
||||
|
||||
namespace Phanes::Core::Application
|
||||
{
|
||||
class PHANES_CORE PhanesProject
|
||||
{
|
||||
|
||||
private:
|
||||
|
||||
std::string projectName;
|
||||
|
||||
public:
|
||||
|
||||
PhanesProject(std::string _ProjectName);
|
||||
virtual ~PhanesProject();
|
||||
|
||||
/**
|
||||
* PhanesEngine main loop.
|
||||
*/
|
||||
void Run();
|
||||
|
||||
/**
|
||||
* Getter for project name;
|
||||
*/
|
||||
|
||||
FORCEINLINE std::string GetName();
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Function to be overwriten by client.
|
||||
*/
|
||||
|
||||
PhanesProject* CreatePhanesGame();
|
||||
}
|
Reference in New Issue
Block a user