I need to convert a plain double C-array to a VectorXd*. I found Map class that seems to do the job but there's something I didn't understand.
class A {
private:
VectorXd *jpos_;
public:
int init( double *v, int len )
{
// Here I would like to assign v to jpos_ in an efficient way
// (something like a pointer assignment without allocation or vector iterations)
// Note that length of v is len
}
};
I tried the following but it doesn't work:
int A::init( double *v, int len )
{
jpos_ = &VectorXd::Map( v, len );
}
or
int A::init( double *v, int len )
{
jpos_ = &Map<VectorXd>( v, len );
}
What's wrong? Thanks.
Emanuele
If you want to map an array by means of Eigen::Map
, your member jpos_
should be of type Eigen::Map
:
class A
{
private:
Eigen::Map<Eigen::VectorXd> jpos_;
public:
A(double* v, int len) : jpos_ {v, len} {}
};
If you need to keep the VectorXd
, you need to copy the values to the vector. It is a bit strange to see VectorXd*
. It's similar like std::vector<double>*
. I would recommend not using VectorXd*
.
class A
{
private:
Eigen::VectorXd jpos_;
public:
A(double* v, int len) : jpos_ {v}
{
for (int i=0; i<len; ++i)
jpos_(i) = v[i];
}
};
EDIT:
If you really need VectorXd*
, you can allocate one with new
(and don't forget to delete
it).