mirror of
https://github.com/thunderbrewhq/typhoon.git
synced 2025-12-12 02:22:30 +00:00
feat(vector): update C2Vector class
This commit is contained in:
parent
8e406788b1
commit
f28f744a5b
5 changed files with 248 additions and 2 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
|
@ -1,6 +1,8 @@
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.idea
|
.vs
|
||||||
.vscode
|
.vscode
|
||||||
|
.idea
|
||||||
|
|
||||||
/build
|
/build
|
||||||
/dist
|
/cmake-build-*
|
||||||
|
/out
|
||||||
|
|
@ -13,6 +13,30 @@ class CMath {
|
||||||
static constexpr float OO_TWO_PI = 1.0f / TWO_PI;
|
static constexpr float OO_TWO_PI = 1.0f / TWO_PI;
|
||||||
|
|
||||||
// Static functions
|
// 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) {
|
static int32_t fint(float n) {
|
||||||
return static_cast<int32_t>(n);
|
return static_cast<int32_t>(n);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,174 @@
|
||||||
#include "tempest/vector/C2Vector.hpp"
|
#include "tempest/vector/C2Vector.hpp"
|
||||||
|
#include "tempest/Math.hpp"
|
||||||
|
|
||||||
|
#include <storm/Error.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
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) {
|
bool C2Vector::operator==(const C2Vector& v) {
|
||||||
return this->x == v.x && this->y == v.y;
|
return this->x == v.x && this->y == v.y;
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,63 @@
|
||||||
#ifndef TEMPEST_VECTOR_C_2VECTOR_HPP
|
#ifndef TEMPEST_VECTOR_C_2VECTOR_HPP
|
||||||
#define TEMPEST_VECTOR_C_2VECTOR_HPP
|
#define TEMPEST_VECTOR_C_2VECTOR_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
class C2Vector {
|
class C2Vector {
|
||||||
public:
|
public:
|
||||||
// Member variables
|
// Member variables
|
||||||
float x = 0.0f;
|
float x = 0.0f;
|
||||||
float y = 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
|
// Member functions
|
||||||
C2Vector() = default;
|
C2Vector() = default;
|
||||||
C2Vector(float x, float y)
|
C2Vector(float x, float y)
|
||||||
: x(x)
|
: x(x)
|
||||||
, y(y) {};
|
, 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);
|
bool operator==(const C2Vector& v);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,10 @@ class C3Vector {
|
||||||
|
|
||||||
// Member functions
|
// Member functions
|
||||||
C3Vector() = default;
|
C3Vector() = default;
|
||||||
|
C3Vector(float a)
|
||||||
|
: x(a)
|
||||||
|
, y(a)
|
||||||
|
, z(a) {};
|
||||||
C3Vector(float x, float y, float z)
|
C3Vector(float x, float y, float z)
|
||||||
: x(x)
|
: x(x)
|
||||||
, y(y)
|
, y(y)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue