In tutorials regarding the MD5Mesh format, like this one, we often see code like
float t = 1.0f - (q.x * q.x) - (q.y * q.y) - (q.z * q.z);
if (t < 0.0f) {
q.w = 0.0f;
} else {
q.w = -sqrt (t);
}
for computing the w-component of a unit quaternion given x, y, z
.
Why do we obtain w in this manner?
w = ± sqrt(1 - x^2 - y^2 - z^2)
, but I am shaky on the if-statement logic. Doest < 0
occur often in practice? What I am trying to figure out is if we are mitigating the effects of bad input by settingw
to a reasonable value of 0 (I am new to quaternions, so forgive my greenness). – pcapp May 03 '13 at 02:18NaN
when rounding causes t to be something like-1E-9
. – Peter Taylor May 03 '13 at 08:19