Main MRPT website > C++ reference
MRPT logo

matrix_adaptors.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef mrpt_matrix_adaptors_H
00029 #define mrpt_matrix_adaptors_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/math/math_frwds.h>  // Fordward declarations
00033 
00034 namespace mrpt
00035 {
00036         namespace math
00037         {
00038 
00039         /** Internal classes not to be directly used by the user. */
00040         // Forward declarations:
00041         template<typename T,typename U,bool UIsObject> class CBinaryRelation;
00042         namespace detail
00043         {
00044                 /**
00045                   * 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
00046                   * types, using a unsigned char instead of a bool.
00047                   */
00048                 template<typename U,bool B> class MatrixWrapper;
00049 
00050                 // partial specializations:
00051                 template<typename U> class MatrixWrapper<U,true>        {
00052                 public:
00053                         typedef CMatrixTemplateObjects<U> MatrixType;
00054                 };
00055                 template<typename U> class MatrixWrapper<U,false>       {
00056                 public:
00057                         typedef CMatrixTemplate<U> MatrixType;
00058                 };
00059 
00060                 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);
00061         }
00062 
00063 
00064                 namespace detail        {
00065                         /** Template class for matrix accessor's iterators.
00066                           * \sa CMatrixRowAccessor,CMatrixColumnAccessor
00067                           */
00068                         template<typename A,typename T> class AccessorIterator  {
00069                         protected:
00070                                 A *base;
00071                                 int pos;
00072                         public:
00073                                 //typedefs for iterator_traits:
00074                                 typedef std::random_access_iterator_tag iterator_category;
00075                                 typedef T value_type;
00076                                 typedef int difference_type;
00077                                 typedef T *pointer;
00078                                 typedef T &reference;
00079 
00080                                 inline AccessorIterator(A &obj,size_t N):base(&obj),pos(N)      {}
00081                                 inline T &operator*() const     {
00082                                         return (*base)[pos];
00083                                 }
00084                                 inline AccessorIterator<A,T> &operator++()      {
00085                                         ++pos;
00086                                         return *this;
00087                                 }
00088                                 inline AccessorIterator<A,T> operator++(int)    {
00089                                         AccessorIterator<A,T> it=*this;
00090                                         ++*this;
00091                                         return it;
00092                                 }
00093                                 inline AccessorIterator<A,T> &operator--()      {
00094                                         --pos;
00095                                         return *this;
00096                                 }
00097                                 inline AccessorIterator<A,T> operator--(int)    {
00098                                         AccessorIterator<A,T> it=*this;
00099                                         --*this;
00100                                         return it;
00101                                 }
00102                                 inline AccessorIterator<A,T> &operator+=(int off)       {
00103                                         pos+=off;
00104                                         return *this;
00105                                 }
00106                                 inline AccessorIterator<A,T> operator+(int off) const   {
00107                                         AccessorIterator<A,T> it=*this;
00108                                         it+=off;
00109                                         return it;
00110                                 }
00111                                 inline AccessorIterator<A,T> &operator-=(int off)       {
00112                                         pos-=off;
00113                                         return *this;
00114                                 }
00115                                 inline AccessorIterator<A,T> operator-(int off) const   {
00116                                         AccessorIterator<A,T> it=*this;
00117                                         it-=off;
00118                                         return it;
00119                                 }
00120                                 inline int operator-(const AccessorIterator<A,T> &it) const     {
00121                                         return pos-it.pos;
00122                                 }
00123                                 inline T &operator[](int off) const     {
00124                                         return (*base)[pos+off];
00125                                 }
00126                                 inline bool operator==(const AccessorIterator<A,T> &it) const   {
00127                                         return (pos==it.pos)&&(base==it.base);
00128                                 }
00129                                 inline bool operator!=(const AccessorIterator<A,T> &it) const   {
00130                                         return !(operator==(it));
00131                                 }
00132                         };
00133 
00134                         /** Template class for matrix accessor's iterators.
00135                           * \sa CMatrixRowAccessor,CMatrixColumnAccessor
00136                           */
00137                         template<typename A,typename T> class ReverseAccessorIterator   {
00138                         protected:
00139                                 A *base;
00140                                 int pos;
00141                         public:
00142                                 //typedefs for iterator_traits:
00143                                 typedef std::random_access_iterator_tag iterator_category;
00144                                 typedef T value_type;
00145                                 typedef int difference_type;
00146                                 typedef T *pointer;
00147                                 typedef T &reference;
00148 
00149                                 inline ReverseAccessorIterator(A &obj,size_t N):base(&obj),pos(N)       {}
00150                                 inline T &operator*() const     {
00151                                         return (*base)[pos];
00152                                 }
00153                                 inline ReverseAccessorIterator<A,T> &operator++()       {
00154                                         --pos;
00155                                         return *this;
00156                                 }
00157                                 inline ReverseAccessorIterator<A,T> operator++(int)     {
00158                                         ReverseAccessorIterator<A,T> it=*this;
00159                                         ++*this;        //Yes, that's right.
00160                                         return it;
00161                                 }
00162                                 inline ReverseAccessorIterator<A,T> &operator--()       {
00163                                         ++pos;
00164                                         return *this;
00165                                 }
00166                                 inline ReverseAccessorIterator<A,T> operator--(int)     {
00167                                         ReverseAccessorIterator<A,T> it=*this;
00168                                         --*this;        //Yes, that's right.
00169                                         return it;
00170                                 }
00171                                 inline ReverseAccessorIterator<A,T> &operator+=(int off)        {
00172                                         pos-=off;
00173                                         return *this;
00174                                 }
00175                                 inline ReverseAccessorIterator<A,T> operator+(int off) const    {
00176                                         ReverseAccessorIterator<A,T> it=*this;
00177                                         it+=off;        //Yes, that's right.
00178                                         return it;
00179                                 }
00180                                 inline AccessorIterator<A,T> &operator-=(int off)       {
00181                                         pos+=off;
00182                                         return *this;
00183                                 }
00184                                 inline AccessorIterator<A,T> operator-(int off) const   {
00185                                         ReverseAccessorIterator<A,T> it=*this;
00186                                         it-=off;        //Yes, that's right
00187                                         return it;
00188                                 }
00189                                 inline int operator-(const ReverseAccessorIterator<A,T> &it) const      {
00190                                         return it.pos-pos;
00191                                 }
00192                                 inline T &operator[](int off) const     {
00193                                         return (*base)[pos-off];
00194                                 }
00195                                 inline bool operator==(const ReverseAccessorIterator<A,T> &it) const    {
00196                                         return (pos==it.pos)&&(&base==&it.base);
00197                                 }
00198                                 inline bool operator!=(const ReverseAccessorIterator<A,T> &it) const    {
00199                                         return !(operator==(it));
00200                                 }
00201                         };
00202                 }       //End of detail namespace
00203 
00204 
00205                 /** A vector-like wrapper for a Matrix for accessing the elements of a given row with a [] operator.
00206                   *  For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
00207                   * \sa CMatrixColumnAccessor,CMatrixRowAccessorExtended,CConstMatrixRowAccessor,CConstMatrixRowAccessorExtended
00208                   */
00209                 template <typename MAT>
00210                 class CMatrixRowAccessor
00211                 {
00212                 protected:
00213                         MAT *m_mat;
00214                         size_t  m_rowInd;
00215                 public:
00216                         typedef typename MAT::value_type value_type;
00217                         typedef CMatrixRowAccessor<MAT> mrpt_autotype;
00218                         //DECLARE_MRPT_CONTAINER_TYPES
00219                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00220                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00221                         inline CMatrixRowAccessor(MAT &mat, size_t rowIdx) : m_mat(&mat), m_rowInd(rowIdx) { ASSERT_(rowIdx<mat.getRowCount()) }
00222                         inline CMatrixRowAccessor()     {}
00223                         inline value_type &operator[](const size_t i) { return (*m_mat)(m_rowInd,i); }
00224                         inline value_type operator[](const size_t i) const { return (*m_mat)(m_rowInd,i); }
00225                         typedef detail::AccessorIterator<CMatrixRowAccessor<MAT>,value_type> iterator;
00226                         typedef detail::AccessorIterator<const CMatrixRowAccessor<MAT>,const value_type> const_iterator;
00227                         typedef detail::ReverseAccessorIterator<CMatrixRowAccessor<MAT>,value_type> reverse_iterator;
00228                         typedef detail::ReverseAccessorIterator<const CMatrixRowAccessor<MAT>,const value_type> const_reverse_iterator;
00229                         inline iterator begin() {
00230                                 return iterator(*this,0);
00231                         }
00232                         inline const_iterator begin() const     {
00233                                 return const_iterator(*this,0);
00234                         }
00235                         inline iterator end()   {
00236                                 return iterator(*this,m_mat->getColCount());
00237                         }
00238                         inline const_iterator end() const       {
00239                                 return const_iterator(*this,m_mat->getColCount());
00240                         }
00241                         inline reverse_iterator rbegin()        {
00242                                 return reverse_iterator(*this,m_mat->getColCount()-1);
00243                         }
00244                         inline const_reverse_iterator rbegin() const    {
00245                                 return const_reverse_iterator(*this,m_mat.getColCount()-1);
00246                         }
00247                         inline reverse_iterator rend()  {
00248                                 return reverse_iterator(*this,-1);
00249                         }
00250                         inline const_reverse_iterator rend() const      {
00251                                 return const_reverse_iterator(*this,-1);
00252                         }
00253                         inline size_t size() const      {
00254                                 return m_mat->getColCount();
00255                         }
00256                         inline void resize(size_t N)    {
00257                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00258                         }
00259                 };
00260                 template<typename MAT> inline CMatrixRowAccessor<MAT> getRowAccessor(MAT &m,size_t rowIdx)      {
00261                         return CMatrixRowAccessor<MAT>(m,rowIdx);
00262                 }
00263 
00264                 /** A vector-like wrapper for a Matrix for accessing the elements of a given row with a [] operator, with offset and custom spacing.
00265                   *  For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
00266                   * \sa CMatrixColumnAccessorExtended,CMatrixRowAccessor,CConstMatrixRowAccessor,CConstMatrixRowAccessorExtended
00267                   */
00268                 template<class MAT>
00269                 class CMatrixRowAccessorExtended        {
00270                 protected:
00271                         MAT *m_mat;
00272                         size_t m_rowInd;
00273                         size_t m_colOffset;
00274                         size_t m_elementsSpace;
00275                         size_t howMany;
00276                 public:
00277                         typedef typename MAT::value_type value_type;
00278                         typedef CMatrixRowAccessorExtended<MAT> mrpt_autotype;
00279                         //DECLARE_MRPT_CONTAINER_TYPES
00280                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00281                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00282                         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)  {
00283                                 ASSERT_(row<mat.getRowCount());
00284                                 howMany=(mat.getColCount()-m_colOffset)/m_elementsSpace;
00285                         }
00286                         inline CMatrixRowAccessorExtended()     {}
00287                         inline value_type &operator[](size_t i) {
00288                                 return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
00289                         }
00290                         inline value_type operator[](size_t i) const {
00291                                 return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
00292                         }
00293                         typedef detail::AccessorIterator<CMatrixRowAccessorExtended<MAT>,value_type> iterator;
00294                         typedef detail::AccessorIterator<const CMatrixRowAccessorExtended<MAT>,const value_type> const_iterator;
00295                         typedef detail::ReverseAccessorIterator<CMatrixRowAccessorExtended<MAT>,value_type> reverse_iterator;
00296                         typedef detail::ReverseAccessorIterator<const CMatrixRowAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00297                         inline iterator begin() {
00298                                 return iterator(*this,0);
00299                         }
00300                         inline const_iterator begin() const     {
00301                                 return const_iterator(*this,0);
00302                         }
00303                         inline iterator end()   {
00304                                 return iterator(*this,howMany);
00305                         }
00306                         inline const_iterator end() const       {
00307                                 return const_iterator(*this,howMany);
00308                         }
00309                         inline reverse_iterator rbegin()        {
00310                                 return reverse_iterator(*this,howMany-1);
00311                         }
00312                         inline const_reverse_iterator rbegin() const    {
00313                                 return const_reverse_iterator(*this,howMany-1);
00314                         }
00315                         inline reverse_iterator rend()  {
00316                                 return reverse_iterator(*this,-1);
00317                         }
00318                         inline const_reverse_iterator rend() const      {
00319                                 return const_reverse_iterator(*this,-1);
00320                         }
00321                         inline size_t size() const      {
00322                                 return howMany;
00323                         }
00324                         inline void resize(size_t N)    {
00325                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00326                         }
00327                 };
00328                 template<typename MAT> inline CMatrixRowAccessorExtended<MAT> getRowAccessor(MAT &m,size_t rowIdx,size_t offset,size_t space=1) {
00329                         return CMatrixRowAccessor<MAT>(m,rowIdx,offset,space);
00330                 }
00331 
00332                 /** A vector-like wrapper for a const Matrix for accessing the elements of a given row with a [] operator.
00333                   *  For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
00334                   * \sa CConstMatrixColumnAccessor,CMatrixRowAccessorExtended,CMatrixRowAccessor,CConstMatrixRowAccessorExtended
00335                   */
00336                 template<class MAT>
00337                 class CConstMatrixRowAccessor   {
00338                 protected:
00339                         const MAT *m_mat;
00340                         size_t m_rowInd;
00341                 public:
00342                         typedef typename MAT::value_type value_type;
00343                         typedef CConstMatrixRowAccessor<MAT> mrpt_autotype;
00344                         //DECLARE_MRPT_CONTAINER_TYPES
00345                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00346                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00347                         inline CConstMatrixRowAccessor(const MAT &mat,size_t row):m_mat(&mat),m_rowInd(row)     {
00348                                 ASSERT_(row<mat.getRowCount());
00349                         }
00350                         inline CConstMatrixRowAccessor()        {}
00351                         inline value_type operator[](size_t i) const    {
00352                                 return (*m_mat)(m_rowInd,i);
00353                         }
00354                         typedef detail::AccessorIterator<const CConstMatrixRowAccessor<MAT>,const value_type> const_iterator;
00355                         typedef detail::ReverseAccessorIterator<const CConstMatrixRowAccessor<MAT>,const value_type> const_reverse_iterator;
00356                         inline const_iterator begin() const     {
00357                                 return const_iterator(*this,0);
00358                         }
00359                         inline const_iterator end() const       {
00360                                 return const_iterator(*this,m_mat->getColCount());
00361                         }
00362                         inline const_reverse_iterator rbegin() const    {
00363                                 return const_reverse_iterator(*this,m_mat->getColCount()-1);
00364                         }
00365                         inline const_reverse_iterator rend() const      {
00366                                 return const_reverse_iterator(*this,-1);
00367                         }
00368                         inline size_t size() const      {
00369                                 return m_mat->getColCount();
00370                         }
00371                         inline void resize(size_t N)    {
00372                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00373                         }
00374                 };
00375                 template<typename MAT> inline CConstMatrixRowAccessor<MAT> getRowAccessor(const MAT &m,size_t rowIdx)   {
00376                         return CMatrixRowAccessor<MAT>(m,rowIdx);
00377                 }
00378 
00379                 /** A vector-like wrapper for a const Matrix for accessing the elements of a given row with a [] operator, with offset and custom spacing.
00380                   *  For usage with MRPT's CMatrixTemplate only (for MRPT numeric matrices, use Eigen methods)
00381                   * \sa CConstMatrixColumnAccessorExtended,CMatrixRowAccessor,CConstMatrixRowAccessor,CMatrixRowAccessorExtended
00382                   */
00383                 template<class MAT>
00384                 class CConstMatrixRowAccessorExtended   {
00385                 protected:
00386                         const MAT *m_mat;
00387                         size_t m_rowInd;
00388                         size_t m_colOffset;
00389                         size_t m_elementsSpace;
00390                         size_t howMany;
00391                 public:
00392                         typedef typename MAT::value_type value_type;
00393                         typedef CConstMatrixRowAccessorExtended<MAT> mrpt_autotype;
00394                         //DECLARE_MRPT_CONTAINER_TYPES
00395                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00396                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00397                         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)       {
00398                                 ASSERT_(row<mat.getRowCount());
00399                                 howMany=(mat.getColCount()-m_colOffset)/m_elementsSpace;
00400                         }
00401                         inline CConstMatrixRowAccessorExtended()        {}
00402                         inline value_type operator[](size_t i) const    {
00403                                 return (*m_mat)(m_rowInd,m_colOffset+(i*m_elementsSpace));
00404                         }
00405                         typedef detail::AccessorIterator<const CConstMatrixRowAccessorExtended<MAT>,const value_type> const_iterator;
00406                         typedef detail::ReverseAccessorIterator<const CConstMatrixRowAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00407                         inline const_iterator begin() const     {
00408                                 return const_iterator(*this,0);
00409                         }
00410                         inline const_iterator end() const       {
00411                                 return const_iterator(*this,howMany);
00412                         }
00413                         inline const_reverse_iterator rbegin() const    {
00414                                 return const_reverse_iterator(*this,howMany-1);
00415                         }
00416                         inline const_reverse_iterator rend() const      {
00417                                 return const_reverse_iterator(*this,-1);
00418                         }
00419                         inline size_t size() const      {
00420                                 return howMany;
00421                         }
00422                         inline void resize(size_t N)    {
00423                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00424                         }
00425                 };
00426                 template<typename MAT> inline CConstMatrixRowAccessorExtended<MAT> getRowAccessor(const MAT &m,size_t rowIdx,size_t offset,size_t space=1)      {
00427                         return CConstMatrixRowAccessorExtended<MAT>(m,rowIdx,offset,space);
00428                 }
00429 
00430 
00431                 /** A vector-like wrapper for a Matrix for accessing the elements of a given column with a [] operator.
00432                   * \sa CMatrixRowAccessor,CMatrixColumnAccessorExtended,CConstMatrixColumnAccessor,CConstMatrixColumnAccessorExtended
00433                   */
00434                 template <typename MAT> class CMatrixColumnAccessor     {
00435                 protected:
00436                         MAT *m_mat;
00437                         size_t  m_colInd;
00438                 public:
00439                         typedef typename MAT::value_type value_type;
00440                         typedef CMatrixColumnAccessor<MAT> mrpt_autotype;
00441                         //DECLARE_MRPT_CONTAINER_TYPES
00442                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00443                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00444                         inline CMatrixColumnAccessor(MAT &mat, size_t colIdx) : m_mat(&mat), m_colInd(colIdx) { ASSERT_(colIdx<mat.getColCount()) }
00445                         inline CMatrixColumnAccessor()  {}
00446                         inline value_type &operator[](const size_t i) { return (*m_mat)(i,m_colInd); }
00447                         inline value_type operator[](const size_t i) const { return (*m_mat)(i,m_colInd); }
00448                         typedef detail::AccessorIterator<CMatrixColumnAccessor<MAT>,value_type> iterator;
00449                         typedef detail::AccessorIterator<const CMatrixColumnAccessor<MAT>,const value_type> const_iterator;
00450                         typedef detail::ReverseAccessorIterator<CMatrixColumnAccessor<MAT>,value_type> reverse_iterator;
00451                         typedef detail::ReverseAccessorIterator<const CMatrixColumnAccessor<MAT>,const value_type> const_reverse_iterator;
00452                         inline iterator begin() {
00453                                 return iterator(*this,0);
00454                         }
00455                         inline const_iterator begin() const     {
00456                                 return const_iterator(*this,0);
00457                         }
00458                         inline iterator end()   {
00459                                 return iterator(*this,m_mat->getRowCount());
00460                         }
00461                         inline const_iterator end() const       {
00462                                 return const_iterator(*this,m_mat->getRowCount());
00463                         }
00464                         inline reverse_iterator rbegin()        {
00465                                 return reverse_iterator(*this,m_mat->getRowCount()-1);
00466                         }
00467                         inline const_reverse_iterator rbegin() const    {
00468                                 return const_reverse_iterator(*this,m_mat->getRowCount()-1);
00469                         }
00470                         inline reverse_iterator rend()  {
00471                                 return reverse_iterator(*this,-1);
00472                         }
00473                         inline const_reverse_iterator rend() const      {
00474                                 return const_reverse_iterator(*this,-1);
00475                         }
00476                         inline size_t size() const      {
00477                                 return m_mat->getRowCount();
00478                         }
00479                         inline void resize(size_t N)    {
00480                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00481                         }
00482                 };
00483                 template<typename MAT> inline CMatrixColumnAccessor<MAT> getColumnAccessor(MAT &m,size_t colIdx)        {
00484                         return CMatrixColumnAccessor<MAT>(m,colIdx);
00485                 }
00486 
00487                 /** A vector-like wrapper for a Matrix for accessing the elements of a given column with a [] operator, with offset and custom spacing.
00488                   * \sa CMatrixRowAccessorExtended,CMatrixColumnAccessor,CConstMatrixColumnAccessor,CConstMatrixColumnAccessorExtended
00489                   */
00490                 template<typename MAT>
00491                 class CMatrixColumnAccessorExtended     {
00492                 protected:
00493                         MAT *m_mat;
00494                         size_t m_colInd;
00495                         size_t m_rowOffset;
00496                         size_t m_elementsSpace;
00497                         size_t howMany;
00498                 public:
00499                         typedef typename MAT::value_type value_type;
00500                         typedef CMatrixColumnAccessorExtended<MAT> mrpt_autotype;
00501                         //DECLARE_MRPT_CONTAINER_TYPES
00502                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00503                         //DECLARE_COMMON_CONTAINERS_MEMBERS(mrpt_autotype)
00504                         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)       {
00505                                 ASSERT_(col<mat.getColCount());
00506                                 howMany=(mat.getRowCount()-m_rowOffset)/m_elementsSpace;
00507                         }
00508                         inline CMatrixColumnAccessorExtended()  {}
00509                         inline value_type &operator[](size_t i) {
00510                                 return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
00511                         }
00512                         inline value_type operator[](size_t i) const    {
00513                                 return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
00514                         }
00515                         typedef detail::AccessorIterator<CMatrixColumnAccessorExtended<MAT>,value_type> iterator;
00516                         typedef detail::AccessorIterator<const CMatrixColumnAccessorExtended<MAT>,const value_type> const_iterator;
00517                         typedef detail::ReverseAccessorIterator<CMatrixColumnAccessorExtended<MAT>,value_type> reverse_iterator;
00518                         typedef detail::ReverseAccessorIterator<const CMatrixColumnAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00519                         inline iterator begin() {
00520                                 return iterator(*this,0);
00521                         }
00522                         inline const_iterator begin() const     {
00523                                 return const_iterator(*this,0);
00524                         }
00525                         inline iterator end()   {
00526                                 return iterator(*this,howMany);
00527                         }
00528                         inline const_iterator end() const       {
00529                                 return const_iterator(*this,howMany);
00530                         }
00531                         inline reverse_iterator rbegin()        {
00532                                 return reverse_iterator(*this,howMany-1);
00533                         }
00534                         inline const_reverse_iterator rbegin() const    {
00535                                 return const_reverse_iterator(*this,howMany-1);
00536                         }
00537                         inline reverse_iterator rend()  {
00538                                 return reverse_iterator(*this,-1);
00539                         }
00540                         inline const_reverse_iterator rend() const      {
00541                                 return const_reverse_iterator(*this,-1);
00542                         }
00543                         inline size_t size() const      {
00544                                 return howMany();
00545                         }
00546                         inline void resize(size_t N)    {
00547                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00548                         }
00549                 };
00550                 template<typename MAT> inline CMatrixColumnAccessorExtended<MAT> getColumnAccessor(MAT &m,size_t colIdx,size_t offset,size_t space=1)   {
00551                         return CMatrixColumnAccessorExtended<MAT>(m,colIdx,offset,space);
00552                 }
00553 
00554                 /** A vector-like wrapper for a const Matrix for accessing the elements of a given column with a [] operator.
00555                   * \sa CConstMatrixRowAccessor,CMatrixColumnAccessorExtended,CMatrixColumnAccessor,CConstMatrixColumnAccessorExtended
00556                   */
00557                 template<class MAT>
00558                 class CConstMatrixColumnAccessor        {
00559                 protected:
00560                         const MAT *m_mat;
00561                         size_t m_colInd;
00562                 public:
00563                         typedef typename MAT::value_type value_type;
00564                         typedef CConstMatrixColumnAccessor<MAT> mrpt_autotype;
00565                         //DECLARE_MRPT_CONTAINER_TYPES
00566                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00567                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00568                         inline CConstMatrixColumnAccessor(const MAT &mat,size_t colIdx):m_mat(&mat),m_colInd(colIdx)    {
00569                                 ASSERT_(colIdx<mat.getColCount());
00570                         }
00571                         inline CConstMatrixColumnAccessor()     {}
00572                         inline value_type operator[](size_t i) const    {
00573                                 return (*m_mat)(i,m_colInd);
00574                         }
00575                         typedef detail::AccessorIterator<const CConstMatrixColumnAccessor<MAT>,const value_type> const_iterator;
00576                         typedef detail::ReverseAccessorIterator<const CConstMatrixColumnAccessor<MAT>,const value_type> const_reverse_iterator;
00577                         inline const_iterator begin() const     {
00578                                 return const_iterator(*this,0);
00579                         }
00580                         inline const_iterator end() const       {
00581                                 return const_iterator(*this,m_mat->getRowCount());
00582                         }
00583                         inline const_reverse_iterator rbegin() const    {
00584                                 return const_reverse_iterator(*this,m_mat->getRowCount()-1);
00585                         }
00586                         inline const_reverse_iterator rend() const      {
00587                                 return const_reverse_iterator(*this,-1);
00588                         }
00589                         inline size_t size() const      {
00590                                 return m_mat->getRowCount();
00591                         }
00592                         inline void resize(size_t N)    {
00593                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00594                         }
00595                 };
00596                 template<typename MAT> inline CConstMatrixColumnAccessor<MAT> getColumnAccessor(const MAT &m,size_t colIdx)     {
00597                         return CConstMatrixColumnAccessor<MAT>(m,colIdx);
00598                 }
00599 
00600                 /** A vector-like wrapper for a const Matrix for accessing the elements of a given column with a [] operator, with offset and custom spacing.
00601                   * \sa CConstMatrixRowAccessorExtended,CMatrixColumnAccessor,CConstMatrixColumnAccessor,CMatrixColumnAccessorExtended
00602                   */
00603                 template<typename MAT>
00604                 class CConstMatrixColumnAccessorExtended        {
00605                 protected:
00606                         const MAT *m_mat;
00607                         size_t m_colInd;
00608                         size_t m_rowOffset;
00609                         size_t m_elementsSpace;
00610                         size_t howMany;
00611                 public:
00612                         typedef typename MAT::value_type value_type;
00613                         typedef CMatrixColumnAccessorExtended<MAT> mrpt_autotype;
00614                         //DECLARE_MRPT_CONTAINER_TYPES
00615                         //DECLARE_MRPT_CONTAINER_IS_VECTOR
00616                         //DECLARE_COMMON_CONTAINERS_MEMBERS(value_type)
00617                         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)    {
00618                                 ASSERT_(col<mat.getColCount());
00619                                 howMany=(mat.getRowCount()-m_rowOffset)/m_elementsSpace;
00620                         }
00621                         inline CConstMatrixColumnAccessorExtended()     {}
00622                         inline value_type operator[](size_t i) const    {
00623                                 return (*m_mat)(m_rowOffset+(i*m_elementsSpace),m_colInd);
00624                         }
00625                         typedef detail::AccessorIterator<const CConstMatrixColumnAccessorExtended<MAT>,const value_type> const_iterator;
00626                         typedef detail::ReverseAccessorIterator<const CConstMatrixColumnAccessorExtended<MAT>,const value_type> const_reverse_iterator;
00627                         inline const_iterator begin() const     {
00628                                 return const_iterator(*this,0);
00629                         }
00630                         inline const_iterator end() const       {
00631                                 return const_iterator(*this,howMany);
00632                         }
00633                         inline const_reverse_iterator rbegin() const    {
00634                                 return const_reverse_iterator(*this,howMany-1);
00635                         }
00636                         inline const_reverse_iterator rend() const      {
00637                                 return const_reverse_iterator(*this,-1);
00638                         }
00639                         inline size_t size() const      {
00640                                 return howMany;
00641                         }
00642                         inline void resize(size_t N)    {
00643                                 if (N!=size()) throw std::logic_error("Tried to resize a fixed-size vector");
00644                         }
00645                 };
00646                 template<typename MAT> inline CConstMatrixColumnAccessorExtended<MAT> getColumnAccessor(const MAT &m,size_t colIdx,size_t offset,size_t space=1)        {
00647                         return CConstMatrixColumnAccessorExtended<MAT>(m,colIdx,offset,space);
00648                 }
00649 
00650 
00651         } // End of namespace
00652 } // End of namespace
00653 
00654 
00655 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011