Eigen学习笔记
1. Eigen简介
2. 基础类型
3. Eigen的部分函数
3.1 向量
// ref: https://blog.csdn.net/subtitle_/article/details/131428919
列向量:
Eigen::VectorXf/d/i c;
或Eigen::Matrix<int, 5, 1> c;
行向量:
Eigen::RowVectorXf/d/i r;
或Eigen::Matrix<int, 1, 5> r;
向量赋值:
列向量赋值,Eigen::Vector2f c(5.0, 2.0);
行向量赋值,Eigen::RowVector2f r(5.0, 2.0);
逐元素赋值,Eigen::VectorXd c(2); c(0)=1;c(1)=2;
使用矩阵赋值,Eigen::Matrix<int, 1, 5> r; r << 1, 2, 3, 4, 5;
3.2 矩阵
矩阵定义
Eigen::MatrixXf/d/i m;
矩阵赋值
逐矩阵元素单独赋值:
Eigen::MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = m(0, 1)+m(1,0);
逗号赋值:
Eigen::Matrix3f m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
// ref: https://blog.csdn.net/hankerbit/article/details/82345603
Eigen::Matrix3d mat1;
mat1 << 1, 2, 3,
4, 5, 6,
7, 8, 9;
std::cout << "mat1=\n" << mat1 <<std::endl;
std::cout << "mat1转置矩阵=\n" << mat1.transpose() << std::endl;
std::cout << "mat1共轭伴随矩阵=\n" << mat1.adjoint() << std::endl;
std::cout << "mat1逆矩阵=\n" << mat1.inverse() << std::endl;
std::cout << "mat1行列式=\n" << mat1.determinant() << std::endl;
// 求解特征值与特征向量
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigensover(mat1);
if (eigensover.info() != Eigen::Success) abort();
std::cout << "特征值: \n" << eigensover.eigenvalues() << std::endl;
std::cout << "特征向量:\n" << eigensover.eigenvectors() << std::endl;
结果:
mat1 =
1 2 3
4 5 6
7 8 9
mat1转置矩阵
1 4 7
2 5 8
3 6 9
mat1共额伴随矩阵
1 4 7
2 5 8
3 6 9
mat1逆矩阵
-inf inf -inf
inf -inf inf
-inf inf -inf
mat1行列式
0
特征值:
-3.15747
-0.672768
18.8302
特征向量:
-0.802389 -0.434025 0.409627
-0.168127 0.822962 0.542649
0.57263 -0.366546 0.733307
3.3 Geometry模块
Eigen/Geometry模块
3.3.1 Eigen::AxngleAxis旋转向量
// 初始化
Eigen::AngleAxisd rotation_vector(alpha, Eigen::Vecot3d(x, y, z));
// 转换为旋转矩阵
Eigen::Matrix3d rotation_matrix;
rotation_matrix = rotation_vector.matrix();
// or
rotation_matrix = rotation_vector.toRotationMatrix();
// 转换为Eular角
eular_angles = rotation_vector.matrix().eularAngles(2, 1, 0);
// 转换为四元数
Eigen::Quaternion q = Quaterniond(rotation_vecotr);
扩展阅读:
ref: https://blog.csdn.net/hitgavin/article/details/106713290
轴角和旋转向量本质上一样。轴角用四个元素表达旋转,其中三个元素表示旋转轴,一个元素表示旋转角度。例如 r=[x,y,z,theta],其中[x,y,z]表示旋转轴,theta表示旋转角。旋转向量与轴角相同,只是旋转向量用三个元素描述旋转,它把theta角乘到了旋转轴上,例如[x*theta, y*theta, z*theta]。
姿态的轴角表示法与四元数非常相似,都是描述绕某一个轴旋转了一个角度。
R
(
α
,
β
,
γ
)
=
R
y
(
α
)
R
x
(
β
)
R
z
(
γ
)
R(\alpha,\beta,\gamma) =R_y(\alpha)R_x(\beta)R_z(\gamma)
R(α,β,γ)=Ry(α)Rx(β)Rz(γ)
欧拉角构造旋转矩阵相当于直接把三个Elemental Rotation Matrix乘在一起。
ref: https://zhuanlan.zhihu.com/p/45404840
3.3.2 欧拉角
Eigen::eularAngles(a0, a1, a2);
a0, a1, a2从{0, 1, 2}中取值,0 X轴, 1 Y轴, 2 Z轴, 旋转顺序为从后到前(下一次旋转使用的旋转轴是上一次旋转之后的。
Eigen::Vector3f euler_angles = rotation_matrix.eulerAngles(2, 1, 0);
// 等价于
rotation_matrix = AngleAxisd(euler_angles[0], Eigen::Vector3f::UnitZ())
* AngelAxisd(euler_angles[1], Eigen::Vector3f::UnitY())
* AngelAxisd(euler_angles[2], Eigen::Vector3f::UnitX());
// 均表示先绕X轴转euler_angles[2]弧度,再绕Y轴转euler_angles[1]弧度,再绕Z轴转eular_angles[0]弧度。
3.3.3 欧式变换
Eigen::Isometry
// 定义
Eigen::Isometry3d T = Eigen::Isometry3d::Identity();
T.rotate(rotation_matrix);
T.pretranslate(Eigen::Vector3d(x, y, z));
// 转为4*4变换矩阵
Eigen::Matrix4d transform_matrix = T.matrix();
// 欧式变换进行坐标变换
Eigen::Vector3d v = (1, 2, 3);
Eigen::Vector3d v_transformed = T * v;
3.3.4 四元数
Eigen::Quanternion
// 初始化
Eigen::Quanterniond q(w, x, y, z);
// 初始化时,实部在前,虚部在后,但内部存储顺序为[x, y, z, w]
// 使用旋转向量进行初始化
Eigen::AngleAxisd rotation_vector;
Eigen::Quanterniond q1(rotation_vector);
// 使用旋转矩阵进行初始化
Eigen::Matrix3d rotation_matrix;
Eigen::Quanterniond q2(rotation_matrix);
// 打印四元数
std::cout << q1.coeffs() << std::endl;
// 输出结果为 [x, y, z, w]
// 使用四元数旋转向量 ?
Eigen::Vector3d v_rotated = q * v;