Temp-Classes/rage/vector.hpp

238 lines
3.8 KiB
C++
Raw Permalink Normal View History

2022-01-28 08:15:26 +08:00
#pragma once
namespace rage
{
2024-04-01 02:40:59 +08:00
template <typename T>
2023-05-10 04:18:42 +08:00
union vector2
{
T data[2];
2024-04-01 02:40:59 +08:00
struct
{
T x, y;
};
2023-05-10 04:18:42 +08:00
constexpr vector2(T x, T y) : x(x),
2024-04-01 02:40:59 +08:00
y(y)
2023-05-10 04:18:42 +08:00
{
}
constexpr vector2() : x(),
2024-04-01 02:40:59 +08:00
y()
2023-05-10 04:18:42 +08:00
{
}
};
2024-04-01 02:40:59 +08:00
template <typename T>
2023-05-10 04:18:42 +08:00
union vector3
{
T data[3];
2024-04-01 02:40:59 +08:00
struct
{
T x, y, z;
};
constexpr vector3(T x, T y, T z) noexcept : x(x),
2024-04-01 02:40:59 +08:00
y(y),
z(z)
{
}
constexpr vector3() noexcept : x(),
2024-04-01 02:40:59 +08:00
y(),
z()
{
}
constexpr vector3 operator+(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x + rhs.x, y + rhs.y, z + rhs.z);
}
constexpr vector3 operator-(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x - rhs.x, y - rhs.y, z - rhs.z);
}
constexpr vector3 operator*(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x * rhs.x, y * rhs.y, z * rhs.z);
}
constexpr vector3 operator/(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x / rhs.x, y / rhs.y, z / rhs.z);
}
constexpr vector3 operator+(const float &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x + rhs, y + rhs, z + rhs);
}
constexpr vector3 operator-(const float &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x - rhs, y - rhs, z - rhs);
}
constexpr vector3 operator*(const float &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x * rhs, y * rhs, z * rhs);
}
constexpr vector3 operator/(const float &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x / rhs, y / rhs, z / rhs);
}
constexpr bool operator==(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return x == rhs.x && y == rhs.y && z == rhs.z;
}
constexpr vector3 &operator+=(const vector3 &rhs)
2024-04-01 02:40:59 +08:00
{
return *this = *this + rhs;
}
constexpr vector3 &operator-=(const vector3 &rhs)
2024-04-01 02:40:59 +08:00
{
return *this = *this - rhs;
}
2023-05-10 04:18:42 +08:00
constexpr vector3 &operator*=(const vector3 &rhs)
2023-05-10 04:18:42 +08:00
{
2024-04-01 02:40:59 +08:00
return *this = *this * rhs;
2023-05-10 04:18:42 +08:00
}
constexpr vector3 &operator/=(const vector3 &rhs)
2023-05-10 04:18:42 +08:00
{
2024-04-01 02:40:59 +08:00
return *this = *this / rhs;
}
constexpr vector3 &operator+=(const float &rhs)
2024-04-01 02:40:59 +08:00
{
return *this = *this + rhs;
}
constexpr vector3 &operator-=(const float &rhs)
2024-04-01 02:40:59 +08:00
{
return *this = *this - rhs;
}
constexpr vector3 &operator*=(const float &rhs)
2024-04-01 02:40:59 +08:00
{
return *this = *this * rhs;
}
constexpr vector3 &operator/=(const float &rhs)
2024-04-01 02:40:59 +08:00
{
return *this = *this / rhs;
}
constexpr T length() const
2024-04-01 02:40:59 +08:00
{
return sqrt(x * x + y * y + z * z);
}
constexpr T magnitude() const
2024-04-01 02:40:59 +08:00
{
return length();
}
// This doesn't modify the vector inline.
constexpr vector3 normalize() const
2024-04-01 02:40:59 +08:00
{
T len = length();
if (len)
{
return *this * (1 / len);
}
return *this;
}
// This doesn't modify the vector inline.
constexpr vector3 multiply(const vector3 rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(x * rhs.x, y * rhs.y, z * rhs.z);
}
// This doesn't modify the vector inline.
constexpr vector3 invert() const
2024-04-01 02:40:59 +08:00
{
return *this * -1;
}
// This doesn't modify the vector inline.
constexpr vector3 cross_product(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return vector3(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
}
constexpr float dot_product(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return x * rhs.x + y * rhs.y + z * rhs.z;
}
constexpr float distance(const vector3 &rhs) const
2024-04-01 02:40:59 +08:00
{
return (*this - rhs).length();
2023-05-10 04:18:42 +08:00
}
};
2024-04-01 02:40:59 +08:00
template <typename T>
2023-05-10 04:18:42 +08:00
union vector4
{
T data[4];
2024-04-01 02:40:59 +08:00
struct
{
T x, y, z, w;
};
2023-05-10 04:18:42 +08:00
constexpr vector4(T x, T y, T z, T w) : x(x),
2024-04-01 02:40:59 +08:00
y(y),
z(z),
w(w)
2023-05-10 04:18:42 +08:00
{
}
constexpr vector4() : x(),
2024-04-01 02:40:59 +08:00
y(),
z(),
w()
2023-05-10 04:18:42 +08:00
{
}
};
2024-04-01 02:40:59 +08:00
template <typename T>
2023-05-10 04:18:42 +08:00
union matrix34
{
T data[3][4];
2024-04-01 02:40:59 +08:00
struct
{
struct
{
T x, y, z, w;
} rows[3];
};
2023-05-10 04:18:42 +08:00
};
2024-04-01 02:40:59 +08:00
template <typename T>
2023-05-10 04:18:42 +08:00
union matrix44
{
T data[4][4];
2024-04-01 02:40:59 +08:00
struct
{
struct
{
T x, y, z, w;
} rows[4];
};
2023-05-10 04:18:42 +08:00
};
typedef vector2<float> fvector2;
typedef vector3<float> fvector3;
typedef vector4<float> fvector4;
typedef matrix34<float> fmatrix34;
typedef matrix44<float> fmatrix44;
2022-01-28 08:15:26 +08:00
}