'How to create a transformation matrix from a scale and a translation, using Eigen3?
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.
Solution 1:[1]
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>();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | chtz |
