首页 > 解决方案 > 从 Eigen::AngleAxisd 到 Eigen::Vector3d 的转换

问题描述

我通过以下方式将旋转向量转换为旋转矩阵:

Eigen::Vector3d rotVec(-0.67925191, -1.35850382, -2.03775573);
Eigen::AngleAxisd rotAA(rotVec.norm(), rotVec.normalized());
Eigen::Matrix3d rotationMatrix = rotAA.matrix();

然后我想做从矩阵到向量的反向转换,我要做的是:

Eigen::AngleAxisd rotvec2(rotationMatrix);

然而,我无法将 Eigen::AngleAxisd 转换回 Eigen::Vector3d 并且 AngleAxisd 本身似乎只有一个角度和轴成员函数。

有谁知道我们该怎么做?

标签: c++geometryeigeneigen3

解决方案


AngleAxisangle()具有比和更多的成员函数axis()。要么fromRotationMatrix(const MatrixBase< Derived > &mat)AngleAxis(const MatrixBase< Derived > &mat)将做转换回来的第一步。见:https ://eigen.tuxfamily.org/dox/classEigen_1_1AngleAxis.html

然后使用乘积angle() * axis()取回原始向量。

#include <Eigen/Dense>
#include <cstdlib>

int main()
{
    using namespace Eigen;
    using T = double;

    Vector3<T> const rotVec{-0.67925191, -1.35850382, -2.03775573};
    AngleAxis<T> const rotAA{rotVec.norm(), rotVec.normalized()};
    Matrix3<T> const rotMat{rotAA.toRotationMatrix()};

#if 0
    AngleAxis<T> rotAA2{};
    rotAA2.fromRotationMatrix(rotMat);
#else
    AngleAxis<T> const rotAA2{rotMat};
#endif

    Vector3<T> const rotVec2{rotAA2.angle() * rotAA2.axis()};
    bool const is_valid = rotVec.isApprox(rotVec2);
    return is_valid ? EXIT_SUCCESS : EXIT_FAILURE;
}

推荐阅读