四元素、欧拉角及旋转矩阵之间的转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuoyueljl/article/details/70789472

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

2、测试结果


转载自:https://blog.csdn.net/zhuoyueljl/article/details/70789472

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuoyueljl/article/details/70789472

猜你喜欢

转载自blog.csdn.net/baidu_38172402/article/details/82792109