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

View File

@@ -10,21 +10,21 @@ namespace omath::angles
{
template<class 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>);
}
template<class 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));
}
template<class 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);
@@ -35,19 +35,19 @@ namespace omath::angles
template<class 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 horizontal_fov
= static_cast<Type>(2) * std::atan(std::tan(fov_as_radians / static_cast<Type>(2)) * aspect);
const auto horizontal_fov =
static_cast<Type>(2) * std::atan(std::tan(fov_as_radians / static_cast<Type>(2)) * aspect);
return radians_to_degrees(horizontal_fov);
}
template<class 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)
return angle;

View File

@@ -28,20 +28,20 @@ namespace omath
class Color final : public Vector4<float>
{
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);
}
constexpr explicit Color() = default;
constexpr explicit Color() noexcept = default;
[[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};
}
[[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{};
@@ -82,13 +82,13 @@ namespace omath
}
[[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);
}
[[nodiscard]]
constexpr Hsv to_hsv() const
constexpr Hsv to_hsv() const noexcept
{
Hsv hsv_data;
@@ -120,11 +120,11 @@ namespace omath
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);
}
constexpr void set_hue(const float hue)
constexpr void set_hue(const float hue) noexcept
{
auto hsv = to_hsv();
hsv.hue = hue;
@@ -132,7 +132,7 @@ namespace omath
*this = from_hsv(hsv);
}
constexpr void set_saturation(const float saturation)
constexpr void set_saturation(const float saturation) noexcept
{
auto hsv = to_hsv();
hsv.saturation = saturation;
@@ -140,7 +140,7 @@ namespace omath
*this = from_hsv(hsv);
}
constexpr void set_value(const float value)
constexpr void set_value(const float value) noexcept
{
auto hsv = to_hsv();
hsv.value = value;
@@ -148,7 +148,7 @@ namespace omath
*this = from_hsv(hsv);
}
[[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);
return Color(*this * (1.f - ratio) + other * ratio);