From f28f744a5b0fd71d47ea0e3bad3d8dd66d7753c7 Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 27 May 2025 01:02:28 +0400 Subject: [PATCH] feat(vector): update C2Vector class --- .gitignore | 6 +- tempest/math/CMath.hpp | 24 +++++ tempest/vector/C2Vector.cpp | 170 ++++++++++++++++++++++++++++++++++++ tempest/vector/C2Vector.hpp | 46 ++++++++++ tempest/vector/C3Vector.hpp | 4 + 5 files changed, 248 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 5976665..9a323fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .DS_Store -.idea +.vs .vscode +.idea /build -/dist +/cmake-build-* +/out \ No newline at end of file diff --git a/tempest/math/CMath.hpp b/tempest/math/CMath.hpp index 48a8ba8..10ac6dd 100644 --- a/tempest/math/CMath.hpp +++ b/tempest/math/CMath.hpp @@ -13,6 +13,30 @@ class CMath { static constexpr float OO_TWO_PI = 1.0f / TWO_PI; // Static functions + static float acos(float x) { + return ::acosf(x); + } + + static float cos(float x) { + return ::cosf(x); + } + + static float sin(float x) { + return ::sinf(x); + } + + static float fabs(float x) { + return ::fabs(x); + } + + static bool fequalz(float a, float b, float z) { + return z > CMath::fabs(a - b); + } + + static bool fequal(float a, float b) { + return CMath::fequalz(a, b, 0.00000023841858f); + } + static int32_t fint(float n) { return static_cast(n); } diff --git a/tempest/vector/C2Vector.cpp b/tempest/vector/C2Vector.cpp index 9819fde..f9ad583 100644 --- a/tempest/vector/C2Vector.cpp +++ b/tempest/vector/C2Vector.cpp @@ -1,4 +1,174 @@ #include "tempest/vector/C2Vector.hpp" +#include "tempest/Math.hpp" + +#include + + +C2Vector C2Vector::FromAxisAngle(float axang, float mag) { + float x = mag * CMath::cos(axang); + float y = mag * CMath::sin(axang); + return C2Vector(x, y); +} + +C2Vector C2Vector::Min(const C2Vector& a, const C2Vector& b) { + float x = std::fmin(a.x, b.x); + float y = std::fmin(a.y, b.y); + return C2Vector(x, y); +} + +C2Vector C2Vector::Max(const C2Vector& a, const C2Vector& b) { + float x = std::fmax(a.x, b.x); + float y = std::fmax(a.y, b.y); + return C2Vector(x, y); +} + +C2Vector C2Vector::Lerp(const C2Vector& a, const C2Vector& l, const C2Vector& h) { + return C2Vector( + l.x + (h.x - l.x) * a.x, + l.y + (h.y - h.y) * a.y); +} + +float C2Vector::Dot(const C2Vector& l, const C2Vector& r) { + return (l.x * r.x) + (l.y * r.y); +} + +float C2Vector::Cross(const C2Vector& l, const C2Vector& r) { + return (l.x * r.y) - (r.x * l.y); +} + +void C2Vector::Get(float& ox, float& oy) const { + ox = this->x; + oy = this->y; +} +void C2Vector::Set(float nx, float ny) { + this->x = nx; + this->y = ny; +} + +C2Vector& C2Vector::operator+=(float a) { + this->x += a; + this->y += a; + return *this; +} + +C2Vector& C2Vector::operator+=(const C2Vector& a) { + this->x += a.x; + this->y += a.y; + return *this; +} + +C2Vector& C2Vector::operator-=(float a) { + this->x -= a; + this->y -= a; + return *this; +} + +C2Vector& C2Vector::operator-=(const C2Vector& a) { + this->x -= a.x; + this->y -= a.y; + return *this; +} + +C2Vector& C2Vector::operator*=(float a) { + this->x *= a; + this->y *= a; + return *this; +} + +C2Vector& C2Vector::operator*=(const C2Vector& a) { + this->x *= a.x; + this->y *= a.y; + return *this; +} + +C2Vector& C2Vector::operator/=(float a) { + this->x /= a; + this->y /= a; + return *this; +} + +C2Vector& C2Vector::operator/=(const C2Vector& a) { + this->x /= a.x; + this->y /= a.y; + return *this; +} + +C2Vector C2Vector::operator-() { + return C2Vector(-this->x, -this->y); +} + +float& C2Vector::operator[](uint32_t sub) { + STORM_ASSERT(sub < C2Vector::eComponents); + switch (sub) { + case 1: + return this->y; + case 0: + default: + return this->x; + } +} + +const float& C2Vector::operator[](uint32_t sub) const { + STORM_ASSERT(sub < C2Vector::eComponents); + switch (sub) { + case 1: + return this->y; + case 0: + default: + return this->x; + } +} + +float C2Vector::SquaredMag() const { + return (this->x * this->x) + (this->y * this->y); +} + +float C2Vector::Mag() const { + return CMath::sqrt(this->SquaredMag()); +} + +float C2Vector::SumC() const { + return this->x + this->y; +} + +bool C2Vector::IsUnit() const { + return CMath::fequalz(this->SquaredMag(), 1.0f, 0.001f); +} + +float C2Vector::AxisAngle() const { + return this->AxisAngle(this->Mag()); +} + +float C2Vector::AxisAngle(float mag) const { + if (CMath::fequal(mag, 0.0f)) { + return 0.0f; + } + + float angle = CMath::acos(this->x / mag); + + if (this->y >= 0.0f) { + return angle; + } + else { + return 6.2831855f - angle; + } +} + +void C2Vector::Normalize() { + float mag = this->Mag(); + STORM_ASSERT(mag > 0.0f); + mag = 1.0f / mag; + this->x *= mag; + this->y *= mag; +} + +void C2Vector::Scale(float a) { + float mag = this->Mag(); + STORM_ASSERT(mag > 0.0f); + mag = a * 1.0f / mag; + this->x *= mag; + this->y *= mag; +} bool C2Vector::operator==(const C2Vector& v) { return this->x == v.x && this->y == v.y; diff --git a/tempest/vector/C2Vector.hpp b/tempest/vector/C2Vector.hpp index c445c8d..00881e2 100644 --- a/tempest/vector/C2Vector.hpp +++ b/tempest/vector/C2Vector.hpp @@ -1,17 +1,63 @@ #ifndef TEMPEST_VECTOR_C_2VECTOR_HPP #define TEMPEST_VECTOR_C_2VECTOR_HPP +#include + class C2Vector { public: // Member variables float x = 0.0f; float y = 0.0f; + enum : uint32_t { + eComponents = 2 + }; + + // Static functions + static C2Vector FromAxisAngle(float axang, float mag); + static C2Vector Min(const C2Vector& a, const C2Vector& b); + static C2Vector Max(const C2Vector& a, const C2Vector& b); + static C2Vector Lerp(const C2Vector& a, const C2Vector& l, const C2Vector& h); + static float Dot(const C2Vector& l, const C2Vector& r); + static float Cross(const C2Vector& l, const C2Vector& r); + + // Member functions C2Vector() = default; C2Vector(float x, float y) : x(x) , y(y) {}; + C2Vector(float a) + : x(a) + , y(a) {}; + + void Get(float& ox, float& oy) const; + void Set(float nx, float ny); + + C2Vector& operator+=(float a); + C2Vector& operator+=(const C2Vector& a); + C2Vector& operator-=(float a); + C2Vector& operator-=(const C2Vector& a); + C2Vector& operator*=(float a); + C2Vector& operator*=(const C2Vector& a); + C2Vector& operator/=(float a); + C2Vector& operator/=(const C2Vector& a); + + C2Vector operator-(); + + float& operator[](uint32_t sub); + const float& operator[](uint32_t sub) const; + + float SquaredMag() const; + float Mag() const; + float SumC() const; + bool IsUnit() const; + float AxisAngle() const; + float AxisAngle(float mag) const; + + void Normalize(); + void Scale(float a); + bool operator==(const C2Vector& v); }; diff --git a/tempest/vector/C3Vector.hpp b/tempest/vector/C3Vector.hpp index ea9c34b..59121ad 100644 --- a/tempest/vector/C3Vector.hpp +++ b/tempest/vector/C3Vector.hpp @@ -14,6 +14,10 @@ class C3Vector { // Member functions C3Vector() = default; + C3Vector(float a) + : x(a) + , y(a) + , z(a) {}; C3Vector(float x, float y, float z) : x(x) , y(y)