Main MRPT website > C++ reference
MRPT logo
bits.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7  | Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8  | Copyright (c) 2012-2013, University of Almeria |
9  | All rights reserved. |
10  | |
11  | Redistribution and use in source and binary forms, with or without |
12  | modification, are permitted provided that the following conditions are |
13  | met: |
14  | * Redistributions of source code must retain the above copyright |
15  | notice, this list of conditions and the following disclaimer. |
16  | * Redistributions in binary form must reproduce the above copyright |
17  | notice, this list of conditions and the following disclaimer in the |
18  | documentation and/or other materials provided with the distribution. |
19  | * Neither the name of the copyright holders nor the |
20  | names of its contributors may be used to endorse or promote products |
21  | derived from this software without specific prior written permission.|
22  | |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24  | 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25  | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26  | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29  | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31  | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32  | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33  | POSSIBILITY OF SUCH DAMAGE. |
34  +---------------------------------------------------------------------------+ */
35 
36 #ifndef UTILSDEFS_H
37 #error "This file is intended for include from utils_defs.h only!"
38 #endif
39 
40 #include <mrpt/utils/SSE_types.h> // needed by SSE intrinsics used in some inline functions below.
41 
42 /** This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
43  */
44 namespace mrpt
45 {
46  /** A std::string version of C sprintf.
47  * You can call this to obtain a std::string using printf-like syntax.
48  * Based on very nice code by Paul Senzee, published at http://senzee.blogspot.com/2006/05/c-formatting-stdstring.html
49  * Function implemented in format.cpp
50  */
51  std::string BASE_IMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
52 
53  namespace system
54  {
55  // Forward definition: (Required for Visual C++ 6 implementation of THROW_EXCEPTION...)
56  std::string BASE_IMPEXP extractFileName(const std::string &filePath);
57  std::string BASE_IMPEXP stack_trace(bool calling_from_exception);
58  }
59 
60  namespace math
61  {
62  bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS;
63  bool BASE_IMPEXP isNaN(double v) MRPT_NO_THROWS;
64  bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS;
65  bool BASE_IMPEXP isFinite(double v) MRPT_NO_THROWS;
66 
67  // This inline function is used everywhere, so just move it here even it's not a forward declaration!
68  /*! Returns the size of the matrix in the i'th dimension: 1=rows, 2=columns (MATLAB-compatible function)
69  * \note Template argument MATRIXLIKE can be: CMatrixTemplate, CMatrixTemplateNumeric, CMatrixFixedNumeric
70  */
71  template <class MATRIXLIKE>
72  inline size_t size( const MATRIXLIKE& m, int dim )
73  {
74  if (dim==1) return m.getRowCount();
75  else if (dim==2) return m.getColCount();
76  else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
77  }
78  }
79 
80  namespace utils
81  {
82  class CFileStream;
83  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
84  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
85 
86  struct CProfilerProxy {
87  const char*f;
88  CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
90  };
91 
92 #ifdef DEG2RAD // functions are preferred over macros
93 #undef DEG2RAD
94 #endif
95 #ifdef RAD2DEG
96 #undef RAD2DEG
97 #endif
98  /** Degrees to radians */
99  inline double DEG2RAD(const double x) { return x*M_PI/180.0; }
100  /** Degrees to radians */
101  inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
102  /** Degrees to radians */
103  inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
104  /** Radians to degrees */
105  inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
106  /** Radians to degrees */
107  inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
108 
109 # ifdef HAVE_LONG_DOUBLE
110  /** Degrees to radians */
111  inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
112  /** Radians to degrees */
113  inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
114 # endif
115 
116  /** Returns the sign of X as "1" or "-1" */
117  template <typename T>
118  inline int sign(T x) { return x<0 ? -1:1; }
119 
120  /** Returns the sign of X as "0", "1" or "-1" */
121  template <typename T>
122  inline int signWithZero(T x) { return x==0?0:sign(x);}
123 
124  /** Returns the closer integer (int) to x */
125  template <typename T>
126  inline int round(const T value)
127  {
128  #if MRPT_HAS_SSE2
129  __m128d t = _mm_set_sd( value );
130  return _mm_cvtsd_si32(t);
131  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
132  int t;
133  __asm
134  {
135  fld value;
136  fistp t;
137  }
138  return t;
139  #elif defined HAVE_LRINT || defined __GNUC__
140  return static_cast<int>(lrint(value));
141  #else
142  return static_cast<int>(value + 0.5);
143  #endif
144  }
145 
146  /** Returns the closer integer (long) to x */
147  template <typename T>
148  inline long round_long(const T value)
149  {
150  #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
151  __m128d t = _mm_set_sd( value );
152  return _mm_cvtsd_si64(t);
153  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
154  long t;
155  __asm
156  {
157  fld value;
158  fistp t;
159  }
160  return t;
161  #elif defined HAVE_LRINT || defined __GNUC__
162  return lrint(value);
163  #else
164  return static_cast<long>(value + 0.5);
165  #endif
166  }
167 
168  /** Efficient and portable evaluation of the absolute difference of two unsigned integer values
169  * (but will also work for signed and floating point types) */
170  template <typename T>
171  inline T abs_diff(const T a, const T b) {
172  return std::max(a,b) - std::min(a,b);
173  }
174 
175  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) ); }
176  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) ); }
177 
178  /** Rounds toward zero */
179  template <typename T>
180  inline int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
181 
182  /** Inline function for the square of a number. */
183  template<class T>
184  inline T square(const T x) { return x*x; }
185 
186  /** Utility to get a cast'ed pointer from a smart pointer */
187  template <class R, class P>
188  inline R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
189 
190  /** Utility to get a cast'ed pointer from a smart pointer */
191  template <class R, class P>
192  inline const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
193 
194  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
195  template <class T> inline void reverseBytesInPlace(T& v_in_out)
196  {
197  uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_in_out);
198  std::reverse(ptr,ptr+sizeof(T));
199  }
200 
201  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
202  template <class T> inline void reverseBytes(const T &v_in, T& v_out)
203  {
204  v_out = v_in;
205  reverseBytesInPlace(v_out);
206  }
207 
208 
209  /** If the second argument is below the first one, set the first argument to this lower value. */
210  template <typename T,typename K>
211  inline void keep_min(T &var, const K test_val) {
212  if (test_val<var) var = test_val;
213  }
214 
215  /** If the second argument is above the first one, set the first argument to this higher value. */
216  template <typename T,typename K>
217  inline void keep_max(T &var, const K test_val) {
218  if (test_val>var) var = test_val;
219  }
220 
221  /** Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL. */
222  template <class T>
223  void delete_safe(T *& ptr) {
224  if (ptr) {
225  delete ptr;
226  ptr = NULL;
227  }
228  }
229 
230  /** Like calling a std::vector<>'s clear() method, but really forcing deallocating the memory. */
231  template <class T>
232  inline void vector_strong_clear(std::vector<T> & v) { std::vector<T> dummy; dummy.swap(v); }
233 
234  } // End of namespace
235 } // end of namespace
236 
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:175
int round(const T value)
Returns the closer integer (int) to x.
Definition: bits.h:126
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:171
#define MRPT_NO_THROWS
Used after member declarations.
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".
Definition: bits.h:122
#define MRPT_printf_format_check(_FMT_, _VARARGS_)
long round_long(const T value)
Returns the closer integer (long) to x.
Definition: bits.h:148
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:223
CProfilerProxy(const char *func_name)
Definition: bits.h:88
#define M_PIf
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:195
double DEG2RAD(const double x)
Degrees to radians.
Definition: bits.h:99
void vector_strong_clear(std::vector< T > &v)
Like calling a std::vector<>&#39;s clear() method, but really forcing deallocating the memory...
Definition: bits.h:232
const char * f
Definition: bits.h:87
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:217
int sign(T x)
Returns the sign of X as "1" or "-1".
Definition: bits.h:118
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:202
R * getAs(stlplus::smart_ptr_clone< P > &o)
Utility to get a cast&#39;ed pointer from a smart pointer.
Definition: bits.h:188
std::string BASE_IMPEXP stack_trace(bool calling_from_exception=false)
Dumps the current program stack with detailed information of source files and lines.
T square(const T x)
Inline function for the square of a number.
Definition: bits.h:184
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string BASE_IMPEXP extractFileName(const std::string &filePath)
Extract just the name (without extension) of a filename from a complete path plus name plus extension...
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:211
size_t size(const MATRIXLIKE &m, int dim)
Definition: bits.h:72
const T max3(const T &A, const T &B, const T &C)
Definition: bits.h:176
double RAD2DEG(const double x)
Radians to degrees.
Definition: bits.h:105
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)
#define M_PI
Definition: mrpt_macros.h:408
int fix(T x)
Rounds toward zero.
Definition: bits.h:180



Page generated by Doxygen 1.8.14 for MRPT 1.0.2 SVN: at lun oct 28 00:52:41 CET 2019 Hosted on:
SourceForge.net Logo