Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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;
35  bool BASE_IMPEXP isFinite(float 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, int dim )
44  {
45  if (dim==1) return m.getRowCount();
46  else if (dim==2) return m.getColCount();
47  else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
48  }
49  }
50 
51  namespace utils
52  {
53  class CFileStream;
54  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
55  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
56 
57  struct CProfilerProxy {
58  const char*f;
59  CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
61  };
62 
63 #ifdef DEG2RAD // functions are preferred over macros
64 #undef DEG2RAD
65 #endif
66 #ifdef RAD2DEG
67 #undef RAD2DEG
68 #endif
69 #if !defined(M_PI)
70 # define M_PI 3.14159265358979323846
71 #endif
72 
73  /** Degrees to radians */
74  inline double DEG2RAD(const double x) { return x*M_PI/180.0; }
75  /** Degrees to radians */
76  inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
77  /** Degrees to radians */
78  inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
79  /** Radians to degrees */
80  inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
81  /** Radians to degrees */
82  inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
83 
84 # ifdef HAVE_LONG_DOUBLE
85  /** Degrees to radians */
86  inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
87  /** Radians to degrees */
88  inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
89 # endif
90 
91 #define DEG2RAD DEG2RAD // This is required to avoid other libs (like PCL) to #define their own versions of DEG2RAD
92 #define RAD2DEG RAD2DEG // This is required to avoid other libs (like PCL) to #define their own versions of RAD2DEG
93 
94  /** Returns the sign of X as "1" or "-1" */
95  template <typename T>
96  inline int sign(T x) { return x<0 ? -1:1; }
97 
98  /** Returns the sign of X as "0", "1" or "-1" */
99  template <typename T>
100  inline int signWithZero(T x) { return x==0?0:sign(x);}
101 
102  /** Efficient and portable evaluation of the absolute difference of two unsigned integer values
103  * (but will also work for signed and floating point types) */
104  template <typename T>
105  inline T abs_diff(const T a, const T b) {
106  return std::max(a,b) - std::min(a,b);
107  }
108 
109  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) ); }
110  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) ); }
111 
112  /** Rounds toward zero */
113  template <typename T>
114  inline int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
115 
116  /** Inline function for the square of a number. */
117  template<class T>
118  inline T square(const T x) { return x*x; }
119 
120  /** Utility to get a cast'ed pointer from a smart pointer */
121  template <class R, class SMART_PTR>
122  inline R* getAs(SMART_PTR &o) { return static_cast<R*>( & (*o) ); }
123 
124  /** Utility to get a cast'ed pointer from a smart pointer */
125  template <class R, class SMART_PTR>
126  inline const R* getAs(const SMART_PTR &o) { return static_cast<const R*>( & (*o) ); }
127 
128  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
129  void BASE_IMPEXP reverseBytesInPlace(bool& v_in_out);
130  void BASE_IMPEXP reverseBytesInPlace(uint8_t& v_in_out);
131  void BASE_IMPEXP reverseBytesInPlace(int8_t& v_in_out);
132  void BASE_IMPEXP reverseBytesInPlace(uint16_t& v_in_out);
133  void BASE_IMPEXP reverseBytesInPlace(int16_t& v_in_out);
134  void BASE_IMPEXP reverseBytesInPlace(uint32_t& v_in_out);
135  void BASE_IMPEXP reverseBytesInPlace(int32_t& v_in_out);
136  void BASE_IMPEXP reverseBytesInPlace(uint64_t& v_in_out);
137  void BASE_IMPEXP reverseBytesInPlace(int64_t& v_in_out);
138  void BASE_IMPEXP reverseBytesInPlace(float& v_in_out);
139  void BASE_IMPEXP reverseBytesInPlace(double& v_in_out);
140 #ifdef HAVE_LONG_DOUBLE
141  void BASE_IMPEXP reverseBytesInPlace(long double& v_in_out);
142 #endif
143 
144  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
145  template <class T> inline void reverseBytes(const T &v_in, T& v_out)
146  {
147  v_out = v_in;
148  reverseBytesInPlace(v_out);
149  }
150 
151 
152  /** If the second argument is below the first one, set the first argument to this lower value. */
153  template <typename T,typename K>
154  inline void keep_min(T &var, const K test_val) {
155  if (test_val<var) var = test_val;
156  }
157  /** If the second argument is above the first one, set the first argument to this higher value. */
158  template <typename T,typename K>
159  inline void keep_max(T &var, const K test_val) {
160  if (test_val>var) var = test_val;
161  }
162  /** Saturate the value of var (the variable gets modified) so it does not get out of [min,max]. */
163  template <typename T>
164  inline void saturate(T &var, const T sat_min, const T sat_max) {
165  if (var>sat_max) var = sat_max;
166  if (var<sat_min) var = sat_min;
167  }
168  /** Like saturate() but it returns the value instead of modifying the variable */
169  template <typename T>
170  inline T saturate_val(const T &value, const T sat_min, const T sat_max) {
171  T var=value;
172  if (var>sat_max) var = sat_max;
173  if (var<sat_min) var = sat_min;
174  return var;
175  }
176 
177  /** Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL. */
178  template <class T>
179  void delete_safe(T *& ptr) {
180  if (ptr) {
181  delete ptr;
182  ptr = NULL;
183  }
184  }
185 
186  /** Like calling a std::vector<>'s clear() method, but really forcing deallocating the memory. */
187  template <class VECTOR_T>
188  inline void vector_strong_clear(VECTOR_T & v) { VECTOR_T dummy; dummy.swap(v); }
189 
190  } // End of namespace
191 } // end of namespace
bool BASE_IMPEXP isFinite(float f) MRPT_NO_THROWS
Returns true if the number is non infinity.
const T min3(const T &A, const T &B, const T &C)
Definition: bits.h:109
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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:105
#define MRPT_NO_THROWS
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:170
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:164
#define M_PI
Definition: bits.h:70
R * getAs(SMART_PTR &o)
Utility to get a cast&#39;ed pointer from a smart pointer.
Definition: bits.h:122
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".
Definition: bits.h:100
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
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) ...
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:179
CProfilerProxy(const char *func_name)
Definition: bits.h:59
#define M_PIf
double DEG2RAD(const double x)
Degrees to radians.
Definition: bits.h:74
const char * f
Definition: bits.h:58
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:159
int sign(T x)
Returns the sign of X as "1" or "-1".
Definition: bits.h:96
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:145
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:118
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:188
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool BASE_IMPEXP isNaN(float f) MRPT_NO_THROWS
Returns true if the number is NaN.
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:154
size_t size(const MATRIXLIKE &m, int dim)
Definition: bits.h:43
const T max3(const T &A, const T &B, const T &C)
Definition: bits.h:110
double RAD2DEG(const double x)
Radians to degrees.
Definition: bits.h:80
void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS
void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)
int fix(T x)
Rounds toward zero.
Definition: bits.h:114



Page generated by Doxygen 1.8.11 for MRPT 1.4.0 SVN: at Mon Aug 15 11:50:21 UTC 2016