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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020