【Eigen】旋转方向及eulerAngles函数参数说明

1. 旋转方向

不同的几何库对于旋转方向的正负号问题的定义不尽相同。这里主要验证下Eigen库旋转时,正负号判定的问题。

1.1 绕X轴旋转一个正角度

void TEST_rotation_direction_positive_negative_view_from_x_positive_to_origin()
{
    Eigen::Matrix3d R;
    R = Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitX());
    Eigen::Vector3d input_point(0, 1, 0);
    Eigen::Vector3d output_point = R * input_point;

    std::cout << "output_point.y: " << output_point(1) << std::endl;
    std::cout << "output_point.z: " << output_point(2) << std::endl;
    if(output_point[2] > 0)
        std::cout << "逆时针为正" << std::endl;
    else
        std::cout << "顺时针为正" << std::endl;
}

输出如下:

output_point.y: 0.707107
output_point.z: 0.707107
逆时针为正

1.2 绕Y轴旋转一个正角度

void TEST_rotation_direction_positive_negative_view_from_y_positive_to_origin()
{
    Eigen::Matrix3d R;
    R = Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitY());
    Eigen::Vector3d input_point(0, 0, 1);
    Eigen::Vector3d output_point = R * input_point;

    std::cout << "output_point.x: " << output_point(0) << std::endl;
    std::cout << "output_point.y: " << output_point(2) << std::endl;
    if(output_point[0] > 0)
        std::cout << "逆时针为正" << std::endl;
    else
        std::cout << "顺时针为正" << std::endl;
}

输出如下:

output_point.x: 0.707107
output_point.y: 0.707107
逆时针为正

1.3 绕Z轴旋转一个正角度

void TEST_rotation_direction_positive_negative_view_from_z_positive_to_origin()
{
    Eigen::Matrix3d R;
    R = Eigen::AngleAxisd(M_PI / 4, Eigen::Vector3d::UnitZ());
    Eigen::Vector3d input_point(1.0, 0, 0);
    Eigen::Vector3d output_point = R * input_point;

    std::cout << "output_point.x: " << output_point(0) << std::endl;
    std::cout << "output_point.y: " << output_point(1) << std::endl;
    if(output_point[1] > 0)
        std::cout << "逆时针为正" << std::endl;
    else
        std::cout << "顺时针为正" << std::endl;
}

输出如下:

output_point.x: 0.707107
output_point.y: 0.707107
逆时针为正

1.4 结果

由任意一个轴的正方向看向坐标系的原点,逆时针为正

2. eulerAngles函数参数说明

eulerAngles()用于将旋转矩阵转换为欧拉角,使用时需要根据实际情况指定绕轴旋转的顺序。eulerAngles()的函数原型如下:
Eigen::vector3d eulerAngles(Index a0, Index a1, Index a2),其中参数a0 a1 a20 1 2表示旋转轴,其中0表示X轴,1表示Y轴,2表示Z轴。此外,a0 表示首先选择的轴,a1 表示其次旋转的轴,a2 表示最后旋转的轴。输出结果的顺序与轴旋转的顺序相同。
以下通过两种情况进行验证:

2.1 先绕X轴旋转,其次绕Y轴旋转,最后绕Z轴旋转

void TEST_x30_y45_z60()
{
    double x = M_PI / 6;
    double y = M_PI / 4;
    double z = M_PI / 3;

    Eigen::Matrix3d R;
    R = Eigen::AngleAxisd(x, Eigen::Vector3d::UnitX()) *
        Eigen::AngleAxisd(y, Eigen::Vector3d::UnitY()) *
        Eigen::AngleAxisd(z, Eigen::Vector3d::UnitZ());

    auto angle = R.eulerAngles(0, 1, 2) * 180 / M_PI;
    std::cout << "X  = " << angle(0) << std::endl;
    std::cout << "Y  = " << angle(1) << std::endl;
    std::cout << "Z  = " << angle(2) << std::endl;
    if ((std::abs(angle(0) - 30) < 0.001) &&
        (std::abs(angle(1) - 45) < 0.001) &&
        (std::abs(angle(2) - 60) < 0.001))
    {
        std::cout << "xyz" << std::endl;
    }
}

输出结果如下:

X = 30
Y = 45
Z = 60
xyz

2.2 先绕Z轴旋转,其次绕Y轴旋转,最后绕X轴旋转

void TEST_z60_y45_x30()
{
    double x = M_PI / 6;
    double y = M_PI / 4;
    double z = M_PI / 3;

    Eigen::Matrix3d R;
    R = Eigen::AngleAxisd(z, Eigen::Vector3d::UnitZ()) *
        Eigen::AngleAxisd(y, Eigen::Vector3d::UnitY()) *
        Eigen::AngleAxisd(x, Eigen::Vector3d::UnitX());

    auto angle = R.eulerAngles(2, 1, 0) * 180 / M_PI;
    std::cout << "Z  = " << angle(0) << std::endl;
    std::cout << "Y  = " << angle(1) << std::endl;
    std::cout << "Z  = " << angle(2) << std::endl;
    if ((std::abs(angle(0) - 60) < 0.001) &&
        (std::abs(angle(1) - 45) < 0.001) &&
        (std::abs(angle(2) - 30) < 0.001))
    {
        std::cout << "zyx" << std::endl;
    }
}

输出结果如下:

Z = 60
Y = 45
Z = 30
zyx

  • 14
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
旋转矩阵是用来描述物体在三维空间中的旋转变换的工具。在Eigen库中,可以使用旋转矩阵对向量进行旋转操作。旋转矩阵可以通过旋转向量、欧拉角或四元数来初始化。 对于旋转向量,可以使用Eigen库中的AngleAxis类来表示。通过将旋转向量与单位向量相乘,可以得到旋转矩阵。具体步骤如下: 1. 初始化旋转向量:使用AngleAxis类来创建一个旋转向量,指定旋转角度和旋转轴。 2. 旋转向量转旋转矩阵:使用toRotationMatrix()函数将旋转向量转换为旋转矩阵。 示例代码如下: ``` #include <Eigen/Core> #include <Eigen/Geometry> Eigen::Vector3d axis(1.0, 0.0, 0.0); // 旋转轴 double angle = M_PI / 2; // 旋转角度 Eigen::AngleAxisd rotation_vector(angle, axis); // 初始化旋转向量 Eigen::Matrix3d rotation_matrix = rotation_vector.toRotationMatrix(); // 旋转向量转旋转矩阵 ``` 这样,你就可以使用旋转矩阵来对向量进行旋转操作了。 #### 引用[.reference_title] - *1* *2* [使用Eigen实现四元数、欧拉角、旋转矩阵、旋转向量之间的转换](https://blog.csdn.net/qq_34213260/article/details/113651597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhy29563

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值