将平移应用于特征顶点

Apply translation to Eigen vertices

我正在尝试使用从外部库以 Vector3d 形式接收的翻译信息,用 Eigen 置换几何体。我不担心轮换。

我已经尝试了我有限的知识 Eigen 允许的一切。我当前的代码如下所示:

void Mesh::displace(const Vector3d& v)
{
    Transform<double, 3, Affine> t = Transform<double, 3, Affine>::Identity();
    t.translate(v);
}

现在我很难将此翻译应用到我的 m_vertices,它是 MatrixXd3 by N 列,其中 3 代表 x, y, zN 代表顶点。

转换矩阵最终看起来像这样(Xs 代表转换的翻译部分):

1, 0, 0, X,
0, 1, 0, X,
0, 0, 1, X,
0, 0, 0, 1

基于此,我很确定到目前为止我已经准备好了一切。

我已经多次尝试应用翻译,但那些确实编译的在运行时崩溃了。

如果您只需要翻译,则不需要 Transform 对象。您可以简单地将平移矢量添加到网格中需要置换的每个位置矢量。如果您同时需要旋转和平移,那么您需要将它们分别应用为

Output = Rotation*Input + Translation

以下代码证明这两种方法是等效的。

#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Geometry>

using namespace Eigen;

int main(){
    int N = 2;

    // The translation vector
    Vector3d translation;
    translation << 1.0, 2.0, 3.0;

    // The N points to be translated
    Matrix3Xd points(3,N);
    points.setRandom(3,N);
    std::cout<< " The initial positions :" << std::endl
             << points << std::endl;

    // ******************** Case 1: Pure Translation ******************** //
    // Just add the translation vector to each point
    Matrix3Xd displaced_points(3,N);
    displaced_points = points.colwise() + translation;
    std::cout<< " The displaced positions :" << std::endl
             << displaced_points << std::endl;

    // **************** Case 2: Full Affine transformation **************** //
    std::cout<< "******************** Method 2 ********************" << std::endl;
    Transform<double_t, 3, Affine> t =
            Transform<double_t, 3, Affine>::Identity();
    t.translate(translation);
    std::cout<< "The transformation: " << std::endl
             << t.translation() << std::endl;
    // We need to apply the rotation and translation separately
    for( auto i=0; i < N; ++i){
        displaced_points.col(i) = t.linear()*points.col(i) + t.translation();
    }
    std::cout<< " The displaced positions :" << std::endl
             << displaced_points << std::endl;

    return 0;
}

这会产生如下输出:

The initial positions :
 0.680375   0.59688
-0.211234  0.823295
 0.566198 -0.604897

 The displaced positions :
1.68038 1.59688
1.78877 2.82329
 3.5662  2.3951
******************** Method 2 ********************
The transformation: 
1
2
3
 The displaced positions :
1.68038 1.59688
1.78877 2.82329
 3.5662  2.3951