3 const Vector3 Vector3::ZERO(0.0f, 0.0f, 0.0f);
10 float Quaternion::Norm()
const 12 return w*w + x*x + y*y + z*z;
15 float Quaternion::normalise(
void)
18 float factor = 1.0f / sqrt(len);
19 *
this = *
this * factor;
25 float fNorm = w*w + x*x + y*y + z*z;
28 float fInvNorm = 1.0f / fNorm;
29 return Quaternion(w*fInvNorm, -x*fInvNorm, -y*fInvNorm, -z*fInvNorm);
37 void Quaternion::FromRotationMatrix(
const Matrix3& kRot)
42 float fTrace = kRot[0][0] + kRot[1][1] + kRot[2][2];
48 fRoot = sqrt(fTrace + 1.0f);
51 x = (kRot[2][1] - kRot[1][2])*fRoot;
52 y = (kRot[0][2] - kRot[2][0])*fRoot;
53 z = (kRot[1][0] - kRot[0][1])*fRoot;
58 static size_t s_iNext[3] = { 1, 2, 0 };
60 if (kRot[1][1] > kRot[0][0])
62 if (kRot[2][2] > kRot[i][i])
64 size_t j = s_iNext[i];
65 size_t k = s_iNext[j];
67 fRoot = sqrt(kRot[i][i] - kRot[j][j] - kRot[k][k] + 1.0f);
68 float* apkQuat[3] = { &x, &y, &z };
69 *apkQuat[i] = 0.5f*fRoot;
71 w = (kRot[k][j] - kRot[j][k])*fRoot;
72 *apkQuat[j] = (kRot[j][i] + kRot[i][j])*fRoot;
73 *apkQuat[k] = (kRot[k][i] + kRot[i][k])*fRoot;