首页 > 解决方案 > 将平移应用于特征顶点

问题描述

我正在尝试Eigen使用从外部库以Vector3d. 我不担心轮换。

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

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

现在我很难将此翻译应用于我的m_verticeswhich is a MatrixXd3 by N其中3代表x, y, zN代表顶点的列。

转换矩阵最终看起来像这样(Xs 表示转换的平移部分):

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

基于此,我很确定到目前为止我已经掌握了一切。

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

标签: c++matrixgeometryeigen

解决方案


如果您只需要翻译,则不需要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

推荐阅读