Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skinning_weight_iterator.h
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
8#pragma once
9
12#include <momentum/math/fwd.h>
13
14namespace momentum {
15
16template <typename T>
18 size_t parentBone{};
19 float weight{};
20 Eigen::Vector3<T> weightedWorldSpacePoint;
21
22 bool operator<(const BoneWeightT<T>& rhs) const {
23 return parentBone < rhs.parentBone;
24 }
25
26 bool operator>(const BoneWeightT<T>& rhs) const {
27 return parentBone > rhs.parentBone;
28 }
29
31 this->weight += rhs.weight;
32 this->weightedWorldSpacePoint += rhs.weightedWorldSpacePoint;
33 return *this;
34 }
35};
36
37// When we compute the derivatives of skinned points, we usually
38// end up with a loop like this:
39// for each skinned point i
40// for each skinning bone and weight B_ij, w_ij
41// for each ancestor k
42// compute the weighted change in the skinned point wrt the ancestor
43// The redundancy here is that most points are skinned to a set of bones in the
44// same hierarchy (for example, two parts of the arm) and so it's redundant to
45// walk up the whole hierarchy from scratch for each skinned bone.
46//
47// This class instead does a single pass up the tree from leaf-most to root-most
48// skinned bones, accumulating skinning weights along the way. By packaging it
49// in this class we hide the complexity and simplify the jacobian/gradient
50// calculation.
51template <typename T>
53 public:
55 const Character& character,
56 const MeshT<T>& restMesh,
57 const SkeletonStateT<T>& skelState,
58 Eigen::Index vertexIndex);
59
61 const Character& character,
62 const SkinnedLocator& locator,
63 const Eigen::Vector3<T>& locatorPosition,
64 const SkeletonStateT<T>& skelState);
65
66 [[nodiscard]] bool finished() const;
67
68 // Returns the tuple <parent bone index, bone weight, vertex position in world space wrt the
69 // current bone>
70 std::tuple<size_t, T, Eigen::Vector3<T>> next();
71
72 private:
73 void checkInvariants();
74
75 std::array<BoneWeightT<T>, kMaxSkinJoints> boneWeights;
76 int nBoneWeights;
77 const Character& character;
78};
79
80} // namespace momentum
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 uint32_t kMaxSkinJoints
Maximum number of joints that can influence a single vertex.
bool operator>(const BoneWeightT< T > &rhs) const
bool operator<(const BoneWeightT< T > &rhs) const
Eigen::Vector3< T > weightedWorldSpacePoint
BoneWeightT< T > & operator+=(const BoneWeightT< T > &rhs)
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
Represents the complete state of a skeleton.
A locator attached to multiple joints, with its runtime position determined by blending the skinning ...