Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
character_utility.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
10
15#include "momentum/common/log.h"
16#include "momentum/math/mesh.h"
17
18#include <unordered_set>
19
20namespace momentum {
21
23 const JointPhysicalProperties& properties,
24 const Skeleton& skeleton) {
25 if (properties.jointIndex < skeleton.joints.size() &&
26 (properties.jointName.empty() ||
27 skeleton.joints.at(properties.jointIndex).name == properties.jointName)) {
28 return properties.jointIndex;
29 }
30
31 if (!properties.jointName.empty()) {
32 return skeleton.getJointIdByName(properties.jointName);
33 }
34
35 return kInvalidIndex;
36}
37
38namespace {
39
40Skeleton scale(const Skeleton& skel, float scale) {
41 Skeleton result = skel;
42 for (auto& j : result.joints) {
43 j.translationOffset *= scale;
44 }
45 return result;
46}
47
48std::unique_ptr<Mesh> scale(const std::unique_ptr<Mesh>& mesh, float scale) {
49 if (!mesh) {
50 return {};
51 }
52
53 auto result = std::make_unique<Mesh>(*mesh);
54 for (auto& v : result->vertices) {
55 v *= scale;
56 }
57 return result;
58}
59
60LocatorList scale(const LocatorList& locators, float scale) {
61 LocatorList result = locators;
62 for (auto& l : result) {
63 l.offset *= scale;
64 l.limitOrigin *= scale;
65 }
66 return result;
67}
68
69ParameterLimits scale(const ParameterLimits& limits, float scale) {
70 ParameterLimits result = limits;
71 for (auto& limit : result) {
72 if (limit.type == LimitType::Ellipsoid) {
73 limit.data.ellipsoid.ellipsoid.translation() *= scale;
74 limit.data.ellipsoid.ellipsoidInv.translation() *= scale;
75 limit.data.ellipsoid.offset *= scale;
76 }
77 }
78
79 return result;
80}
81
82std::unique_ptr<CollisionGeometry> scale(
83 const std::unique_ptr<CollisionGeometry>& geom,
84 float scale) {
85 if (!geom) {
86 return {};
87 }
88
89 auto result = std::make_unique<CollisionGeometry>(*geom);
90 for (auto& primitive : (*result)) {
91 primitive.transformation.translation *= scale;
92 if (primitive.type == CollisionPrimitiveType::TaperedCapsule) {
93 primitive.radius *= scale;
94 primitive.length *= scale;
95 } else if (primitive.type == CollisionPrimitiveType::Ellipsoid) {
96 primitive.ellipsoidRadii *= scale;
97 } else if (primitive.type == CollisionPrimitiveType::Box) {
98 primitive.boxHalfExtents *= scale;
99 }
100 }
101
102 return result;
103}
104
105float getPhysicalMassScale(CharacterMassScale massScale, float lengthScale) {
106 switch (massScale) {
108 return 1.0f;
110 return lengthScale * lengthScale * lengthScale;
111 }
112
113 // Keep the switch exhaustive for compiler warnings; this catches invalid runtime enum values.
114 MT_THROW("Unknown CharacterMassScale policy: {}", static_cast<int>(massScale));
115}
116
118 const PhysicalProperties& physicalProperties,
119 float lengthScale,
120 CharacterMassScale massScale) {
121 const float massScaleValue = getPhysicalMassScale(massScale, lengthScale);
122
123 PhysicalProperties result = physicalProperties;
124 for (auto& jointProperties : result) {
125 jointProperties.centerOfMassOffset *= lengthScale;
126 jointProperties.mass *= massScaleValue;
127 jointProperties.inertia *= massScaleValue * lengthScale * lengthScale;
128 }
129 return result;
130}
131
132TransformationList scaleInverseBindPose(
133 const TransformationList& transformations,
134 const float scale) {
135 TransformationList result = transformations;
136 MT_CHECK(scale > 0.0f);
137 for (auto& t : result) {
138 t.translation() *= scale;
139 }
140 return result;
141}
142
143PhysicalProperties mapPhysicalProperties(
144 const PhysicalProperties& physicalProperties,
145 const Skeleton& sourceSkeleton,
146 const std::vector<size_t>& jointMapping,
147 const Skeleton& targetSkeleton) {
148 PhysicalProperties result;
149 result.reserve(physicalProperties.size());
150
151 for (const auto& jointProperties : physicalProperties) {
152 const size_t sourceJointIndex =
153 resolvePhysicalPropertiesJointIndex(jointProperties, sourceSkeleton);
154 if (sourceJointIndex == kInvalidIndex || sourceJointIndex >= jointMapping.size()) {
155 continue;
156 }
157
158 const size_t targetJointIndex = jointMapping[sourceJointIndex];
159 if (targetJointIndex == kInvalidIndex || targetJointIndex >= targetSkeleton.joints.size()) {
160 continue;
161 }
162
163 JointPhysicalProperties mappedProperties = jointProperties;
164 mappedProperties.jointIndex = targetJointIndex;
165 mappedProperties.jointName = targetSkeleton.joints.at(targetJointIndex).name;
166 result.push_back(std::move(mappedProperties));
167 }
168
169 return result;
170}
171
172template <typename T>
173std::vector<T> mapParents(const std::vector<T>& objects, const std::vector<size_t>& jointMapping) {
174 std::vector<T> result;
175 result.reserve(objects.size());
176 for (auto o : objects) {
177 if (o.parent == kInvalidIndex) {
178 result.push_back(o);
179 continue;
180 }
181 MT_CHECK(
182 o.parent < jointMapping.size(),
183 "Parent {} exceeds joint mapping size {}",
184 o.parent,
185 jointMapping.size());
186 o.parent = jointMapping[o.parent];
187 if (o.parent != kInvalidIndex) {
188 result.push_back(o);
189 }
190 }
191 return result;
192}
193
194ParameterLimits mapParameterLimits(
195 const ParameterLimits& limits,
196 const std::vector<size_t>& jointMapping,
197 const std::vector<size_t>& parameterMapping) {
198 ParameterLimits result;
199 result.reserve(limits.size());
200 for (auto l : limits) {
201 switch (l.type) {
203 l.data.minMax.parameterIndex = parameterMapping[l.data.minMax.parameterIndex];
204 if (l.data.minMax.parameterIndex != kInvalidIndex) {
205 result.push_back(l);
206 }
207 break;
210 l.data.minMaxJoint.jointIndex = jointMapping[l.data.minMaxJoint.jointIndex];
211 if (l.data.minMaxJoint.jointIndex != kInvalidIndex) {
212 result.push_back(l);
213 }
214 break;
216 l.data.linear.referenceIndex = parameterMapping[l.data.linear.referenceIndex];
217 l.data.linear.targetIndex = parameterMapping[l.data.linear.targetIndex];
218 if (l.data.linear.referenceIndex != kInvalidIndex &&
219 l.data.linear.targetIndex != kInvalidIndex) {
220 result.push_back(l);
221 }
222 break;
224 l.data.linearJoint.referenceJointIndex =
225 parameterMapping[l.data.linearJoint.referenceJointIndex];
226 l.data.linearJoint.targetJointIndex = parameterMapping[l.data.linearJoint.targetJointIndex];
227 if (l.data.linearJoint.referenceJointIndex != kInvalidIndex &&
228 l.data.linearJoint.targetJointIndex != kInvalidIndex) {
229 result.push_back(l);
230 }
231 break;
233 l.data.halfPlane.param1 = parameterMapping[l.data.halfPlane.param1];
234 l.data.halfPlane.param2 = parameterMapping[l.data.halfPlane.param2];
235 if (l.data.halfPlane.param1 != kInvalidIndex && l.data.halfPlane.param2 != kInvalidIndex) {
236 result.push_back(l);
237 }
238 break;
240 l.data.ellipsoid.parent = jointMapping[l.data.ellipsoid.parent];
241 l.data.ellipsoid.ellipsoidParent = jointMapping[l.data.ellipsoid.ellipsoidParent];
242 if (l.data.ellipsoid.parent != kInvalidIndex &&
243 l.data.ellipsoid.ellipsoidParent != kInvalidIndex) {
244 result.push_back(l);
245 }
246 break;
248 // nothing to do here.
249 break;
250 }
251 }
252 }
253 return result;
254}
255
256SkinWeights mapSkinWeights(SkinWeights weights, const std::vector<size_t>& jointMapping) {
257 SkinWeights result(weights);
258
259 for (Eigen::Index iRow = 0; iRow < weights.index.rows(); ++iRow) {
260 for (Eigen::Index jCol = 0; jCol < weights.index.cols(); ++jCol) {
261 if (weights.weight(iRow, jCol) == 0) {
262 continue;
263 }
264
265 const auto mapped = jointMapping[result.index(iRow, jCol)];
266 MT_CHECK(mapped != kInvalidIndex);
267 result.index(iRow, jCol) = (int)mapped;
268 }
269 }
270
271 return result;
272}
273
274template <typename T>
275std::vector<T> mergeVectors(const std::vector<T>& left, const std::vector<T>& right) {
276 std::vector<T> result;
277 result.reserve(left.size() + right.size());
278 std::copy(std::begin(left), std::end(left), std::back_inserter(result));
279 std::copy(std::begin(right), std::end(right), std::back_inserter(result));
280 return result;
281}
282
283std::vector<Eigen::Triplet<float>> toTriplets(const SparseRowMatrixf& mat) {
284 std::vector<Eigen::Triplet<float>> result;
285 for (int k = 0; k < mat.outerSize(); ++k) {
286 for (SparseRowMatrixf::InnerIterator it(mat, k); it; ++it) {
287 result.emplace_back(it.row(), it.col(), it.value());
288 }
289 }
290 return result;
291}
292
293std::vector<size_t> addMappedParameters(
294 const ParameterTransform& paramTransformOrig,
295 const std::vector<size_t>& jointMapping,
296 ParameterTransform& paramTransformResult) {
297 std::vector<bool> validParamsOrig(paramTransformOrig.numAllModelParameters());
298
299 // Pass 1: figure out which parameters we're going to keep:
300 for (Eigen::Index kJointParam = 0; kJointParam < paramTransformOrig.transform.outerSize();
301 ++kJointParam) {
302 const Eigen::Index jointIndex = kJointParam / kParametersPerJoint;
303 const size_t mappedJointIndex = jointMapping[jointIndex];
304 if (mappedJointIndex == kInvalidIndex) {
305 continue;
306 }
307
308 // Mark every model parameter that drives this kept joint as valid.
309 for (SparseRowMatrixf::InnerIterator it(paramTransformOrig.transform, kJointParam); it; ++it) {
310 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
311 validParamsOrig[it.col()] = true;
312 }
313 }
314
315 std::vector<size_t> origParamToNewParam(
316 static_cast<size_t>(paramTransformOrig.numAllModelParameters()), kInvalidIndex);
317 std::unordered_set<std::string> existingParams(
318 paramTransformResult.name.begin(), paramTransformResult.name.end());
319 for (Eigen::Index iParamOld = 0; iParamOld < paramTransformOrig.numAllModelParameters();
320 ++iParamOld) {
321 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
322 if (!validParamsOrig[iParamOld]) {
323 continue;
324 }
325
327 existingParams.find(paramTransformOrig.name[iParamOld]) != existingParams.end(),
328 "Duplicate parameter {} found while merging parameter transforms.",
329 paramTransformOrig.name[iParamOld]);
330
331 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
332 origParamToNewParam[iParamOld] = paramTransformResult.name.size();
333 paramTransformResult.name.push_back(paramTransformOrig.name[iParamOld]);
334 }
335
336 std::vector<Eigen::Triplet<float>> tripletsNew = toTriplets(paramTransformResult.transform);
337 for (Eigen::Index kJointParam = 0; kJointParam < paramTransformOrig.transform.outerSize();
338 ++kJointParam) {
339 const Eigen::Index jointIndex = kJointParam / kParametersPerJoint;
340 const Eigen::Index offset = kJointParam % kParametersPerJoint;
341 const size_t mappedJointIndex = jointMapping[jointIndex];
342 if (mappedJointIndex == kInvalidIndex) {
343 continue;
344 }
345
346 const auto mappedJointParam = mappedJointIndex * kParametersPerJoint + offset;
347
348 for (SparseRowMatrixf::InnerIterator it(paramTransformOrig.transform, kJointParam); it; ++it) {
349 const auto iOldParam = it.col();
350 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
351 const auto iNewParam = origParamToNewParam[iOldParam];
352
353 if (iNewParam != SIZE_MAX) {
354 tripletsNew.emplace_back((int)mappedJointParam, (int)iNewParam, it.value());
355 }
356 }
357 }
358 paramTransformResult.transform.resize(
359 paramTransformResult.transform.rows(), paramTransformResult.name.size());
360 paramTransformResult.transform.setFromTriplets(tripletsNew.begin(), tripletsNew.end());
361
362 // Update the parameter sets:
363 for (const auto& paramSetOrig : paramTransformOrig.parameterSets) {
364 for (Eigen::Index iOrigParam = 0; iOrigParam < paramTransformOrig.numAllModelParameters();
365 ++iOrigParam) {
366 if (!paramSetOrig.second.test(iOrigParam)) {
367 continue;
368 }
369
370 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
371 const auto iNewParam = origParamToNewParam[iOrigParam];
372 if (iNewParam != kInvalidIndex) {
373 paramTransformResult.parameterSets[paramSetOrig.first].set(iNewParam);
374 }
375 }
376 }
377
378 return origParamToNewParam;
379}
380
381LocatorList removeDuplicateLocators(LocatorList locators, const LocatorList& toRemove) {
382 std::unordered_set<std::string> toRemoveNames;
383 for (const auto& l : toRemove) {
384 toRemoveNames.insert(l.name);
385 }
386
387 locators.erase(
388 std::remove_if(
389 locators.begin(),
390 locators.end(),
391 [&toRemoveNames](const Locator& l) { return toRemoveNames.count(l.name) > 0; }),
392 locators.end());
393
394 return locators;
395}
396
397PhysicalProperties removeDuplicatePhysicalProperties(
398 PhysicalProperties properties,
399 const PhysicalProperties& toRemove) {
400 std::unordered_set<size_t> toRemoveJointIndices;
401 for (const auto& jointProperties : toRemove) {
402 toRemoveJointIndices.insert(jointProperties.jointIndex);
403 }
404
405 properties.erase(
406 std::remove_if(
407 properties.begin(),
408 properties.end(),
409 [&toRemoveJointIndices](const JointPhysicalProperties& jointProperties) {
410 return toRemoveJointIndices.count(jointProperties.jointIndex) > 0;
411 }),
412 properties.end());
413
414 return properties;
415}
416
417} // namespace
418
419Character scaleCharacter(const Character& character, float s, CharacterMassScale massScale) {
420 MT_CHECK(s > 0.0f, "scale > 0.0f, got {}", s);
421
422 return {
423 scale(character.skeleton, s),
424 character.parameterTransform,
425 scale(character.parameterLimits, s),
426 scale(character.locators, s),
427 scale(character.mesh, s).get(),
428 character.skinWeights.get(),
429 scale(character.collision, s).get(),
430 character.poseShapes.get(),
431 character.blendShape,
432 character.faceExpressionBlendShape,
433 character.name,
434 scaleInverseBindPose(character.inverseBindPose, s),
436 "",
437 scale(character.physicalProperties, s, massScale)};
438}
439
440namespace {
441
442Skeleton transformSkeleton(const Skeleton& skeleton, const Eigen::Affine3f& xform) {
443 const Eigen::Vector3f singularValues = xform.linear().jacobiSvd().singularValues();
444 for (Eigen::Index i = 0; i < 3; ++i) {
445 MT_CHECK(
446 singularValues(i) > 0.99f && singularValues(i) < 1.01f,
447 "Transform should not include scale or shear.");
448 }
449
450 const Eigen::Quaternionf rotation(xform.linear());
451 const Eigen::Vector3f translation(xform.translation());
452
453 Skeleton result(skeleton);
454 MT_CHECK(!result.joints.empty());
455 MT_CHECK(!skeleton.joints.empty());
456 if (!result.joints.empty() && !skeleton.joints.empty()) {
457 result.joints.front().preRotation = rotation * skeleton.joints.front().preRotation;
458 result.joints.front().translationOffset =
459 rotation * skeleton.joints.front().translationOffset + translation;
460 }
461 return result;
462}
463
464BlendShape_const_p transformBlendShape(
465 const BlendShape_const_p& blendShape,
466 const Eigen::Affine3f& xform) {
467 if (!blendShape) {
468 return nullptr;
469 }
470
471 const Eigen::Vector3f singularValues = xform.linear().jacobiSvd().singularValues();
472 for (Eigen::Index i = 0; i < 3; ++i) {
473 MT_CHECK(
474 singularValues(i) > 0.99f && singularValues(i) < 1.01f,
475 "Transform should not include scale or shear.");
476 }
477
478 const Eigen::Quaternionf rotation(xform.linear());
479 const Eigen::Vector3f translation(xform.translation());
480
481 momentum::BlendShape_p blendShapeTransformed =
482 std::make_unique<momentum::BlendShape>(*blendShape);
483
484 // apply the full transformation to the blendshape basis
485 std::vector<Vector3f> baseShape = blendShapeTransformed->getBaseShape();
486 for (auto& v : baseShape) {
487 v = xform * v;
488 }
489 blendShapeTransformed->setBaseShape(baseShape);
490
491 // apply only the rotation to the shape vectors
492 Eigen::MatrixXf shapeVectors = blendShapeTransformed->getShapeVectors();
493 // Apply rotation to each 3D displacement within each shape vector column
494 for (Eigen::Index iShape = 0; iShape < shapeVectors.cols(); ++iShape) {
495 for (Eigen::Index iVertex = 0; iVertex < shapeVectors.rows() / 3; ++iVertex) {
496 Eigen::Vector3f displacement = shapeVectors.block<3, 1>(iVertex * 3, iShape);
497 shapeVectors.block<3, 1>(iVertex * 3, iShape) = rotation * displacement;
498 }
499 }
500 blendShapeTransformed->setShapeVectors(shapeVectors);
501
502 return blendShapeTransformed;
503}
504
505std::unique_ptr<Mesh> transformMesh(const std::unique_ptr<Mesh>& mesh, const Eigen::Affine3f& xf) {
506 if (!mesh) {
507 return {};
508 }
509
510 auto result = std::make_unique<Mesh>(*mesh);
511 for (auto& v : result->vertices) {
512 v = xf * v;
513 }
514
515 for (auto& v : result->normals) {
516 v = xf.linear() * v;
517 }
518
519 return result;
520}
521
522TransformationList transformInverseBindPose(
523 const TransformationList& inverseBindPose,
524 const Eigen::Affine3f& xf) {
525 TransformationList result;
526 result.reserve(inverseBindPose.size());
527 const Eigen::Affine3f xfInv = xf.inverse();
528 for (const auto& m : inverseBindPose) {
529 result.push_back(m * xfInv);
530 }
531 return result;
532}
533
534std::unique_ptr<CollisionGeometry> transformCollisionGeometry(
535 const std::unique_ptr<CollisionGeometry>& collision,
536 const Eigen::Affine3f& xf) {
537 if (!collision) {
538 return {};
539 }
540
541 auto result = std::make_unique<CollisionGeometry>(*collision);
542 const Transform xformTransform(xf);
543 for (auto& primitive : *result) {
544 if (primitive.parent == kInvalidIndex) {
545 primitive.transformation = xformTransform * primitive.transformation;
546 }
547 }
548 return result;
549}
550
551} // namespace
552
553Character transformCharacter(const Character& character, const Affine3f& xform) {
554 return {
555 transformSkeleton(character.skeleton, xform),
556 character.parameterTransform,
557 character.parameterLimits,
558 character.locators,
559 transformMesh(character.mesh, xform).get(),
560 character.skinWeights.get(),
561 transformCollisionGeometry(character.collision, xform).get(),
562 character.poseShapes.get(),
563 transformBlendShape(character.blendShape, xform),
564 character.faceExpressionBlendShape,
565 character.name,
566 transformInverseBindPose(character.inverseBindPose, xform),
568 "",
569 character.physicalProperties};
570}
571
573 const Character& srcCharacter,
574 const Character& tgtCharacter,
575 const std::string& srcRootJoint,
576 const std::string& tgtRootJoint) {
577 const Skeleton& srcSkeleton = srcCharacter.skeleton;
578 const Skeleton& tgtSkeleton = tgtCharacter.skeleton;
579
580 MT_THROW_IF(srcSkeleton.joints.empty(), "Trying to reparent empty skeleton.");
581
582 const auto tgtRoot = tgtSkeleton.getJointIdByName(tgtRootJoint);
584 tgtRoot == kInvalidIndex,
585 "Unable to re-root skeleton, target root joint {} not found.",
586 tgtRootJoint);
587
588 const auto srcRoot = srcSkeleton.getJointIdByName(srcRootJoint);
590 srcRoot == kInvalidIndex,
591 "Unable to re-root skeleton, source root joint {} not found.",
592 srcRootJoint);
593
594 Skeleton combinedSkeleton;
595
596 // Make sure we don't insert duplicates:
597 std::unordered_map<std::string, size_t> combinedSkeletonJointMapping;
598
599 auto addJoint =
600 [&](const Skeleton& skeleton, size_t jointIndex, std::vector<size_t>& jointMap) -> size_t {
601 const Joint& origJoint = skeleton.joints[jointIndex];
602 Joint combinedJoint = origJoint;
604 combinedSkeletonJointMapping.find(combinedJoint.name) != combinedSkeletonJointMapping.end(),
605 "Duplicate joint '{}' found while reparenting.",
606 origJoint.name);
607 const size_t combinedIndex = combinedSkeleton.joints.size();
608 jointMap[jointIndex] = combinedIndex;
609 combinedSkeletonJointMapping.insert({combinedJoint.name, combinedIndex});
610
611 // Remap the parent:
612 if (origJoint.parent != kInvalidIndex) {
613 auto itr = combinedSkeletonJointMapping.find(skeleton.joints[origJoint.parent].name);
614 MT_CHECK(itr != combinedSkeletonJointMapping.end());
615 combinedJoint.parent = itr->second;
616 }
617
618 combinedSkeleton.joints.push_back(combinedJoint);
619 return combinedIndex;
620 };
621
622 std::vector<size_t> srcToCombinedJoints(srcCharacter.skeleton.joints.size(), kInvalidIndex);
623 std::vector<size_t> tgtToCombinedJoints(tgtCharacter.skeleton.joints.size(), kInvalidIndex);
624
625 for (size_t iTgtJoint = 0; iTgtJoint < tgtSkeleton.joints.size(); ++iTgtJoint) {
626 // Add the replacement joints just after the new parent rather than at the very end; this will
627 // improve coherency a bit.
628 if (iTgtJoint == tgtRoot) {
629 addJoint(tgtSkeleton, iTgtJoint, tgtToCombinedJoints);
630
631 // Copy the joints from the source skeleton over in place of the target root:
632 for (size_t kSrcJoint = srcRoot + 1; kSrcJoint < srcSkeleton.joints.size(); ++kSrcJoint) {
633 if (srcSkeleton.isAncestor(kSrcJoint, srcRoot)) {
634 addJoint(srcSkeleton, kSrcJoint, srcToCombinedJoints);
635 }
636 }
637 } else if (!tgtSkeleton.isAncestor(iTgtJoint, tgtRoot)) {
638 addJoint(tgtSkeleton, iTgtJoint, tgtToCombinedJoints);
639 }
640 }
641
642 // For locators, use the parent joint to decide whether to use the src locator or
643 // the target locator.
644 // For duplicate locators that are on both the src and tgt skeletons (but parented to different
645 // bones), we will always take the src locator. This is because we trust the locators on the
646 // primary skeleton's hands more than the secondary skeleton.
647 const LocatorList combinedLocators = [&]() {
648 const LocatorList remappedSrcLocators =
649 mapParents<Locator>(srcCharacter.locators, srcToCombinedJoints);
650 const LocatorList remappedTgtLocators =
651 mapParents<Locator>(tgtCharacter.locators, tgtToCombinedJoints);
652 return mergeVectors(
653 removeDuplicateLocators(remappedTgtLocators, remappedSrcLocators), remappedSrcLocators);
654 }();
655 {
656 std::unordered_set<std::string> locatorNames;
657 for (const auto& l : combinedLocators) {
658 MT_LOGW_IF(locatorNames.count(l.name) != 0, "Duplicate locator: {}", l.name);
659 locatorNames.insert(l.name);
660 }
661 }
662
663 CollisionGeometry combinedCollisionGeometry;
664 if (tgtCharacter.collision) {
665 combinedCollisionGeometry = mergeVectors(
666 combinedCollisionGeometry, mapParents(*tgtCharacter.collision, tgtToCombinedJoints));
667 }
668 if (srcCharacter.collision) {
669 combinedCollisionGeometry = mergeVectors(
670 combinedCollisionGeometry, mapParents(*srcCharacter.collision, srcToCombinedJoints));
671 }
672
673 // Build the merged parameter transform:
674 ParameterTransform combinedParamTransform;
675 combinedParamTransform.offsets =
676 VectorXf::Zero(combinedSkeleton.joints.size() * kParametersPerJoint);
677 combinedParamTransform.transform.resize(combinedSkeleton.joints.size() * kParametersPerJoint, 0);
678 const std::vector<size_t> tgtToCombinedParameters = addMappedParameters(
679 tgtCharacter.parameterTransform, tgtToCombinedJoints, combinedParamTransform);
680 const std::vector<size_t> srcToCombinedParameters = addMappedParameters(
681 srcCharacter.parameterTransform, srcToCombinedJoints, combinedParamTransform);
682 combinedParamTransform.activeJointParams = combinedParamTransform.computeActiveJointParams();
683
684 const ParameterLimits combinedParameterLimits = mergeVectors(
685 mapParameterLimits(
686 tgtCharacter.parameterLimits, tgtToCombinedJoints, tgtToCombinedParameters),
687 mapParameterLimits(
688 srcCharacter.parameterLimits, srcToCombinedJoints, srcToCombinedParameters));
689
690 std::unique_ptr<SkinWeights> skinWeightsCombined;
691 {
692 // Mapping for every joint in the target skeleton to a joint in the combined skeleton.
693 // The goal here is something that we can use to map e.g. the skinning weights.
694 // Rules are:
695 // 1. if the target joint is in the combined skeleton, then map it directly.
696 // 2. otherwise, find a name with a matching joint in the source skeleton.
697 // 3. if not, then go up one level of the hierarchy and try again.
698 std::vector<size_t> tgtToCombinedWithParents(
699 tgtCharacter.skeleton.joints.size(), kInvalidIndex);
700 for (size_t iTgtJoint = 0; iTgtJoint < tgtSkeleton.joints.size(); ++iTgtJoint) {
701 size_t tgtParent = iTgtJoint;
702 while (tgtParent != kInvalidIndex) {
703 const auto itr = combinedSkeletonJointMapping.find(tgtSkeleton.joints[tgtParent].name);
704 if (itr != combinedSkeletonJointMapping.end()) {
705 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
706 tgtToCombinedWithParents[iTgtJoint] = itr->second;
707 break;
708 }
709 tgtParent = tgtSkeleton.joints[iTgtJoint].parent;
710 }
711 MT_CHECK(tgtParent != kInvalidIndex);
712 }
713
714 if (tgtCharacter.skinWeights) {
715 skinWeightsCombined = std::make_unique<SkinWeights>(
716 mapSkinWeights(*tgtCharacter.skinWeights, tgtToCombinedWithParents));
717 }
718 }
719
720 const PhysicalProperties combinedPhysicalProperties = [&]() {
721 const PhysicalProperties remappedSrcPhysicalProperties = mapPhysicalProperties(
722 srcCharacter.physicalProperties,
723 srcCharacter.skeleton,
724 srcToCombinedJoints,
725 combinedSkeleton);
726 const PhysicalProperties remappedTgtPhysicalProperties = mapPhysicalProperties(
727 tgtCharacter.physicalProperties,
728 tgtCharacter.skeleton,
729 tgtToCombinedJoints,
730 combinedSkeleton);
731
732 // The source hierarchy replaces the target subtree. If both sides provide physical properties
733 // for the same combined joint, keep the source body rather than duplicating mass and inertia.
734 return mergeVectors(
735 removeDuplicatePhysicalProperties(
736 remappedTgtPhysicalProperties, remappedSrcPhysicalProperties),
737 remappedSrcPhysicalProperties);
738 }();
739
740 return {
741 combinedSkeleton,
742 combinedParamTransform,
743 combinedParameterLimits,
744 combinedLocators,
745 tgtCharacter.mesh.get(),
746 skinWeightsCombined.get(),
747 combinedCollisionGeometry.empty() ? nullptr : &combinedCollisionGeometry,
748 tgtCharacter.poseShapes.get(),
749 tgtCharacter.blendShape,
751 "",
754 "",
755 combinedPhysicalProperties};
756}
757
759 std::vector<bool> toRemove(character.skeleton.joints.size(), false);
760 for (const auto& j : jointsToRemove) {
761 MT_THROW_IF(j >= character.skeleton.joints.size(), "Invalid joint found in removeJoints.");
762 toRemove[j] = true;
763 }
764
765 // Remove all joints parented under the target joints as well:
766 const auto& srcSkeleton = character.skeleton;
767 std::vector<size_t> srcToResultJoints(srcSkeleton.joints.size(), kInvalidIndex);
768 Skeleton resultSkeleton;
769 for (size_t iJoint = 0; iJoint < srcSkeleton.joints.size(); ++iJoint) {
770 bool shouldRemove = false;
771
772 size_t parent = iJoint;
773 while (parent != kInvalidIndex) {
774 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
775 if (toRemove[parent]) {
776 shouldRemove = true;
777 break;
778 }
779 if (parent < character.skeleton.joints.size()) {
780 parent = character.skeleton.joints[parent].parent;
781 } else {
782 break;
783 }
784 }
785
786 if (shouldRemove) {
787 continue;
788 }
789
790 srcToResultJoints[iJoint] = resultSkeleton.joints.size();
791 auto joint = srcSkeleton.joints[iJoint];
792 if (joint.parent != kInvalidIndex) {
793 joint.parent = srcToResultJoints[joint.parent];
794 }
795 resultSkeleton.joints.push_back(joint);
796 }
797
798 ParameterTransform resultParamTransform =
800 const std::vector<size_t> srcToResultParameters =
801 addMappedParameters(character.parameterTransform, srcToResultJoints, resultParamTransform);
802
803 resultParamTransform.activeJointParams = resultParamTransform.computeActiveJointParams();
804
805 std::unique_ptr<SkinWeights> resultSkinWeights;
806 {
807 // Mapping for every joint in the target skeleton to a joint in the combined skeleton.
808 std::vector<size_t> srcToResultJointsWithParents(
809 character.skeleton.joints.size(), kInvalidIndex);
810 for (size_t iSrcJoint = 0; iSrcJoint < srcSkeleton.joints.size(); ++iSrcJoint) {
811 size_t srcParent = iSrcJoint;
812 // Anything skinned to a deleted joint should get skinned to its parent instead:
813 while (srcParent != kInvalidIndex) {
814 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
815 srcToResultJointsWithParents[iSrcJoint] = srcToResultJoints[srcParent];
816 if (srcToResultJointsWithParents[iSrcJoint] != kInvalidIndex) {
817 break;
818 }
819 srcParent = srcSkeleton.joints[srcParent].parent;
820 }
821 }
822
823 if (character.skinWeights) {
824 resultSkinWeights = std::make_unique<SkinWeights>(
825 mapSkinWeights(*character.skinWeights, srcToResultJointsWithParents));
826 }
827 }
828
829 // For locators, use the parent joint to decide whether to use the src locator or
830 // the target locator.
831 const LocatorList resultLocators = mapParents<Locator>(character.locators, srcToResultJoints);
832
833 CollisionGeometry resultCollisionGeometry;
834 if (character.collision) {
835 resultCollisionGeometry = mapParents(*character.collision, srcToResultJoints);
836 }
837
838 const ParameterLimits resultParameterLimits =
839 mapParameterLimits(character.parameterLimits, srcToResultJoints, srcToResultParameters);
840
841 const PhysicalProperties resultPhysicalProperties = mapPhysicalProperties(
842 character.physicalProperties, character.skeleton, srcToResultJoints, resultSkeleton);
843
844 return {
845 resultSkeleton,
846 resultParamTransform,
847 resultParameterLimits,
848 resultLocators,
849 character.mesh.get(),
850 resultSkinWeights.get(),
851 resultCollisionGeometry.empty() ? nullptr : &resultCollisionGeometry,
852 character.poseShapes.get(),
853 character.blendShape,
855 "",
858 "",
859 resultPhysicalProperties};
860}
861
863 const Character& character,
864 const std::string& name,
865 const Eigen::Vector3f& translationOffset,
866 const Eigen::Quaternionf& preRotation) {
867 // 1. Add a new joint to the skeleton
868 const size_t boneIndex = character.skeleton.joints.size();
869 JointList newJoints = character.skeleton.joints;
870 Joint newJoint;
871 newJoint.name = name;
872 newJoint.parent = kInvalidIndex;
873 newJoint.translationOffset = translationOffset;
874 newJoint.preRotation = preRotation;
875 newJoints.push_back(newJoint);
876 Skeleton newSkeleton(std::move(newJoints));
877
878 // 2. Extend the ParameterTransform with 6 new model parameters
879 ParameterTransform newParamTransform = character.parameterTransform;
880
881 const size_t numJoints = boneIndex + 1;
882 const size_t newNumJointParams = numJoints * kParametersPerJoint;
883 const size_t oldNumJointParams = boneIndex * kParametersPerJoint;
884
885 // Resize offsets
886 VectorXf newOffsets = VectorXf::Zero(newNumJointParams);
887 newOffsets.head(oldNumJointParams) = newParamTransform.offsets;
888 newParamTransform.offsets = std::move(newOffsets);
889
890 // Record the parameter start index and append 6 new parameter names
891 const size_t parameterStartIndex = newParamTransform.name.size();
892 newParamTransform.name.push_back(name + "_tx");
893 newParamTransform.name.push_back(name + "_ty");
894 newParamTransform.name.push_back(name + "_tz");
895 newParamTransform.name.push_back(name + "_rx");
896 newParamTransform.name.push_back(name + "_ry");
897 newParamTransform.name.push_back(name + "_rz");
898
899 // Rebuild the sparse transform matrix: keep existing triplets and add 6 new ones
900 std::vector<Eigen::Triplet<float>> triplets = toTriplets(newParamTransform.transform);
901 triplets.emplace_back(
902 static_cast<int>(boneIndex * kParametersPerJoint + TX),
903 static_cast<int>(parameterStartIndex + 0),
904 1.0f);
905 triplets.emplace_back(
906 static_cast<int>(boneIndex * kParametersPerJoint + TY),
907 static_cast<int>(parameterStartIndex + 1),
908 1.0f);
909 triplets.emplace_back(
910 static_cast<int>(boneIndex * kParametersPerJoint + TZ),
911 static_cast<int>(parameterStartIndex + 2),
912 1.0f);
913 triplets.emplace_back(
914 static_cast<int>(boneIndex * kParametersPerJoint + RX),
915 static_cast<int>(parameterStartIndex + 3),
916 1.0f);
917 triplets.emplace_back(
918 static_cast<int>(boneIndex * kParametersPerJoint + RY),
919 static_cast<int>(parameterStartIndex + 4),
920 1.0f);
921 triplets.emplace_back(
922 static_cast<int>(boneIndex * kParametersPerJoint + RZ),
923 static_cast<int>(parameterStartIndex + 5),
924 1.0f);
925
926 newParamTransform.transform.resize(newNumJointParams, newParamTransform.name.size());
927 newParamTransform.transform.setFromTriplets(triplets.begin(), triplets.end());
928
929 // Recompute activeJointParams
930 newParamTransform.activeJointParams = newParamTransform.computeActiveJointParams();
931
932 // 3. Construct and return the new Character. Pass empty inverseBindPose so the constructor calls
933 // initInverseBindPose().
934 return {
935 Character(
936 newSkeleton,
937 newParamTransform,
938 character.parameterLimits,
939 character.locators,
940 character.mesh.get(),
941 character.skinWeights.get(),
942 character.collision.get(),
943 character.poseShapes.get(),
944 character.blendShape,
945 character.faceExpressionBlendShape,
946 character.name,
948 character.skinnedLocators,
949 character.metadata,
950 character.physicalProperties),
951 boneIndex,
952 parameterStartIndex};
953}
954
956 const MotionParameters& inputMotion,
957 const Character& targetCharacter) {
958 // re-arrange poses according to the character names
959 const auto& parameterNames = std::get<0>(inputMotion);
960 const auto& motion = std::get<1>(inputMotion);
961
962 MT_CHECK(
963 static_cast<int>(parameterNames.size()) == motion.rows(),
964 "The number of parameter names {} does not match the number of rows {} in the motion.",
965 parameterNames.size(),
966 motion.rows());
967
968 MatrixXf result =
969 MatrixXf::Zero(targetCharacter.parameterTransform.numAllModelParameters(), motion.cols());
970 for (size_t i = 0; i < parameterNames.size(); i++) {
971 const auto index = targetCharacter.parameterTransform.getParameterIdByName(parameterNames[i]);
972 if (index != kInvalidIndex) {
973 result.row(index) = motion.row(i);
974 } else {
975 MT_LOGW("Model parameter {} not found in source motion", parameterNames[i]);
976 }
977 }
978
979 return result;
980}
981
983 const IdentityParameters& inputIdentity,
984 const Character& targetCharacter) {
985 // re-arrange offsets according to the character names
986 const auto& jointNames = std::get<0>(inputIdentity);
987 const auto& identity = std::get<1>(inputIdentity);
988
989 MT_CHECK(
990 static_cast<int>(jointNames.size() * kParametersPerJoint) == identity.size(),
991 "The number of joint parameters {} does not match the identity vector size {}.",
992 jointNames.size() * kParametersPerJoint,
993 identity.size());
994
995 VectorXf result = VectorXf::Zero(targetCharacter.skeleton.joints.size() * kParametersPerJoint);
996 for (size_t i = 0; i < jointNames.size(); i++) {
997 const auto index = targetCharacter.skeleton.getJointIdByName(jointNames[i]);
998 if (index != kInvalidIndex) {
999 result.template middleRows<kParametersPerJoint>(index * kParametersPerJoint).noalias() =
1000 identity.v.template middleRows<kParametersPerJoint>(i * kParametersPerJoint);
1001 } else {
1002 MT_LOGW("Joint {} not found in source identity", jointNames[i]);
1003 }
1004 }
1005
1006 return result;
1007}
1008
1009namespace {
1010
1011// Selects the entries of `baseVertices` flagged true in `activeVertices`,
1012// reserving capacity for `newVertexCount` results.
1013std::vector<Vector3f> reduceBaseShapeVertices(
1014 const std::vector<Vector3f>& baseVertices,
1015 const std::vector<bool>& activeVertices,
1016 size_t newVertexCount) {
1017 std::vector<Vector3f> newBaseVertices;
1018 newBaseVertices.reserve(newVertexCount);
1019
1020 for (size_t i = 0; i < activeVertices.size(); ++i) {
1021 if (activeVertices[i]) {
1022 newBaseVertices.push_back(baseVertices[i]);
1023 }
1024 }
1025
1026 return newBaseVertices;
1027}
1028
1029// Compacts a [vertexCount*3 x numShapes] shape-vector matrix down to
1030// [newVertexCount*3 x numShapes] by keeping the rows for active vertices.
1031MatrixXf reduceBlendShapeVectors(
1032 const MatrixXf& shapeVectors,
1033 const std::vector<bool>& activeVertices,
1034 size_t newVertexCount) {
1035 MatrixXf newShapeVectors(newVertexCount * 3, shapeVectors.cols());
1036
1037 size_t newIdx = 0;
1038 for (size_t i = 0; i < activeVertices.size(); ++i) {
1039 if (activeVertices[i]) {
1040 newShapeVectors.block(newIdx * 3, 0, 3, shapeVectors.cols()) =
1041 shapeVectors.block(i * 3, 0, 3, shapeVectors.cols());
1042 newIdx++;
1043 }
1044 }
1045
1046 return newShapeVectors;
1047}
1048
1049std::pair<std::vector<size_t>, std::vector<size_t>> createIndexMapping(
1050 const std::vector<bool>& activeElements) {
1051 std::vector<size_t> forwardMapping;
1052 std::vector<size_t> reverseMapping(activeElements.size(), kInvalidIndex);
1053 for (size_t i = 0; i < activeElements.size(); ++i) {
1054 if (activeElements[i]) {
1055 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
1056 reverseMapping[i] = forwardMapping.size();
1057 forwardMapping.push_back(i);
1058 }
1059 }
1060
1061 return {forwardMapping, reverseMapping};
1062}
1063
1064template <typename T>
1065std::vector<T> selectVertices(
1066 const std::vector<T>& sourceVector,
1067 const std::vector<size_t>& selected) {
1068 std::vector<T> result;
1069 result.reserve(selected.size());
1070
1071 for (unsigned long i : selected) {
1072 MT_CHECK(i < sourceVector.size());
1073 result.push_back(sourceVector[i]);
1074 }
1075
1076 return result;
1077}
1078
1079std::vector<Eigen::Vector3i> remapFaces(
1080 const std::vector<Eigen::Vector3i>& faces,
1081 const std::vector<size_t>& mapping,
1082 const std::vector<bool>& activeFaces) {
1083 const size_t nNewFaceCount = std::count(activeFaces.begin(), activeFaces.end(), true);
1084
1085 std::vector<Eigen::Vector3i> remappedFaces;
1086 remappedFaces.reserve(nNewFaceCount);
1087
1088 for (size_t iFace = 0; iFace < faces.size(); ++iFace) {
1089 if (!activeFaces[iFace]) {
1090 continue;
1091 }
1092
1093 const auto& face = faces[iFace];
1094 MT_CHECK(
1095 mapping[face[0]] != kInvalidIndex && mapping[face[1]] != kInvalidIndex &&
1096 mapping[face[2]] != kInvalidIndex);
1097
1098 remappedFaces.emplace_back(
1099 static_cast<int>(mapping[face[0]]),
1100 static_cast<int>(mapping[face[1]]),
1101 static_cast<int>(mapping[face[2]]));
1102 }
1103
1104 return remappedFaces;
1105}
1106
1107template <typename T>
1108void remapPolyFaces(
1109 MeshT<T>& newMesh,
1110 const MeshT<T>& mesh,
1111 const std::vector<bool>& activeVertices,
1112 const std::vector<size_t>& reverseVertexMapping,
1113 const std::vector<size_t>& reverseTexcoordMapping) {
1114 if (mesh.polyFaces.empty()) {
1115 return;
1116 }
1117
1118 newMesh.polyFaceSizes.reserve(mesh.polyFaceSizes.size());
1119 newMesh.polyFaces.reserve(mesh.polyFaces.size());
1120 if (!mesh.polyTexcoordFaces.empty()) {
1121 newMesh.polyTexcoordFaces.reserve(mesh.polyTexcoordFaces.size());
1122 }
1123
1124 uint32_t offset = 0;
1125 for (const auto polySize : mesh.polyFaceSizes) {
1126 // Check if all vertices of this polygon are active
1127 bool allActive = true;
1128 for (uint32_t i = 0; i < polySize; ++i) {
1129 const auto vtx = mesh.polyFaces[offset + i];
1130 if (vtx >= activeVertices.size() || !activeVertices[vtx]) {
1131 allActive = false;
1132 break;
1133 }
1134 }
1135
1136 if (allActive) {
1137 newMesh.polyFaceSizes.push_back(polySize);
1138 for (uint32_t i = 0; i < polySize; ++i) {
1139 newMesh.polyFaces.push_back(
1140 static_cast<uint32_t>(reverseVertexMapping[mesh.polyFaces[offset + i]]));
1141 }
1142 if (!mesh.polyTexcoordFaces.empty()) {
1143 for (uint32_t i = 0; i < polySize; ++i) {
1144 newMesh.polyTexcoordFaces.push_back(
1145 static_cast<uint32_t>(reverseTexcoordMapping[mesh.polyTexcoordFaces[offset + i]]));
1146 }
1147 }
1148 }
1149
1150 offset += polySize;
1151 }
1152}
1153
1154std::vector<std::vector<int32_t>> remapLines(
1155 const std::vector<std::vector<int32_t>>& lines,
1156 const std::vector<size_t>& mapping) {
1157 std::vector<std::vector<int32_t>> remappedLines;
1158 remappedLines.reserve(lines.size());
1159
1160 for (const auto& line : lines) {
1161 std::vector<int32_t> newLine;
1162 newLine.reserve(line.size());
1163
1164 bool lineValid = true;
1165 for (int32_t vertexIdx : line) {
1166 if (vertexIdx >= 0 && vertexIdx < static_cast<int32_t>(mapping.size()) &&
1167 mapping[vertexIdx] != kInvalidIndex) {
1168 newLine.push_back(static_cast<int32_t>(mapping[vertexIdx]));
1169 } else {
1170 lineValid = false;
1171 break;
1172 }
1173 }
1174
1175 if (lineValid && !newLine.empty()) {
1176 remappedLines.push_back(std::move(newLine));
1177 }
1178 }
1179
1180 return remappedLines;
1181}
1182
1183std::vector<bool> verticesToFaces(
1184 const std::vector<Eigen::Vector3i>& faces,
1185 const std::vector<bool>& activeVertices) {
1186 std::vector<bool> activeFaces(faces.size(), false);
1187
1188 for (size_t faceIdx = 0; faceIdx < faces.size(); ++faceIdx) {
1189 const auto& face = faces[faceIdx];
1190 // Face is active only if all its vertices are active and within bounds
1191 if (face[0] >= 0 && face[0] < static_cast<int>(activeVertices.size()) && face[1] >= 0 &&
1192 face[1] < static_cast<int>(activeVertices.size()) && face[2] >= 0 &&
1193 face[2] < static_cast<int>(activeVertices.size()) && activeVertices[face[0]] &&
1194 activeVertices[face[1]] && activeVertices[face[2]]) {
1195 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
1196 activeFaces[faceIdx] = true;
1197 }
1198 }
1199
1200 return activeFaces;
1201}
1202
1203std::vector<bool> facesToVertices(
1204 const std::vector<Eigen::Vector3i>& faces,
1205 const std::vector<bool>& activeFaces,
1206 size_t vertexCount) {
1207 MT_CHECK(
1208 activeFaces.size() == faces.size(),
1209 "Active faces size ({}) does not match face count ({})",
1210 activeFaces.size(),
1211 faces.size());
1212
1213 std::vector<bool> activeVertices(vertexCount, false);
1214
1215 for (size_t faceIdx = 0; faceIdx < activeFaces.size(); ++faceIdx) {
1216 if (activeFaces[faceIdx]) {
1217 const auto& face = faces[faceIdx];
1218 if (face[0] >= 0 && face[0] < static_cast<int>(vertexCount)) {
1219 activeVertices[face[0]] = true;
1220 }
1221 if (face[1] >= 0 && face[1] < static_cast<int>(vertexCount)) {
1222 activeVertices[face[1]] = true;
1223 }
1224 if (face[2] >= 0 && face[2] < static_cast<int>(vertexCount)) {
1225 activeVertices[face[2]] = true;
1226 }
1227 }
1228 }
1229
1230 return activeVertices;
1231}
1232
1233std::vector<bool> verticesToPolys(
1234 const std::vector<uint32_t>& polyFaces,
1235 const std::vector<uint32_t>& polyFaceSizes,
1236 const std::vector<bool>& activeVertices) {
1237 if (polyFaceSizes.empty()) {
1238 return {};
1239 }
1240 MT_CHECK(!polyFaceSizes.empty());
1241 std::vector<bool> activePolys(polyFaceSizes.size(), false);
1242 uint32_t offset = 0;
1243 for (size_t polyIdx = 0; polyIdx < polyFaceSizes.size(); ++polyIdx) {
1244 const auto polySize = polyFaceSizes[polyIdx];
1245 bool allActive = true;
1246 for (uint32_t i = 0; i < polySize; ++i) {
1247 const auto vtx = polyFaces[offset + i];
1248 if (vtx >= activeVertices.size() || !activeVertices[vtx]) {
1249 allActive = false;
1250 break;
1251 }
1252 }
1253 // NOLINTNEXTLINE(facebook-hte-LocalUncheckedArrayBounds)
1254 activePolys[polyIdx] = allActive;
1255 offset += polySize;
1256 }
1257 return activePolys;
1258}
1259
1260std::vector<bool> polysToVertices(
1261 const std::vector<uint32_t>& polyFaces,
1262 const std::vector<uint32_t>& polyFaceSizes,
1263 const std::vector<bool>& activePolys,
1264 size_t vertexCount) {
1265 std::vector<bool> activeVertices(vertexCount, false);
1266 if (activePolys.empty()) {
1267 return activeVertices;
1268 }
1269 uint32_t offset = 0;
1270 for (size_t polyIdx = 0; polyIdx < polyFaceSizes.size(); ++polyIdx) {
1271 const auto polySize = polyFaceSizes[polyIdx];
1272 if (activePolys[polyIdx]) {
1273 for (uint32_t i = 0; i < polySize; ++i) {
1274 const auto vtx = polyFaces[offset + i];
1275 if (vtx < vertexCount) {
1276 activeVertices[vtx] = true;
1277 }
1278 }
1279 }
1280 offset += polySize;
1281 }
1282 return activeVertices;
1283}
1284
1285// Internal mesh reduction function used by both reduceMeshComponents and
1286// the public reduceMeshByFaces/reduceMeshByVertices for Mesh.
1287template <typename T>
1288MeshT<T> reduceMeshInternal(
1289 const MeshT<T>& mesh,
1290 const std::vector<bool>& activeVertices,
1291 const std::vector<bool>& activeFaces) {
1292 const auto [forwardVertexMapping, reverseVertexMapping] = createIndexMapping(activeVertices);
1293
1294 MeshT<T> newMesh;
1295
1296 newMesh.vertices = selectVertices(mesh.vertices, forwardVertexMapping);
1297
1298 if (!mesh.normals.empty()) {
1299 newMesh.normals = selectVertices(mesh.normals, forwardVertexMapping);
1300 }
1301
1302 if (!mesh.colors.empty()) {
1303 newMesh.colors = selectVertices(mesh.colors, forwardVertexMapping);
1304 }
1305
1306 if (!mesh.confidence.empty()) {
1307 newMesh.confidence = selectVertices(mesh.confidence, forwardVertexMapping);
1308 }
1309
1310 newMesh.faces = remapFaces(mesh.faces, reverseVertexMapping, activeFaces);
1311
1312 if (!mesh.lines.empty()) {
1313 newMesh.lines = remapLines(mesh.lines, reverseVertexMapping);
1314 }
1315
1316 std::vector<size_t> reverseTextureVertexMapping;
1317 if (!mesh.texcoord_faces.empty() && !mesh.texcoords.empty()) {
1318 std::vector<bool> activeTextureTriangles = activeFaces;
1319 activeTextureTriangles.resize(mesh.texcoord_faces.size(), false);
1320
1321 const std::vector<bool> activeTextureVertices =
1322 facesToVertices(mesh.texcoord_faces, activeTextureTriangles, mesh.texcoords.size());
1323
1324 auto [forwardTextureVertexMapping, revTexMapping] = createIndexMapping(activeTextureVertices);
1325 reverseTextureVertexMapping = std::move(revTexMapping);
1326
1327 newMesh.texcoords = selectVertices(mesh.texcoords, forwardTextureVertexMapping);
1328 newMesh.texcoord_faces =
1329 remapFaces(mesh.texcoord_faces, reverseTextureVertexMapping, activeTextureTriangles);
1330 newMesh.texcoord_lines = remapLines(mesh.texcoord_lines, reverseTextureVertexMapping);
1331 }
1332
1333 remapPolyFaces(newMesh, mesh, activeVertices, reverseVertexMapping, reverseTextureVertexMapping);
1334
1335 return newMesh;
1336}
1337
1338// Builds a new character whose mesh, skin weights, pose shapes, and blend shapes
1339// are restricted to the given active vertices/faces. Skeleton, parameter transform,
1340// limits, locators, collision, and inverseBindPose are passed through unchanged.
1341Character reduceMeshComponents(
1342 const Character& character,
1343 const std::vector<bool>& activeVertices,
1344 const std::vector<bool>& activeFaces) {
1345 MT_CHECK(character.mesh, "Cannot reduce mesh: character has no mesh");
1346 MT_CHECK(
1347 activeVertices.size() == character.mesh->vertices.size(),
1348 "Active vertices size ({}) does not match mesh vertex count ({})",
1349 activeVertices.size(),
1350 character.mesh->vertices.size());
1351 MT_CHECK(
1352 activeFaces.size() == character.mesh->faces.size(),
1353 "Active faces size ({}) does not match mesh face count ({})",
1354 activeFaces.size(),
1355 character.mesh->faces.size());
1356
1357 const auto [forwardVertexMapping, reverseVertexMapping] = createIndexMapping(activeVertices);
1358
1359 auto newMesh = std::make_unique<Mesh>(
1360 reduceMeshInternal<float>(*character.mesh, activeVertices, activeFaces));
1361
1362 std::unique_ptr<SkinWeights> newSkinWeights;
1363 if (character.skinWeights) {
1364 newSkinWeights = std::make_unique<SkinWeights>();
1365 newSkinWeights->index.resize(forwardVertexMapping.size(), kMaxSkinJoints);
1366 newSkinWeights->weight.resize(forwardVertexMapping.size(), kMaxSkinJoints);
1367
1368 for (size_t newRowIdx = 0; newRowIdx < forwardVertexMapping.size(); ++newRowIdx) {
1369 const auto oldRowIdx = forwardVertexMapping[newRowIdx];
1370 newSkinWeights->index.row(newRowIdx) = character.skinWeights->index.row(oldRowIdx);
1371 newSkinWeights->weight.row(newRowIdx) = character.skinWeights->weight.row(oldRowIdx);
1372 }
1373 }
1374
1375 std::unique_ptr<PoseShape> newPoseShapes;
1376 if (character.poseShapes) {
1377 newPoseShapes = std::make_unique<PoseShape>(*character.poseShapes);
1378
1379 // Reduce the base shape (stored as flat vector [x1,y1,z1,x2,y2,z2,...])
1380 VectorXf newBaseShape(forwardVertexMapping.size() * 3);
1381 size_t newIdx = 0;
1382 for (size_t i = 0; i < activeVertices.size(); ++i) {
1383 if (activeVertices[i]) {
1384 newBaseShape.template segment<3>(newIdx * 3) =
1385 character.poseShapes->baseShape.template segment<3>(i * 3);
1386 newIdx++;
1387 }
1388 }
1389 newPoseShapes->baseShape = std::move(newBaseShape);
1390
1391 MatrixXf newShapeVectors(
1392 forwardVertexMapping.size() * 3, character.poseShapes->shapeVectors.cols());
1393 newIdx = 0;
1394 for (size_t i = 0; i < activeVertices.size(); ++i) {
1395 if (activeVertices[i]) {
1396 newShapeVectors.block(newIdx * 3, 0, 3, character.poseShapes->shapeVectors.cols()) =
1397 character.poseShapes->shapeVectors.block(
1398 i * 3, 0, 3, character.poseShapes->shapeVectors.cols());
1399 newIdx++;
1400 }
1401 }
1402 newPoseShapes->shapeVectors = std::move(newShapeVectors);
1403 }
1404
1405 BlendShape_const_p newBlendShape;
1406 if (character.blendShape) {
1407 auto blendShape = std::make_shared<BlendShape>();
1408
1409 std::vector<Vector3f> newBaseVertices = reduceBaseShapeVertices(
1410 character.blendShape->getBaseShape(), activeVertices, forwardVertexMapping.size());
1411 blendShape->setBaseShape(newBaseVertices);
1412
1413 MatrixXf newShapeVectors = reduceBlendShapeVectors(
1414 character.blendShape->getShapeVectors(), activeVertices, forwardVertexMapping.size());
1415 blendShape->setShapeVectors(newShapeVectors);
1416
1417 newBlendShape = blendShape;
1418 }
1419
1420 BlendShapeBase_const_p newFaceExpressionBlendShape;
1421 if (character.faceExpressionBlendShape) {
1422 auto faceBlendShape = std::make_shared<BlendShapeBase>(
1423 forwardVertexMapping.size(), character.faceExpressionBlendShape->shapeSize());
1424
1425 MatrixXf newShapeVectors = reduceBlendShapeVectors(
1426 character.faceExpressionBlendShape->getShapeVectors(),
1427 activeVertices,
1428 forwardVertexMapping.size());
1429 faceBlendShape->setShapeVectors(newShapeVectors);
1430
1431 newFaceExpressionBlendShape = faceBlendShape;
1432 }
1433
1434 return {
1435 character.skeleton,
1436 character.parameterTransform,
1437 character.parameterLimits,
1438 character.locators,
1439 newMesh.get(),
1440 newSkinWeights.get(),
1441 character.collision.get(),
1442 newPoseShapes.get(),
1443 std::move(newBlendShape),
1444 std::move(newFaceExpressionBlendShape),
1445 character.name,
1446 character.inverseBindPose,
1447 character.skinnedLocators,
1448 character.metadata,
1449 character.physicalProperties};
1450}
1451
1452} // namespace
1453
1455 const Character& character,
1456 const std::vector<bool>& activeVertices) {
1457 MT_CHECK(character.mesh, "Cannot reduce mesh: character has no mesh");
1458 MT_CHECK(
1459 activeVertices.size() == character.mesh->vertices.size(),
1460 "Active vertices size ({}) does not match mesh vertex count ({})",
1461 activeVertices.size(),
1462 character.mesh->vertices.size());
1463
1464 const auto activeFaces = verticesToFaces(*character.mesh, activeVertices);
1465
1466 return reduceMeshComponents(character, activeVertices, activeFaces);
1467}
1468
1469Character reduceMeshByFaces(const Character& character, const std::vector<bool>& activeFaces) {
1470 MT_CHECK(character.mesh, "Cannot reduce mesh: character has no mesh");
1471 MT_CHECK(
1472 activeFaces.size() == character.mesh->faces.size(),
1473 "Active faces size ({}) does not match mesh face count ({})",
1474 activeFaces.size(),
1475 character.mesh->faces.size());
1476
1477 const auto activeVertices = facesToVertices(*character.mesh, activeFaces);
1478
1479 return reduceMeshComponents(character, activeVertices, activeFaces);
1480}
1481
1482template <typename T>
1483MeshT<T> reduceMeshByVertices(const MeshT<T>& mesh, const std::vector<bool>& activeVertices) {
1484 MT_CHECK(
1485 activeVertices.size() == mesh.vertices.size(),
1486 "Active vertices size ({}) does not match mesh vertex count ({})",
1487 activeVertices.size(),
1488 mesh.vertices.size());
1489
1490 const auto activeFaces = verticesToFaces(mesh, activeVertices);
1491
1492 return reduceMeshInternal(mesh, activeVertices, activeFaces);
1493}
1494
1495template <typename T>
1496MeshT<T> reduceMeshByFaces(const MeshT<T>& mesh, const std::vector<bool>& activeFaces) {
1497 MT_CHECK(
1498 activeFaces.size() == mesh.faces.size(),
1499 "Active faces size ({}) does not match mesh face count ({})",
1500 activeFaces.size(),
1501 mesh.faces.size());
1502
1503 const auto activeVertices = facesToVertices(mesh, activeFaces);
1504
1505 return reduceMeshInternal(mesh, activeVertices, activeFaces);
1506}
1507
1508template <typename T>
1509std::vector<bool> verticesToFaces(const MeshT<T>& mesh, const std::vector<bool>& activeVertices) {
1510 MT_CHECK(
1511 activeVertices.size() == mesh.vertices.size(),
1512 "Active vertices size ({}) does not match mesh vertex count ({})",
1513 activeVertices.size(),
1514 mesh.vertices.size());
1515
1516 return verticesToFaces(mesh.faces, activeVertices);
1517}
1518
1519template <typename T>
1520std::vector<bool> facesToVertices(const MeshT<T>& mesh, const std::vector<bool>& activeFaces) {
1521 MT_CHECK(
1522 activeFaces.size() == mesh.faces.size(),
1523 "Active faces size ({}) does not match mesh face count ({})",
1524 activeFaces.size(),
1525 mesh.faces.size());
1526
1527 return facesToVertices(mesh.faces, activeFaces, mesh.vertices.size());
1528}
1529
1530template <typename T>
1531std::vector<bool> verticesToPolys(const MeshT<T>& mesh, const std::vector<bool>& activeVertices) {
1532 MT_CHECK(
1533 activeVertices.size() == mesh.vertices.size(),
1534 "Active vertices size ({}) does not match mesh vertex count ({})",
1535 activeVertices.size(),
1536 mesh.vertices.size());
1537
1538 return verticesToPolys(mesh.polyFaces, mesh.polyFaceSizes, activeVertices);
1539}
1540
1541template <typename T>
1542std::vector<bool> polysToVertices(const MeshT<T>& mesh, const std::vector<bool>& activePolys) {
1543 MT_CHECK(
1544 activePolys.size() == mesh.polyFaceSizes.size(),
1545 "Active polys size ({}) does not match polygon count ({})",
1546 activePolys.size(),
1547 mesh.polyFaceSizes.size());
1548
1549 return polysToVertices(mesh.polyFaces, mesh.polyFaceSizes, activePolys, mesh.vertices.size());
1550}
1551
1552template <typename T>
1553MeshT<T> reduceMeshByPolys(const MeshT<T>& mesh, const std::vector<bool>& activePolys) {
1554 MT_CHECK(
1555 activePolys.size() == mesh.polyFaceSizes.size(),
1556 "Active polys size ({}) does not match polygon count ({})",
1557 activePolys.size(),
1558 mesh.polyFaceSizes.size());
1559
1560 const auto activeVertices = polysToVertices(mesh, activePolys);
1561 const auto activeFaces = verticesToFaces(mesh, activeVertices);
1562
1563 return reduceMeshInternal(mesh, activeVertices, activeFaces);
1564}
1565
1566Character reduceMeshByPolys(const Character& character, const std::vector<bool>& activePolys) {
1567 MT_CHECK(character.mesh, "Cannot reduce mesh: character has no mesh");
1568 MT_CHECK(
1569 activePolys.size() == character.mesh->polyFaceSizes.size(),
1570 "Active polys size ({}) does not match polygon count ({})",
1571 activePolys.size(),
1572 character.mesh->polyFaceSizes.size());
1573
1574 const auto activeVertices = polysToVertices(*character.mesh, activePolys);
1575 const auto activeFaces = verticesToFaces(*character.mesh, activeVertices);
1576
1577 return reduceMeshComponents(character, activeVertices, activeFaces);
1578}
1579
1581 const MeshT<float>& mesh,
1582 const std::vector<bool>& activeVertices);
1583
1585 const MeshT<double>& mesh,
1586 const std::vector<bool>& activeVertices);
1587
1589 const MeshT<float>& mesh,
1590 const std::vector<bool>& activeFaces);
1591
1593 const MeshT<double>& mesh,
1594 const std::vector<bool>& activeFaces);
1595
1596template std::vector<bool> verticesToFaces<float>(
1597 const MeshT<float>& mesh,
1598 const std::vector<bool>& activeVertices);
1599
1600template std::vector<bool> verticesToFaces<double>(
1601 const MeshT<double>& mesh,
1602 const std::vector<bool>& activeVertices);
1603
1604template std::vector<bool> facesToVertices<float>(
1605 const MeshT<float>& mesh,
1606 const std::vector<bool>& activeFaces);
1607
1608template std::vector<bool> facesToVertices<double>(
1609 const MeshT<double>& mesh,
1610 const std::vector<bool>& activeFaces);
1611
1612template std::vector<bool> verticesToPolys<float>(
1613 const MeshT<float>& mesh,
1614 const std::vector<bool>& activeVertices);
1615
1616template std::vector<bool> verticesToPolys<double>(
1617 const MeshT<double>& mesh,
1618 const std::vector<bool>& activeVertices);
1619
1620template std::vector<bool> polysToVertices<float>(
1621 const MeshT<float>& mesh,
1622 const std::vector<bool>& activePolys);
1623
1624template std::vector<bool> polysToVertices<double>(
1625 const MeshT<double>& mesh,
1626 const std::vector<bool>& activePolys);
1627
1629 const MeshT<float>& mesh,
1630 const std::vector<bool>& activePolys);
1631
1633 const MeshT<double>& mesh,
1634 const std::vector<bool>& activePolys);
1635
1636} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
Definition exception.h:31
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
#define MT_LOGW(...)
Definition log.h:180
#define MT_LOGW_IF(condition,...)
Definition log.h:186
template MeshT< double > reduceMeshByVertices< double >(const MeshT< double > &mesh, const std::vector< bool > &activeVertices)
TransformT< float > Transform
Definition fwd.h:77
CollisionGeometryT< float > CollisionGeometry
SkeletonT< float > Skeleton
Definition fwd.h:263
Affine3< float > Affine3f
Definition types.h:405
::std::shared_ptr< const BlendShapeBase > BlendShapeBase_const_p
Definition fwd.h:32
std::tuple< std::vector< std::string >, JointParameters > IdentityParameters
A tuple of skeleton joint names and identity parameters.
Definition types.h:179
template std::vector< bool > facesToVertices< double >(const MeshT< double > &mesh, const std::vector< bool > &activeFaces)
Character replaceSkeletonHierarchy(const Character &srcCharacter, const Character &tgtCharacter, const std::string &srcRootJoint, const std::string &tgtRootJoint)
Replaces the subtree of tgtCharacter's skeleton rooted at tgtRootJoint with the subtree of srcCharact...
Character removeJoints(const Character &character, momentum::span< const size_t > jointsToRemove)
Removes the specified joints and any joints parented beneath them from the character.
JointListT< float > JointList
Definition joint.h:81
JointParameters mapIdentityToCharacter(const IdentityParameters &inputIdentity, const Character &targetCharacter)
Maps the input JointParameter vector to a target character by matching joint names.
template MeshT< float > reduceMeshByFaces< float >(const MeshT< float > &mesh, const std::vector< bool > &activeFaces)
template std::vector< bool > verticesToPolys< double >(const MeshT< double > &mesh, const std::vector< bool > &activeVertices)
std::vector< bool > facesToVertices(const MeshT< T > &mesh, const std::vector< bool > &activeFaces)
Converts a face selection to a vertex selection.
Character reduceMeshByVertices(const Character &character, const std::vector< bool > &activeVertices)
Reduces the character's mesh to only include the specified vertices and associated faces.
@ TY
Definition types.h:22
@ TZ
Definition types.h:22
@ TX
Definition types.h:22
@ RX
Definition types.h:22
@ RZ
Definition types.h:22
@ RY
Definition types.h:22
MatrixXf mapMotionToCharacter(const MotionParameters &inputMotion, const Character &targetCharacter)
Maps the input ModelParameter motion to a target character by matching model parameter names.
std::tuple< std::vector< std::string >, Eigen::MatrixXf > MotionParameters
A tuple of model parameter names and a matrix of poses across a sequence of frames.
Definition types.h:173
std::vector< Affine3f > TransformationList
One affine transform per joint, indexed by joint id.
Definition types.h:410
std::vector< ParameterLimit > ParameterLimits
template MeshT< float > reduceMeshByPolys< float >(const MeshT< float > &mesh, const std::vector< bool > &activePolys)
template std::vector< bool > verticesToFaces< double >(const MeshT< double > &mesh, const std::vector< bool > &activeVertices)
::std::shared_ptr< BlendShape > BlendShape_p
Definition fwd.h:20
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
template std::vector< bool > verticesToFaces< float >(const MeshT< float > &mesh, const std::vector< bool > &activeVertices)
std::vector< bool > verticesToPolys(const MeshT< T > &mesh, const std::vector< bool > &activeVertices)
Converts a vertex selection to a polygon selection.
std::vector< JointPhysicalProperties > PhysicalProperties
Optional physical mass properties for a character.
Definition joint.h:114
std::vector< SkinnedLocator > SkinnedLocatorList
A collection of locators attached to a skeleton.
template std::vector< bool > facesToVertices< float >(const MeshT< float > &mesh, const std::vector< bool > &activeFaces)
JointParametersT< float > JointParameters
Definition types.h:135
template MeshT< double > reduceMeshByPolys< double >(const MeshT< double > &mesh, const std::vector< bool > &activePolys)
MeshT< T > reduceMeshByPolys(const MeshT< T > &mesh, const std::vector< bool > &activePolys)
Reduces a standalone mesh to only include the specified polygons and associated vertices.
RigidTransformNodeResult addRigidTransformNode(const Character &character, const std::string &name, const Eigen::Vector3f &translationOffset, const Eigen::Quaternionf &preRotation)
Adds a new root-level joint with 6 rigid DOF parameters (tx/ty/tz/rx/ry/rz) to a character.
constexpr size_t kParametersPerJoint
Definition types.h:21
template MeshT< double > reduceMeshByFaces< double >(const MeshT< double > &mesh, const std::vector< bool > &activeFaces)
JointT< float > Joint
Definition fwd.h:206
std::vector< bool > polysToVertices(const MeshT< T > &mesh, const std::vector< bool > &activePolys)
Converts a polygon selection to a vertex selection.
template std::vector< bool > verticesToPolys< float >(const MeshT< float > &mesh, const std::vector< bool > &activeVertices)
template std::vector< bool > polysToVertices< float >(const MeshT< float > &mesh, const std::vector< bool > &activePolys)
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
size_t resolvePhysicalPropertiesJointIndex(const JointPhysicalProperties &properties, const Skeleton &skeleton)
Resolves physical mass properties to a skeleton joint.
VectorX< float > VectorXf
Definition types.h:209
template std::vector< bool > polysToVertices< double >(const MeshT< double > &mesh, const std::vector< bool > &activePolys)
CharacterMassScale
Policy for choosing how physical mass changes when scaling a character.
@ PreserveDensity
Preserve density while scaling lengths, so mass changes with volume.
@ PreserveMass
Preserve total mass while scaling lengths. This is appropriate for unit conversions.
Character reduceMeshByFaces(const Character &character, const std::vector< bool > &activeFaces)
Reduces the character's mesh to only include the specified faces and associated vertices.
::std::shared_ptr< const BlendShape > BlendShape_const_p
Definition fwd.h:23
std::vector< bool > verticesToFaces(const MeshT< T > &mesh, const std::vector< bool > &activeVertices)
Converts a vertex selection to a face selection.
constexpr uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
template MeshT< float > reduceMeshByVertices< float >(const MeshT< float > &mesh, const std::vector< bool > &activeVertices)
SparseRowMatrix< float > SparseRowMatrixf
Definition types.h:193
MatrixX< float > MatrixXf
Definition types.h:82
Character transformCharacter(const Character &character, const Affine3f &xform)
Transforms the character (mesh and skeleton) by the desired transformation matrix.
std::vector< Locator > LocatorList
A collection of locators attached to a skeleton.
Definition locator.h:79
Character scaleCharacter(const Character &character, float s, CharacterMassScale massScale)
Scales the character (mesh and skeleton) by the desired amount.
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
TransformationList inverseBindPose
Inverse of the bind pose transformations for each joint.
Definition character.h:76
LocatorList locators
Points of interest attached to joints.
Definition character.h:49
SkinWeights_u skinWeights
Defines how mesh vertices are influenced by joints.
Definition character.h:58
PhysicalProperties physicalProperties
Joint-level physical mass and inertia data loaded from model assets.
Definition character.h:67
BlendShapeBase_const_p faceExpressionBlendShape
Facial expression blend shapes.
Definition character.h:73
ParameterLimits parameterLimits
Constraints on model parameters.
Definition character.h:46
Mesh_u mesh
3D mesh representing the character's surface
Definition character.h:55
PoseShape_u poseShapes
Pose-dependent shape corrections.
Definition character.h:61
Skeleton skeleton
Skeletal structure defining the character's joints and hierarchy.
Definition character.h:36
std::string name
Character identifier.
Definition character.h:82
SkinnedLocatorList skinnedLocators
Points of interest attached to joints, with skinning weights.
Definition character.h:52
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
Definition character.h:39
BlendShape_const_p blendShape
Shape variations that can be blended.
Definition character.h:70
std::string metadata
Metadata (as a JSON-serialized string).
Definition character.h:85
CollisionGeometry_u collision
Collision volumes for the character.
Definition character.h:64
Physical mass properties attached to one skeleton joint.
Definition joint.h:88
size_t jointIndex
Cached joint index.
Definition joint.h:97
std::string jointName
Skeleton joint that owns this physical body.
Definition joint.h:93
size_t parent
Index of the parent joint in the skeleton hierarchy, or kInvalidIndex for the root.
Definition joint.h:24
Quaternion< T > preRotation
Pre-rotation from the parent joint's axes to this joint's local axes.
Definition joint.h:32
Vector3< T > translationOffset
Constant translation offset from the parent joint's origin to this joint's origin,...
Definition joint.h:36
std::string name
Definition joint.h:21
Represents a point attached to a joint in a skeleton.
Definition locator.h:19
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
Definition mesh.h:20
std::vector< uint32_t > polyFaces
Packed polygon face vertex indices (all polygons concatenated back-to-back).
Definition mesh.h:67
std::vector< uint32_t > polyFaceSizes
Number of vertices in each polygon face.
Definition mesh.h:73
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
Definition mesh.h:22
std::vector< Eigen::Vector3i > faces
List of triangular faces defined by vertex indices.
Definition mesh.h:28
VectorX< bool > activeJointParams
The list of joint parameters that are actually active and influenced from the transform.
static ParameterTransformT< float > empty(size_t nJointParameters)
VectorX< bool > computeActiveJointParams(const ParameterSet &ps=allParams()) const
Compute activeJointParams based on the transform and the input ParameterSet.
Eigen::Index numAllModelParameters() const
Dimension of all model parameters, including pose, scaling, marker joints, and blendshape parameters ...
std::vector< std::string > name
The list of model parameter names.
size_t getParameterIdByName(const std::string &nm) const
Return the index of a model parameter from its name.
SparseRowMatrix< T > transform
The sparse mapping matrix that maps model parameters to joint parameters.
Result of addRigidTransformNode containing the new character and indices of the added joint and model...
size_t getJointIdByName(std::string_view name) const
Returns the index of a joint with the given name, or kInvalidIndex if not found.
Definition skeleton.cpp:25
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
Definition skeleton.h:25
bool isAncestor(size_t jointId, size_t ancestorJointId) const
Returns true if ancestorJointId is an ancestor of jointId.
Definition skeleton.cpp:73
Linear blend skinning weights for a character mesh: per-vertex joint indices and influence weights.