00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00043 #include "pstdint.h"
00044
00045 #define __STDC_FORMAT_MACROS
00046 #if HAVE_INTTYPES_H
00047 # include <inttypes.h>
00048 #elif defined(_MSC_VER)
00049 # include <mrpt/utils/msvc_inttypes.h>
00050 #endif
00051
00052
00053 #if MRPT_HAS_SSE2
00054 #include <emmintrin.h>
00055 #include <mmintrin.h>
00056 #endif
00057
00058
00059 #define DECLARE_MRPT_CONTAINER_TYPES \
00060 template <class ANOTHERCONT> struct mrpt_container { \
00061 typedef void void_type; \
00062 typedef ANOTHERCONT dum_type; \
00063 typedef mrpt_autotype cont_type; \
00064 typedef typename mrpt_autotype::value_type element_type; \
00065 };
00066
00067 #define COMMON_DECLARE_MRPT_CONTAINER_IS_MATRIX \
00068 typedef void mrpt_matrix_tag; \
00069 template <class ANOTHERCONT> struct mrpt_matrix_tag_templ { typedef void void_type; typedef ANOTHERCONT dum_type; typedef mrpt_autotype cont_type; \
00070 };
00071
00072 #define DECLARE_MRPT_CONTAINER_IS_MATRIX \
00073 COMMON_DECLARE_MRPT_CONTAINER_IS_MATRIX \
00074 enum dummy_enum { mrpt_matrix_type_ncols = -1, \
00075 mrpt_matrix_type_nrows = -1 }; \
00076
00077 #define DECLARE_MRPT_CONTAINER_IS_MATRIX_FIXED(NROWS,NCOLS) \
00078 COMMON_DECLARE_MRPT_CONTAINER_IS_MATRIX \
00079 enum dummy_enum { mrpt_matrix_type_ncols = NCOLS, \
00080 mrpt_matrix_type_nrows = NROWS };
00081
00082
00083 #define DECLARE_MRPT_CONTAINER_IS_VECTOR \
00084 typedef void mrpt_vector_tag; \
00085 template <class ANOTHERCONT> struct mrpt_vector_tag_templ { typedef void void_type; typedef ANOTHERCONT dum_type; typedef mrpt_autotype cont_type; }; \
00086 enum dummy_enum { mrpt_vector_type_len = -1 }; \
00087
00088 #define DECLARE_MRPT_CONTAINER_IS_VECTOR_FIXED(LEN) \
00089 typedef void mrpt_vector_tag; \
00090 template <class ANOTHERCONT> struct mrpt_vector_tag_templ { typedef void void_type; typedef ANOTHERCONT dum_type; typedef mrpt_autotype cont_type; }; \
00091 enum dummy_enum { mrpt_vector_type_len = LEN }; \
00092
00093
00094
00095 #define RET_VOID_ASSERT_MRPTCONTAINER(_CONTAINER) \
00096 typename _CONTAINER::template mrpt_container<void>::void_type
00097
00098
00099
00100 #define RET_VOID_ASSERT_MRPTCONTAINERS(_CONTAINER1,_CONTAINER2) \
00101 typename _CONTAINER1::template mrpt_container<typename _CONTAINER2::mrpt_autotype>::void_type
00102
00103
00104
00105 #define RET_CONT1_ASSERT_MRPTCONTAINERS(_CONTAINER1,_CONTAINER2) \
00106 typename _CONTAINER1::template mrpt_container<typename _CONTAINER2::mrpt_autotype>::cont_type
00107
00108
00109
00110 #define RET_ELEMENT_ASSERT_MRPTCONTAINERS(_CONTAINER1,_CONTAINER2) \
00111 typename _CONTAINER1::template mrpt_container<typename _CONTAINER2::mrpt_autotype>::element_type
00112
00113
00114
00115 #define RET_CONT_ASSERT_MRPTCONTAINER(_CONTAINER) \
00116 typename _CONTAINER::mrpt_autotype
00117
00118
00119
00120 #define RET_ELEMENT_ASSERT_MRPTCONTAINER(_CONTAINER) \
00121 typename _CONTAINER::mrpt_autotype::value_type
00122
00123
00124
00125 #define RET_TYPE_ASSERT_MRPTCONTAINER(_CONTAINER,_RETTYPE) \
00126 typename _CONTAINER::template mrpt_container<_RETTYPE>::dum_type
00127
00128
00129
00130 #define RET_VOID_ASSERT_MRPTMATRIX(_MATRIX) \
00131 typename _MATRIX::template mrpt_matrix_tag_templ<void>::void_type
00132
00133
00134
00135 #define RET_TYPE_ASSERT_MRPTMATRIX(_MATRIX,_RETTYPE) \
00136 typename _MATRIX::template mrpt_matrix_tag_templ<_RETTYPE>::dum_type
00137
00138
00139
00140 #define RET_MAT_ASSERT_MRPTMATRIX(_MATRIX) \
00141 typename _MATRIX::template mrpt_matrix_tag_templ<void>::cont_type
00142
00143
00144
00145 #define RET_VOID_ASSERT_MRPTMATRICES(_MATRIX1,_MATRIX2) \
00146 typename _MATRIX1::template mrpt_matrix_tag_templ<typename _MATRIX2::mrpt_matrix_tag>::void_type
00147
00148
00149
00150 #define RET_MAT1_ASSERT_MRPTMATRICES(_MATRIX1,_MATRIX2) \
00151 typename _MATRIX1::template mrpt_matrix_tag_templ<typename _MATRIX2::mrpt_matrix_tag>::cont_type
00152
00153
00154
00155
00156
00157 #define RET_CONT1_ASSERT_MRPTVECTORS(_VECTOR1,_VECTOR2) \
00158 typename _VECTOR1::template mrpt_vector_tag_templ<typename _VECTOR2::mrpt_vector_tag>::cont_type
00159
00160
00161
00162 #include <mrpt/math/math_frwds.h>
00163
00164
00165 namespace mrpt
00166 {
00167
00168 template <typename _TYPE>
00169 struct mrpt_base_vector : public std::vector<_TYPE>
00170 {
00171 inline mrpt_base_vector() : std::vector<_TYPE>() {}
00172 inline mrpt_base_vector(const std::vector<_TYPE> &o) : std::vector<_TYPE>(o) {}
00173 inline mrpt_base_vector<_TYPE> & operator =(const std::vector<_TYPE> &o) { std::vector<_TYPE>::operator =(o); return *this; }
00174 inline mrpt_base_vector(size_t N) : std::vector<_TYPE>(N) { }
00175 inline mrpt_base_vector(size_t N,_TYPE val) : std::vector<_TYPE>(N,val) { }
00176 template<typename ITERATOR> inline mrpt_base_vector(const ITERATOR &b,const ITERATOR &e):std::vector<_TYPE>(b,e) {}
00177 typedef mrpt_base_vector<_TYPE> mrpt_autotype;
00178 DECLARE_MRPT_CONTAINER_TYPES
00179 DECLARE_MRPT_CONTAINER_IS_VECTOR
00180 DECLARE_COMMON_CONTAINERS_MEMBERS(_TYPE)
00181 };
00182
00183 typedef mrpt_base_vector<float> vector_float;
00184 typedef mrpt_base_vector<double> vector_double;
00185
00186 typedef mrpt_base_vector<int8_t> vector_signed_byte;
00187 typedef mrpt_base_vector<int16_t> vector_signed_word;
00188 typedef mrpt_base_vector<int32_t> vector_int;
00189 typedef mrpt_base_vector<int64_t> vector_long;
00190 typedef mrpt_base_vector<size_t> vector_size_t;
00191 typedef mrpt_base_vector<uint8_t> vector_byte;
00192 typedef mrpt_base_vector<uint16_t> vector_word;
00193 typedef mrpt_base_vector<uint32_t> vector_uint;
00194
00195 typedef std::vector<bool> vector_bool;
00196 typedef std::vector<std::string> vector_string;
00197
00198 namespace utils
00199 {
00200
00201 typedef void (*TFunctor_noRet_1inputs)(const void *);
00202 typedef void (*TFunctor_noRet_2inputs)(const void *,const void *);
00203 typedef void (*TFunctor_noRet_3inputs)(const void *,const void *,const void *);
00204
00205 typedef double (*TFunctor_retDouble_1inputs)(const void *);
00206 typedef double (*TFunctor_retDouble_2inputs)(const void *,const void *);
00207 typedef double (*TFunctor_retDouble_3inputs)(const void *,const void *,const void *);
00208
00209 typedef void (*TFunctor_retVecDbl_inpVecDbl)(const vector_double &in, vector_double &out);
00210 typedef void (*TFunctor_retVecFlt_inpVecFlt)(const vector_float &in, vector_float &out);
00211 typedef void (*TFunctor_retVecInt_inpVecInt)(const vector_int &in, vector_int &out);
00212
00213 typedef void (*TFunctor_retVecDbl_inp2VecDbl)(const vector_double &x,const vector_double &y, vector_double &out);
00214 typedef void (*TFunctor_retVecFlt_inp2VecFlt)(const vector_float &x,const vector_float &y, vector_float &out);
00215 typedef void (*TFunctor_retVecInt_inp2VecInt)(const vector_int &x,const vector_int &y, vector_int &out);
00216
00217 typedef double (*TFunctor_retDbl_inp1VecDbl)(const vector_double &in1);
00218 typedef double (*TFunctor_retDbl_inp2VecDbl)(const vector_double &in1,const vector_double &in2);
00219 typedef double (*TFunctor_retDbl_inp3VecDbl)(const vector_double &in1,const vector_double &in2,const vector_double &in3);
00220
00221
00222
00223 #if defined(_MSC_VER) && (_MSC_VER>=1300)
00224 typedef unsigned long long POINTER_TYPE;
00225 #else
00226 typedef unsigned long POINTER_TYPE;
00227 #endif
00228
00229
00230 struct BASE_IMPEXP TColor
00231 {
00232 inline TColor() : R(0),G(0),B(0),A(255) { }
00233 inline TColor(uint8_t r,uint8_t g,uint8_t b, uint8_t alpha=255) : R(r),G(g),B(b),A(alpha) { }
00234 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) { }
00235 uint8_t R,G,B,A;
00236
00237 inline operator unsigned int(void) const { return (((unsigned int)R)<<16) | (((unsigned int)G)<<8) | B; }
00238
00239 static TColor red;
00240 static TColor green;
00241 static TColor blue;
00242 static TColor white;
00243 static TColor black;
00244 static TColor gray;
00245 };
00246
00247
00248 struct BASE_IMPEXP TColorf
00249 {
00250 TColorf(float r=0,float g=0,float b=0, float alpha=1.0f) : R(r),G(g),B(b),A(alpha) { }
00251 float R,G,B,A;
00252 };
00253
00254
00255 struct BASE_IMPEXP TPixelCoordf
00256 {
00257 float x,y;
00258
00259
00260 TPixelCoordf() : x(),y() {}
00261
00262
00263 TPixelCoordf(const float _x,const float _y) : x(_x), y(_y) { }
00264 };
00265
00266
00267 struct BASE_IMPEXP TPixelCoord
00268 {
00269 TPixelCoord() : x(0),y(0) { }
00270 TPixelCoord(const int _x,const int _y) : x(_x), y(_y) { }
00271
00272 int x,y;
00273 };
00274
00275 typedef TPixelCoord TImageSize;
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 template <typename T>
00301 struct TParameters : public std::map<std::string,T>
00302 {
00303 typedef std::map<std::string,T> BASE;
00304
00305 TParameters() : BASE() { }
00306
00307 TParameters(const char *nam1,...) : BASE() {
00308 if (!nam1) return;
00309 T val;
00310 va_list args;
00311 va_start(args,nam1);
00312
00313 val = va_arg(args,T);
00314 BASE::operator[](std::string(nam1)) = val;
00315
00316 const char *nam;
00317 do{
00318 nam = va_arg(args,const char*);
00319 if (nam) {
00320 val = va_arg(args,T);
00321 BASE::operator[](std::string(nam)) = val;
00322 }
00323 } while (nam);
00324 va_end(args);
00325 }
00326 inline bool has(const std::string &s) const { return std::map<std::string,T>::end()!=BASE::find(s); }
00327
00328
00329
00330 inline T operator[](const std::string &s) const {
00331 typename BASE::const_iterator it =BASE::find(s);
00332 if (BASE::end()==it)
00333 throw std::logic_error(std::string("Parameter '")+s+std::string("' is not present.").c_str());
00334 return it->second;
00335 }
00336
00337
00338 inline T getWithDefaultVal(const std::string &s, const T& defaultVal) const {
00339 typename BASE::const_iterator it =BASE::find(s);
00340 if (BASE::end()==it)
00341 return defaultVal;
00342 else return it->second;
00343 }
00344
00345 inline T & operator[](const std::string &s) { return BASE::operator[](s); }
00346
00347
00348 inline void dumpToConsole() const { std::cout << getAsString(); }
00349
00350
00351 inline std::string getAsString() const { std::string s; getAsString(s); return s; }
00352
00353
00354 void getAsString(std::string &s) const {
00355 size_t maxStrLen = 10;
00356 for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it) maxStrLen = std::max(maxStrLen, it->first.size() );
00357 maxStrLen++;
00358 std::stringstream str;
00359 for (typename BASE::const_iterator it=BASE::begin();it!=BASE::end();++it)
00360 str << it->first << std::string(maxStrLen-it->first.size(),' ') << " = " << it->second << std::endl;
00361 s = str.str();
00362 }
00363 };
00364
00365 typedef TParameters<double> TParametersDouble;
00366 typedef TParameters<std::string> TParametersString;
00367
00368 }
00369 }
00370
00371 #endif
00372