Compare commits

..

2 Commits

Author SHA1 Message Date
orange 20f99d85fc fix 2026-07-19 16:34:07 +03:00
orange de5e1e8200 improved prediction 2026-07-19 16:18:29 +03:00
16 changed files with 93 additions and 120 deletions
@@ -707,11 +707,7 @@ jobs:
- name: Build - name: Build
run: | run: |
if [[ "${{ matrix.msystem }}" == "MINGW32" ]]; then
cmake --build cmake-build/build/${{ matrix.preset }} --target unit_tests omath --parallel 1
else
cmake --build cmake-build/build/${{ matrix.preset }} --target unit_tests omath cmake --build cmake-build/build/${{ matrix.preset }} --target unit_tests omath
fi
- name: Run unit_tests.exe - name: Run unit_tests.exe
run: | run: |
+10 -12
View File
@@ -3,8 +3,8 @@
> Header: `omath/trigonometry/angle.hpp` > Header: `omath/trigonometry/angle.hpp`
> Namespace: `omath` > Namespace: `omath`
> Template: `Angle<Type = float, min = 0, max = 360, flags = AngleFlags::Normalized>` > Template: `Angle<Type = float, min = 0, max = 360, flags = AngleFlags::Normalized>`
> Requires: `std::is_floating_point_v<Type>` > Requires: `std::is_arithmetic_v<Type>`
> Formatters: `std::formatter` for `char` and `wchar_t` → `"{}deg"` > Formatters: `std::formatter` for `char`, `wchar_t`, `char8_t` → `"{}deg"`
--- ---
@@ -14,7 +14,7 @@
Two behaviors via `AngleFlags`: Two behaviors via `AngleFlags`:
* `AngleFlags::Normalized` (default): values are wrapped into `[min, max)` using `angles::wrap_angle`. * `AngleFlags::Normalized` (default): values are wrapped into `[min, max]` using `angles::wrap_angle`.
* `AngleFlags::Clamped`: values are clamped to `[min, max]` using `std::clamp`. * `AngleFlags::Clamped`: values are clamped to `[min, max]` using `std::clamp`.
--- ---
@@ -28,16 +28,12 @@ enum class AngleFlags { Normalized = 0, Clamped = 1 };
template<class Type = float, Type min = Type(0), Type max = Type(360), template<class Type = float, Type min = Type(0), Type max = Type(360),
AngleFlags flags = AngleFlags::Normalized> AngleFlags flags = AngleFlags::Normalized>
requires std::is_floating_point_v<Type> requires std::is_arithmetic_v<Type>
class Angle { class Angle {
public: public:
// Construction // Construction
static constexpr Angle from_degrees(const Type& deg) noexcept; static constexpr Angle from_degrees(const Type& deg) noexcept;
static constexpr Angle from_radians(const Type& rad) noexcept; static constexpr Angle from_radians(const Type& rad) noexcept;
static constexpr Angle from_asin(const Type& value) noexcept;
static constexpr Angle from_acos(const Type& value) noexcept;
static constexpr Angle from_atan(const Type& value) noexcept;
static constexpr Angle from_atan2(const Type& y, const Type& x) noexcept;
constexpr Angle() noexcept; // 0 deg, adjusted by flags/range constexpr Angle() noexcept; // 0 deg, adjusted by flags/range
// Accessors / conversions (degrees stored internally) // Accessors / conversions (degrees stored internally)
@@ -49,9 +45,10 @@ public:
Type sin() const noexcept; Type sin() const noexcept;
Type cos() const noexcept; Type cos() const noexcept;
Type tan() const noexcept; Type tan() const noexcept;
Type atan() const noexcept; // atan(as_radians()) (rarely used)
Type cot() const noexcept; // cos()/sin() (watch sin≈0) Type cot() const noexcept; // cos()/sin() (watch sin≈0)
// Arithmetic (wraps or clamps per flags and configured range) // Arithmetic (wraps or clamps per flags and [min,max])
constexpr Angle& operator+=(const Angle&) noexcept; constexpr Angle& operator+=(const Angle&) noexcept;
constexpr Angle& operator-=(const Angle&) noexcept; constexpr Angle& operator-=(const Angle&) noexcept;
constexpr Angle operator+(const Angle&) noexcept; constexpr Angle operator+(const Angle&) noexcept;
@@ -71,7 +68,7 @@ public:
std::format("{}", Angle<float>::from_degrees(45)); // "45deg" std::format("{}", Angle<float>::from_degrees(45)); // "45deg"
``` ```
Formatters exist for `char` and `wchar_t`. Formatters exist for `char`, `wchar_t`, and `char8_t`.
--- ---
@@ -119,9 +116,10 @@ float deg = *yaw; // same as yaw.as_degrees()
## Semantics & notes ## Semantics & notes
* **Storage & units:** Internally stores **degrees** (`Type m_angle`). `as_radians()`/`from_radians()` use the project helpers in `omath::angles`. * **Storage & units:** Internally stores **degrees** (`Type m_angle`). `as_radians()`/`from_radians()` use the project helpers in `omath::angles`.
* **Arithmetic honors policy:** `operator+=`/`-=` and the binary `+`/`-` apply **wrap** or **clamp**, mirroring construction behavior. * **Arithmetic honors policy:** `operator+=`/`-=` and the binary `+`/`-` apply **wrap** or **clamp** in `[min,max]`, mirroring construction behavior.
* **`atan()`**: returns `std::atan(as_radians())` (the arctangent of the *radian value*). This is mathematically unusual for an angle type and is rarely useful; prefer `tan()`/`atan2` in client code when solving geometry problems.
* **`cot()` / `tan()` singularities:** Near multiples where `sin() ≈ 0` or `cos() ≈ 0`, results blow up. Guard in your usage if inputs can approach these points. * **`cot()` / `tan()` singularities:** Near multiples where `sin() ≈ 0` or `cos() ≈ 0`, results blow up. Guard in your usage if inputs can approach these points.
* **Comparison:** `operator<=>` is defaulted. Normalization canonicalizes the maximum endpoint to the minimum endpoint. * **Comparison:** `operator<=>` is defaulted. With normalization, distinct representatives can compare as expected (e.g., `-180` vs `180` in signed ranges are distinct endpoints).
* **No implicit numeric conversion:** Theres **no `operator Type()`**. Use `as_degrees()`/`as_radians()` (or `*angle`) explicitly—this intentional friction avoids unit mistakes. * **No implicit numeric conversion:** Theres **no `operator Type()`**. Use `as_degrees()`/`as_radians()` (or `*angle`) explicitly—this intentional friction avoids unit mistakes.
--- ---
+8 -8
View File
@@ -4,7 +4,7 @@
> Namespace: `omath::angles` > Namespace: `omath::angles`
> All functions are `[[nodiscard]]` and `noexcept` where applicable. > All functions are `[[nodiscard]]` and `noexcept` where applicable.
A small set of constexpr-friendly utilities for converting between degrees/radians, converting horizontal/vertical field of view, and wrapping angles into a half-open interval. A small set of constexpr-friendly utilities for converting between degrees/radians, converting horizontal/vertical field of view, and wrapping angles into a closed interval.
--- ---
@@ -29,9 +29,9 @@ template<class Type>
requires std::is_floating_point_v<Type> requires std::is_floating_point_v<Type>
Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect) noexcept; Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect) noexcept;
// Wrap angle into [min, max) (floating-point types) // Wrap angle into [min, max] (any arithmetic type)
template<class Type> template<class Type>
requires std::is_floating_point_v<Type> requires std::is_arithmetic_v<Type>
Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept; Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept;
``` ```
@@ -66,10 +66,10 @@ Formulas (in radians):
### Wrapping angles (or any periodic value) ### Wrapping angles (or any periodic value)
Wrap any floating-point `angle` into `[min, max)`: Wrap any numeric `angle` into `[min, max]`:
```cpp ```cpp
// Wrap degrees into [0, 360) // Wrap degrees into [0, 360]
float a = omath::angles::wrap_angle( 370.0f, 0.0f, 360.0f); // 10 float a = omath::angles::wrap_angle( 370.0f, 0.0f, 360.0f); // 10
float b = omath::angles::wrap_angle( -15.0f, 0.0f, 360.0f); // 345 float b = omath::angles::wrap_angle( -15.0f, 0.0f, 360.0f); // 345
// Signed range [-180,180] // Signed range [-180,180]
@@ -83,10 +83,10 @@ float c = omath::angles::wrap_angle( 200.0f, -180.0f, 180.0f); // -160
* **Type requirements** * **Type requirements**
* Converters & FOV helpers require **floating-point** `Type`. * Converters & FOV helpers require **floating-point** `Type`.
* `wrap_angle` accepts floating-point types. * `wrap_angle` accepts any arithmetic `Type` (floats or integers).
* **Aspect ratio** must be **positive** and finite. For `aspect == 0` the FOV helpers are undefined. * **Aspect ratio** must be **positive** and finite. For `aspect == 0` the FOV helpers are undefined.
* **Units**: FOV functions accept/return **degrees** but compute internally in radians. * **Units**: FOV functions accept/return **degrees** but compute internally in radians.
* **Wrapping interval**: Behavior assumes `max > min`. The result lies in the half-open interval `[min, max)`. * **Wrapping interval**: Behavior assumes `max > min`. The result lies in the **closed interval** `[min, max]` with modulo arithmetic; if you need half-open behavior (e.g., `[min,max)`), adjust your range or post-process endpoint cases.
* **constexpr**: Converters are `constexpr`; FOV helpers are runtime constexpr-compatible except for `std::atan/std::tan` constraints on some standard libraries. * **constexpr**: Converters are `constexpr`; FOV helpers are runtime constexpr-compatible except for `std::atan/std::tan` constraints on some standard libraries.
--- ---
@@ -103,5 +103,5 @@ float v = horizontal_fov_to_vertical(90.0f, 16.0f/9.0f);
float h = vertical_fov_to_horizontal(v, 16.0f/9.0f); float h = vertical_fov_to_horizontal(v, 16.0f/9.0f);
assert(std::abs(h - 90.0f) < 1e-5f); assert(std::abs(h - 90.0f) < 1e-5f);
assert(wrap_angle(360.0f, 0.0f, 360.0f) == 0.0f); assert(wrap_angle(360.0f, 0.0f, 360.0f) == 0.0f || wrap_angle(360.0f, 0.0f, 360.0f) == 360.0f);
``` ```
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/cry_engine/formulas.hpp" #include "omath/engines/cry_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::cry_engine namespace omath::cry_engine
{ {
@@ -15,8 +16,8 @@ namespace omath::cry_engine
const Vector3<float>& look_at) noexcept const Vector3<float>& look_at) noexcept
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_asin(direction.z), -YawAngle::from_atan2(direction.x, direction.y), return {PitchAngle::from_radians(internal::asin(direction.z)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(-internal::atan2(direction.x, direction.y)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/frostbite_engine/formulas.hpp" #include "omath/engines/frostbite_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::frostbite_engine namespace omath::frostbite_engine
@@ -17,8 +18,8 @@ namespace omath::frostbite_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {-PitchAngle::from_asin(direction.y), YawAngle::from_atan2(direction.x, direction.z), return {PitchAngle::from_radians(-internal::asin(direction.y)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(internal::atan2(direction.x, direction.z)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/iw_engine/formulas.hpp" #include "omath/engines/iw_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::iw_engine namespace omath::iw_engine
@@ -17,8 +18,8 @@ namespace omath::iw_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {-PitchAngle::from_asin(direction.z), YawAngle::from_atan2(direction.y, direction.x), return {PitchAngle::from_radians(-internal::asin(direction.z)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(internal::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/opengl_engine/formulas.hpp" #include "omath/engines/opengl_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::opengl_engine namespace omath::opengl_engine
@@ -17,8 +18,8 @@ namespace omath::opengl_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_asin(direction.y), -YawAngle::from_atan2(direction.x, -direction.z), return {PitchAngle::from_radians(internal::asin(direction.y)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(-internal::atan2(direction.x, -direction.z)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/rage_engine/formulas.hpp" #include "omath/engines/rage_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::rage_engine namespace omath::rage_engine
@@ -17,8 +18,8 @@ namespace omath::rage_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_asin(direction.z), -YawAngle::from_atan2(direction.x, direction.y), return {PitchAngle::from_radians(internal::asin(direction.z)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(-internal::atan2(direction.x, direction.y)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/source_engine/formulas.hpp" #include "omath/engines/source_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::source_engine namespace omath::source_engine
@@ -17,8 +18,8 @@ namespace omath::source_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {-PitchAngle::from_asin(direction.z), return {PitchAngle::from_radians(-internal::asin(direction.z)),
YawAngle::from_atan2(direction.y, direction.x), RollAngle::from_radians(0.f)}; YawAngle::from_radians(internal::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/unity_engine/formulas.hpp" #include "omath/engines/unity_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::unity_engine namespace omath::unity_engine
@@ -17,8 +18,8 @@ namespace omath::unity_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {-PitchAngle::from_asin(direction.y), YawAngle::from_atan2(direction.x, direction.z), return {PitchAngle::from_radians(-internal::asin(direction.y)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(internal::atan2(direction.x, direction.z)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
@@ -4,6 +4,7 @@
#pragma once #pragma once
#include "omath/engines/unreal_engine/formulas.hpp" #include "omath/engines/unreal_engine/formulas.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp" #include "omath/projection/camera.hpp"
namespace omath::unreal_engine namespace omath::unreal_engine
@@ -17,8 +18,8 @@ namespace omath::unreal_engine
{ {
const auto direction = (look_at - cam_origin).normalized(); const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_asin(direction.z), YawAngle::from_atan2(direction.y, direction.x), return {PitchAngle::from_radians(internal::asin(direction.z)),
RollAngle::from_radians(0.f)}; YawAngle::from_radians(internal::atan2(direction.y, direction.x)), RollAngle::from_radians(0.f)};
} }
[[nodiscard("view matrix result should not be discarded")]] [[nodiscard("view matrix result should not be discarded")]]
+32 -31
View File
@@ -6,9 +6,7 @@
#include "omath/internal/constexpr_math.hpp" #include "omath/internal/constexpr_math.hpp"
#include "omath/trigonometry/angles.hpp" #include "omath/trigonometry/angles.hpp"
#include <algorithm> #include <algorithm>
#include <compare>
#include <format> #include <format>
#include <type_traits>
#include <utility> #include <utility>
namespace omath namespace omath
@@ -20,7 +18,7 @@ namespace omath
}; };
template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized> template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized>
requires std::is_floating_point_v<Type> requires std::is_arithmetic_v<Type>
class Angle class Angle
{ {
Type m_angle; Type m_angle;
@@ -45,7 +43,7 @@ namespace omath
{ {
return Angle{degrees}; return Angle{degrees};
} }
constexpr Angle() noexcept: Angle(Type{0}) constexpr Angle() noexcept: m_angle(0)
{ {
} }
[[nodiscard]] [[nodiscard]]
@@ -54,30 +52,6 @@ namespace omath
return Angle{angles::radians_to_degrees<Type>(degrees)}; return Angle{angles::radians_to_degrees<Type>(degrees)};
} }
[[nodiscard]]
constexpr static Angle from_asin(const Type& value) noexcept
{
return from_radians(internal::asin(value));
}
[[nodiscard]]
constexpr static Angle from_acos(const Type& value) noexcept
{
return from_radians(internal::acos(value));
}
[[nodiscard]]
constexpr static Angle from_atan(const Type& value) noexcept
{
return from_radians(internal::atan(value));
}
[[nodiscard]]
constexpr static Angle from_atan2(const Type& y, const Type& x) noexcept
{
return from_radians(internal::atan2(y, x));
}
[[nodiscard]] [[nodiscard]]
constexpr const Type& operator*() const noexcept constexpr const Type& operator*() const noexcept
{ {
@@ -114,6 +88,12 @@ namespace omath
return internal::tan(as_radians()); return internal::tan(as_radians());
} }
[[nodiscard]]
constexpr Type atan() const noexcept
{
return internal::atan(as_radians());
}
[[nodiscard]] [[nodiscard]]
constexpr Type cot() const noexcept constexpr Type cot() const noexcept
{ {
@@ -141,8 +121,7 @@ namespace omath
constexpr Angle& operator-=(const Angle& other) noexcept constexpr Angle& operator-=(const Angle& other) noexcept
{ {
*this = Angle{m_angle - other.m_angle}; return operator+=(-other);
return *this;
} }
[[nodiscard]] [[nodiscard]]
@@ -163,7 +142,7 @@ namespace omath
[[nodiscard]] [[nodiscard]]
constexpr Angle operator-(const Angle& other) const noexcept constexpr Angle operator-(const Angle& other) const noexcept
{ {
return Angle{m_angle - other.m_angle}; return operator+(-other);
} }
[[nodiscard]] [[nodiscard]]
@@ -193,6 +172,7 @@ struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char> final // NOLINT(*-dc
return std::format_to(ctx.out(), "{}deg", a.as_degrees()); return std::format_to(ctx.out(), "{}deg", a.as_degrees());
} }
}; };
// wchar_t formatter // wchar_t formatter
template<class T, T MinV, T MaxV, omath::AngleFlags F> template<class T, T MinV, T MaxV, omath::AngleFlags F>
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, wchar_t> final // NOLINT(*-dcl58-cpp) struct std::formatter<omath::Angle<T, MinV, MaxV, F>, wchar_t> final // NOLINT(*-dcl58-cpp)
@@ -213,3 +193,24 @@ struct std::formatter<omath::Angle<T, MinV, MaxV, F>, wchar_t> final // NOLINT(*
return std::format_to(ctx.out(), L"{}deg", a.as_degrees()); return std::format_to(ctx.out(), L"{}deg", a.as_degrees());
} }
}; };
// wchar_t formatter
template<class T, T MinV, T MaxV, omath::AngleFlags F>
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char8_t> final // NOLINT(*-dcl58-cpp)
{
using AngleT = omath::Angle<T, MinV, MaxV, F>;
[[nodiscard]]
static constexpr auto parse(std::wformat_parse_context& ctx)
{
return ctx.begin();
}
template<class FormatContext>
[[nodiscard]]
auto format(const AngleT& a, FormatContext& ctx) const
{
static_assert(std::is_same_v<typename FormatContext::char_type, char8_t>);
return std::format_to(ctx.out(), u8"{}deg", a.as_degrees());
}
};
+2 -2
View File
@@ -48,10 +48,10 @@ namespace omath::angles
} }
template<class Type> template<class Type>
requires std::is_floating_point_v<Type> requires std::is_arithmetic_v<Type>
[[nodiscard]] constexpr Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept [[nodiscard]] constexpr 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;
const Type range = max - min; const Type range = max - min;
+2
View File
@@ -177,7 +177,9 @@ if command -v genhtml >/dev/null 2>&1; then
--title "Omath Coverage Report" \ --title "Omath Coverage Report" \
--show-details \ --show-details \
--legend \ --legend \
--demangle-cpp \
--num-spaces 4 \ --num-spaces 4 \
--sort-tables \
--function-coverage \ --function-coverage \
--branch-coverage --branch-coverage
+13 -38
View File
@@ -14,14 +14,9 @@ namespace
// Handy aliases (defaults: Type=float, [0,360], Normalized) // Handy aliases (defaults: Type=float, [0,360], Normalized)
using Deg = Angle<float, static_cast<float>(0), static_cast<float>(360), AngleFlags::Normalized>; using Deg = Angle<float, static_cast<float>(0), static_cast<float>(360), AngleFlags::Normalized>;
using Fov = Angle<float, static_cast<float>(0), static_cast<float>(180), AngleFlags::Clamped>;
using Offset = Angle<float, static_cast<float>(10), static_cast<float>(20), AngleFlags::Clamped>;
using Pitch = Angle<float, static_cast<float>(-90), static_cast<float>(90), AngleFlags::Clamped>; using Pitch = Angle<float, static_cast<float>(-90), static_cast<float>(90), AngleFlags::Clamped>;
using Turn = Angle<float, static_cast<float>(-180), static_cast<float>(180), AngleFlags::Normalized>; using Turn = Angle<float, static_cast<float>(-180), static_cast<float>(180), AngleFlags::Normalized>;
template<class Type>
concept SupportedAngleType = requires { typename Angle<Type>; };
constexpr float k_eps = 1e-5f; constexpr float k_eps = 1e-5f;
constexpr bool close_to(const float actual, const float expected, const float epsilon) constexpr bool close_to(const float actual, const float expected, const float epsilon)
@@ -41,12 +36,6 @@ TEST(UnitTestAngle, DefaultConstructor_IsZeroDegrees)
EXPECT_FLOAT_EQ(a.as_degrees(), 0.0f); EXPECT_FLOAT_EQ(a.as_degrees(), 0.0f);
} }
TEST(UnitTestAngle, DefaultConstructor_AppliesRangePolicy)
{
constexpr Offset a;
EXPECT_FLOAT_EQ(a.as_degrees(), 10.0f);
}
TEST(UnitTestAngle, FromDegrees_Normalized_WrapsAboveMax) TEST(UnitTestAngle, FromDegrees_Normalized_WrapsAboveMax)
{ {
const Deg a = Deg::from_degrees(370.0f); const Deg a = Deg::from_degrees(370.0f);
@@ -77,14 +66,6 @@ TEST(UnitTestAngle, FromRadians_And_AsRadians)
EXPECT_NEAR(b.as_radians(), std::numbers::pi_v<float>, 1e-6f); EXPECT_NEAR(b.as_radians(), std::numbers::pi_v<float>, 1e-6f);
} }
TEST(UnitTestAngle, FromInverseTrigonometricFunctions)
{
EXPECT_NEAR(Pitch::from_asin(0.5f).as_degrees(), 30.0f, k_eps);
EXPECT_NEAR(Pitch::from_acos(0.5f).as_degrees(), 60.0f, k_eps);
EXPECT_NEAR(Pitch::from_atan(1.0f).as_degrees(), 45.0f, k_eps);
EXPECT_NEAR(Turn::from_atan2(-1.0f, -1.0f).as_degrees(), -135.0f, k_eps);
}
// ---------- Unary minus & deref ---------- // ---------- Unary minus & deref ----------
TEST(UnitTestAngle, UnaryMinus_Normalized) TEST(UnitTestAngle, UnaryMinus_Normalized)
@@ -120,6 +101,17 @@ TEST(UnitTestAngle, SinCosTanCot_BasicCases)
EXPECT_NEAR(a90.cos(), 0.0f, 1e-4f); EXPECT_NEAR(a90.cos(), 0.0f, 1e-4f);
} }
TEST(UnitTestAngle, Atan_IsAtanOfRadians)
{
// atan(as_radians). For 0° -> atan(0)=0.
const Deg a0 = Deg::from_degrees(0.0f);
EXPECT_NEAR(a0.atan(), 0.0f, k_eps);
const Deg a45 = Deg::from_degrees(45.0f);
// atan(pi/4) ≈ 0.665773...
EXPECT_NEAR(a45.atan(), 0.66577375f, 1e-6f);
}
// ---------- Compound arithmetic ---------- // ---------- Compound arithmetic ----------
TEST(UnitTestAngle, PlusEquals_Normalized_Wraps) TEST(UnitTestAngle, PlusEquals_Normalized_Wraps)
@@ -150,16 +142,6 @@ TEST(UnitTestAngle, MinusEquals_Clamped_Clamps)
EXPECT_FLOAT_EQ(p.as_degrees(), -90.0f); EXPECT_FLOAT_EQ(p.as_degrees(), -90.0f);
} }
TEST(UnitTestAngle, Subtraction_ClampedNonSymmetricRange)
{
Fov compound = Fov::from_degrees(90.0f);
compound -= Fov::from_degrees(10.0f);
EXPECT_FLOAT_EQ(compound.as_degrees(), 80.0f);
const Fov binary = Fov::from_degrees(90.0f) - Fov::from_degrees(10.0f);
EXPECT_FLOAT_EQ(binary.as_degrees(), 80.0f);
}
// ---------- Alternative ranges ---------- // ---------- Alternative ranges ----------
TEST(UnitTestAngle, NormalizedRange_Neg180To180) TEST(UnitTestAngle, NormalizedRange_Neg180To180)
@@ -223,12 +205,5 @@ static_assert(close_to(Pitch::from_degrees(45.0f).tan(), 1.0f, 1e-4f),
"Tan should be constexpr with embedded constexpr math"); "Tan should be constexpr with embedded constexpr math");
static_assert(close_to(Pitch::from_degrees(45.0f).cot(), 1.0f, 1e-4f), static_assert(close_to(Pitch::from_degrees(45.0f).cot(), 1.0f, 1e-4f),
"Cot should be constexpr with embedded constexpr math"); "Cot should be constexpr with embedded constexpr math");
static_assert(close_to(Pitch::from_asin(0.5f).as_degrees(), 30.0f, k_eps), static_assert(close_to(Pitch::from_degrees(45.0f).atan(), 0.66577375f, 1e-6f),
"From asin should be constexpr with embedded constexpr math"); "Atan should be constexpr with embedded constexpr math");
static_assert(close_to(Pitch::from_acos(0.5f).as_degrees(), 60.0f, k_eps),
"From acos should be constexpr with embedded constexpr math");
static_assert(close_to(Pitch::from_atan(1.0f).as_degrees(), 45.0f, k_eps),
"From atan should be constexpr with embedded constexpr math");
static_assert(close_to(Turn::from_atan2(-1.0f, -1.0f).as_degrees(), -135.0f, k_eps),
"From atan2 should be constexpr with embedded constexpr math");
static_assert(!SupportedAngleType<int>, "Angle should only accept floating-point types");
-7
View File
@@ -47,10 +47,3 @@ TEST(unit_test_angles, wrap_angle_negative_range)
EXPECT_NEAR(wrapped, 270.f, 0.01f); EXPECT_NEAR(wrapped, 270.f, 0.01f);
} }
TEST(unit_test_angles, wrap_angle_maximum_maps_to_minimum)
{
const float wrapped = omath::angles::wrap_angle(360.f, 0.f, 360.f);
EXPECT_FLOAT_EQ(wrapped, 0.f);
}