added noexcept for vector types

This commit is contained in:
2025-05-04 19:13:26 +03:00
parent f6f8bba032
commit 9a38d47b0d
3 changed files with 85 additions and 85 deletions

View File

@@ -24,25 +24,25 @@ namespace omath
// Constructors // Constructors
constexpr Vector2() = default; constexpr Vector2() = default;
constexpr Vector2(const Type& x, const Type& y): x(x), y(y) constexpr Vector2(const Type& x, const Type& y) noexcept: x(x), y(y)
{ {
} }
// Equality operators // Equality operators
[[nodiscard]] [[nodiscard]]
constexpr bool operator==(const Vector2& src) const constexpr bool operator==(const Vector2& src) const noexcept
{ {
return x == src.x && y == src.y; return x == src.x && y == src.y;
} }
[[nodiscard]] [[nodiscard]]
constexpr bool operator!=(const Vector2& src) const constexpr bool operator!=(const Vector2& src) const noexcept
{ {
return !(*this == src); return !(*this == src);
} }
// Compound assignment operators // Compound assignment operators
constexpr Vector2& operator+=(const Vector2& v) constexpr Vector2& operator+=(const Vector2& v) noexcept
{ {
x += v.x; x += v.x;
y += v.y; y += v.y;
@@ -50,7 +50,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator-=(const Vector2& v) constexpr Vector2& operator-=(const Vector2& v) noexcept
{ {
x -= v.x; x -= v.x;
y -= v.y; y -= v.y;
@@ -58,7 +58,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator*=(const Vector2& v) constexpr Vector2& operator*=(const Vector2& v) noexcept
{ {
x *= v.x; x *= v.x;
y *= v.y; y *= v.y;
@@ -66,7 +66,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator/=(const Vector2& v) constexpr Vector2& operator/=(const Vector2& v) noexcept
{ {
x /= v.x; x /= v.x;
y /= v.y; y /= v.y;
@@ -74,7 +74,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator*=(const Type& fl) constexpr Vector2& operator*=(const Type& fl) noexcept
{ {
x *= fl; x *= fl;
y *= fl; y *= fl;
@@ -82,7 +82,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator/=(const Type& fl) constexpr Vector2& operator/=(const Type& fl) noexcept
{ {
x /= fl; x /= fl;
y /= fl; y /= fl;
@@ -90,7 +90,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator+=(const Type& fl) constexpr Vector2& operator+=(const Type& fl) noexcept
{ {
x += fl; x += fl;
y += fl; y += fl;
@@ -98,7 +98,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector2& operator-=(const Type& fl) constexpr Vector2& operator-=(const Type& fl) noexcept
{ {
x -= fl; x -= fl;
y -= fl; y -= fl;
@@ -107,50 +107,50 @@ namespace omath
} }
// Basic vector operations // Basic vector operations
[[nodiscard]] Type distance_to(const Vector2& other) const [[nodiscard]] Type distance_to(const Vector2& other) const noexcept
{ {
return std::sqrt(distance_to_sqr(other)); return std::sqrt(distance_to_sqr(other));
} }
[[nodiscard]] constexpr Type distance_to_sqr(const Vector2& other) const [[nodiscard]] constexpr Type distance_to_sqr(const Vector2& other) const noexcept
{ {
return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y); return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
} }
[[nodiscard]] constexpr Type dot(const Vector2& other) const [[nodiscard]] constexpr Type dot(const Vector2& other) const noexcept
{ {
return x * other.x + y * other.y; return x * other.x + y * other.y;
} }
#ifndef _MSC_VER #ifndef _MSC_VER
[[nodiscard]] constexpr Type length() const [[nodiscard]] constexpr Type length() const noexcept
{ {
return std::hypot(this->x, this->y); return std::hypot(this->x, this->y);
} }
[[nodiscard]] constexpr Vector2 normalized() const [[nodiscard]] constexpr Vector2 normalized() const noexcept
{ {
const Type len = length(); const Type len = length();
return len > 0.f ? *this / len : *this; return len > 0.f ? *this / len : *this;
} }
#else #else
[[nodiscard]] Type length() const [[nodiscard]] Type length() const noexcept
{ {
return std::hypot(x, y); return std::hypot(x, y);
} }
[[nodiscard]] Vector2 normalized() const [[nodiscard]] Vector2 normalized() const noexcept
{ {
const Type len = length(); const Type len = length();
return len > 0.f ? *this / len : *this; return len > 0.f ? *this / len : *this;
} }
#endif #endif
[[nodiscard]] constexpr Type length_sqr() const [[nodiscard]] constexpr Type length_sqr() const noexcept
{ {
return x * x + y * y; return x * x + y * y;
} }
constexpr Vector2& abs() constexpr Vector2& abs() noexcept
{ {
// FIXME: Replace with std::abs, if it will become constexprable // FIXME: Replace with std::abs, if it will become constexprable
x = x < 0 ? -x : x; x = x < 0 ? -x : x;
@@ -158,47 +158,47 @@ namespace omath
return *this; return *this;
} }
[[nodiscard]] constexpr Vector2 operator-() const [[nodiscard]] constexpr Vector2 operator-() const noexcept
{ {
return {-x, -y}; return {-x, -y};
} }
// Binary arithmetic operators // Binary arithmetic operators
[[nodiscard]] constexpr Vector2 operator+(const Vector2& v) const [[nodiscard]] constexpr Vector2 operator+(const Vector2& v) const noexcept
{ {
return {x + v.x, y + v.y}; return {x + v.x, y + v.y};
} }
[[nodiscard]] constexpr Vector2 operator-(const Vector2& v) const [[nodiscard]] constexpr Vector2 operator-(const Vector2& v) const noexcept
{ {
return {x - v.x, y - v.y}; return {x - v.x, y - v.y};
} }
[[nodiscard]] constexpr Vector2 operator*(const float fl) const [[nodiscard]] constexpr Vector2 operator*(const float fl) const noexcept
{ {
return {x * fl, y * fl}; return {x * fl, y * fl};
} }
[[nodiscard]] constexpr Vector2 operator/(const float fl) const [[nodiscard]] constexpr Vector2 operator/(const float fl) const noexcept
{ {
return {x / fl, y / fl}; return {x / fl, y / fl};
} }
// Sum of elements // Sum of elements
[[nodiscard]] constexpr Type sum() const [[nodiscard]] constexpr Type sum() const noexcept
{ {
return x + y; return x + y;
} }
[[nodiscard]] [[nodiscard]]
constexpr std::tuple<Type, Type> as_tuple() const constexpr std::tuple<Type, Type> as_tuple() const noexcept
{ {
return std::make_tuple(x, y); return std::make_tuple(x, y);
} }
#ifdef OMATH_IMGUI_INTEGRATION #ifdef OMATH_IMGUI_INTEGRATION
[[nodiscard]] [[nodiscard]]
ImVec2 to_im_vec2() const ImVec2 to_im_vec2() const noexcept
{ {
return {static_cast<float>(this->x), static_cast<float>(this->y)}; return {static_cast<float>(this->x), static_cast<float>(this->y)};
} }

View File

@@ -24,22 +24,22 @@ namespace omath
{ {
public: public:
Type z = static_cast<Type>(0); Type z = static_cast<Type>(0);
constexpr Vector3(const Type& x, const Type& y, const Type& z): Vector2<Type>(x, y), z(z) constexpr Vector3(const Type& x, const Type& y, const Type& z) noexcept: Vector2<Type>(x, y), z(z)
{ {
} }
constexpr Vector3(): Vector2<Type>() {}; constexpr Vector3() noexcept: Vector2<Type>() {};
[[nodiscard]] constexpr bool operator==(const Vector3& src) const [[nodiscard]] constexpr bool operator==(const Vector3& src) const noexcept
{ {
return Vector2<Type>::operator==(src) && (src.z == z); return Vector2<Type>::operator==(src) && (src.z == z);
} }
[[nodiscard]] constexpr bool operator!=(const Vector3& src) const [[nodiscard]] constexpr bool operator!=(const Vector3& src) const noexcept
{ {
return !(*this == src); return !(*this == src);
} }
constexpr Vector3& operator+=(const Vector3& v) constexpr Vector3& operator+=(const Vector3& v) noexcept
{ {
Vector2<Type>::operator+=(v); Vector2<Type>::operator+=(v);
z += v.z; z += v.z;
@@ -47,7 +47,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator-=(const Vector3& v) constexpr Vector3& operator-=(const Vector3& v) noexcept
{ {
Vector2<Type>::operator-=(v); Vector2<Type>::operator-=(v);
z -= v.z; z -= v.z;
@@ -55,7 +55,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator*=(const float fl) constexpr Vector3& operator*=(const float fl) noexcept
{ {
Vector2<Type>::operator*=(fl); Vector2<Type>::operator*=(fl);
z *= fl; z *= fl;
@@ -63,7 +63,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator*=(const Vector3& v) constexpr Vector3& operator*=(const Vector3& v) noexcept
{ {
Vector2<Type>::operator*=(v); Vector2<Type>::operator*=(v);
z *= v.z; z *= v.z;
@@ -71,7 +71,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator/=(const Vector3& v) constexpr Vector3& operator/=(const Vector3& v) noexcept
{ {
Vector2<Type>::operator/=(v); Vector2<Type>::operator/=(v);
z /= v.z; z /= v.z;
@@ -79,7 +79,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator+=(const float fl) constexpr Vector3& operator+=(const float fl) noexcept
{ {
Vector2<Type>::operator+=(fl); Vector2<Type>::operator+=(fl);
z += fl; z += fl;
@@ -87,7 +87,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator/=(const float fl) constexpr Vector3& operator/=(const float fl) noexcept
{ {
Vector2<Type>::operator/=(fl); Vector2<Type>::operator/=(fl);
z /= fl; z /= fl;
@@ -95,7 +95,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& operator-=(const float fl) constexpr Vector3& operator-=(const float fl) noexcept
{ {
Vector2<Type>::operator-=(fl); Vector2<Type>::operator-=(fl);
z -= fl; z -= fl;
@@ -103,7 +103,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector3& abs() constexpr Vector3& abs() noexcept
{ {
Vector2<Type>::abs(); Vector2<Type>::abs();
z = z < 0.f ? -z : z; z = z < 0.f ? -z : z;
@@ -111,12 +111,12 @@ namespace omath
return *this; return *this;
} }
[[nodiscard]] constexpr Type distance_to_sqr(const Vector3& other) const [[nodiscard]] constexpr Type distance_to_sqr(const Vector3& other) const noexcept
{ {
return (*this - other).length_sqr(); return (*this - other).length_sqr();
} }
[[nodiscard]] constexpr Type dot(const Vector3& other) const [[nodiscard]] constexpr Type dot(const Vector3& other) const noexcept
{ {
return Vector2<Type>::dot(other) + z * other.z; return Vector2<Type>::dot(other) + z * other.z;
} }
@@ -142,80 +142,80 @@ namespace omath
return length_value != 0 ? *this / length_value : *this; return length_value != 0 ? *this / length_value : *this;
} }
#else #else
[[nodiscard]] Type length() const [[nodiscard]] Type length() const noexcept
{ {
return std::hypot(this->x, this->y, z); return std::hypot(this->x, this->y, z);
} }
[[nodiscard]] Vector3 normalized() const [[nodiscard]] Vector3 normalized() const noexcept
{ {
const Type len = this->length(); const Type len = this->length();
return len != 0 ? *this / len : *this; return len != 0 ? *this / len : *this;
} }
[[nodiscard]] Type length_2d() const [[nodiscard]] Type length_2d() const noexcept
{ {
return Vector2<Type>::length(); return Vector2<Type>::length();
} }
[[nodiscard]] Type distance_to(const Vector3& vOther) const [[nodiscard]] Type distance_to(const Vector3& vOther) const noexcept
{ {
return (*this - vOther).length(); return (*this - vOther).length();
} }
#endif #endif
[[nodiscard]] constexpr Type length_sqr() const [[nodiscard]] constexpr Type length_sqr() const noexcept
{ {
return Vector2<Type>::length_sqr() + z * z; return Vector2<Type>::length_sqr() + z * z;
} }
[[nodiscard]] constexpr Vector3 operator-() const [[nodiscard]] constexpr Vector3 operator-() const noexcept
{ {
return {-this->x, -this->y, -z}; return {-this->x, -this->y, -z};
} }
[[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const [[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const noexcept
{ {
return {this->x + v.x, this->y + v.y, z + v.z}; return {this->x + v.x, this->y + v.y, z + v.z};
} }
[[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const [[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const noexcept
{ {
return {this->x - v.x, this->y - v.y, z - v.z}; return {this->x - v.x, this->y - v.y, z - v.z};
} }
[[nodiscard]] constexpr Vector3 operator*(const float fl) const [[nodiscard]] constexpr Vector3 operator*(const float fl) const noexcept
{ {
return {this->x * fl, this->y * fl, z * fl}; return {this->x * fl, this->y * fl, z * fl};
} }
[[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const [[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const noexcept
{ {
return {this->x * v.x, this->y * v.y, z * v.z}; return {this->x * v.x, this->y * v.y, z * v.z};
} }
[[nodiscard]] constexpr Vector3 operator/(const float fl) const [[nodiscard]] constexpr Vector3 operator/(const float fl) const noexcept
{ {
return {this->x / fl, this->y / fl, z / fl}; return {this->x / fl, this->y / fl, z / fl};
} }
[[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const [[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const noexcept
{ {
return {this->x / v.x, this->y / v.y, z / v.z}; return {this->x / v.x, this->y / v.y, z / v.z};
} }
[[nodiscard]] constexpr Vector3 cross(const Vector3& v) const [[nodiscard]] constexpr Vector3 cross(const Vector3& v) const noexcept
{ {
return {this->y * v.z - z * v.y, z * v.x - this->x * v.z, this->x * v.y - this->y * v.x}; return {this->y * v.z - z * v.y, z * v.x - this->x * v.z, this->x * v.y - this->y * v.x};
} }
[[nodiscard]] constexpr Type sum() const [[nodiscard]] constexpr Type sum() const noexcept
{ {
return sum_2d() + z; return sum_2d() + z;
} }
[[nodiscard]] std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error> [[nodiscard]] std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
angle_between(const Vector3& other) const angle_between(const Vector3& other) const noexcept
{ {
const auto bottom = length() * other.length(); const auto bottom = length() * other.length();
@@ -225,7 +225,7 @@ namespace omath
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::from_radians(std::acos(dot(other) / bottom)); return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::from_radians(std::acos(dot(other) / bottom));
} }
[[nodiscard]] bool is_perpendicular(const Vector3& other) const [[nodiscard]] bool is_perpendicular(const Vector3& other) const noexcept
{ {
if (const auto angle = angle_between(other)) if (const auto angle = angle_between(other))
return angle->as_degrees() == 90.f; return angle->as_degrees() == 90.f;
@@ -233,17 +233,17 @@ namespace omath
return false; return false;
} }
[[nodiscard]] constexpr Type sum_2d() const [[nodiscard]] constexpr Type sum_2d() const noexcept
{ {
return Vector2<Type>::sum(); return Vector2<Type>::sum();
} }
[[nodiscard]] constexpr std::tuple<Type, Type, Type> as_tuple() const [[nodiscard]] constexpr std::tuple<Type, Type, Type> as_tuple() const noexcept
{ {
return std::make_tuple(this->x, this->y, z); return std::make_tuple(this->x, this->y, z);
} }
[[nodiscard]] Vector3 view_angle_to(const Vector3& other) const [[nodiscard]] Vector3 view_angle_to(const Vector3& other) const noexcept
{ {
const float distance = distance_to(other); const float distance = distance_to(other);
const auto delta = other - *this; const auto delta = other - *this;

View File

@@ -17,21 +17,21 @@ namespace omath
constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w): Vector3<Type>(x, y, z), w(w) constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w): Vector3<Type>(x, y, z), w(w)
{ {
} }
constexpr Vector4(): Vector3<Type>(), w(0) {}; constexpr Vector4() noexcept : Vector3<Type>(), w(0) {};
[[nodiscard]] [[nodiscard]]
constexpr bool operator==(const Vector4& src) const constexpr bool operator==(const Vector4& src) const noexcept
{ {
return Vector3<Type>::operator==(src) && w == src.w; return Vector3<Type>::operator==(src) && w == src.w;
} }
[[nodiscard]] [[nodiscard]]
constexpr bool operator!=(const Vector4& src) const constexpr bool operator!=(const Vector4& src) const noexcept
{ {
return !(*this == src); return !(*this == src);
} }
constexpr Vector4& operator+=(const Vector4& v) constexpr Vector4& operator+=(const Vector4& v) noexcept
{ {
Vector3<Type>::operator+=(v); Vector3<Type>::operator+=(v);
w += v.w; w += v.w;
@@ -39,7 +39,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator-=(const Vector4& v) constexpr Vector4& operator-=(const Vector4& v) noexcept
{ {
Vector3<Type>::operator-=(v); Vector3<Type>::operator-=(v);
w -= v.w; w -= v.w;
@@ -47,7 +47,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator*=(const float scalar) constexpr Vector4& operator*=(const float scalar) noexcept
{ {
Vector3<Type>::operator*=(scalar); Vector3<Type>::operator*=(scalar);
w *= scalar; w *= scalar;
@@ -55,7 +55,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator*=(const Vector4& v) constexpr Vector4& operator*=(const Vector4& v) noexcept
{ {
Vector3<Type>::operator*=(v); Vector3<Type>::operator*=(v);
w *= v.w; w *= v.w;
@@ -63,7 +63,7 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator/=(const float scalar) constexpr Vector4& operator/=(const float scalar) noexcept
{ {
Vector3<Type>::operator/=(scalar); Vector3<Type>::operator/=(scalar);
w /= scalar; w /= scalar;
@@ -71,36 +71,36 @@ namespace omath
return *this; return *this;
} }
constexpr Vector4& operator/=(const Vector4& v) constexpr Vector4& operator/=(const Vector4& v) noexcept
{ {
Vector3<Type>::operator/=(v); Vector3<Type>::operator/=(v);
w /= v.w; w /= v.w;
return *this; return *this;
} }
[[nodiscard]] constexpr Type length_sqr() const [[nodiscard]] constexpr Type length_sqr() const noexcept
{ {
return Vector3<Type>::length_sqr() + w * w; return Vector3<Type>::length_sqr() + w * w;
} }
[[nodiscard]] constexpr Type dot(const Vector4& other) const [[nodiscard]] constexpr Type dot(const Vector4& other) const noexcept
{ {
return Vector3<Type>::dot(other) + w * other.w; return Vector3<Type>::dot(other) + w * other.w;
} }
[[nodiscard]] Vector3<Type> length() const [[nodiscard]] Vector3<Type> length() const noexcept
{ {
return std::sqrt(length_sqr()); return std::sqrt(length_sqr());
} }
constexpr Vector4& abs() constexpr Vector4& abs() noexcept
{ {
Vector3<Type>::abs(); Vector3<Type>::abs();
w = w < 0.f ? -w : w; w = w < 0.f ? -w : w;
return *this; return *this;
} }
constexpr Vector4& clamp(const Type& min, const Type& max) constexpr Vector4& clamp(const Type& min, const Type& max) noexcept
{ {
this->x = std::clamp(this->x, min, max); this->x = std::clamp(this->x, min, max);
this->y = std::clamp(this->y, min, max); this->y = std::clamp(this->y, min, max);
@@ -110,56 +110,56 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator-() const constexpr Vector4 operator-() const noexcept
{ {
return {-this->x, -this->y, -this->z, -w}; return {-this->x, -this->y, -this->z, -w};
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator+(const Vector4& v) const constexpr Vector4 operator+(const Vector4& v) const noexcept
{ {
return {this->x + v.x, this->y + v.y, this->z + v.z, w + v.w}; return {this->x + v.x, this->y + v.y, this->z + v.z, w + v.w};
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator-(const Vector4& v) const constexpr Vector4 operator-(const Vector4& v) const noexcept
{ {
return {this->x - v.x, this->y - v.y, this->z - v.z, w - v.w}; return {this->x - v.x, this->y - v.y, this->z - v.z, w - v.w};
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator*(const Type& scalar) const constexpr Vector4 operator*(const Type& scalar) const noexcept
{ {
return {this->x * scalar, this->y * scalar, this->z * scalar, w * scalar}; return {this->x * scalar, this->y * scalar, this->z * scalar, w * scalar};
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator*(const Vector4& v) const constexpr Vector4 operator*(const Vector4& v) const noexcept
{ {
return {this->x * v.x, this->y * v.y, this->z * v.z, w * v.w}; return {this->x * v.x, this->y * v.y, this->z * v.z, w * v.w};
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator/(const Type& scalar) const constexpr Vector4 operator/(const Type& scalar) const noexcept
{ {
return {this->x / scalar, this->y / scalar, this->z / scalar, w / scalar}; return {this->x / scalar, this->y / scalar, this->z / scalar, w / scalar};
} }
[[nodiscard]] [[nodiscard]]
constexpr Vector4 operator/(const Vector4& v) const constexpr Vector4 operator/(const Vector4& v) const noexcept
{ {
return {this->x / v.x, this->y / v.y, this->z / v.z, w / v.w}; return {this->x / v.x, this->y / v.y, this->z / v.z, w / v.w};
} }
[[nodiscard]] [[nodiscard]]
constexpr Type sum() const constexpr Type sum() const noexcept
{ {
return Vector3<Type>::sum() + w; return Vector3<Type>::sum() + w;
} }
#ifdef OMATH_IMGUI_INTEGRATION #ifdef OMATH_IMGUI_INTEGRATION
[[nodiscard]] [[nodiscard]]
ImVec4 to_im_vec4() const ImVec4 to_im_vec4() const noexcept
{ {
return { return {
static_cast<float>(this->x), static_cast<float>(this->x),