3
0

EigenanalysisUtilities.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <cmath>
  9. #include <AzCore/std/algorithm.h>
  10. #include <LinearAlgebra.h>
  11. #include <Eigenanalysis/Utilities.h>
  12. namespace NumericalMethods::Eigenanalysis
  13. {
  14. VectorVariable CrossProduct(const VectorVariable& lhs, const VectorVariable& rhs)
  15. {
  16. AZ_Assert(
  17. lhs.GetDimension() == 3 && rhs.GetDimension() == 3, "VectorVariable dimensions invalid for cross product."
  18. );
  19. return VectorVariable::CreateFromVector({
  20. lhs[1] * rhs[2] - lhs[2] * rhs[1],
  21. lhs[2] * rhs[0] - lhs[0] * rhs[2],
  22. lhs[0] * rhs[1] - lhs[1] * rhs[0]
  23. });
  24. }
  25. void ComputeOrthogonalComplement(
  26. const VectorVariable& vecW, VectorVariable& vecU, VectorVariable& vecV
  27. )
  28. {
  29. // Robustly computes a right-handed orthogonal basis {vecU, vecV, vecW}.
  30. double invLength = 1.0;
  31. if (fabs(vecW[0]) > fabs(vecW[1]))
  32. {
  33. // The component of maximum absolute value is either vecW[0] or vecW[2].
  34. invLength /= sqrt(vecW[0] * vecW[0] + vecW[2] * vecW[2]);
  35. vecU = VectorVariable::CreateFromVector({ -vecW[2] * invLength, 0.0, vecW[0] * invLength });
  36. }
  37. else
  38. {
  39. // The component of maximum absolute value is either vecW[1] or vecW[2].
  40. invLength /= sqrt(vecW[1] * vecW[1] + vecW[2] * vecW[2]);
  41. vecU = VectorVariable::CreateFromVector({ 0.0, vecW[2] * invLength, -vecW[1] * invLength });
  42. }
  43. vecV = CrossProduct(vecW, vecU);
  44. }
  45. VectorVariable ComputeEigenvector0(
  46. double a00, double a01, double a02, double a11, double a12, double a22, double val
  47. )
  48. {
  49. // By definition, (A-e*I)v = 0, where e is the eigenvalue and v is the corresponding eigenvector to be found.
  50. // This condition implies that the rows (A-e*I) must be perpendicular to v. This matrix must have rank 2, so two
  51. // rows will be linearly dependent. For those two rows, the cross product will be (nearly) zero. So to find v,
  52. // we can simply take the cross product of the two rows that maximize its magnitude.
  53. VectorVariable row0 = VectorVariable::CreateFromVector({ a00 - val, a01, a02 });
  54. VectorVariable row1 = VectorVariable::CreateFromVector({ a01, a11 - val, a12 });
  55. VectorVariable row2 = VectorVariable::CreateFromVector({ a02, a12, a22 - val });
  56. VectorVariable r0xr1 = CrossProduct(row0, row1);
  57. VectorVariable r0xr2 = CrossProduct(row0, row2);
  58. VectorVariable r1xr2 = CrossProduct(row1, row2);
  59. double d0 = r0xr1.Dot(r0xr1);
  60. double d1 = r0xr2.Dot(r0xr2);
  61. double d2 = r1xr2.Dot(r1xr2);
  62. return d0 >= d1 && d0 >= d2 ? r0xr1 * (1.0 / sqrt(d0)) :
  63. d1 >= d0 && d1 >= d2 ? r0xr2 * (1.0 / sqrt(d1)) :
  64. r1xr2 * (1.0 / sqrt(d2)) ;
  65. }
  66. VectorVariable ComputeEigenvector1(
  67. double a00,
  68. double a01,
  69. double a02,
  70. double a11,
  71. double a12,
  72. double a22,
  73. double val,
  74. const VectorVariable& vec
  75. )
  76. {
  77. // Real symmetric matrices must have orthogonal eigenvectors. Thus, if we generate two vectors vecU and vecV
  78. // orthogonal to the eigenvector vec already found, the remaining eigenvectors must be a circular combination
  79. // of vecU and vecW. This reduces the problem to a 2D system. For details see Eberly.
  80. VectorVariable vecU(3);
  81. VectorVariable vecV(3);
  82. ComputeOrthogonalComplement(vec, vecU, vecV);
  83. MatrixVariable matA(3, 3);
  84. matA.Element(0, 0) = a00;
  85. matA.Element(0, 1) = a01;
  86. matA.Element(0, 2) = a02;
  87. matA.Element(1, 0) = a01;
  88. matA.Element(1, 1) = a11;
  89. matA.Element(1, 2) = a12;
  90. matA.Element(2, 0) = a02;
  91. matA.Element(2, 1) = a12;
  92. matA.Element(2, 2) = a22;
  93. double m00 = vecU.Dot(matA * vecU) - val;
  94. double absM00 = fabs(m00);
  95. double m01 = vecU.Dot(matA * vecV);
  96. double absM01 = fabs(m01);
  97. double m11 = vecV.Dot(matA * vecV) - val;
  98. double absM11 = fabs(m11);
  99. auto discardComponentAndNormalize = [](double& factor, double& other) {
  100. other /= factor;
  101. factor = 1.0 / sqrt(1.0 + other * other);
  102. other *= factor;
  103. };
  104. if (absM00 > absM11)
  105. {
  106. if (AZStd::max(absM00, absM01) > 0.0)
  107. {
  108. if (absM00 >= absM01)
  109. {
  110. discardComponentAndNormalize(m00, m01);
  111. }
  112. else
  113. {
  114. discardComponentAndNormalize(m01, m00);
  115. }
  116. return vecU * m01 - vecV * m00;
  117. }
  118. else
  119. {
  120. return vecU;
  121. }
  122. }
  123. else
  124. {
  125. if (AZStd::max(absM11, absM01) > 0.0)
  126. {
  127. if (absM11 >= absM01)
  128. {
  129. discardComponentAndNormalize(m11, m01);
  130. }
  131. else
  132. {
  133. discardComponentAndNormalize(m01, m11);
  134. }
  135. return vecU * m11 - vecV * m01;
  136. }
  137. else
  138. {
  139. return vecU;
  140. }
  141. }
  142. }
  143. VectorVariable ComputeEigenvector2(const VectorVariable& vec0, const VectorVariable& vec1)
  144. {
  145. return CrossProduct(vec0, vec1);
  146. }
  147. } // namespace NumericalMethods::Eigenanalysis