四元数、欧拉角、旋转矩阵之间互相转换C++源码

转自四元数、欧拉角、旋转矩阵之间互相转换C++源码
还可参考
Creating a rotation matrix with pitch, yaw, roll using Eigen
Finding roll, pitch yaw from 3X3 rotation matrix with Eigen


1、源码

  1. #include <iostream>  
  2. #include <Eigen/Eigen>  
  3. #include <stdlib.h>  
  4. #include <Eigen/Geometry>  
  5. #include <Eigen/Core>  
  6. #include <vector>  
  7. #include <math.h>  
  8.   
  9. using namespace std;  
  10. using namespace Eigen;  
  11.   
  12. Eigen::Quaterniond euler2Quaternion(const double roll, const double pitch, const double yaw)  
  13. {  
  14.     Eigen::AngleAxisd rollAngle(roll, Eigen::Vector3d::UnitZ());  
  15.     Eigen::AngleAxisd yawAngle(yaw, Eigen::Vector3d::UnitY());  
  16.     Eigen::AngleAxisd pitchAngle(pitch, Eigen::Vector3d::UnitX());  
  17.   
  18.     Eigen::Quaterniond q = rollAngle  yawAngle  pitchAngle;  
  19.     cout << “Euler2Quaternion result is:” <<endl;  
  20.     cout << ”x = ” << q.x() <<endl;  
  21.     cout << ”y = ” << q.y() <<endl;  
  22.     cout << ”z = ” << q.z() <<endl;  
  23.     cout << ”w = ” << q.w() <<endl<<endl;  
  24.     return q;  
  25. }  
  26.   
  27. Eigen::Vector3d Quaterniond2Euler(const double x,const double y,const double z,const double w)  
  28. {  
  29.     Eigen::Quaterniond q;  
  30.     q.x() = x;  
  31.     q.y() = y;  
  32.     q.z() = z;  
  33.     q.w() = w;  
  34.   
  35.     Eigen::Vector3d euler = q.toRotationMatrix().eulerAngles(2, 1, 0);  
  36.     cout << “Quaterniond2Euler result is:” <<endl;  
  37.     cout << ”x = ”<< euler[2] << endl ;  
  38.     cout << ”y = ”<< euler[1] << endl ;  
  39.     cout << ”z = ”<< euler[0] << endl << endl;  
  40. }  
  41.   
  42. Eigen::Matrix3d Quaternion2RotationMatrix(const double x,const double y,const double z,const double w)  
  43. {  
  44.     Eigen::Quaterniond q;  
  45.     q.x() = x;  
  46.     q.y() = y;  
  47.     q.z() = z;  
  48.     q.w() = w;  
  49.   
  50.     Eigen::Matrix3d R = q.normalized().toRotationMatrix();  
  51.     cout << “Quaternion2RotationMatrix result is:” <<endl;  
  52.     cout << ”R = ” << endl << R << endl<< endl;  
  53.     return R;  
  54. }  
  55.   
  56.   
  57. Eigen::Quaterniond rotationMatrix2Quaterniond(Eigen::Matrix3d R)  
  58. {  
  59.     Eigen::Quaterniond q = Eigen::Quaterniond(R);  
  60.     q.normalize();  
  61.     cout << “RotationMatrix2Quaterniond result is:” <<endl;  
  62.     cout << ”x = ” << q.x() <<endl;  
  63.     cout << ”y = ” << q.y() <<endl;  
  64.     cout << ”z = ” << q.z() <<endl;  
  65.     cout << ”w = ” << q.w() <<endl<<endl;  
  66.     return q;  
  67. }  
  68.   
  69. Eigen::Matrix3d euler2RotationMatrix(const double roll, const double pitch, const double yaw)  
  70. {  
  71.     Eigen::AngleAxisd rollAngle(roll, Eigen::Vector3d::UnitZ());  
  72.     Eigen::AngleAxisd yawAngle(yaw, Eigen::Vector3d::UnitY());  
  73.     Eigen::AngleAxisd pitchAngle(pitch, Eigen::Vector3d::UnitX());  
  74.   
  75.     Eigen::Quaterniond q = rollAngle  yawAngle  pitchAngle;  
  76.     Eigen::Matrix3d R = q.matrix();  
  77.     cout << “Euler2RotationMatrix result is:” <<endl;  
  78.     cout << ”R = ” << endl << R << endl<<endl;  
  79.     return R;  
  80. }  
  81.   
  82. Eigen::Vector3d RotationMatrix2euler(Eigen::Matrix3d R)  
  83. {  
  84.     Eigen::Matrix3d m;  
  85.     m = R;  
  86.     Eigen::Vector3d euler = m.eulerAngles(0, 1, 2);  
  87.     cout << “RotationMatrix2euler result is:” << endl;  
  88.     cout << ”x = ”<< euler[2] << endl ;  
  89.     cout << ”y = ”<< euler[1] << endl ;  
  90.     cout << ”z = ”<< euler[0] << endl << endl;  
  91.     return euler;  
  92. }  
  93.   
  94.   
  95. int main(int argc, char **argv)  
  96. {  
  97.   
  98. //this is euler2Quaternion transform function,please input your euler angle//  
  99.   euler2Quaternion(0,0,0);  
  100.   
  101. //this is Quaternion2Euler transform function,please input your euler angle//  
  102.   Quaterniond2Euler(0,0,0,1);  
  103.   
  104. //this is Quaternion2RotationMatrix transform function,please input your Quaternion parameter//  
  105.   Quaternion2RotationMatrix(0,0,0,1);  
  106.   
  107. //this is rotationMatrix2Euler transform function,please input your RotationMatrix parameter like following//  
  108.   Eigen::Vector3d x_axiz,y_axiz,z_axiz;  
  109.   x_axiz << 1,0,0;  
  110.   y_axiz << 0,1,0;  
  111.   z_axiz << 0,0,1;  
  112.   Eigen::Matrix3d R;  
  113.   R << x_axiz,y_axiz,z_axiz;  
  114.   rotationMatrix2Quaterniond(R);  
  115.   
  116. //this is euler2RotationMatrix transform function,please input your euler angle for the function parameter//  
  117.   euler2RotationMatrix(0,0,0);  
  118.   
  119. //this is RotationMatrix2euler transform function,please input your euler angle for the function parameter//  
  120.   RotationMatrix2euler(R);  
  121.   
  122.   cout << “All transform is done!” << endl;  
  123. }  
 


