Multiplying Many Vectors by Matrix in C++ Using Eigen

  array-broadcasting, c++, eigen, matrix, vector

The problem is simple, I have matrix Nx3 where each row represents a 3D point. I want to rotate each of those points using rotation matrix (3×3) R.
Based on similar questions I tried the following code with no success.

Eigen::MatrixXf GlobPos;
Eigen::MatrixXf LocPos;
LocPos <<  1,  2,  3,
           4,  5,  6,
           7,  8,  9,
          10, 11, 12;
Eigen::Matrix3f R;
rotMat <<  0, 0, -1,
          -1, 0,  0,
           0, 1,  0;

GlobPos = R * LocPos.array().rowwise();
// expected result
-3 -1 2
-6 -4 5
-9 -7 8
-12 -10 11

What would be the best way to approach this problem using Eigen? I know I can always use for loops but I am sure there is a more elegant and faster solution.

Source: Windows Questions C++

LEAVE A COMMENT