Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen::Matrix<double,4,4> to Eigen::Quaterniond

Tags:

c++

matrix

eigen3

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());
like image 617
sumit kumar Avatar asked Dec 18 '25 07:12

sumit kumar


1 Answers

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.

like image 51
wychmaster Avatar answered Dec 20 '25 21:12

wychmaster