SkeletonMapper.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // Calculate the transform that takes this joint from skeleton 1 to 2
  22. Mat44 joint_1_to_2 = inNeutralPose1[j1].Inversed() * inNeutralPose2[j2];
  23. // Ensure bottom right element is 1 (numerical imprecision in the inverse can make this not so)
  24. joint_1_to_2(3, 3) = 1.0f;
  25. mMappings.emplace_back(j1, j2, joint_1_to_2);
  26. mapped1[j1] = true;
  27. mapped2[j2] = true;
  28. break;
  29. }
  30. Array<int> cur_chain; // Taken out of the loop to minimize amount of allocations
  31. // Find joint chains
  32. for (int m1 = 0; m1 < (int)mMappings.size(); ++m1)
  33. {
  34. Array<int> chain2;
  35. int chain2_m = -1;
  36. for (int m2 = m1 + 1; m2 < (int)mMappings.size(); ++m2)
  37. {
  38. // Find the chain from back from m2 to m1
  39. int start = mMappings[m1].mJointIdx2;
  40. int end = mMappings[m2].mJointIdx2;
  41. int cur = end;
  42. cur_chain.clear(); // Should preserve memory
  43. do
  44. {
  45. cur_chain.push_back(cur);
  46. cur = inSkeleton2->GetJoint(cur).mParentJointIndex;
  47. }
  48. while (cur >= 0 && cur != start && !mapped2[cur]);
  49. cur_chain.push_back(start);
  50. if (cur == start // This should be the correct chain
  51. && cur_chain.size() > 2 // It should have joints between the mapped joints
  52. && cur_chain.size() > chain2.size()) // And it should be the longest so far
  53. {
  54. chain2.swap(cur_chain);
  55. chain2_m = m2;
  56. }
  57. }
  58. if (!chain2.empty())
  59. {
  60. // Get the chain for 1
  61. Array<int> chain1;
  62. int start = mMappings[m1].mJointIdx1;
  63. int cur = mMappings[chain2_m].mJointIdx1;
  64. do
  65. {
  66. chain1.push_back(cur);
  67. cur = inSkeleton1->GetJoint(cur).mParentJointIndex;
  68. }
  69. while (cur >= 0 && cur != start && !mapped1[cur]);
  70. chain1.push_back(start);
  71. // If the chain exists in 1 too
  72. if (cur == start)
  73. {
  74. // Reverse the chains
  75. reverse(chain1.begin(), chain1.end());
  76. reverse(chain2.begin(), chain2.end());
  77. // Mark elements mapped
  78. for (int j1 : chain1)
  79. mapped1[j1] = true;
  80. for (int j2 : chain2)
  81. mapped2[j2] = true;
  82. // Insert the chain
  83. mChains.emplace_back(std::move(chain1), std::move(chain2));
  84. }
  85. }
  86. }
  87. // Collect unmapped joints from 2
  88. for (int j2 = 0; j2 < n2; ++j2)
  89. if (!mapped2[j2])
  90. mUnmapped.emplace_back(j2, inSkeleton2->GetJoint(j2).mParentJointIndex);
  91. }
  92. void SkeletonMapper::LockTranslations(const Skeleton *inSkeleton2, const bool *inLockedTranslations, const Mat44 *inNeutralPose2)
  93. {
  94. JPH_ASSERT(inSkeleton2->AreJointsCorrectlyOrdered());
  95. int n = inSkeleton2->GetJointCount();
  96. // Copy locked joints to array but don't actually include the first joint (this is physics driven)
  97. for (int i = 0; i < n; ++i)
  98. if (inLockedTranslations[i])
  99. {
  100. Locked l;
  101. l.mJointIdx = i;
  102. l.mParentJointIdx = inSkeleton2->GetJoint(i).mParentJointIndex;
  103. if (l.mParentJointIdx >= 0)
  104. l.mTranslation = inNeutralPose2[l.mParentJointIdx].Inversed() * inNeutralPose2[i].GetTranslation();
  105. else
  106. l.mTranslation = inNeutralPose2[i].GetTranslation();
  107. mLockedTranslations.push_back(l);
  108. }
  109. }
  110. void SkeletonMapper::LockAllTranslations(const Skeleton *inSkeleton2, const Mat44 *inNeutralPose2)
  111. {
  112. JPH_ASSERT(!mMappings.empty(), "Call Initialize first!");
  113. JPH_ASSERT(inSkeleton2->AreJointsCorrectlyOrdered());
  114. // The first mapping is the top most one (remember that joints should be ordered so that parents go before children).
  115. // Because we created the mappings from the lowest joint first, this should contain the first mappable joint.
  116. int root_idx = mMappings[0].mJointIdx2;
  117. // Create temp array to hold locked joints
  118. int n = inSkeleton2->GetJointCount();
  119. bool *locked_translations = (bool *)JPH_STACK_ALLOC(n * sizeof(bool));
  120. memset(locked_translations, 0, n * sizeof(bool));
  121. // Mark root as locked
  122. locked_translations[root_idx] = true;
  123. // Loop over all joints and propagate the locked flag to all children
  124. for (int i = root_idx + 1; i < n; ++i)
  125. {
  126. int parent_idx = inSkeleton2->GetJoint(i).mParentJointIndex;
  127. if (parent_idx >= 0)
  128. locked_translations[i] = locked_translations[parent_idx];
  129. }
  130. // Unmark root because we don't actually want to include this (this determines the position of the entire ragdoll)
  131. locked_translations[root_idx] = false;
  132. // Call the generic function
  133. LockTranslations(inSkeleton2, locked_translations, inNeutralPose2);
  134. }
  135. void SkeletonMapper::Map(const Mat44 *inPose1ModelSpace, const Mat44 *inPose2LocalSpace, Mat44 *outPose2ModelSpace) const
  136. {
  137. // Apply direct mappings
  138. for (const Mapping &m : mMappings)
  139. outPose2ModelSpace[m.mJointIdx2] = inPose1ModelSpace[m.mJointIdx1] * m.mJoint1To2;
  140. // Apply chain mappings
  141. for (const Chain &c : mChains)
  142. {
  143. // Calculate end of chain given local space transforms of the joints of the chain
  144. Mat44 &chain_start = outPose2ModelSpace[c.mJointIndices2.front()];
  145. Mat44 chain_end = chain_start;
  146. for (int j = 1; j < (int)c.mJointIndices2.size(); ++j)
  147. chain_end = chain_end * inPose2LocalSpace[c.mJointIndices2[j]];
  148. // Calculate the direction in world space for skeleton 1 and skeleton 2 and the rotation between them
  149. Vec3 actual = chain_end.GetTranslation() - chain_start.GetTranslation();
  150. Vec3 desired = inPose1ModelSpace[c.mJointIndices1.back()].GetTranslation() - inPose1ModelSpace[c.mJointIndices1.front()].GetTranslation();
  151. Quat rotation = Quat::sFromTo(actual, desired);
  152. // Rotate the start of the chain
  153. chain_start.SetRotation(Mat44::sRotation(rotation) * chain_start.GetRotation());
  154. // Update all joints but the first and the last joint using their local space transforms
  155. for (int j = 1; j < (int)c.mJointIndices2.size() - 1; ++j)
  156. {
  157. int parent = c.mJointIndices2[j - 1];
  158. int child = c.mJointIndices2[j];
  159. outPose2ModelSpace[child] = outPose2ModelSpace[parent] * inPose2LocalSpace[child];
  160. }
  161. }
  162. // All unmapped joints take the local pose and convert it to model space
  163. for (const Unmapped &u : mUnmapped)
  164. if (u.mParentJointIdx >= 0)
  165. {
  166. JPH_ASSERT(u.mParentJointIdx < u.mJointIdx, "Joints must be ordered: parents first");
  167. outPose2ModelSpace[u.mJointIdx] = outPose2ModelSpace[u.mParentJointIdx] * inPose2LocalSpace[u.mJointIdx];
  168. }
  169. else
  170. outPose2ModelSpace[u.mJointIdx] = inPose2LocalSpace[u.mJointIdx];
  171. // Update all locked joint translations
  172. for (const Locked &l : mLockedTranslations)
  173. outPose2ModelSpace[l.mJointIdx].SetTranslation(outPose2ModelSpace[l.mParentJointIdx] * l.mTranslation);
  174. }
  175. void SkeletonMapper::MapReverse(const Mat44 *inPose2ModelSpace, Mat44 *outPose1ModelSpace) const
  176. {
  177. // Normally each joint in skeleton 1 should be present in the mapping, so we only need to apply the direct mappings
  178. for (const Mapping &m : mMappings)
  179. outPose1ModelSpace[m.mJointIdx1] = inPose2ModelSpace[m.mJointIdx2] * m.mJoint2To1;
  180. }
  181. int SkeletonMapper::GetMappedJointIdx(int inJoint1Idx) const
  182. {
  183. for (const Mapping &m : mMappings)
  184. if (m.mJointIdx1 == inJoint1Idx)
  185. return m.mJointIdx2;
  186. return -1;
  187. }
  188. bool SkeletonMapper::IsJointTranslationLocked(int inJoint2Idx) const
  189. {
  190. for (const Locked &l : mLockedTranslations)
  191. if (l.mJointIdx == inJoint2Idx)
  192. return true;
  193. return false;
  194. }
  195. JPH_NAMESPACE_END