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 CMatrixView_H
00029 #define CMatrixView_H
00030
00031 #include <mrpt/math/math_frwds.h>
00032 #include <mrpt/math/matrix_iterators.h>
00033
00034
00035
00036
00037
00038 namespace mrpt
00039 {
00040 namespace math
00041 {
00042
00043
00044
00045
00046
00047 class CMatrixView
00048 {
00049
00050 };
00051
00052 #define DECLARE_MATRIX_VIEW_TYPES(_M) \
00053 typedef typename _M::value_type value_type; \
00054 typedef typename _M::reference reference; \
00055 typedef typename _M::const_reference const_reference; \
00056 typedef typename _M::size_type size_type; \
00057 typedef typename _M::difference_type difference_type; \
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 template <class MATRIXTYPE>
00068 class CMatrixViewTranspose : public CMatrixView
00069 {
00070 protected:
00071 MATRIXTYPE &base;
00072
00073 public:
00074 typedef CMatrixViewTranspose<MATRIXTYPE> mrpt_autotype;
00075 DECLARE_MATRIX_VIEW_TYPES(MATRIXTYPE)
00076 DECLARE_MRPT_CONTAINER_TYPES
00077 DECLARE_MRPT_CONTAINER_IS_MATRIX
00078 DECLARE_MRPT_MATRIX_ITERATORS
00079 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
00080
00081 inline CMatrixViewTranspose(MATRIXTYPE &m):base(m) {}
00082 inline size_t getRowCount() const { return base.getColCount(); }
00083 inline size_t getColCount() const { return base.getRowCount(); }
00084 inline value_type &get_unsafe(size_t r,size_t c) { return base.get_unsafe(c,r); }
00085 inline value_type get_unsafe(size_t r,size_t c) const { return base.get_unsafe(c,r); }
00086 inline void set_unsafe(size_t r,size_t c,value_type v) { base.set_unsafe(c,r,v); }
00087 inline value_type &operator()(const size_t r,const size_t c) { return base(c,r); }
00088 inline value_type operator()(const size_t r,const size_t c) const { return base(c,r); }
00089 template<typename OTHERMATRIX> inline mrpt_autotype &operator=(const OTHERMATRIX &m) {
00090 const size_t NR = this->getRowCount();
00091 const size_t NC = this->getColCount();
00092 ASSERT_(NR==m.getRowCount());
00093 ASSERT_(NC==m.getColCount());
00094 for (size_t i=0;i<NR;++i) for (size_t j=0;j<NC;++j) this->get_unsafe(i,j)=m.get_unsafe(i,j);
00095 return *this;
00096 }
00097 inline CMatrixTemplateSize size() {
00098 size_t dimsA[]={this->getRowCount(),this->getColCount()};
00099 CMatrixTemplateSize dims(dimsA);
00100 return dims;
00101 }
00102 inline void setSize(size_t r,size_t c) {
00103 if ((this->getRowCount()!=r)||(this->getColCount()!=c)) throw std::logic_error("Tried to change size of a matrix view.");
00104 }
00105 inline void resize(size_t rtc) {
00106 if (this->getRowCount()*this->getColCount()!=rtc) throw std::logic_error("Tried to change size of a matrix view.");
00107 }
00108 };
00109 template<typename MAT> CMatrixViewTranspose<MAT> getTransposed(MAT &m) {
00110 return CMatrixViewTranspose<MAT>(m);
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 template <class MAT> class CConstMatrixViewTranspose:public CMatrixView {
00121 protected:
00122 const MAT &base;
00123 public:
00124 typedef CConstMatrixViewTranspose<MAT> mrpt_autotype;
00125 DECLARE_MATRIX_VIEW_TYPES(MAT)
00126 DECLARE_MRPT_CONTAINER_TYPES
00127 DECLARE_MRPT_CONTAINER_IS_MATRIX
00128 DECLARE_MRPT_MATRIX_ITERATORS
00129 DECLARE_COMMON_MATRIX_MEMBERS(value_type);
00130 inline CConstMatrixViewTranspose(const MAT &m):base(m) {}
00131 inline size_t getRowCount() const {
00132 return base.getColCount();
00133 }
00134 inline size_t getColCount() const {
00135 return base.getRowCount();
00136 }
00137 inline value_type get_unsafe(size_t r,size_t c) const {
00138 return base.get_unsafe(c,r);
00139 }
00140 inline value_type operator()(size_t r,size_t c) const {
00141 return base(c,r);
00142 }
00143 inline CMatrixTemplateSize size() const {
00144 CMatrixTemplateSize dims();
00145 dims[0]=getRowCount();
00146 dims[1]=getColCount();
00147 return dims;
00148 }
00149 inline void setSize(size_t r,size_t c) {
00150 if ((getRowCount()!=r)||(getColCount()!=c)) throw std::logic_error("Tried to change the size of a matrix view.");
00151 }
00152 inline void resize(size_t rc) {
00153 if (getRowCount()*getColCount()!=rc) throw std::logic_error("Tried to chgange the size of a matrix view.");
00154 }
00155 };
00156 template<typename MAT> CConstMatrixViewTranspose<MAT> getTransposed(const MAT &m) {
00157 return CConstMatrixViewTranspose<MAT>(m);
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 template<typename MATRIXTYPE,size_t NR,size_t NC> class CSubmatrixView:public CMatrixView {
00169 protected:
00170 MATRIXTYPE &base;
00171 size_t firstRow;
00172 size_t firstCol;
00173 public:
00174 typedef CSubmatrixView<MATRIXTYPE,NR,NC> mrpt_autotype;
00175 DECLARE_MATRIX_VIEW_TYPES(MATRIXTYPE)
00176 DECLARE_MRPT_CONTAINER_TYPES
00177 DECLARE_MRPT_CONTAINER_IS_MATRIX
00178 DECLARE_MRPT_MATRIX_ITERATORS
00179 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
00180 inline CSubmatrixView(MATRIXTYPE &m,size_t r1,size_t c1):base(m),firstRow(r1),firstCol(c1) {}
00181 inline static size_t getRowCount() {
00182 return NR;
00183 }
00184 inline static size_t getColCount() {
00185 return NC;
00186 }
00187 inline value_type &get_unsafe(size_t r,size_t c) {
00188 return base.get_unsafe(firstRow+r,firstCol+c);
00189 }
00190 inline value_type get_unsafe(size_t r,size_t c) const {
00191 return base.get_unsafe(firstRow+r,firstCol+c);
00192 }
00193 inline void set_unsafe(size_t r,size_t c,value_type v) {
00194 base.set_unsafe(firstRow+r,firstCol+c,v);
00195 }
00196 inline value_type &operator()(const size_t r,const size_t c) {
00197 return base(firstRow+r,firstCol+c);
00198 }
00199 inline value_type operator()(const size_t r,const size_t c) const {
00200 return base(firstRow+r,firstCol+c);
00201 }
00202 template<typename OTHERMATRIX> inline mrpt_autotype &operator=(const OTHERMATRIX &m) {
00203 ASSERT_(NR==m.getRowCount());
00204 ASSERT_(NC==m.getColCount());
00205 for (size_t i=0;i<NR;++i) for (size_t j=0;j<NC;++j) base.get_unsafe(firstRow+i,firstCol+j)=m.get_unsafe(i,j);
00206 return *this;
00207 }
00208 inline static CMatrixTemplateSize size() {
00209 static size_t dimsA[]={NR,NC};
00210 static CMatrixTemplateSize dims(dimsA);
00211 return dims;
00212 }
00213 static inline void setSize(size_t r,size_t c) {
00214 if ((NR!=r)||(NC!=c)) throw std::logic_error("Tried to change size on a fixed-size matrix.");
00215 }
00216 static inline void resize(size_t rtc) {
00217 if (NR*NC!=rtc) throw std::logic_error("Tried to change size on a fixed-size matrix.");
00218 }
00219 };
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 template<typename MATRIXTYPE> class CArbitrarySubmatrixView:public CMatrixView {
00231 protected:
00232 MATRIXTYPE &base;
00233 std::vector<size_t> rows;
00234 std::vector<size_t> cols;
00235 public:
00236 typedef CArbitrarySubmatrixView<MATRIXTYPE> mrpt_autotype;
00237 DECLARE_MATRIX_VIEW_TYPES(MATRIXTYPE)
00238 DECLARE_MRPT_CONTAINER_TYPES
00239 DECLARE_MRPT_CONTAINER_IS_MATRIX
00240 DECLARE_MRPT_MATRIX_ITERATORS
00241 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
00242 inline CArbitrarySubmatrixView(MATRIXTYPE &m,const std::vector<size_t> &rs,const std::vector<size_t> &cs):base(m),rows(rs),cols(cs) {}
00243 inline CArbitrarySubmatrixView(MATRIXTYPE &m,const std::vector<size_t> &rows_and_cols):base(m),rows(rows_and_cols),cols(rows_and_cols) {}
00244 inline CArbitrarySubmatrixView(MATRIXTYPE &m,size_t firstRow,size_t numRows,size_t firstCol,size_t numCols):base(m),rows(numRows),cols(numCols) {
00245 for (size_t i=0;i<numRows;++i) rows[i]=firstRow+i;
00246 for (size_t i=0;i<numCols;++i) cols[i]=firstCol+i;
00247 }
00248 inline size_t getRowCount() const {
00249 return rows.size();
00250 }
00251 inline size_t getColCount() const {
00252 return cols.size();
00253 }
00254 inline value_type &get_unsafe(size_t r,size_t c) {
00255 return base.get_unsafe(rows[r],cols[r]);
00256 }
00257 inline value_type get_unsafe(size_t r,size_t c) const {
00258 return base.get_unsafe(rows[r],cols[r]);
00259 }
00260 inline void set_unsafe(size_t r,size_t c,value_type v) {
00261 base.set_unsafe(rows[r],cols[c],v);
00262 }
00263 inline value_type &operator()(size_t r,size_t c) {
00264 return base(rows.at(r),cols.at(c));
00265 }
00266 inline value_type operator()(size_t r,size_t c) const {
00267 return base(rows.at(r),cols.at(c));
00268 }
00269 template<typename OTHERMATRIX> inline mrpt_autotype &operator=(const OTHERMATRIX &m) {
00270 ASSERT_(rows.size()==m.getRowCount());
00271 ASSERT_(cols.size()==m.getColCount());
00272 for (size_t i=0;i<rows.size();++i) for (size_t j=0;j<cols.size();++j) base.get_unsafe(rows[i],cols[j])=m.get_unsafe(i,j);
00273 return *this;
00274 }
00275 inline CMatrixTemplateSize size() const {
00276 size_t dimsA[2];
00277 dimsA[0]=rows.size();
00278 dimsA[1]=cols.size();
00279 return CMatrixTemplateSize(dimsA);
00280 }
00281 inline void setSize(size_t r,size_t c) {
00282 if ((r!=rows.size())||(c!=cols.size())) throw std::logic_error("Tried to change size on a fixed-size matrix.");
00283 }
00284 inline void resize(size_t rtc) {
00285 if (rows.size()*cols.size()!=rtc) throw std::logic_error("Tried to change size on a fixed-size matrix.");
00286 }
00287 inline void getRealRowIndices(std::vector<size_t> &vec) const {
00288 vec=rows;
00289 }
00290 inline void getRealColumnIndices(std::vector<size_t> &vec) const {
00291 vec=cols;
00292 }
00293 inline void deleteRow(size_t r) {
00294 ASSERT_(r<rows.size());
00295 rows.erase(rows.begin()+r);
00296 }
00297 inline void deleteColumn(size_t c) {
00298 ASSERT_(c<cols.size());
00299 cols.erase(cols.begin()+c);
00300 }
00301 inline size_t getProxyRow(size_t r) const {
00302 ASSERT_(r<rows.size());
00303 return rows[r];
00304 }
00305 inline size_t getProxyCol(size_t c) const {
00306 ASSERT_(c<cols.size());
00307 return cols[c];
00308 }
00309 inline value_type &getWithRowProxied(size_t proxyRow,size_t c) {
00310 return base.get_unsafe(proxyRow,cols[c]);
00311 }
00312 inline value_type getWithRowProxied(size_t proxyRow,size_t c) const {
00313 return base.get_unsafe(proxyRow,cols[c]);
00314 }
00315 inline value_type &getWithColProxied(size_t r,size_t proxyCol) {
00316 return base.get_unsafe(rows[r],proxyCol);
00317 }
00318 inline value_type getWithColProxied(size_t r,size_t proxyCol) const {
00319 return base.get_unsafe(rows[r],proxyCol);
00320 }
00321 };
00322
00323 template<typename MATRIXTYPE> class CConstArbitrarySubmatrixView:public CMatrixView {
00324 protected:
00325 const MATRIXTYPE &base;
00326 std::vector<size_t> rows;
00327 std::vector<size_t> cols;
00328 public:
00329 typedef CConstArbitrarySubmatrixView<MATRIXTYPE> mrpt_autotype;
00330 DECLARE_MATRIX_VIEW_TYPES(MATRIXTYPE)
00331 DECLARE_MRPT_CONTAINER_TYPES
00332 DECLARE_MRPT_CONTAINER_IS_MATRIX
00333 DECLARE_MRPT_MATRIX_ITERATORS
00334 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
00335 inline CConstArbitrarySubmatrixView(const MATRIXTYPE &m,const std::vector<size_t> &rs,const std::vector<size_t> &cs):base(m),rows(rs),cols(cs) {}
00336 inline CConstArbitrarySubmatrixView(const MATRIXTYPE &m,const std::vector<size_t> &rows_and_cols):base(m),rows(rows_and_cols),cols(rows_and_cols) {}
00337 inline CConstArbitrarySubmatrixView(const MATRIXTYPE &m,size_t firstRow,size_t numRows,size_t firstCol,size_t numCols):base(m),rows(numRows),cols(numCols) {
00338 for (size_t i=0;i<numRows;++i) rows[i]=firstRow+i;
00339 for (size_t i=0;i<numCols;++i) cols[i]=firstCol+i;
00340 }
00341 inline size_t getRowCount() const {
00342 return rows.size();
00343 }
00344 inline size_t getColCount() const {
00345 return cols.size();
00346 }
00347 inline value_type get_unsafe(size_t r,size_t c) const {
00348 return base.get_unsafe(rows[r],cols[r]);
00349 }
00350 inline value_type operator()(size_t r,size_t c) const {
00351 return base(rows.at(r),cols.at(c));
00352 }
00353 inline CMatrixTemplateSize size() const {
00354 size_t dimsA[2];
00355 dimsA[0]=rows.size();
00356 dimsA[1]=cols.size();
00357 return CMatrixTemplateSize(dimsA);
00358 }
00359 inline void setSize(size_t r,size_t c) {
00360 if ((r!=rows.size())||(c!=cols.size())) throw std::logic_error("Tried to change size on a fixed-size matrix.");
00361 }
00362 inline void resize(size_t rtc) {
00363 if (rows.size()*cols.size()!=rtc) throw std::logic_error("Tried to change size on a fixed-size matrix.");
00364 }
00365 inline void getRealRowIndices(std::vector<size_t> &vec) const {
00366 vec=rows;
00367 }
00368 inline void getRealColumnIndices(std::vector<size_t> &vec) const {
00369 vec=cols;
00370 }
00371 inline void deleteRow(size_t r) {
00372 ASSERT_(r<rows.size());
00373 rows.erase(rows.begin()+r);
00374 }
00375 inline void deleteColumn(size_t c) {
00376 ASSERT_(c<cols.size());
00377 cols.erase(cols.begin()+c);
00378 }
00379 inline size_t getProxyRow(size_t r) const {
00380 ASSERT_(r<rows.size());
00381 return rows[r];
00382 }
00383 inline size_t getProxyCol(size_t c) const {
00384 ASSERT_(c<cols.size());
00385 return cols[c];
00386 }
00387 inline value_type getWithRowProxied(size_t proxyRow,size_t c) const {
00388 return base.get_unsafe(proxyRow,cols[c]);
00389 }
00390 inline value_type getWithColProxied(size_t r,size_t proxyCol) const {
00391 return base.get_unsafe(rows[r],proxyCol);
00392 }
00393 };
00394
00395
00396
00397
00398
00399
00400 template<typename MATRIXTYPE> class CDiagonalMatrixView :public CMatrixView {
00401 protected:
00402 MATRIXTYPE &base;
00403 public:
00404 typedef CDiagonalMatrixView<MATRIXTYPE> mrpt_autotype;
00405 DECLARE_MATRIX_VIEW_TYPES(MATRIXTYPE)
00406 DECLARE_MRPT_CONTAINER_TYPES
00407 DECLARE_MRPT_CONTAINER_IS_MATRIX
00408 DECLARE_MRPT_MATRIX_ITERATORS
00409 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
00410 inline CDiagonalMatrixView(MATRIXTYPE &m):base(m) {}
00411 inline size_t getRowCount() const { return 1; }
00412 inline size_t getColCount() const { return base.getRowCount(); }
00413 inline value_type &get_unsafe(size_t r,size_t c) {
00414 if (r!=0) throw std::runtime_error("CDiagonalMatrixView is a 1xN matrix: access out of range");
00415 else return base.get_unsafe(c,c);
00416 }
00417 inline value_type get_unsafe(size_t r,size_t c) const {
00418 if (r!=0) throw std::runtime_error("CDiagonalMatrixView is a 1xN matrix: access out of range");
00419 else return base.get_unsafe(c,c);
00420 }
00421 inline void set_unsafe(size_t r,size_t c,value_type v) {
00422 if (r!=0) throw std::runtime_error("CDiagonalMatrixView is a 1xN matrix: access out of range");
00423 else return base.get_unsafe(c,c)=v;
00424 }
00425 inline value_type &operator()(size_t r,size_t c) {
00426 if (r!=0) throw std::runtime_error("CDiagonalMatrixView is a 1xN matrix: access out of range");
00427 else return base(c,c);
00428 }
00429 inline value_type operator()(size_t r,size_t c) const {
00430 if (r!=0) throw std::runtime_error("CDiagonalMatrixView is a 1xN matrix: access out of range");
00431 else return base(c,c);
00432 }
00433 inline value_type operator[](size_t i) const {
00434 return base.get_unsafe(i,i);
00435 }
00436 inline value_type &operator[](size_t i) {
00437 return base.get_unsafe(i,i);
00438 }
00439
00440 template<typename CONTAINER> inline mrpt_autotype &operator=(const CONTAINER &m) {
00441 const size_t nElements = m.size();
00442 ASSERT_(nElements==base.getColCount())
00443 typename CONTAINER::const_iterator itSrc;
00444 size_t c;
00445 for (c=0,itSrc=m.begin(); c<nElements; ++c, ++itSrc)
00446 base.get_unsafe(c,c) = *itSrc;
00447 return *this;
00448 }
00449 inline CMatrixTemplateSize size() const {
00450 size_t dimsA[2];
00451 dimsA[0]=1;
00452 dimsA[1]=base.getColCount();
00453 return CMatrixTemplateSize(dimsA);
00454 }
00455 inline void setSize(size_t r,size_t c) {
00456 if ((r!=1)||(c!=base.getColCount())) throw std::logic_error("Tried to change size of a diagonal matrix with another size.");
00457 }
00458 inline void resize(size_t rtc) {
00459 if (rtc!=base.getColCount()) throw std::logic_error("Tried to change size of a diagonal matrix with another size.");
00460 }
00461 };
00462
00463 namespace detail {
00464
00465
00466
00467 template<typename A,typename T> class AccessorIterator {
00468 protected:
00469 A *base;
00470 int pos;
00471 public:
00472
00473 typedef std::random_access_iterator_tag iterator_category;
00474 typedef T value_type;
00475 typedef int difference_type;
00476 typedef T *pointer;
00477 typedef T &reference;
00478
00479 inline AccessorIterator(A &obj,size_t N):base(&obj),pos(N) {}
00480 inline T &operator*() const {
00481 return (*base)[pos];
00482 }
00483 inline AccessorIterator<A,T> &operator++() {
00484 ++pos;
00485 return *this;
00486 }
00487 inline AccessorIterator<A,T> operator++(int) {
00488 AccessorIterator<A,T> it=*this;
00489 ++*this;
00490 return it;
00491 }
00492 inline AccessorIterator<A,T> &operator--() {
00493 --pos;
00494 return *this;
00495 }
00496 inline AccessorIterator<A,T> operator--(int) {
00497 AccessorIterator<A,T> it=*this;
00498 --*this;
00499 return it;
00500 }
00501 inline AccessorIterator<A,T> &operator+=(int off) {
00502 pos+=off;
00503 return *this;
00504 }
00505 inline AccessorIterator<A,T> operator+(int off) const {
00506 AccessorIterator<A,T> it=*this;
00507 it+=off;
00508 return it;
00509 }
00510 inline AccessorIterator<A,T> &operator-=(int off) {
00511 pos-=off;
00512 return *this;
00513 }
00514 inline AccessorIterator<A,T> operator-(int off) const {
00515 AccessorIterator<A,T> it=*this;
00516 it-=off;
00517 return it;
00518 }
00519 inline int operator-(const AccessorIterator<A,T> &it) const {
00520 return pos-it.pos;
00521 }
00522 inline T &operator[](int off) const {
00523 return (*base)[pos+off];
00524 }
00525 inline bool operator==(const AccessorIterator<A,T> &it) const {
00526 return (pos==it.pos)&&(base==it.base);
00527 }
00528 inline bool operator!=(const AccessorIterator<A,T> &it) const {
00529 return !(operator==(it));
00530 }
00531 };
00532
00533
00534
00535
00536 template<typename A,typename T> class ReverseAccessorIterator {
00537 protected:
00538 A *base;
00539 int pos;
00540 public:
00541
00542 typedef std::random_access_iterator_tag iterator_category;
00543 typedef T value_type;
00544 typedef int difference_type;
00545 typedef T *pointer;
00546 typedef T &reference;
00547
00548 inline ReverseAccessorIterator(A &obj,size_t N):base(&obj),pos(N) {}
00549 inline T &operator*() const {
00550 return (*base)[pos];
00551 }
00552 inline ReverseAccessorIterator<A,T> &operator++() {
00553 --pos;
00554 return *this;
00555 }
00556 inline ReverseAccessorIterator<A,T> operator++(int) {
00557 ReverseAccessorIterator<A,T> it=*this;
00558 ++*this;
00559 return it;
00560 }
00561 inline ReverseAccessorIterator<A,T> &operator--() {
00562 ++pos;
00563 return *this;
00564 }
00565 inline ReverseAccessorIterator<A,T> operator--(int) {
00566 ReverseAccessorIterator<A,T> it=*this;
00567 --*this;
00568 return it;
00569 }
00570 inline ReverseAccessorIterator<A,T> &operator+=(int off) {
00571 pos-=off;
00572 return *this;
00573 }
00574 inline ReverseAccessorIterator<A,T> operator+(int off) const {
00575 ReverseAccessorIterator<A,T> it=*this;
00576 it+=off;
00577 return it;
00578 }
00579 inline AccessorIterator<A,T> &operator-=(int off) {
00580 pos+=off;
00581 return *this;
00582 }
00583 inline AccessorIterator<A,T> operator-(int off) const {
00584 ReverseAccessorIterator<A,T> it=*this;
00585 it-=off;
00586 return it;
00587 }
00588 inline int operator-(const ReverseAccessorIterator<A,T> &it) const {
00589 return it.pos-pos;
00590 }
00591 inline T &operator[](int off) const {
00592 return (*base)[pos-off];
00593 }
00594 inline bool operator==(const ReverseAccessorIterator<A,T> &it) const {
00595 return (pos==it.pos)&&(&base==&it.base);
00596 }
00597 inline bool operator!=(const ReverseAccessorIterator<A,T> &it) const {
00598 return !(operator==(it));
00599 }
00600 };
00601 }
00602
00603
00604
00605
00606 template <typename MAT> class CMatrixColumnAccessor {
00607 protected:
00608 MAT *m_mat;
00609 size_t m_colInd;
00610 public:
00611 typedef typename MAT::value_type value_type;
00612 typedef CMatrixColumnAccessor<MAT> mrpt_autotype;
00613 DECLARE_MRPT_CONTAINER_TYPES
00614 DECLARE_MRPT_CONTAINER_IS_VECTOR
00615 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00616 inline CMatrixColumnAccessor(MAT &mat, size_t colIdx) : m_mat(&mat), m_colInd(colIdx) { ASSERT_(colIdx<mat.getColCount()) }
00617 inline CMatrixColumnAccessor() {}
00618 inline value_type &operator[](const size_t i) { return (*m_mat)(i,m_colInd); }
00619 inline value_type operator[](const size_t i) const { return (*m_mat)(i,m_colInd); }
00620 typedef detail::AccessorIterator<CMatrixColumnAccessor<MAT>,value_type> iterator;
00621 typedef detail::AccessorIterator<const CMatrixColumnAccessor<MAT>,const value_type> const_iterator;
00622 typedef detail::ReverseAccessorIterator<CMatrixColumnAccessor<MAT>,value_type> reverse_iterator;
00623 typedef detail::ReverseAccessorIterator<const CMatrixColumnAccessor<MAT>,const value_type> const_reverse_iterator;
00624 inline iterator begin() {
00625 return iterator(*this,0);
00626 }
00627 inline const_iterator begin() const {
00628 return const_iterator(*this,0);
00629 }
00630 inline iterator end() {
00631 return iterator(*this,m_mat->getRowCount());
00632 }
00633 inline const_iterator end() const {
00634 return const_iterator(*this,m_mat->getRowCount());
00635 }
00636 inline reverse_iterator rbegin() {
00637 return reverse_iterator(*this,m_mat->getRowCount()-1);
00638 }
00639 inline const_reverse_iterator rbegin() const {
00640 return const_reverse_iterator(*this,m_mat->getRowCount()-1);
00641 }
00642 inline reverse_iterator rend() {
00643 return reverse_iterator(*this,-1);
00644 }
00645 inline const_reverse_iterator rend() const {
00646 return const_reverse_iterator(*this,-1);
00647 }
00648 inline size_t size() const {
00649 return m_mat->getRowCount();
00650 }
00651 inline void resize(size_t N) {
00652 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00653 }
00654 };
00655 template<typename MAT> inline CMatrixColumnAccessor<MAT> getColumnAccessor(MAT &m,size_t colIdx) {
00656 return CMatrixColumnAccessor<MAT>(m,colIdx);
00657 }
00658
00659
00660
00661
00662 template<typename MAT>
00663 class CMatrixColumnAccessorExtended {
00664 protected:
00665 MAT *m_mat;
00666 size_t m_colInd;
00667 size_t m_rowOffset;
00668 size_t m_elementsSpace;
00669 size_t howMany;
00670 public:
00671 typedef typename MAT::value_type value_type;
00672 typedef CMatrixColumnAccessorExtended<MAT> mrpt_autotype;
00673 DECLARE_MRPT_CONTAINER_TYPES
00674 DECLARE_MRPT_CONTAINER_IS_VECTOR
00675 DECLARE_COMMON_CONTAINERS_MEMBERS(mrpt_autotype)
00676 inline CMatrixColumnAccessorExtended(MAT &mat,size_t col,size_t offset,size_t space):m_mat(&mat),m_colInd(col),m_rowOffset(offset),m_elementsSpace(space) {
00677 ASSERT_(col<mat.getColCount());
00678 howMany=(mat.getRowCount()-m_rowOffset)/m_elementsSpace;
00679 }
00680 inline CMatrixColumnAccessorExtended() {}
00681 inline value_type &operator[](size_t i) {
00682 return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
00683 }
00684 inline value_type operator[](size_t i) const {
00685 return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
00686 }
00687 typedef detail::AccessorIterator<CMatrixColumnAccessorExtended<MAT>,value_type> iterator;
00688 typedef detail::AccessorIterator<const CMatrixColumnAccessorExtended<MAT>,const value_type> const_iterator;
00689 typedef detail::ReverseAccessorIterator<CMatrixColumnAccessorExtended<MAT>,value_type> reverse_iterator;
00690 typedef detail::ReverseAccessorIterator<const CMatrixColumnAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00691 inline iterator begin() {
00692 return iterator(*this,0);
00693 }
00694 inline const_iterator begin() const {
00695 return const_iterator(*this,0);
00696 }
00697 inline iterator end() {
00698 return iterator(*this,howMany);
00699 }
00700 inline const_iterator end() const {
00701 return const_iterator(*this,howMany);
00702 }
00703 inline reverse_iterator rbegin() {
00704 return reverse_iterator(*this,howMany-1);
00705 }
00706 inline const_reverse_iterator rbegin() const {
00707 return const_reverse_iterator(*this,howMany-1);
00708 }
00709 inline reverse_iterator rend() {
00710 return reverse_iterator(*this,-1);
00711 }
00712 inline const_reverse_iterator rend() const {
00713 return const_reverse_iterator(*this,-1);
00714 }
00715 inline size_t size() const {
00716 return howMany();
00717 }
00718 inline void resize(size_t N) {
00719 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00720 }
00721 };
00722 template<typename MAT> inline CMatrixColumnAccessorExtended<MAT> getColumnAccessor(MAT &m,size_t colIdx,size_t offset,size_t space=1) {
00723 return CMatrixColumnAccessorExtended<MAT>(m,colIdx,offset,space);
00724 }
00725
00726
00727
00728
00729 template<class MAT>
00730 class CConstMatrixColumnAccessor {
00731 protected:
00732 const MAT *m_mat;
00733 size_t m_colInd;
00734 public:
00735 typedef typename MAT::value_type value_type;
00736 typedef CConstMatrixColumnAccessor<MAT> mrpt_autotype;
00737 DECLARE_MRPT_CONTAINER_TYPES
00738 DECLARE_MRPT_CONTAINER_IS_VECTOR
00739 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00740 inline CConstMatrixColumnAccessor(const MAT &mat,size_t colIdx):m_mat(&mat),m_colInd(colIdx) {
00741 ASSERT_(colIdx<mat.getColCount());
00742 }
00743 inline CConstMatrixColumnAccessor() {}
00744 inline value_type operator[](size_t i) const {
00745 return (*m_mat)(i,m_colInd);
00746 }
00747 typedef detail::AccessorIterator<const CConstMatrixColumnAccessor<MAT>,const value_type> const_iterator;
00748 typedef detail::ReverseAccessorIterator<const CConstMatrixColumnAccessor<MAT>,const value_type> const_reverse_iterator;
00749 inline const_iterator begin() const {
00750 return const_iterator(*this,0);
00751 }
00752 inline const_iterator end() const {
00753 return const_iterator(*this,m_mat->getRowCount());
00754 }
00755 inline const_reverse_iterator rbegin() const {
00756 return const_reverse_iterator(*this,m_mat->getRowCount()-1);
00757 }
00758 inline const_reverse_iterator rend() const {
00759 return const_reverse_iterator(*this,-1);
00760 }
00761 inline size_t size() const {
00762 return m_mat->getRowCount();
00763 }
00764 inline void resize(size_t N) {
00765 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00766 }
00767 };
00768 template<typename MAT> inline CConstMatrixColumnAccessor<MAT> getColumnAccessor(const MAT &m,size_t colIdx) {
00769 return CConstMatrixColumnAccessor<MAT>(m,colIdx);
00770 }
00771
00772
00773
00774
00775 template<typename MAT>
00776 class CConstMatrixColumnAccessorExtended {
00777 protected:
00778 const MAT *m_mat;
00779 size_t m_colInd;
00780 size_t m_rowOffset;
00781 size_t m_elementsSpace;
00782 size_t howMany;
00783 public:
00784 typedef typename MAT::value_type value_type;
00785 typedef CMatrixColumnAccessorExtended<MAT> mrpt_autotype;
00786 DECLARE_MRPT_CONTAINER_TYPES
00787 DECLARE_MRPT_CONTAINER_IS_VECTOR
00788 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00789 inline CConstMatrixColumnAccessorExtended(const MAT &mat,size_t col,size_t offset,size_t space):m_mat(&mat),m_colInd(col),m_rowOffset(offset),m_elementsSpace(space) {
00790 ASSERT_(col<mat.getColCount());
00791 howMany=(mat.getRowCount()-m_rowOffset)/m_elementsSpace;
00792 }
00793 inline CConstMatrixColumnAccessorExtended() {}
00794 inline value_type operator[](size_t i) const {
00795 return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
00796 }
00797 typedef detail::AccessorIterator<const CConstMatrixColumnAccessorExtended<MAT>,const value_type> const_iterator;
00798 typedef detail::ReverseAccessorIterator<const CConstMatrixColumnAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00799 inline const_iterator begin() const {
00800 return const_iterator(*this,0);
00801 }
00802 inline const_iterator end() const {
00803 return const_iterator(*this,howMany);
00804 }
00805 inline const_reverse_iterator rbegin() const {
00806 return const_reverse_iterator(*this,howMany-1);
00807 }
00808 inline const_reverse_iterator rend() const {
00809 return const_reverse_iterator(*this,-1);
00810 }
00811 inline size_t size() const {
00812 return howMany;
00813 }
00814 inline void resize(size_t N) {
00815 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00816 }
00817 };
00818 template<typename MAT> inline CConstMatrixColumnAccessorExtended<MAT> getColumnAccessor(const MAT &m,size_t colIdx,size_t offset,size_t space=1) {
00819 return CConstMatrixColumnAccessorExtended<MAT>(m,colIdx,offset,space);
00820 }
00821
00822
00823
00824
00825 template <typename MAT>
00826 class CMatrixRowAccessor
00827 {
00828 protected:
00829 MAT *m_mat;
00830 size_t m_rowInd;
00831 public:
00832 typedef typename MAT::value_type value_type;
00833 typedef CMatrixRowAccessor<MAT> mrpt_autotype;
00834 DECLARE_MRPT_CONTAINER_TYPES
00835 DECLARE_MRPT_CONTAINER_IS_VECTOR
00836 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00837 inline CMatrixRowAccessor(MAT &mat, size_t rowIdx) : m_mat(&mat), m_rowInd(rowIdx) { ASSERT_(rowIdx<mat.getRowCount()) }
00838 inline CMatrixRowAccessor() {}
00839 inline value_type &operator[](const size_t i) { return (*m_mat)(m_rowInd,i); }
00840 inline value_type operator[](const size_t i) const { return (*m_mat)(m_rowInd,i); }
00841 typedef detail::AccessorIterator<CMatrixRowAccessor<MAT>,value_type> iterator;
00842 typedef detail::AccessorIterator<const CMatrixRowAccessor<MAT>,const value_type> const_iterator;
00843 typedef detail::ReverseAccessorIterator<CMatrixRowAccessor<MAT>,value_type> reverse_iterator;
00844 typedef detail::ReverseAccessorIterator<const CMatrixRowAccessor<MAT>,const value_type> const_reverse_iterator;
00845 inline iterator begin() {
00846 return iterator(*this,0);
00847 }
00848 inline const_iterator begin() const {
00849 return const_iterator(*this,0);
00850 }
00851 inline iterator end() {
00852 return iterator(*this,m_mat->getColCount());
00853 }
00854 inline const_iterator end() const {
00855 return const_iterator(*this,m_mat->getColCount());
00856 }
00857 inline reverse_iterator rbegin() {
00858 return reverse_iterator(*this,m_mat->getColCount()-1);
00859 }
00860 inline const_reverse_iterator rbegin() const {
00861 return const_reverse_iterator(*this,m_mat.getColCount()-1);
00862 }
00863 inline reverse_iterator rend() {
00864 return reverse_iterator(*this,-1);
00865 }
00866 inline const_reverse_iterator rend() const {
00867 return const_reverse_iterator(*this,-1);
00868 }
00869 inline size_t size() const {
00870 return m_mat->getColCount();
00871 }
00872 inline void resize(size_t N) {
00873 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00874 }
00875 };
00876 template<typename MAT> inline CMatrixRowAccessor<MAT> getRowAccessor(MAT &m,size_t rowIdx) {
00877 return CMatrixRowAccessor<MAT>(m,rowIdx);
00878 }
00879
00880
00881
00882
00883 template<class MAT>
00884 class CMatrixRowAccessorExtended {
00885 protected:
00886 MAT *m_mat;
00887 size_t m_rowInd;
00888 size_t m_colOffset;
00889 size_t m_elementsSpace;
00890 size_t howMany;
00891 public:
00892 typedef typename MAT::value_type value_type;
00893 typedef CMatrixRowAccessorExtended<MAT> mrpt_autotype;
00894 DECLARE_MRPT_CONTAINER_TYPES
00895 DECLARE_MRPT_CONTAINER_IS_VECTOR
00896 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00897 inline CMatrixRowAccessorExtended(MAT &mat,size_t row,size_t offset,size_t space):m_mat(&mat),m_rowInd(row),m_colOffset(offset),m_elementsSpace(space) {
00898 ASSERT_(row<mat.getRowCount());
00899 howMany=(mat.getColCount()-m_colOffset)/m_elementsSpace;
00900 }
00901 inline CMatrixRowAccessorExtended() {}
00902 inline value_type &operator[](size_t i) {
00903 return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
00904 }
00905 inline value_type operator[](size_t i) const {
00906 return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
00907 }
00908 typedef detail::AccessorIterator<CMatrixRowAccessorExtended<MAT>,value_type> iterator;
00909 typedef detail::AccessorIterator<const CMatrixRowAccessorExtended<MAT>,const value_type> const_iterator;
00910 typedef detail::ReverseAccessorIterator<CMatrixRowAccessorExtended<MAT>,value_type> reverse_iterator;
00911 typedef detail::ReverseAccessorIterator<const CMatrixRowAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00912 inline iterator begin() {
00913 return iterator(*this,0);
00914 }
00915 inline const_iterator begin() const {
00916 return const_iterator(*this,0);
00917 }
00918 inline iterator end() {
00919 return iterator(*this,howMany);
00920 }
00921 inline const_iterator end() const {
00922 return const_iterator(*this,howMany);
00923 }
00924 inline reverse_iterator rbegin() {
00925 return reverse_iterator(*this,howMany-1);
00926 }
00927 inline const_reverse_iterator rbegin() const {
00928 return const_reverse_iterator(*this,howMany-1);
00929 }
00930 inline reverse_iterator rend() {
00931 return reverse_iterator(*this,-1);
00932 }
00933 inline const_reverse_iterator rend() const {
00934 return const_reverse_iterator(*this,-1);
00935 }
00936 inline size_t size() const {
00937 return howMany;
00938 }
00939 inline void resize(size_t N) {
00940 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00941 }
00942 };
00943 template<typename MAT> inline CMatrixRowAccessorExtended<MAT> getRowAccessor(MAT &m,size_t rowIdx,size_t offset,size_t space=1) {
00944 return CMatrixRowAccessor<MAT>(m,rowIdx,offset,space);
00945 }
00946
00947
00948
00949
00950 template<class MAT>
00951 class CConstMatrixRowAccessor {
00952 protected:
00953 const MAT *m_mat;
00954 size_t m_rowInd;
00955 public:
00956 typedef typename MAT::value_type value_type;
00957 typedef CConstMatrixRowAccessor<MAT> mrpt_autotype;
00958 DECLARE_MRPT_CONTAINER_TYPES
00959 DECLARE_MRPT_CONTAINER_IS_VECTOR
00960 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00961 inline CConstMatrixRowAccessor(const MAT &mat,size_t row):m_mat(&mat),m_rowInd(row) {
00962 ASSERT_(row<mat.getRowCount());
00963 }
00964 inline CConstMatrixRowAccessor() {}
00965 inline value_type operator[](size_t i) const {
00966 return (*m_mat)(m_rowInd,i);
00967 }
00968 typedef detail::AccessorIterator<const CConstMatrixRowAccessor<MAT>,const value_type> const_iterator;
00969 typedef detail::ReverseAccessorIterator<const CConstMatrixRowAccessor<MAT>,const value_type> const_reverse_iterator;
00970 inline const_iterator begin() const {
00971 return const_iterator(*this,0);
00972 }
00973 inline const_iterator end() const {
00974 return const_iterator(*this,m_mat->getColCount());
00975 }
00976 inline const_reverse_iterator rbegin() const {
00977 return const_reverse_iterator(*this,m_mat->getColCount()-1);
00978 }
00979 inline const_reverse_iterator rend() const {
00980 return const_reverse_iterator(*this,-1);
00981 }
00982 inline size_t size() const {
00983 return m_mat->getColCount();
00984 }
00985 inline void resize(size_t N) {
00986 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00987 }
00988 };
00989 template<typename MAT> inline CConstMatrixRowAccessor<MAT> getRowAccessor(const MAT &m,size_t rowIdx) {
00990 return CMatrixRowAccessor<MAT>(m,rowIdx);
00991 }
00992
00993
00994
00995
00996 template<class MAT>
00997 class CConstMatrixRowAccessorExtended {
00998 protected:
00999 const MAT *m_mat;
01000 size_t m_rowInd;
01001 size_t m_colOffset;
01002 size_t m_elementsSpace;
01003 size_t howMany;
01004 public:
01005 typedef typename MAT::value_type value_type;
01006 typedef CConstMatrixRowAccessorExtended<MAT> mrpt_autotype;
01007 DECLARE_MRPT_CONTAINER_TYPES
01008 DECLARE_MRPT_CONTAINER_IS_VECTOR
01009 DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
01010 inline CConstMatrixRowAccessorExtended(const MAT &mat,size_t row,size_t offset,size_t space):m_mat(&mat),m_rowInd(row),m_colOffset(offset),m_elementsSpace(space) {
01011 ASSERT_(row<mat.getRowCount());
01012 howMany=(mat.getColCount()-m_colOffset)/m_elementsSpace;
01013 }
01014 inline CConstMatrixRowAccessorExtended() {}
01015 inline value_type operator[](size_t i) const {
01016 return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
01017 }
01018 typedef detail::AccessorIterator<const CConstMatrixRowAccessorExtended<MAT>,const value_type> const_iterator;
01019 typedef detail::ReverseAccessorIterator<const CConstMatrixRowAccessorExtended<MAT>,const value_type> const_reverse_iterator;
01020 inline const_iterator begin() const {
01021 return const_iterator(*this,0);
01022 }
01023 inline const_iterator end() const {
01024 return const_iterator(*this,howMany);
01025 }
01026 inline const_reverse_iterator rbegin() const {
01027 return const_reverse_iterator(*this,howMany-1);
01028 }
01029 inline const_reverse_iterator rend() const {
01030 return const_reverse_iterator(*this,-1);
01031 }
01032 inline size_t size() const {
01033 return howMany;
01034 }
01035 inline void resize(size_t N) {
01036 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
01037 }
01038 };
01039 template<typename MAT> inline CConstMatrixRowAccessorExtended<MAT> getRowAccessor(const MAT &m,size_t rowIdx,size_t offset,size_t space=1) {
01040 return CConstMatrixRowAccessorExtended<MAT>(m,rowIdx,offset,space);
01041 }
01042
01043 template<typename VEC> class CVectorRowWrapper:public CMatrixView {
01044 private:
01045 VEC &vec;
01046 public:
01047 typedef CVectorRowWrapper<VEC> mrpt_autotype;
01048 DECLARE_MATRIX_VIEW_TYPES(VEC)
01049 DECLARE_MRPT_CONTAINER_TYPES
01050 DECLARE_MRPT_CONTAINER_IS_MATRIX
01051 DECLARE_MRPT_MATRIX_ITERATORS
01052 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
01053 inline CVectorRowWrapper(VEC &v):vec(v) {}
01054 inline static size_t getRowCount() {
01055 return 1;
01056 }
01057 inline size_t getColCount() const {
01058 return vec.size();
01059 }
01060 inline value_type &get_unsafe(size_t i,size_t j) {
01061 return vec[j];
01062 }
01063 inline value_type get_unsafe(size_t i,size_t j) const {
01064 return vec[j];
01065 }
01066 inline void set_unsafe(size_t i,size_t j,value_type v) {
01067 vec[j]=v;
01068 }
01069 inline value_type &operator()(size_t i,size_t j) {
01070 ASSERT_(i==0);
01071 return vec[j];
01072 }
01073 inline value_type operator()(size_t i,size_t j) const {
01074 ASSERT_(i==0);
01075 return vec[j];
01076 }
01077 inline void setSize(size_t r,size_t c) {
01078 ASSERT_(r==1);
01079 vec.resize(c);
01080 }
01081 inline void resize(size_t rc) {
01082 vec.resize(rc);
01083 }
01084 };
01085 template<typename VEC> inline CVectorRowWrapper<VEC> getAsRow(VEC &v) {
01086 return CVectorRowWrapper<VEC>(v);
01087 }
01088
01089 template<typename VEC> class CConstVectorRowWrapper:public CMatrixView {
01090 private:
01091 const VEC &vec;
01092 public:
01093 typedef CConstVectorRowWrapper<VEC> mrpt_autotype;
01094 DECLARE_MATRIX_VIEW_TYPES(VEC)
01095 DECLARE_MRPT_CONTAINER_TYPES
01096 DECLARE_MRPT_CONTAINER_IS_MATRIX
01097 DECLARE_MRPT_MATRIX_ITERATORS
01098 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
01099 inline CConstVectorRowWrapper(const VEC &v):vec(v) {}
01100 inline static size_t getRowCount() {
01101 return 1;
01102 }
01103 inline size_t getColCount() const {
01104 return vec.size();
01105 }
01106 inline value_type get_unsafe(size_t i,size_t j) const {
01107 return vec[j];
01108 }
01109 inline value_type operator()(size_t i,size_t j) const {
01110 ASSERT_(i==0);
01111 return vec[j];
01112 }
01113 inline void setSize(size_t r,size_t c) {
01114 ASSERT_(r==0);
01115 if (c!=vec.size()) throw std::logic_error("Tried to resize a const object.");
01116 }
01117 inline void resize(size_t rc) {
01118 if (rc!=vec.size()) throw std::logic_error("Tried to resize a const object.");
01119 }
01120 };
01121 template<typename VEC> inline CConstVectorRowWrapper<VEC> getAsRow(const VEC &v) {
01122 return CConstVectorRowWrapper<VEC>(v);
01123 }
01124
01125 template<typename VEC> class CVectorColumnWrapper:public CMatrixView {
01126 private:
01127 VEC &vec;
01128 public:
01129 typedef CVectorColumnWrapper<VEC> mrpt_autotype;
01130 DECLARE_MATRIX_VIEW_TYPES(VEC)
01131 DECLARE_MRPT_CONTAINER_TYPES
01132 DECLARE_MRPT_CONTAINER_IS_MATRIX
01133 DECLARE_MRPT_MATRIX_ITERATORS
01134 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
01135 inline CVectorColumnWrapper(VEC &v):vec(v) {}
01136 inline size_t getRowCount() const {
01137 return vec.size();
01138 }
01139 inline static size_t getColCount() {
01140 return 1;
01141 }
01142 inline value_type &get_unsafe(size_t i,size_t j) {
01143 return vec[i];
01144 }
01145 inline value_type get_unsafe(size_t i,size_t j) const {
01146 return vec[i];
01147 }
01148 inline void set_unsafe(size_t i,size_t j,value_type v) {
01149 vec[i]=v;
01150 }
01151 inline value_type &operator()(size_t i,size_t j) {
01152 ASSERT_(j==0);
01153 return vec[i];
01154 }
01155 inline value_type operator()(size_t i,size_t j) const {
01156 ASSERT_(j==0);
01157 return vec[i];
01158 }
01159 inline void setSize(size_t r,size_t c) {
01160 ASSERT_(c==1);
01161 vec.resize(r);
01162 }
01163 inline void resize(size_t rc) {
01164 vec.resize(rc);
01165 }
01166 };
01167 template<typename VEC> inline CVectorColumnWrapper<VEC> getAsRow(VEC &v) {
01168 return CVectorColumnWrapper<VEC>(v);
01169 }
01170
01171 template<typename VEC> class CConstVectorColumnWrapper:public CMatrixView {
01172 private:
01173 const VEC &vec;
01174 public:
01175 typedef CConstVectorColumnWrapper<VEC> mrpt_autotype;
01176 DECLARE_MATRIX_VIEW_TYPES(VEC)
01177 DECLARE_MRPT_CONTAINER_TYPES
01178 DECLARE_MRPT_CONTAINER_IS_MATRIX
01179 DECLARE_MRPT_MATRIX_ITERATORS
01180 DECLARE_COMMON_MATRIX_MEMBERS(value_type)
01181 inline CConstVectorColumnWrapper(const VEC &v):vec(v) {}
01182 inline size_t getRowCount() const {
01183 return vec.size();
01184 }
01185 inline size_t static getColCount() {
01186 return 1;
01187 }
01188 inline value_type get_unsafe(size_t i,size_t j) const {
01189 return vec[i];
01190 }
01191 inline value_type operator()(size_t i,size_t j) const {
01192 ASSERT_(j==0);
01193 return vec[i];
01194 }
01195 inline void setSize(size_t r,size_t c) {
01196 ASSERT_(c==1);
01197 if (r!=vec.size()) throw std::logic_error("Tried to resize a const object.");
01198 }
01199 inline void resize(size_t rc) {
01200 if (rc!=vec.size()) throw std::logic_error("Tried to resize a const object.");
01201 }
01202 };
01203 template<typename VEC> inline CConstVectorColumnWrapper<VEC> getAsRow(VEC &v) {
01204 return CConstVectorColumnWrapper<VEC>(v);
01205 }
01206
01207
01208
01209
01210
01211 template<typename M1,typename M2> class JointHorizontalAccessor {
01212 private:
01213 M1 &first;
01214 M2 &second;
01215 public:
01216 typedef typename M1::value_type value_type;
01217 const size_t C1,C2;
01218 const size_t R;
01219 inline JointHorizontalAccessor(M1 &f,M2 &s):first(f),second(s),C1(f.getColCount()),C2(s.getColCount()),R(f.getRowCount()) {
01220 ASSERT_(s.getRowCount()==R);
01221
01222 }
01223 inline value_type &getFirst(size_t dim1,size_t dim2) {
01224 return first.get_unsafe(dim1,dim2);
01225 }
01226 inline value_type &getSecond(size_t dim1,size_t dim2) {
01227 return second.get_unsafe(dim1,dim2);
01228 }
01229 inline size_t getRowCount() const {
01230 return R;
01231 }
01232 inline size_t getColCount() const {
01233 return C1+C2;
01234 }
01235 inline value_type &operator()(size_t i,size_t j) {
01236 return (j<C1)?first(i,j):second(i,j-C1);
01237 }
01238 inline value_type operator()(size_t i,size_t j) const {
01239 return (j<C1)?first(i,j):second(i,j-C1);
01240 }
01241 inline value_type &get_unsafe(size_t i,size_t j) {
01242 return (j<C1)?first.get_unsafe(i,j):second.get_unsafe(i,j-C1);
01243 }
01244 inline value_type get_unsafe(size_t i,size_t j) const {
01245 return (j<C1)?first.get_unsafe(i,j):second.get_unsafe(i,j-C1);
01246 }
01247 inline void set_unsafe(size_t i,size_t j,value_type v) {
01248 if (j<C1) first.set_unsafe(i,j,v);
01249 else second.set_unsafe(i,j-C1,v);
01250 }
01251 };
01252
01253
01254 template<typename M1,typename M2> class JointVerticalAccessor {
01255 private:
01256 M1 &first;
01257 M2 &second;
01258 public:
01259 typedef typename M1::value_type value_type;
01260 const size_t C1,C2;
01261 const size_t R;
01262 inline JointVerticalAccessor(M1 &f,M2 &s):first(f),second(s),C1(f.getRowCount()),C2(s.getRowCount()),R(f.getColCount()) {
01263 ASSERT_(s.getColCount()==R);
01264
01265 }
01266
01267 inline value_type &getFirst(size_t dim1,size_t dim2) {
01268 return first.get_unsafe(dim2,dim1);
01269 }
01270 inline value_type &getSecond(size_t dim1,size_t dim2) {
01271 return second.get_unsafe(dim2,dim1);
01272 }
01273 inline size_t getRowCount() const {
01274 return C1+C2;
01275 }
01276 inline size_t getColCount() const {
01277 return R;
01278 }
01279
01280 inline value_type &operator()(size_t i,size_t j) {
01281 return (i<C1)?first(i,j):second(i-C1,j);
01282 }
01283 inline value_type operator()(size_t i,size_t j) const {
01284 return (i<C1)?first(i,j):second(i-C1,j);
01285 }
01286 inline value_type &get_unsafe(size_t i,size_t j) {
01287 return (i<C1)?first.get_unsafe(i,j):second.get_unsafe(i-C1,j);
01288 }
01289 inline value_type get_unsafe(size_t i,size_t j) const {
01290 return (i<C1)?first.get_unsafe(i,j):second.get_unsafe(i-C1,j);
01291 }
01292 inline void set_unsafe(size_t i,size_t j,value_type v) {
01293 if (i<C1) first.set_unsafe(i,j,v);
01294 else second.set_unsafe(i-C1,j,v);
01295 }
01296 };
01297
01298
01299
01300
01301 template<typename JA> class JointAccessor {
01302 public:
01303 typedef typename JA::value_type value_type;
01304 private:
01305 JA &joint;
01306 value_type eps;
01307 public:
01308 inline JointAccessor(JA &j,value_type e=1e-7):joint(j),eps(e) {}
01309 inline void sumRowMultiplied(size_t from,size_t to,size_t startIndex,value_type coef) {
01310 for (size_t i=startIndex;i<joint.C1;++i) joint.getFirst(to,i)+=coef*joint.getFirst(from,i);
01311 for (size_t i=0;i<joint.C2;++i) joint.getSecond(to,i)+=coef*joint.getSecond(from,i);
01312 }
01313
01314
01315
01316
01317
01318 inline void substractRowAsNeeded(size_t from,size_t to) {
01319 value_type coef=joint.getFirst(to,from)/joint.getFirst(from,from);
01320 if (std::abs(coef)>=eps) sumRowMultiplied(from,to,from+1,-coef);
01321 }
01322 inline void substractWhenReduced(size_t from,size_t to) {
01323 value_type coef=joint.getFirst(to,from);
01324 if (std::abs(coef)>=eps) for (size_t i=0;i<joint.C2;++i) joint.getSecond(to,i)-=coef*joint.getSecond(from,i);
01325 }
01326 inline size_t size() const {
01327 return joint.R;
01328 }
01329 void unitarizeReducedRow(size_t pos) {
01330 value_type coef=joint.getFirst(pos,pos);
01331 joint.getFirst(pos,pos)=static_cast<value_type>(1);
01332 for (size_t i=pos+1;i<joint.C1;++i) joint.getFirst(pos,i)/=coef;
01333 for (size_t i=0;i<joint.C2;++i) joint.getSecond(pos,i)/=coef;
01334 }
01335 void ensureSuitablePos(size_t pos) {
01336 if (std::abs(joint.getFirst(pos,pos))>=eps) return;
01337 for (size_t i=pos+1;i<joint.R;++i) if (std::abs(joint.getFirst(i,pos))>=eps) {
01338 for (size_t j=pos;j<joint.C1;++j) joint.getFirst(pos,j)+=joint.getFirst(i,j);
01339 for (size_t j=0;j<joint.C2;++j) joint.getSecond(pos,j)+=joint.getSecond(i,j);
01340 return;
01341 }
01342 throw std::logic_error("ensureSuitablePos: Divisor is a singular matrix");
01343 }
01344 void ensureAndUnitarizeLast() {
01345 size_t pos=joint.R-1;
01346 value_type coef=joint.getFirst(pos,pos);
01347 if (std::abs(coef)<eps) throw std::logic_error("ensureAndUnitarizeLast: Divisor is a singular matrix");
01348 joint.getFirst(pos,pos)=value_type(1);
01349 for (size_t i=0;i<joint.C2;++i) joint.getSecond(pos,i)/=coef;
01350 }
01351 void dumpToConsole() const {
01352 for (size_t i=0;i<joint.R;i++) {
01353 for (size_t c=0;c<joint.C1;c++) printf("%15.03f ",joint.getFirst(i,c));
01354 for (size_t c=0;c<joint.C2;c++) printf("%15.03f ",joint.getSecond(i,c));
01355 printf("\n");
01356 }
01357 }
01358 };
01359
01360
01361 template<typename T> class IndirectAccessWrapper {
01362 private:
01363 CMatrixTemplateNumeric<T> mat;
01364 std::vector<size_t> rowsIndices;
01365 std::vector<size_t> colsIndices;
01366 class Generator {
01367 private:
01368 size_t N;
01369 size_t i;
01370 public:
01371 inline Generator(size_t n):N(n),i(0) {}
01372 inline size_t operator()() {
01373 return i++;
01374 }
01375 };
01376 public:
01377 inline IndirectAccessWrapper(const CMatrixTemplateNumeric<T> &m):mat(m),rowsIndices(m.getRowCount()),colsIndices(m.getColCount()) {
01378 std::generate(rowsIndices.begin(),rowsIndices.end(),Generator(m.getRowCount()));
01379 std::generate(colsIndices.begin(),colsIndices.end(),Generator(m.getColCount()));
01380 }
01381 inline T &operator()(size_t r,size_t c) {
01382 return mat.get_unsafe(rowsIndices[r],colsIndices[c]);
01383 }
01384 inline const T &operator()(size_t r,size_t c) const {
01385 return mat.get_unsafe(rowsIndices[r],colsIndices[c]);
01386 }
01387 inline void deleteRow(size_t r) {
01388 rowsIndices.erase(rowsIndices.begin()+ r);
01389 }
01390 inline void deleteColumn(size_t c) {
01391 colsIndices.erase(colsIndices.begin()+ c);
01392 }
01393 inline size_t getRowCount() const {
01394 return rowsIndices.size();
01395 }
01396 inline size_t getColCount() const {
01397 return colsIndices.size();
01398 }
01399 inline size_t getProxyRow(size_t r) {
01400 return rowsIndices[r];
01401 }
01402 inline T &getProxied(size_t proxiedR,size_t c) {
01403 return mat.get_unsafe(proxiedR,colsIndices[c]);
01404 }
01405 inline const T &getProxied(size_t proxiedR,size_t c) const {
01406 return mat.get_unsafe(proxiedR,colsIndices[c]);
01407 }
01408 };
01409
01410
01411 }
01412 }
01413
01414 #endif
01415