Search code examples
c++eigen

Eigen error : INVALID_MATRIX_PRODUCT multiplying vector and matrix


im working with c++ and eigen. I try to implement matrix rotation but i get this error Eigen\src/Core/GeneralProduct.h(410,3): error C2338: INVALID_MATRIX_PRODUCT

    Eigen::Vector3f box_pos = Eigen::Vector3f(-2.0f, 2.5f, -2.0f);
    box_pos *= RotY(5.0f);

RotY function :

   Eigen::Matrix3f RotY(float angle) {
    float s = sin(angle);
    float c = cos(angle);
    Eigen::Matrix3f matrix;
    matrix << c, 0., s, 0., 1., 0., -s, 0., c;
    return matrix;
}

Solution

  • box_pos is a column vector. If you write box_pos *= RotY(5.0f) you multiply the matrix from the right side and the inner dimensions (1 and 3) do not match.

    You either have to transpose box_pos and work with a row vector or rewrite the multiplication as

    box_pos = RotY(5.0f) * box_pos;