Main MRPT website > C++ reference
MRPT logo
bits.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 
29 #ifndef UTILSDEFS_H
30 #error "This file is intended for include from utils_defs.h only!"
31 #endif
32 
33 #include <mrpt/utils/SSE_types.h> // needed by SSE intrinsics used in some inline functions below.
34 
35 /** This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
36  */
37 namespace mrpt
38 {
39  /** A std::string version of C sprintf.
40  * You can call this to obtain a std::string using printf-like syntax.
41  * Based on very nice code by Paul Senzee, published at http://senzee.blogspot.com/2006/05/c-formatting-stdstring.html
42  * Function implemented in format.cpp
43  */
44  std::string BASE_IMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
45 
46  namespace system
47  {
48  // Forward definition: (Required for Visual C++ 6 implementation of THROW_EXCEPTION...)
49  std::string BASE_IMPEXP extractFileName(const std::string &filePath);
50  std::string BASE_IMPEXP stack_trace(bool calling_from_exception);
51  }
52 
53  namespace math
54  {
55  bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS;
56  bool BASE_IMPEXP isNaN(double v) MRPT_NO_THROWS;
57  bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS;
58  bool BASE_IMPEXP isFinite(double v) MRPT_NO_THROWS;
59 
60  // This inline function is used everywhere, so just move it here even it's not a forward declaration!
61  /*! Returns the size of the matrix in the i'th dimension: 1=rows, 2=columns (MATLAB-compatible function)
62  * \note Template argument MATRIXLIKE can be: CMatrixTemplate, CMatrixTemplateNumeric, CMatrixFixedNumeric
63  */
64  template <class MATRIXLIKE>
65  inline size_t size( const MATRIXLIKE& m, int dim )
66  {
67  if (dim==1) return m.getRowCount();
68  else if (dim==2) return m.getColCount();
69  else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
70  }
71  }
72 
73  namespace utils
74  {
75  class CFileStream;
76  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
77  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
78 
79  struct CProfilerProxy {
80  const char*f;
81  CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
83  };
84 
85  /** Degrees to radians */
86  inline double DEG2RAD(const double x) { return x*M_PI/180.0; }
87  /** Degrees to radians */
88  inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
89  /** Degrees to radians */
90  inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
91  /** Radians to degrees */
92  inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
93  /** Radians to degrees */
94  inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
95 
96 # ifdef HAVE_LONG_DOUBLE
97  /** Degrees to radians */
98  inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
99  /** Radians to degrees */
100  inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
101 # endif
102 
103  /** Returns the sign of X as "1" or "-1" */
104  template <typename T>
105  inline int sign(T x) { return x<0 ? -1:1; }
106 
107  /** Returns the sign of X as "0", "1" or "-1" */
108  template <typename T>
109  inline int signWithZero(T x) { return x==0?0:sign(x);}
110 
111  /** Returns the closer integer (int) to x */
112  template <typename T>
113  inline int round(const T value)
114  {
115  #if MRPT_HAS_SSE2
116  __m128d t = _mm_set_sd( value );
117  return _mm_cvtsd_si32(t);
118  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
119  int t;
120  __asm
121  {
122  fld value;
123  fistp t;
124  }
125  return t;
126  #elif defined HAVE_LRINT || defined __GNUC__
127  return static_cast<int>(lrint(value));
128  #else
129  return static_cast<int>(value + 0.5);
130  #endif
131  }
132 
133  /** Returns the closer integer (long) to x */
134  template <typename T>
135  inline long round_long(const T value)
136  {
137  #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
138  __m128d t = _mm_set_sd( value );
139  return _mm_cvtsd_si64(t);
140  #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
141  long t;
142  __asm
143  {
144  fld value;
145  fistp t;
146  }
147  return t;
148  #elif defined HAVE_LRINT || defined __GNUC__
149  return lrint(value);
150  #else
151  return static_cast<long>(value + 0.5);
152  #endif
153  }
154 
155  /** Rounds toward zero */
156  template <typename T>
157  inline int fix(T x) { return x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
158 
159  /** Inline function for the square of a number. */
160  template<class T>
161  inline T square(const T x) { return x*x; }
162 
163  /** Utility to get a cast'ed pointer from a smart pointer */
164  template <class R, class P>
165  inline R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
166 
167  /** Utility to get a cast'ed pointer from a smart pointer */
168  template <class R, class P>
169  inline const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
170 
171  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
172  template <class T> inline void reverseBytesInPlace(T& v_in_out)
173  {
174  uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_in_out);
175  std::reverse(ptr,ptr+sizeof(T));
176  }
177 
178  /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian) */
179  template <class T> inline void reverseBytes(const T &v_in, T& v_out)
180  {
181  v_out = v_in;
182  reverseBytesInPlace(v_out);
183  }
184 
185 
186  /** If the second argument is below the first one, set the first argument to this lower value. */
187  template <typename T,typename K>
188  inline void keep_min(T &var, const K test_val) {
189  if (test_val<var) var = test_val;
190  }
191 
192  /** If the second argument is above the first one, set the first argument to this higher value. */
193  template <typename T,typename K>
194  inline void keep_max(T &var, const K test_val) {
195  if (test_val>var) var = test_val;
196  }
197 
198  /** Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL. */
199  template <class T>
200  void delete_safe(T *& ptr) {
201  if (ptr) {
202  delete ptr;
203  ptr = NULL;
204  }
205  }
206 
207  /** Like calling a std::vector<>'s clear() method, but really forcing deallocating the memory. */
208  template <class T>
209  inline void vector_strong_clear(std::vector<T> & v) { std::vector<T> dummy; dummy.swap(v); }
210 
211  } // End of namespace
212 } // end of namespace
213 



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013