UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
math.hpp
1
42#ifndef UFO_NUMERIC_MATH_HPP
43#define UFO_NUMERIC_MATH_HPP
44
45// STL
46#include <algorithm>
47#include <cassert>
48#include <cmath>
49#include <concepts>
50#include <limits>
51#include <numbers>
52#include <ranges>
53
54namespace ufo
55{
69template <class T>
70[[nodiscard]] constexpr int sign(T val) noexcept
71{
72 if constexpr (std::is_unsigned_v<T>) {
73 return T(0) < val;
74 } else {
75 return (T(0) < val) - (val < T(0));
76 }
77}
78
85template <std::floating_point T>
86[[nodiscard]] constexpr T radians(T deg) noexcept
87{
88 return deg * std::numbers::pi_v<T> / T(180);
89}
90
97template <std::floating_point T>
98[[nodiscard]] constexpr T degrees(T rad) noexcept
99{
100 return rad * T(180) / std::numbers::pi_v<T>;
101}
102
110template <class T>
111 requires std::is_arithmetic_v<T>
112[[nodiscard]] constexpr T ipow(T base, int exp) noexcept
113{
114 T result = std::ranges::fold_left(std::views::repeat(base, std::abs(exp)), T(1),
115 std::multiplies<>());
116 return 0 <= exp ? result : T(1) / result;
117}
118
128template <std::floating_point T>
129[[nodiscard]] constexpr T probabilityToLogit(T probability)
130{
131 assert(T(0) <= probability && T(1) >= probability);
132 if (T(0) >= probability) {
133 return -std::numeric_limits<T>::infinity();
134 }
135 if (T(1) <= probability) {
136 return std::numeric_limits<T>::infinity();
137 }
138 return std::log(probability / (T(1) - probability));
139}
140
147template <std::floating_point T>
148[[nodiscard]] constexpr T logitToProbability(T logit)
149{
150 return T(1) / (T(1) + std::exp(-logit));
151}
152
156} // namespace ufo
157
158#endif // UFO_NUMERIC_MATH_HPP
constexpr T radians(T deg) noexcept
Converts degrees to radians.
Definition math.hpp:86
constexpr int sign(T val) noexcept
Returns the sign of a value.
Definition math.hpp:70
constexpr T degrees(T rad) noexcept
Converts radians to degrees.
Definition math.hpp:98
constexpr T probabilityToLogit(T probability)
Converts probability to logit value.
Definition math.hpp:129
constexpr T ipow(T base, int exp) noexcept
Computes integer power of a base.
Definition math.hpp:112
constexpr T logitToProbability(T logit)
Converts logit value to probability.
Definition math.hpp:148
All vision-related classes and functions.
Definition cloud.hpp:49