FvD
FvD

Reputation: 1375

Displaying an affine transformation in Eigen

I am trying to do something as simple as:

std::cout << e << std::endl;  

where e is of type Eigen::Affine3d. However, I am getting unhelpful error messages like:

cannot bind 'std::ostream {aka std::basic_ostream<char>}'   
lvalue to 'std::basic_ostream<char>&&'

The reason for which is helpfully explained here, but where the answer does not apply.

The official documentation is curt, implying only that Affine3d and Affine3f objects are matrices. Eigen matrices and vectors can be printed by std::cout without issue though. So what is the problem?

Upvotes: 6

Views: 10090

Answers (2)

user4290866
user4290866

Reputation:

To be honest, I would prefer to overload the stream operator. This makes the repeated use more convinient. This you can do like this

std::ostream& operator<<(std::ostream& stream, const Eigen::Affine3d& affine)
{
    stream << "Rotation: " << std::endl << affine.rotation() << std::endl;
    stream << "Translation: " << std::endl <<  affine.translation() << std::endl;

    return stream;
}

int main()
{

    Eigen::Affine3d l;

    std::cout << l << std::endl;

    return 0;
}

Be aware that l is uninitialized

Upvotes: 4

FvD
FvD

Reputation: 1375

Annoyingly, the << operator is not defined for Affine objects. You have to call the matrix() function to get the printable representation:

std::cout << e.matrix() << std::endl;

If you're not a fan of homogenous matrices:

Eigen::Matrix3d m = e.rotation();
Eigen::Vector3d v = e.translation();
std::cout << "Rotation: " << std::endl << m << std::endl;
std::cout << "Translation: " << std::endl << v << std::endl;

Hopefully someone can save a few minutes of annoyance.

PS:Another lonely SO question mentioned this solution in passing.

Upvotes: 14

Related Questions