在Eigen中显示仿射变换

问题描述 投票:2回答:2

我想做一些简单的事情:

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

其中e的类型为Eigen::Affine3d。但是,我得到了无益的错误消息,如:

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

其原因有助于解释here,但答案不适用。

official documentation是简洁的,暗示只有Affine3d和Affine3f对象是矩阵。然而,std::cout可以打印特征矩阵和向量而没有问题。那么问题是什么?

c++ eigen cout eigen3
2个回答
3
投票

令人讨厌的是,没有为<<对象定义Affine运算符。你必须调用matrix()函数来获得可打印的表示:

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

如果你不是同质矩阵的粉丝:

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;

希望有人可以节省几分钟的烦恼。

PS:Another lonely SO question顺便提到了这个解决方案。


2
投票

说实话,我宁愿重载流操作符。这使得重复使用更加方便。你可以这样做

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

请注意,l未初始化

© www.soinside.com 2019 - 2024. All rights reserved.