SkeletonMapper.cpp 7.8 KB

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