c++eigen

Shorter way to apply transform to matrix containing vectors in Eigen?


Is there a shorter way to apply the transform?

auto vecs = Matrix<float, Dynamic, 3>(); //vector in each row.
Affine3f transform = ...; // some Affine3 transform

for (int r = 0; r < vecs.rows(); r++) {
    Vector3f v = vecs.row(r);
    v = transform * v;
    vecs.row(r) = v;
}

Solution

  • @JE42 is right, for loops are almost never necessary with Eigen. It is certainly possible to simplify. However, note that transform * v in your case is not a simple multiplication but an affine transformation equivalent to transform.linear() * v + transform.translation(). https://eigen.tuxfamily.org/dox-devel/group__TutorialGeometry.html

    Also, your "points" are rows, so additional transposes are needed.

    In sum, (without actually compiling or testing it) your calculation should be equivalent to

    vecs.transpose() = (transform.linear() * vecs.transpose()).array() + transform.translation();