From 11ae45b6f25e4de1b228f5c6069bc4a390cab429 Mon Sep 17 00:00:00 2001 From: scorpioblood <77296181+scorpioblood@users.noreply.github.com> Date: Mon, 27 May 2024 22:03:10 +0200 Subject: [PATCH] Several updates. --- Engine/Source/Runtime/Core/Core.h | 5 - .../Runtime/Core/private/Math/Matrix4.cpp | 347 --------- .../Runtime/Core/private/Math/Vector4.cpp | 388 ---------- .../Runtime/Core/public/Math/Boilerplate.h | 4 +- .../Runtime/Core/public/Math/MathCommon.hpp | 32 + .../Source/Runtime/Core/public/Math/MathFwd.h | 42 +- .../Core/public/Math/SIMD/PhanesSIMD.h | 93 --- .../Runtime/Core/public/Math/Vector3.hpp | 16 +- .../Runtime/Core/public/Math/Vector4.hpp | 705 ++++++++++++++++++ .../Runtime/Core/public/Math/Vector4.inl | 177 +++++ Engine/Source/Runtime/Phanes.h | 3 +- Tests/TestProject/Main.cpp | 16 +- 12 files changed, 951 insertions(+), 877 deletions(-) delete mode 100644 Engine/Source/Runtime/Core/private/Math/Matrix4.cpp delete mode 100644 Engine/Source/Runtime/Core/private/Math/Vector4.cpp delete mode 100644 Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMD.h create mode 100644 Engine/Source/Runtime/Core/public/Math/Vector4.hpp create mode 100644 Engine/Source/Runtime/Core/public/Math/Vector4.inl diff --git a/Engine/Source/Runtime/Core/Core.h b/Engine/Source/Runtime/Core/Core.h index 4838b68..4425cf0 100644 --- a/Engine/Source/Runtime/Core/Core.h +++ b/Engine/Source/Runtime/Core/Core.h @@ -66,9 +66,4 @@ namespace Phanes return std::make_unique(std::forward(args)...); } -} - -int test() -{ - } \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/private/Math/Matrix4.cpp b/Engine/Source/Runtime/Core/private/Math/Matrix4.cpp deleted file mode 100644 index 3769eb2..0000000 --- a/Engine/Source/Runtime/Core/private/Math/Matrix4.cpp +++ /dev/null @@ -1,347 +0,0 @@ -// ========================== // -// 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(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(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(a[0]); - Vector3 v1 = reinterpret_cast(a[1]); - Vector3 v2 = reinterpret_cast(a[2]); - Vector3 v3 = reinterpret_cast(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(a[0]); - Vector3 v1 = reinterpret_cast(a[1]); - Vector3 v2 = reinterpret_cast(a[2]); - Vector3 v3 = reinterpret_cast(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(a[0]); - Vector3 v1 = reinterpret_cast(a[1]); - Vector3 v2 = reinterpret_cast(a[2]); - Vector3 v3 = reinterpret_cast(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; -} - diff --git a/Engine/Source/Runtime/Core/private/Math/Vector4.cpp b/Engine/Source/Runtime/Core/private/Math/Vector4.cpp deleted file mode 100644 index 884d3fe..0000000 --- a/Engine/Source/Runtime/Core/private/Math/Vector4.cpp +++ /dev/null @@ -1,388 +0,0 @@ -// ========================== // -// 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); -} \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/Boilerplate.h b/Engine/Source/Runtime/Core/public/Math/Boilerplate.h index 74621e1..e00696a 100644 --- a/Engine/Source/Runtime/Core/public/Math/Boilerplate.h +++ b/Engine/Source/Runtime/Core/public/Math/Boilerplate.h @@ -61,7 +61,9 @@ namespace Phanes::Core::Math template concept IntType = std::is_integral_v; - + // Typenames with Arithmethic constrain have to be number. + template + concept Arithmethic = std::is_arithmetic_v; diff --git a/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp b/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp index b53b6ac..d61a1db 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp +++ b/Engine/Source/Runtime/Core/public/Math/MathCommon.hpp @@ -124,6 +124,38 @@ namespace Phanes::Core::Math { } + + template + FORCEINLINE T Abs(T s) + { + return abs(s); + } + + template<> + FORCEINLINE float Abs(float s) + { + return fabs(s); + }; + + template<> + FORCEINLINE long long Abs(long long s) + { + return llabs(s); + }; + + template<> + FORCEINLINE long Abs(long s) + { + return labs(s); + }; + + template<> + FORCEINLINE double Abs(double s) + { + return fabsl(s); + }; + + } // phanes #endif // !MATH_COMMON_H \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/MathFwd.h b/Engine/Source/Runtime/Core/public/Math/MathFwd.h index d22d332..1025fc1 100644 --- a/Engine/Source/Runtime/Core/public/Math/MathFwd.h +++ b/Engine/Source/Runtime/Core/public/Math/MathFwd.h @@ -24,27 +24,27 @@ namespace Phanes::Core::Math { * Template forward declarations. */ - template struct TColor; - template struct TLinearColor; - template struct TVector2; - template struct TVector3; - template struct TRay; - template struct TLine; - template struct TPlane; - template struct TMatrix2; - template struct TMatrix3; - template struct TMatrix4; - template struct TQuaternion; - template struct TTransform; - template struct TPoint2; - template struct TPoint3; - template struct TPoint4; - template struct TIntVector2; - template struct TIntVector3; - template struct TIntVector4; - template struct TIntPoint2; - template struct TIntPoint3; - template struct TIntPoint4; + template struct TColor; + template struct TLinearColor; + template struct TVector2; + template struct TVector3; + template struct TRay; + template struct TLine; + template struct TPlane; + template struct TMatrix2; + template struct TMatrix3; + template struct TMatrix4; + template struct TQuaternion; + template struct TTransform; + template struct TPoint2; + template struct TPoint3; + template struct TPoint4; + template struct TIntVector2; + template struct TIntVector3; + template struct TIntVector4; + template struct TIntPoint2; + template struct TIntPoint3; + template struct TIntPoint4; template struct TVector4; /** diff --git a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMD.h b/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMD.h deleted file mode 100644 index 92c7812..0000000 --- a/Engine/Source/Runtime/Core/public/Math/SIMD/PhanesSIMD.h +++ /dev/null @@ -1,93 +0,0 @@ -// 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 "Core/public/Math/MathTypes.h" - - -#if P_INTRINSICS == P_INTRINSICS_AVX2 -# include "PhanesVectorMathAVX2.hpp" -#elif P_INTRINSICS == P_INTRINSICS_AVX -# include "PhanesVectorMathAVX.hpp" -#elif P_INTRINSICS == P_INTRINSICS_SSE -# include "PhanesVectorMathSSE.hpp" -#elif P_INTRINSICS == P_INTRINSICS_NEON -# include "PhanesVectorMathNeon.hpp" -#elif P_INTRINSICS == P_INTRINSICS_FPU -# include "PhanesVectorMathFPU.hpp" -#endif - - -// Register aliases -namespace Phanes::Core::Types -{ - -#if P_INTRINSICS >= 1 - - typedef __m128 Vec4f32Reg; - typedef __m128d Vec2f64Reg; - - typedef __m128i Vec4i32Reg; - typedef __m128i Vec2i64Reg; - - typedef __m128i Vec4u32Reg; - typedef __m128i Vec2u64Reg; - -#elif P_INTRINSICS != P_INTRINSICS_NEON - - typedef struct alignas(16) Vec4f32Reg { float data[4]; } Vec4f32Reg; - typedef struct alignas(16) Vec2f64Reg { double data[2]; } Vec2f64Reg; - typedef struct alignas(16) Vec4i32Reg { int data[4]; } Vec4i32Reg; - typedef struct alignas(16) Vec2i64Reg { Phanes::Core::Types::int64 data[2]; } Vec2i64Reg; - typedef struct alignas(16) Vec4u32Reg { unsigned int data[4]; } Vec4u32Reg; - typedef struct alignas(16) Vec2u64Reg { Phanes::Core::Types::uint64 data[4]; } Vec2u64Reg; - -#endif - - -#if P_INTRINSICS >= 2 - - typedef __m256 Vec4x2f32Reg; - typedef __m256 Vec8f32Reg; - typedef __m256d Vec2x2f64Reg; - typedef __m256d Vec4f64Reg; - -#elif P_INTRINSICS != P_INTRINSICS_NEON - - typedef struct alignas(32) Vec4x2f32Reg { float data[8]; } Vec4x2f32Reg; - typedef struct alignas(32) Vec8f32Reg { float data[8]; } Vec8f32Reg; - typedef struct alignas(32) Vec2x2f64Reg { double data[4]; } Vec2x2f64Reg; - typedef struct alignas(32) Vec4f64Reg { double data[4]; } Vec4f64Reg; - -#endif - - -#if P_INTRINSICS == 3 - - typedef __m256i Vec4x2i32Reg; - typedef __m256i Vec8i32Reg; - typedef __m256i Vec2x2i64Reg; - typedef __m256i Vec4i64Reg; - - typedef __m256i Vec4x2u32Reg; - typedef __m256i Vec8u32Reg; - typedef __m256i Vec2x2u64Reg; - typedef __m256i Vec4u64Reg; - -#elif P_INTRINSICS != P_INTRINSICS_NEON - - typedef struct alignas(32) Vec4x2i32Reg { int data[8]; } Vec4x2i32Reg; - typedef struct alignas(32) Vec8i32Reg { int data[8]; } Vec8i32Reg; - typedef struct alignas(32) Vec2x2i64Reg { Phanes::Core::Types::int64 data[4]; } Vec2x2i64Reg; - typedef struct alignas(32) Vec4i64Reg { Phanes::Core::Types::int64 data[4]; } Vec4i64Reg; - - typedef struct alignas(32) Vec4x2u32Reg { unsigned int data[8]; } Vec4x2u32Reg; - typedef struct alignas(32) Vec8u32Reg { unsigned int data[8]; } Vec8u32Reg; - typedef struct alignas(32) Vec2x2u64Reg { Phanes::Core::Types::uint64 data[4]; } Vec2x2u64Reg; - typedef struct alignas(32) Vec4u64Reg { Phanes::Core::Types::uint64 data[4]; } Vec4u64Reg; - -#endif - - // NEON ... -} \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/Vector3.hpp b/Engine/Source/Runtime/Core/public/Math/Vector3.hpp index b50d64b..679eb36 100644 --- a/Engine/Source/Runtime/Core/public/Math/Vector3.hpp +++ b/Engine/Source/Runtime/Core/public/Math/Vector3.hpp @@ -8,6 +8,7 @@ #include "Core/public/Math/MathCommon.hpp" #include "Core/public/Math/MathAbstractTypes.h" #include "Core/public/Math/MathFwd.h" +#include "Core/public/Math/SIMD/Storage.h" #ifndef P_DEBUG #pragma warning(disable : 4244) @@ -884,21 +885,6 @@ namespace Phanes::Core::Math { return v1; }; - /** - * Reflect by plane - * - * @param(v1) Vector to mirror - * @param(plane) Plane to mirror on - * - * @note result is stored in v1. - */ - - template - FORCEINLINE TVector3 ReflectFromPlaneV(TVector3& v1, const TVector3& normal) - { - return ReflectV(v1, normal); - } - /** * Rotates vector around axis * diff --git a/Engine/Source/Runtime/Core/public/Math/Vector4.hpp b/Engine/Source/Runtime/Core/public/Math/Vector4.hpp new file mode 100644 index 0000000..63bcd2f --- /dev/null +++ b/Engine/Source/Runtime/Core/public/Math/Vector4.hpp @@ -0,0 +1,705 @@ +#pragma once + + +#include "Core/public/Math/Boilerplate.h" +#include "Core/public/Math/MathCommon.hpp" +#include "Core/public/Math/SIMD/Storage.h" + +#include "Core/public/Math/MathFwd.h" + +#include "Core/public/Math/Vector2.hpp" + +namespace Phanes::Core::Math +{ + + /// 4D Vector defined with x, y, z, w. + template + struct TVector4 + { + public: + using Real = T; + union + { + struct { + /// + /// X component of vector + /// + T x; + + /// + /// X component of vector + /// + T y; + + /// + /// Z component of vector + /// + T z; + + /// + /// W component of vector + /// + T w; + + }; + /// + /// Wraps components in one array / xmm register. + /// + union + { + typename Phanes::Core::SIMD::Storage<4, T, IsAlgined>::type comp; + typename Phanes::Core::SIMD::Storage<4, T, IsAlgined>::type data; + }; + + }; + + /// Default constructor + TVector4() = default; + + /// Copy constructor + TVector4(const TVector4& v); + + /// + /// Construct vector from one scalar. + /// x,y,z,w = s + /// + /// Scalar + TVector4(Real s); + + /// + /// Construct vector from x, y, z, w components. + /// + /// X component + /// Y component + /// Z component + /// W component + TVector4(Real _x, Real _y, Real _z, Real _w); + + /// + /// Construct vector from two 2d vectors like: + /// x, y = v1.x, v1.y + /// z, w = v2.x, v2.y + /// + /// TVector2 one + /// TVector2 two + TVector4(const TVector2& v1, const TVector2& v2); + + /// + /// Construct vector from array of components + /// + /// Array of at least 4 components + TVector4(const Real* comp); + + /// + /// Construct the vector, by calculating the way between two points. + /// + /// Starting point of the vector. + /// End point of the vector. + TVector4(const TPoint4& start, const TPoint4& end); + + }; + + // ===================== // + // Vector4 operators // + // ===================== // + + // Unary arithmetic operators + + /// + /// Vector addition. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Copy of v1. + template + TVector4 operator+= (TVector4& v1, const TVector4& v2); + + /// + /// Vector - scalar addition. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Copy of v1. + template + TVector4 operator+= (TVector4& v1, T s); + + /// + /// Vector substraction. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Copy of v1. + template + TVector4 operator-= (TVector4& v1, const TVector4& v2); + + /// + /// Vector - scalar substraction. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Copy of v1. + template + TVector4 operator-= (TVector4& v1, T s); + + /// + /// Vector - scalar multiplication. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Copy of v1. + template + TVector4 operator*= (TVector4& v1, T s); + + /// + /// Scale vector by another vector componentwise. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Copy of v1. + template + TVector4 operator*= (TVector4& v1, const TVector4& v2); + + /// + /// Vector - scalar division. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Copy of v1. + template + TVector4 operator/= (TVector4& v1, T s); + + /// + /// Coponentwise vector division. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Copy of v1. + template + TVector4 operator/= (TVector4& v1, const TVector4& v2); + + + + // Unary arithmetic operators + + /// + /// Vector addition. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Computed vector. + template + TVector4 operator+ (TVector4& v1, const TVector4& v2); + + /// + /// Vector - scalar addition. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Computed vector. + template + TVector4 operator+ (TVector4& v1, T s); + + /// + /// Vector substraction. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Computed vector. + template + TVector4 operator- (TVector4& v1, const TVector4& v2); + + /// + /// Vector - scalar substraction. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Computed vector. + template + TVector4 operator- (TVector4& v1, T s); + + /// + /// Vector - scalar multiplication. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Computed vector. + template + TVector4 operator* (TVector4& v1, T s); + + /// + /// Scale vector by another vector componentwise. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Computed vector. + template + TVector4 operator* (TVector4& v1, const TVector4& v2); + + /// + /// Vector - scalar division. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Scalar + /// Computed vector. + template + TVector4 operator/ (TVector4& v1, T s); + + /// + /// Componentwise vector division. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Computed vector. + template + TVector4 operator/ (TVector4& v1, const TVector4& v2); + + + + // Comparison operators + + /// + /// Test to vectors for equality. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// True, if equal and false if not. + template + bool operator==(const TVector4& v1, const TVector4& v2); + + + /// + /// Test to vectors for inequality. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// True, if inequal and false if equal. + template + bool operator!=(const TVector4& v1, const TVector4& v2); + + + + + // Unaray increment operators + + + /// + /// Increment vector by one + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4& operator++(TVector4& v1); + + /// + /// Decrement vector by one + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// v1 + template + TVector4& operator--(TVector4& v1); + + /// + /// Increment vector by one + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// v1 + template + TVector4 operator++(TVector4& v1, int); + + /// + /// Decrement vector by one + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4 operator--(TVector4& v1, int); + + + + + + + + // ====================== // + // TVector4 functions // + // ====================== // + + /// + /// Get magnitude of vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Magnitude of vector. + template + T Magnitude(const TVector4& v); + + /// + /// Get square of magnitude of vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Square of magnitude of vector. + template + T SqrMagnitude(const TVector4& v); + + /// + /// Get magnitude of vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Magnitude of vector. + template + constexpr T Length(const TVector4& v) { return Magnitude(v); } + + /// + /// Get square of magnitude of vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Square of magnitude of vector. + template + constexpr T SqrLength(const TVector4& v) { return SqrMagnitude(v); } + + /// + /// Angle between two vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// + template + T Angle(const TVector4& v1, const TVector4& v2); + + /// + /// Cosine of angle between two vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// + template + T CosineAngle(const TVector4& v1, const TVector4& v2); + + /// + /// Normalizes a vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Normalized vector + template + TVector4 Normalize(const TVector4& v1); + + /// + /// Normalizes a vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4 NormalizeV(TVector4& v1); + + /// + /// Normalizes a vector. + /// + /// Doesn't check for zero vector. + /// Type of vector + /// Vector is aligned? + /// Vector + /// Normalized vector + template + TVector4 UnsafeNormalize(const TVector4& v1); + + /// + /// Normalizes a vector. + /// + /// Doesn't check for zero vector. + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4 UnsafeNormalizeV(TVector4& v1); + + /// + /// Calculates the dot product between two vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Dot product between vectors. + template + T DotP(const TVector4& v1, const TVector4& v2); + + /// + /// Gets componentwise max of both vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Vector with componentwise max of both vectors. + template + TVector4 Max(const TVector4& v1, const TVector4& v2); + + /// + /// Gets componentwise max of both vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Copy of v1. + template + TVector4 MaxV(TVector4& v1, const TVector4& v2); + + /// + /// Gets componentwise min of both vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Vector with componentwise max of both vectors. + template + TVector4 Min(const TVector4& v1, const TVector4& v2); + + /// + /// Gets componentwise min of both vectors. + /// + /// Type of vector + /// Vector is aligned? + /// Vector one + /// Vector two + /// Copy of v1. + template + TVector4 MinV(TVector4& v1, const TVector4& v2); + + /// + /// Inverses vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Inverted vector + template + TVector4 Negate(const TVector4& v1); + + /// + /// Inverses vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4 NegateV(TVector4& v1); + + /// + /// Inverses the components of vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Vector with reciprocal of components. + template + TVector4 CompInverse(const TVector4& v1); + + /// + /// Inverses the components of vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4 CompInverseV(TVector4& v1); + + /// + /// Clamp the vectors length to a magnitude. + /// + /// Type of vector + /// Vector is aligned? + /// + /// + /// Vector with magnitude clamped to s. + template + TVector4 ClampToMagnitude(const TVector4& v1, T s); + + /// + /// Clamp the vectors length to a magnitude. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Magnitude + /// Copy of v1. + template + TVector4 ClampToMagnitudeV(TVector4& v1, T s); + + /// + /// Scale vector to a magnitude + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Magnitude + /// Vector with scaled magnitude. + template + TVector4 ScaleToMagnitude(const TVector4& v1, T s); + + /// + /// Scale vector to a magnitude + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Magnitude + /// Copy of v1. + template + TVector4 ScaleToMagnitudeV(TVector4& v1, T s); + + /// + /// Reflect vector on plane. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Planes normal + /// Reflected vector + template + TVector4 Reflect(const TVector4& v1, const TVector4 normal); + + /// + /// Reflect vector on plane. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Planes normal + /// Copy of v1. + template + TVector4 ReflectV(TVector4& v1, const TVector4 normal); + + /// + /// Project vector v1 onto v2. + /// + /// Type of vector + /// Vector is aligned? + /// Vector to project + /// Vector to project on + /// Projected vector. + template + TVector4 Project(const TVector4& v1, const TVector4 v2); + + /// + /// Project vector v1 onto v2. + /// + /// Type of vector + /// Vector is aligned? + /// Vector to project + /// Vector to project on + /// Copy of v1. + template + TVector4 ProjectV(const TVector4& v1, const TVector4 v2); + + /// + /// Reject vector v1 from v2. + /// + /// Type of vector + /// Vector is aligned? + /// Vector to reject + /// Vector to reject from + /// Rejected vector. + template + TVector4 Reject(const TVector4& v1, const TVector4 v2); + + /// + /// Reject vector v1 from v2. + /// + /// Type of vector + /// Vector is aligned? + /// Vector to reject + /// Vector to reject from + /// Copy of v1. + template + TVector4 RejectV(const TVector4& v1, const TVector4 v2); + + /// + /// Perspective divide vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Perspective divided vector. + template + TVector4 PrespectiveDivide(const TVector4& v1); + + /// + /// Perspective divide vector. + /// + /// Type of vector + /// Vector is aligned? + /// Vector + /// Copy of v1. + template + TVector4 PrespectiveDivideV(TVector4& v1); +} + +// No SIMD +#include "Core/public/Math/Vector4.inl" + +// SIMD +#include "Core/public/Math/SIMD/SIMDIntrinsics.h" \ No newline at end of file diff --git a/Engine/Source/Runtime/Core/public/Math/Vector4.inl b/Engine/Source/Runtime/Core/public/Math/Vector4.inl new file mode 100644 index 0000000..4f2cfcc --- /dev/null +++ b/Engine/Source/Runtime/Core/public/Math/Vector4.inl @@ -0,0 +1,177 @@ +#pragma once + +#include "Core/public/Math/Boilerplate.h" + +#include "Core/public/Math/Detail/Vector4Decl.inl" +#include "Core/public/Math/Vector4.hpp" + +#include + +namespace Phanes::Core::Math +{ + template + TVector4::TVector4(const TVector4& v) : + x(v.x), + y(v.y), + z(v.z), + w(v.w) + {} + + template + TVector4::TVector4(Real _x, Real _y, Real _z, Real _w) : + x(_x), + y(_y), + z(_z), + w(_w) + {} + + template + TVector4 operator+=(TVector4& v1, const TVector4& v2) + { + Detail::compute_vec4_add::map(v1, v1, v2); + return v1; + } + + template + TVector4 operator+=(TVector4& v1, T s) + { + Detail::compute_vec4_add::map(v1, v1, s); + return v1; + } + + template + TVector4 operator-=(TVector4& v1, const TVector4& v2) + { + Detail::compute_vec4_sub::map(v1, v1, v2); + return v1; + } + + template + TVector4 operator-=(TVector4& v1, T s) + { + Detail::compute_vec4_sub::map(v1, v1, s); + return v1; + } + + template + TVector4 operator*=(TVector4& v1, const TVector4& v2) + { + Detail::compute_vec4_mul::map(v1, v1, v2); + return v1; + } + + template + TVector4 operator*=(TVector4& v1, T s) + { + Detail::compute_vec4_mul::map(v1, v1, s); + return v1; + } + + template + TVector4 operator/=(TVector4& v1, const TVector4& v2) + { + Detail::compute_vec4_div::map(v1, v1, v2); + return v1; + } + + template + TVector4 operator/=(TVector4& v1, T s) + { + Detail::compute_vec4_div::map(v1, v1, s); + return v1; + } + + template + TVector4 operator+(TVector4& v1, const TVector4& v2) + { + TVector4 r; + Detail::compute_vec4_add::map(r, v1, v2); + return r; + } + + template + TVector4 operator+(TVector4& v1, T s) + { + TVector4 r; + Detail::compute_vec4_add::map(r, v1, s); + return r; + } + + template + TVector4 operator-(TVector4& v1, const TVector4& v2) + { + TVector4 r; + Detail::compute_vec4_sub::map(r, v1, v2); + return r; + } + + template + TVector4 operator-(TVector4& v1, T s) + { + TVector4 r; + Detail::compute_vec4_sub::map(r, v1, s); + return r; + } + + template + TVector4 operator*(TVector4& v1, const TVector4& v2) + { + TVector4 r; + Detail::compute_vec4_mul::map(r, v1, v2); + return r; + } + + template + TVector4 operator*(TVector4& v1, T s) + { + TVector4 r; + Detail::compute_vec4_mul::map(r, v1, s); + return r; + } + + template + TVector4 operator/(TVector4& v1, const TVector4& v2) + { + TVector4 r; + Detail::compute_vec4_div::map(r, v1, v2); + return r; + } + + template + TVector4 operator/(TVector4& v1, T s) + { + TVector4 r; + Detail::compute_vec4_div::map(r, v1, s); + return r; + } + + // Comparision + + template + TVector4 operator==(const TVector4& v1, const TVector4& v2) + { + return Detail::compute_vec4_eq::map(v1, v2); + } + + template + TVector4 operator!=(const TVector4& v1, const TVector4& v2) + { + return Detail::compute_vec4_ieq::map(v1, v2); + } + + + + + // SIMD + + + template<> + TVector4::TVector4(Real _x, Real _y, Real _z, Real _w) : + x(_x), + y(_y), + z(_z), + w(_w) + { + this->comp = _mm_load_ps(reinterpret_cast(&x)); + } +} \ No newline at end of file diff --git a/Engine/Source/Runtime/Phanes.h b/Engine/Source/Runtime/Phanes.h index d6d4576..a8d0f75 100644 --- a/Engine/Source/Runtime/Phanes.h +++ b/Engine/Source/Runtime/Phanes.h @@ -2,4 +2,5 @@ // --- Core ------------------- -#include "Core/Include.h" \ No newline at end of file +#include "Core/Include.h" + diff --git a/Tests/TestProject/Main.cpp b/Tests/TestProject/Main.cpp index 64da70e..2e891de 100644 --- a/Tests/TestProject/Main.cpp +++ b/Tests/TestProject/Main.cpp @@ -1,27 +1,31 @@ #include #include -#include +#include "Core/public/Math/Vector4.hpp" +namespace PMath = Phanes::Core::Math; int main() { - + PMath::TVector4 vec0{ 3.4f, 2.3f, 1.2f, 7.5f }; + PMath::TVector4 vec1{ 7.5f, 3.4f, 2.7f, 2.6f }; - for (int i = 0; i < 10; i++) + /*for (int i = 0; i < 10; i++) { auto start = std::chrono::high_resolution_clock::now(); for (size_t i = 0; i < 999999; i++) { - + } auto end = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast(end - start).count() << std::endl; - } - + }*/ + // vec0 += vec1; + + std::cout << vec0.x << " " << vec0.y << " " << vec0.z << " " << vec0.w << std::endl; return 0;