Contents
  1. I. Introduction to and Installation of the Eigen Library
  2. 1.1 What Is Eigen?
  3. 1.2 Installing Eigen
  4. II. Basic Use of the Eigen Library
  5. 2.1 Including Eigen
  6. 2.2 Basic Eigen Syntax
  7. 2.3 Implementing Rotation Transformations with Eigen

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

FunctionSyntax
Declare an m-by-n float matrixeigen Eigen::Matrix<float,m,n> matrix_name;
Declare a three-dimensional column vectorEigen::Vector3d vector_name;
Declare a three-by-three square matrixEigen::Matrix3d matrix_name;
Dynamic-size matrixEigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_name;
Initialize a matrix as a zero matrixEigen::Matrix3d matrix_name = Eigen::Matrix3d::Zero();
Input datamatrix_name << 1,2,3,4,5,6;
Output datacout << matrix_name << endl;
Data type conversionmatrix_name.cast<double>()
Matrix multiplicationmatrix_name1 * matrix_name2
Transposematrix_name.transpose();
Sum of all elementsmatrix_name.sum()
Tracematrix_name.trace()
Inversematrix_name.inverse()
Determinantmatrix_name.determinant()
Conjugate matrixmatrix_name.conjugate()
Adjoint matrixmatrix_adjoint()
Compute eigenvaluesEigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigenSolver(matrix3d);
EigenvalueseigenSolver.eigenvalues()
EigenvectorseigenSolver.eigenvectors()

2.3 Implementing Rotation Transformations with Eigen

  1. Three-dimensional rotation matrix: Simply create a three-dimensional matrix
//例:创建一个三阶单位矩阵
Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
  1. 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));
  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();
  1. 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;
  1. 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;
  1. Perform a coordinate transformation with a rotation matrix
v_rotated = rotation_matrix * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
  1. 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;
  1. Use a quaternion
//创建一个四元数
Eigen::Quaterniond q;
//可以直接把旋转向量赋值给四元数
q = Eigen::Quaterniond(rotation_vector);
//也可以把旋转矩阵赋值给它
q=Eigen::Quaterniond(rotation_matrix);
//使用四元数旋转一个向量
v_rotated = q*v;