Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_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
22#include "momentum/math/mesh.h"
23
24#include <numeric>
25
26namespace momentum {
27
28template <typename T>
30 : SequenceErrorFunctionT<T>(character.skeleton, character.parameterTransform),
31 character_(character) {
32 MT_CHECK(static_cast<bool>(character_.mesh));
33 MT_CHECK(static_cast<bool>(character_.skinWeights));
34}
35
36template <typename T>
38 constraints_.clear();
39}
40
41template <typename T>
43 int vertexIndex,
44 T weight,
45 const Eigen::Vector3<T>& targetVelocity) {
46 MT_CHECK(vertexIndex >= 0 && ((size_t)vertexIndex) < character_.mesh->vertices.size());
47 constraints_.push_back(VertexVelocityConstraintT<T>{vertexIndex, weight, targetVelocity});
48}
49
50template <typename T>
52 std::span<const ModelParametersT<T>> modelParameters,
53 std::span<const SkeletonStateT<T>> skelStates,
54 std::span<const MeshStateT<T>> meshStates) const {
56 MT_CHECK(modelParameters.size() == 2);
57 MT_CHECK(skelStates.size() == 2);
58 MT_CHECK(meshStates.size() == 2);
59 MT_CHECK_NOTNULL(meshStates[0].posedMesh_);
60 MT_CHECK_NOTNULL(meshStates[1].posedMesh_);
61
62 double error = 0.0;
63
64 // Calculate vertex velocities and compare with target velocities
65 for (const auto& constraint : constraints_) {
66 const Eigen::Vector3<T>& vertex0 = meshStates[0].posedMesh_->vertices[constraint.vertexIndex];
67 const Eigen::Vector3<T>& vertex1 = meshStates[1].posedMesh_->vertices[constraint.vertexIndex];
68
69 // Compute actual velocity (difference between frames)
70 const Eigen::Vector3<T> actualVelocity = vertex1 - vertex0;
71
72 // Compute velocity difference
73 const Eigen::Vector3<T> velocityDiff = actualVelocity - constraint.targetVelocity;
74
75 error += constraint.weight * velocityDiff.squaredNorm() * kVelocityWeight;
76 }
77
78 return error * this->weight_;
79}
80
81template <typename T>
82double VertexSequenceErrorFunctionT<T>::calculateVelocityGradient(
83 std::span<const SkeletonStateT<T>> skelStates,
84 std::span<const MeshStateT<T>> meshStates,
85 const VertexVelocityConstraintT<T>& constraint,
86 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
87 const Eigen::Vector3<T>& vertex0 = meshStates[0].posedMesh_->vertices[constraint.vertexIndex];
88 const Eigen::Vector3<T>& vertex1 = meshStates[1].posedMesh_->vertices[constraint.vertexIndex];
89
90 const Eigen::Vector3<T> actualVelocity = vertex1 - vertex0;
91 const Eigen::Vector3<T> velocityDiff = actualVelocity - constraint.targetVelocity;
92
93 const T wgt = constraint.weight * 2.0f * kVelocityWeight * this->weight_;
94
95 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
96
97 Eigen::Ref<Eigen::VectorX<T>> grad0 = gradient.segment(0, nParam);
98 Eigen::Ref<Eigen::VectorX<T>> grad1 = gradient.segment(nParam, nParam);
99
100 // Gradient with respect to frame 0 (negative contribution to velocity)
101 {
102 SkinningWeightIteratorT<T> skinningIter(
103 character_, *meshStates[0].restMesh_, skelStates[0], constraint.vertexIndex);
104
105 while (!skinningIter.finished()) {
106 size_t jointIndex = 0;
107 T boneWeight;
108 Eigen::Vector3<T> pos;
109 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
110
111 MT_CHECK(jointIndex < this->skeleton_.joints.size());
112
113 const auto& jointState = skelStates[0].jointState[jointIndex];
114 const size_t paramIndex = jointIndex * kParametersPerJoint;
115 const Eigen::Vector3<T> posd = pos - jointState.translation();
116
117 for (size_t d = 0; d < 3; d++) {
118 if (this->activeJointParams_[paramIndex + d]) {
119 // Create a temporary gradient vector for frame 0
121 -boneWeight * velocityDiff.dot(jointState.getTranslationDerivative(d)) * wgt,
122 paramIndex + d,
123 this->parameterTransform_,
124 grad0);
125 }
126 if (this->activeJointParams_[paramIndex + 3 + d]) {
128 -boneWeight * velocityDiff.dot(jointState.getRotationDerivative(d, posd)) * wgt,
129 paramIndex + 3 + d,
130 this->parameterTransform_,
131 grad0);
132 }
133 }
134 if (this->activeJointParams_[paramIndex + 6]) {
136 -boneWeight * velocityDiff.dot(jointState.getScaleDerivative(posd)) * wgt,
137 paramIndex + 6,
138 this->parameterTransform_,
139 grad0);
140 }
141 }
142 }
143
144 // Gradient with respect to frame 1 (positive contribution to velocity)
145 {
146 SkinningWeightIteratorT<T> skinningIter(
147 character_, *meshStates[1].restMesh_, skelStates[1], constraint.vertexIndex);
148
149 while (!skinningIter.finished()) {
150 size_t jointIndex = 0;
151 T boneWeight;
152 Eigen::Vector3<T> pos;
153 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
154
155 MT_CHECK(jointIndex < this->skeleton_.joints.size());
156
157 const auto& jointState = skelStates[1].jointState[jointIndex];
158 const size_t paramIndex = jointIndex * kParametersPerJoint;
159 const Eigen::Vector3<T> posd = pos - jointState.translation();
160
161 for (size_t d = 0; d < 3; d++) {
162 if (this->activeJointParams_[paramIndex + d]) {
163 // Create a temporary gradient vector for frame 1
165 boneWeight * velocityDiff.dot(jointState.getTranslationDerivative(d)) * wgt,
166 paramIndex + d,
167 this->parameterTransform_,
168 grad1);
169 }
170 if (this->activeJointParams_[paramIndex + 3 + d]) {
172 boneWeight * velocityDiff.dot(jointState.getRotationDerivative(d, posd)) * wgt,
173 paramIndex + 3 + d,
174 this->parameterTransform_,
175 grad1);
176 }
177 }
178 if (this->activeJointParams_[paramIndex + 6]) {
180 boneWeight * velocityDiff.dot(jointState.getScaleDerivative(posd)) * wgt,
181 paramIndex + 6,
182 this->parameterTransform_,
183 grad1);
184 }
185 }
186 }
187
188 return constraint.weight * velocityDiff.squaredNorm() * kVelocityWeight;
189}
190
191template <typename T>
193 std::span<const ModelParametersT<T>> modelParameters,
194 std::span<const SkeletonStateT<T>> skelStates,
195 std::span<const MeshStateT<T>> meshStates,
196 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
198 MT_CHECK(modelParameters.size() == 2);
199 MT_CHECK(skelStates.size() == 2);
200 MT_CHECK(meshStates.size() == 2);
201 MT_CHECK_NOTNULL(meshStates[0].posedMesh_);
202 MT_CHECK_NOTNULL(meshStates[1].posedMesh_);
203
204 double error = 0.0;
205
206 // Process constraints sequentially without multithreading
207 for (const auto& constraint : constraints_) {
208 error += calculateVelocityGradient(skelStates, meshStates, constraint, gradient);
209 }
210
211 return this->weight_ * error;
212}
213
214template <typename T>
215double VertexSequenceErrorFunctionT<T>::calculateVelocityJacobian(
216 std::span<const SkeletonStateT<T>> skelStates,
217 std::span<const MeshStateT<T>> meshStates,
218 const VertexVelocityConstraintT<T>& constraint,
219 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
220 Eigen::Ref<Eigen::VectorX<T>> residual,
221 Eigen::Index startRow) const {
222 const Eigen::Vector3<T>& vertex0 = meshStates[0].posedMesh_->vertices[constraint.vertexIndex];
223 const Eigen::Vector3<T>& vertex1 = meshStates[1].posedMesh_->vertices[constraint.vertexIndex];
224
225 const Eigen::Vector3<T> actualVelocity = vertex1 - vertex0;
226 const Eigen::Vector3<T> velocityDiff = actualVelocity - constraint.targetVelocity;
227
228 const T wgt = std::sqrt(constraint.weight * kVelocityWeight * this->weight_);
229
230 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
231
232 // Jacobian with respect to frame 0 (negative contribution to velocity)
233 {
234 SkinningWeightIteratorT<T> skinningIter(
235 character_, *meshStates[0].restMesh_, skelStates[0], constraint.vertexIndex);
236
237 while (!skinningIter.finished()) {
238 size_t jointIndex = 0;
239 T boneWeight;
240 Eigen::Vector3<T> pos;
241 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
242
243 MT_CHECK(jointIndex < this->skeleton_.joints.size());
244
245 const auto& jointState = skelStates[0].jointState[jointIndex];
246 const size_t paramIndex = jointIndex * kParametersPerJoint;
247 const Eigen::Vector3<T> posd = pos - jointState.translation();
248
249 for (size_t d = 0; d < 3; d++) {
250 if (this->activeJointParams_[paramIndex + d]) {
252 -boneWeight * wgt * jointState.getTranslationDerivative(d),
253 paramIndex + d,
254 this->parameterTransform_,
255 jacobian.block(startRow, 0, 3, nParam));
256 }
257 if (this->activeJointParams_[paramIndex + 3 + d]) {
259 -boneWeight * wgt * jointState.getRotationDerivative(d, posd),
260 paramIndex + d + 3,
261 this->parameterTransform_,
262 jacobian.block(startRow, 0, 3, nParam));
263 }
264 }
265 if (this->activeJointParams_[paramIndex + 6]) {
267 -boneWeight * wgt * jointState.getScaleDerivative(posd),
268 paramIndex + 6,
269 this->parameterTransform_,
270 jacobian.block(startRow, 0, 3, nParam));
271 }
272 }
273 }
274
275 // Jacobian with respect to frame 1 (positive contribution to velocity)
276 {
277 SkinningWeightIteratorT<T> skinningIter(
278 character_, *meshStates[1].restMesh_, skelStates[1], constraint.vertexIndex);
279
280 while (!skinningIter.finished()) {
281 size_t jointIndex = 0;
282 T boneWeight;
283 Eigen::Vector3<T> pos;
284 std::tie(jointIndex, boneWeight, pos) = skinningIter.next();
285
286 MT_CHECK(jointIndex < this->skeleton_.joints.size());
287
288 const auto& jointState = skelStates[1].jointState[jointIndex];
289 const size_t paramIndex = jointIndex * kParametersPerJoint;
290 const Eigen::Vector3<T> posd = pos - jointState.translation();
291
292 for (size_t d = 0; d < 3; d++) {
293 if (this->activeJointParams_[paramIndex + d]) {
295 boneWeight * wgt * jointState.getTranslationDerivative(d),
296 paramIndex + d,
297 this->parameterTransform_,
298 jacobian.block(startRow, nParam, 3, nParam));
299 }
300 if (this->activeJointParams_[paramIndex + 3 + d]) {
302 boneWeight * wgt * jointState.getRotationDerivative(d, posd),
303 paramIndex + d + 3,
304 this->parameterTransform_,
305 jacobian.block(startRow, nParam, 3, nParam));
306 }
307 }
308 if (this->activeJointParams_[paramIndex + 6]) {
310 boneWeight * wgt * jointState.getScaleDerivative(posd),
311 paramIndex + 6,
312 this->parameterTransform_,
313 jacobian.block(startRow, nParam, 3, nParam));
314 }
315 }
316 }
317
318 residual.segment(startRow, 3) = velocityDiff * wgt;
319
320 return wgt * wgt * velocityDiff.squaredNorm();
321}
322
323template <typename T>
325 std::span<const ModelParametersT<T>> modelParameters,
326 std::span<const SkeletonStateT<T>> skelStates,
327 std::span<const MeshStateT<T>> meshStates,
328 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
329 Eigen::Ref<Eigen::VectorX<T>> residual,
330 int& usedRows) const {
332 MT_CHECK(modelParameters.size() == 2);
333 MT_CHECK(skelStates.size() == 2);
334 MT_CHECK(meshStates.size() == 2);
335 MT_CHECK_NOTNULL(meshStates[0].posedMesh_);
336 MT_CHECK_NOTNULL(meshStates[1].posedMesh_);
337
338 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
339 MT_CHECK(jacobian.cols() == 2 * nParam);
340 MT_CHECK(jacobian.rows() >= (Eigen::Index)(3 * constraints_.size()));
341 MT_CHECK(residual.rows() >= (Eigen::Index)(3 * constraints_.size()));
342
343 double error = 0.0;
344
345 // Process constraints sequentially without multithreading
346 for (size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
347 error += calculateVelocityJacobian(
348 skelStates, meshStates, constraints_[iCons], jacobian, residual, 3 * iCons);
349 }
350
351 usedRows = gsl::narrow_cast<int>(3 * constraints_.size());
352 return error;
353}
354
355template <typename T>
357 return 3 * constraints_.size();
358}
359
362
363} // namespace momentum
This file provides functions for applying blend shape deformations and skeletal transformations to ch...
#define MT_CHECK_NOTNULL(ptr,...)
Asserts that ptr is not null; on failure, throws std::runtime_error with the optional fmt-style messa...
Definition checks.h:45
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
const ParameterTransform & parameterTransform_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Error function that penalizes differences in vertex velocity between source and target motion.
double getError(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates) const final
void addConstraint(int vertexIndex, T weight, const Eigen::Vector3< T > &targetVelocity)
Add a vertex velocity constraint.
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
void clearConstraints()
Clear all vertex velocity constraints.
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
Functions for linear blend skinning (Skeletal Subspace Deformation).
void jacobian_jointParams_to_modelParams(const Eigen::Ref< const Eigen::VectorX< T > > &jacobian_jointParams, const Eigen::Index iJointParam, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobian)
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
constexpr size_t kParametersPerJoint
Definition types.h:21
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
Represents the complete state of a skeleton.
Constraint structure for vertex velocity.