SkeletonMapper.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/Reference.h>
  6. #include <Jolt/Skeleton/Skeleton.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Class that is able to map a low detail (ragdoll) skeleton to a high detail (animation) skeleton and vice versa
  9. class JPH_EXPORT SkeletonMapper : public RefTarget<SkeletonMapper>
  10. {
  11. public:
  12. /// A joint that maps 1-on-1 to a joint in the other skeleton
  13. class Mapping
  14. {
  15. public:
  16. Mapping() = default;
  17. Mapping(int inJointIdx1, int inJointIdx2, Mat44Arg inJoint1To2) : mJointIdx1(inJointIdx1), mJointIdx2(inJointIdx2), mJoint1To2(inJoint1To2), mJoint2To1(inJoint1To2.Inversed())
  18. {
  19. // Ensure bottom right element is 1 (numerical imprecision in the inverse can make this not so)
  20. mJoint2To1(3, 3) = 1.0f;
  21. }
  22. int mJointIdx1; ///< Index of joint from skeleton 1
  23. int mJointIdx2; ///< Corresponding index of joint from skeleton 2
  24. Mat44 mJoint1To2; ///< Transforms this joint from skeleton 1 to 2
  25. Mat44 mJoint2To1; ///< Inverse of the transform above
  26. };
  27. /// A joint chain that starts with a 1-on-1 mapped joint and ends with a 1-on-1 mapped joint with intermediate joints that cannot be mapped
  28. class Chain
  29. {
  30. public:
  31. Chain() = default;
  32. Chain(Array<int> &&inJointIndices1, Array<int> &&inJointIndices2) : mJointIndices1(std::move(inJointIndices1)), mJointIndices2(std::move(inJointIndices2)) { }
  33. Array<int> mJointIndices1; ///< Joint chain from skeleton 1
  34. Array<int> mJointIndices2; ///< Corresponding joint chain from skeleton 2
  35. };
  36. /// Joints that could not be mapped from skeleton 1 to 2
  37. class Unmapped
  38. {
  39. public:
  40. Unmapped() = default;
  41. Unmapped(int inJointIdx, int inParentJointIdx) : mJointIdx(inJointIdx), mParentJointIdx(inParentJointIdx) { }
  42. int mJointIdx; ///< Joint index of unmappable joint
  43. int mParentJointIdx; ///< Parent joint index of unmappable joint
  44. };
  45. /// Joints that should have their translation locked (fixed)
  46. class Locked
  47. {
  48. public:
  49. int mJointIdx; ///< Joint index of joint with locked translation (in skeleton 2)
  50. int mParentJointIdx; ///< Parent joint index of joint with locked translation (in skeleton 2)
  51. Vec3 mTranslation; ///< Translation of neutral pose
  52. };
  53. /// A function that is called to determine if a joint can be mapped from source to target skeleton
  54. using CanMapJoint = function<bool (const Skeleton *, int, const Skeleton *, int)>;
  55. /// Default function that checks if the names of the joints are equal
  56. static bool sDefaultCanMapJoint(const Skeleton *inSkeleton1, int inIndex1, const Skeleton *inSkeleton2, int inIndex2)
  57. {
  58. return inSkeleton1->GetJoint(inIndex1).mName == inSkeleton2->GetJoint(inIndex2).mName;
  59. }
  60. /// Initialize the skeleton mapper. Skeleton 1 should be the (low detail) ragdoll skeleton and skeleton 2 the (high detail) animation skeleton.
  61. /// We assume that each joint in skeleton 1 can be mapped to a joint in skeleton 2 (if not mapping from animation skeleton to ragdoll skeleton will be undefined).
  62. /// Skeleton 2 should have the same hierarchy as skeleton 1 but can contain extra joints between those in skeleton 1 and it can have extra joints at the root and leaves of the skeleton.
  63. /// @param inSkeleton1 Source skeleton to map from.
  64. /// @param inNeutralPose1 Neutral pose of the source skeleton (model space)
  65. /// @param inSkeleton2 Target skeleton to map to.
  66. /// @param inNeutralPose2 Neutral pose of the target skeleton (model space), inNeutralPose1 and inNeutralPose2 must match as closely as possible, preferably the position of the mappable joints should be identical.
  67. /// @param inCanMapJoint Function that checks if joints in skeleton 1 and skeleton 2 are equal.
  68. void Initialize(const Skeleton *inSkeleton1, const Mat44 *inNeutralPose1, const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2, const CanMapJoint &inCanMapJoint = sDefaultCanMapJoint);
  69. /// This can be called so lock the translation of a specified set of joints in skeleton 2.
  70. /// Because constraints are never 100% rigid, there's always a little bit of stretch in the ragdoll when the ragdoll is under stress.
  71. /// Locking the translations of the pose will remove the visual stretch from the ragdoll but will introduce a difference between the
  72. /// physical simulation and the visual representation.
  73. /// @param inSkeleton2 Target skeleton to map to.
  74. /// @param inLockedTranslations An array of bools the size of inSkeleton2->GetJointCount(), for each joint indicating if the joint is locked.
  75. /// @param inNeutralPose2 Neutral pose to take reference translations from
  76. void LockTranslations(const Skeleton *inSkeleton2, const bool *inLockedTranslations, const Mat44 *inNeutralPose2);
  77. /// After Initialize(), this can be called to lock the translation of all joints in skeleton 2 below the first mapped joint to those of the neutral pose.
  78. /// Because constraints are never 100% rigid, there's always a little bit of stretch in the ragdoll when the ragdoll is under stress.
  79. /// Locking the translations of the pose will remove the visual stretch from the ragdoll but will introduce a difference between the
  80. /// physical simulation and the visual representation.
  81. /// @param inSkeleton2 Target skeleton to map to.
  82. /// @param inNeutralPose2 Neutral pose to take reference translations from
  83. void LockAllTranslations(const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2);
  84. /// Map a pose. Joints that were directly mappable will be copied in model space from pose 1 to pose 2. Any joints that are only present in skeleton 2
  85. /// will get their model space transform calculated through the local space transforms of pose 2. Joints that are part of a joint chain between two
  86. /// mapped joints will be reoriented towards the next joint in skeleton 1. This means that it is possible for unmapped joints to have some animation,
  87. /// but very extreme animation poses will show artifacts.
  88. /// @param inPose1ModelSpace Pose on skeleton 1 in model space
  89. /// @param inPose2LocalSpace Pose on skeleton 2 in local space (used for the joints that cannot be mapped)
  90. /// @param outPose2ModelSpace Model space pose on skeleton 2 (the output of the mapping)
  91. void Map(const Mat44 *inPose1ModelSpace, const Mat44 *inPose2LocalSpace, Mat44 *outPose2ModelSpace) const;
  92. /// Reverse map a pose, this will only use the mappings and not the chains (it assumes that all joints in skeleton 1 are mapped)
  93. /// @param inPose2ModelSpace Model space pose on skeleton 2
  94. /// @param outPose1ModelSpace When the function returns this will contain the model space pose for skeleton 1
  95. void MapReverse(const Mat44 *inPose2ModelSpace, Mat44 *outPose1ModelSpace) const;
  96. /// Search through the directly mapped joints (mMappings) and find inJoint1Idx, returns the corresponding Joint2Idx or -1 if not found.
  97. int GetMappedJointIdx(int inJoint1Idx) const;
  98. /// Search through the locked translations (mLockedTranslations) and find if joint inJoint2Idx is locked.
  99. bool IsJointTranslationLocked(int inJoint2Idx) const;
  100. using MappingVector = Array<Mapping>;
  101. using ChainVector = Array<Chain>;
  102. using UnmappedVector = Array<Unmapped>;
  103. using LockedVector = Array<Locked>;
  104. ///@name Access to the mapped joints
  105. ///@{
  106. const MappingVector & GetMappings() const { return mMappings; }
  107. MappingVector & GetMappings() { return mMappings; }
  108. const ChainVector & GetChains() const { return mChains; }
  109. ChainVector & GetChains() { return mChains; }
  110. const UnmappedVector & GetUnmapped() const { return mUnmapped; }
  111. UnmappedVector & GetUnmapped() { return mUnmapped; }
  112. const LockedVector & GetLockedTranslations() const { return mLockedTranslations; }
  113. LockedVector & GetLockedTranslations() { return mLockedTranslations; }
  114. ///@}
  115. private:
  116. /// Joint mappings
  117. MappingVector mMappings;
  118. ChainVector mChains;
  119. UnmappedVector mUnmapped; ///< Joint indices that could not be mapped from 1 to 2 (these are indices in 2)
  120. LockedVector mLockedTranslations;
  121. };
  122. JPH_NAMESPACE_END