///c数组转Eigen::MatrixXd
double vec[6] = {1, 2, 3, 4, 5, 6};
std::cout << Eigen::Map<Eigen::VectorXd>(&vec[0], sizeof (vec) / sizeof(double)) << "\n";
double arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
std::cout << Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(&arr[0][0], 3, 3) << "\n";
///std::vector<double>转Eigen::MatrixXd
std::vector<int> vec2{1, 2, 3, 4, 5, 6};
std::cout << Eigen::Map<Eigen::VectorXi>(vec2.data(), 6) << "\n";
Eigen::Matrix3d mat2 = Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(&arr[0][0], 3, 3);
std::cout << mat2 << "\n";
///Eigen::MatrixXd转std::vector<double>
Eigen::Isometry3d vRes = Eigen::Isometry3d::Identity();
Eigen::Matrix<double, 4, 4, Eigen::RowMajor> res = vRes.matrix();
auto res = std::vector<double>(res.data(), res.data() + res.size());
Eigen::Matrix<float, 4, 4, Eigen::RowMajor> A;
A << 0.814723686393179, 0.632359246225410, 0.957506835434298, 0.957166948242946,
0.905791937075619, 0.097540404999410, 0.964888535199277, 0.485375648722841,
0.126986816293506, 0.278498218867048, 0.157613081677548, 0.800280468888800,
0.913375856139019, 0.546881519204984, 0.970592781760616, 0.141886338627215;
Eigen::Matrix<float, 4, 4, Eigen::RowMajor> B;
B << 0.421761282626275, 0.655740699156587, 0.678735154857773, 0.655477890177557,
0.915735525189067, 0.035711678574190, 0.757740130578333, 0.171186687811562,
0.792207329559554, 0.849129305868777, 0.743132468124916, 0.706046088019609,
0.959492426392903, 0.933993247757551, 0.392227019534168, 0.031832846377421;
Eigen::Matrix<float, 4, 4, Eigen::RowMajor> C;
C = A * B;
std::cout << "\n" << C << std::endl;
std::cout << "\n";
float A1[4][4];
float B1[4][4];
float C1[4][4];
Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&A1[0][0], 4, 4) = A;
Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&B1[0][0], 4, 4) = B;
for (auto & i : B1)
{
for (float j : i)
{
std::cout << j << " ";
}
std::cout << "\n";
}
auto A2 = Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&A1[0][0], 4, 4);
auto B2 = Eigen::Map<Eigen::Matrix<float, 4, 4, Eigen::RowMajor>>(&B1[0][0], 4, 4);
std::cout << "\n" << A2 << std::endl;
std::cout << "\n" << B2 << std::endl;
float V[4] = {1,2,3,4};
auto V1 = Eigen::Map<Eigen::Vector4f>(&V[0], 4, 1);
std::cout << V1.transpose() << std::endl;
float V2[4];
Eigen::Map<Eigen::Vector4f>(&V2[0], 4, 1) = V1;
for (auto & i : V2)
{
std::cout << i << " ";
}
std::cout << "\n";
Eigen::Map的常见用法
于 2021-06-11 11:49:15 首次发布