Search code examples
c++eigencouteigen3

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?


Solution

  • 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.