Reputation: 1675
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.
Upvotes: 0
Views: 661
Reputation: 12781
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
Upvotes: 0