Main MRPT website > C++ reference
MRPT logo

types.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 mrpt_utils_types_H
00030 #define mrpt_utils_types_H
00031 
00032 #include <vector>
00033 #include <list>
00034 #include <set>
00035 #include <map>
00036 #include <string>
00037 #include <stdexcept>
00038 #include <cstdarg>
00039 #include <iostream>
00040 #include <sstream>
00041 
00042 #include <ctime>
00043 
00044 // Define macros in platform dependant stdint.h header:
00045 #ifndef __STDC_FORMAT_MACROS
00046 #       define __STDC_FORMAT_MACROS
00047 #endif
00048 #ifndef __STDC_CONSTANT_MACROS
00049 #       define __STDC_CONSTANT_MACROS
00050 #endif
00051 #ifndef __STDC_LIMIT_MACROS
00052 #       define __STDC_LIMIT_MACROS
00053 #endif
00054 
00055 // Standard elemental types:
00056 #include "pstdint.h"  // The "portable stdint header file"
00057 
00058 #if HAVE_INTTYPES_H
00059 #       include <inttypes.h>
00060 #elif defined(_MSC_VER)
00061 #       include <mrpt/utils/msvc_inttypes.h>
00062 #endif
00063 
00064 // SSE2, SSE3 types:
00065 #if MRPT_HAS_SSE2
00066         #include <emmintrin.h>
00067         #include <mmintrin.h>
00068 #endif
00069 
00070 // needed here for a few basic types used in Eigen MRPT's plugin:
00071 #include <mrpt/math/math_frwds.h>
00072 
00073 // --------------------------------------------------
00074 // Include the Eigen3 library headers, including
00075 //  MRPT's extensions:
00076 // --------------------------------------------------
00077 #include <iostream> // These headers are assumed by <mrpt/math/eigen_plugins.h>:
00078 #include <fstream>
00079 #include <sstream>
00080 #ifdef EIGEN_MAJOR_VERSION
00081 #       error **FATAL ERROR**: MRPT headers must be included before Eigen headers.
00082 #endif
00083 #define EIGEN_USE_NEW_STDVECTOR
00084 #include <Eigen/Dense>
00085 #include <Eigen/StdVector>
00086 #include <Eigen/StdDeque>
00087 
00088 #if !EIGEN_VERSION_AT_LEAST(2,90,0)
00089 #error MRPT needs version 3.0.0-beta of Eigen or newer
00090 #endif
00091 
00092 // Template implementations that need to be after all Eigen includes:
00093 #include EIGEN_MATRIXBASE_PLUGIN_POST_IMPL
00094 // --------------------------------------------------
00095 //  End of Eigen includes
00096 // --------------------------------------------------
00097 
00098 
00099 // This must be put inside any MRPT class that inherits from an Eigen class:
00100 #define MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(_CLASS_) \
00101         /*! Assignment operator from any other Eigen class */ \
00102     template<typename OtherDerived> \
00103     inline mrpt_autotype & operator= (const Eigen::MatrixBase <OtherDerived>& other) { \
00104         /*Base::operator=(other.template cast<typename Base::Scalar>());*/ \
00105         Base::operator=(other); \
00106         return *this; \
00107     } \
00108         /*! Constructor from any other Eigen class */ \
00109     template<typename OtherDerived> \
00110         inline _CLASS_(const Eigen::MatrixBase <OtherDerived>& other) : Base(other.template cast<typename Base::Scalar>()) { } \
00111 
00112 namespace mrpt
00113 {
00114         /** The base class of MRPT vectors, actually, Eigen column matrices of dynamic size with specialized constructors that resemble std::vector. */
00115         template <typename T>
00116         struct dynamicsize_vector : public Eigen::Matrix<T,Eigen::Dynamic,1>
00117         {
00118                 typedef Eigen::Matrix<T,Eigen::Dynamic,1> Base;
00119                 typedef dynamicsize_vector<T> mrpt_autotype;
00120                 MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(dynamicsize_vector)
00121 
00122                 /** Default constructor: empty vector */
00123                 inline dynamicsize_vector() : Base() {}
00124                 /** Constructor, initializes to a given initial size */
00125                 inline dynamicsize_vector(size_t N) : Base(N,1) { Base::derived().setZero(); }
00126                 /** Constructor, initializes to a given initial size, all elements to a given value */
00127                 inline dynamicsize_vector(size_t N, T init_val) : Base(N,1) { Base::derived().assign(init_val); }
00128                 /** Constructor, initializes from a std::vector<> of scalars */
00129                 template <typename R>
00130                 inline dynamicsize_vector(const std::vector<R>& v) : Base(v.size(),1) { for (size_t i=0;i<v.size();i++) (*this)[i]=v[i]; }
00131                 /** Overloaded resize method that mimics std::vector::resize(SIZE,DEFAULT_VALUE) instead of resize(nrows,ncols) \note This method exists for backward compatibility in MRPT  */
00132                 inline void resize(const size_t N, const T default_val) { Base::derived().resize(N,1); Base::derived().setConstant(default_val); }
00133                 /** Normal resize of the vector (preserving old contents). */
00134                 inline void resize(const size_t N) { Base::derived().conservativeResize(N); }
00135                 /** Reset the vector to a 0-length */
00136                 inline void clear() { *this = dynamicsize_vector<T>(); }
00137                 /** DOES NOTHING (it's here for backward compatibility) */
00138                 inline void reserve(size_t dummy_size) { }
00139         };
00140 
00141         typedef dynamicsize_vector<float>  vector_float;
00142         typedef dynamicsize_vector<double> vector_double;
00143 
00144         typedef std::vector<int8_t>      vector_signed_byte;
00145         typedef std::vector<int16_t>     vector_signed_word;
00146         typedef std::vector<int32_t>     vector_int;
00147         typedef std::vector<int64_t>     vector_long;
00148         typedef std::vector<size_t>      vector_size_t;
00149         typedef std::vector<uint8_t>     vector_byte;
00150         typedef std::vector<uint16_t>    vector_word;
00151         typedef std::vector<uint32_t>    vector_uint;
00152         typedef std::vector<bool>        vector_bool;   //!<  A type for passing a vector of bools.
00153         typedef std::vector<std::string> vector_string; //!<  A type for passing a vector of strings.
00154 
00155         /** Helper types for STL containers with Eigen memory allocators. */
00156         template <class TYPE1,class TYPE2=TYPE1>
00157         struct aligned_containers
00158         {
00159                 typedef std::pair<TYPE1,TYPE2> pair_t;
00160                 typedef std::vector<TYPE1, Eigen::aligned_allocator<TYPE1> > vector_t;
00161                 typedef std::deque<TYPE1, Eigen::aligned_allocator<TYPE1> > deque_t;
00162                 typedef std::map<TYPE1,TYPE2,std::less<TYPE1>,Eigen::aligned_allocator<std::pair<const TYPE1,TYPE2> > > map_t;
00163                 typedef std::multimap<TYPE1,TYPE2,std::less<TYPE1>,Eigen::aligned_allocator<std::pair<const TYPE1,TYPE2> > > multimap_t;
00164         };
00165 
00166         namespace utils
00167         {
00168                 /** For performing type casting from a pointer to its numeric value.
00169                 */
00170                 #if defined(_MSC_VER) && (_MSC_VER>=1300)
00171                         typedef unsigned long long POINTER_TYPE;
00172                 #else
00173                         typedef unsigned long POINTER_TYPE;
00174                 #endif
00175 
00176                 /** A RGB color - 8bit */
00177                 struct BASE_IMPEXP TColor
00178                 {
00179                         inline TColor() : R(0),G(0),B(0),A(255) { }
00180                         inline TColor(uint8_t r,uint8_t g,uint8_t b, uint8_t alpha=255) : R(r),G(g),B(b),A(alpha) { }
00181                         inline explicit TColor(const unsigned int color_RGB_24bit) : R(uint8_t(color_RGB_24bit>>16)),G(uint8_t(color_RGB_24bit>>8)),B(uint8_t(color_RGB_24bit)),A(255) { }
00182                         uint8_t R,G,B,A;
00183                         /** Operator for implicit conversion into an int binary representation 0xRRGGBB */
00184                         inline operator unsigned int(void) const { return (((unsigned int)R)<<16) | (((unsigned int)G)<<8) | B; }
00185 
00186                         static TColor red; //!< Predefined colors
00187                         static TColor green;//!< Predefined colors
00188                         static TColor blue;//!< Predefined colors
00189                         static TColor white;//!< Predefined colors
00190                         static TColor black;//!< Predefined colors
00191                         static TColor gray;     //!< Predefined colors
00192                 };
00193 
00194                 /** A RGB color - floats in the range [0,1] */
00195                 struct BASE_IMPEXP TColorf
00196                 {
00197                         TColorf(float r=0,float g=0,float b=0, float alpha=1.0f) : R(r),G(g),B(b),A(alpha) { }
00198                         explicit TColorf(const TColor &col) : R(col.R*(1.f/255)),G(col.G*(1.f/255)),B(col.B*(1.f/255)),A(col.A*(1.f/255)) { }
00199                         float R,G,B,A;
00200                 };
00201 
00202                 /** A pair (x,y) of pixel coordinates (subpixel resolution). */
00203                 struct BASE_IMPEXP TPixelCoordf
00204                 {
00205                         float x,y;
00206 
00207                         /** Default constructor: undefined values of x,y */
00208                         TPixelCoordf() : x(),y() {}
00209 
00210                         /** Constructor from x,y values */
00211                         TPixelCoordf(const float _x,const float _y) : x(_x), y(_y) { }
00212                 };
00213 
00214                 std::ostream BASE_IMPEXP & operator <<(std::ostream& o, const TPixelCoordf& p); //!< Prints TPixelCoordf as "(x,y)"
00215 
00216                 /** A pair (x,y) of pixel coordinates (integer resolution). */
00217                 struct BASE_IMPEXP TPixelCoord
00218                 {
00219                         TPixelCoord() : x(0),y(0) { }
00220                         TPixelCoord(const int _x,const int _y) : x(_x), y(_y) { }
00221 
00222                         int x,y;
00223                 };
00224 
00225                 std::ostream BASE_IMPEXP & operator <<(std::ostream& o, const TPixelCoord& p); //!< Prints TPixelCoord as "(x,y)"
00226 
00227                 typedef TPixelCoord TImageSize; //!< A type for image sizes.
00228 
00229                 /** For usage when passing a dynamic number of (numeric) arguments to a function, by name.
00230                   *  \code
00231                   *    TParameters<double> p;  // or TParametersDouble
00232                   *    p["v_max"] = 1.0;  // Write
00233                   *    ...
00234                   *    cout << p["w_max"]; // Read, even if "p" was const.
00235                   *  \endcode
00236                   *
00237                   *  A default list of parameters can be passed to the constructor as a sequence
00238                   *   of pairs "name, value", which MUST end in a NULL name string. Names MUST BE "const char*"
00239                   *   (that is, "old plain strings" are OK), not std::string objects!.
00240                   *  See this example:
00241                   *
00242                   *  \code
00243                   *    TParameters<double> p("par1",2.0, "par2",-4.5, "par3",9.0, NULL); // MUST end with a NULL
00244                   *  \endcode
00245                   *
00246                   *  <b>VERY IMPORTANT:</b> If you use the NULL-ended constructor above, make sure all the values are of the proper
00247                   *    type or it will crash in runtime. For example, in a TParametersDouble all values must be double's, so
00248                   *    if you type "10" the compiler will make it an "int". Instead, write "10.0".
00249                   *
00250                   * \sa the example in MRPT/samples/params-by-name
00251                   */
00252                 template <typename T>
00253                 struct TParameters : public std::map<std::string,T>
00254                 {
00255                         typedef std::map<std::string,T> BASE;
00256                         /** Default constructor (initializes empty) */
00257                         TParameters() : BASE() { }
00258                         /** Constructor with a list of initial values (see the description and use example in mrpt::utils::TParameters) */
00259                         TParameters(const char *nam1,...) : BASE() {
00260                                 if (!nam1) return; // No parameters
00261                                 T val;
00262                                 va_list args;
00263                                 va_start(args,nam1);
00264                                 // 1st one out of the loop:
00265                                 val = va_arg(args,T);
00266                                 BASE::operator[](std::string(nam1)) = val;
00267                                 // Loop until NULL:
00268                                 const char *nam;
00269                                 do{
00270                                         nam = va_arg(args,const char*);
00271                                         if (nam) {
00272                                                 val = va_arg(args,T);
00273                                                 BASE::operator[](std::string(nam)) = val;
00274                                         }
00275                                 } while (nam);
00276                                 va_end(args);
00277                         }
00278                         inline bool has(const std::string &s) const { return std::map<std::string,T>::end()!=BASE::find(s); }
00279                         /** A const version of the [] operator, for usage as read-only.
00280                           * \exception std::logic_error On parameter not present. Please, check existence with "has" before reading.
00281                           */
00282                         inline T operator[](const std::string &s) const {
00283                                 typename BASE::const_iterator it =BASE::find(s);
00284                                 if (BASE::end()==it)
00285                                         throw std::logic_error(std::string("Parameter '")+s+std::string("' is not present.").c_str());
00286                                 return it->second;
00287                         }
00288                         /** A const version of the [] operator and with a default value in case the parameter is not set (for usage as read-only).
00289                           */
00290                         inline T getWithDefaultVal(const std::string &s, const T& defaultVal) const {
00291                                 typename BASE::const_iterator it =BASE::find(s);
00292                                 if (BASE::end()==it)
00293                                                 return defaultVal;
00294                                 else    return it->second;
00295                         }
00296                         /** The write (non-const) version of the [] operator. */
00297                         inline T & operator[](const std::string &s) { return BASE::operator[](s); }
00298 
00299                         /** Dumps to console the output from getAsString() */
00300                         inline void dumpToConsole() const { std::cout << getAsString(); }
00301 
00302                         /** Returns a multi-like string representation of the parameters like : 'nam   = val\nnam2   = val2...' */
00303                         inline std::string getAsString() const { std::string s; getAsString(s); return s; }
00304 
00305                         /** Returns a multi-like string representation of the parameters like : 'nam   = val\nnam2   = val2...' */
00306                         void getAsString(std::string &s) const {
00307                                 size_t maxStrLen = 10;
00308                                 for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it) maxStrLen = std::max(maxStrLen, it->first.size() );
00309                                 maxStrLen++;
00310                                 std::stringstream str;
00311                                 for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it)
00312                                         str << it->first << std::string(maxStrLen-it->first.size(),' ') << " = " << it->second << std::endl;
00313                                 s = str.str();
00314                         }
00315                 };
00316 
00317                 typedef TParameters<double>       TParametersDouble; //!< See the generic template mrpt::utils::TParameters
00318                 typedef TParameters<std::string>  TParametersString; //!< See the generic template mrpt::utils::TParameters
00319 
00320                 typedef uint64_t TNodeID;  //!< The type for node IDs in graphs of different types.
00321                 typedef std::pair<TNodeID,TNodeID> TPairNodeIDs; //!< A pair of node IDs.
00322                 #define INVALID_NODEID  static_cast<TNodeID>(-1)
00323 
00324         } // end namespace
00325 }
00326 
00327 #endif
00328 



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