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 #define _USE_MATH_DEFINES // (For VS to define M_PI, etc. in cmath)
13 #include <cmath> // floor()
14 #include <algorithm> // reverse()
15 
16 /** This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries. */
17 namespace mrpt
18 {
19  /** A std::string version of C sprintf.
20  * You can call this to obtain a std::string using printf-like syntax.
21  * Based on very nice code by Paul Senzee, published at http://senzee.blogspot.com/2006/05/c-formatting-stdstring.html
22  * Function implemented in format.cpp
23  */
24  std::string BASE_IMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
25 
26  namespace math
27  {
28  bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS;
29  bool BASE_IMPEXP isNaN(double v) MRPT_NO_THROWS;
30  bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS;
31  bool BASE_IMPEXP isFinite(double v) MRPT_NO_THROWS;
32 
33  // This inline function is used everywhere, so just move it here even it's not a forward declaration!
34  /*! Returns the size of the matrix in the i'th dimension: 1=rows, 2=columns (MATLAB-compatible function)
35  * \note Template argument MATRIXLIKE can be: mrpt::math::CMatrixTemplate, mrpt::math::CMatrixTemplateNumeric, mrpt::math::CMatrixFixedNumeric
36  */
37  template <class MATRIXLIKE>
38  inline size_t size( const MATRIXLIKE& m, int dim )
39  {
40  if (dim==1) return m.getRowCount();
41  else if (dim==2) return m.getColCount();
42  else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
43  }
44  }
45 
46  namespace utils
47  {
48  class CFileStream;
49  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
50  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
51 
52  struct CProfilerProxy {
53  const char*f;
54  CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
56  };
57 
58 #ifdef DEG2RAD // functions are preferred over macros
59 #undef DEG2RAD
60 #endif
61 #ifdef RAD2DEG
62 #undef RAD2DEG
63 #endif
64 #if !defined(M_PI)
65 # define M_PI 3.14159265358979323846
66 #endif
67 
68  /** Degrees to radians */
69  inline double DEG2RAD(const double x) { return x*M_PI/180.0; }
70  /** Degrees to radians */
71  inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
72  /** Degrees to radians */
73  inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
74  /** Radians to degrees */
75  inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
76  /** Radians to degrees */
77  inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
78 
79 # ifdef HAVE_LONG_DOUBLE
80  /** Degrees to radians */
81  inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
82  /** Radians to degrees */
83  inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
84 # endif
85 
86 #define DEG2RAD DEG2RAD // This is required to avoid other libs (like PCL) to #define their own versions of DEG2RAD
87 #define RAD2DEG RAD2DEG // This is required to avoid other libs (like PCL) to #define their own versions of RAD2DEG
88 
89  /** Returns the sign of X as "1" or "-1" */
90  template <typename T>
91  inline int sign(T x) { return x<0 ? -1:1; }
92 
93  /** Returns the sign of X as "0", "1" or "-1" */
94  template <typename T>
95  inline int signWithZero(T x) { return x==0?0:sign(x);}
96 
97  /** Efficient and portable evaluation of the absolute difference of two unsigned integer values
98  * (but will also work for signed and floating point types) */
99  template <typename T>
100  inline T abs_diff(const T a, const T b) {
101  return std::max(a,b) - std::min(a,b);
102  }
103 
104  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) ); }
105  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) ); }
106 
107  /** Rounds toward zero */
108  template <typename T>
109  inline int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
110 
111  /** Inline function for the square of a number. */
112  template<class T>
113  inline T square(const T x) { return x*x; }
114 
115  /** Utility to get a cast'ed pointer from a smart pointer */
116  template <class R, class SMART_PTR>
117  inline R* getAs(SMART_PTR &o) { return static_cast<R*>( & (*o) ); }
118 
119  /** Utility to get a cast'ed pointer from a smart pointer */
120  template <class R, class SMART_PTR>
121  inline const R* getAs(const SMART_PTR &o) { return static_cast<const R*>( & (*o) ); }
122 
123  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
124  template <class T> inline void reverseBytesInPlace(T& v_in_out)
125  {
126  unsigned char *ptr = reinterpret_cast<unsigned char*>(&v_in_out);
127  std::reverse(ptr,ptr+sizeof(T));
128  }
129 
130  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
131  template <class T> inline void reverseBytes(const T &v_in, T& v_out)
132  {
133  v_out = v_in;
134  reverseBytesInPlace(v_out);
135  }
136 
137 
138  /** If the second argument is below the first one, set the first argument to this lower value. */
139  template <typename T,typename K>
140  inline void keep_min(T &var, const K test_val) {
141  if (test_val<var) var = test_val;
142  }
143  /** If the second argument is above the first one, set the first argument to this higher value. */
144  template <typename T,typename K>
145  inline void keep_max(T &var, const K test_val) {
146  if (test_val>var) var = test_val;
147  }
148  /** Saturate the value of var (the variable gets modified) so it does not get out of [min,max]. */
149  template <typename T>
150  inline void saturate(T &var, const T sat_min, const T sat_max) {
151  if (var>sat_max) var = sat_max;
152  if (var<sat_min) var = sat_min;
153  }
154  /** Like saturate() but it returns the value instead of modifying the variable */
155  template <typename T>
156  inline T saturate_val(const T &value, const T sat_min, const T sat_max) {
157  T var=value;
158  if (var>sat_max) var = sat_max;
159  if (var<sat_min) var = sat_min;
160  return var;
161  }
162 
163  /** Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL. */
164  template <class T>
165  void delete_safe(T *& ptr) {
166  if (ptr) {
167  delete ptr;
168  ptr = NULL;
169  }
170  }
171 
172  /** Like calling a std::vector<>'s clear() method, but really forcing deallocating the memory. */
173  template <class VECTOR_T>
174  inline void vector_strong_clear(VECTOR_T & v) { VECTOR_T dummy; dummy.swap(v); }
175 
176  } // End of namespace
177 } // end of namespace
#define M_PI
Definition: bits.h:65
bool BASE_IMPEXP isNaN(float f) MRPT_NO_THROWS
Returns true if the number is NaN.
bool BASE_IMPEXP isFinite(float f) MRPT_NO_THROWS
Returns true if the number is non infinity.
#define THROW_EXCEPTION_CUSTOM_MSG1(msg, param1)
#define M_PIf
Definition: mrpt_macros.h:366
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
Definition: mrpt_macros.h:379
#define MRPT_NO_THROWS
Used after member declarations.
Definition: mrpt_macros.h:391
size_t size(const MATRIXLIKE &m, int dim)
Definition: bits.h:38
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:156
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".
Definition: bits.h:95
R * getAs(SMART_PTR &o)
Utility to get a cast'ed pointer from a smart pointer.
Definition: bits.h:117
double DEG2RAD(const double x)
Degrees to radians.
Definition: bits.h:69
int sign(T x)
Returns the sign of X as "1" or "-1".
Definition: bits.h:91
void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS
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:145
void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS
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:131
double RAD2DEG(const double x)
Radians to degrees.
Definition: bits.h:75
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:113
void vector_strong_clear(VECTOR_T &v)
Like calling a std::vector<>'s clear() method, but really forcing deallocating the memory.
Definition: bits.h:174
void reverseBytesInPlace(T &v_in_out)
Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)
Definition: bits.h:124
int fix(T x)
Rounds toward zero
Definition: bits.h:109
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:100
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,...
Definition: bits.h:150
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:165
const T max3(const T &A, const T &B, const T &C)
Definition: bits.h:105
const T min3(const T &A, const T &B, const T &C)
Definition: bits.h:104
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:140
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
CProfilerProxy(const char *func_name)
Definition: bits.h:54
const char * f
Definition: bits.h:53



Page generated by Doxygen 1.9.1 for MRPT 1.4.0 SVN: at Mon Apr 18 03:56:21 UTC 2022