四元数到旋转矩阵,使用特征库的值不正确

Quaternion to Rotation Matrix, incorrect values using Eigen Library

我正在尝试使用关于 Y 轴的四元数将对象旋转 45 度。 指定四元数后,我试图获得旋转矩阵。但是我看到的值不正确

Eigen::Quaterniond q;
q.x() = 0;
q.y() = 1;
q.z() = 0;
q.w() = PI/8;    // Half of the rotation angle must be specified, even IDK why

Eigen::Matrix3d R = q.normalized().toRotationMatrix();
std::cout << "R=" << std::endl << R << std::endl;

输出:

R=
-0.732    -0   -0.680
     0     1       -0
 0.680     0   -0.732


由于沿 Y 轴的 OpenGL 旋转矩阵应为:

因此我的预期输出应该是:

R=
 0.707     0    0.707
     0     1        0
-0.707     0    0.707

不仅值偏离了一小部分,而且值上的错误符号导致了一些意外的旋转。由于负号,我的立方体正在旋转 180 度加上指定的角度。我一整天都在为这件事伤脑筋。有人可以告诉我我做错了什么吗?

您初始化四元数的方式不正确。如果直接初始化四元数坐标,需要考虑definition

或者,Eigen 中的 Quaternion class 提供了一个来自 axis-angle representation.

的构造函数

此代码:

#include <Eigen/Geometry>
#include <iostream>

void outputAsMatrix(const Eigen::Quaterniond& q)
{
    std::cout << "R=" << std::endl << q.normalized().toRotationMatrix() << std::endl;
}

void main()
{
    auto angle = M_PI / 4;
    auto sinA = std::sin(angle / 2);
    auto cosA = std::cos(angle / 2);

    Eigen::Quaterniond q;
    q.x() = 0 * sinA;
    q.y() = 1 * sinA;
    q.z() = 0 * sinA;
    q.w() = cosA;    

    outputAsMatrix(q);
    outputAsMatrix(Eigen::Quaterniond{Eigen::AngleAxisd{angle, Eigen::Vector3d{0, 1, 0}}});
}

输出你所期望的:

R=
 0.707107         0  0.707107
        0         1         0
-0.707107         0  0.707107
R=
 0.707107         0  0.707107
        0         1         0
-0.707107         0  0.707107