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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions common/doubles_floats_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?

{
double T = M[0] + M[4] + M[8] + 1.0;
double S;

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);
}
Comment on lines +461 to +485
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.


TFN(s_normalize)(q, 4, q);
}

static inline void TFN(s_mat_to_quat)(const TNAME M[16], TNAME q[4])
{
double T = M[0] + M[5] + M[10] + 1.0;
Expand Down