Game Development Reference
In-Depth Information
Listing 16.7. The CalculateEigensystem() function calculates the eigenvalues and eigen-
vectors of a 3
×
3
symmetric matrix.
Parameters
×
3
m
The 3
matrix for which eigenvalues and eigenvectors are to be calculated. This
matrix must be symmetric.
lambda
A pointer to an array where the three eigenvalues are to be returned.
A 3
matrix whose columns contain the eigenvectors upon return. The i -th
column corresponds to the i -th eigenvalue returned in the lambda array.
×
3
r
const float epsilon = 1.0e-10F;
const int maxSweeps = 32;
void CalculateEigensystem( const Matrix3D& m, float *lambda, Matrix3D& r)
{
float m11 = m(0,0);
float m12 = m(0,1);
float m13 = m(0,2);
float m22 = m(1,1);
float m23 = m(1,2);
float m33 = m(2,2);
r.SetIdentity();
for ( int a = 0; a < maxSweeps; a++)
{
// Exit if off-diagonal entries small enough.
if ((Fabs(m12) < epsilon) && (Fabs(m13) < epsilon) &&
(Fabs(m23) < epsilon)) break ;
// Annihilate (1,2) entry.
if (m12 != 0.0F)
{
float u = (m22 - m11) * 0.5F / m12;
float u2 = u * u;
float u2p1 = u2 + 1.0F;
float t = (u2p1 != u2) ?
((u < 0.0F) ? -1.0F : 1.0F) * (sqrt(u2p1) - fabs(u))
: 0.5F / u;
float c = 1.0F / sqrt(t * t + 1.0F);
float s = c * t;