#include
#include
#include
using namespace Eigen;
using namespace std;
int main()
{
Eigen::Matrix<float, 4, 4> transformation = Eigen::Matrix<float, 4, 4>::Identity();
Eigen::Quaterniond quaternion;
//1,从弧度(欧拉角)转四元数
float yaw = M_PI / 4; // 弧度角
//float pitch = M_PI / 4; // 弧度角
//float roll = M_PI / 4; // 弧度角
float pitch = 0; // 弧度角
float roll = 0; // 弧度角
quaternion = Eigen::AngleAxisd(yaw, Eigen::Vector3d::UnitZ()) *
Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitX());
cout << "4元数w:" << endl << quaternion.w() << endl;
cout << "4元数x:" << endl << quaternion.x() << endl;
cout << "4元数y:" << endl << quaternion.y() << endl;
cout << "4元数z:" << endl << quaternion.z() << endl;
cout << "4元数:" << endl << quaternion.matrix() << endl;
Eigen::Isometry3d iso = Eigen::Translation3d(1,2,3) * quaternion;
Eigen::Matrix4d res = iso.matrix();
cout << "等距映射:" << endl << res << endl;
// 2,从旋转矩阵构造四元数
Eigen::Matrix<double, 3, 3> rot;
rot = quaternion.matrix();
Eigen::Quaterniond qua(rot);
cout << "qua4元数w:" << endl << qua.w() << endl;
cout << "qua4元数x:" << endl << qua.x() << endl;
cout << "qua4元数y:" << endl << qua.y() << endl;
cout << "qua4元数z:" << endl << qua.z() << endl;
// 3,从四元数转换为旋转矩阵
Eigen::Matrix<double, 3, 3> rotation = qua.toRotationMatrix();
cout << "旋转矩阵:" << endl << rotation << endl;
// 4,从四元数转换为欧拉角
Eigen::Vector3d euler = qua.toRotationMatrix().eulerAngles(2, 1, 0);
cout << "欧拉角:" << endl << euler << endl; //弧度单位
// 5,从欧拉角转换为旋转矩阵(先转四元数, 再转旋转矩阵)
//Eigen::Quaterniond quaternion_1 = Eigen::AngleAxisd(yaw, Eigen::Vector3d::UnitZ()) *
// Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitY()) *
// Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitX());
Eigen::Quaterniond quaternion_1 = Eigen::AngleAxisd(euler(0), Eigen::Vector3d::UnitZ()) *
Eigen::AngleAxisd(euler(1), Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(euler(2), Eigen::Vector3d::UnitX());
Eigen::Matrix3d rotation_2 = quaternion_1.toRotationMatrix();
cout << "旋转矩阵:" << endl << rotation_2 << endl; //弧度单位
return 0;
}
打印输出:
4元数w:
0.844623
4元数x:
0.191342
4元数y:
0.46194
4元数z:
0.191342
4元数:
0.5 -0.146447 0.853553
0.5 0.853553 -0.146447
-0.707107 0.5 0.5
等距映射:
0.5 -0.146447 0.853553 1
0.5 0.853553 -0.146447 2
-0.707107 0.5 0.5 3
0 0 0 1
qua4元数w:
0.844623
qua4元数x:
0.191342
qua4元数y:
0.46194
qua4元数z:
0.191342
旋转矩阵:
0.5 -0.146447 0.853553
0.5 0.853553 -0.146447
-0.707107 0.5 0.5
欧拉角:
0.785398
0.785398
0.785398
旋转矩阵:
0.5 -0.146447 0.853553
0.5 0.853553 -0.146447
-0.707107 0.5 0.5
E:\c++\dianyun_3\x64\Debug\dianyun_1.exe (进程 81544)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
在线转换网站:1、三维在线旋转变换网站 https://www.andre-gaschler.com/rotationconverter/
2、 Rotation Conversion Tool https://danceswithcode.net/engineeringnotes/quaternions/conversion_tool.html
3、角度、弧度在线转换工具 https://www.osgeo.cn/app/sc210
参考链接:https://www.jianshu.com/p/4fda4c34b829
https://blog.csdn.net/hw140701/article/details/106255294