Contents
SLAM Notes series:https://blog.csdn.net/weixin_44543463/category_10925276.html
I. Introduction to and Installation of the Eigen Library
1.1 What Is Eigen?
Eigen is an open-source C++ linear algebra library that provides fast matrix and linear algebra operations, equation solving, and other features. Many higher-level software libraries also use Eigen for matrix operations. Eigen is a header-only library, so to use it, you only need to include its header files; no library linking is required.
1.2 Installing Eigen
If Eigen is not installed on your computer, enter the following command to install it.
sudo apt-get install libeigen3-dev
II. Basic Use of the Eigen Library
2.1 Including Eigen
Add the following two header files at the beginning of the cpp file
#include <Eigen/Core>
#include <Eigen/Dense>
Specify Eigen’s header-file directory in CMakeLists.txt (if you installed Eigen in a different location, you must change the header-file directory here)
include_directories("/usr/include/eigen3")
2.2 Basic Eigen Syntax
| Function | Syntax |
|---|---|
| Declare an m-by-n float matrix | eigen Eigen::Matrix<float,m,n> matrix_name; |
| Declare a three-dimensional column vector | Eigen::Vector3d vector_name; |
| Declare a three-by-three square matrix | Eigen::Matrix3d matrix_name; |
| Dynamic-size matrix | Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_name; |
| Initialize a matrix as a zero matrix | Eigen::Matrix3d matrix_name = Eigen::Matrix3d::Zero(); |
| Input data | matrix_name << 1,2,3,4,5,6; |
| Output data | cout << matrix_name << endl; |
| Data type conversion | matrix_name.cast<double>() |
| Matrix multiplication | matrix_name1 * matrix_name2 |
| Transpose | matrix_name.transpose(); |
| Sum of all elements | matrix_name.sum() |
| Trace | matrix_name.trace() |
| Inverse | matrix_name.inverse() |
| Determinant | matrix_name.determinant() |
| Conjugate matrix | matrix_name.conjugate() |
| Adjoint matrix | matrix_adjoint() |
| Compute eigenvalues | Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigenSolver(matrix3d); |
| Eigenvalues | eigenSolver.eigenvalues() |
| Eigenvectors | eigenSolver.eigenvectors() |
2.3 Implementing Rotation Transformations with Eigen
- Three-dimensional rotation matrix: Simply create a three-dimensional matrix
//例:创建一个三阶单位矩阵
Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
- Rotation vector: Use AngleAxis. You can multiply it by a vector to perform a rotation (because the operator is overloaded). The arguments in parentheses are the rotation vector’s angle and axis
//创建一个绕z轴旋转45°的旋转向量
Eigen::AngleAxisd rotation_vector(M_PI/4,Eigen::Vector3d(0,0,1));
- Convert a rotation vector to a three-dimensional rotation matrix
//将rotation_vector这个旋转向量转换为旋转矩阵并打印出来
cout<<"rotation matrix = \n"<<rotation_vector.matrix()<<endl;
//或通过toRotationMatrix转换为旋转矩阵
rotation_matrix = rotation_vector.toRotationMatrix();
- Convert a rotation matrix to Euler angles
//将旋转矩阵转换为ZYX顺序的欧拉角,即yaw-pitch-roll
Eigen::Vector3d euler_angles = rotation_matrix.eulerAngles(2,1,0);
cout<<"yaw pitch roll = "<<euler_angles.transpose()<<endl;
- Perform a coordinate transformation with a rotation vector: Because the operator is overloaded, you can perform the rotation simply by multiplying the rotation vector by the coordinate vector
//定义一个x方向的向量,用前面定义的旋转向量进行旋转,然后输出旋转后的结果。
Eigen::Vector3d v(1,0,0);
Eigen::Vector3d v_rotated = rotation_vector * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
- Perform a coordinate transformation with a rotation matrix
v_rotated = rotation_matrix * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
- Perform a coordinate transformation with a transformation matrix
//定义一个名为T的变换矩阵,虽说是3d,但实际是4x4矩阵,Identity说明旋转是0,平移也是0
Eigen::Isometry3d T=Eigen::Isometry3d::Identity();
//将左上角的旋转矩阵设为按旋转向量rotation_vector旋转
T.rotate(rotation_vector);
//设置右上角的平移矩阵为[1,3,4](旋转前平移)
T.pretranslate(Eigen::Vector3d(1,3,4));
//因为运算符重载,变换矩阵可以直接乘三维向量
Eigen::Vector3d v_transformed = T*v;
- Use a quaternion
//创建一个四元数
Eigen::Quaterniond q;
//可以直接把旋转向量赋值给四元数
q = Eigen::Quaterniond(rotation_vector);
//也可以把旋转矩阵赋值给它
q=Eigen::Quaterniond(rotation_matrix);
//使用四元数旋转一个向量
v_rotated = q*v;![[SLAM Notes] How to Use Eigen for Matrix Operations](https://img.mahaofei.com/img/202112231730503-slam-notes2-10.png)
Comments