mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
maded vec3 constexprable, addded lvl waring option
This commit is contained in:
@@ -6,7 +6,7 @@ project(omath)
|
||||
set(CMAKE_CXX_STANDARD 26)
|
||||
|
||||
option(BUILD_TESTS "Build unit tests" ON)
|
||||
|
||||
option(THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to threat them as errors" ON)
|
||||
add_library(omath STATIC source/Vector3.cpp)
|
||||
|
||||
add_subdirectory(source)
|
||||
@@ -16,4 +16,10 @@ if(BUILD_TESTS)
|
||||
add_subdirectory(tests)
|
||||
endif ()
|
||||
|
||||
if (WIN32 AND THREAT_WARNING_AS_ERROR)
|
||||
target_compile_definitions(omath PRIVATE /W4 /WX)
|
||||
elseif(UNIX AND THREAT_WARNING_AS_ERROR)
|
||||
target_compile_definitions(omath PRIVATE -Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
target_include_directories(omath PUBLIC include)
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -9,178 +9,22 @@
|
||||
namespace omath
|
||||
{
|
||||
|
||||
Vector3::Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool Vector3::operator==(const Vector3 &src) const
|
||||
{
|
||||
return Vector2::operator==(src) && (src.z == z);
|
||||
}
|
||||
|
||||
bool Vector3::operator!=(const Vector3 &src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator+=(const Vector3 &v)
|
||||
{
|
||||
Vector2::operator+=(v);
|
||||
z += v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator-=(const Vector3 &v)
|
||||
{
|
||||
Vector2::operator-=(v);
|
||||
z -= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator*=(const float fl)
|
||||
{
|
||||
Vector2::operator*=(fl);
|
||||
z *= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator*=(const Vector3 &v)
|
||||
{
|
||||
Vector2::operator*=(v);
|
||||
z *= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator/=(const Vector3 &v)
|
||||
{
|
||||
Vector2::operator/=(v);
|
||||
z /= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator+=(const float fl)
|
||||
{
|
||||
Vector2::operator+=(fl);
|
||||
z += fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator/=(const float fl)
|
||||
{
|
||||
Vector2::operator/=(fl);
|
||||
z /= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator-=(const float fl)
|
||||
{
|
||||
Vector2::operator-=(fl);
|
||||
z -= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
float Vector3::DistTo(const Vector3 &vOther) const
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
}
|
||||
|
||||
Vector3 &Vector3::Abs()
|
||||
{
|
||||
Vector2::Abs();
|
||||
z = std::abs(z);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
float Vector3::DistToSqr(const Vector3 &vOther) const
|
||||
{
|
||||
return (*this - vOther).LengthSqr();
|
||||
}
|
||||
|
||||
float Vector3::Dot(const Vector3 &vOther) const
|
||||
{
|
||||
return Vector2::Dot(vOther) + z * vOther.z;
|
||||
}
|
||||
|
||||
float Vector3::Length() const
|
||||
{
|
||||
return std::sqrt(Vector2::LengthSqr() + z * z);
|
||||
}
|
||||
|
||||
float Vector3::LengthSqr() const
|
||||
{
|
||||
return Vector2::LengthSqr() + z * z;
|
||||
}
|
||||
|
||||
float Vector3::Length2D() const
|
||||
{
|
||||
return Vector2::Length();
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator-() const
|
||||
{
|
||||
return {-x, -y, -z};
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator+(const Vector3 &v) const
|
||||
{
|
||||
return {x + v.x, y + v.y, z + v.z};
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator-(const Vector3 &v) const
|
||||
{
|
||||
return {x - v.x, y - v.y, z - v.z};
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator*(float fl) const
|
||||
{
|
||||
return {x * fl, y * fl, z * fl};
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator*(const Vector3 &v) const
|
||||
{
|
||||
return {x * v.x, y * v.y, z * v.z};
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator/(const float fl) const
|
||||
{
|
||||
return {x / fl, y / fl, z / fl};
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator/(const Vector3 &v) const
|
||||
{
|
||||
return {x / v.x, y / v.y, z / v.z};
|
||||
}
|
||||
|
||||
Vector3 Vector3::CreateVelocity(const float pitch, const float yaw, const float speed)
|
||||
{
|
||||
return
|
||||
{
|
||||
std::cos(angles::DegreesToRadians(pitch)) * std::cos(angles::DegreesToRadians(yaw)) * speed,
|
||||
std::cos(angles::DegreesToRadians(pitch)) * std::sin(angles::DegreesToRadians(yaw)) * speed,
|
||||
std::sin(angles::DegreesToRadians(pitch)) * speed,
|
||||
};
|
||||
}
|
||||
|
||||
float Vector3::Sum() const
|
||||
{
|
||||
return Vector3::Sum2D() + z;
|
||||
}
|
||||
|
||||
float Vector3::Sum2D() const
|
||||
{
|
||||
return Vector2::Sum();
|
||||
}
|
||||
|
||||
Vector3 Vector3::ViewAngleTo(const Vector3 &other) const
|
||||
{
|
||||
const float distance = DistTo(other);
|
||||
@@ -233,15 +77,6 @@ namespace omath
|
||||
return RightVector(pitch, yaw, roll).Cross(ForwardVector(pitch, yaw));
|
||||
}
|
||||
|
||||
Vector3 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
|
||||
};
|
||||
}
|
||||
|
||||
Vector3 Vector3::Normalized() const
|
||||
{
|
||||
@@ -249,9 +84,4 @@ namespace omath
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
}
|
||||
|
||||
std::tuple<float, float, float> Vector3::AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y, z);
|
||||
}
|
||||
}
|
||||
@@ -8,14 +8,9 @@
|
||||
|
||||
namespace omath::prediction
|
||||
{
|
||||
Vector3 Projectile::CalculateVelocity(const float pitch, const float yaw) const
|
||||
{
|
||||
return Vector3::CreateVelocity(pitch, yaw, m_launchSpeed);
|
||||
}
|
||||
|
||||
Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
||||
{
|
||||
auto currentPos = m_origin + Vector3::CreateVelocity(pitch, yaw, m_launchSpeed) * time;
|
||||
auto currentPos = m_origin + Vector3::ForwardVector(pitch, yaw) * m_launchSpeed * time;
|
||||
currentPos.z -= (gravity * m_gravityScale) * std::pow(time, 2.f) * 0.5f;
|
||||
|
||||
return currentPos;
|
||||
|
||||
Reference in New Issue
Block a user