Main MRPT website > C++ reference
MRPT logo

bits.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 
00029 #ifndef UTILSDEFS_H
00030 #error "This file is intended for include from utils_defs.h only!"
00031 #endif
00032 
00033 /** This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
00034  */
00035 namespace mrpt
00036 {
00037         /** A std::string version of C sprintf.
00038           *  You can call this to obtain a std::string using printf-like syntax.
00039           *  Based on very nice code by Paul Senzee, published at http://senzee.blogspot.com/2006/05/c-formatting-stdstring.html
00040           *  Function implemented in format.cpp
00041           */
00042         std::string BASE_IMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
00043 
00044         namespace system
00045         {
00046                 // Forward definition: (Required for Visual C++ 6 implementation of THROW_EXCEPTION...)
00047                 std::string BASE_IMPEXP extractFileName(const std::string &filePath);
00048                 std::string BASE_IMPEXP stack_trace(bool calling_from_exception);
00049         }
00050 
00051         namespace math
00052         {
00053                 bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS;
00054                 bool BASE_IMPEXP isNaN(double v) MRPT_NO_THROWS;
00055                 bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS;
00056                 bool BASE_IMPEXP isFinite(double v) MRPT_NO_THROWS;
00057 
00058                 // This inline function is used everywhere, so just move it here even it's not a forward declaration!
00059                 /*! Returns the size of the matrix in the i'th dimension: 1=rows, 2=columns (MATLAB-compatible function)
00060                   *  \note Template argument MATRIXLIKE can be: CMatrixTemplate, CMatrixTemplateNumeric, CMatrixFixedNumeric
00061                   */
00062                 template <class MATRIXLIKE>
00063                 inline size_t size( const MATRIXLIKE& m, int dim )
00064                 {
00065                         if (dim==1) return m.getRowCount();
00066                         else if (dim==2) return m.getColCount();
00067                         else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
00068                 }
00069         }
00070 
00071         namespace utils
00072         {
00073                 class CFileStream;
00074                 void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
00075                 void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
00076 
00077                 struct CProfilerProxy {
00078                         const char*f;
00079                         CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
00080                         ~CProfilerProxy() { global_profiler_leave(f); }
00081                 };
00082 
00083                 /** Degrees to radians */
00084                 inline double DEG2RAD(const double x) { return x*M_PI/180.0;    }
00085                 /** Degrees to radians */
00086                 inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
00087                 /** Degrees to radians */
00088                 inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
00089                 /** Radians to degrees */
00090                 inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
00091                 /** Radians to degrees */
00092                 inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
00093 
00094 #       ifdef HAVE_LONG_DOUBLE
00095                 /** Degrees to radians */
00096                 inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
00097                 /** Radians to degrees */
00098                 inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
00099 #       endif
00100 
00101                 /** Returns the sign of X as "1" or "-1" */
00102                 template <typename T>
00103                 inline int sign(T x) { return x<0 ? -1:1; }
00104 
00105                 /** Returns the sign of X as "0", "1" or "-1" */
00106                 template <typename T>
00107                 inline int signWithZero(T x)    { return x==0?0:sign(x);}
00108 
00109                 /** Returns the closer integer (int) to x */
00110                 template <typename T>
00111                 inline int round(const T value)
00112                 {
00113                 #if MRPT_HAS_SSE2
00114                         __m128d t = _mm_set_sd( value );
00115                         return _mm_cvtsd_si32(t);
00116                 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
00117                         int t;
00118                         __asm
00119                         {
00120                                 fld value;
00121                                 fistp t;
00122                         }
00123                         return t;
00124                 #elif defined HAVE_LRINT || defined __GNUC__
00125                         return static_cast<int>(lrint(value));
00126                 #else
00127                         return static_cast<int>(value + 0.5);
00128                 #endif
00129                 }
00130 
00131                 /** Returns the closer integer (long) to x */
00132                 template <typename T>
00133                 inline long round_long(const T value)
00134                 {
00135                 #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
00136                         __m128d t = _mm_set_sd( value );
00137                         return _mm_cvtsd_si64(t);
00138                 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
00139                         long t;
00140                         __asm
00141                         {
00142                                 fld value;
00143                                 fistp t;
00144                         }
00145                         return t;
00146                 #elif defined HAVE_LRINT || defined __GNUC__
00147                         return lrint(value);
00148                 #else
00149                         return static_cast<long>(value + 0.5);
00150                 #endif
00151                 }
00152 
00153                 /** Rounds toward zero  */
00154                 template <typename T>
00155                 inline int fix(T x) { return  x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
00156 
00157                 /** Inline function for the square of a number. */
00158                 template<class T>
00159                 inline T square(const T x)    { return x*x; }
00160 
00161                 /** Utility to get a cast'ed pointer from a smart pointer */
00162                 template <class R, class P>
00163                 inline R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
00164 
00165                 /** Utility to get a cast'ed pointer from a smart pointer */
00166                 template <class R, class P>
00167                 inline const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
00168 
00169                 /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)  */
00170                 template <class T> inline void reverseBytesInPlace(T& v_in_out)
00171                 {
00172                         uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_in_out);
00173                         std::reverse(ptr,ptr+sizeof(T));
00174                 }
00175 
00176                 /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)  */
00177                 template <class T> inline void reverseBytes(const T &v_in, T& v_out)
00178                 {
00179                         v_out = v_in;
00180                         reverseBytesInPlace(v_out);
00181                 }
00182                 
00183 
00184                 /** If the second argument is below the first one, set the first argument to this lower value. */
00185                 template <typename T,typename K>
00186                 inline void keep_min(T &var,  const K test_val) {
00187                         if (test_val<var) var = test_val;
00188                 }
00189 
00190                 /** If the second argument is above the first one, set the first argument to this higher value. */
00191                 template <typename T,typename K>
00192                 inline void keep_max(T &var,  const K test_val) {
00193                         if (test_val>var) var = test_val;
00194                 }
00195 
00196                 /** Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL. */
00197                 template <class T>
00198                 void delete_safe(T *& ptr) {
00199                         if (ptr) {
00200                                 delete ptr;
00201                                 ptr = NULL;
00202                         }
00203                 }
00204 
00205         } // End of namespace
00206 } // end of namespace
00207 



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011