Fixposition SDK 0.0.0-heads/main-0-g7b59b93
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} // namespace math
97} // namespace common
98} // namespace fpsdk
99#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 T RadToDeg(T radians)
Convert radians to degrees.
Definition math.hpp:79
Fixposition SDK.
Definition fpsdk_doc.hpp:20