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.
std::vector<std::array<double,2>>(2*nelements) to 3 elements. Also, in the snippet you provide there is nodx_, just adx. - chtzVector3dholds exactly 3 elements. Do you want aMatrix3Xdperhaps? What operation do you do afterwards with that? - chtz