Alexandre Severino
Alexandre Severino

Reputation: 1613

Apply translation to Eigen vertices

I'm trying to displace a geometry with Eigen using the translation info received from external library in form of a Vector3d. I'm not worried about rotation.

I have tried everything that my limited knowledge of Eigen allowed. My current code looks like this:

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

Now I'm having a really hard time applying this translation to my m_vertices which is a MatrixXd, 3 by N columns where the 3 represents x, y, z and the N represents the vertices.

The transformation matrix ends up looking like this (the Xs representing the translation part of the transformation):

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

Based on that, I'm pretty sure I've got everything right up to this point.

I have made a number of attempts of applying the translation, but the ones that did compile crashed on runtime.

Upvotes: 0

Views: 1923

Answers (1)

user6764549
user6764549

Reputation:

If you only need translations, you don't need a Transform object. You can simply do a vector addition of the translation vector to each position vector in your mesh that you need to displace. If you need both a rotation and a translation then you need to apply those separately as

Output = Rotation*Input + Translation

The following code demonstrates that both the approaches are equivalent.

#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;
}

This produces output like follows:

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

Upvotes: 2

Related Questions