From 9a38d47b0d46b05ba000cbfa292c1d9c79ed2fa0 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 4 May 2025 19:13:26 +0300 Subject: [PATCH] added noexcept for vector types --- include/omath/vector2.hpp | 56 ++++++++++++++++---------------- include/omath/vector3.hpp | 68 +++++++++++++++++++-------------------- include/omath/vector4.hpp | 46 +++++++++++++------------- 3 files changed, 85 insertions(+), 85 deletions(-) diff --git a/include/omath/vector2.hpp b/include/omath/vector2.hpp index 915b1d3..03a98cc 100644 --- a/include/omath/vector2.hpp +++ b/include/omath/vector2.hpp @@ -24,25 +24,25 @@ namespace omath // Constructors 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 [[nodiscard]] - constexpr bool operator==(const Vector2& src) const + constexpr bool operator==(const Vector2& src) const noexcept { return x == src.x && y == src.y; } [[nodiscard]] - constexpr bool operator!=(const Vector2& src) const + constexpr bool operator!=(const Vector2& src) const noexcept { return !(*this == src); } // Compound assignment operators - constexpr Vector2& operator+=(const Vector2& v) + constexpr Vector2& operator+=(const Vector2& v) noexcept { x += v.x; y += v.y; @@ -50,7 +50,7 @@ namespace omath return *this; } - constexpr Vector2& operator-=(const Vector2& v) + constexpr Vector2& operator-=(const Vector2& v) noexcept { x -= v.x; y -= v.y; @@ -58,7 +58,7 @@ namespace omath return *this; } - constexpr Vector2& operator*=(const Vector2& v) + constexpr Vector2& operator*=(const Vector2& v) noexcept { x *= v.x; y *= v.y; @@ -66,7 +66,7 @@ namespace omath return *this; } - constexpr Vector2& operator/=(const Vector2& v) + constexpr Vector2& operator/=(const Vector2& v) noexcept { x /= v.x; y /= v.y; @@ -74,7 +74,7 @@ namespace omath return *this; } - constexpr Vector2& operator*=(const Type& fl) + constexpr Vector2& operator*=(const Type& fl) noexcept { x *= fl; y *= fl; @@ -82,7 +82,7 @@ namespace omath return *this; } - constexpr Vector2& operator/=(const Type& fl) + constexpr Vector2& operator/=(const Type& fl) noexcept { x /= fl; y /= fl; @@ -90,7 +90,7 @@ namespace omath return *this; } - constexpr Vector2& operator+=(const Type& fl) + constexpr Vector2& operator+=(const Type& fl) noexcept { x += fl; y += fl; @@ -98,7 +98,7 @@ namespace omath return *this; } - constexpr Vector2& operator-=(const Type& fl) + constexpr Vector2& operator-=(const Type& fl) noexcept { x -= fl; y -= fl; @@ -107,50 +107,50 @@ namespace omath } // 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)); } - [[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); } - [[nodiscard]] constexpr Type dot(const Vector2& other) const + [[nodiscard]] constexpr Type dot(const Vector2& other) const noexcept { return x * other.x + y * other.y; } #ifndef _MSC_VER - [[nodiscard]] constexpr Type length() const + [[nodiscard]] constexpr Type length() const noexcept { return std::hypot(this->x, this->y); } - [[nodiscard]] constexpr Vector2 normalized() const + [[nodiscard]] constexpr Vector2 normalized() const noexcept { const Type len = length(); return len > 0.f ? *this / len : *this; } #else - [[nodiscard]] Type length() const + [[nodiscard]] Type length() const noexcept { return std::hypot(x, y); } - [[nodiscard]] Vector2 normalized() const + [[nodiscard]] Vector2 normalized() const noexcept { const Type len = length(); return len > 0.f ? *this / len : *this; } #endif - [[nodiscard]] constexpr Type length_sqr() const + [[nodiscard]] constexpr Type length_sqr() const noexcept { return x * x + y * y; } - constexpr Vector2& abs() + constexpr Vector2& abs() noexcept { // FIXME: Replace with std::abs, if it will become constexprable x = x < 0 ? -x : x; @@ -158,47 +158,47 @@ namespace omath return *this; } - [[nodiscard]] constexpr Vector2 operator-() const + [[nodiscard]] constexpr Vector2 operator-() const noexcept { return {-x, -y}; } // 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}; } - [[nodiscard]] constexpr Vector2 operator-(const Vector2& v) const + [[nodiscard]] constexpr Vector2 operator-(const Vector2& v) const noexcept { 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}; } - [[nodiscard]] constexpr Vector2 operator/(const float fl) const + [[nodiscard]] constexpr Vector2 operator/(const float fl) const noexcept { return {x / fl, y / fl}; } // Sum of elements - [[nodiscard]] constexpr Type sum() const + [[nodiscard]] constexpr Type sum() const noexcept { return x + y; } [[nodiscard]] - constexpr std::tuple as_tuple() const + constexpr std::tuple as_tuple() const noexcept { return std::make_tuple(x, y); } #ifdef OMATH_IMGUI_INTEGRATION [[nodiscard]] - ImVec2 to_im_vec2() const + ImVec2 to_im_vec2() const noexcept { return {static_cast(this->x), static_cast(this->y)}; } diff --git a/include/omath/vector3.hpp b/include/omath/vector3.hpp index d535168..fc30297 100644 --- a/include/omath/vector3.hpp +++ b/include/omath/vector3.hpp @@ -24,22 +24,22 @@ namespace omath { public: Type z = static_cast(0); - constexpr Vector3(const Type& x, const Type& y, const Type& z): Vector2(x, y), z(z) + constexpr Vector3(const Type& x, const Type& y, const Type& z) noexcept: Vector2(x, y), z(z) { } - constexpr Vector3(): Vector2() {}; + constexpr Vector3() noexcept: Vector2() {}; - [[nodiscard]] constexpr bool operator==(const Vector3& src) const + [[nodiscard]] constexpr bool operator==(const Vector3& src) const noexcept { return Vector2::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); } - constexpr Vector3& operator+=(const Vector3& v) + constexpr Vector3& operator+=(const Vector3& v) noexcept { Vector2::operator+=(v); z += v.z; @@ -47,7 +47,7 @@ namespace omath return *this; } - constexpr Vector3& operator-=(const Vector3& v) + constexpr Vector3& operator-=(const Vector3& v) noexcept { Vector2::operator-=(v); z -= v.z; @@ -55,7 +55,7 @@ namespace omath return *this; } - constexpr Vector3& operator*=(const float fl) + constexpr Vector3& operator*=(const float fl) noexcept { Vector2::operator*=(fl); z *= fl; @@ -63,7 +63,7 @@ namespace omath return *this; } - constexpr Vector3& operator*=(const Vector3& v) + constexpr Vector3& operator*=(const Vector3& v) noexcept { Vector2::operator*=(v); z *= v.z; @@ -71,7 +71,7 @@ namespace omath return *this; } - constexpr Vector3& operator/=(const Vector3& v) + constexpr Vector3& operator/=(const Vector3& v) noexcept { Vector2::operator/=(v); z /= v.z; @@ -79,7 +79,7 @@ namespace omath return *this; } - constexpr Vector3& operator+=(const float fl) + constexpr Vector3& operator+=(const float fl) noexcept { Vector2::operator+=(fl); z += fl; @@ -87,7 +87,7 @@ namespace omath return *this; } - constexpr Vector3& operator/=(const float fl) + constexpr Vector3& operator/=(const float fl) noexcept { Vector2::operator/=(fl); z /= fl; @@ -95,7 +95,7 @@ namespace omath return *this; } - constexpr Vector3& operator-=(const float fl) + constexpr Vector3& operator-=(const float fl) noexcept { Vector2::operator-=(fl); z -= fl; @@ -103,7 +103,7 @@ namespace omath return *this; } - constexpr Vector3& abs() + constexpr Vector3& abs() noexcept { Vector2::abs(); z = z < 0.f ? -z : z; @@ -111,12 +111,12 @@ namespace omath 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(); } - [[nodiscard]] constexpr Type dot(const Vector3& other) const + [[nodiscard]] constexpr Type dot(const Vector3& other) const noexcept { return Vector2::dot(other) + z * other.z; } @@ -142,80 +142,80 @@ namespace omath return length_value != 0 ? *this / length_value : *this; } #else - [[nodiscard]] Type length() const + [[nodiscard]] Type length() const noexcept { return std::hypot(this->x, this->y, z); } - [[nodiscard]] Vector3 normalized() const + [[nodiscard]] Vector3 normalized() const noexcept { const Type len = this->length(); return len != 0 ? *this / len : *this; } - [[nodiscard]] Type length_2d() const + [[nodiscard]] Type length_2d() const noexcept { return Vector2::length(); } - [[nodiscard]] Type distance_to(const Vector3& vOther) const + [[nodiscard]] Type distance_to(const Vector3& vOther) const noexcept { return (*this - vOther).length(); } #endif - [[nodiscard]] constexpr Type length_sqr() const + [[nodiscard]] constexpr Type length_sqr() const noexcept { return Vector2::length_sqr() + z * z; } - [[nodiscard]] constexpr Vector3 operator-() const + [[nodiscard]] constexpr Vector3 operator-() const noexcept { 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}; } - [[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}; } - [[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}; } - [[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}; } - [[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}; } - [[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}; } - [[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}; } - [[nodiscard]] constexpr Type sum() const + [[nodiscard]] constexpr Type sum() const noexcept { return sum_2d() + z; } [[nodiscard]] std::expected, Vector3Error> - angle_between(const Vector3& other) const + angle_between(const Vector3& other) const noexcept { const auto bottom = length() * other.length(); @@ -225,7 +225,7 @@ namespace omath return Angle::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)) return angle->as_degrees() == 90.f; @@ -233,17 +233,17 @@ namespace omath return false; } - [[nodiscard]] constexpr Type sum_2d() const + [[nodiscard]] constexpr Type sum_2d() const noexcept { return Vector2::sum(); } - [[nodiscard]] constexpr std::tuple as_tuple() const + [[nodiscard]] constexpr std::tuple as_tuple() const noexcept { 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 auto delta = other - *this; diff --git a/include/omath/vector4.hpp b/include/omath/vector4.hpp index 36e0c0a..2f69989 100644 --- a/include/omath/vector4.hpp +++ b/include/omath/vector4.hpp @@ -17,21 +17,21 @@ namespace omath constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w): Vector3(x, y, z), w(w) { } - constexpr Vector4(): Vector3(), w(0) {}; + constexpr Vector4() noexcept : Vector3(), w(0) {}; [[nodiscard]] - constexpr bool operator==(const Vector4& src) const + constexpr bool operator==(const Vector4& src) const noexcept { return Vector3::operator==(src) && w == src.w; } [[nodiscard]] - constexpr bool operator!=(const Vector4& src) const + constexpr bool operator!=(const Vector4& src) const noexcept { return !(*this == src); } - constexpr Vector4& operator+=(const Vector4& v) + constexpr Vector4& operator+=(const Vector4& v) noexcept { Vector3::operator+=(v); w += v.w; @@ -39,7 +39,7 @@ namespace omath return *this; } - constexpr Vector4& operator-=(const Vector4& v) + constexpr Vector4& operator-=(const Vector4& v) noexcept { Vector3::operator-=(v); w -= v.w; @@ -47,7 +47,7 @@ namespace omath return *this; } - constexpr Vector4& operator*=(const float scalar) + constexpr Vector4& operator*=(const float scalar) noexcept { Vector3::operator*=(scalar); w *= scalar; @@ -55,7 +55,7 @@ namespace omath return *this; } - constexpr Vector4& operator*=(const Vector4& v) + constexpr Vector4& operator*=(const Vector4& v) noexcept { Vector3::operator*=(v); w *= v.w; @@ -63,7 +63,7 @@ namespace omath return *this; } - constexpr Vector4& operator/=(const float scalar) + constexpr Vector4& operator/=(const float scalar) noexcept { Vector3::operator/=(scalar); w /= scalar; @@ -71,36 +71,36 @@ namespace omath return *this; } - constexpr Vector4& operator/=(const Vector4& v) + constexpr Vector4& operator/=(const Vector4& v) noexcept { Vector3::operator/=(v); w /= v.w; return *this; } - [[nodiscard]] constexpr Type length_sqr() const + [[nodiscard]] constexpr Type length_sqr() const noexcept { return Vector3::length_sqr() + w * w; } - [[nodiscard]] constexpr Type dot(const Vector4& other) const + [[nodiscard]] constexpr Type dot(const Vector4& other) const noexcept { return Vector3::dot(other) + w * other.w; } - [[nodiscard]] Vector3 length() const + [[nodiscard]] Vector3 length() const noexcept { return std::sqrt(length_sqr()); } - constexpr Vector4& abs() + constexpr Vector4& abs() noexcept { Vector3::abs(); w = w < 0.f ? -w : w; 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->y = std::clamp(this->y, min, max); @@ -110,56 +110,56 @@ namespace omath } [[nodiscard]] - constexpr Vector4 operator-() const + constexpr Vector4 operator-() const noexcept { return {-this->x, -this->y, -this->z, -w}; } [[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}; } [[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}; } [[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}; } [[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}; } [[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}; } [[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}; } [[nodiscard]] - constexpr Type sum() const + constexpr Type sum() const noexcept { return Vector3::sum() + w; } #ifdef OMATH_IMGUI_INTEGRATION [[nodiscard]] - ImVec4 to_im_vec4() const + ImVec4 to_im_vec4() const noexcept { return { static_cast(this->x),