MRPT  1.9.9
core/include/mrpt/core/bits_math.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10
11 #include <cmath> // floor(),isnan(),...
12 #include <stdexcept>
13
14 namespace mrpt
15 {
16 /** Inline function for the square of a number. */
17 template <class T>
18 inline T square(const T x)
19 {
20  return x * x;
21 }
22
23 /** Faster version of std::hypot(), to use when overflow is not an issue and we
24  * prefer fast code. */
25 template <class T>
26 inline T hypot_fast(const T x, const T y)
27 {
28  return std::sqrt(x * x + y * y);
29 }
30
31 #ifdef DEG2RAD // functions are preferred over macros
33 #endif
36 #endif
37 #if !defined(M_PI)
38 #define M_PI 3.14159265358979323846
39 #endif
40
41 /** Degrees to radians */
42 inline double DEG2RAD(const double x) { return x * M_PI / 180.0; }
43 /** Degrees to radians */
44 inline float DEG2RAD(const float x) { return x * float(M_PI) / 180.0f; }
45 /** Degrees to radians */
46 inline double DEG2RAD(const int x) { return x * M_PI / 180.0; }
47 /** Radians to degrees */
48 inline double RAD2DEG(const double x) { return x * 180.0 / M_PI; }
49 /** Radians to degrees */
50 inline float RAD2DEG(const float x) { return x * 180.0f / float(M_PI); }
51 #if !defined(M_PIl)
52 #define M_PIl 3.14159265358979323846264338327950288L
53 #define M_2PIl (2.0L * 3.14159265358979323846264338327950288L)
54 #endif
55 /** Degrees to radians */
56 inline long double DEG2RAD(const long double x) { return x * M_PIl / 180.0; }
57 /** Radians to degrees */
58 inline long double RAD2DEG(const long double x) { return x * 180.0 / M_PIl; }
59 #define DEG2RAD \
60  DEG2RAD // This is required to avoid other libs (like PCL) to #define their
61 // own versions of DEG2RAD
62 #define RAD2DEG \
63  RAD2DEG // This is required to avoid other libs (like PCL) to #define their
64 // own versions of RAD2DEG
65
66 /** Returns the sign of X as "1" or "-1" */
67 template <typename T>
68 inline int sign(T x)
69 {
70  return x < 0 ? -1 : 1;
71 }
72
73 /** Returns the sign of X as "0", "1" or "-1" */
74 template <typename T>
75 inline int signWithZero(T x)
76 {
77  return (x == 0 || x == -0) ? 0 : sign(x);
78 }
79
80 /** Returns the lowest, possitive among two numbers. If both are non-positive
81  * (<=0), the lowest one is returned. */
82 template <typename T>
83 T lowestPositive(const T a, const T b)
84 {
85  if (a > 0 && a <= b)
86  return a; // a positive and smaller than b
87  else if (b > 0)
88  return b; // b is positive and either smaller than a or a is negative
89  else
90  return a; // at least b is negative, we might not have an answer
91 }
92
93 /** Efficient and portable evaluation of the absolute difference of two unsigned
94  * integer values
95  * (but will also work for signed and floating point types) */
96 template <typename T>
97 inline T abs_diff(const T a, const T b)
98 {
99  return std::max(a, b) - std::min(a, b);
100 }
101
102 template <typename T>
103 inline const T min3(const T& A, const T& B, const T& C)
104 {
105  return std::min<T>(A, std::min<T>(B, C));
106 }
107 template <typename T>
108 inline const T max3(const T& A, const T& B, const T& C)
109 {
110  return std::max<T>(A, std::max<T>(B, C));
111 }
112
113 /** Rounds toward zero */
114 template <typename T>
115 inline int fix(T x)
116 {
117  return x > 0 ? static_cast<int>(floor(static_cast<double>(x)))
118  : static_cast<int>(ceil(static_cast<double>(x)));
119 }
120
121 /** If the second argument is below the first one, set the first argument to
122  * this lower value. */
123 template <typename T, typename K>
124 inline void keep_min(T& var, const K test_val)
125 {
126  if (test_val < var) var = test_val;
127 }
128 /** If the second argument is above the first one, set the first argument to
129  * this higher value. */
130 template <typename T, typename K>
131 inline void keep_max(T& var, const K test_val)
132 {
133  if (test_val > var) var = test_val;
134 }
135 /** Saturate the value of var (the variable gets modified) so it does not get
136  * out of [min,max]. */
137 template <typename T>
138 inline void saturate(T& var, const T sat_min, const T sat_max)
139 {
140  if (var > sat_max) var = sat_max;
141  if (var < sat_min) var = sat_min;
142 }
143 /** Like saturate() but it returns the value instead of modifying the variable
144  */
145 template <typename T>
146 inline T saturate_val(const T& value, const T sat_min, const T sat_max)
147 {
148  T var = value;
149  if (var > sat_max) var = sat_max;
150  if (var < sat_min) var = sat_min;
151  return var;
152 }
153
154 /** Round up to the nearest power of two of a given number */
155 template <class T>
157 {
158  T n = 1;
159  while (n < val)
160  {
161  n <<= 1;
162  if (n <= 1) throw std::invalid_argument("round2up: Overflow!");
163  }
164  return n;
165 }
166 } // namespace mrpt
void saturate(T &var, const T sat_min, const T sat_max)
Saturate the value of var (the variable gets modified) so it does not get out of [min,max].
#define min(a, b)
double RAD2DEG(const double x)
T hypot_fast(const T x, const T y)
Faster version of std::hypot(), to use when overflow is not an issue and we prefer fast code...
T lowestPositive(const T a, const T b)
Returns the lowest, possitive among two numbers.
int fix(T x)
Rounds toward zero.
double DEG2RAD(const double x)
const T max3(const T &A, const T &B, const T &C)
GLenum GLsizei n
Definition: glext.h:5074
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
T square(const T x)
Inline function for the square of a number.
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
T abs_diff(const T a, const T b)
Efficient and portable evaluation of the absolute difference of two unsigned integer values (but will...
T round2up(T val)
Round up to the nearest power of two of a given number.
int val
Definition: mrpt_jpeglib.h:955
GLubyte GLubyte b
Definition: glext.h:6279
const T min3(const T &A, const T &B, const T &C)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLenum GLint GLint y
Definition: glext.h:3538
int sign(T x)
Returns the sign of X as "1" or "-1".
GLsizei const GLfloat * value
Definition: glext.h:4117
T saturate_val(const T &value, const T sat_min, const T sat_max)
Like saturate() but it returns the value instead of modifying the variable.
GLenum GLint x
Definition: glext.h:3538
GLubyte GLubyte GLubyte a
Definition: glext.h:6279

 Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020