I have an a Eigen::MatrixXd
that I made from std::vector
of Eigen::Vector3d
. It was easy.
I made some transform manipulation on that matrix and I want return result as std vector of Eigen::Vector3d
.
How could I make
std::vector<Eigen::Vector3d>
form Eigen::Matrix3d?
Better stick with the std::vector
object. Here is how I usually deal with such cases:
std::vector<Vector3d> vecs(n);
auto mat = Matrix3Xd::Map(vecs[0].data(), 3, vecs.size());
This creates a view on the data owned by vecs
. Then play with mat
as you like (except resizing of course,). eg::
mat = my_affine * mat;
No need to copy back the values from mat
to vecs
, but of course, if you have a Matrix3Xf
or MatrixXf
already at hand and want to copy it to vecs
, then simply write:
mat = other_mat;
Provided that vecs.size() == other_mat.cols()
, otherwise you need to resize vecs
first and re-create a Map
with new size.
I agree with @ggael and @RHertel that you should stick with one representation. If you need to dynamically insert Vector3d
objects, then std::vector
is probably the better solution (and you can still access it like an Eigen object, using Eigen::Map
as shown by ggael).
Likewise, if you have a Matrix3Xd
and want to use it column-wise in a standard algorithm, you can do that using .colwise().begin()
and .colwise().end()
which by itself won't copy any data (this requires the development branch of Eigen -- or the upcoming 3.4 version).
This also gives an easy way to create an std::vector
from an Eigen::Matrix3Xd
:
// `mat` needs to have 3 rows (at runtime)
std::vector<Eigen::Vector3d> vec(mat.colwise().begin(), mat.colwise().end());
Godbolt-Demo: https://godbolt.org/z/uCqZni
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.