如何使用 Eigen3 从比例和平移创建变换矩阵?

发布于 2025-01-09 13:19:24 字数 1010 浏览 4 评论 0原文

我对如何创建一个简单的变换矩阵感到困惑。

我有以下 C++ 代码:

  static constexpr float pos_scale = 2 * size / iside;
  static constexpr std::array<float, 2> pos_translation = { -size, -size };

不幸的是, pos_translation 需要是 std::array 因为你无法制作 Eigen::Vector2f > constexpr。

然后我有一个向量:

Vector2i xy;

它将填充一些数据。 我想将 xy 转换为 Vector2f,方法是将其转换为齐次坐标,将其与变换矩阵相乘,然后再次删除齐次坐标。

变换矩阵

Matrix3f xy_to_pos;

至少声明为,我认为这是正确使用的类型?

我的问题是,如何从 pos_scale (必须首先应用:首先是比例,然后是平移)和 pos_translation 初始化 xy_to_pos

一旦我有了xy_to_pos,我将如何使用它来将xy转换为Vector2f pos

我尝试了各种方法,

Matrix3f xy_to_pos = Matrix3f::Identity() * Eigen::Scaling(pos_scale) * Eigen::Translation2f(pos_translation[0], pos_translation[1]);

但没有编译任何内容,并且编译错误也没有帮助。当然,我也用谷歌搜索过这个问题,但找不到有帮助的例子。

I am stumped on how to create a simple transformation matrix.

I have the following C++ code:

  static constexpr float pos_scale = 2 * size / iside;
  static constexpr std::array<float, 2> pos_translation = { -size, -size };

Unfortunately, the pos_translation needs to be a std::array because you can't make an Eigen::Vector2f constexpr.

Then I have a vector:

Vector2i xy;

That will be filled with some data.
I want to convert xy to a Vector2f by converting it to homogeneous coordinates, multiplying it with a transformation matrix and then dropping the homogeneous coordinates again.

The transformation matrix is declared as

Matrix3f xy_to_pos;

at least, I think that is the right type to use?

My question is, how can I initialize xy_to_pos from pos_scale (which has to be applied first: first the scale then the translation) and pos_translation?

And once I have xy_to_pos how would I use it to convert xy to Vector2f pos?

I tried all kinds of things, like

Matrix3f xy_to_pos = Matrix3f::Identity() * Eigen::Scaling(pos_scale) * Eigen::Translation2f(pos_translation[0], pos_translation[1]);

but nothing compiles, and the compile errors don't help. I Googled for this too of course, but couldn't find an example that helped.

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

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

发布评论

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

评论(1

荒芜了季节 2025-01-16 13:19:24

我想通了:

static constexpr float pos_scale = 2 * size / iside;
static constexpr std::array<float, 2> pos_translation = { -size, -size };

Transform<float, 2, Affine> const xy_to_pos =
    Transform<float, 2, Affine>{Transform<float, 2, Affine>::Identity()}
    .translate(Vector2f{pos_translation.data()})
    .scale(pos_scale);

Vector2i xy(2, 3); // Or whatever

Vector2f pos = xy_to_pos * xy.cast<float>();


I figured it out:

static constexpr float pos_scale = 2 * size / iside;
static constexpr std::array<float, 2> pos_translation = { -size, -size };

Transform<float, 2, Affine> const xy_to_pos =
    Transform<float, 2, Affine>{Transform<float, 2, Affine>::Identity()}
    .translate(Vector2f{pos_translation.data()})
    .scale(pos_scale);

Vector2i xy(2, 3); // Or whatever

Vector2f pos = xy_to_pos * xy.cast<float>();


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文