Fixposition SDK 0.0.0-heads/main-0-g5c7edb5
Collection of c++ libraries and apps for use with Fixposition products on Linux
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 Convert degrees to radians
40 *
41 * @tparam T value type
42 * @param[in] degrees Angle in degrees
43 *
44 * @returns the angle in radians
45 */
46template <typename T>
47constexpr inline T DegToRad(T degrees)
48{
49 static_assert(::std::is_floating_point<T>::value, "Value must be float or double");
50 return degrees * M_PI / 180.0;
51}
52
53/**
54 * @brief Convert radians to degrees
55 *
56 * @tparam T value type
57 * @param[in] radians Angle in radians
58 * @returns the angle in radians
59 */
60template <typename T>
61constexpr inline T RadToDeg(T radians)
62{
63 static_assert(::std::is_floating_point<T>::value, "Value must be float or double");
64 return radians * 180.0 / M_PI;
65}
66
67/**
68 * @brief Round to desired number of fractional digits (of precision)
69 *
70 * @param[in] value The value
71 * @param[in] digits Number of digits (0-12), param clamped to range
72 *
73 * @returns the value rounded to the given number of fractional digits, or the original value if it is not finite
74 */
75double RoundToFracDigits(const double value, const int digits);
76
77/**
78 * @brief Clip to desired number of fractional digits (of precision)
79 *
80 * @param[in] value The value
81 * @param[in] digits Number of digits (0-12), param clamped to range
82 *
83 * @returns the value clipped to the given number of fractional digits, or the original value if it is not finite
84 */
85double ClipToFracDigits(const double value, const int digits);
86
87// ---------------------------------------------------------------------------------------------------------------------
88/**
89 * @name Bit manipulation functions
90 *
91 * Examples:
92 *
93 * @code{cpp}
94 * uint8_t mask = 0;
95 * SetBits(mask, Bit(0) | Bit(1) | Bit(7)); // mask is now 0x83
96 * const bool bit_7_is_set = CheckBitsAll(mask, Bit(7)); // true
97 * @endcode
98 *
99 * @{
100 */
101/**
102 * @brief Return a number with the given bit set to 1 (i.e. 2^bit)
103 *
104 * @tparam T Numerical type
105 * @param[in] bit bit to be set to 1 (0-63, depending on T, 0 = LSB)
106 *
107 * @returns the mask (value) with the desired bit set
108 */
109template <typename T>
110constexpr T Bit(const std::size_t bit)
111{
112 return static_cast<T>(static_cast<uint64_t>(1) << bit);
113}
114
115/**
116 * @brief Checks if all bits are set
117 *
118 * @tparam T Numerical type
119 * @param[in] mask Mask (value) that should be checked
120 * @param[in] bits Bit(s) to be checked
121 *
122 * @returns true if all bit(s) is (are) set, false otherwise
123 */
124template <typename T>
125constexpr bool CheckBitsAll(const T mask, const T bits)
126{
127 return (mask & bits) == bits;
128}
129
130/**
131 * @brief Checks if any bits are set
132 *
133 * @tparam T Numerical type
134 * @param[in] mask Mask (value) to be checked
135 * @param[in] bits Bit(s) to be checked
136 *
137 * @returns true if any bit(s) is (are) set, false otherwise
138 */
139template <typename T>
140constexpr bool CheckBitsAny(const T mask, const T bits)
141{
142 return (mask & bits) != 0;
143}
144
145/**
146 * @brief Extracts bits
147 *
148 * @tparam T Numerical type
149 * @param[in] value The bitfield value
150 * @param[in] mask Mask of bits that should be extracted
151 *
152 * @returns the extracted bits
153 */
154template <typename T>
155constexpr T GetBits(const T value, const T mask)
156{
157 return (value & mask);
158}
159
160/**
161 * @brief Sets the bits
162 *
163 * @tparam T Numerical type
164 * @param[in,out] mask Mask (value) to be modified
165 * @param[in] bits Bit(s) to be set
166 */
167template <typename T>
168inline void SetBits(T& mask, const T bits)
169{
170 mask |= bits;
171}
172
173/**
174 * @brief Clears the bits
175 *
176 * @tparam T Numerical type
177 * @param[in,out] mask Mask (value) to be modified
178 * @param[in] bits Bit(s) to be cleared
179 */
180template <typename T>
181inline void ClearBits(T& mask, const T bits)
182{
183 mask &= ~bits;
184}
185
186/**
187 * @brief Toggles the bits
188 *
189 * @tparam T Numerical type
190 * @param[in,out] mask Mask (value) to be modified
191 * @param[in] bits Bit(s) to be toggled
192 */
193template <typename T>
194inline void ToggleBits(T& mask, const T bits)
195{
196 mask ^= bits;
197}
198
199///@}
200// ---------------------------------------------------------------------------------------------------------------------
201
202/* ****************************************************************************************************************** */
203} // namespace math
204} // namespace common
205} // namespace fpsdk
206#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 DegToRad(T degrees)
Convert degrees to radians.
Definition math.hpp:47
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:110
constexpr bool CheckBitsAny(const T mask, const T bits)
Checks if any bits are set.
Definition math.hpp:140
void SetBits(T &mask, const T bits)
Sets the bits.
Definition math.hpp:168
void ToggleBits(T &mask, const T bits)
Toggles the bits.
Definition math.hpp:194
constexpr bool CheckBitsAll(const T mask, const T bits)
Checks if all bits are set.
Definition math.hpp:125
constexpr T RadToDeg(T radians)
Convert radians to degrees.
Definition math.hpp:61
void ClearBits(T &mask, const T bits)
Clears the bits.
Definition math.hpp:181
constexpr T GetBits(const T value, const T mask)
Extracts bits.
Definition math.hpp:155
Fixposition SDK.