欧拉角和旋转矩阵的转换(附相关代码实现)

 1、欧拉角到旋转矩阵

 代码实现:

Eigen::Matrix3d euler2Rotation( Eigen::Vector3d  eulerAngles)
{
    double roll = eulerAngles(0);
    double pitch = eulerAngles(1);
    double yaw = eulerAngles(2);

    double cr = cos(roll); double sr = sin(roll);
    double cp = cos(pitch); double sp = sin(pitch);
    double cy = cos(yaw); double sy = sin(yaw);

    Eigen::Matrix3d RIb;
    RIb<< cy*cp ,   cy*sp*sr - sy*cr,   sy*sr + cy* cr*sp,
            sy*cp,    cy *cr + sy*sr*sp,  sp*sy*cr - cy*sr,
            -sp,         cp*sr,           cp*cr;
    return RIb;
}

2、旋转矩阵到欧拉角

 代码实现:

//由旋转矩阵计算欧拉角
        private double[] rotationMatrixToEulerAngles(double[] M)
        {
            double R00 = M[0], R01 = M[1], R02 = M[2];
            double R10 = M[4], R11 = M[5], R12 = M[6];
            double R20 = M[8], R21 = M[9], R22 = M[10];

            double sy = Math.Sqrt(R00 * R00 + R10 * R10);

            bool singular = sy < 1e-6; // If

            double x, y, z;
            if (!singular)
            {
                x = Math.Atan2(R21, R22);
                y = Math.Atan2(-R20, sy);
                z = Math.Atan2(R10, R00);
            }
            else
            {
                x = Math.Atan2(-R12, R11);
                y = Math.Atan2(-R20, sy);
                z = 0;
            }
            x = x * 180.0 / Math.PI;
            y = y * 180.0 / Math.PI;
            z = z * 180.0 / Math.PI;
            double[] angle = new double[3] { x, y, z };
            return angle;
        }

 

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们可以使用NumPy和scipy库来实现Python中ZYX顺序欧拉角旋转矩阵的相互转换。下面是一个示例代码,展示了如何将旋转矩阵转换欧拉角: ```python import numpy as np from scipy.spatial.transform import Rotation as R def rotation_matrix_to_euler_angles(rotation_matrix): r = R.from_matrix(rotation_matrix) euler_angles = r.as_euler('zyx', degrees=True) return euler_angles # 示例旋转矩阵 rotation_matrix = np.array([ [0.8660254, -0.5 , 0. ], [0.5 , 0.8660254, 0. ], [0. , 0. , 1. ] ]) euler_angles = rotation_matrix_to_euler_angles(rotation_matrix) print(euler_angles) ``` 如果你想将欧拉角转换旋转矩阵,我们可以使用下面的代码实现: ```python import numpy as np import math def euler_angles_to_rotation_matrix(roll, pitch, yaw): R_z = np.array([ [math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1] ]) R_y = np.array([ [math.cos(pitch), 0, math.sin(pitch)], [0, 1, 0], [-math.sin(pitch), 0, math.cos(pitch)] ]) R_x = np.array([ [1, 0, 0], [0, math.cos(roll), -math.sin(roll)], [0, math.sin(roll), math.cos(roll)] ]) R = np.dot(R_z, np.dot(R_y, R_x)) return R # 示例欧拉角 roll = 45 pitch = 30 yaw = 60 rotation_matrix = euler_angles_to_rotation_matrix(roll, pitch, yaw) print(rotation_matrix) ``` 这些代码可以帮助你在Python中进行ZYX顺序欧拉角旋转矩阵的相互转换。请根据实际情况进行调整和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [四元数,欧拉角旋转矩阵相互转换以及矩阵求逆合集(C++和python)](https://blog.csdn.net/Will_Ye/article/details/127498034)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [用python 写一个旋转矩阵欧拉角的程序](https://blog.csdn.net/kangzengxin/article/details/130977331)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值