1
Eigen::Matrix4d transformation_matrix; //assume this is initialized
Eigen::Matrix4d &matrix = transformation_matrix;   
Eigen::Quaterniond quat;
quat(matrix);

I am trying to find a rotation matrix from a transformation matrix. There is already an API in Eigen::Quaterniond library toRotationMatrix. But to use this, I need a 4x4 Quaterniond (Please correct, if this is wrong).

Getting compilation error as

error: no match for call to ‘(Eigen::Quaterniond {aka Eigen::Quaternion<double>}) (const Matrix4d&)’

Please help. Followed this link for conversion

Eigen::Quaterniond quat;
quat(matrix);    

When initializing Quaterniond , I am getting a different error :

Eigen::internal::quaternionbase_assign_impl<Eigen::Matrix<double, 4, 4>, 4, 4>’ used in nested name specifier
   internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived());
wychmaster
  • 712
  • 2
  • 8
  • 23

1 Answers1

2

What you are doing here:

Eigen::Quaterniond quat;
quat(matrix);

is creating a quaternion and then calling it's operator() with a matrix. But as far as I can see, the Quaternion does not even have an operator() for anything: documentation

Take a look at the first answer of ggael in the question you referred to. You can either use the constructor of the quaternion:

Eigen::Quaterniond quat(matrix);

or it's assignment operator:

Eigen::Quaterniond quat;
quat = matrix;

Both are defined for a matrix. However, I can't tell you, if they work for 4x4 matrices. If not extract the 3x3 rotation part of the matrix.

Edit:

After a quick test on my computer, you can't pass a 4x4 matrix to a quaternion. Either use a Eigen::Matrix3d instead or do something like this:

Eigen::Quaterniond quat(matrix.topLeftCorner<3, 3>());

Edit 2:

I am trying to find a rotation matrix from a transformation matrix. There is already an API in Eigen::Quaterniond library toRotationMatrix. But to use this, I need a 4x4 Quaterniond (Please correct, if this is wrong).

Well, this isn't really correct. A rotation matrix can be converted into a quaternion and a quaternion into a rotation matrix. In a 4x4 matrix, the rotation part is contained inside the top-left 3x3 submatrix. However, in a general transformation matrix, this part isn't necessarily only the rotation. It can also contain other transformations like scaling. In this case, I am not sure, if you can transform it directly into a quaternion without extracting the rotational part first. I doubt it, but I am not sure about this. However, if you have a general transformation matrix, you can test if the upper 3x3 part is a pure rotation by calculating its determinant. If it is 1, you have a pure rotation. If this is not the case, take a look at this link to calculate the rotational part. If your goal is to extract just the rotation of a 4x4 matrix, you do not need quaternions at all. Rotation matrices and quaternions are just different representations of the same thing.

wychmaster
  • 712
  • 2
  • 8
  • 23
  • Corrected the initialization. on doing this Eigen::Quaterniond quat; quat(matrix); I am getting a different error Eigen::internal::quaternionbase_assign_impl, 4, 4>’ used in nested name specifier internal::quaternionbase_assign_impl::run(*this, xpr.derived()); – sumit kumar Mar 19 '20 at 14:37
  • Have a look at my edit. The error is related to the fact, that 4x4 matrices are not supported. Extract the rotation part as shown in the edit. – wychmaster Mar 19 '20 at 14:43
  • Watch out, there was a mistake since I wrote my answer from another computer than the one I tested the code. Now the edit should be fine. – wychmaster Mar 19 '20 at 14:49
  • Hi Thanks for your answer . I am able to convert matrix to Quaternion. yes, you are right. I wanted to get rotation matrix but it is with scaling factor due to which I am not getting proper results. The link which you have mentioned it's quite good but I am unable to understand it. It will be great if you can share some other links or code related to this. – sumit kumar Mar 20 '20 at 15:17
  • The answer provided by "imallett" in the link is as simple as it can be. Don't know how to explain it better. I have no clue what you are struggling with, but maybe this link might help you: https://www.brainvoyager.com/bv/doc/UsersGuide/CoordsAndTransforms/SpatialTransformationMatrices.html - it explains how a 4x4 transformation matrix for 3d space is composed and why. After reading and understanding this, revisit the link in my answer. It should be much clearer then. – wychmaster Mar 21 '20 at 13:38