How translation a matrix(4x4) in Eigen?

ぐ巨炮叔叔 提交于 2019-12-06 05:03:33

问题


How translation a matrix(4x4) in Eigen?

//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);

ie, I have matrix:

1.0   0.0   0.0   0.0
0.0   1.0   0.0   0.0
0.0   0.0   1.0   0.0
0.0   0.0   0.0   1.0

And I want get this:

1.0   0.0   0.0   3.0
0.0   1.0   0.0   4.0
0.0   0.0   1.0   5.0
0.0   0.0   0.0   1.0

Right? How I can do this?

I can do this:

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

But it's not elegant. =) What you advice?


回答1:


Like this:

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

Here is the doc with more details.




回答2:


Some alternative to catscradle answer:

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

or

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

or

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

or

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


来源:https://stackoverflow.com/questions/20323487/how-translation-a-matrix4x4-in-eigen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!