Debian Codegraph @

thirdparty/jolt_physics/Jolt/Skeleton/SkeletonMapper.cpp

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