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 #ifndef _MRPT_CArray_H
00029 #define _MRPT_CArray_H
00030
00031 #include <iterator>
00032 #include <mrpt/utils/utils_defs.h>
00033 #include <mrpt/math/math_frwds.h>
00034 #include <mrpt/math/ops_containers.h>
00035 #include <mrpt/utils/CSerializable.h>
00036
00037 namespace mrpt
00038 {
00039 namespace math
00040 {
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 template <typename T, std::size_t N>
00074 class CArray {
00075 public:
00076 T elems[N];
00077
00078 public:
00079
00080 typedef T value_type;
00081 typedef T* iterator;
00082 typedef const T* const_iterator;
00083 typedef T& reference;
00084 typedef const T& const_reference;
00085 typedef std::size_t size_type;
00086 typedef std::ptrdiff_t difference_type;
00087
00088
00089 inline iterator begin() { return elems; }
00090 inline const_iterator begin() const { return elems; }
00091 inline iterator end() { return elems+N; }
00092 inline const_iterator end() const { return elems+N; }
00093
00094
00095 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
00096 typedef std::reverse_iterator<iterator> reverse_iterator;
00097 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00098 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
00099
00100 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
00101 reference, iterator, reference> > reverse_iterator;
00102 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
00103 const_reference, iterator, reference> > const_reverse_iterator;
00104 #else
00105
00106 typedef std::reverse_iterator<iterator,T> reverse_iterator;
00107 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
00108 #endif
00109
00110 reverse_iterator rbegin() { return reverse_iterator(end()); }
00111 const_reverse_iterator rbegin() const {
00112 return const_reverse_iterator(end());
00113 }
00114 reverse_iterator rend() { return reverse_iterator(begin()); }
00115 const_reverse_iterator rend() const {
00116 return const_reverse_iterator(begin());
00117 }
00118
00119
00120 inline reference operator[](size_type i) { return elems[i]; }
00121 inline const_reference operator[](size_type i) const { return elems[i]; }
00122
00123
00124 reference at(size_type i) { rangecheck(i); return elems[i]; }
00125 const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
00126
00127
00128 reference front() { return elems[0]; }
00129 const_reference front() const { return elems[0]; }
00130 reference back() { return elems[N-1]; }
00131 const_reference back() const { return elems[N-1]; }
00132
00133
00134 static inline size_type size() { return N; }
00135 static bool empty() { return false; }
00136 static size_type max_size() { return N; }
00137 enum { static_size = N };
00138
00139
00140 inline void resize(const size_t nElements) {
00141 if (nElements!=N)
00142 throw std::logic_error(format("Try to change the size of a %u-CArray to %u.",static_cast<unsigned>(N),static_cast<unsigned>(nElements)));
00143 }
00144
00145
00146 void swap (CArray<T,N>& y) {
00147 std::swap_ranges(begin(),end(),y.begin());
00148 }
00149
00150
00151 const T* data() const { return elems; }
00152
00153
00154 T* data() { return elems; }
00155
00156
00157 template <typename T2>
00158 CArray<T,N>& operator= (const CArray<T2,N>& rhs) {
00159 std::copy(rhs.begin(),rhs.end(), begin());
00160 return *this;
00161 }
00162
00163
00164 inline void assign (const T& value)
00165 {
00166 for (size_t i=0;i<N;i++) elems[i]=value;
00167 }
00168
00169 void assign (const size_t n, const T& value)
00170 {
00171 ASSERTDEB_(N==n);
00172 for (size_t i=0;i<N;i++) elems[i]=value;
00173 }
00174
00175
00176 template<typename I> void assign(I b,const I &e) {
00177 ASSERTDEB_(std::distance(b,e)==N);
00178 for (iterator i=begin();i<end();++i) *i=*(b++);
00179 }
00180
00181 private:
00182
00183 static void rangecheck (size_type i) {
00184 if (i >= size()) {
00185 throw std::out_of_range("CArray<>: index out of range");
00186 }
00187 }
00188
00189 };
00190
00191
00192 template <typename T>
00193 class CArray<T,0> {
00194 public:
00195 char c;
00196
00197 public:
00198
00199 typedef T value_type;
00200 typedef T* iterator;
00201 typedef const T* const_iterator;
00202 typedef T& reference;
00203 typedef const T& const_reference;
00204 typedef std::size_t size_type;
00205 typedef std::ptrdiff_t difference_type;
00206
00207
00208 iterator begin() { return reinterpret_cast< iterator >( &c ); }
00209 const_iterator begin() const { return reinterpret_cast< const_iterator >( &c ); }
00210 iterator end() { return reinterpret_cast< iterator >( &c ); }
00211 const_iterator end() const { return reinterpret_cast< const_iterator >( &c ); }
00212
00213
00214 #if !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
00215 typedef std::reverse_iterator<iterator> reverse_iterator;
00216 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00217 #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
00218
00219 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
00220 reference, iterator, reference> > reverse_iterator;
00221 typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
00222 const_reference, iterator, reference> > const_reverse_iterator;
00223 #else
00224
00225 typedef std::reverse_iterator<iterator,T> reverse_iterator;
00226 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
00227 #endif
00228
00229 reverse_iterator rbegin() { return reverse_iterator(end()); }
00230 const_reverse_iterator rbegin() const {
00231 return const_reverse_iterator(end());
00232 }
00233 reverse_iterator rend() { return reverse_iterator(begin()); }
00234 const_reverse_iterator rend() const {
00235 return const_reverse_iterator(begin());
00236 }
00237
00238
00239 reference at(size_type i) {
00240 throw std::out_of_range("CArray<0>: index out of range");
00241 }
00242 const_reference at(size_type i) const {
00243 throw std::out_of_range("<0>: index out of range");
00244 }
00245
00246
00247 static size_type size() { return 0; }
00248 static bool empty() { return true; }
00249 static size_type max_size() { return 0; }
00250 enum { static_size = 0 };
00251
00252
00253 void swap (CArray<T,0>& y) {
00254
00255 }
00256
00257
00258 const T* data() const { return NULL; }
00259 T* data() { return NULL; }
00260
00261
00262 template < typename T2 >
00263 CArray< T,0 >& operator= (const CArray< T2, 0>& rhs) {
00264 return *this;
00265 }
00266
00267
00268
00269
00270 inline reference operator[](size_type i) { makes_no_sense(); static T dumm=0; return dumm; }
00271 inline const_reference operator[](size_type i) const { makes_no_sense(); static T dumm=0; return dumm; }
00272
00273
00274 reference front() { makes_no_sense(); }
00275 const_reference front() const { makes_no_sense(); }
00276 reference back() { makes_no_sense(); }
00277 const_reference back() const { makes_no_sense(); }
00278
00279 private:
00280
00281
00282 static void makes_no_sense () {
00283
00284 throw std::out_of_range("CArray<0>: index out of range");
00285 }
00286 };
00287
00288
00289 template<class T, std::size_t N>
00290 bool operator== (const CArray<T,N>& x, const CArray<T,N>& y) {
00291 return std::equal(x.begin(), x.end(), y.begin());
00292 }
00293 template<class T, std::size_t N>
00294 bool operator< (const CArray<T,N>& x, const CArray<T,N>& y) {
00295 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
00296 }
00297 template<class T, std::size_t N>
00298 bool operator!= (const CArray<T,N>& x, const CArray<T,N>& y) {
00299 return !(x==y);
00300 }
00301 template<class T, std::size_t N>
00302 bool operator> (const CArray<T,N>& x, const CArray<T,N>& y) {
00303 return y<x;
00304 }
00305 template<class T, std::size_t N>
00306 bool operator<= (const CArray<T,N>& x, const CArray<T,N>& y) {
00307 return !(y<x);
00308 }
00309 template<class T, std::size_t N>
00310 bool operator>= (const CArray<T,N>& x, const CArray<T,N>& y) {
00311 return !(x<y);
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 template <typename T, std::size_t N>
00323 class CArrayNumeric : public Eigen::Matrix<T,N,1>
00324 {
00325 public:
00326 typedef Eigen::Matrix<T,N,1> Base;
00327
00328 CArrayNumeric() {}
00329
00330 CArrayNumeric(const T*ptr) : Eigen::Matrix<T,N,1>(ptr) {}
00331
00332 MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(CArrayNumeric)
00333
00334
00335 template <class ARRAYLIKE>
00336 explicit CArrayNumeric(const ARRAYLIKE &obj) : Eigen::Matrix<T,N,1>(obj) {}
00337
00338 template<typename OtherDerived>
00339 inline CArrayNumeric<T,N> & operator= (const Eigen::MatrixBase <OtherDerived>& other) {
00340 Base::operator=(other);
00341 return *this;
00342 }
00343
00344 };
00345
00346
00347
00348
00349
00350 template <std::size_t N>
00351 class CArrayFloat : public CArrayNumeric<float,N>
00352 {
00353 public:
00354 typedef CArrayNumeric<float,N> Base;
00355 typedef CArrayFloat<N> mrpt_autotype;
00356
00357 CArrayFloat() {}
00358 CArrayFloat(const float*ptr) : CArrayNumeric<float,N>(ptr) {}
00359
00360 MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(CArrayFloat)
00361
00362
00363 template <class ARRAYLIKE>
00364 explicit CArrayFloat(const ARRAYLIKE &obj) : CArrayNumeric<float,N>(obj) {}
00365 MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CArrayFloat)
00366 };
00367
00368
00369
00370 template <std::size_t N>
00371 class CArrayDouble : public CArrayNumeric<double,N>
00372 {
00373 public:
00374 typedef CArrayNumeric<double,N> Base;
00375 typedef CArrayDouble<N> mrpt_autotype;
00376
00377 CArrayDouble() {}
00378 CArrayDouble(const double*ptr) : CArrayNumeric<double,N>(ptr) {}
00379
00380 MRPT_MATRIX_CONSTRUCTORS_FROM_POSES(CArrayDouble)
00381
00382
00383 template <class ARRAYLIKE>
00384 explicit CArrayDouble(const ARRAYLIKE &obj) : CArrayNumeric<double,N>(obj) {}
00385 MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CArrayDouble)
00386 };
00387
00388
00389
00390 template <std::size_t N>
00391 class CArrayInt : public CArrayNumeric<int,N>
00392 {
00393 public:
00394 typedef CArrayNumeric<int,N> Base;
00395 typedef CArrayInt<N> mrpt_autotype;
00396
00397 CArrayInt() {}
00398 CArrayInt(const int*ptr) : CArrayNumeric<int,N>(ptr) {}
00399 MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CArrayInt)
00400 };
00401
00402
00403
00404 template <std::size_t N>
00405 class CArrayUInt : public CArrayNumeric<unsigned int,N>
00406 {
00407 public:
00408 typedef CArrayNumeric<unsigned int,N> Base;
00409 typedef CArrayUInt<N> mrpt_autotype;
00410
00411 CArrayUInt() {}
00412 CArrayUInt(const unsigned int*ptr) : CArrayNumeric<unsigned int,N>(ptr) {}
00413 MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CArrayUInt)
00414 };
00415
00416
00417 struct CMatrixTemplateSize : public Eigen::Matrix<size_t,2,1>
00418 {
00419 typedef Eigen::Matrix<size_t,2,1> Base;
00420 typedef CMatrixTemplateSize mrpt_autotype;
00421
00422 inline CMatrixTemplateSize() : Eigen::Matrix<size_t,2,1>() {}
00423 inline CMatrixTemplateSize(const size_t *d) : Eigen::Matrix<size_t,2,1>(d) {}
00424
00425 inline bool operator==(const CMatrixTemplateSize&o) const { return Eigen::Matrix<size_t,2,1>::operator()(0)==o[0] && Eigen::Matrix<size_t,2,1>::operator()(1)==o[1]; }
00426 inline bool operator!=(const CMatrixTemplateSize&o) const { return !(*this==o); }
00427
00428 inline operator size_t(void) const { return Eigen::Matrix<size_t,2,1>::operator()(0)*Eigen::Matrix<size_t,2,1>::operator()(1); }
00429 MRPT_EIGEN_DERIVED_CLASS_CTOR_OPERATOR_EQUAL(CMatrixTemplateSize)
00430 };
00431
00432 }
00433
00434 namespace utils
00435 {
00436
00437 template<typename T,size_t N> struct TTypeName <mrpt::math::CArrayNumeric<T,N> > {
00438 static std::string get() { return mrpt::format("CArrayNumeric<%s,%u>",TTypeName<T>::get().c_str(),static_cast<unsigned int>(N)); } };
00439 template<size_t N> struct TTypeName <mrpt::math::CArrayDouble<N> > {
00440 static std::string get() { return mrpt::format("CArrayNumeric<double,%u>",static_cast<unsigned int>(N)); } };
00441 template<size_t N> struct TTypeName <mrpt::math::CArrayFloat<N> > {
00442 static std::string get() { return mrpt::format("CArrayNumeric<float,%u>",static_cast<unsigned int>(N)); } };
00443 }
00444
00445
00446 }
00447
00448
00449 #endif