Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
joint_to_joint_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
17
18namespace momentum {
19
20namespace {
21
22// Express the source joint in the reference joint's frame (T_ref^{-1} * T_src).
23// transformAtoB combines transforms along the common-ancestor path, which is
24// more numerically stable than computing the inverse-and-multiply directly.
25template <typename T>
26TransformT<T> relativeTransform(
27 const Skeleton& skeleton,
28 const SkeletonStateT<T>& state,
29 size_t sourceJoint,
30 size_t referenceJoint) {
31 return transformAtoB(sourceJoint, referenceJoint, skeleton, state);
32}
33
34// Read-only references shared by every chain-walk helper below. Bundling them
35// keeps the helper signatures (and the call sites in getGradient/getJacobian)
36// short, which also keeps the per-function cyclomatic complexity in check.
37template <typename T>
38struct ChainContext {
39 const Skeleton& skeleton;
40 const VectorX<bool>& activeJointParams;
41 const ParameterTransform& parameterTransform;
42};
43
44// ---------------------------------------------------------------------------
45// Gradient chain-walk helpers. Each walks the joint parent chain from
46// leafJoint up to (but not including) stopJoint, accumulating into the
47// model-parameter gradient segment [gradOffset, gradOffset + nParam).
48// ---------------------------------------------------------------------------
49
50// Accumulate gradient for the translation column (col 3 of T_rel).
51// Handles translation, rotation, and scale derivatives.
52template <typename T>
53void accumTransGrad(
54 const ChainContext<T>& ctx,
55 size_t leafJoint,
56 size_t stopJoint,
57 const SkeletonStateT<T>& state,
58 const Vector3<T>& worldPos,
59 const Vector3<T>& wg,
60 T wgt,
61 Eigen::Ref<Eigen::VectorX<T>> gradient,
62 Eigen::Index gradOffset,
63 Eigen::Index nParam) {
64 Eigen::Ref<Eigen::VectorX<T>> gradientSegment = gradient.segment(gradOffset, nParam);
65 size_t cur = leafJoint;
66 while (cur != kInvalidIndex && cur != stopJoint) {
67 const auto& js = state.jointState[cur];
68 const size_t pi = cur * kParametersPerJoint;
69 const Vector3<T> posd = worldPos - js.translation();
70
71 for (size_t d = 0; d < 3; d++) {
72 if (ctx.activeJointParams[pi + d]) {
73 const T val = wg.dot(js.getTranslationDerivative(d)) * wgt;
75 val, pi + d, ctx.parameterTransform, gradientSegment);
76 }
77 if (ctx.activeJointParams[pi + 3 + d]) {
78 const T val = wg.dot(js.getRotationDerivative(d, posd)) * wgt;
80 val, pi + 3 + d, ctx.parameterTransform, gradientSegment);
81 }
82 }
83 if (ctx.activeJointParams[pi + 6]) {
84 const T val = wg.dot(js.getScaleDerivative(posd)) * wgt;
85 gradient_jointParams_to_modelParams<T>(val, pi + 6, ctx.parameterTransform, gradientSegment);
86 }
87 cur = ctx.skeleton.joints[cur].parent;
88 }
89}
90
91// Accumulate gradient for the R_ref^T rotation effect on translation.
92template <typename T>
93void accumRefRotTransGrad(
94 const ChainContext<T>& ctx,
95 size_t leafJoint,
96 size_t stopJoint,
97 const SkeletonStateT<T>& state,
98 const Vector3<T>& d_vec,
99 const Vector3<T>& wg,
100 T wgt,
101 Eigen::Ref<Eigen::VectorX<T>> gradient,
102 Eigen::Index gradOffset,
103 Eigen::Index nParam) {
104 Eigen::Ref<Eigen::VectorX<T>> gradientSegment = gradient.segment(gradOffset, nParam);
105 size_t cur = leafJoint;
106 while (cur != kInvalidIndex && cur != stopJoint) {
107 const auto& js = state.jointState[cur];
108 const size_t pi = cur * kParametersPerJoint;
109
110 for (size_t d = 0; d < 3; d++) {
111 if (ctx.activeJointParams[pi + 3 + d]) {
112 const T val = -wg.dot(js.getRotationDerivative(d, d_vec)) * wgt;
114 val, pi + 3 + d, ctx.parameterTransform, gradientSegment);
115 }
116 }
117 cur = ctx.skeleton.joints[cur].parent;
118 }
119}
120
121// Accumulate gradient for the reference-joint inverse-scale effect on
122// translation. The per-parameter value is constant along the chain, so it is
123// computed once outside the walk.
124template <typename T>
125void accumRefInvScaleGrad(
126 const ChainContext<T>& ctx,
127 size_t leafJoint,
128 size_t stopJoint,
129 const Vector3<T>& relTranslation,
130 const Vector3<T>& errCol,
131 T wgt,
132 Eigen::Ref<Eigen::VectorX<T>> gradient,
133 Eigen::Index gradOffset,
134 Eigen::Index nParam) {
135 Eigen::Ref<Eigen::VectorX<T>> gradientSegment = gradient.segment(gradOffset, nParam);
136 const T val = -errCol.dot(relTranslation) * ln2<T>() * wgt;
137 size_t cur = leafJoint;
138 while (cur != kInvalidIndex && cur != stopJoint) {
139 const size_t pi = cur * kParametersPerJoint;
140 if (ctx.activeJointParams[pi + 6]) {
141 gradient_jointParams_to_modelParams<T>(val, pi + 6, ctx.parameterTransform, gradientSegment);
142 }
143 cur = ctx.skeleton.joints[cur].parent;
144 }
145}
146
147// Accumulate gradient for the rotation columns (cols 0-2 of T_rel).
148// Rotation columns are scale-independent, so only rotation derivatives apply.
149template <typename T>
150void accumRotColGrad(
151 const ChainContext<T>& ctx,
152 size_t leafJoint,
153 size_t stopJoint,
154 const SkeletonStateT<T>& state,
155 const Vector3<T>& srcCol,
156 const Vector3<T>& wg,
157 T wgt,
158 Eigen::Ref<Eigen::VectorX<T>> gradient,
159 Eigen::Index gradOffset,
160 Eigen::Index nParam) {
161 Eigen::Ref<Eigen::VectorX<T>> gradientSegment = gradient.segment(gradOffset, nParam);
162 size_t cur = leafJoint;
163 while (cur != kInvalidIndex && cur != stopJoint) {
164 const auto& js = state.jointState[cur];
165 const size_t pi = cur * kParametersPerJoint;
166
167 for (size_t d = 0; d < 3; d++) {
168 if (ctx.activeJointParams[pi + 3 + d]) {
169 const T val = wg.dot(js.getRotationDerivative(d, srcCol)) * wgt;
171 val, pi + 3 + d, ctx.parameterTransform, gradientSegment);
172 }
173 }
174 cur = ctx.skeleton.joints[cur].parent;
175 }
176}
177
178// ---------------------------------------------------------------------------
179// Jacobian chain-walk helpers. These mirror the gradient helpers but write a
180// 3xN Jacobian block (3 residual rows of one transform column, at rows
181// [rowBase, rowBase + 3) and columns [colOffset, colOffset + nParam)) instead
182// of a scalar gradient contribution.
183// ---------------------------------------------------------------------------
184
185// Accumulate the translation-column Jacobian from a joint chain.
186template <typename T>
187void accumTransJac(
188 const ChainContext<T>& ctx,
189 size_t leafJoint,
190 size_t stopJoint,
191 const SkeletonStateT<T>& state,
192 const Vector3<T>& worldPos,
193 const Matrix3<T>& refRotT,
194 T sign,
195 T f,
196 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
197 Eigen::Index colOffset,
198 Eigen::Index rowBase,
199 Eigen::Index nParam) {
200 auto jacobianBlock = jacobian.block(rowBase, colOffset, 3, nParam);
201 size_t cur = leafJoint;
202 while (cur != kInvalidIndex && cur != stopJoint) {
203 const auto& js = state.jointState[cur];
204 const size_t pi = cur * kParametersPerJoint;
205 const Vector3<T> posd = worldPos - js.translation();
206
207 for (size_t d = 0; d < 3; d++) {
208 if (ctx.activeJointParams[pi + d]) {
209 const Vector3<T> jc = sign * f * refRotT * js.getTranslationDerivative(d);
210 jacobian_jointParams_to_modelParams<T>(jc, pi + d, ctx.parameterTransform, jacobianBlock);
211 }
212 if (ctx.activeJointParams[pi + 3 + d]) {
213 const Vector3<T> jc = sign * f * refRotT * js.getRotationDerivative(d, posd);
215 jc, pi + 3 + d, ctx.parameterTransform, jacobianBlock);
216 }
217 }
218 if (ctx.activeJointParams[pi + 6]) {
219 const Vector3<T> jc = sign * f * refRotT * js.getScaleDerivative(posd);
220 jacobian_jointParams_to_modelParams<T>(jc, pi + 6, ctx.parameterTransform, jacobianBlock);
221 }
222 cur = ctx.skeleton.joints[cur].parent;
223 }
224}
225
226// Accumulate the R_ref^T rotation effect on the translation column.
227template <typename T>
228void accumRefRotTransJac(
229 const ChainContext<T>& ctx,
230 size_t leafJoint,
231 size_t stopJoint,
232 const SkeletonStateT<T>& state,
233 const Vector3<T>& d_vec,
234 const Matrix3<T>& refRotT,
235 T sign,
236 T f,
237 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
238 Eigen::Index colOffset,
239 Eigen::Index rowBase,
240 Eigen::Index nParam) {
241 auto jacobianBlock = jacobian.block(rowBase, colOffset, 3, nParam);
242 size_t cur = leafJoint;
243 while (cur != kInvalidIndex && cur != stopJoint) {
244 const auto& js = state.jointState[cur];
245 const size_t pi = cur * kParametersPerJoint;
246
247 for (size_t d = 0; d < 3; d++) {
248 if (ctx.activeJointParams[pi + 3 + d]) {
249 const Vector3<T> jc = -sign * f * refRotT * js.getRotationDerivative(d, d_vec);
251 jc, pi + 3 + d, ctx.parameterTransform, jacobianBlock);
252 }
253 }
254 cur = ctx.skeleton.joints[cur].parent;
255 }
256}
257
258// Accumulate the reference-joint inverse-scale effect on the translation
259// column. The per-parameter contribution is constant along the chain.
260template <typename T>
261void accumRefInvScaleJac(
262 const ChainContext<T>& ctx,
263 size_t leafJoint,
264 size_t stopJoint,
265 const Vector3<T>& relTranslation,
266 T sign,
267 T f,
268 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
269 Eigen::Index colOffset,
270 Eigen::Index rowBase,
271 Eigen::Index nParam) {
272 auto jacobianBlock = jacobian.block(rowBase, colOffset, 3, nParam);
273 const Vector3<T> jc = -sign * f * ln2<T>() * relTranslation;
274 size_t cur = leafJoint;
275 while (cur != kInvalidIndex && cur != stopJoint) {
276 const size_t pi = cur * kParametersPerJoint;
277 if (ctx.activeJointParams[pi + 6]) {
278 jacobian_jointParams_to_modelParams<T>(jc, pi + 6, ctx.parameterTransform, jacobianBlock);
279 }
280 cur = ctx.skeleton.joints[cur].parent;
281 }
282}
283
284// Accumulate the rotation-column Jacobian (cols 0-2 of T_rel: R_ref^T * R_src[:, c]).
285// Rotation columns are scale-independent, so only rotation derivatives apply.
286template <typename T>
287void accumRotColJac(
288 const ChainContext<T>& ctx,
289 size_t leafJoint,
290 size_t stopJoint,
291 const SkeletonStateT<T>& state,
292 const Vector3<T>& srcCol,
293 const Matrix3<T>& refRotT,
294 T sign,
295 T f,
296 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
297 Eigen::Index colOffset,
298 Eigen::Index rowBase,
299 Eigen::Index nParam) {
300 auto jacobianBlock = jacobian.block(rowBase, colOffset, 3, nParam);
301 size_t cur = leafJoint;
302 while (cur != kInvalidIndex && cur != stopJoint) {
303 const auto& js = state.jointState[cur];
304 const size_t pi = cur * kParametersPerJoint;
305
306 for (size_t d = 0; d < 3; d++) {
307 if (ctx.activeJointParams[pi + 3 + d]) {
308 const Vector3<T> jc = sign * f * refRotT * js.getRotationDerivative(d, srcCol);
310 jc, pi + 3 + d, ctx.parameterTransform, jacobianBlock);
311 }
312 }
313 cur = ctx.skeleton.joints[cur].parent;
314 }
315}
316
317} // namespace
318
319template <typename T>
324
325template <typename T>
327 const Character& character)
328 : JointToJointSequenceErrorFunctionT(character.skeleton, character.parameterTransform) {}
329
330template <typename T>
332 const JointToJointSequenceConstraintT<T>& constraint) {
333 MT_CHECK(
334 constraint.sourceJoint < this->skeleton_.joints.size(),
335 "Invalid sourceJoint index: {}",
336 constraint.sourceJoint);
337 MT_CHECK(
338 constraint.referenceJoint < this->skeleton_.joints.size(),
339 "Invalid referenceJoint index: {}",
340 constraint.referenceJoint);
341 constraints_.push_back(constraint);
342}
343
344template <typename T>
346 size_t sourceJoint,
347 size_t referenceJoint,
348 T weight,
349 const std::string& name) {
351 c.sourceJoint = sourceJoint;
352 c.referenceJoint = referenceJoint;
353 c.weight = weight;
354 c.name = name;
355 addConstraint(c);
356}
357
358template <typename T>
360 constraints_.clear();
361}
362
363template <typename T>
365 std::span<const ModelParametersT<T>> /* modelParameters */,
366 std::span<const SkeletonStateT<T>> skelStates,
367 std::span<const MeshStateT<T>> /* meshStates */) const {
369
370 if (constraints_.empty()) {
371 return 0.0;
372 }
373
374 MT_CHECK(skelStates.size() == 2);
375
376 double error = 0.0;
377
378 for (const auto& constr : constraints_) {
379 const TransformT<T> prev = relativeTransform(
380 this->skeleton_, skelStates[0], constr.sourceJoint, constr.referenceJoint);
381 const TransformT<T> next = relativeTransform(
382 this->skeleton_, skelStates[1], constr.sourceJoint, constr.referenceJoint);
383
384 const Matrix3<T> prevRelRot = prev.toRotationMatrix();
385 const Matrix3<T> nextRelRot = next.toRotationMatrix();
386 const Matrix3<T> rotDiff = nextRelRot - prevRelRot;
387 const Vector3<T> posDiff = next.translation - prev.translation;
388 const T wBase = this->weight_ * constr.weight;
389 error += rotDiff.squaredNorm() * wBase * rotWgt_;
390 error += posDiff.squaredNorm() * wBase * posWgt_;
391 }
392
393 return error;
394}
395
396template <typename T>
398 std::span<const ModelParametersT<T>> /* modelParameters */,
399 std::span<const SkeletonStateT<T>> skelStates,
400 std::span<const MeshStateT<T>> /* meshStates */,
401 Eigen::Ref<Eigen::VectorX<T>> gradient) const {
403
404 if (constraints_.empty()) {
405 return 0.0;
406 }
407
408 MT_CHECK(skelStates.size() == 2);
409 const auto& prevState = skelStates[0];
410 const auto& nextState = skelStates[1];
411
412 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
413 MT_CHECK(gradient.size() == 2 * nParam);
414
415 const ChainContext<T> ctx{this->skeleton_, this->activeJointParams_, this->parameterTransform_};
416
417 double error = 0.0;
418
419 for (const auto& constr : constraints_) {
420 const auto& prevSrc = prevState.jointState[constr.sourceJoint];
421 const auto& prevRef = prevState.jointState[constr.referenceJoint];
422 const auto& nextSrc = nextState.jointState[constr.sourceJoint];
423 const auto& nextRef = nextState.jointState[constr.referenceJoint];
424 const TransformT<T> prev =
425 relativeTransform(this->skeleton_, prevState, constr.sourceJoint, constr.referenceJoint);
426 const TransformT<T> next =
427 relativeTransform(this->skeleton_, nextState, constr.sourceJoint, constr.referenceJoint);
428
429 const Matrix3<T> prevRelRot = prev.toRotationMatrix();
430 const Matrix3<T> nextRelRot = next.toRotationMatrix();
431 const Matrix3<T> rotDiff = nextRelRot - prevRelRot;
432 const Vector3<T> posDiff = next.translation - prev.translation;
433 const T wBase = this->weight_ * constr.weight;
434 error += rotDiff.squaredNorm() * wBase * rotWgt_;
435 error += posDiff.squaredNorm() * wBase * posWgt_;
436
437 const size_t lca = this->skeleton_.commonAncestor(constr.sourceJoint, constr.referenceJoint);
438 const Matrix3<T> prevRefRot = prevRef.transform.toRotationMatrix();
439 const Matrix3<T> nextRefRot = nextRef.transform.toRotationMatrix();
440 const T prevRefInvScale = T(1) / prevRef.transform.scale;
441 const T nextRefInvScale = T(1) / nextRef.transform.scale;
442 const Vector3<T> prevWorldOffset = prevSrc.translation() - prevRef.translation();
443 const Vector3<T> nextWorldOffset = nextSrc.translation() - nextRef.translation();
444
445 // First three residuals are rotation matrix columns; the fourth is relative translation.
446 for (int col = 0; col < 4; col++) {
447 const T colWgt = (col < 3) ? rotWgt_ : posWgt_;
448 const T wgt = T(2) * wBase * colWgt;
449 const Vector3<T> errCol = (col < 3) ? rotDiff.col(col) : posDiff;
450
451 if (col < 3) {
452 const Vector3<T> nextWg = nextRefRot * errCol;
453 const Vector3<T> nextWorldSrcCol = nextRefRot * nextRelRot.col(col);
454 accumRotColGrad(
455 ctx,
456 constr.sourceJoint,
457 lca,
458 nextState,
459 nextWorldSrcCol,
460 nextWg,
461 wgt,
462 gradient,
463 nParam,
464 nParam);
465 accumRotColGrad(
466 ctx,
467 constr.referenceJoint,
468 lca,
469 nextState,
470 nextWorldSrcCol,
471 nextWg,
472 -wgt,
473 gradient,
474 nParam,
475 nParam);
476
477 const Vector3<T> prevWg = prevRefRot * errCol;
478 const Vector3<T> prevWorldSrcCol = prevRefRot * prevRelRot.col(col);
479 accumRotColGrad(
480 ctx,
481 constr.sourceJoint,
482 lca,
483 prevState,
484 prevWorldSrcCol,
485 prevWg,
486 -wgt,
487 gradient,
488 0,
489 nParam);
490 accumRotColGrad(
491 ctx,
492 constr.referenceJoint,
493 lca,
494 prevState,
495 prevWorldSrcCol,
496 prevWg,
497 wgt,
498 gradient,
499 0,
500 nParam);
501 } else {
502 const Vector3<T> nextWg = nextRefInvScale * (nextRefRot * errCol);
503 accumTransGrad(
504 ctx,
505 constr.sourceJoint,
506 lca,
507 nextState,
508 nextSrc.translation(),
509 nextWg,
510 wgt,
511 gradient,
512 nParam,
513 nParam);
514 accumTransGrad(
515 ctx,
516 constr.referenceJoint,
517 lca,
518 nextState,
519 nextRef.translation(),
520 nextWg,
521 -wgt,
522 gradient,
523 nParam,
524 nParam);
525 accumRefRotTransGrad(
526 ctx,
527 constr.referenceJoint,
528 lca,
529 nextState,
530 nextWorldOffset,
531 nextWg,
532 wgt,
533 gradient,
534 nParam,
535 nParam);
536 accumRefInvScaleGrad(
537 ctx,
538 constr.referenceJoint,
539 lca,
540 next.translation,
541 errCol,
542 wgt,
543 gradient,
544 nParam,
545 nParam);
546
547 const Vector3<T> prevWg = prevRefInvScale * (prevRefRot * errCol);
548 accumTransGrad(
549 ctx,
550 constr.sourceJoint,
551 lca,
552 prevState,
553 prevSrc.translation(),
554 prevWg,
555 -wgt,
556 gradient,
557 0,
558 nParam);
559 accumTransGrad(
560 ctx,
561 constr.referenceJoint,
562 lca,
563 prevState,
564 prevRef.translation(),
565 prevWg,
566 wgt,
567 gradient,
568 0,
569 nParam);
570 accumRefRotTransGrad(
571 ctx,
572 constr.referenceJoint,
573 lca,
574 prevState,
575 prevWorldOffset,
576 prevWg,
577 -wgt,
578 gradient,
579 0,
580 nParam);
581 accumRefInvScaleGrad(
582 ctx, constr.referenceJoint, lca, prev.translation, errCol, -wgt, gradient, 0, nParam);
583 }
584 }
585 }
586
587 return error;
588}
589
590template <typename T>
592 return constraints_.size() * 12;
593}
594
595template <typename T>
597 std::span<const ModelParametersT<T>> /* modelParameters */,
598 std::span<const SkeletonStateT<T>> skelStates,
599 std::span<const MeshStateT<T>> /* meshStates */,
600 Eigen::Ref<Eigen::MatrixX<T>> jacobian_full,
601 Eigen::Ref<Eigen::VectorX<T>> residual_full,
602 int& usedRows) const {
604
605 if (constraints_.empty()) {
606 usedRows = 0;
607 return 0.0;
608 }
609
610 MT_CHECK(skelStates.size() == 2);
611 const auto& prevState = skelStates[0];
612 const auto& nextState = skelStates[1];
613
614 const Eigen::Index nParam = this->parameterTransform_.numAllModelParameters();
615 MT_CHECK(jacobian_full.cols() == 2 * nParam);
616
617 const ChainContext<T> ctx{this->skeleton_, this->activeJointParams_, this->parameterTransform_};
618
619 double error = 0.0;
620 Eigen::Index offset = 0;
621
622 for (const auto& constr : constraints_) {
623 const auto& prevSrc = prevState.jointState[constr.sourceJoint];
624 const auto& prevRef = prevState.jointState[constr.referenceJoint];
625 const auto& nextSrc = nextState.jointState[constr.sourceJoint];
626 const auto& nextRef = nextState.jointState[constr.referenceJoint];
627 const TransformT<T> prev =
628 relativeTransform(this->skeleton_, prevState, constr.sourceJoint, constr.referenceJoint);
629 const TransformT<T> next =
630 relativeTransform(this->skeleton_, nextState, constr.sourceJoint, constr.referenceJoint);
631
632 const Matrix3<T> prevRelRot = prev.toRotationMatrix();
633 const Matrix3<T> nextRelRot = next.toRotationMatrix();
634 const Matrix3<T> rotDiff = nextRelRot - prevRelRot;
635 const Vector3<T> posDiff = next.translation - prev.translation;
636 const T wBase = this->weight_ * constr.weight;
637 error += rotDiff.squaredNorm() * wBase * rotWgt_;
638 error += posDiff.squaredNorm() * wBase * posWgt_;
639
640 const T rotFac = std::sqrt(wBase * rotWgt_);
641 for (int col = 0; col < 3; col++) {
642 residual_full.template segment<3>(offset + col * 3) = rotDiff.col(col) * rotFac;
643 }
644 const T posFac = std::sqrt(wBase * posWgt_);
645 residual_full.template segment<3>(offset + 9) = posDiff * posFac;
646
647 const size_t lca = this->skeleton_.commonAncestor(constr.sourceJoint, constr.referenceJoint);
648 const Matrix3<T> prevRefRot = prevRef.transform.toRotationMatrix();
649 const Matrix3<T> nextRefRot = nextRef.transform.toRotationMatrix();
650 const Matrix3<T> prevRefRotT = prevRefRot.transpose();
651 const Matrix3<T> nextRefRotT = nextRefRot.transpose();
652 const T prevRefInvScale = T(1) / prevRef.transform.scale;
653 const T nextRefInvScale = T(1) / nextRef.transform.scale;
654 const Vector3<T> prevWorldOffset = prevSrc.translation() - prevRef.translation();
655 const Vector3<T> nextWorldOffset = nextSrc.translation() - nextRef.translation();
656 const Matrix3<T> prevInvScaleRefRotT = prevRefInvScale * prevRefRotT;
657 const Matrix3<T> nextInvScaleRefRotT = nextRefInvScale * nextRefRotT;
658
659 // First three row groups are rotation matrix columns; the fourth is relative translation.
660 for (int col = 0; col < 4; col++) {
661 const Eigen::Index rowBase = offset + col * 3;
662 const T colWgt = (col < 3) ? rotWgt_ : posWgt_;
663 const T fac = std::sqrt(wBase * colWgt);
664
665 if (col < 3) {
666 const Vector3<T> nextWorldSrcCol = nextRefRot * nextRelRot.col(col);
667 accumRotColJac(
668 ctx,
669 constr.sourceJoint,
670 lca,
671 nextState,
672 nextWorldSrcCol,
673 nextRefRotT,
674 T(1),
675 fac,
676 jacobian_full,
677 nParam,
678 rowBase,
679 nParam);
680 accumRotColJac(
681 ctx,
682 constr.referenceJoint,
683 lca,
684 nextState,
685 nextWorldSrcCol,
686 nextRefRotT,
687 T(-1),
688 fac,
689 jacobian_full,
690 nParam,
691 rowBase,
692 nParam);
693
694 const Vector3<T> prevWorldSrcCol = prevRefRot * prevRelRot.col(col);
695 accumRotColJac(
696 ctx,
697 constr.sourceJoint,
698 lca,
699 prevState,
700 prevWorldSrcCol,
701 prevRefRotT,
702 T(-1),
703 fac,
704 jacobian_full,
705 0,
706 rowBase,
707 nParam);
708 accumRotColJac(
709 ctx,
710 constr.referenceJoint,
711 lca,
712 prevState,
713 prevWorldSrcCol,
714 prevRefRotT,
715 T(1),
716 fac,
717 jacobian_full,
718 0,
719 rowBase,
720 nParam);
721 } else {
722 accumTransJac(
723 ctx,
724 constr.sourceJoint,
725 lca,
726 nextState,
727 nextSrc.translation(),
728 nextInvScaleRefRotT,
729 T(1),
730 fac,
731 jacobian_full,
732 nParam,
733 rowBase,
734 nParam);
735 accumTransJac(
736 ctx,
737 constr.referenceJoint,
738 lca,
739 nextState,
740 nextRef.translation(),
741 nextInvScaleRefRotT,
742 T(-1),
743 fac,
744 jacobian_full,
745 nParam,
746 rowBase,
747 nParam);
748 accumRefRotTransJac(
749 ctx,
750 constr.referenceJoint,
751 lca,
752 nextState,
753 nextWorldOffset,
754 nextInvScaleRefRotT,
755 T(1),
756 fac,
757 jacobian_full,
758 nParam,
759 rowBase,
760 nParam);
761 accumRefInvScaleJac(
762 ctx,
763 constr.referenceJoint,
764 lca,
765 next.translation,
766 T(1),
767 fac,
768 jacobian_full,
769 nParam,
770 rowBase,
771 nParam);
772
773 accumTransJac(
774 ctx,
775 constr.sourceJoint,
776 lca,
777 prevState,
778 prevSrc.translation(),
779 prevInvScaleRefRotT,
780 T(-1),
781 fac,
782 jacobian_full,
783 0,
784 rowBase,
785 nParam);
786 accumTransJac(
787 ctx,
788 constr.referenceJoint,
789 lca,
790 prevState,
791 prevRef.translation(),
792 prevInvScaleRefRotT,
793 T(1),
794 fac,
795 jacobian_full,
796 0,
797 rowBase,
798 nParam);
799 accumRefRotTransJac(
800 ctx,
801 constr.referenceJoint,
802 lca,
803 prevState,
804 prevWorldOffset,
805 prevInvScaleRefRotT,
806 T(-1),
807 fac,
808 jacobian_full,
809 0,
810 rowBase,
811 nParam);
812 accumRefInvScaleJac(
813 ctx,
814 constr.referenceJoint,
815 lca,
816 prev.translation,
817 T(-1),
818 fac,
819 jacobian_full,
820 0,
821 rowBase,
822 nParam);
823 }
824 }
825
826 offset += 12;
827 }
828
829 usedRows = gsl::narrow_cast<int>(offset);
830 return error;
831}
832
835
836} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
Error function that penalizes frame-to-frame changes in the relative transform between two joints.
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
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
JointToJointSequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
void addConstraint(const JointToJointSequenceConstraintT< T > &constraint)
Add a constraint using the struct.
const ParameterTransform & parameterTransform_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
SkeletonT< float > Skeleton
Definition fwd.h:263
TransformT< T > transformAtoB(size_t jointA, size_t jointB, const Skeleton &referenceSkeleton, const SkeletonStateT< T > &skelState)
Computes the relative transform between two joints in a skeleton.
Eigen::Matrix< T, 3, 3 > Matrix3
Definition types.h:96
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)
constexpr T ln2() noexcept
Returns the natural logarithm of 2 (log_e 2).
Definition constants.h:40
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform &parameterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
constexpr size_t kParametersPerJoint
Definition types.h:21
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
Vector< T, 3 > Vector3
Definition types.h:236
constexpr T pi() noexcept
Returns the mathematical constant pi.
Definition constants.h:50
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
Constraint data for the joint-to-joint sequence error function.
size_t referenceJoint
Index of the reference joint (the frame of reference).
size_t sourceJoint
Index of the source joint (the joint whose relative pose we track).
std::string name
Name of the constraint (for debugging).
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.
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36
Vector3< T > translation
Definition transform.h:40
Matrix3< T > toRotationMatrix() const
Returns the rotation as a 3x3 orthogonal matrix (scale not included).
Definition transform.h:160