Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skinning_weight_iterator.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
9
16#include "momentum/math/mesh.h"
17
18namespace momentum {
19
20template <typename T>
22 const Character& character,
23 const MeshT<T>& restMesh,
24 const SkeletonStateT<T>& skelState,
25 Eigen::Index vertexIndex)
26 : character(character) {
27 const auto& skinWeights = *character.skinWeights;
28 nBoneWeights = 0;
29 {
30 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
31 const auto w = skinWeights.weight(vertexIndex, i);
32 const auto parentBone = skinWeights.index(vertexIndex, i);
33 if (w > 0) {
34 if (parentBone < character.inverseBindPose.size() &&
35 parentBone < skelState.jointState.size()) {
36 boneWeights[nBoneWeights++] = {
37 parentBone,
38 w,
39 T(w) *
40 (skelState.jointState[parentBone].transform *
41 (character.inverseBindPose[parentBone].template cast<T>() *
42 restMesh.vertices[vertexIndex]))};
43 }
44 }
45 }
46 std::sort(
47 boneWeights.begin(), boneWeights.begin() + nBoneWeights, std::greater<BoneWeightT<T>>());
48 }
49 checkInvariants();
50}
51
52template <typename T>
54 const Character& character,
55 const SkinnedLocator& locator,
56 const Eigen::Vector3<T>& locatorPosition,
57 const SkeletonStateT<T>& skelState)
58 : character(character) {
59 nBoneWeights = 0;
60 for (uint32_t i = 0; i < kMaxSkinJoints; ++i) {
61 const auto w = locator.skinWeights[i];
62 const auto parentBone = locator.parents[i];
63 if (w > 0 && parentBone < character.inverseBindPose.size() &&
64 parentBone < skelState.jointState.size()) {
65 boneWeights[nBoneWeights++] = {
66 parentBone,
67 w,
68 T(w) *
69 (skelState.jointState[parentBone].transform *
70 (character.inverseBindPose[parentBone].template cast<T>() *
71 locatorPosition.template cast<T>()))};
72 }
73 }
74 std::sort(
75 boneWeights.begin(), boneWeights.begin() + nBoneWeights, std::greater<BoneWeightT<T>>());
76}
77
78template <typename T>
80 return nBoneWeights == 0;
81}
82
83// Returns the tuple <parent bone index, bone weight, vertex position in world space wrt the
84// current bone>
85template <typename T>
86std::tuple<size_t, T, Eigen::Vector3<T>> SkinningWeightIteratorT<T>::next() {
87 MT_CHECK(nBoneWeights != 0);
88 const BoneWeightT<T> result = boneWeights[0];
89
90 boneWeights[0].parentBone = this->character.skeleton.joints[result.parentBone].parent;
91 if (boneWeights[0].parentBone == kInvalidIndex) {
92 // Reached the root, so we're done with this one:
93 for (int i = 1; i < nBoneWeights; ++i) {
94 boneWeights[i - 1] = boneWeights[i];
95 }
96 --nBoneWeights;
97 } else {
98 // We decreased the bone index by moving up to its parent; now figure out
99 // where the bone weight should be relocated in the list.
100 int i = 1;
101 while (i < nBoneWeights && boneWeights[i - 1].parentBone < boneWeights[i].parentBone) {
102 std::swap(boneWeights[i - 1], boneWeights[i]);
103 ++i;
104 }
105
106 if (i < nBoneWeights && boneWeights[i - 1].parentBone == boneWeights[i].parentBone) {
107 // Merge them:
108 boneWeights[i - 1] += boneWeights[i];
109
110 // strip out the duplicate:
111 ++i;
112 while (i < nBoneWeights) {
113 boneWeights[i - 1] = boneWeights[i];
114 ++i;
115 }
116 --nBoneWeights;
117 }
118 }
119
120 checkInvariants();
121 return {result.parentBone, result.weight, result.weightedWorldSpacePoint / result.weight};
122}
123
124template <typename T>
125void SkinningWeightIteratorT<T>::checkInvariants() {
126#ifndef NDEBUG
127 if (nBoneWeights > 0) {
128 // Check the invariants:
129 float sum = 0;
130 for (int i = 0; i < nBoneWeights; ++i) {
131 MT_CHECK(boneWeights[i].parentBone < this->character.skeleton.joints.size());
132 MT_CHECK(boneWeights[i].weight > 0 && boneWeights[i].weight <= 1.001);
133 MT_CHECK(i == 0 || boneWeights[i - 1].parentBone > boneWeights[i].parentBone);
134 sum += boneWeights[i].weight;
135 }
136 MT_CHECK(sum > 0.99f && sum < 1.01f);
137 }
138#endif
139}
140
141template class SkinningWeightIteratorT<float>;
143
144} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
SkinningWeightIteratorT(const Character &character, const MeshT< T > &restMesh, const SkeletonStateT< T > &skelState, Eigen::Index vertexIndex)
std::tuple< size_t, T, Eigen::Vector3< T > > next()
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
constexpr uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Definition types.h:466
Eigen::Vector3< T > weightedWorldSpacePoint
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
Definition mesh.h:20
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
Definition mesh.h:22
Represents the complete state of a skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.
A locator attached to multiple joints, with its runtime position determined by blending the skinning ...
Eigen::Matrix< uint32_t, kMaxSkinJoints, 1 > parents
Indices of the parent joints used for skinning.
Eigen::Matrix< float, kMaxSkinJoints, 1 > skinWeights
Skinning weight for each entry in parents. Typically sums to 1.0.