FeatureAngularVelocity.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <AzCore/Console/IConsole.h>
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <Allocators.h>
  12. #include <EMotionFX/Source/ActorInstance.h>
  13. #include <EMotionFX/Source/EMotionFXManager.h>
  14. #include <EMotionFX/Source/EventManager.h>
  15. #include <EMotionFX/Source/TransformData.h>
  16. #include <FeatureAngularVelocity.h>
  17. #include <FeatureMatrixTransformer.h>
  18. #include <FrameDatabase.h>
  19. #include <MotionMatchingInstance.h>
  20. #include <PoseDataJointVelocities.h>
  21. namespace EMotionFX::MotionMatching
  22. {
  23. AZ_CVAR_EXTERNED(float, mm_debugDrawVelocityScale);
  24. AZ_CLASS_ALLOCATOR_IMPL(FeatureAngularVelocity, MotionMatchAllocator)
  25. void FeatureAngularVelocity::ExtractFeatureValues(const ExtractFeatureContext& context)
  26. {
  27. const ActorInstance* actorInstance = context.m_actorInstance;
  28. const Frame& frame = context.m_frameDatabase->GetFrame(context.m_frameIndex);
  29. AnimGraphPose* tempPose = context.m_posePool.RequestPose(actorInstance);
  30. {
  31. // Calculate the joint velocities for the sampled pose using the same method as we do for the frame database.
  32. PoseDataJointVelocities* velocityPoseData = tempPose->GetPose().GetAndPreparePoseData<PoseDataJointVelocities>(actorInstance);
  33. velocityPoseData->CalculateVelocity(
  34. actorInstance, context.m_posePool, frame.GetSourceMotion(), frame.GetSampleTime(), m_relativeToNodeIndex);
  35. const AZ::Vector3& angularVelocity = velocityPoseData->GetAngularVelocities()[m_jointIndex];
  36. context.m_featureMatrix.SetVector3(context.m_frameIndex, m_featureColumnOffset, angularVelocity);
  37. }
  38. context.m_posePool.FreePose(tempPose);
  39. }
  40. void FeatureAngularVelocity::FillQueryVector(QueryVector& queryVector, const QueryVectorContext& context)
  41. {
  42. PoseDataJointVelocities* velocityPoseData = context.m_currentPose.GetPoseData<PoseDataJointVelocities>();
  43. AZ_Assert(velocityPoseData, "Cannot calculate velocity feature cost without joint velocity pose data.");
  44. const AZ::Vector3 currentVelocity = velocityPoseData->GetAngularVelocity(m_jointIndex);
  45. queryVector.SetVector3(currentVelocity, m_featureColumnOffset);
  46. }
  47. float FeatureAngularVelocity::CalculateFrameCost(size_t frameIndex, const FrameCostContext& context) const
  48. {
  49. const AZ::Vector3 queryVelocity = context.m_queryVector.GetVector3(m_featureColumnOffset);
  50. const AZ::Vector3 frameVelocity = context.m_featureMatrix.GetVector3(frameIndex, m_featureColumnOffset);
  51. return CalcResidual(queryVelocity, frameVelocity);
  52. }
  53. void FeatureAngularVelocity::DebugDraw(
  54. AzFramework::DebugDisplayRequests& debugDisplay,
  55. const Pose& pose,
  56. const AZ::Vector3& velocity,
  57. size_t jointIndex,
  58. size_t relativeToJointIndex,
  59. const AZ::Color& color)
  60. {
  61. const Transform jointModelTM = pose.GetModelSpaceTransform(jointIndex);
  62. const Transform relativeToWorldTM = pose.GetWorldSpaceTransform(relativeToJointIndex);
  63. const AZ::Vector3 jointPosition = relativeToWorldTM.TransformPoint(jointModelTM.m_position);
  64. const AZ::Vector3 velocityWorldSpace = relativeToWorldTM.TransformVector(velocity);
  65. DebugDrawVelocity(debugDisplay, jointPosition, velocityWorldSpace * mm_debugDrawVelocityScale, color);
  66. }
  67. void FeatureAngularVelocity::DebugDraw(
  68. AzFramework::DebugDisplayRequests& debugDisplay,
  69. const Pose& currentPose,
  70. const FeatureMatrix& featureMatrix,
  71. const FeatureMatrixTransformer* featureTransformer,
  72. size_t frameIndex)
  73. {
  74. if (m_jointIndex == InvalidIndex)
  75. {
  76. return;
  77. }
  78. AZ::Vector3 angularVelocity = featureMatrix.GetVector3(frameIndex, m_featureColumnOffset);
  79. if (featureTransformer)
  80. {
  81. angularVelocity = featureTransformer->InverseTransform(angularVelocity, m_featureColumnOffset);
  82. }
  83. DebugDraw(debugDisplay, currentPose, angularVelocity, m_jointIndex, m_relativeToNodeIndex, m_debugColor);
  84. }
  85. void FeatureAngularVelocity::Reflect(AZ::ReflectContext* context)
  86. {
  87. auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  88. if (!serializeContext)
  89. {
  90. return;
  91. }
  92. serializeContext->Class<FeatureAngularVelocity, Feature>()->Version(1);
  93. AZ::EditContext* editContext = serializeContext->GetEditContext();
  94. if (!editContext)
  95. {
  96. return;
  97. }
  98. editContext->Class<FeatureAngularVelocity>("FeatureAngularVelocity", "Matches joint angular velocities.")
  99. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  100. ->Attribute(AZ::Edit::Attributes::AutoExpand, "");
  101. }
  102. size_t FeatureAngularVelocity::GetNumDimensions() const
  103. {
  104. return 3;
  105. }
  106. AZStd::string FeatureAngularVelocity::GetDimensionName(size_t index) const
  107. {
  108. AZStd::string result = m_jointName;
  109. result += '.';
  110. switch (index)
  111. {
  112. case 0:
  113. {
  114. result += "AngularVelocityX";
  115. break;
  116. }
  117. case 1:
  118. {
  119. result += "AngularVelocityY";
  120. break;
  121. }
  122. case 2:
  123. {
  124. result += "AngularVelocityZ";
  125. break;
  126. }
  127. default:
  128. {
  129. result += Feature::GetDimensionName(index);
  130. }
  131. }
  132. return result;
  133. }
  134. } // namespace EMotionFX::MotionMatching