如何创建 Eigen::Ref 的向量

how to create a vector of Eigen::Ref

我想在 Eigen::MatrixXd 中引用不连续的行。这将作为函数中的参数传递,而不更改 MatrixXd(主要行)值。 因为我总是需要 select 某些行来传递给这个函数, 我想我可以使用对 selected 行的引用向量。

但即使创建这个向量似乎也是不可能的:points 有 P.rows() 行,但每一行都是相同的,即 P.

中的最后一行

你能告诉我为什么会发生这种情况以及如何解决它吗?

typedef Eigen::Ref<const Eigen::RowVector3d> OctreePoint;
typedef std::vector<OctreePoint> OctreePoints;

Eigen::MatrixXd P;
// load P from some file
OctreePoints points; 
for (int i = 0; i < P.rows(); ++i)
    {
            // OctreePoint p = P.row(i);
        points.push_back(P.row(i));
        // std::cout << p << std::endl;
     }
std::cout << points << std::endl;

这里的主要问题: P.row(i) 将有一个内在的步伐,因为 P 是(与您的假设相反)专栏专业。这使得每个 Eigen::Ref 包含一个临时文件,其中包含该行的副本(即,它不是实际引用)。

这里基本上有两个选择:

  1. 使用 Eigen::Ref<const Eigen::RowVector3d, 0, Eigen::InnerStride<> > 获取实际参考。
  2. 使用 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> P;
  3. 使 P 行为主

这是一个使用 1 的(non-const)变体的示例:

    typedef Eigen::Ref<Eigen::RowVector3d, 0, Eigen::InnerStride<> > OctreePoint;
    typedef std::vector<OctreePoint> OctreePoints;

    // Alternatively, use this for P:
    // Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> P;
    Eigen::MatrixXd P;
    P.setRandom(3,3);
    std::cout << P << " @ " << P.data() << "\n\n";
    OctreePoints points;
    points.reserve(1);
    for (int i = 0; i < P.rows(); ++i)
    {
        points.push_back(P.row(i));
    }
    points[0][0] = 42.0; // Modify an element of `points` for testing purposes

    for(auto p : points ) std::cout << p << " @ " << p.data() << '\n';

    std::cout << '\n' << P << '\n';

这会生成类似于以下输出的内容:

 0.680375   0.59688 -0.329554
-0.211234  0.823295  0.536459
 0.566198 -0.604897 -0.444451 @ 0x25b8c20

       42   0.59688 -0.329554 @ 0x25b8c20
-0.211234  0.823295  0.536459 @ 0x25b8c28
 0.566198 -0.604897 -0.444451 @ 0x25b8c30

       42   0.59688 -0.329554
-0.211234  0.823295  0.536459
 0.566198 -0.604897 -0.444451

一般来说,我会非常谨慎地将 non-copyable 成员存储到 std::vector——只要你只是 push_back(或更好的 emplace_back),一切都应该没事的。如果您开始在矢量内移动元素,编译将失败或可能导致奇怪的结果。