SkeletonMapper.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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(std::move(chain1), std::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::LockTranslations(const Skeleton *inSkeleton2, const bool *inLockedTranslations, const Mat44 *inNeutralPose2)
  89. {
  90. JPH_ASSERT(inSkeleton2->AreJointsCorrectlyOrdered());
  91. int n = inSkeleton2->GetJointCount();
  92. // Copy locked joints to array but don't actually include the first joint (this is physics driven)
  93. for (int i = 0; i < n; ++i)
  94. if (inLockedTranslations[i])
  95. {
  96. Locked l;
  97. l.mJointIdx = i;
  98. l.mParentJointIdx = inSkeleton2->GetJoint(i).mParentJointIndex;
  99. if (l.mParentJointIdx >= 0)
  100. l.mTranslation = inNeutralPose2[l.mParentJointIdx].Inversed() * inNeutralPose2[i].GetTranslation();
  101. else
  102. l.mTranslation = inNeutralPose2[i].GetTranslation();
  103. mLockedTranslations.push_back(l);
  104. }
  105. }
  106. void SkeletonMapper::LockAllTranslations(const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2)
  107. {
  108. JPH_ASSERT(!mMappings.empty(), "Call Initialize first!");
  109. JPH_ASSERT(inSkeleton2->AreJointsCorrectlyOrdered());
  110. // The first mapping is the top most one (remember that joints should be ordered so that parents go before children).
  111. // Because we created the mappings from the lowest joint first, this should contain the first mappable joint.
  112. int root_idx = mMappings[0].mJointIdx2;
  113. // Create temp array to hold locked joints
  114. int n = inSkeleton2->GetJointCount();
  115. bool *locked_translations = (bool *)JPH_STACK_ALLOC(n * sizeof(bool));
  116. memset(locked_translations, 0, n * sizeof(bool));
  117. // Mark root as locked
  118. locked_translations[root_idx] = true;
  119. // Loop over all joints and propagate the locked flag to all children
  120. for (int i = root_idx + 1; i < n; ++i)
  121. {
  122. int parent_idx = inSkeleton2->GetJoint(i).mParentJointIndex;
  123. if (parent_idx >= 0)
  124. locked_translations[i] = locked_translations[parent_idx];
  125. }
  126. // Unmark root because we don't actually want to include this (this determines the position of the entire ragdoll)
  127. locked_translations[root_idx] = false;
  128. // Call the generic function
  129. LockTranslations(inSkeleton2, locked_translations, inNeutralPose2);
  130. }
  131. void SkeletonMapper::Map(const Mat44 *inPose1ModelSpace, const Mat44 *inPose2LocalSpace, Mat44 *outPose2ModelSpace) const
  132. {
  133. // Apply direct mappings
  134. for (const Mapping &m : mMappings)
  135. outPose2ModelSpace[m.mJointIdx2] = inPose1ModelSpace[m.mJointIdx1] * m.mJoint1To2;
  136. // Apply chain mappings
  137. for (const Chain &c : mChains)
  138. {
  139. // Calculate end of chain given local space transforms of the joints of the chain
  140. Mat44 &chain_start = outPose2ModelSpace[c.mJointIndices2.front()];
  141. Mat44 chain_end = chain_start;
  142. for (int j = 1; j < (int)c.mJointIndices2.size(); ++j)
  143. chain_end = chain_end * inPose2LocalSpace[c.mJointIndices2[j]];
  144. // Calculate the direction in world space for skeleton 1 and skeleton 2 and the rotation between them
  145. Vec3 actual = chain_end.GetTranslation() - chain_start.GetTranslation();
  146. Vec3 desired = inPose1ModelSpace[c.mJointIndices1.back()].GetTranslation() - inPose1ModelSpace[c.mJointIndices1.front()].GetTranslation();
  147. Quat rotation = Quat::sFromTo(actual, desired);
  148. // Rotate the start of the chain
  149. chain_start.SetRotation(Mat44::sRotation(rotation) * chain_start.GetRotation());
  150. // Update all joints but the first and the last joint using their local space transforms
  151. for (int j = 1; j < (int)c.mJointIndices2.size() - 1; ++j)
  152. {
  153. int parent = c.mJointIndices2[j - 1];
  154. int child = c.mJointIndices2[j];
  155. outPose2ModelSpace[child] = outPose2ModelSpace[parent] * inPose2LocalSpace[child];
  156. }
  157. }
  158. // All unmapped joints take the local pose and convert it to model space
  159. for (const Unmapped &u : mUnmapped)
  160. if (u.mParentJointIdx >= 0)
  161. {
  162. JPH_ASSERT(u.mParentJointIdx < u.mJointIdx, "Joints must be ordered: parents first");
  163. outPose2ModelSpace[u.mJointIdx] = outPose2ModelSpace[u.mParentJointIdx] * inPose2LocalSpace[u.mJointIdx];
  164. }
  165. else
  166. outPose2ModelSpace[u.mJointIdx] = inPose2LocalSpace[u.mJointIdx];
  167. // Update all locked joint translations
  168. for (const Locked &l : mLockedTranslations)
  169. outPose2ModelSpace[l.mJointIdx].SetTranslation(outPose2ModelSpace[l.mParentJointIdx] * l.mTranslation);
  170. }
  171. void SkeletonMapper::MapReverse(const Mat44 *inPose2ModelSpace, Mat44 *outPose1ModelSpace) const
  172. {
  173. // Normally each joint in skeleton 1 should be present in the mapping, so we only need to apply the direct mappings
  174. for (const Mapping &m : mMappings)
  175. outPose1ModelSpace[m.mJointIdx1] = inPose2ModelSpace[m.mJointIdx2] * m.mJoint2To1;
  176. }
  177. JPH_NAMESPACE_END