added noexcept for color and angles

This commit is contained in:
2025-05-04 19:16:49 +03:00
parent 9a38d47b0d
commit 6749f9f759
3 changed files with 36 additions and 36 deletions

View File

@@ -20,7 +20,7 @@ namespace omath
class Angle class Angle
{ {
Type m_angle; Type m_angle;
constexpr explicit Angle(const Type& degrees) constexpr explicit Angle(const Type& degrees) noexcept
{ {
if constexpr (flags == AngleFlags::Normalized) if constexpr (flags == AngleFlags::Normalized)
m_angle = angles::wrap_angle(degrees, min, max); m_angle = angles::wrap_angle(degrees, min, max);
@@ -36,68 +36,68 @@ namespace omath
public: public:
[[nodiscard]] [[nodiscard]]
constexpr static Angle from_degrees(const Type& degrees) constexpr static Angle from_degrees(const Type& degrees) noexcept
{ {
return Angle{degrees}; return Angle{degrees};
} }
constexpr Angle(): m_angle(0) constexpr Angle() noexcept: m_angle(0)
{ {
} }
[[nodiscard]] [[nodiscard]]
constexpr static Angle from_radians(const Type& degrees) constexpr static Angle from_radians(const Type& degrees) noexcept
{ {
return Angle{angles::radians_to_degrees<Type>(degrees)}; return Angle{angles::radians_to_degrees<Type>(degrees)};
} }
[[nodiscard]] [[nodiscard]]
constexpr const Type& operator*() const constexpr const Type& operator*() const noexcept
{ {
return m_angle; return m_angle;
} }
[[nodiscard]] [[nodiscard]]
constexpr Type as_degrees() const constexpr Type as_degrees() const noexcept
{ {
return m_angle; return m_angle;
} }
[[nodiscard]] [[nodiscard]]
constexpr Type as_radians() const constexpr Type as_radians() const noexcept
{ {
return angles::degrees_to_radians(m_angle); return angles::degrees_to_radians(m_angle);
} }
[[nodiscard]] [[nodiscard]]
Type sin() const Type sin() const noexcept
{ {
return std::sin(as_radians()); return std::sin(as_radians());
} }
[[nodiscard]] [[nodiscard]]
Type cos() const Type cos() const noexcept
{ {
return std::cos(as_radians()); return std::cos(as_radians());
} }
[[nodiscard]] [[nodiscard]]
Type tan() const Type tan() const noexcept
{ {
return std::tan(as_radians()); return std::tan(as_radians());
} }
[[nodiscard]] [[nodiscard]]
Type atan() const Type atan() const noexcept
{ {
return std::atan(as_radians()); return std::atan(as_radians());
} }
[[nodiscard]] [[nodiscard]]
Type cot() const Type cot() const noexcept
{ {
return cos() / sin(); return cos() / sin();
} }
constexpr Angle& operator+=(const Angle& other) constexpr Angle& operator+=(const Angle& other) noexcept
{ {
if constexpr (flags == AngleFlags::Normalized) if constexpr (flags == AngleFlags::Normalized)
m_angle = angles::wrap_angle(m_angle + other.m_angle, min, max); m_angle = angles::wrap_angle(m_angle + other.m_angle, min, max);
@@ -114,15 +114,15 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr std::partial_ordering operator<=>(const Angle& other) const = default; constexpr std::partial_ordering operator<=>(const Angle& other) const noexcept = default;
constexpr Angle& operator-=(const Angle& other) constexpr Angle& operator-=(const Angle& other) noexcept
{ {
return operator+=(-other); return operator+=(-other);
} }
[[nodiscard]] [[nodiscard]]
constexpr Angle& operator+(const Angle& other) constexpr Angle& operator+(const Angle& other) noexcept
{ {
if constexpr (flags == AngleFlags::Normalized) if constexpr (flags == AngleFlags::Normalized)
return {angles::wrap_angle(m_angle + other.m_angle, min, max)}; return {angles::wrap_angle(m_angle + other.m_angle, min, max)};
@@ -137,13 +137,13 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr Angle& operator-(const Angle& other) constexpr Angle& operator-(const Angle& other) noexcept
{ {
return operator+(-other); return operator+(-other);
} }
[[nodiscard]] [[nodiscard]]
constexpr Angle operator-() const constexpr Angle operator-() const noexcept
{ {
return Angle{-m_angle}; return Angle{-m_angle};
} }

View File

@@ -10,21 +10,21 @@ namespace omath::angles
{ {
template<class Type> template<class Type>
requires std::is_floating_point_v<Type> requires std::is_floating_point_v<Type>
[[nodiscard]] constexpr Type radians_to_degrees(const Type& radians) [[nodiscard]] constexpr Type radians_to_degrees(const Type& radians) noexcept
{ {
return radians * (static_cast<Type>(180) / std::numbers::pi_v<Type>); return radians * (static_cast<Type>(180) / std::numbers::pi_v<Type>);
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type> requires std::is_floating_point_v<Type>
[[nodiscard]] constexpr Type degrees_to_radians(const Type& degrees) [[nodiscard]] constexpr Type degrees_to_radians(const Type& degrees) noexcept
{ {
return degrees * (std::numbers::pi_v<Type> / static_cast<Type>(180)); return degrees * (std::numbers::pi_v<Type> / static_cast<Type>(180));
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type> requires std::is_floating_point_v<Type>
[[nodiscard]] Type horizontal_fov_to_vertical(const Type& horizontal_fov, const Type& aspect) [[nodiscard]] Type horizontal_fov_to_vertical(const Type& horizontal_fov, const Type& aspect) noexcept
{ {
const auto fov_rad = degrees_to_radians(horizontal_fov); const auto fov_rad = degrees_to_radians(horizontal_fov);
@@ -35,19 +35,19 @@ namespace omath::angles
template<class Type> template<class Type>
requires std::is_floating_point_v<Type> requires std::is_floating_point_v<Type>
[[nodiscard]] Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect) [[nodiscard]] Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect) noexcept
{ {
const auto fov_as_radians = degrees_to_radians(vertical_fov); const auto fov_as_radians = degrees_to_radians(vertical_fov);
const auto horizontal_fov const auto horizontal_fov =
= static_cast<Type>(2) * std::atan(std::tan(fov_as_radians / static_cast<Type>(2)) * aspect); static_cast<Type>(2) * std::atan(std::tan(fov_as_radians / static_cast<Type>(2)) * aspect);
return radians_to_degrees(horizontal_fov); return radians_to_degrees(horizontal_fov);
} }
template<class Type> template<class Type>
requires std::is_arithmetic_v<Type> requires std::is_arithmetic_v<Type>
[[nodiscard]] Type wrap_angle(const Type& angle, const Type& min, const Type& max) [[nodiscard]] Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept
{ {
if (angle <= max && angle >= min) if (angle <= max && angle >= min)
return angle; return angle;

View File

@@ -28,20 +28,20 @@ namespace omath
class Color final : public Vector4<float> class Color final : public Vector4<float>
{ {
public: public:
constexpr Color(const float r, const float g, const float b, const float a): Vector4(r, g, b, a) constexpr Color(const float r, const float g, const float b, const float a) noexcept: Vector4(r, g, b, a)
{ {
clamp(0.f, 1.f); clamp(0.f, 1.f);
} }
constexpr explicit Color() = default; constexpr explicit Color() noexcept = default;
[[nodiscard]] [[nodiscard]]
constexpr static Color from_rgba(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) constexpr static Color from_rgba(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) noexcept
{ {
return Color{Vector4(r, g, b, a) / 255.f}; return Color{Vector4(r, g, b, a) / 255.f};
} }
[[nodiscard]] [[nodiscard]]
constexpr static Color from_hsv(float hue, const float saturation, const float value) constexpr static Color from_hsv(float hue, const float saturation, const float value) noexcept
{ {
float r{}, g{}, b{}; float r{}, g{}, b{};
@@ -82,13 +82,13 @@ namespace omath
} }
[[nodiscard]] [[nodiscard]]
constexpr static Color from_hsv(const Hsv& hsv) constexpr static Color from_hsv(const Hsv& hsv) noexcept
{ {
return from_hsv(hsv.hue, hsv.saturation, hsv.value); return from_hsv(hsv.hue, hsv.saturation, hsv.value);
} }
[[nodiscard]] [[nodiscard]]
constexpr Hsv to_hsv() const constexpr Hsv to_hsv() const noexcept
{ {
Hsv hsv_data; Hsv hsv_data;
@@ -120,11 +120,11 @@ namespace omath
return hsv_data; return hsv_data;
} }
constexpr explicit Color(const Vector4& vec): Vector4(vec) constexpr explicit Color(const Vector4& vec) noexcept: Vector4(vec)
{ {
clamp(0.f, 1.f); clamp(0.f, 1.f);
} }
constexpr void set_hue(const float hue) constexpr void set_hue(const float hue) noexcept
{ {
auto hsv = to_hsv(); auto hsv = to_hsv();
hsv.hue = hue; hsv.hue = hue;
@@ -132,7 +132,7 @@ namespace omath
*this = from_hsv(hsv); *this = from_hsv(hsv);
} }
constexpr void set_saturation(const float saturation) constexpr void set_saturation(const float saturation) noexcept
{ {
auto hsv = to_hsv(); auto hsv = to_hsv();
hsv.saturation = saturation; hsv.saturation = saturation;
@@ -140,7 +140,7 @@ namespace omath
*this = from_hsv(hsv); *this = from_hsv(hsv);
} }
constexpr void set_value(const float value) constexpr void set_value(const float value) noexcept
{ {
auto hsv = to_hsv(); auto hsv = to_hsv();
hsv.value = value; hsv.value = value;
@@ -148,7 +148,7 @@ namespace omath
*this = from_hsv(hsv); *this = from_hsv(hsv);
} }
[[nodiscard]] [[nodiscard]]
constexpr Color blend(const Color& other, float ratio) const constexpr Color blend(const Color& other, float ratio) const noexcept
{ {
ratio = std::clamp(ratio, 0.f, 1.f); ratio = std::clamp(ratio, 0.f, 1.f);
return Color(*this * (1.f - ratio) + other * ratio); return Color(*this * (1.f - ratio) + other * ratio);