This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Return a reference to a column of a dense matrix

Tags: None
(comma "," separated)
davidje13
Registered Member
Posts
13
Karma
0
OS
I'm using a dense matrix as a vertex list, so I have something like:

Code: Select all
template< typename T > class foo {
   Eigen::Matrix<T,4,Eigen::Dynamic> _points;
public:
   Eigen::Matrix<T,4,1> point( long index ) const {
      return _points.col( index );
   }
   ?????& point( long index ) {
      return _points.col( index );
   }
}


The first (const) getter is easy; I convert the column reference to a normal matrix and return it. But I have no idea what return type to use in the second case. I want to be able to do code like:

Code: Select all
myFoo.point( 3 ).normalize( );


(ok so normalize makes no sense here but you get the idea). I've tried this but it causes a compiler crash (!)

Code: Select all
decltype( _points.col( 0 ) )& point( long index ) {
   return _points.col( index );
}


Is there a particular (explicit) class / typedef I can use?
davidje13
Registered Member
Posts
13
Karma
0
OS
Nevermind; I just found this thread viewtopic.php?f=74&t=95125 which answers this.

The working solution is:

Code: Select all
template< typename T > class foo {
   typedef Eigen::Matrix<T,4,Eigen::Dynamic> points_t;
   points_t _points;
public:
   typename points_t::ConstColXpr point( long index ) const {
      return _points.col( index );
   }
   typename points_t::ColXpr point( long index ) {
      return _points.col( index );
   }
}


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]