如何将 affine2d 变换应用于 3d 矩阵块?

How to apply affine2d transform onto a block of 3d matrix?

我有一个矩阵(x、y、z 每列),只想在 x 和 y 轴上执行 2D 变换,忽略 z。 affine2d 好像不能和 block 相乘,请问有没有其他方法可以实现?

Eigen::matrix<double, 3, 4> x3d;
x3d <<
  1, 2, 3, 4,
  2, 3, 4, 5,
  1, 1, 1, 1;
auto x2d = x3d.topRows(2);
Eigen::Affine2d T = Eigen::Translation2d(1, 2) * Eigen::Scaling(1., 2.);
x2d = T*x2d;

输出错误:

> /home/lei/Work/SurfTomo/./include/Eigen/src/Geometry/Transform.h:1361:5:
> error: static_assert failed "YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES"
>     EIGEN_STATIC_ASSERT(OtherRows==Dim, YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES);
>     ^                   ~~~~~~~~~~~~~~ /home/lei/Work/SurfTomo/./include/Eigen/src/Core/util/StaticAssert.h:32:40:
> note: expanded from macro 'EIGEN_STATIC_ASSERT'
>     #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
>                                        ^             ~ /home/lei/Work/SurfTomo/./include/Eigen/src/Geometry/Transform.h:442:77:
> note: in instantiation of member function
>       'Eigen::internal::transform_right_product_impl<Eigen::Transform<double,
> 2, 2, 0>, Eigen::Block<Eigen::Matrix<double, 3, 4, 0, 3, 4>, -1, 4,
>       false>, 2, 4>::run' requested here   { return internal::transform_right_product_impl<Transform,
> OtherDerived>::run(*this,other.derived()); }
>                                                                             ^ /home/lei/Work/SurfTomo/test/test_3dto2d.cc:27:10: note: in
> instantiation of function template specialization
> 'Eigen::Transform<double, 2, 2,
>       0>::operator*<Eigen::Block<Eigen::Matrix<double, 3, 4, 0, 3, 4>, -1, 4, false> >' requested here   x2d = T*x2d;
>          ^ 1 error generated.

我发现下面的代码可以解决这个问题,但我不明白为什么会这样。

x2d = T * x2d.colwise().homogeneous();

Eigen::Affine2d相乘时,Eigen想在编译时知道另一个矩阵的行数(因为依赖于此,采用不同的代码路径,并且运行-时间检查在这里会产生很大的开销。

只需使用

创建您的 x2d 矩阵
auto x2d = x3d.topRows<2>();