Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
pose_prior_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
13
14namespace momentum {
15
16template <typename T>
18 const Skeleton& skel,
19 const ParameterTransform& pt,
20 std::shared_ptr<const MppcaT<T>> pp)
21 : SkeletonErrorFunctionT<T>(skel, pt), posePrior_(std::move(pp)) {
22 if (posePrior_) {
24 }
25}
26
27template <typename T>
29 const Character& character,
30 std::shared_ptr<const MppcaT<T>> pp)
31 : PosePriorErrorFunctionT<T>(character.skeleton, character.parameterTransform, std::move(pp)) {}
32
33template <typename T>
34void PosePriorErrorFunctionT<T>::setPosePrior(std::shared_ptr<const MppcaT<T>> pp) {
35 posePrior_ = std::move(pp);
36
38}
39
40template <typename T>
42 // create map from posePrior to this->parameterTransform_ and the other way round
43 ppMap_.resize(posePrior_->names.size(), kInvalidIndex);
44 invMap_.resize(this->parameterTransform_.numAllModelParameters(), kInvalidIndex);
45
46 for (size_t i = 0; i < posePrior_->names.size(); i++) {
47 for (Eigen::Index j = 0; j < this->parameterTransform_.numAllModelParameters(); j++) {
48 if (posePrior_->names[i] == this->parameterTransform_.name[j]) {
49 ppMap_[i] = j;
50 invMap_[j] = i;
51 }
52 }
53 }
54}
55
56template <typename T>
57Eigen::VectorX<T> mapParameters(const ModelParametersT<T>& in, const std::vector<size_t>& mp) {
58 Eigen::VectorX<T> res = Eigen::VectorX<T>::Zero(mp.size());
59 for (size_t i = 0; i < mp.size(); i++) {
60 if (mp[i] != kInvalidIndex) {
61 res[i] = in[mp[i]];
62 }
63 }
64 return res;
65}
66
67template <typename T>
69 // loop over all joints and check for smoothness
70 if (invMap_.size() != gsl::narrow_cast<size_t>(params.size())) {
71 return 0.0;
72 }
73
74 // NOTE: this is NOT the correct value for a full MPPCA. To simplify computation we only
75 // consider the gaussian distribution which is "closest" to our data point and treat it as
76 // if all other mixtures don't exist.
77 size_t meanShapeIdx = 0;
78 Eigen::VectorX<T> bestDiff;
79 T minDist, bestR;
80 getBestFitMode(params, meanShapeIdx, bestDiff, bestR, minDist);
81
82 return -minDist;
83}
84
85template <typename T>
87 const ModelParametersT<T>& params,
88 const SkeletonStateT<T>& /*state*/,
89 const MeshStateT<T>& /* meshState */) {
90 // return error
91 return -logProbability(params) * kPosePriorWeight * this->weight_;
92}
93
94template <typename T>
96 const ModelParametersT<T>& params,
97 const SkeletonStateT<T>& /*state*/,
98 const MeshStateT<T>& /* meshState */,
99 Eigen::Ref<Eigen::VectorX<T>> gradient) {
101
102 // loop over all joints and check for smoothness
103 double error = 0.0;
104
105 if (!posePrior_ || invMap_.size() != gsl::narrow_cast<size_t>(params.size())) {
106 return error;
107 }
108
109 const auto& posePrior = *posePrior_;
110
111 // NOTE: this is NOT the correct gradient for an MPPCA. To simplify computation we only
112 // consider the gaussian distribution which is "closest" to our data point and treat it as
113 // if all other mixtures don't exist.
114 size_t meanShapeIdx = 0;
115 Eigen::VectorX<T> bestDiff;
116 T minDist, bestR;
117 getBestFitMode(params, meanShapeIdx, bestDiff, bestR, minDist);
118
119 if (meanShapeIdx == kInvalidIndex) {
120 return 0;
121 }
122
123 error += minDist;
124
125 // calculate the gradient for the best parameter
126 for (size_t i = 0; i < posePrior.d; i++) {
127 if (ppMap_[i] != kInvalidIndex) {
128 gradient(ppMap_[i]) +=
129 bestDiff.dot(posePrior.Cinv[meanShapeIdx].col(i)) * kPosePriorWeight * this->weight_;
130 }
131 }
132
133 // return error
134 return error * kPosePriorWeight * this->weight_;
135}
136
137template <typename T>
139 if (posePrior_) {
140 return posePrior_->d;
141 } else {
142 return 0;
143 }
144}
145
146template <typename T>
148 const ModelParametersT<T>& params,
149 const SkeletonStateT<T>& /*state*/,
150 const MeshStateT<T>& /* meshState */,
151 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
152 Eigen::Ref<Eigen::VectorX<T>> residual,
153 int& usedRows) {
155
156 // loop over all joints and check for smoothness
157 double error = 0.0;
158 usedRows = 0;
159
160 if (!posePrior_ || invMap_.size() != gsl::narrow_cast<size_t>(params.size())) {
161 return error;
162 }
163
164 const auto& posePrior = *posePrior_;
165
166 // NOTE: this is NOT the correct jacobian for an MPPCA. To simplify computation we only
167 // consider the gaussian distribution which is "closest" to our data point and treat it as
168 // if all other mixtures don't exist.
169 size_t meanShapeIdx = 0;
170 Eigen::VectorX<T> bestDiff;
171 T minDist, bestR;
172
173 getBestFitMode(params, meanShapeIdx, bestDiff, bestR, minDist);
174
175 if (meanShapeIdx == kInvalidIndex) {
176 return T(0);
177 }
178
179 error += minDist * kPosePriorWeight * this->weight_;
180
181 const T wgt = std::sqrt(T(0.5) * kPosePriorWeight * this->weight_);
182
183 // calculate the gradient for the best parameter
184 // NOTE: The dimensions are not forced to match,
185 // we thus write in the upper rows on purpose
186 this->gradientL_.noalias() = posePrior.L[meanShapeIdx].topRows(posePrior.d) * wgt;
187 residual.head(posePrior.d).noalias() = this->gradientL_ * bestDiff;
188
189 // re-order the jacobian columns on the fly
190 jacobian.topRows(posePrior.d).setConstant(T(0));
191 for (size_t i = 0; i < invMap_.size(); i++) {
192 if (invMap_[i] != kInvalidIndex) {
193 jacobian.col(i).head(posePrior.d) = this->gradientL_.col(invMap_[i]);
194 }
195 }
196
197 usedRows = gsl::narrow_cast<int>(posePrior.d);
198
199 // return error
200 return error;
201}
202
203template <typename T>
205 const ModelParametersT<T>& params) const {
206 size_t meanShapeIdx = 0;
207 Eigen::VectorX<T> bestDiff;
208 T minDist, bestR;
209 getBestFitMode(params, meanShapeIdx, bestDiff, bestR, minDist);
210 if (!posePrior_ || meanShapeIdx == kInvalidIndex) {
211 return Eigen::VectorX<T>::Zero(this->parameterTransform_.numAllModelParameters());
212 }
213
214 return mapParameters<T>(posePrior_->mu.row(meanShapeIdx).transpose(), invMap_);
215}
216
217template <typename T>
219 const ModelParametersT<T>& params,
220 size_t& bestIdx,
221 Eigen::VectorX<T>& bestDiff,
222 T& bestR,
223 T& minDist) const {
224 minDist = std::numeric_limits<T>::max();
225 bestR = -std::numeric_limits<T>::max();
226 bestIdx = kInvalidIndex;
227
228 if (!posePrior_) {
229 return;
230 }
231
232 const auto& posePrior = *posePrior_;
233
234 const Eigen::VectorX<T> subParams = mapParameters(params, ppMap_);
235
236 for (size_t c = 0; c < posePrior.p; c++) {
237 const Eigen::VectorX<T> diff = subParams - posePrior.mu.row(c).transpose();
238 const T squareDist = T(0.5) * diff.transpose() * posePrior.Cinv[c] * diff;
239 // Rpre is the constant part of R that does not depend on the parameters,
240 // so it's precalculated
241 const T R = posePrior.Rpre(c) - squareDist;
242 if (R > bestR) {
243 bestR = R;
244 bestDiff = diff;
245 minDist = squareDist;
246 bestIdx = c;
247 }
248 }
249}
250
251template class PosePriorErrorFunctionT<float>;
253
254} // namespace momentum
double getJacobian(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
Eigen::VectorX< T > getMeanShape(const ModelParametersT< T > &params) const
void setPosePrior(std::shared_ptr< const MppcaT< T > > pp)
double logProbability(const ModelParametersT< T > &params) const
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
std::shared_ptr< const MppcaT< T > > posePrior_
void getBestFitMode(const ModelParametersT< T > &params, size_t &bestIdx, Eigen::VectorX< T > &bestDiff, T &bestR, T &minDist) const
PosePriorErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, std::shared_ptr< const MppcaT< T > > pp)
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
const ParameterTransform & parameterTransform_
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
Eigen::VectorX< T > mapParameters(const ModelParametersT< T > &in, const std::vector< size_t > &mp)
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
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto size() const
Definition types.h:87
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
Mixture of Probabilistic Principal Component Analyzers (MPPCA).
Definition mppca.h:29
Represents the complete state of a skeleton.