一、旋转矩阵
旋转矩阵直接可以使用Matrix类来定义。关于矩阵的操作可以看我之前写的内容:
Eigen库的基本使用说明_HHB791829200的博客-CSDN博客https://blog.csdn.net/HHB791829200/article/details/123417317
二、旋转轴
Eigen中有专门的几何变化的头文件Geometry。其中旋转轴在Eigen中通过一个选择度数和一个旋转轴表示:
AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));使用这种方式定义需要声明类型,也可以使用默认设定好的类型如float 和double类型。
//3D rotation as an angle + axis
//AngleAxis<float> aa(angle_in_radian, Vector3f(ax,ay,az));
Matrix3d roation_matrix=Matrix3d::Identity();
Matrix3f roation_matrixf=Matrix3f::Identity();
AngleAxisd roation_vector(M_PI/4,Vector3d(0,0,1));
AngleAxis<float> roation_vectorf(M_PI/4,Vector3f(0,0,1));
使用旋转矩阵和旋转轴对点(1,0,0)进行旋转:
//使用选择矩阵和选择向量对点(1,0,0)进行坐标变化
Vector3d vector(1,0,0);
Vector3d v_roation = roation_vector*vector;
cout<<" this Angle Axis to (1,0,0) \n "<<v_roation<<endl;
cout<<"this roation Matrix to (1,0,0) \n"<<roation_matrix*vector<<endl;
三、欧拉角
欧拉角的定义:
m = AngleAxisf (angle1, Vector3f::UnitZ ())
* AngleAxisf (angle2, Vector3f::UnitY ())
* AngleAxisf (angle3, Vector3f::UnitZ ());
这种表示方式比较麻烦如果我们知道旋转矩阵可以转换到欧拉角:
Vector3d Euler_Angles=roation_matrix.eulerAngles(2,1,0);//ZYX顺序其中 Z轴表示2 Y轴表示1 X
//轴表示0
cout<<"euler angle is "<<Euler_Angles.transpose()<<endl;
四、变换矩阵
变换矩阵使用Isometry3d进行定义:可以开始定义一个单位阵之后只有旋转向量和平移来舒适化变化矩阵。
Isometry3d T=Isometry3d::Identity(); //定义为3d,但实际为4d
T.rotate(roation_vector);//设置旋转
T.pretranslate(Vector3d(1,3,4));//设置平移
cout<<" this transform is \n "<<T.matrix()<<endl;
//使用变换矩阵进行坐标变化
Vector3d v_tranformed=T*vector;
cout<<"use tranform : \n"<<v_tranformed<<endl;
五、四元数
Quaternion<float> q; q = AngleAxis<float>(angle_in_radian, axis);
当然四元素也可以用旋转矩阵初始化
q=Quaternion(Matrix_roation);
直接对四元数进行赋值一定要将设置为归一化下的坐标,q.normalize();
Quaterniond q=Quaterniond(roation_vector);
cout<<"quaterniond is"<<q.coeffs().transpose()<<endl;//前三个为虚部后一个为实部
q=Quaterniond(roation_matrix);
cout<<"quaterniond is"<<q.coeffs().transpose()<<endl;
//使用四元数进行坐标变化
Vector3d v_q=q*vector;
cout<<" use quaterniond \n"<<v_q<<endl;