Displaying an affine transformation in Eigen
Asked Answered
H

2

6

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?

Hyksos answered 10/9, 2018 at 10:28 Comment(1)
Please include entire error messageAlmira
H
14

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.

Hyksos answered 10/9, 2018 at 10:28 Comment(0)
M
4

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

Musser answered 10/9, 2018 at 12:34 Comment(1)
This is the neater way that should be in Eigen out of the box in my opinion.Hyksos

© 2022 - 2024 — McMap. All rights reserved.