RDR-Classes/rage/vector.hpp

150 lines
2.5 KiB
C++
Raw Permalink Normal View History

#pragma once
#include <cmath>
#pragma pack(push, 0x10)
namespace rage
{
2024-08-08 08:11:50 +08:00
// atVector<float, 2>?
class vector2
{
public:
2024-08-08 08:11:50 +08:00
float x, y;
2024-08-08 08:11:50 +08:00
constexpr vector2(float x, float y) :
x(x),
y(y)
{
}
constexpr vector2() :
x(),
y()
{
}
};
2024-08-08 08:11:50 +08:00
// atVector<float, 3>?
class vector3
{
public:
2024-08-08 08:11:50 +08:00
float x, y, z;
private:
int _PAD; // because alignof doesn't work
public:
2024-08-08 08:11:50 +08:00
constexpr vector3(float x, float y, float z) :
x(x),
y(y),
z(z)
{
}
constexpr vector3() :
x(),
y(),
z()
{
}
2024-08-08 08:11:50 +08:00
bool operator==(const vector3& other) const
{
return this->x == other.x && this->y == other.y && this->z == other.z;
}
template<typename T>
2024-08-08 08:11:50 +08:00
bool operator!=(const vector3& other) const
{
return this->x != other.x || this->y != other.y || this->z != other.z;
}
2024-08-08 08:11:50 +08:00
vector3 operator+(const vector3& other) const
{
2024-08-08 08:11:50 +08:00
vector3 vec;
vec.x = this->x + other.x;
vec.y = this->y + other.y;
vec.z = this->z + other.z;
return vec;
}
2024-08-08 08:11:50 +08:00
vector3 operator-(const vector3& other) const
{
2024-08-08 08:11:50 +08:00
vector3 vec;
vec.x = this->x - other.x;
vec.y = this->y - other.y;
vec.z = this->z - other.z;
return vec;
}
2024-08-08 08:11:50 +08:00
vector3 operator*(const vector3& other) const
{
2024-08-08 08:11:50 +08:00
vector3 vec;
vec.x = this->x * other.x;
vec.y = this->y * other.y;
vec.z = this->z * other.z;
return vec;
}
2024-08-08 08:11:50 +08:00
vector3 operator*(const float& other) const
{
2024-08-08 08:11:50 +08:00
vector3 vec;
vec.x = this->x * other;
vec.y = this->y * other;
vec.z = this->z * other;
return vec;
}
inline float GetMagnitude() const
{
2024-08-08 08:11:50 +08:00
return sqrt(x * x + y * y + z * z);
}
2024-08-08 08:11:50 +08:00
inline float GetDistance(const vector3& other) const
{
return (*this - other).GetMagnitude();
}
};
2024-08-08 08:11:50 +08:00
static_assert(sizeof(rage::vector3) == 0x10);
// atVector<float, 4>?
class vector4
{
public:
float x, y, z, w;
constexpr vector4(float x, float y, float z, float w) :
x(x),
y(y),
z(z),
w(w)
{
}
constexpr vector4() :
x(),
y(),
z(),
w()
{
}
};
union matrix34
{
float data[4][4]; // yes, the underlying representation is 4x4
struct { struct { float x, y, z, w; } rows[3]; };
};
union matrix44
{
float data[4][4];
struct { struct { float x, y, z, w; } rows[4]; };
};
// backwards compatibility
// TODO: remove these!
using fvector2 = vector2;
using fvector3 = vector3;
using fvector4 = vector4;
};
#pragma pack(pop)