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://www.mrpt.org/                                |
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 #include <mrpt/utils/SSE_types.h>  // needed by SSE intrinsics used in some inline functions below.
00034 
00035 /** This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
00036  */
00037 namespace mrpt
00038 {
00039         /** A std::string version of C sprintf.
00040           *  You can call this to obtain a std::string using printf-like syntax.
00041           *  Based on very nice code by Paul Senzee, published at http://senzee.blogspot.com/2006/05/c-formatting-stdstring.html
00042           *  Function implemented in format.cpp
00043           */
00044         std::string BASE_IMPEXP format(const char *fmt, ...) MRPT_printf_format_check(1,2);
00045 
00046         namespace system
00047         {
00048                 // Forward definition: (Required for Visual C++ 6 implementation of THROW_EXCEPTION...)
00049                 std::string BASE_IMPEXP extractFileName(const std::string &filePath);
00050                 std::string BASE_IMPEXP stack_trace(bool calling_from_exception);
00051         }
00052 
00053         namespace math
00054         {
00055                 bool BASE_IMPEXP isNaN(float v) MRPT_NO_THROWS;
00056                 bool BASE_IMPEXP isNaN(double v) MRPT_NO_THROWS;
00057                 bool BASE_IMPEXP isFinite(float v) MRPT_NO_THROWS;
00058                 bool BASE_IMPEXP isFinite(double v) MRPT_NO_THROWS;
00059 
00060                 // This inline function is used everywhere, so just move it here even it's not a forward declaration!
00061                 /*! Returns the size of the matrix in the i'th dimension: 1=rows, 2=columns (MATLAB-compatible function)
00062                   *  \note Template argument MATRIXLIKE can be: CMatrixTemplate, CMatrixTemplateNumeric, CMatrixFixedNumeric
00063                   */
00064                 template <class MATRIXLIKE>
00065                 inline size_t size( const MATRIXLIKE& m, int dim )
00066                 {
00067                         if (dim==1) return m.getRowCount();
00068                         else if (dim==2) return m.getColCount();
00069                         else THROW_EXCEPTION_CUSTOM_MSG1("size: Queried matrix dimension must be 1 or 2. Called with i=%i",dim);
00070                 }
00071         }
00072 
00073         namespace utils
00074         {
00075                 class CFileStream;
00076                 void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
00077                 void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
00078 
00079                 struct CProfilerProxy {
00080                         const char*f;
00081                         CProfilerProxy(const char*func_name) : f(func_name) { global_profiler_enter(f); }
00082                         ~CProfilerProxy() { global_profiler_leave(f); }
00083                 };
00084 
00085                 /** Degrees to radians */
00086                 inline double DEG2RAD(const double x) { return x*M_PI/180.0;    }
00087                 /** Degrees to radians */
00088                 inline float DEG2RAD(const float x) { return x*M_PIf/180.0f; }
00089                 /** Degrees to radians */
00090                 inline float DEG2RAD(const int x) { return x*M_PIf/180.0f; }
00091                 /** Radians to degrees */
00092                 inline double RAD2DEG(const double x) { return x*180.0/M_PI; }
00093                 /** Radians to degrees */
00094                 inline float RAD2DEG(const float x) { return x*180.0f/M_PIf; }
00095 
00096 #       ifdef HAVE_LONG_DOUBLE
00097                 /** Degrees to radians */
00098                 inline long double DEG2RAD(const long double x) { return x*M_PIl/180.0; }
00099                 /** Radians to degrees */
00100                 inline long double RAD2DEG(const long double x) { return x*180.0/M_PIl; }
00101 #       endif
00102 
00103                 /** Returns the sign of X as "1" or "-1" */
00104                 template <typename T>
00105                 inline int sign(T x) { return x<0 ? -1:1; }
00106 
00107                 /** Returns the sign of X as "0", "1" or "-1" */
00108                 template <typename T>
00109                 inline int signWithZero(T x)    { return x==0?0:sign(x);}
00110 
00111                 /** Returns the closer integer (int) to x */
00112                 template <typename T>
00113                 inline int round(const T value)
00114                 {
00115                 #if MRPT_HAS_SSE2
00116                         __m128d t = _mm_set_sd( value );
00117                         return _mm_cvtsd_si32(t);
00118                 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
00119                         int t;
00120                         __asm
00121                         {
00122                                 fld value;
00123                                 fistp t;
00124                         }
00125                         return t;
00126                 #elif defined HAVE_LRINT || defined __GNUC__
00127                         return static_cast<int>(lrint(value));
00128                 #else
00129                         return static_cast<int>(value + 0.5);
00130                 #endif
00131                 }
00132 
00133                 /** Returns the closer integer (long) to x */
00134                 template <typename T>
00135                 inline long round_long(const T value)
00136                 {
00137                 #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE==64
00138                         __m128d t = _mm_set_sd( value );
00139                         return _mm_cvtsd_si64(t);
00140                 #elif (defined WIN32 || defined _WIN32) && !defined WIN64 && !defined _WIN64 && defined _MSC_VER
00141                         long t;
00142                         __asm
00143                         {
00144                                 fld value;
00145                                 fistp t;
00146                         }
00147                         return t;
00148                 #elif defined HAVE_LRINT || defined __GNUC__
00149                         return lrint(value);
00150                 #else
00151                         return static_cast<long>(value + 0.5);
00152                 #endif
00153                 }
00154 
00155                 /** Rounds toward zero  */
00156                 template <typename T>
00157                 inline int fix(T x) { return  x>0 ? static_cast<int>(floor(static_cast<double>(x))) : static_cast<int>(ceil(static_cast<double>(x))) ; }
00158 
00159                 /** Inline function for the square of a number. */
00160                 template<class T>
00161                 inline T square(const T x)    { return x*x; }
00162 
00163                 /** Utility to get a cast'ed pointer from a smart pointer */
00164                 template <class R, class P>
00165                 inline R* getAs(stlplus::smart_ptr_clone<P> &o) { return static_cast<R*>( & (*o) ); }
00166 
00167                 /** Utility to get a cast'ed pointer from a smart pointer */
00168                 template <class R, class P>
00169                 inline const R* getAs(const stlplus::smart_ptr_clone<P> &o) { return static_cast<const R*>( & (*o) ); }
00170 
00171                 /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)  */
00172                 template <class T> inline void reverseBytesInPlace(T& v_in_out)
00173                 {
00174                         uint8_t *ptr = reinterpret_cast<uint8_t*>(&v_in_out);
00175                         std::reverse(ptr,ptr+sizeof(T));
00176                 }
00177 
00178                 /** Reverse the order of the bytes of a given type (useful for transforming btw little/big endian)  */
00179                 template <class T> inline void reverseBytes(const T &v_in, T& v_out)
00180                 {
00181                         v_out = v_in;
00182                         reverseBytesInPlace(v_out);
00183                 }
00184 
00185 
00186                 /** If the second argument is below the first one, set the first argument to this lower value. */
00187                 template <typename T,typename K>
00188                 inline void keep_min(T &var,  const K test_val) {
00189                         if (test_val<var) var = test_val;
00190                 }
00191 
00192                 /** If the second argument is above the first one, set the first argument to this higher value. */
00193                 template <typename T,typename K>
00194                 inline void keep_max(T &var,  const K test_val) {
00195                         if (test_val>var) var = test_val;
00196                 }
00197 
00198                 /** Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL. */
00199                 template <class T>
00200                 void delete_safe(T *& ptr) {
00201                         if (ptr) {
00202                                 delete ptr;
00203                                 ptr = NULL;
00204                         }
00205                 }
00206 
00207                 /** Like calling a std::vector<>'s clear() method, but really forcing deallocating the memory. */
00208                 template <class T>
00209                 inline void vector_strong_clear(std::vector<T> & v) { std::vector<T> dummy; dummy.swap(v); }
00210 
00211         } // End of namespace
00212 } // end of namespace
00213 



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011