Fixposition SDK 0.0.0-heads/main-0-gd0a6ce2
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 * @brief Clip to desired number of fractional digits (of precision)
97 *
98 * @param[in] value The value
99 * @param[in] digits Number of digits (0-12), param clamped to range
100 *
101 * @returns the value clipped to the given number of fractional digits, or the original value if it is not finite
102 */
103double ClipToFracDigits(const double value, const int digits);
104
105// ---------------------------------------------------------------------------------------------------------------------
106/**
107 * @name Bit manipulation functions
108 *
109 * Examples:
110 *
111 * @code{cpp}
112 * uint8_t mask = 0;
113 * SetBits(mask, Bit(0) | Bit(1) | Bit(7)); // mask is now 0x83
114 * const bool bit_7_is_set = CheckBitsAll(mask, Bit(7)); // true
115 * @endcode
116 *
117 * @{
118 */
119/**
120 * @brief Return a number with the given bit set to 1 (i.e. 2^bit)
121 *
122 * @tparam T Numerical type
123 * @param[in] bit bit to be set to 1 (0-63, depending on T, 0 = LSB)
124 *
125 * @returns the mask (value) with the desired bit set
126 */
127template <typename T>
128constexpr T Bit(const std::size_t bit)
129{
130 return static_cast<T>(static_cast<uint64_t>(1) << bit);
131}
132
133/**
134 * @brief Checks if all bits are set
135 *
136 * @tparam T Numerical type
137 * @param[in] mask Mask (value) that should be checked
138 * @param[in] bits Bit(s) to be checked
139 *
140 * @returns true if all bit(s) is (are) set, false otherwise
141 */
142template <typename T>
143constexpr bool CheckBitsAll(const T mask, const T bits)
144{
145 return (mask & bits) == bits;
146}
147
148/**
149 * @brief Checks if any bits are set
150 *
151 * @tparam T Numerical type
152 * @param[in] mask Mask (value) to be checked
153 * @param[in] bits Bit(s) to be checked
154 *
155 * @returns true if any bit(s) is (are) set, false otherwise
156 */
157template <typename T>
158constexpr bool CheckBitsAny(const T mask, const T bits)
159{
160 return (mask & bits) != 0;
161}
162
163/**
164 * @brief Extracts bits
165 *
166 * @tparam T Numerical type
167 * @param[in] value The bitfield value
168 * @param[in] mask Mask of bits that should be extracted
169 *
170 * @returns the extracted bits
171 */
172template <typename T>
173constexpr T GetBits(const T value, const T mask)
174{
175 return (value & mask);
176}
177
178/**
179 * @brief Sets the bits
180 *
181 * @tparam T Numerical type
182 * @param[in,out] mask Mask (value) to be modified
183 * @param[in] bits Bit(s) to be set
184 */
185template <typename T>
186inline void SetBits(T& mask, const T bits)
187{
188 mask |= bits;
189}
190
191/**
192 * @brief Clears the bits
193 *
194 * @tparam T Numerical type
195 * @param[in,out] mask Mask (value) to be modified
196 * @param[in] bits Bit(s) to be cleared
197 */
198template <typename T>
199inline void ClearBits(T& mask, const T bits)
200{
201 mask &= ~bits;
202}
203
204/**
205 * @brief Toggles the bits
206 *
207 * @tparam T Numerical type
208 * @param[in,out] mask Mask (value) to be modified
209 * @param[in] bits Bit(s) to be toggled
210 */
211template <typename T>
212inline void ToggleBits(T& mask, const T bits)
213{
214 mask ^= bits;
215}
216
217///@}
218// ---------------------------------------------------------------------------------------------------------------------
219
220/* ****************************************************************************************************************** */
221} // namespace math
222} // namespace common
223} // namespace fpsdk
224#endif // __FPSDK_COMMON_MATH_HPP__
double RoundToFracDigits(const double value, const int digits)
Round to desired number of fractional digits (of precision)
double ClipToFracDigits(const double value, const int digits)
Clip 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 T Bit(const std::size_t bit)
Return a number with the given bit set to 1 (i.e. 2^bit)
Definition math.hpp:128
constexpr bool CheckBitsAny(const T mask, const T bits)
Checks if any bits are set.
Definition math.hpp:158
void SetBits(T &mask, const T bits)
Sets the bits.
Definition math.hpp:186
void ToggleBits(T &mask, const T bits)
Toggles the bits.
Definition math.hpp:212
constexpr bool CheckBitsAll(const T mask, const T bits)
Checks if all bits are set.
Definition math.hpp:143
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:199
constexpr T GetBits(const T value, const T mask)
Extracts bits.
Definition math.hpp:173
Fixposition SDK.