在特征中显示仿射变换



我正在尝试做一些简单的事情,例如:

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

其中e属于Eigen::Affine3d类型。但是,我收到无用的错误消息,例如:

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

这里解释了其原因,但答案不适用。

官方文档很简洁,只暗示 Affine3d 和 Affine3f 对象是矩阵。特征矩阵和向量可以通过std::cout打印而不会出现问题。那么问题出在哪里呢?

令人讨厌的是,<<运算符没有为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:另一个孤独的SO问题顺便提到了这个解决方案。

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

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 未初始化

最新更新