mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 15:03:27 +00:00
maded vec3 constexprable, addded lvl waring option
This commit is contained in:
@@ -21,32 +21,36 @@ namespace omath
|
||||
constexpr Vector2(float x, float y) : x(x), y(y) {}
|
||||
|
||||
// Equality operators
|
||||
[[nodiscard]]
|
||||
constexpr bool operator==(const Vector2& src) const
|
||||
{
|
||||
return x == src.x && y == src.y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator!=(const Vector2& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
// Compound assignment operators
|
||||
Vector2& operator+=(const Vector2& v)
|
||||
constexpr Vector2& operator+=(const Vector2& v)
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator-=(const Vector2& v)
|
||||
constexpr Vector2& operator-=(const Vector2& v)
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator*=(const Vector2& v)
|
||||
constexpr Vector2& operator*=(const Vector2& v)
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
@@ -54,7 +58,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator/=(const Vector2& v)
|
||||
constexpr Vector2& operator/=(const Vector2& v)
|
||||
{
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
@@ -62,30 +66,35 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator*=(float fl)
|
||||
constexpr Vector2& operator*=(float fl)
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator/=(float fl)
|
||||
constexpr Vector2& operator/=(float fl)
|
||||
{
|
||||
x /= fl;
|
||||
y /= fl;
|
||||
return *this;
|
||||
}
|
||||
Vector2& operator+=(float fl)
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2& operator-=(float fl)
|
||||
constexpr Vector2& operator+=(float fl)
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator-=(float fl)
|
||||
{
|
||||
x -= fl;
|
||||
y -= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -111,45 +120,48 @@ namespace omath
|
||||
{
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
constexpr Vector2& Abs()
|
||||
{
|
||||
//FIXME: Replace with std::abs, if it will become constexprable
|
||||
x = x < 0 ? -x : x;
|
||||
y = y < 0 ? -y : y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class type>
|
||||
constexpr const type& As() const
|
||||
[[nodiscard]] constexpr const type& As() const
|
||||
{
|
||||
return *reinterpret_cast<const type*>(this);
|
||||
}
|
||||
template<class type>
|
||||
constexpr type& As()
|
||||
[[nodiscard]] constexpr type& As()
|
||||
{
|
||||
return *reinterpret_cast<type*>(this);
|
||||
}
|
||||
|
||||
// Unary negation operator
|
||||
constexpr Vector2 operator-() const
|
||||
[[nodiscard]] constexpr Vector2 operator-() const
|
||||
{
|
||||
return {-x, -y};
|
||||
}
|
||||
|
||||
// Binary arithmetic operators
|
||||
constexpr Vector2 operator+(const Vector2& v) const
|
||||
[[nodiscard]] constexpr Vector2 operator+(const Vector2& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y};
|
||||
}
|
||||
|
||||
constexpr Vector2 operator-(const Vector2& v) const
|
||||
[[nodiscard]] constexpr Vector2 operator-(const Vector2& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y};
|
||||
}
|
||||
constexpr Vector2 operator*(float fl) const
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator*(float fl) const
|
||||
{
|
||||
return {x * fl, y * fl};
|
||||
}
|
||||
constexpr Vector2 operator/(float fl) const
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator/(float fl) const
|
||||
{
|
||||
return {x / fl, y / fl};
|
||||
}
|
||||
@@ -158,7 +170,7 @@ namespace omath
|
||||
// Normalize the vector
|
||||
[[nodiscard]] Vector2 Normalized() const;
|
||||
|
||||
// Sum of elements
|
||||
// Sum of elements
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
{
|
||||
return x + y;
|
||||
|
||||
@@ -15,42 +15,166 @@ namespace omath
|
||||
{
|
||||
public:
|
||||
float z = 0.f;
|
||||
Vector3(float x, float y, float z);
|
||||
Vector3() = default;
|
||||
constexpr Vector3(float x, float y, float z) : Vector2(x, y), z(z) { }
|
||||
constexpr Vector3() : Vector2(), z(0.f) {};
|
||||
|
||||
bool operator==(const Vector3& src) const;
|
||||
bool operator!=(const Vector3& src) const;
|
||||
[[nodiscard]] constexpr bool operator==(const Vector3& src) const
|
||||
{
|
||||
return Vector2::operator==(src) && (src.z == z);
|
||||
}
|
||||
|
||||
Vector3& operator+=(const Vector3& v);
|
||||
Vector3& operator-=(const Vector3& v);
|
||||
Vector3& operator*=(float fl);
|
||||
Vector3& operator*=(const Vector3& v);
|
||||
Vector3& operator/=(const Vector3& v);
|
||||
Vector3& operator+=(float fl);
|
||||
Vector3& operator/=(float fl);
|
||||
Vector3& operator-=(float fl);
|
||||
[[nodiscard]] constexpr bool operator!=(const Vector3& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
[[nodiscard]] float DistTo(const Vector3& vOther) const;
|
||||
Vector3& Abs();
|
||||
[[nodiscard]] float DistToSqr(const Vector3& vOther) const;
|
||||
[[nodiscard]] float Dot(const Vector3& vOther) const;
|
||||
constexpr Vector3& operator+=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator+=(v);
|
||||
z += v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator-=(v);
|
||||
z -= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(float fl)
|
||||
{
|
||||
Vector2::operator*=(fl);
|
||||
z *= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator*=(v);
|
||||
z *= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator/=(v);
|
||||
z /= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(float fl)
|
||||
{
|
||||
Vector2::operator+=(fl);
|
||||
z += fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(float fl)
|
||||
{
|
||||
Vector2::operator/=(fl);
|
||||
z /= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(float fl)
|
||||
{
|
||||
Vector2::operator-=(fl);
|
||||
z -= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
float DistTo(const Vector3& vOther) const;
|
||||
|
||||
constexpr Vector3& Abs()
|
||||
{
|
||||
Vector2::Abs();
|
||||
z = z < 0.f ? -z : z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float DistToSqr(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).LengthSqr();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector3& vOther) const
|
||||
{
|
||||
return Vector2::Dot(vOther) + z * vOther.z;
|
||||
}
|
||||
[[nodiscard]] float Length() const;
|
||||
[[nodiscard]] float LengthSqr() const;
|
||||
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
{
|
||||
return Vector2::LengthSqr() + z * z;
|
||||
}
|
||||
|
||||
[[nodiscard]] float Length2D() const;
|
||||
|
||||
Vector3 operator-() const;
|
||||
Vector3 operator+(const Vector3& v) const;
|
||||
Vector3 operator-(const Vector3& v) const;
|
||||
Vector3 operator*(float fl) const;
|
||||
Vector3 operator*(const Vector3& v) const;
|
||||
Vector3 operator/(float fl) const;
|
||||
Vector3 operator/(const Vector3& v) const;
|
||||
[[nodiscard]] constexpr Vector3 operator-() const
|
||||
{
|
||||
return {-x, -y, -z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y, z + v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y, z - v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(float fl) const
|
||||
{
|
||||
return {x * fl, y * fl, z * fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const
|
||||
{
|
||||
return {x * v.x, y * v.y, z * v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(float fl) const
|
||||
{
|
||||
return {x / fl, y / fl, z / fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const
|
||||
{
|
||||
return {x / v.x, y / v.y, z / v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const
|
||||
{
|
||||
return
|
||||
{
|
||||
y * v.z - z * v.y,
|
||||
z * v.x - x * v.z,
|
||||
x * v.y - y * v.x
|
||||
};
|
||||
}
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
{
|
||||
return Vector3::Sum2D() + z;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Sum2D() const
|
||||
{
|
||||
return Vector2::Sum();
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 Cross(const Vector3 &v) const;
|
||||
[[nodiscard]] static Vector3 CreateVelocity(float pitch, float yaw, float speed);
|
||||
[[nodiscard]] float Sum() const;
|
||||
[[nodiscard]] float Sum2D() const;
|
||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const;
|
||||
|
||||
[[nodiscard]] static Vector3 ForwardVector(float pitch, float yaw);
|
||||
@@ -61,7 +185,10 @@ namespace omath
|
||||
[[nodiscard]]
|
||||
Vector3 Normalized() const;
|
||||
|
||||
[[nodiscard]] std::tuple<float, float, float> AsTuple() const;
|
||||
[[nodiscard]] std::tuple<float, float, float> AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y, z);
|
||||
}
|
||||
};
|
||||
}
|
||||
// ReSharper disable once CppRedundantNamespaceDefinition
|
||||
|
||||
@@ -12,9 +12,6 @@ namespace omath::prediction
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 CalculateVelocity(float pitch, float yaw) const;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 PredictPosition(float pitch, float yaw, float time, float gravity) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user