fixed bug when std::hypot is not constexpr for MSVC

This commit is contained in:
2024-10-20 08:35:21 -07:00
parent 7aa1db5f27
commit 046b560025
2 changed files with 58 additions and 25 deletions

View File

@@ -114,11 +114,29 @@ namespace omath
return x * vOther.x + y * vOther.y; return x * vOther.x + y * vOther.y;
} }
#ifndef _MSC_VER
[[nodiscard]] constexpr float Length() const [[nodiscard]] constexpr float Length() const
{ {
return std::hypot(x, y); return std::hypot(x, y);
} }
[[nodiscard]] constexpr Vector2 Normalized() const
{
const float len = Length();
return len > 0.f ? *this / len : *this;
}
#else
[[nodiscard]] float Length() const
{
return std::hypot(x, y);
}
[[nodiscard]] Vector2 Normalized() const
{
const float len = Length();
return len > 0.f ? *this / len : *this;
}
#endif
[[nodiscard]] constexpr float LengthSqr() const [[nodiscard]] constexpr float LengthSqr() const
{ {
return x * x + y * y; return x * x + y * y;
@@ -169,13 +187,6 @@ namespace omath
return {x / fl, y / fl}; return {x / fl, y / fl};
} }
// Normalize the vector
[[nodiscard]] constexpr Vector2 Normalized() const
{
const float len = Length();
return len > 0.f ? *this / len : *this;
}
// Sum of elements // Sum of elements
[[nodiscard]] constexpr float Sum() const [[nodiscard]] constexpr float Sum() const
{ {

View File

@@ -92,11 +92,6 @@ namespace omath
return *this; return *this;
} }
[[nodiscard]] constexpr float DistTo(const Vector3& vOther) const
{
return (*this - vOther).Length();
}
constexpr Vector3& Abs() constexpr Vector3& Abs()
{ {
Vector2::Abs(); Vector2::Abs();
@@ -115,20 +110,55 @@ namespace omath
return Vector2::Dot(vOther) + z * vOther.z; return Vector2::Dot(vOther) + z * vOther.z;
} }
#ifndef _MSC_VER
[[nodiscard]] constexpr float Length() const [[nodiscard]] constexpr float Length() const
{ {
return std::hypot(x, y, z); return std::hypot(x, y, z);
} }
[[nodiscard]] constexpr float LengthSqr() const
{
return Vector2::LengthSqr() + z * z;
}
[[nodiscard]] constexpr float Length2D() const [[nodiscard]] constexpr float Length2D() const
{ {
return Vector2::Length(); return Vector2::Length();
} }
[[nodiscard]] float DistTo(const Vector3& vOther) const
{
return (*this - vOther).Length();
}
[[nodiscard]] constexpr Vector3 Normalized() const
{
const float length = this->Length();
return length != 0 ? *this / length : *this;
}
#else
[[nodiscard]] float Length() const
{
return std::hypot(x, y, z);
}
[[nodiscard]] Vector3 Normalized() const
{
const float length = this->Length();
return length != 0 ? *this / length : *this;
}
[[nodiscard]] float Length2D() const
{
return Vector2::Length();
}
[[nodiscard]] float DistTo(const Vector3& vOther) const
{
return (*this - vOther).Length();
}
#endif
[[nodiscard]] constexpr float LengthSqr() const
{
return Vector2::LengthSqr() + z * z;
}
[[nodiscard]] constexpr Vector3 operator-() const [[nodiscard]] constexpr Vector3 operator-() const
{ {
@@ -190,14 +220,6 @@ namespace omath
[[nodiscard]] static Vector3 RightVector(float pitch, float yaw, float roll); [[nodiscard]] static Vector3 RightVector(float pitch, float yaw, float roll);
[[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll); [[nodiscard]] static Vector3 UpVector(float pitch, float yaw, float roll);
[[nodiscard]] constexpr Vector3 Normalized() const
{
const float length = this->Length();
return length != 0 ? *this / length : *this;
}
[[nodiscard]] std::tuple<float, float, float> AsTuple() const [[nodiscard]] std::tuple<float, float, float> AsTuple() const
{ {
return std::make_tuple(x, y, z); return std::make_tuple(x, y, z);