Main MRPT website > C++ reference
MRPT logo
matrix_adaptors.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef mrpt_matrix_adaptors_H
29 #define mrpt_matrix_adaptors_H
30 
31 #include <mrpt/utils/utils_defs.h>
32 #include <mrpt/math/math_frwds.h> // Fordward declarations
33 
34 namespace mrpt
35 {
36  namespace math
37  {
38 
39  /** Internal classes not to be directly used by the user. */
40  // Forward declarations:
41  template<typename T,typename U,bool UIsObject> class CBinaryRelation;
42  namespace detail
43  {
44  /**
45  * This template is a trick to switch the type of a variable using a boolean variable in the template. It's easy to extend its functionality to several
46  * types, using a unsigned char instead of a bool.
47  */
48  template<typename U,bool B> class MatrixWrapper;
49 
50  // partial specializations:
51  template<typename U> class MatrixWrapper<U,true> {
52  public:
54  };
55  template<typename U> class MatrixWrapper<U,false> {
56  public:
58  };
59 
60  template<typename T,typename U,bool UIsObject,typename FunctionType> inline void applyFunction(CBinaryRelation<T,U,UIsObject> &o, FunctionType fun,size_t e1,size_t e2,const T &T1,const T &T2);
61  }
62 
63 
64  namespace detail {
65  /** Template class for matrix accessor's iterators.
66  * \sa CMatrixRowAccessor,CMatrixColumnAccessor
67  */
68  template<typename A,typename T> class AccessorIterator {
69  protected:
70  A *base;
71  int pos;
72  public:
73  //typedefs for iterator_traits:
74  typedef std::random_access_iterator_tag iterator_category;
75  typedef T value_type;
76  typedef int difference_type;
77  typedef T *pointer;
78  typedef T &reference;
79 
80  inline AccessorIterator(A &obj,size_t N):base(&obj),pos(N) {}
81  inline T &operator*() const {
82  return (*base)[pos];
83  }
85  ++pos;
86  return *this;
87  }
89  AccessorIterator<A,T> it=*this;
90  ++*this;
91  return it;
92  }
94  --pos;
95  return *this;
96  }
98  AccessorIterator<A,T> it=*this;
99  --*this;
100  return it;
101  }
103  pos+=off;
104  return *this;
105  }
106  inline AccessorIterator<A,T> operator+(int off) const {
107  AccessorIterator<A,T> it=*this;
108  it+=off;
109  return it;
110  }
112  pos-=off;
113  return *this;
114  }
115  inline AccessorIterator<A,T> operator-(int off) const {
116  AccessorIterator<A,T> it=*this;
117  it-=off;
118  return it;
119  }
120  inline int operator-(const AccessorIterator<A,T> &it) const {
121  return pos-it.pos;
122  }
123  inline T &operator[](int off) const {
124  return (*base)[pos+off];
125  }
126  inline bool operator==(const AccessorIterator<A,T> &it) const {
127  return (pos==it.pos)&&(base==it.base);
128  }
129  inline bool operator!=(const AccessorIterator<A,T> &it) const {
130  return !(operator==(it));
131  }
132  };
133 
134  /** Template class for matrix accessor's iterators.
135  * \sa CMatrixRowAccessor,CMatrixColumnAccessor
136  */
137  template<typename A,typename T> class ReverseAccessorIterator {
138  protected:
139  A *base;
140  int pos;
141  public:
142  //typedefs for iterator_traits:
143  typedef std::random_access_iterator_tag iterator_category;
144  typedef T value_type;
145  typedef int difference_type;
146  typedef T *pointer;
147  typedef T &reference;
148 
149  inline ReverseAccessorIterator(A &obj,size_t N):base(&obj),pos(N) {}
150  inline T &operator*() const {
151  return (*base)[pos];
152  }
154  --pos;
155  return *this;
156  }
159  ++*this; //Yes, that's right.
160  return it;
161  }
163  ++pos;
164  return *this;
165  }
168  --*this; //Yes, that's right.
169  return it;
170  }
172  pos-=off;
173  return *this;
174  }
175  inline ReverseAccessorIterator<A,T> operator+(int off) const {
177  it+=off; //Yes, that's right.
178  return it;
179  }
181  pos+=off;
182  return *this;
183  }
184  inline AccessorIterator<A,T> operator-(int off) const {
186  it-=off; //Yes, that's right
187  return it;
188  }
189  inline int operator-(const ReverseAccessorIterator<A,T> &it) const {
190  return it.pos-pos;
191  }
192  inline T &operator[](int off) const {
193  return (*base)[pos-off];
194  }
195  inline bool operator==(const ReverseAccessorIterator<A,T> &it) const {
196  return (pos==it.pos)&&(&base==&it.base);
197  }
198  inline bool operator!=(const ReverseAccessorIterator<A,T> &it) const {
199  return !(operator==(it));
200  }
201  };
202  } //End of detail namespace
203 
204 
205  /** A vector-like wrapper for a Matrix for accessing the elements of a given row with a [] operator.
206  * For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
207  * \sa CMatrixColumnAccessor,CMatrixRowAccessorExtended,CConstMatrixRowAccessor,CConstMatrixRowAccessorExtended
208  */
209  template <typename MAT>
211  {
212  protected:
213  MAT *m_mat;
214  size_t m_rowInd;
215  public:
216  typedef typename MAT::value_type value_type;
218  //DECLARE_MRPT_CONTAINER_TYPES
219  //DECLARE_MRPT_CONTAINER_IS_VECTOR
220  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
221  inline CMatrixRowAccessor(MAT &mat, size_t rowIdx) : m_mat(&mat), m_rowInd(rowIdx) { ASSERT_(rowIdx<mat.getRowCount()) }
222  inline CMatrixRowAccessor() {}
223  inline value_type &operator[](const size_t i) { return (*m_mat)(m_rowInd,i); }
224  inline value_type operator[](const size_t i) const { return (*m_mat)(m_rowInd,i); }
229  inline iterator begin() {
230  return iterator(*this,0);
231  }
232  inline const_iterator begin() const {
233  return const_iterator(*this,0);
234  }
235  inline iterator end() {
236  return iterator(*this,m_mat->getColCount());
237  }
238  inline const_iterator end() const {
239  return const_iterator(*this,m_mat->getColCount());
240  }
241  inline reverse_iterator rbegin() {
242  return reverse_iterator(*this,m_mat->getColCount()-1);
243  }
244  inline const_reverse_iterator rbegin() const {
245  return const_reverse_iterator(*this,m_mat.getColCount()-1);
246  }
247  inline reverse_iterator rend() {
248  return reverse_iterator(*this,-1);
249  }
250  inline const_reverse_iterator rend() const {
251  return const_reverse_iterator(*this,-1);
252  }
253  inline size_t size() const {
254  return m_mat->getColCount();
255  }
256  inline void resize(size_t N) {
257  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
258  }
259  };
260  template<typename MAT> inline CMatrixRowAccessor<MAT> getRowAccessor(MAT &m,size_t rowIdx) {
261  return CMatrixRowAccessor<MAT>(m,rowIdx);
262  }
263 
264  /** A vector-like wrapper for a Matrix for accessing the elements of a given row with a [] operator, with offset and custom spacing.
265  * For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
266  * \sa CMatrixColumnAccessorExtended,CMatrixRowAccessor,CConstMatrixRowAccessor,CConstMatrixRowAccessorExtended
267  */
268  template<class MAT>
270  protected:
271  MAT *m_mat;
272  size_t m_rowInd;
273  size_t m_colOffset;
275  size_t howMany;
276  public:
277  typedef typename MAT::value_type value_type;
279  //DECLARE_MRPT_CONTAINER_TYPES
280  //DECLARE_MRPT_CONTAINER_IS_VECTOR
281  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
282  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) {
283  ASSERT_(row<mat.getRowCount());
284  howMany=(mat.getColCount()-m_colOffset)/m_elementsSpace;
285  }
287  inline value_type &operator[](size_t i) {
288  return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
289  }
290  inline value_type operator[](size_t i) const {
291  return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
292  }
297  inline iterator begin() {
298  return iterator(*this,0);
299  }
300  inline const_iterator begin() const {
301  return const_iterator(*this,0);
302  }
303  inline iterator end() {
304  return iterator(*this,howMany);
305  }
306  inline const_iterator end() const {
307  return const_iterator(*this,howMany);
308  }
310  return reverse_iterator(*this,howMany-1);
311  }
313  return const_reverse_iterator(*this,howMany-1);
314  }
316  return reverse_iterator(*this,-1);
317  }
318  inline const_reverse_iterator rend() const {
319  return const_reverse_iterator(*this,-1);
320  }
321  inline size_t size() const {
322  return howMany;
323  }
324  inline void resize(size_t N) {
325  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
326  }
327  };
328  template<typename MAT> inline CMatrixRowAccessorExtended<MAT> getRowAccessor(MAT &m,size_t rowIdx,size_t offset,size_t space=1) {
329  return CMatrixRowAccessor<MAT>(m,rowIdx,offset,space);
330  }
331 
332  /** A vector-like wrapper for a const Matrix for accessing the elements of a given row with a [] operator.
333  * For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
334  * \sa CConstMatrixColumnAccessor,CMatrixRowAccessorExtended,CMatrixRowAccessor,CConstMatrixRowAccessorExtended
335  */
336  template<class MAT>
338  protected:
339  const MAT *m_mat;
340  size_t m_rowInd;
341  public:
342  typedef typename MAT::value_type value_type;
344  //DECLARE_MRPT_CONTAINER_TYPES
345  //DECLARE_MRPT_CONTAINER_IS_VECTOR
346  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
347  inline CConstMatrixRowAccessor(const MAT &mat,size_t row):m_mat(&mat),m_rowInd(row) {
348  ASSERT_(row<mat.getRowCount());
349  }
351  inline value_type operator[](size_t i) const {
352  return (*m_mat)(m_rowInd,i);
353  }
356  inline const_iterator begin() const {
357  return const_iterator(*this,0);
358  }
359  inline const_iterator end() const {
360  return const_iterator(*this,m_mat->getColCount());
361  }
363  return const_reverse_iterator(*this,m_mat->getColCount()-1);
364  }
365  inline const_reverse_iterator rend() const {
366  return const_reverse_iterator(*this,-1);
367  }
368  inline size_t size() const {
369  return m_mat->getColCount();
370  }
371  inline void resize(size_t N) {
372  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
373  }
374  };
375  template<typename MAT> inline CConstMatrixRowAccessor<MAT> getRowAccessor(const MAT &m,size_t rowIdx) {
376  return CMatrixRowAccessor<MAT>(m,rowIdx);
377  }
378 
379  /** A vector-like wrapper for a const Matrix for accessing the elements of a given row with a [] operator, with offset and custom spacing.
380  * For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
381  * \sa CConstMatrixColumnAccessorExtended,CMatrixRowAccessor,CConstMatrixRowAccessor,CMatrixRowAccessorExtended
382  */
383  template<class MAT>
385  protected:
386  const MAT *m_mat;
387  size_t m_rowInd;
388  size_t m_colOffset;
390  size_t howMany;
391  public:
392  typedef typename MAT::value_type value_type;
394  //DECLARE_MRPT_CONTAINER_TYPES
395  //DECLARE_MRPT_CONTAINER_IS_VECTOR
396  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
397  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) {
398  ASSERT_(row<mat.getRowCount());
399  howMany=(mat.getColCount()-m_colOffset)/m_elementsSpace;
400  }
402  inline value_type operator[](size_t i) const {
403  return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
404  }
407  inline const_iterator begin() const {
408  return const_iterator(*this,0);
409  }
410  inline const_iterator end() const {
411  return const_iterator(*this,howMany);
412  }
414  return const_reverse_iterator(*this,howMany-1);
415  }
416  inline const_reverse_iterator rend() const {
417  return const_reverse_iterator(*this,-1);
418  }
419  inline size_t size() const {
420  return howMany;
421  }
422  inline void resize(size_t N) {
423  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
424  }
425  };
426  template<typename MAT> inline CConstMatrixRowAccessorExtended<MAT> getRowAccessor(const MAT &m,size_t rowIdx,size_t offset,size_t space=1) {
427  return CConstMatrixRowAccessorExtended<MAT>(m,rowIdx,offset,space);
428  }
429 
430 
431  /** A vector-like wrapper for a Matrix for accessing the elements of a given column with a [] operator.
432  * \sa CMatrixRowAccessor,CMatrixColumnAccessorExtended,CConstMatrixColumnAccessor,CConstMatrixColumnAccessorExtended
433  */
434  template <typename MAT> class CMatrixColumnAccessor {
435  protected:
436  MAT *m_mat;
437  size_t m_colInd;
438  public:
439  typedef typename MAT::value_type value_type;
441  //DECLARE_MRPT_CONTAINER_TYPES
442  //DECLARE_MRPT_CONTAINER_IS_VECTOR
443  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
444  inline CMatrixColumnAccessor(MAT &mat, size_t colIdx) : m_mat(&mat), m_colInd(colIdx) { ASSERT_(colIdx<mat.getColCount()) }
446  inline value_type &operator[](const size_t i) { return (*m_mat)(i,m_colInd); }
447  inline value_type operator[](const size_t i) const { return (*m_mat)(i,m_colInd); }
452  inline iterator begin() {
453  return iterator(*this,0);
454  }
455  inline const_iterator begin() const {
456  return const_iterator(*this,0);
457  }
458  inline iterator end() {
459  return iterator(*this,m_mat->getRowCount());
460  }
461  inline const_iterator end() const {
462  return const_iterator(*this,m_mat->getRowCount());
463  }
464  inline reverse_iterator rbegin() {
465  return reverse_iterator(*this,m_mat->getRowCount()-1);
466  }
467  inline const_reverse_iterator rbegin() const {
468  return const_reverse_iterator(*this,m_mat->getRowCount()-1);
469  }
470  inline reverse_iterator rend() {
471  return reverse_iterator(*this,-1);
472  }
473  inline const_reverse_iterator rend() const {
474  return const_reverse_iterator(*this,-1);
475  }
476  inline size_t size() const {
477  return m_mat->getRowCount();
478  }
479  inline void resize(size_t N) {
480  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
481  }
482  };
483  template<typename MAT> inline CMatrixColumnAccessor<MAT> getColumnAccessor(MAT &m,size_t colIdx) {
484  return CMatrixColumnAccessor<MAT>(m,colIdx);
485  }
486 
487  /** A vector-like wrapper for a Matrix for accessing the elements of a given column with a [] operator, with offset and custom spacing.
488  * \sa CMatrixRowAccessorExtended,CMatrixColumnAccessor,CConstMatrixColumnAccessor,CConstMatrixColumnAccessorExtended
489  */
490  template<typename MAT>
492  protected:
493  MAT *m_mat;
494  size_t m_colInd;
495  size_t m_rowOffset;
497  size_t howMany;
498  public:
499  typedef typename MAT::value_type value_type;
501  //DECLARE_MRPT_CONTAINER_TYPES
502  //DECLARE_MRPT_CONTAINER_IS_VECTOR
503  //DECLARE_COMMON_CONTAINERS_MEMBERS(mrpt_autotype)
504  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) {
505  ASSERT_(col<mat.getColCount());
506  howMany=(mat.getRowCount()-m_rowOffset)/m_elementsSpace;
507  }
509  inline value_type &operator[](size_t i) {
510  return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
511  }
512  inline value_type operator[](size_t i) const {
513  return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
514  }
519  inline iterator begin() {
520  return iterator(*this,0);
521  }
522  inline const_iterator begin() const {
523  return const_iterator(*this,0);
524  }
525  inline iterator end() {
526  return iterator(*this,howMany);
527  }
528  inline const_iterator end() const {
529  return const_iterator(*this,howMany);
530  }
532  return reverse_iterator(*this,howMany-1);
533  }
535  return const_reverse_iterator(*this,howMany-1);
536  }
538  return reverse_iterator(*this,-1);
539  }
540  inline const_reverse_iterator rend() const {
541  return const_reverse_iterator(*this,-1);
542  }
543  inline size_t size() const {
544  return howMany();
545  }
546  inline void resize(size_t N) {
547  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
548  }
549  };
550  template<typename MAT> inline CMatrixColumnAccessorExtended<MAT> getColumnAccessor(MAT &m,size_t colIdx,size_t offset,size_t space=1) {
551  return CMatrixColumnAccessorExtended<MAT>(m,colIdx,offset,space);
552  }
553 
554  /** A vector-like wrapper for a const Matrix for accessing the elements of a given column with a [] operator.
555  * \sa CConstMatrixRowAccessor,CMatrixColumnAccessorExtended,CMatrixColumnAccessor,CConstMatrixColumnAccessorExtended
556  */
557  template<class MAT>
559  protected:
560  const MAT *m_mat;
561  size_t m_colInd;
562  public:
563  typedef typename MAT::value_type value_type;
565  //DECLARE_MRPT_CONTAINER_TYPES
566  //DECLARE_MRPT_CONTAINER_IS_VECTOR
567  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
568  inline CConstMatrixColumnAccessor(const MAT &mat,size_t colIdx):m_mat(&mat),m_colInd(colIdx) {
569  ASSERT_(colIdx<mat.getColCount());
570  }
572  inline value_type operator[](size_t i) const {
573  return (*m_mat)(i,m_colInd);
574  }
577  inline const_iterator begin() const {
578  return const_iterator(*this,0);
579  }
580  inline const_iterator end() const {
581  return const_iterator(*this,m_mat->getRowCount());
582  }
584  return const_reverse_iterator(*this,m_mat->getRowCount()-1);
585  }
586  inline const_reverse_iterator rend() const {
587  return const_reverse_iterator(*this,-1);
588  }
589  inline size_t size() const {
590  return m_mat->getRowCount();
591  }
592  inline void resize(size_t N) {
593  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
594  }
595  };
596  template<typename MAT> inline CConstMatrixColumnAccessor<MAT> getColumnAccessor(const MAT &m,size_t colIdx) {
597  return CConstMatrixColumnAccessor<MAT>(m,colIdx);
598  }
599 
600  /** A vector-like wrapper for a const Matrix for accessing the elements of a given column with a [] operator, with offset and custom spacing.
601  * \sa CConstMatrixRowAccessorExtended,CMatrixColumnAccessor,CConstMatrixColumnAccessor,CMatrixColumnAccessorExtended
602  */
603  template<typename MAT>
605  protected:
606  const MAT *m_mat;
607  size_t m_colInd;
608  size_t m_rowOffset;
610  size_t howMany;
611  public:
612  typedef typename MAT::value_type value_type;
614  //DECLARE_MRPT_CONTAINER_TYPES
615  //DECLARE_MRPT_CONTAINER_IS_VECTOR
616  //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
617  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) {
618  ASSERT_(col<mat.getColCount());
619  howMany=(mat.getRowCount()-m_rowOffset)/m_elementsSpace;
620  }
622  inline value_type operator[](size_t i) const {
623  return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
624  }
627  inline const_iterator begin() const {
628  return const_iterator(*this,0);
629  }
630  inline const_iterator end() const {
631  return const_iterator(*this,howMany);
632  }
634  return const_reverse_iterator(*this,howMany-1);
635  }
636  inline const_reverse_iterator rend() const {
637  return const_reverse_iterator(*this,-1);
638  }
639  inline size_t size() const {
640  return howMany;
641  }
642  inline void resize(size_t N) {
643  if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
644  }
645  };
646  template<typename MAT> inline CConstMatrixColumnAccessorExtended<MAT> getColumnAccessor(const MAT &m,size_t colIdx,size_t offset,size_t space=1) {
647  return CConstMatrixColumnAccessorExtended<MAT>(m,colIdx,offset,space);
648  }
649 
650 
651  } // End of namespace
652 } // End of namespace
653 
654 
655 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013