-
Notifications
You must be signed in to change notification settings - Fork 144
Open
Description
Unfortunately, the function doesn't give correct results.
Here's a proposal to change it (taken from https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm):
float tr = M[0][0] + M[1][1] + M[2][2];
if (tr > 0) {
float S = sqrt(tr+1.0) * 2; // S=4*qw
q[3] = 0.25 * S;
q[0] = (M[1][2] - M[2][1]) / S;
q[1] = (M[2][0] - M[0][2]) / S;
q[2] = (M[0][1] - M[1][0]) / S;
} else if ((M[0][0] > M[1][1])&(M[0][0] > M[2][2])) {
float S = sqrt(1.0 + M[0][0] - M[1][1] - M[2][2]) * 2; // S=4*qx
q[3] = (M[1][2] - M[2][1]) / S;
q[0] = 0.25 * S;
q[1] = (M[1][0] + M[0][1]) / S;
q[2] = (M[2][0] + M[0][2]) / S;
} else if (M[1][1] > M[2][2]) {
float S = sqrt(1.0 + M[1][1] - M[0][0] - M[2][2]) * 2; // S=4*qy
q[3] = (M[2][0] - M[0][2]) / S;
q[0] = (M[1][0] + M[0][1]) / S;
q[1] = 0.25 * S;
q[2] = (M[2][1] + M[1][2]) / S;
} else {
float S = sqrt(1.0 + M[2][2] - M[0][0] - M[1][1]) * 2; // S=4*qz
q[3] = (M[0][1] - M[1][0]) / S;
q[0] = (M[2][0] + M[0][2]) / S;
q[1] = (M[2][1] + M[1][2]) / S;
q[2] = 0.25 * S;
}
qristofer
Metadata
Metadata
Assignees
Labels
No labels