c++ 四元数转欧拉角

c++ 四元数转欧拉角

两种方法,供你选择;

方法1;



/*
输入:x,y,z,w 为四元数
输出:roll,pitch,yaw欧拉角
**/
static void toEulerAngle(const double x,const double y,const double z,const double w, double& roll, double& pitch, double& yaw)
{
// roll (x-axis rotation)
    double sinr_cosp = +2.0 * (w * x + y * z);
    double cosr_cosp = +1.0 - 2.0 * (x * x + y * y);
    roll = atan2(sinr_cosp, cosr_cosp);

// pitch (y-axis rotation)
    double sinp = +2.0 * (w * y - z * x);
    if (fabs(sinp) >= 1)
        pitch = copysign(M_PI / 2, sinp); // use 90 degrees if out of range
    else
        pitch = asin(sinp);

// yaw (z-axis rotation)
    double siny_cosp = +2.0 * (w * z + x * y);
    double cosy_cosp = +1.0 - 2.0 * (y * y + z * z);
    yaw = atan2(siny_cosp, cosy_cosp);
//    return yaw;
}


调用

            cv::Vec3d euler;
            toEulerAngle(theta[0],theta[1],theta[2],theta[3],euler[0],euler[1],euler[2]);
            cout <<"euler "<< euler << endl;

方法二-依赖库Eigen;

eigen安装;有就跳过;

sudo apt install libeigen3-dev
#include <Eigen/Dense>
#include <Eigen/Geometry>
#include <Eigen/StdVector>
#include <Eigen/Core>

Vector3d Quaterniond2Euler(const double x,const double y,const double z,const double w)
{
    Eigen::Quaterniond q;
    q.x() = x;
    q.y() = y;
    q.z() = z;
    q.w() = w;

    Eigen::Vector3d euler = q.toRotationMatrix().eulerAngles(2, 1, 0);
    cout << "Quaterniond2Euler result is:" <<endl;
    cout << "x = "<< euler[2] << endl ;
    cout << "y = "<< euler[1] << endl ;
    cout << "z = "<< euler[0] << endl << endl;
    return euler;
}

调用;theta是四元数;返回 Eigen::Vector3d euler

 Eigen::Vector3d euler = Quaterniond2Euler(theta[0],theta[1],theta[2],theta[3] );
欧拉角可以通过四元数进行转换。具体方法如下: 首先,将欧拉角转换为旋矩阵。旋矩阵可以通过以下公式计算: $R_x(\alpha)=\begin{pmatrix} 1 & 0 & 0 \\ 0 & \cos\alpha & -\sin\alpha \\ 0 & \sin\alpha & \cos\alpha \\ \end{pmatrix}$ $R_y(\beta)=\begin{pmatrix} \cos\beta & 0 & \sin\beta \\ 0 & 1 & 0 \\ -\sin\beta & 0 & \cos\beta \\ \end{pmatrix}$ $R_z(\gamma)=\begin{pmatrix} \cos\gamma & -\sin\gamma & 0 \\ \sin\gamma & \cos\gamma & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}$ 其中,$R_x(\alpha)$、$R_y(\beta)$、$R_z(\gamma)$分别为绕$x$、$y$、$z$轴旋$\alpha$、$\beta$、$\gamma$角度的旋矩阵。 然后,将三个旋矩阵相乘,得到一个总的旋矩阵$R$: $R = R_z(\gamma)R_y(\beta)R_x(\alpha)$ 接着,将总的旋矩阵$R$转换四元数$q$。根据四元数的定义,我们可以将旋矩阵$R$表示为: $R=\begin{pmatrix} 1-2(q_2^2+q_3^2) & 2(q_1q_2-q_3q_0) & 2(q_1q_3+q_2q_0) \\ 2(q_1q_2+q_3q_0) & 1-2(q_1^2+q_3^2) & 2(q_2q_3-q_1q_0) \\ 2(q_1q_3-q_2q_0) & 2(q_2q_3+q_1q_0) & 1-2(q_1^2+q_2^2) \\ \end{pmatrix}$ 其中,$q_0$、$q_1$、$q_2$、$q_3$分别为四元数的实部和虚部。 最后,我们可以通过以下公式计算四元数$q$: $q=\begin{pmatrix} \cos\frac{\alpha}{2}\cos\frac{\beta}{2}\cos\frac{\gamma}{2}+\sin\frac{\alpha}{2}\sin\frac{\beta}{2}\sin\frac{\gamma}{2} \\ \sin\frac{\alpha}{2}\cos\frac{\beta}{2}\cos\frac{\gamma}{2}-\cos\frac{\alpha}{2}\sin\frac{\beta}{2}\sin\frac{\gamma}{2} \\ \cos\frac{\alpha}{2}\sin\frac{\beta}{2}\cos\frac{\gamma}{2}+\sin\frac{\alpha}{2}\cos\frac{\beta}{2}\sin\frac{\gamma}{2} \\ \cos\frac{\alpha}{2}\cos\frac{\beta}{2}\sin\frac{\gamma}{2}-\sin\frac{\alpha}{2}\sin\frac{\beta}{2}\cos\frac{\gamma}{2} \\ \end{pmatrix}$ 其中,$\alpha$、$\beta$、$\gamma$分别为欧拉角的三个分量。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值