如何在Eigen中平移矩阵(4x4)

How translation a matrix(4x4) in Eigen?

本文关键字:4x4 Eigen      更新时间:2023-10-16

如何在Eigen中平移矩阵(4x4)?

//identity matrix 4x4
/*type=*/Eigen::Matrix<float, 4, 4> /*name=*/result = Eigen::Matrix<float, 4, 4>::Identity();
//translation vector
// 3.0f
// 4.0f
// 5.0f
Translation<float, 3> trans(3.0f, 4.0f, 5.0f);

即,我有矩阵:

1.0 nbsp;0.0 nbsp;0.0 nbsp;0.0
0.0 nbsp;1.0 nbsp;0.0 nbsp;0.0
0.0 nbsp;0.0 nbsp;1.0 nbsp;0.0
0.0 nbsp;0.0 nbsp;0.0 nbsp;1.0

我想得到这个:

1.0 nbsp;0.0 nbsp;0.0 nbsp 3.0
0.0 nbsp;1.0 nbsp;0.0 nbsp 4.0
0.0 nbsp;0.0 nbsp;1.0 nbsp 5.0
0.0 nbsp;0.0 nbsp;0.0 nbsp;1.0

对吧?我该怎么做?

我可以做到:

result(0, 3) = 3.0f;
result(1, 3) = 4.0f;
result(2, 3) = 5.0f;

但它并不优雅你有什么建议?

像这样:

Affine3f transform(Translation3f(1,2,3));
Matrix4f matrix = transform.matrix();

这是有更多细节的文档。

catscradle答案的一些替代方案:

Matrix4f mat = Matrix4f::Identity();
mat.col(3).head<3>() << 1, 2, 3;

mat.col(3).head<3>() = translation_vector;

Matrix4f mat;
mat << Matrix3f::Identity, Vector3f(1, 2, 3),
       0, 0, 0,            1;

Affine3f a;
a.translation() = translation_vector;
Matrix4f mat = a.matrix();