2、测试结果


  • 2
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 欧拉角四元数旋转矩阵和轴都是表示三维旋转的不同方式。 欧拉角是由三个轴组成,按照顺序分别表示绕x轴旋转度、绕y轴旋转度、绕z轴旋转度。 四元数是由四个实数组成,表示旋转的方向和度。 旋转矩阵是由3*3的实数组成的矩阵,表示旋转的线性变换。 轴就是由一个单位向量和一个度组成,表示绕着该单位向量旋转度的意思。 它们之间可以相互转换。具体方法需要根据需要选择相应的公式进行转换. ### 回答2: 欧拉角四元数旋转矩阵和轴是用于表示物体在三维空间中旋转的常见方法。它们可以相互之间进行转换。 首先,欧拉角是使用三个旋转度来描述物体的旋转。通常使用的欧拉角包括俯仰pitch angle)、偏航yaw angle)和滚roll angle)。欧拉角转换通常涉及将欧拉角转换旋转矩阵四元数,并且转换顺序也很重要。 其次,四元数是一种用于表示旋转的数学工具,可以使用具有四个实数部分的向量进行表示。四元数转换通常涉及将四元数转换旋转矩阵欧拉角,或者将旋转矩阵欧拉角转换四元数旋转矩阵是一个3x3矩阵,用于表示物体的旋转。它是通过将欧拉角四元数转换为矩阵来实现的,也可以将矩阵转换欧拉角四元数。 轴是用于表示旋转的方法之一。它由一个向量和一个表示旋转度的标量组成。轴可以通过将轴转换旋转矩阵来实现,也可以通过将旋转矩阵转换为轴来实现。使用轴进行旋转时,常用的轴包括x轴、y轴和z轴。 总结起来,欧拉角四元数旋转矩阵和轴可以相互转换来表示物体的旋转。这些转换过程在计算机图形学、机器人学和游戏开发等领域经常被使用。理解它们之间转换关系可以帮助我们更好地理解和应用旋转的概念。 ### 回答3: 欧拉角四元数旋转矩阵、轴都是用于描述物体在三维空间中的旋转变换的方法,它们之间可以相互转换欧拉角是指通过绕着三个坐标轴的旋转来实现的旋转变换。通常使用三个连续的旋转度来表示,在航空航天领域经常使用俯仰、偏航和滚来描述。但欧拉角存在万向锁问题,即在某些情况下会导致旋转变换不唯一。 四元数是一种四维复数,可以用一个实部和三个虚部来表示。它们可以直接表示旋转变换,并且不存在万向锁问题。通过四元数的乘法运算可以实现旋转变换的组合。同时,由于四元数是一个四维向量,所以它们的存储空间比旋转矩阵小。 旋转矩阵是一个3x3的矩阵,用于表示旋转变换。在旋转矩阵中,每一列表示一个旋转后的坐标轴方向。旋转矩阵可以通过将三个坐标轴绕着相应的度进行旋转得到。但旋转矩阵存在正交性约束,即必须是正交矩阵,并且行列式为1,不满足时需要进行正则化处理。 轴表示旋转轴和旋转度的方法。它将旋转变换化为绕着一个轴旋转一定度的方式来描述。轴旋转矩阵之间转换比较直观,可以通过旋转矩阵的特征向量和特征值得到旋转轴和旋转度。但轴存在方向的不唯一性,即旋转轴可以有两个相反的方向与同一个旋转变换对应。 以上是欧拉角四元数旋转矩阵、轴之间转换方法及特点的简介。它们在三维空间中描述旋转变换时各有优劣,可以根据具体需求选择合适的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值