加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:12:44 所属栏目:百科 来源:网络整理
导读:我正在尝试使用关于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 whyEigen::Matrix3d R
我正在尝试使用关于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旋转矩阵应该是:

enter image description here

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

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

不仅价值偏低一小部分,价值上的错误符号也会导致一些意外的轮换.由于负号,我的立方体正在进行180度转弯加上指定的角度.这一整天都让我不知所措.有人可以告诉我我做错了什么吗?

解决方法

初始化四元数的方式不正确.如果直接初始化四元数的坐标,则应考虑 definition:

enter image description here

或者,Eigen中的Quaternion类提供了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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读