1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2011/07/23)
#include "Wm5MathematicsPCH.h"
#include "Wm5NoniterativeEigen3x3.h"
#include "Wm5Assert.h"
namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
NoniterativeEigen3x3<Real>::NoniterativeEigen3x3 (const Matrix3<Real>& A)
{
// Scale the matrix so its entries are in [-1,1]. The scaling is applied
// only when at least one matrix entry has magnitude larger than 1.
Matrix3<Real> AScaled = A;
Real* scaledEntry = (Real*)AScaled;
Real maxValue = Math<Real>::FAbs(scaledEntry[0]);
Real absValue = Math<Real>::FAbs(scaledEntry[1]);
if (absValue > maxValue)
{
maxValue = absValue;
}
absValue = Math<Real>::FAbs(scaledEntry[2]);
if (absValue > maxValue)
{
maxValue = absValue;
}
absValue = Math<Real>::FAbs(scaledEntry[4]);
if (absValue > maxValue)
{
maxValue = absValue;
}
absValue = Math<Real>::FAbs(scaledEntry[5]);
if (absValue > maxValue)
{
maxValue = absValue;
}
absValue = Math<Real>::FAbs(scaledEntry[8]);
if (absValue > maxValue)
{
maxValue = absValue;
}
int i;
if (maxValue > (Real)1)
{
Real invMaxValue = ((Real)1)/maxValue;
for (i = 0; i < 9; ++i)
{
scaledEntry[i] *= invMaxValue;
}
}
// Compute the eigenvalues using double-precision arithmetic.
double root[3];
ComputeRoots(AScaled,root);
mEigenvalue[0] = (Real)root[0];
mEigenvalue[1] = (Real)root[1];
mEigenvalue[2] = (Real)root[2];
Real maxEntry[3];
Vector3<Real> maxRow[3];
for (i = 0; i < 3; ++i)
{
Matrix3<Real> M = AScaled;
M[0][0] -= mEigenvalue[i];
M[1][1] -= mEigenvalue[i];
M[2][2] -= mEigenvalue[i];
if (!PositiveRank(M, maxEntry[i], maxRow[i]))
{
// Rescale back to the original size.
if (maxValue > (Real)1)
{
for (int j = 0; j < 3; ++j)
{
mEigenvalue[j] *= maxValue;
}
}
mEigenvector[0] = Vector3<Real>::UNIT_X;
mEigenvector[1] = Vector3<Real>::UNIT_Y;
mEigenvector[2] = Vector3<Real>::UNIT_Z;
return;
}
}
Real totalMax = maxEntry[0];
i = 0;
if (maxEntry[1] > totalMax)
{
totalMax = maxEntry[1];
i = 1;
}
if (maxEntry[2] > totalMax)
{
i = 2;
}
if (i == 0)
{
maxRow[0].Normalize();
ComputeVectors(AScaled, maxRow[0], 1, 2, 0);
}
else if (i == 1)
{
maxRow[1].Normalize();
ComputeVectors(AScaled, maxRow[1], 2, 0, 1);
}
else
{
maxRow[2].Normalize();
ComputeVectors(AScaled, maxRow[2], 0, 1, 2);
}
// Rescale back to the original size.
if (maxValue > (Real)1)
{
for (i = 0; i < 3; ++i)
{
mEigenvalue[i] *= maxValue;
}
}
}
//----------------------------------------------------------------------------
template <typename Real>
NoniterativeEigen3x3<Real>::~NoniterativeEigen3x3 ()
{
}
//----------------------------------------------------------------------------
template <typename Real>
const Real NoniterativeEigen3x3<Real>::GetEigenvalue (int i) const
{
assertion(0 <= i && i < 3, "Invalid index\n");
return mEigenvalue[i];
}
//----------------------------------------------------------------------------
template <typename Real>
const Real* NoniterativeEigen3x3<Real>::GetEigenvalues () const
{
return mEigenvalue;
}
//----------------------------------------------------------------------------
template <typename Real>
const Vector3<Real>& NoniterativeEigen3x3<Real>::GetEigenvector(
int i) const
{
assertion(0 <= i && i < 3, "Invalid index\n");
return mEigenvector[i];
}
//----------------------------------------------------------------------------
template <typename Real>
const Vector3<Real>* NoniterativeEigen3x3<Real>::GetEigenvectors () const
{
return mEigenvector;
}
//----------------------------------------------------------------------------
template <typename Real>
void NoniterativeEigen3x3<Real>::ComputeRoots (const Matrix3<Real>& A,
double root[3])
{
// Convert the unique matrix entries to double precision.
double a00 = (double)A[0][0];
double a01 = (double)A[0][1];
double a02 = (double)A[0][2];
double a11 = (double)A[1][1];
double a12 = (double)A[1][2];
double a22 = (double)A[2][2];
// The characteristic equation is x^3 - c2*x^2 + c1*x - c0 = 0. The
// eigenvalues are the roots to this equation, all guaranteed to be
// real-valued, because the matrix is symmetric.
double c0 = a00*a11*a22 + 2.0*a01*a02*a12 - a00*a12*a12 -
a11*a02*a02 - a22*a01*a01;
double c1 = a00*a11 - a01*a01 + a00*a22 - a02*a02 +
a11*a22 - a12*a12;
double c2 = a00 + a11 + a22;
// Construct the parameters used in classifying the roots of the equation
// and in solving the equation for the roots in closed form.
double c2Div3 = c2*msInv3;
double aDiv3 = (c1 - c2*c2Div3)*msInv3;
if (aDiv3 > 0.0)
{
aDiv3 = 0.0;
}
double halfMB = 0.5*(c0 + c2Div3*(2.0*c2Div3*c2Div3 - c1));
double q = halfMB*halfMB + aDiv3*aDiv3*aDiv3;
if (q > 0.0)
{
q = 0.0;
}
// Compute the eigenvalues by solving for the roots of the polynomial.
double magnitude = Mathd::Sqrt(-aDiv3);
double angle = Mathd::ATan2(Mathd::Sqrt(-q), halfMB)*msInv3;
double cs = Mathd::Cos(angle);
double sn = Mathd::Sin(angle);
double root0 = c2Div3 + 2.0*magnitude*cs;
double root1 = c2Div3 - magnitude*(cs + msRoot3*sn);
double root2 = c2Div3 - magnitude*(cs - msRoot3*sn);
// Sort in increasing order.
if (root1 >= root0)
{
root[0] = root0;
root[1] = root1;
}
else
{
root[0] = root1;
root[1] = root0;
}
if (root2 >= root[1])
{
root[2] = root2;
}
else
{
root[2] = root[1];
if (root2 >= root[0])
{
root[1] = root2;
}
else
{
root[1] = root[0];
root[0] = root2;
}
}
}
//----------------------------------------------------------------------------
template <typename Real>
bool NoniterativeEigen3x3<Real>::PositiveRank (Matrix3<Real>& M,
Real& maxEntry, Vector3<Real>& maxRow) const
{
// Locate the maximum-magnitude entry of the matrix.
maxEntry = (Real)-1;
int row, maxRowIndex = -1;
for (row = 0; row < 3; ++row)
{
for (int col = row; col < 3; ++col)
{
Real absValue = Math<Real>::FAbs(M[row][col]);
if (absValue > maxEntry)
{
maxEntry = absValue;
maxRowIndex = row;
}
}
}
// Return the row containing the maximum, to be used for eigenvector
// construction.
maxRow = M.GetRow(maxRowIndex);
return maxEntry >= Math<Real>::ZERO_TOLERANCE;
}
//----------------------------------------------------------------------------
template <typename Real>
void NoniterativeEigen3x3<Real>::ComputeVectors (const Matrix3<Real>& A,
Vector3<Real>& U2, int i0, int i1, int i2)
{
Vector3<Real> U0, U1;
Vector3<Real>::GenerateComplementBasis(U0, U1, U2);
// V[i2] = c0*U0 + c1*U1, c0^2 + c1^2=1
// e2*V[i2] = c0*A*U0 + c1*A*U1
// e2*c0 = c0*U0.Dot(A*U0) + c1*U0.Dot(A*U1) = d00*c0 + d01*c1
// e2*c1 = c0*U1.Dot(A*U0) + c1*U1.Dot(A*U1) = d01*c0 + d11*c1
Vector3<Real> tmp = A*U0;
Real p00 = mEigenvalue[i2] - U0.Dot(tmp);
Real p01 = U1.Dot(tmp);
Real p11 = mEigenvalue[i2] - U1.Dot(A*U1);
Real invLength;
Real maxValue = Math<Real>::FAbs(p00);
int row = 0;
Real absValue = Math<Real>::FAbs(p01);
if (absValue > maxValue)
{
maxValue = absValue;
}
absValue = Math<Real>::FAbs(p11);
if (absValue > maxValue)
{
maxValue = absValue;
row = 1;
}
if (maxValue >= Math<Real>::ZERO_TOLERANCE)
{
if (row == 0)
{
invLength = Math<Real>::InvSqrt(p00*p00 + p01*p01);
p00 *= invLength;
p01 *= invLength;
mEigenvector[i2] = p01*U0 + p00*U1;
}
else
{
invLength = Math<Real>::InvSqrt(p11*p11 + p01*p01);
p11 *= invLength;
p01 *= invLength;
mEigenvector[i2] = p11*U0 + p01*U1;
}
}
else
{
if (row == 0)
{
mEigenvector[i2] = U1;
}
else
{
mEigenvector[i2] = U0;
}
}
// V[i0] = c0*U2 + c1*Cross(U2,V[i2]) = c0*R + c1*S
// e0*V[i0] = c0*A*R + c1*A*S
// e0*c0 = c0*R.Dot(A*R) + c1*R.Dot(A*S) = d00*c0 + d01*c1
// e0*c1 = c0*S.Dot(A*R) + c1*S.Dot(A*S) = d01*c0 + d11*c1
Vector3<Real> S = U2.Cross(mEigenvector[i2]);
tmp = A*U2;
p00 = mEigenvalue[i0] - U2.Dot(tmp);
p01 = S.Dot(tmp);
p11 = mEigenvalue[i0] - S.Dot(A*S);
maxValue = Math<Real>::FAbs(p00);
row = 0;
absValue = Math<Real>::FAbs(p01);
if (absValue > maxValue)
{
maxValue = absValue;
}
absValue = Math<Real>::FAbs(p11);
if (absValue > maxValue)
{
maxValue = absValue;
row = 1;
}
if (maxValue >= Math<Real>::ZERO_TOLERANCE)
{
if (row == 0)
{
invLength = Math<Real>::InvSqrt(p00*p00 + p01*p01);
p00 *= invLength;
p01 *= invLength;
mEigenvector[i0] = p01*U2 + p00*S;
}
else
{
invLength = Math<Real>::InvSqrt(p11*p11 + p01*p01);
p11 *= invLength;
p01 *= invLength;
mEigenvector[i0] = p11*U2 + p01*S;
}
}
else
{
if (row == 0)
{
mEigenvector[i0] = S;
}
else
{
mEigenvector[i0] = U2;
}
}
// V[i1] = Cross(V[i2],V[i0])
mEigenvector[i1] = mEigenvector[i2].Cross(mEigenvector[i0]);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template<> const double NoniterativeEigen3x3<float>::msInv3 = 1.0/3.0;
template<> const double NoniterativeEigen3x3<float>::msRoot3 =
Mathd::Sqrt(3.0);
template WM5_MATHEMATICS_ITEM
class NoniterativeEigen3x3<float>;
template<> const double NoniterativeEigen3x3<double>::msInv3 = 1.0/3.0;
template<> const double NoniterativeEigen3x3<double>::msRoot3 =
Mathd::Sqrt(3.0);
template WM5_MATHEMATICS_ITEM
class NoniterativeEigen3x3<double>;
//----------------------------------------------------------------------------
}
|