Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
model_parameters_sequence_error_function.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
14
15namespace momentum {
16
17template <typename T>
24
25template <typename T>
27 const Character& character)
28 : ModelParametersSequenceErrorFunctionT<T>(character.skeleton, character.parameterTransform) {}
29
30template <typename T>
32 std::span<const ModelParametersT<T>> modelParameters,
33 std::span<const SkeletonStateT<T>> /* skelStates */,
34 std::span<const MeshStateT<T>> /* meshStates */) const {
35 // ignore if we don't have any reasonable data
36 if (targetWeights_.size() !=
37 gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters())) {
38 return 0.0;
39 }
40
41 const auto np = gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters());
42
43 MT_CHECK(modelParameters.size() == 2);
44 const auto& prevParams = modelParameters[0];
45 const auto& nextParams = modelParameters[1];
46
47 // calculate difference between parameters and desired parameters
48 double error = 0;
49 for (Eigen::Index i = 0; i < np; ++i) {
50 if (this->enabledParameters_.test(i) && targetWeights_(i) > 0) {
51 const auto pdiff = targetWeights_(i) * (nextParams(i) - prevParams(i));
52 error += pdiff * pdiff;
53 }
54 }
55
56 // return error
57 return error * this->weight_ * kMotionWeight;
58}
59
60template <typename T>
62 std::span<const ModelParametersT<T>> modelParameters,
63 std::span<const SkeletonStateT<T>> /* skelStates */,
64 std::span<const MeshStateT<T>> /* meshStates */,
65 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
66 // ignore if we don't have any reasonable data
67 if (targetWeights_.size() !=
68 gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters())) {
69 return 0.0;
70 }
71
72 const auto np = gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters());
73
74 MT_CHECK(modelParameters.size() == 2);
75 const auto& prevParams = modelParameters[0];
76 const auto& nextParams = modelParameters[1];
77
78 Eigen::Ref<Eigen::VectorX<T>> prevGrad = gradient.segment(0, np);
79 Eigen::Ref<Eigen::VectorX<T>> nextGrad = gradient.segment(np, np);
80
81 double error = 0;
82 for (Eigen::Index i = 0; i < np; ++i) {
83 if (this->enabledParameters_.test(i) && targetWeights_(i) > 0) {
84 const auto pdiff = targetWeights_(i) * (nextParams(i) - prevParams(i));
85 prevGrad(i) -= 2.0f * targetWeights_(i) * pdiff * this->weight_ * kMotionWeight;
86 nextGrad(i) += 2.0f * targetWeights_(i) * pdiff * this->weight_ * kMotionWeight;
87 error += pdiff * pdiff;
88 }
89 }
90
91 // return error
92 return error * this->weight_ * kMotionWeight;
93}
94
95template <typename T>
97 // Count only parameters that are both enabled and positively weighted, matching the row-emission
98 // condition in getGradient()/getJacobian() so getJacobianSize() stays consistent with usedRows
99 // when a caller disables parameters via setEnabledParameters().
100 const auto np = gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters());
101 if (targetWeights_.size() != np) {
102 return 0;
103 }
104 size_t count = 0;
105 for (Eigen::Index i = 0; i < np; ++i) {
106 if (this->enabledParameters_.test(i) && targetWeights_(i) > 0) {
107 ++count;
108 }
109 }
110 return count;
111}
112
113template <typename T>
115 std::span<const ModelParametersT<T>> modelParameters,
116 std::span<const SkeletonStateT<T>> /* skelStates */,
117 std::span<const MeshStateT<T>> /* meshStates */,
118 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
119 Eigen::Ref<Eigen::VectorX<T>> residual,
120 int& usedRows) const {
122 MT_CHECK(
123 jacobian.cols() ==
124 gsl::narrow_cast<Eigen::Index>(
125 numFrames() * this->parameterTransform_.numAllModelParameters()));
126
127 usedRows = 0;
128
129 const auto np = gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters());
130
131 // ignore if we don't have any reasonable data
132 if (targetWeights_.size() !=
133 gsl::narrow_cast<Eigen::Index>(this->parameterTransform_.numAllModelParameters())) {
134 return 0.0;
135 }
136
137 const float sWeight = std::sqrt(this->weight_ * kMotionWeight);
138
139 MT_CHECK(modelParameters.size() == 2);
140 const auto& prevParams = modelParameters[0];
141 const auto& nextParams = modelParameters[1];
142 Eigen::Ref<Eigen::MatrixX<T>> prevJac = jacobian.topLeftCorner(jacobian.rows(), np);
143 Eigen::Ref<Eigen::MatrixX<T>> nextJac = jacobian.block(0, np, jacobian.rows(), np);
144
145 Eigen::Index out = 0;
146
147 // calculate difference between parameters and desired parameters
148 double error = 0;
149 for (Eigen::Index i = 0; i < np; ++i) {
150 if (this->enabledParameters_.test(i) && targetWeights_(i) > 0) {
151 const auto pdiff = targetWeights_(i) * (nextParams(i) - prevParams(i));
152 error += pdiff * pdiff;
153 residual(out) = pdiff * sWeight;
154 prevJac(out, i) = -sWeight * targetWeights_(i);
155 nextJac(out, i) = sWeight * targetWeights_(i);
156 ++out;
157 }
158 }
159
160 MT_CHECK(out <= residual.rows() && out <= jacobian.rows());
161
162 usedRows = gsl::narrow_cast<int>(out);
163
164 // return error
165 return error * this->weight_ * kMotionWeight;
166}
167
170
171} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
ModelParametersSequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
double getJacobian(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) const final
double getError(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates) const final
double getGradient(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates, Eigen::Ref< Eigen::VectorX< T > > gradient) const final
const ParameterTransform & parameterTransform_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
Definition profile.h:110
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
A vector of size numModelParams holding the values of all model parameters for a character.
Definition types.h:99
Eigen::Index numAllModelParameters() const
Dimension of all model parameters, including pose, scaling, marker joints, and blendshape parameters ...
Represents the complete state of a skeleton.