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