Iva*_*van 4 c++ translation matrix eigen
如何在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);
Run Code Online (Sandbox Code Playgroud)
即,我有矩阵:
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
我想得到:
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
对?我怎么能这样做?
我可以做这个:
result(0, 3) = 3.0f;
result(1, 3) = 4.0f;
result(2, 3) = 5.0f;
Run Code Online (Sandbox Code Playgroud)
但它并不优雅.=)你的建议是什么?
像这样:
Affine3f transform(Translation3f(1,2,3));
Matrix4f matrix = transform.matrix();
Run Code Online (Sandbox Code Playgroud)
这是更详细的文档.
一些替代catscradle的答案:
Matrix4f mat = Matrix4f::Identity();
mat.col(3).head<3>() << 1, 2, 3;
Run Code Online (Sandbox Code Playgroud)
或者
mat.col(3).head<3>() = translation_vector;
Run Code Online (Sandbox Code Playgroud)
或者
Matrix4f mat;
mat << Matrix3f::Identity, Vector3f(1, 2, 3),
0, 0, 0, 1;
Run Code Online (Sandbox Code Playgroud)
或者
Affine3f a;
a.translation() = translation_vector;
Matrix4f mat = a.matrix();
Run Code Online (Sandbox Code Playgroud)