Fixposition SDK 0.0.0-heads/main-0-g90a51ff
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: Math utilities
12 *
13 * @page FPSDK_COMMON_MATH Math utilities
14 *
15 * **API**: fpsdk_common/math.hpp and fpsdk::common::math
16 *
17 */
18#ifndef __FPSDK_COMMON_MATH_HPP__
19#define __FPSDK_COMMON_MATH_HPP__
20
21/* LIBC/STL */
22#include <algorithm>
23#include <cmath>
24#include <cstdint>
25
26/* EXTERNAL */
27
28/* PACKAGE */
29
30namespace fpsdk {
31namespace common {
32/**
33 * @brief Math utilities
34 */
35namespace math {
36/* ****************************************************************************************************************** */
37
38/**
39 * @brief Clamp value in range
40 *
41 * @tparam T numeric type
42 * @param[in] val The value
43 * @param[in] min Minimum value
44 * @param[in] max Maximum value
45 *
46 * @note c++-17 has std::clamp() doing exactly (?) this...
47 *
48 * @returns the value clamped to the given range
49 */
50template <typename T>
51constexpr T Clamp(const T val, const T min, const T max)
52{
53 return std::max(min, std::min(val, max));
54}
55
56/**
57 * @brief Convert degrees to radians
58 *
59 * @tparam T value type
60 * @param[in] degrees Angle in degrees
61 *
62 * @returns the angle in radians
63 */
64template <typename T>
65constexpr inline T DegToRad(T degrees)
66{
67 static_assert(::std::is_floating_point<T>::value, "Value must be float or double");
68 return degrees * M_PI / 180.0;
69}
70
71/**
72 * @brief Convert radians to degrees
73 *
74 * @tparam T value type
75 * @param[in] radians Angle in radians
76 * @returns the angle in radians
77 */
78template <typename T>
79constexpr inline T RadToDeg(T radians)
80{
81 static_assert(::std::is_floating_point<T>::value, "Value must be float or double");
82 return radians * 180.0 / M_PI;
83}
84
85/**
86 * @brief Round to desired number of fractional digits (of precision)
87 *
88 * @param[in] value The value
89 * @param[in] digits Number of digits (0-12), param clamped to range
90 *
91 * @returns the value rounded to the given number of fractional digits, or the original value if it is not finite
92 */
93double RoundToFracDigits(const double value, const int digits);
94
95// ---------------------------------------------------------------------------------------------------------------------
96/**
97 * @name Bit manipulation functions
98 *
99 * Examples:
100 *
101 * @code{cpp}
102 * uint8_t mask = 0;
103 * SetBits(mask, Bit(0) | Bit(1) | Bit(7)); // mask is now 0x83
104 * const bool bit_7_is_set = CheckBits(mask, Bit(7)); // true
105 * @endcode
106 *
107 * @{
108 */
109/**
110 * @brief Return a number with the given bit set to 1 (i.e. 2^bit)
111 *
112 * @tparam T Numerical type
113 * @param[in] bit bit to be set to 1 (0-63, depending on T, 0 = LSB)
114 *
115 * @returns the mask (value) with the desired bit set
116 */
117template <typename T>
118constexpr T Bit(const size_t bit)
119{
120 return static_cast<T>(static_cast<uint64_t>(1) << bit);
121}
122
123/**
124 * @brief Checks if all bits are set
125 *
126 * @tparam T Numerical type
127 * @param[in] mask Mask (value) that should be checked
128 * @param[in] bits Bit(s) to be checked
129 *
130 * @returns true if all bit(s) is (are) set, false otherwise
131 */
132template <typename T>
133constexpr bool CheckBitsAll(const T mask, const T bits)
134{
135 return (mask & bits) == bits;
136}
137
138/**
139 * @brief Checks if any bits are set
140 *
141 * @tparam T Numerical type
142 * @param[in] mask Mask (value) to be checked
143 * @param[in] bits Bit(s) to be checked
144 *
145 * @returns true if any bit(s) is (are) set, false otherwise
146 */
147template <typename T>
148constexpr bool CheckBitsAny(const T mask, const T bits)
149{
150 return (mask & bits) != 0;
151}
152
153/**
154 * @brief Extracts bits
155 *
156 * @tparam T Numerical type
157 * @param[in] value The bitfield value
158 * @param[in] mask Mask of bits that should be extracted
159 *
160 * @returns the extracted bits
161 */
162template <typename T>
163constexpr T GetBits(const T value, const T mask)
164{
165 return (value & mask);
166}
167
168/**
169 * @brief Sets the bits
170 *
171 * @tparam T Numerical type
172 * @param[in,out] mask Mask (value) to be modified
173 * @param[in] bits Bit(s) to be set
174 */
175template <typename T>
176inline void SetBits(T& mask, const T bits)
177{
178 mask |= bits;
179}
180
181/**
182 * @brief Clears the bits
183 *
184 * @tparam T Numerical type
185 * @param[in,out] mask Mask (value) to be modified
186 * @param[in] bits Bit(s) to be cleared
187 */
188template <typename T>
189inline void ClearBits(T& mask, const T bits)
190{
191 mask &= ~bits;
192}
193
194/**
195 * @brief Toggles the bits
196 *
197 * @tparam T Numerical type
198 * @param[in,out] mask Mask (value) to be modified
199 * @param[in] bits Bit(s) to be toggled
200 */
201template <typename T>
202inline void ToggleBits(T& mask, const T bits)
203{
204 mask ^= bits;
205}
206
207///@}
208// ---------------------------------------------------------------------------------------------------------------------
209
210/* ****************************************************************************************************************** */
211} // namespace math
212} // namespace common
213} // namespace fpsdk
214#endif // __FPSDK_COMMON_MATH_HPP__
double RoundToFracDigits(const double value, const int digits)
Round to desired number of fractional digits (of precision)
constexpr T Clamp(const T val, const T min, const T max)
Clamp value in range.
Definition math.hpp:51
constexpr T DegToRad(T degrees)
Convert degrees to radians.
Definition math.hpp:65
constexpr bool CheckBitsAny(const T mask, const T bits)
Checks if any bits are set.
Definition math.hpp:148
void SetBits(T &mask, const T bits)
Sets the bits.
Definition math.hpp:176
constexpr T Bit(const size_t bit)
Return a number with the given bit set to 1 (i.e. 2^bit)
Definition math.hpp:118
void ToggleBits(T &mask, const T bits)
Toggles the bits.
Definition math.hpp:202
constexpr bool CheckBitsAll(const T mask, const T bits)
Checks if all bits are set.
Definition math.hpp:133
constexpr T RadToDeg(T radians)
Convert radians to degrees.
Definition math.hpp:79
void ClearBits(T &mask, const T bits)
Clears the bits.
Definition math.hpp:189
constexpr T GetBits(const T value, const T mask)
Extracts bits.
Definition math.hpp:163
Fixposition SDK.