使 Eigen::Vector 看起来像点向量

发布于 2024-12-28 18:36:17 字数 545 浏览 2 评论 0原文

我想以一种可以像点向量一样进行交互的方式来表示 2D 形状,特别是我希望能够调用运算符[]at () 并返回对类似于 2D 点的事物的引用。目前,我只使用一个类,其唯一的成员变量是点向量,并且在其元素上逐点定义了各种算术和几何运算。

然而,在我的代码的其他部分,我需要将 n 个点的向量视为 2n 维空间的元素,并对其执行基本的线性代数(例如,将向量投影到 R^2n 的给定子空间上)。目前,我每次想要执行此操作时都会创建一个 Eigen::VectorXd 对象,然后在执行这些操作后转换回来。我不想这样做,因为我经常进行转换,以至于所有复制都是效率低下的明显根源。

如果我将数据存储为双精度/浮点/整数的平面数组,我可以将指向其第 n 个元素的指针转换为指向 Point 的指针(其成员只是一对双精度/浮点/整数)。然而,由于我不知道 Eigen 用于向量的内部表示(并且它很可能会改变),所以这是不可能的。

有没有明智的方法来解决这个问题?我可以在任何地方使用 Eigen::Vector,但我真的希望大部分代码能够假装它正在处理一组点。

I want to represent a 2D shape in such a way that it can be interacted with as if it were a vector of points, in particular I want to be able to call operator[] and at() on it and return references to things that act like 2D points. Currently I just use a class whose only member variable is a vector of points and that has various arithmetic and geometric operations defined pointwise on its elements.

However, in other parts of my code I need to treat a vector of n points as an element of 2n dimensional space and perform basic linear algebra on it (e.g. projecting the vector onto a given subspace of R^2n). Currently I'm creating an Eigen::VectorXd object every time I want to do this, and then converting back after performing these operations. I don't want to do this, as I make the conversion often enough that all the copying is a noticeable source of inefficiency.

If I was storing the data as a flat array of doubles/floats/ints, I could cast a pointer to its nth element to a pointer to a Point (whose members would just be a pair of doubles/floats/ints). However, as I don't know the internal representation that Eigen uses for vectors (and it may well change), this isn't possible.

Is there a sensible way of solving this? I could just use Eigen::Vectors everywhere, but I really want most of the code to be able to pretend that it is dealing with a set of points.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2025-01-04 18:36:17

但是,由于我不知道 Eigen 用于向量的内部表示(并且它很可能会改变),所以这是不可能的。

Eigen 提供了 Map 类,允许将普通数组映射到 Eigen 结构。例如:

double numbers[2];
Eigen::Vector2f::Map( numbers ).dot( Eigen::Vector2f::Constant(1) );

However, as I don't know the internal representation that Eigen uses for vectors (and it may well change), this isn't possible.

Eigen offers the Map classes that allow mapping plain arrays to Eigen structures. For example:

double numbers[2];
Eigen::Vector2f::Map( numbers ).dot( Eigen::Vector2f::Constant(1) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文