Main MRPT website > C++ reference for MRPT 1.9.9
bits.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-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/config.h>
13 
14 #include <cmath> // floor(),isnan(),...
15 #include <string>
16 #include <mrpt/utils/mrpt_macros.h>
17 #include <cstdint>
18 
19 /** This is the global namespace for all Mobile Robot Programming Toolkit (MRPT)
20  * libraries. */
21 namespace mrpt
22 {
23 /** A std::string version of C sprintf.
24  * You can call this to obtain a std::string using printf-like syntax.
25  * Based on very nice code by Paul Senzee, published at
26  * http://senzee.blogspot.com/2006/05/c-formatting-stdstring.html
27  * Function implemented in format.cpp
28  */
29 std::string format(const char* fmt, ...) MRPT_printf_format_check(1, 2);
30 
31 namespace math
32 {
33 // This inline function is used everywhere, so just move it here even it's not a
34 // forward declaration!
35 /*! Returns the size of the matrix in the i'th dimension: 1=rows, 2=columns
36  * (MATLAB-compatible function)
37  * \note Template argument MATRIXLIKE can be: mrpt::math::CMatrixTemplate,
38  * mrpt::math::CMatrixTemplateNumeric, mrpt::math::CMatrixFixedNumeric
39  */
40 template <class MATRIXLIKE>
41 inline size_t size(const MATRIXLIKE& m, const int dim)
42 {
43  if (dim == 1)
44  return m.getRowCount();
45  else if (dim == 2)
46  return m.getColCount();
47  else
49  "size: Queried matrix dimension must be 1 or 2. Called with i=%i",
50  dim);
51 }
52 
53 /** Inline function for the square of a number. */
54 template <class T>
55 inline T square(const T x)
56 {
57  return x * x;
58 }
59 
60 /** Faster version of std::hypot(), to use when overflow is not an issue and we
61  * prefer fast code. */
62 template <class T>
63 inline T hypot_fast(const T x, const T y)
64 {
65  return std::sqrt(x * x + y * y);
66 }
67 }
68 
69 namespace utils
70 {
71 class CFileStream;
72 void global_profiler_enter(const char* func_name) noexcept;
73 void global_profiler_leave(const char* func_name) noexcept;
74 
76 {
77  const char* f;
78  CProfilerProxy(const char* func_name) : f(func_name)
79  {
81  }
83 };
84 
85 #ifdef DEG2RAD // functions are preferred over macros
86 #undef DEG2RAD
87 #endif
88 #ifdef RAD2DEG
89 #undef RAD2DEG
90 #endif
91 #if !defined(M_PI)
92 #define M_PI 3.14159265358979323846
93 #endif
94 
95 /** Degrees to radians */
96 inline double DEG2RAD(const double x) { return x * M_PI / 180.0; }
97 /** Degrees to radians */
98 inline float DEG2RAD(const float x) { return x * M_PIf / 180.0f; }
99 /** Degrees to radians */
100 inline float DEG2RAD(const int x) { return x * M_PIf / 180.0f; }
101 /** Radians to degrees */
102 inline double RAD2DEG(const double x) { return x * 180.0 / M_PI; }
103 /** Radians to degrees */
104 inline float RAD2DEG(const float x) { return x * 180.0f / M_PIf; }
105 #ifdef HAVE_LONG_DOUBLE
106 /** Degrees to radians */
107 inline long double DEG2RAD(const long double x) { return x * M_PIl / 180.0; }
108 /** Radians to degrees */
109 inline long double RAD2DEG(const long double x) { return x * 180.0 / M_PIl; }
110 #endif
111 
112 #define DEG2RAD \
113  DEG2RAD // This is required to avoid other libs (like PCL) to #define their
114 // own versions of DEG2RAD
115 #define RAD2DEG \
116  RAD2DEG // This is required to avoid other libs (like PCL) to #define their
117 // own versions of RAD2DEG
118 
119 /** Returns the sign of X as "1" or "-1" */
120 template <typename T>
121 inline int sign(T x)
122 {
123  return x < 0 ? -1 : 1;
124 }
125 
126 /** Returns the sign of X as "0", "1" or "-1" */
127 template <typename T>
128 inline int signWithZero(T x)
129 {
130  return (x == 0 || x == -0) ? 0 : sign(x);
131 }
132 
133 /** Returns the lowest, possitive among two numbers. If both are non-positive
134  * (<=0), the lowest one is returned. */
135 template <typename T>
136 T lowestPositive(const T a, const T b)
137 {
138  if (a > 0 && a <= b)
139  return a; // a positive and smaller than b
140  else if (b > 0)
141  return b; // b is positive and either smaller than a or a is negative
142  else
143  return a; // at least b is negative, we might not have an answer
144 }
145 
146 /** Efficient and portable evaluation of the absolute difference of two unsigned
147  * integer values
148  * (but will also work for signed and floating point types) */
149 template <typename T>
150 inline T abs_diff(const T a, const T b)
151 {
152  return std::max(a, b) - std::min(a, b);
153 }
154 
155 template <typename T>
156 inline const T min3(const T& A, const T& B, const T& C)
157 {
158  return std::min<T>(A, std::min<T>(B, C));
159 }
160 template <typename T>
161 inline const T max3(const T& A, const T& B, const T& C)
162 {
163  return std::max<T>(A, std::max<T>(B, C));
164 }
165 
166 /** Rounds toward zero */
167 template <typename T>
168 inline int fix(T x)
169 {
170  return x > 0 ? static_cast<int>(floor(static_cast<double>(x)))
171  : static_cast<int>(ceil(static_cast<double>(x)));
172 }
173 
174 /** Allow square() to be available under mrpt::math and mrpt::utils */
175 using mrpt::math::square;
176 
177 /** Utility to get a cast'ed pointer from a smart pointer */
178 template <class R, class SMART_PTR>
179 inline R* getAs(SMART_PTR& o)
180 {
181  return static_cast<R*>(&(*o));
182 }
183 
184 /** Utility to get a cast'ed pointer from a smart pointer */
185 template <class R, class SMART_PTR>
186 inline const R* getAs(const SMART_PTR& o)
187 {
188  return static_cast<const R*>(&(*o));
189 }
190 
191 /** Reverse the order of the bytes of a given type (useful for transforming btw
192  * little/big endian) */
193 void reverseBytesInPlace(bool& v_in_out);
194 void reverseBytesInPlace(uint8_t& v_in_out);
195 void reverseBytesInPlace(int8_t& v_in_out);
196 void reverseBytesInPlace(uint16_t& v_in_out);
197 void reverseBytesInPlace(int16_t& v_in_out);
198 void reverseBytesInPlace(uint32_t& v_in_out);
199 void reverseBytesInPlace(int32_t& v_in_out);
200 void reverseBytesInPlace(uint64_t& v_in_out);
201 void reverseBytesInPlace(int64_t& v_in_out);
202 void reverseBytesInPlace(float& v_in_out);
203 void reverseBytesInPlace(double& v_in_out);
204 #ifdef HAVE_LONG_DOUBLE
205 void reverseBytesInPlace(long double& v_in_out);
206 #endif
207 
208 /** Reverse the order of the bytes of a given type (useful for transforming btw
209  * little/big endian) */
210 template <class T>
211 inline void reverseBytes(const T& v_in, T& v_out)
212 {
213  v_out = v_in;
214  reverseBytesInPlace(v_out);
215 }
216 
217 /** If the second argument is below the first one, set the first argument to
218  * this lower value. */
219 template <typename T, typename K>
220 inline void keep_min(T& var, const K test_val)
221 {
222  if (test_val < var) var = test_val;
223 }
224 /** If the second argument is above the first one, set the first argument to
225  * this higher value. */
226 template <typename T, typename K>
227 inline void keep_max(T& var, const K test_val)
228 {
229  if (test_val > var) var = test_val;
230 }
231 /** Saturate the value of var (the variable gets modified) so it does not get
232  * out of [min,max]. */
233 template <typename T>
234 inline void saturate(T& var, const T sat_min, const T sat_max)
235 {
236  if (var > sat_max) var = sat_max;
237  if (var < sat_min) var = sat_min;
238 }
239 /** Like saturate() but it returns the value instead of modifying the variable
240  */
241 template <typename T>
242 inline T saturate_val(const T& value, const T sat_min, const T sat_max)
243 {
244  T var = value;
245  if (var > sat_max) var = sat_max;
246  if (var < sat_min) var = sat_min;
247  return var;
248 }
249 
250 /** Calls "delete" to free an object only if the pointer is not nullptr, then
251  * set the pointer to NULL. */
252 template <class T>
253 void delete_safe(T*& ptr)
254 {
255  if (ptr)
256  {
257  delete ptr;
258  ptr = nullptr;
259  }
260 }
261 
262 /** Like calling a std::vector<>'s clear() method, but really forcing
263  * deallocating the memory. */
264 template <class VECTOR_T>
265 inline void vector_strong_clear(VECTOR_T& v)
266 {
267  VECTOR_T dummy;
268  dummy.swap(v);
269 }
270 
271 /** Returns the smaller number >=len such that it's a multiple of 4 */
272 template <typename T>
274 {
275  if (0 != (len & 0x03)) len += (4 - (len & 0x03));
276  return len;
277 }
278 
279 #define SELBYTE0(v) (v & 0xff)
280 #define SELBYTE1(v) ((v >> 8) & 0xff)
281 #define SELBYTE2(v) ((v >> 16) & 0xff)
282 #define SELBYTE3(v) ((v >> 24) & 0xff)
283 
284 #define MAKEWORD16B(__LOBYTE, __HILOBYTE) ((__LOBYTE) | ((__HILOBYTE) << 8))
285 #define MAKEWORD32B(__LOWORD16, __HIWORD16) \
286  ((__LOWORD16) | ((__HIWORD16) << 16))
287 #define MAKEWORD64B(__LOWORD32, __HIWORD32) \
288  ((__LOWORD32) | ((__HIWORD32) << 32))
289 
290 } // End of namespace
291 } // end of namespace
#define min(a, b)
unsigned __int16 uint16_t
Definition: rptypes.h:44
const T min3(const T &A, const T &B, const T &C)
Definition: bits.h:156
size_t size(const MATRIXLIKE &m, const int dim)
Definition: bits.h:41
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...
Definition: bits.h:63
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
T abs_diff(const T a, const T b)
Efficient and portable evaluation of the absolute difference of two unsigned integer values (but will...
Definition: bits.h:150
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.
Definition: bits.h:242
void global_profiler_leave(const char *func_name) noexcept
Definition: CTimeLogger.cpp:48
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].
Definition: bits.h:234
signed char int8_t
Definition: rptypes.h:40
#define M_PI
Definition: bits.h:92
R * getAs(SMART_PTR &o)
Utility to get a cast&#39;ed pointer from a smart pointer.
Definition: bits.h:179
GLenum GLsizei len
Definition: glext.h:4712
void reverseBytesInPlace(bool &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
Definition: bits.cpp:94
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".
Definition: bits.h:128
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:55
unsigned char uint8_t
Definition: rptypes.h:41
void delete_safe(T *&ptr)
Calls "delete" to free an object only if the pointer is not nullptr, then set the pointer to NULL...
Definition: bits.h:253
CProfilerProxy(const char *func_name)
Definition: bits.h:78
#define M_PIf
__int16 int16_t
Definition: rptypes.h:43
T length2length4N(T len)
Returns the smaller number >=len such that it&#39;s a multiple of 4.
Definition: bits.h:273
__int64 int64_t
Definition: rptypes.h:49
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLubyte GLubyte b
Definition: glext.h:6279
double DEG2RAD(const double x)
Degrees to radians.
Definition: bits.h:96
const char * f
Definition: bits.h:77
GLsizei const GLchar ** string
Definition: glext.h:4101
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...
Definition: bits.h:227
int sign(T x)
Returns the sign of X as "1" or "-1".
Definition: bits.h:121
void reverseBytes(const T &v_in, T &v_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) ...
Definition: bits.h:211
__int32 int32_t
Definition: rptypes.h:46
unsigned __int64 uint64_t
Definition: rptypes.h:50
void vector_strong_clear(VECTOR_T &v)
Like calling a std::vector<>&#39;s clear() method, but really forcing deallocating the memory...
Definition: bits.h:265
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void global_profiler_enter(const char *func_name) noexcept
Definition: CTimeLogger.cpp:44
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...
Definition: bits.h:220
const float R
T square(const T x)
Inline function for the square of a number.
const T max3(const T &A, const T &B, const T &C)
Definition: bits.h:161
GLenum GLint GLint y
Definition: glext.h:3538
double RAD2DEG(const double x)
Radians to degrees.
Definition: bits.h:102
GLsizei const GLfloat * value
Definition: glext.h:4117
GLenum GLint x
Definition: glext.h:3538
unsigned __int32 uint32_t
Definition: rptypes.h:47
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
T lowestPositive(const T a, const T b)
Returns the lowest, possitive among two numbers.
Definition: bits.h:136
int fix(T x)
Rounds toward zero.
Definition: bits.h:168



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019