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