Files
o3de/Gems/PhysX/Code/NumericalMethods/Source/Eigenanalysis/Solver3x3.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

132 lines
5.3 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <algorithm>
#include <cmath>
#include <AzCore/std/algorithm.h>
#include <LinearAlgebra.h>
#include <Eigenanalysis/Solver3x3.h>
#include <Eigenanalysis/Utilities.h>
namespace NumericalMethods::Eigenanalysis
{
SolverResult<Real, 3> NonIterativeSymmetricEigensolver3x3(
double a00, double a01, double a02, double a11, double a12, double a22
)
{
// Using the notation from Eberly:
// A - the symmetric input matrix
// a<ij> - the upper elements of the matrix (0 <= i <= j <= 2).
// B - a matrix derived from A, such that B = (A - q*I)/p where
// p = sqrt( tr( (A-q*I)^2 ) / 6 )
// q = tr(A) / 3
// beta<i> - the eigenvalues of B (0 <= i <= 2)
// alpha<i> - the eigenvalues of A (not explicit, stored in the result) (0 <= i <= 2)
double alpha0 = 0.0;
double alpha1 = 0.0;
double alpha2 = 0.0;
VectorVariable vec0 = VectorVariable::CreateFromVector({ 1.0, 0.0, 0.0 });
VectorVariable vec1 = VectorVariable::CreateFromVector({ 0.0, 1.0, 0.0 });
VectorVariable vec2 = VectorVariable::CreateFromVector({ 0.0, 0.0, 1.0 });
// Precondition the matrix by factoring out the element of biggest magnitude. This is to guard against
// floating-point overflow/underflow.
double maxAbsElem = std::max({fabs(a00), fabs(a01), fabs(a02), fabs(a11), fabs(a12), fabs(a22)});
if (maxAbsElem != 0.0)
{
// A is not the zero matrix.
double invMaxAbsElem = 1.0 / maxAbsElem;
a00 *= invMaxAbsElem;
a01 *= invMaxAbsElem;
a02 *= invMaxAbsElem;
a11 *= invMaxAbsElem;
a12 *= invMaxAbsElem;
a22 *= invMaxAbsElem;
double norm = a01 * a01 + a02 * a02 + a12 * a12;
if (norm > 0.0)
{
// Compute the eigenvalues of A. For a detailed explanation of how the algorithm works, see Eberly.
double q = (a00 + a11 + a22) / 3.0;
double b00 = a00 - q;
double b11 = a11 - q;
double b22 = a22 - q;
double p = sqrt((b00 * b00 + b11 * b11 + b22 * b22 + norm * 2.0) / 6.0);
double c00 = b11 * b22 - a12 * a12;
double c01 = a01 * b22 - a12 * a02;
double c02 = a01 * a12 - b11 * a02;
double det = (b00 * c00 - a01 * c01 + a02 * c02) / (p * p * p);
double halfDet = AZStd::clamp(det * 0.5, -1.0, 1.0);
double angle = acos(halfDet) / 3.0;
static const double twoThirdsPi = 2.09439510239319549;
// The eigenvalues of B are ordered such that beta0 <= beta1 <= beta2.
double beta2 = cos(angle) * 2.0;
double beta0 = cos(angle + twoThirdsPi) * 2.0;
double beta1 = -(beta0 + beta2);
// The eigenvalues of A are ordered such that alpha0 <= alpha1 <= alpha2.
alpha0 = q + p * beta0;
alpha1 = q + p * beta1;
alpha2 = q + p * beta2;
// Compute the eigenvectors. We either have
// beta0 <= beta1 < 0 < beta2 (if halfDet >= 0); or
// beta0 < 0 < beta1 <= beta2 (if halfDef < 0).
// For numerical stability, we use different approaches to compute the eigenvector corresponding to the
// eigenvalue that is definitely not repeated and the other two.
if (halfDet >= 0.0)
{
vec2 = ComputeEigenvector0(a00, a01, a02, a11, a12, a22, alpha2);
vec1 = ComputeEigenvector1(a00, a01, a02, a11, a12, a22, alpha1, vec2);
vec0 = ComputeEigenvector2(vec1, vec2);
}
else
{
vec0 = ComputeEigenvector0(a00, a01, a02, a11, a12, a22, alpha0);
vec1 = ComputeEigenvector1(a00, a01, a02, a11, a12, a22, alpha1, vec0);
vec2 = ComputeEigenvector2(vec0, vec1);
}
}
else
{
// A is a diagonal matrix. The eigenvalues in this case are the elements along the main diagonal, and
// the eigenvectors are the standard Cartesian basis vectors.
alpha0 = a00;
alpha1 = a11;
alpha2 = a22;
}
// The scaling applied to A in the precondition scales the eigenvalues by the same amount and must be
// reverted.
alpha0 *= maxAbsElem;
alpha1 *= maxAbsElem;
alpha2 *= maxAbsElem;
}
return SolverResult<Real, 3>{
SolverOutcome::Success,
{
Eigenpair<Real, 3>{alpha0, {{vec0[0], vec0[1], vec0[2]}}},
Eigenpair<Real, 3>{alpha1, {{vec1[0], vec1[1], vec1[2]}}},
Eigenpair<Real, 3>{alpha2, {{vec2[0], vec2[1], vec2[2]}}}
}
};
}
} // namespace NumericalMethods::Eigenanalysis