0
votes

I have a vector of std::array<double, 2> elements. I need to convert this to an Eigen::Vector3d to use a library and I want to know if there is anyway to use an Eigen::Map to interface with my vector. If my vector was an std::array<double, 3>` I could do something like

        std::array<double, 3> dx;
        Eigen::Map<const Eigen::VectorXd> dx_eig(&dx_[0][0], 3 * dx.size());

but my vector is made of std::array<double, 2> and I can't use directly an Eigen::Map as far as I know.

I want something like this:

std::vector<std::array<double, 2>> x;
Eigen::Map<Eigen::Vector3d> x_eig(x, ???)

such that x_eig behaves like in the following example

std::vector<std::array<double, 2>> x;
Eigen::Vector3d x_eig(3*x.size());
for (int i = 0; i < x.size(); ++i)
{
    x_eig[3*i + 0] = x[i][0];
    x_eig[3*i + 1] = x[i][1];
    x_eig[3*i + 2] = 0;
}

The last snipper requires duplicating memory and copying. If its possible to use some kind of map, it would be possible to reduce memory consumption and maybe improve performance.

As far as I know, Eigen::Map allows to use an Stride class to provide some strides, but they basically size in bytes for next element in a row and size in bytes for next element in a column. No way to specify something much more complex like, read 2 consecutive double and then a 0.

1
Can you please provide a minimal reproducible example, i.e., how would you do manually what you need to do. I have no idea how you want to map from std::vector<std::array<double,2>> (2*n elements) to 3 elements. Also, in the snippet you provide there is no dx_, just a dx. - chtz
Vector3d holds exactly 3 elements. Do you want a Matrix3Xdperhaps? What operation do you do afterwards with that? - chtz

1 Answers

0
votes

May be you can convert to 3d as said below and pass the vector to Eigen::Map.

//YOUR ARRAY.
std::array<double,2> d2 = {1,2};

//CREATE A VECTOR.
std::vector<double> d3(std::begin(d2), std::end(d2);

//ADD THIRD ELEMENT.
d3.push_back(0);

Now pass it to Eigen::Map