30 size_t referenceJoint) {
31 return transformAtoB(sourceJoint, referenceJoint, skeleton, state);
54 const ChainContext<T>& ctx,
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;
67 const auto& js = state.jointState[cur];
69 const Vector3<T> posd = worldPos - js.translation();
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);
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);
83 if (ctx.activeJointParams[
pi + 6]) {
84 const T val = wg.dot(js.getScaleDerivative(posd)) * wgt;
87 cur = ctx.skeleton.joints[cur].parent;
93void accumRefRotTransGrad(
94 const ChainContext<T>& ctx,
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;
107 const auto& js = state.jointState[cur];
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);
117 cur = ctx.skeleton.joints[cur].parent;
125void accumRefInvScaleGrad(
126 const ChainContext<T>& ctx,
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;
140 if (ctx.activeJointParams[
pi + 6]) {
143 cur = ctx.skeleton.joints[cur].parent;
151 const ChainContext<T>& ctx,
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;
164 const auto& js = state.jointState[cur];
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);
174 cur = ctx.skeleton.joints[cur].parent;
188 const ChainContext<T>& ctx,
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;
203 const auto& js = state.jointState[cur];
205 const Vector3<T> posd = worldPos - js.translation();
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);
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);
218 if (ctx.activeJointParams[
pi + 6]) {
219 const Vector3<T> jc = sign * f * refRotT * js.getScaleDerivative(posd);
222 cur = ctx.skeleton.joints[cur].parent;
228void accumRefRotTransJac(
229 const ChainContext<T>& ctx,
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;
244 const auto& js = state.jointState[cur];
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);
254 cur = ctx.skeleton.joints[cur].parent;
261void accumRefInvScaleJac(
262 const ChainContext<T>& ctx,
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);
274 size_t cur = leafJoint;
277 if (ctx.activeJointParams[
pi + 6]) {
280 cur = ctx.skeleton.joints[cur].parent;
288 const ChainContext<T>& ctx,
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;
303 const auto& js = state.jointState[cur];
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);
313 cur = ctx.skeleton.joints[cur].parent;
334 constraint.
sourceJoint < this->skeleton_.joints.size(),
335 "Invalid sourceJoint index: {}",
339 "Invalid referenceJoint index: {}",
341 constraints_.push_back(constraint);
347 size_t referenceJoint,
349 const std::string& name) {
360 constraints_.clear();
370 if (constraints_.empty()) {
378 for (
const auto& constr : constraints_) {
380 this->
skeleton_, skelStates[0], constr.sourceJoint, constr.referenceJoint);
382 this->
skeleton_, skelStates[1], constr.sourceJoint, constr.referenceJoint);
386 const Matrix3<T> rotDiff = nextRelRot - prevRelRot;
388 const T wBase = this->
weight_ * constr.weight;
389 error += rotDiff.squaredNorm() * wBase * rotWgt_;
390 error += posDiff.squaredNorm() * wBase * posWgt_;
401 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
404 if (constraints_.empty()) {
409 const auto& prevState = skelStates[0];
410 const auto& nextState = skelStates[1];
413 MT_CHECK(gradient.size() == 2 * nParam);
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];
425 relativeTransform(this->
skeleton_, prevState, constr.sourceJoint, constr.referenceJoint);
427 relativeTransform(this->
skeleton_, nextState, constr.sourceJoint, constr.referenceJoint);
431 const Matrix3<T> rotDiff = nextRelRot - prevRelRot;
433 const T wBase = this->
weight_ * constr.weight;
434 error += rotDiff.squaredNorm() * wBase * rotWgt_;
435 error += posDiff.squaredNorm() * wBase * posWgt_;
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();
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;
452 const Vector3<T> nextWg = nextRefRot * errCol;
453 const Vector3<T> nextWorldSrcCol = nextRefRot * nextRelRot.col(col);
467 constr.referenceJoint,
477 const Vector3<T> prevWg = prevRefRot * errCol;
478 const Vector3<T> prevWorldSrcCol = prevRefRot * prevRelRot.col(col);
492 constr.referenceJoint,
502 const Vector3<T> nextWg = nextRefInvScale * (nextRefRot * errCol);
508 nextSrc.translation(),
516 constr.referenceJoint,
519 nextRef.translation(),
525 accumRefRotTransGrad(
527 constr.referenceJoint,
536 accumRefInvScaleGrad(
538 constr.referenceJoint,
547 const Vector3<T> prevWg = prevRefInvScale * (prevRefRot * errCol);
553 prevSrc.translation(),
561 constr.referenceJoint,
564 prevRef.translation(),
570 accumRefRotTransGrad(
572 constr.referenceJoint,
581 accumRefInvScaleGrad(
582 ctx, constr.referenceJoint, lca, prev.
translation, errCol, -wgt, gradient, 0, nParam);
592 return constraints_.size() * 12;
600 Eigen::Ref<Eigen::MatrixX<T>> jacobian_full,
601 Eigen::Ref<Eigen::VectorX<T>> residual_full,
602 int& usedRows)
const {
605 if (constraints_.empty()) {
611 const auto& prevState = skelStates[0];
612 const auto& nextState = skelStates[1];
615 MT_CHECK(jacobian_full.cols() == 2 * nParam);
620 Eigen::Index offset = 0;
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];
628 relativeTransform(this->
skeleton_, prevState, constr.sourceJoint, constr.referenceJoint);
630 relativeTransform(this->
skeleton_, nextState, constr.sourceJoint, constr.referenceJoint);
634 const Matrix3<T> rotDiff = nextRelRot - prevRelRot;
636 const T wBase = this->
weight_ * constr.weight;
637 error += rotDiff.squaredNorm() * wBase * rotWgt_;
638 error += posDiff.squaredNorm() * wBase * posWgt_;
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;
644 const T posFac = std::sqrt(wBase * posWgt_);
645 residual_full.template segment<3>(offset + 9) = posDiff * posFac;
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;
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);
666 const Vector3<T> nextWorldSrcCol = nextRefRot * nextRelRot.col(col);
682 constr.referenceJoint,
694 const Vector3<T> prevWorldSrcCol = prevRefRot * prevRelRot.col(col);
710 constr.referenceJoint,
727 nextSrc.translation(),
737 constr.referenceJoint,
740 nextRef.translation(),
750 constr.referenceJoint,
763 constr.referenceJoint,
778 prevSrc.translation(),
788 constr.referenceJoint,
791 prevRef.translation(),
801 constr.referenceJoint,
814 constr.referenceJoint,
829 usedRows = gsl::narrow_cast<int>(offset);
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
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
size_t getJacobianSize() const final
void clearConstraints()
Clear all constraints.
JointToJointSequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
void addConstraint(const JointToJointSequenceConstraintT< T > &constraint)
Add a constraint using the struct.
const ParameterTransform & parameterTransform_
VectorX< bool > activeJointParams_
const Skeleton & skeleton_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
SkeletonT< float > Skeleton
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
void jacobian_jointParams_to_modelParams(const Eigen::Ref< const Eigen::VectorX< T > > &jacobian_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::MatrixX< T > > jacobian)
constexpr T ln2() noexcept
Returns the natural logarithm of 2 (log_e 2).
ParameterTransformT< float > ParameterTransform
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
constexpr size_t kParametersPerJoint
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
constexpr T pi() noexcept
Returns the mathematical constant pi.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
A character model with skeletal structure, mesh, and optional components.
Constraint data for the joint-to-joint sequence error function.
size_t referenceJoint
Index of the reference joint (the frame of reference).
T weight
Weight for this constraint.
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.
A vector of size numModelParams holding the values of all model parameters for a character.
Represents the complete state of a skeleton.