SkeletonMapper.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Skeleton/SkeletonMapper.h>
  5. JPH_NAMESPACE_BEGIN
  6. void SkeletonMapper::Initialize(const Skeleton *inSkeleton1, const Mat44 *inNeutralPose1, const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2, const CanMapJoint &inCanMapJoint)
  7. {
  8. JPH_ASSERT(mMappings.empty() && mChains.empty() && mUnmapped.empty()); // Should not be initialized yet
  9. // Count joints
  10. int n1 = inSkeleton1->GetJointCount();
  11. int n2 = inSkeleton2->GetJointCount();
  12. JPH_ASSERT(n1 <= n2, "Skeleton 1 should be the low detail skeleton!");
  13. // Keep track of mapped joints (initialize to false)
  14. Array<bool> mapped1(n1, false);
  15. Array<bool> mapped2(n2, false);
  16. // Find joints that can be mapped directly
  17. for (int j1 = 0; j1 < n1; ++j1)
  18. for (int j2 = 0; j2 < n2; ++j2)
  19. if (inCanMapJoint(inSkeleton1, j1, inSkeleton2, j2))
  20. {
  21. mMappings.emplace_back(j1, j2, inNeutralPose1[j1].Inversed() * inNeutralPose2[j2]);
  22. mapped1[j1] = true;
  23. mapped2[j2] = true;
  24. break;
  25. }
  26. Array<int> cur_chain; // Taken out of the loop to minimize amount of allocations
  27. // Find joint chains
  28. for (int m1 = 0; m1 < (int)mMappings.size(); ++m1)
  29. {
  30. Array<int> chain2;
  31. int chain2_m = -1;
  32. for (int m2 = m1 + 1; m2 < (int)mMappings.size(); ++m2)
  33. {
  34. // Find the chain from back from m2 to m1
  35. int start = mMappings[m1].mJointIdx2;
  36. int end = mMappings[m2].mJointIdx2;
  37. int cur = end;
  38. cur_chain.clear(); // Should preserve memory
  39. do
  40. {
  41. cur_chain.push_back(cur);
  42. cur = inSkeleton2->GetJoint(cur).mParentJointIndex;
  43. }
  44. while (cur >= 0 && cur != start && !mapped2[cur]);
  45. cur_chain.push_back(start);
  46. if (cur == start // This should be the correct chain
  47. && cur_chain.size() > 2 // It should have joints between the mapped joints
  48. && cur_chain.size() > chain2.size()) // And it should be the longest so far
  49. {
  50. chain2.swap(cur_chain);
  51. chain2_m = m2;
  52. }
  53. }
  54. if (!chain2.empty())
  55. {
  56. // Get the chain for 1
  57. Array<int> chain1;
  58. int start = mMappings[m1].mJointIdx1;
  59. int cur = mMappings[chain2_m].mJointIdx1;
  60. do
  61. {
  62. chain1.push_back(cur);
  63. cur = inSkeleton1->GetJoint(cur).mParentJointIndex;
  64. }
  65. while (cur >= 0 && cur != start && !mapped1[cur]);
  66. chain1.push_back(start);
  67. // If the chain exists in 1 too
  68. if (cur == start)
  69. {
  70. // Reverse the chains
  71. reverse(chain1.begin(), chain1.end());
  72. reverse(chain2.begin(), chain2.end());
  73. // Mark elements mapped
  74. for (int j1 : chain1)
  75. mapped1[j1] = true;
  76. for (int j2 : chain2)
  77. mapped2[j2] = true;
  78. // Insert the chain
  79. mChains.emplace_back(move(chain1), move(chain2));
  80. }
  81. }
  82. }
  83. // Collect unmapped joints from 2
  84. for (int j2 = 0; j2 < n2; ++j2)
  85. if (!mapped2[j2])
  86. mUnmapped.emplace_back(j2, inSkeleton2->GetJoint(j2).mParentJointIndex);
  87. }
  88. void SkeletonMapper::Map(const Mat44 *inPose1ModelSpace, const Mat44 *inPose2LocalSpace, Mat44 *outPose2ModelSpace) const
  89. {
  90. // Apply direct mappings
  91. for (const Mapping &m : mMappings)
  92. outPose2ModelSpace[m.mJointIdx2] = inPose1ModelSpace[m.mJointIdx1] * m.mJoint1To2;
  93. // Apply chain mappings
  94. for (const Chain &c : mChains)
  95. {
  96. // Calculate end of chain given local space transforms of the joints of the chain
  97. Mat44 &chain_start = outPose2ModelSpace[c.mJointIndices2.front()];
  98. Mat44 chain_end = chain_start;
  99. for (int j = 1; j < (int)c.mJointIndices2.size(); ++j)
  100. chain_end = chain_end * inPose2LocalSpace[c.mJointIndices2[j]];
  101. // Calculate the direction in world space for skeleton 1 and skeleton 2 and the rotation between them
  102. Vec3 actual = chain_end.GetTranslation() - chain_start.GetTranslation();
  103. Vec3 desired = inPose1ModelSpace[c.mJointIndices1.back()].GetTranslation() - inPose1ModelSpace[c.mJointIndices1.front()].GetTranslation();
  104. Quat rotation = Quat::sFromTo(actual, desired);
  105. // Rotate the start of the chain
  106. chain_start.SetRotation(Mat44::sRotation(rotation) * chain_start.GetRotation());
  107. // Update all joints but the first and the last joint using their local space transforms
  108. for (int j = 1; j < (int)c.mJointIndices2.size() - 1; ++j)
  109. {
  110. int parent = c.mJointIndices2[j - 1];
  111. int child = c.mJointIndices2[j];
  112. outPose2ModelSpace[child] = outPose2ModelSpace[parent] * inPose2LocalSpace[child];
  113. }
  114. }
  115. // All unmapped joints take the local pose and convert it to model space
  116. for (const Unmapped &u : mUnmapped)
  117. if (u.mParentJointIdx >= 0)
  118. {
  119. JPH_ASSERT(u.mParentJointIdx < u.mJointIdx, "Joints must be ordered: parents first");
  120. outPose2ModelSpace[u.mJointIdx] = outPose2ModelSpace[u.mParentJointIdx] * inPose2LocalSpace[u.mJointIdx];
  121. }
  122. else
  123. outPose2ModelSpace[u.mJointIdx] = inPose2LocalSpace[u.mJointIdx];
  124. }
  125. void SkeletonMapper::MapReverse(const Mat44 *inPose2ModelSpace, Mat44 *outPose1ModelSpace) const
  126. {
  127. // Normally each joint in skeleton 1 should be present in the mapping, so we only need to apply the direct mappings
  128. for (const Mapping &m : mMappings)
  129. outPose1ModelSpace[m.mJointIdx1] = inPose2ModelSpace[m.mJointIdx2] * m.mJoint2To1;
  130. }
  131. JPH_NAMESPACE_END