Eigen库 旋转矩阵和平移向量组成齐次变换矩阵

3,929 阅读1分钟

3\times 3旋转矩阵r3\times 1平移向量t组成4\times 4齐次变换矩阵trans

block操作

Block operation Version constructing a dynamic-size block expression Version constructing a fixed-size block expression
Block of size (p,q), starting at (i,j) matrix.block(i,j,p,q) matrix.block<p,q>(i,j);

方法

Matrix3f r; // rotation matrix
Vector3f t; // translation vector

Matrix4f trans; // transformation matrix
trans.setIdentity();   // set to identity
trans.block<3,3>(0,0) = r; // first 3x3 block set to rotation matrix
trans.block<3,1>(0,3) = t; // fourth column set to translation vector

测试

#include <iostream>
#include <exception>
#include <Eigen/Eigen>

using namespace std;
using namespace Eigen;

constexpr float pi = 3.14f;

int main(int argc, char **argv)
{
	Matrix3f r; // rotation matrix
	Vector3f t; // translation vector
	AngleAxisf rot_x (pi/4, Vector3f(1, 0, 0));
	r = rot_x.matrix();
	t = {0.0f, 0.5f, 0.0f};

	/* construct transformation matrix with rotaion matirx and translation vector */
	Matrix4f trans; // transformation matrix
	trans.setIdentity();   // set to identity
	trans.block<3,3>(0,0) = r; // first 3x3 block set to rotation matrix
	trans.block<3,1>(0,3) = t; // fourth column set to translation vector

	/* print */
	cout << "rotaiton matrix: " << endl << r.format({3}) << endl;
	cout << "translation vector: " << endl << t.format({3}) << endl;
	cout << "transformation matrix: " << endl << trans.format({3}) << endl;

    return EXIT_SUCCESS;
}

输出

参考