Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 3x3 rotation matrix to quaternion function #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chobitsfan
Copy link

@chobitsfan chobitsfan commented Oct 25, 2022

In #90 (comment), @mkrogius suggests using doubles_mat_to_quat to get a quaternion from the matrix. However, rotation matrix in apriltag_pose_t is a 3x3 matrix and doubles_mat_to_quat expects a 4x4 matrix. It would be easier to use if we have a doubles_mat33_to_quat.

@christian-rauch
Copy link
Collaborator

Thank you for the patch. Where is this new function used? I cannot find references to doubles_mat_to_quat. Can you provide more details on why this new function is required?

@chobitsfan
Copy link
Author

Hi @christian-rauch Thank you for reviewing

I cannot find references to doubles_mat_to_quat

Yes. doubles_mat_to_quat is defined but never used. But in #90, @mkrogius suggests it can be used (with doubles_quat_to_rpy) to obtain euler angles from apriltag_pose_t. I found it is quite useful. However, rotation matrix in apriltag_pose_t is a 3x3 matrix and doubles_mat_to_quat expects a 4x4 matrix.

@@ -453,6 +453,40 @@ static inline void TFN(s_mat_to_xyz)(const TNAME M[16], TNAME xyz[3])
xyz[2] = M[11];
}

static inline void TFN(s_mat33_to_quat)(const TNAME M[9], TNAME q[4])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this function "rot_mat_to_quat"? "mat33" sounds as if it accepts any 3x3 matrix. Do you think we need to check that the matrix provided is actually a rotation matrix?

Comment on lines +461 to +485
if (T > 0.0000001) {
S = sqrt(T) * 2;
q[0] = (TNAME)(0.25 * S);
q[1] = (TNAME)((M[7] - M[5]) / S);
q[2] = (TNAME)((M[2] - M[6]) / S);
q[3] = (TNAME)((M[3] - M[1]) / S);
} else if (M[0] > M[4] && M[0] > M[8]) { // Column 0:
S = sqrt(1.0 + M[0] - M[4] - M[8]) * 2;
q[0] = (TNAME)((M[7] - M[5]) / S);
q[1] = (TNAME)(0.25 * S);
q[2] = (TNAME)((M[3] + M[1]) / S);
q[3] = (TNAME)((M[2] + M[6]) / S);
} else if (M[4] > M[8]) { // Column 1:
S = sqrt(1.0 + M[4] - M[0] - M[8]) * 2;
q[0] = (TNAME)((M[2] - M[6]) / S);
q[1] = (TNAME)((M[3] + M[1]) / S);
q[2] = (TNAME)(0.25 * S);
q[3] = (TNAME)((M[7] + M[5]) / S);
} else { // Column 2:
S = sqrt(1.0 + M[8] - M[0] - M[4]);
q[0] = (TNAME)((M[3] - M[1]) / S);
q[1] = (TNAME)((M[2] + M[6]) / S);
q[2] = (TNAME)((M[7] + M[5]) / S);
q[3] = (TNAME)(0.25 * S);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to avoid code duplication with mat_to_quat here. Either have mat_to_quat reuse this very function with a submatrix (the best option IMHO) or extend the 3x3 rotation to 4x4 transformation matrix and reuse mat_to_quat here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants