in glm_quat_from_vecs() - is computation of cos_half_theta correct?
nirodho opened this issue · comments
Maybe I'm missing an optimization here, but the half-angle formula for cosine is:
cos(theta / 2) = sqrt((1 + cos(theta)) / 2)
the code states:
cos(theta / 2) = 1 + cos(theta)
The change results in a different output versor.
void glm_quat_from_vecs(vec3 a, vec3 b, versor dest) {
CGLM_ALIGN(8) vec3 axis;
float cos_theta;
float cos_half_theta;
cos_theta = glm_vec3_dot(a, b);
if (cos_theta >= 1.f - GLM_FLT_EPSILON) { /* a ∥ b */
glm_quat_identity(dest);
return;
}
if (cos_theta < -1.f + GLM_FLT_EPSILON) { /* angle(a, b) = π */
glm_vec3_ortho(a, axis);
cos_half_theta = 0.f; /* cos π/2 */
} else {
glm_vec3_cross(a, b, axis);
cos_half_theta = 1.0f + cos_theta; /* cos 0 + cos θ */
// shouldn't previous line be:
// cos_half_theta = sqrtf(0.5f * (1.0f + cos_theta));
}
glm_quat_init(dest, axis[0], axis[1], axis[2], cos_half_theta);
glm_quat_normalize(dest);
}
After much head-scratching, I've come to understand that I don't understand quaternions quite as well as I thought I did. I see that there IS a clever optimization here, but have yet to figure it out.
PS: I love this library.