34Vector3<T> quaternionLogMapRelativeDerivativeQ1(
38 const Eigen::Matrix<T, 3, 4>& dLogDq) {
42 const Vector3<T> q1_vec(q1.x(), q1.y(), q1.z());
43 const T dq1_w = -vHalf.dot(q1_vec);
44 const Vector3<T> dq1_vec = vHalf * q1.w() + vHalf.cross(q1_vec);
47 const Vector3<T> q2_vec(q2.x(), q2.y(), q2.z());
48 const T dqRel_w = q2.w() * dq1_w - (-q2_vec).
dot(dq1_vec);
49 const Vector3<T> dqRel_vec = q2.w() * dq1_vec + dq1_w * (-q2_vec) + (-q2_vec).cross(dq1_vec);
52 Eigen::Vector4<T> dqRelDt;
53 dqRelDt << dqRel_vec, dqRel_w;
54 return dLogDq * dqRelDt;
59Vector3<T> quaternionLogMapRelativeDerivativeQ2(
63 const Eigen::Matrix<T, 3, 4>& dLogDq) {
67 const Vector3<T> q2_vec(q2.x(), q2.y(), q2.z());
69 const T temp_w = -q2inv_vec.dot(vHalf);
70 const Vector3<T> temp_vec = q2.w() * vHalf + q2inv_vec.cross(vHalf);
73 const Vector3<T> q1_vec(q1.x(), q1.y(), q1.z());
74 const T dqRel_w = temp_w * q1.w() - temp_vec.dot(q1_vec);
75 const Vector3<T> dqRel_vec = temp_w * q1_vec + q1.w() * temp_vec + temp_vec.cross(q1_vec);
78 Eigen::Vector4<T> dqRelDt;
79 dqRelDt << -dqRel_vec, -dqRel_w;
80 return dLogDq * dqRelDt;
97T computeLogMapRotationGradientPrev(
102 const Eigen::Matrix<T, 3, 4>& dLogDq) {
103 const Quaternion<T> transformedPrevRot = targetRot * prevRot;
104 const Quaternion<T> qRel = nextRot.conjugate() * transformedPrevRot;
106 const Vector3<T> transformedAxis = targetRot * axis;
108 quaternionLogMapRelativeDerivativeQ1(transformedPrevRot, nextRot, transformedAxis, dLogDq);
109 return T(2) * logmapVec.dot(jacobian);
119T computeLogMapRotationGradientNext(
123 const Eigen::Matrix<T, 3, 4>& dLogDq) {
124 const Quaternion<T> qRel = nextRot.conjugate() * transformedPrevRot;
127 quaternionLogMapRelativeDerivativeQ2(transformedPrevRot, nextRot, axis, dLogDq);
128 return T(2) * logmapVec.dot(jacobian);
135T computeMatrixDiffRotationGradientPrev(
141 return -T(2) * rotD.cwiseProduct(rotDiff).sum();
147T computeMatrixDiffRotationGradientNext(
152 return T(2) * rotD.cwiseProduct(rotDiff).sum();
163Eigen::Matrix<T, 3, 1> computeLogMapRotationJacobianPrev(
169 const Eigen::Matrix<T, 3, 4>& dLogDq) {
170 const Quaternion<T> transformedPrevRot = targetRot * prevRot;
171 const Vector3<T> transformedAxis = targetRot * axis;
173 quaternionLogMapRelativeDerivativeQ1(transformedPrevRot, nextRot, transformedAxis, dLogDq);
184Eigen::Matrix<T, 3, 1> computeLogMapRotationJacobianNext(
189 const Eigen::Matrix<T, 3, 4>& dLogDq) {
190 return weight * quaternionLogMapRelativeDerivativeQ2(transformedPrevRot, nextRot, axis, dLogDq);
197Eigen::Matrix<T, 9, 1> computeMatrixDiffRotationJacobianPrev(
202 const Eigen::Matrix3<T> rotD = -(targetRot *
crossProductMatrix(axis) * prevRot) * weight;
208Eigen::Matrix<T, 9, 1>
216enum class StateType { Prev, Next };
220struct GradientComputationParams {
222 const TransformT<T>& targetTransform;
223 const Eigen::Quaternion<T>& prevRot;
224 const Eigen::Quaternion<T>& nextRot;
225 const Eigen::Vector3<T>& diff;
229 const Eigen::Matrix3<T>& rotDiff;
230 const Eigen::Matrix<T, 3, 4>& dLogDq;
235void computeStateGradient(
237 const GradientComputationParams<T>& params,
241 const Eigen::VectorX<bool>& activeJointParams,
243 Eigen::Ref<Eigen::VectorX<T>> gradientSegment) {
244 const bool isPrev = (stateType == StateType::Prev);
245 const T posSign = isPrev ? T(-2) : T(2);
247 size_t ancestorJointIndex = params.jointIndex;
250 const Eigen::Vector3<T> posd = state.jointState[params.jointIndex].translation() -
251 state.jointState[ancestorJointIndex].translation();
253 for (
size_t d = 0; d < 3; d++) {
254 if (activeJointParams[paramIndex + d]) {
255 const auto& jointState = state.jointState[ancestorJointIndex];
260 params.targetTransform.rotate(jointState.getTranslationDerivative(d))) *
263 val = posSign * params.diff.dot(jointState.getTranslationDerivative(d)) * params.pwgt;
266 val, paramIndex + d, parameterTransform, gradientSegment);
269 if (activeJointParams[paramIndex + 3 + d]) {
270 const Eigen::Vector3<T> axis = state.jointState[ancestorJointIndex].rotationAxis.col(d);
271 const auto& jointState = state.jointState[ancestorJointIndex];
273 T positionErrorGradient;
275 positionErrorGradient = posSign *
277 params.targetTransform.rotate(jointState.getRotationDerivative(d, posd)));
279 positionErrorGradient =
280 posSign * params.diff.dot(jointState.getRotationDerivative(d, posd)) * params.pwgt;
283 T rotationErrorGradient = T(0);
284 switch (params.rotationErrorType) {
287 rotationErrorGradient = computeLogMapRotationGradientPrev(
288 prevState.jointState[params.jointIndex].rotation(),
290 params.targetTransform.rotation,
294 rotationErrorGradient = computeLogMapRotationGradientNext(
295 params.prevRot, params.nextRot, axis, params.dLogDq);
300 rotationErrorGradient = computeMatrixDiffRotationGradientPrev(
301 prevState.jointState[params.jointIndex].rotation(),
302 params.targetTransform.rotation,
306 rotationErrorGradient = computeMatrixDiffRotationGradientNext(
307 state.jointState[params.jointIndex].rotation(), axis, params.rotDiff);
313 ? (params.pwgt * positionErrorGradient + params.rwgt * rotationErrorGradient)
314 : (positionErrorGradient + params.rwgt * rotationErrorGradient);
316 val, paramIndex + 3 + d, parameterTransform, gradientSegment);
320 if (activeJointParams[paramIndex + 6]) {
321 const auto& jointState = state.jointState[ancestorJointIndex];
325 params.diff.dot(params.targetTransform.rotate(jointState.getScaleDerivative(posd))) *
328 val = posSign * params.diff.dot(jointState.getScaleDerivative(posd)) * params.pwgt;
331 val, paramIndex + 6, parameterTransform, gradientSegment);
334 ancestorJointIndex = skeleton.joints[ancestorJointIndex].parent;
340struct JacobianComputationParams {
342 const TransformT<T>& targetTransform;
343 const Eigen::Quaternion<T>& prevRot;
344 const Eigen::Quaternion<T>& nextRot;
347 size_t rotationResidualSize{0};
350 const Eigen::Matrix<T, 3, 4>& dLogDq;
355void computeStateJacobian(
357 const JacobianComputationParams<T>& params,
361 const Eigen::VectorX<bool>& activeJointParams,
363 Eigen::Ref<Eigen::MatrixX<T>> jacobianBlock) {
364 const bool isPrev = (stateType == StateType::Prev);
366 size_t ancestorJointIndex = params.jointIndex;
368 const auto& jointState = state.jointState[ancestorJointIndex];
370 const Eigen::Vector3<T> posd =
371 state.jointState[params.jointIndex].translation() - jointState.translation();
373 for (
size_t d = 0; d < 3; d++) {
374 if (activeJointParams[paramIndex + d]) {
375 Eigen::Vector3<T> jc;
378 -(params.targetTransform.rotate(jointState.getTranslationDerivative(d))) * params.wgt;
380 jc = jointState.getTranslationDerivative(d) * params.wgt;
383 jc, paramIndex + d, parameterTransform, jacobianBlock.template topRows<3>());
386 if (activeJointParams[paramIndex + 3 + d]) {
387 const Eigen::Vector3<T> axis = state.jointState[ancestorJointIndex].rotationAxis.col(d);
389 Eigen::Vector3<T> jc;
391 jc = -(params.targetTransform.rotate(jointState.getRotationDerivative(d, posd))) *
394 jc = jointState.getRotationDerivative(d, posd) * params.wgt;
397 jc, paramIndex + d + 3, parameterTransform, jacobianBlock.template topRows<3>());
399 switch (params.rotationErrorType) {
401 Eigen::Matrix<T, 3, 1> rotJac;
403 rotJac = computeLogMapRotationJacobianPrev(
404 prevState.jointState[params.jointIndex].rotation(),
406 params.targetTransform.rotation,
411 rotJac = computeLogMapRotationJacobianNext(
412 params.prevRot, params.nextRot, axis, params.awgt, params.dLogDq);
418 jacobianBlock.middleRows(3, params.rotationResidualSize));
422 Eigen::Matrix<T, 9, 1> rotJac;
424 rotJac = computeMatrixDiffRotationJacobianPrev(
425 prevState.jointState[params.jointIndex].rotation(),
426 params.targetTransform.rotation,
430 rotJac = computeMatrixDiffRotationJacobianNext(
431 state.jointState[params.jointIndex].rotation(), axis, params.awgt);
437 jacobianBlock.middleRows(3, params.rotationResidualSize));
444 if (activeJointParams[paramIndex + 6]) {
445 Eigen::Vector3<T> jc;
447 jc = -(params.targetTransform.rotate(jointState.getScaleDerivative(posd))) * params.wgt;
449 jc = jointState.getScaleDerivative(posd) * params.wgt;
452 jc, paramIndex + 6, parameterTransform, jacobianBlock.template topRows<3>());
455 ancestorJointIndex = skeleton.joints[ancestorJointIndex].parent;
467 targetPositionWeights_.setOnes(this->
skeleton_.joints.size());
468 targetRotationWeights_.setOnes(this->skeleton_.joints.size());
479 character.parameterTransform,
480 rotationErrorType) {}
484 targetPositionWeights_.setOnes(this->
skeleton_.joints.size());
485 targetRotationWeights_.setOnes(this->
skeleton_.joints.size());
490 MT_CHECK(target.size() == this->skeleton_.joints.size());
491 targetState_ = target;
496 const Eigen::VectorX<T>& posWeight,
497 const Eigen::VectorX<T>& rotWeight) {
504 MT_CHECK(posWeight.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
505 targetPositionWeights_ = posWeight;
510 MT_CHECK(rotWeight.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
511 targetRotationWeights_ = rotWeight;
526 const auto& prevState = skelStates[0];
527 const auto& nextState = skelStates[1];
531 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
533 T rotationError = T(0);
536 (i < targetState_.size()) ? targetState_[i] :
TransformT<T>();
537 const TransformT<T> transformedPrev = targetTransform * prevState.jointState[i].transform;
538 const Eigen::Quaternion<T>& prevRot = transformedPrev.
rotation;
539 const Eigen::Quaternion<T>& nextRot = nextState.jointState[i].rotation();
541 switch (rotationErrorType_) {
544 const Eigen::Quaternion<T> qRel = nextRot.conjugate() * prevRot;
550 rotationError = logmapVec.squaredNorm();
557 const Eigen::Matrix3<T> rotDiff = nextRot.toRotationMatrix() - prevRot.toRotationMatrix();
558 rotationError = rotDiff.squaredNorm();
566 const Eigen::Vector3<T> diff =
567 nextState.jointState[i].translation() - transformedPrev.
translation;
568 error += diff.squaredNorm() *
kPositionWeight * posWgt_ * targetPositionWeights_[i];
580 Eigen::Ref<Eigen::VectorX<T>> gradient)
const {
584 const auto& prevState = skelStates[0];
585 const auto& nextState = skelStates[1];
588 MT_CHECK(gradient.size() == 2 * nParam);
595 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
596 if (targetRotationWeights_[i] == 0 && targetPositionWeights_[i] == 0) {
602 (i < targetState_.size()) ? targetState_[i] :
TransformT<T>();
603 const TransformT<T> transformedPrev = targetTransform * prevState.jointState[i].transform;
604 const Eigen::Quaternion<T>& prevRot = transformedPrev.
rotation;
605 const Eigen::Quaternion<T>& nextRot = nextState.jointState[i].rotation();
607 T rotationError = T(0);
608 Eigen::Matrix3<T> rotDiff;
609 Eigen::Matrix<T, 3, 4> dLogDq;
611 switch (rotationErrorType_) {
614 const Eigen::Quaternion<T> qRel = nextRot.conjugate() * prevRot;
623 rotationError = logmapVec.squaredNorm();
627 rotDiff = nextRot.toRotationMatrix() - prevRot.toRotationMatrix();
628 rotationError = rotDiff.squaredNorm();
634 error += rotationError * rwgt;
637 const Eigen::Vector3<T> diff =
638 nextState.jointState[i].translation() - transformedPrev.
translation;
640 error += diff.squaredNorm() * pwgt;
641 const GradientComputationParams<T> gradParams{
653 computeStateGradient<T>(
661 gradient.segment(0, nParam));
663 computeStateGradient<T>(
671 gradient.segment(nParam, nParam));
680 const auto nActiveJoints =
681 (targetPositionWeights_.array() != 0 || targetRotationWeights_.array() != 0).count();
684 return nActiveJoints * 6;
687 return nActiveJoints * 12;
696 Eigen::Ref<Eigen::MatrixX<T>> jacobian_full,
697 Eigen::Ref<Eigen::VectorX<T>> residual_full,
698 int& usedRows)
const {
702 const auto& prevState = skelStates[0];
703 const auto& nextState = skelStates[1];
706 MT_CHECK(jacobian_full.cols() == 2 * nParam);
710 Eigen::Index offset = 0;
713 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
714 if (targetRotationWeights_[i] == 0 && targetPositionWeights_[i] == 0) {
719 (i < targetState_.size()) ? targetState_[i] :
TransformT<T>();
720 const TransformT<T> transformedPrev = targetTransform * prevState.jointState[i].transform;
725 const size_t rotationResidualSize =
727 const size_t blockSize = 3 + rotationResidualSize;
730 const auto transDiff =
731 (nextState.jointState[i].translation() - transformedPrev.
translation).eval();
734 const Eigen::Quaternion<T>& prevRot = transformedPrev.
rotation;
735 const Eigen::Quaternion<T>& nextRot = nextState.jointState[i].rotation();
739 const T wgt = std::sqrt(pwgt);
740 error += transDiff.squaredNorm() * pwgt;
741 residual_full.template segment<3>(offset).noalias() = transDiff * wgt;
744 const T awgt = std::sqrt(rwgt);
746 Eigen::Matrix<T, 3, 4> dLogDq;
747 switch (rotationErrorType_) {
750 const Eigen::Quaternion<T> qRel = nextRot.conjugate() * prevRot;
760 error += logmapVec.squaredNorm() * rwgt;
763 residual_full.template segment<3>(offset + 3).noalias() = logmapVec * awgt;
768 const Eigen::Matrix3<T> rotDiff = nextRot.toRotationMatrix() - prevRot.toRotationMatrix();
769 error += rotDiff.squaredNorm() * rwgt;
770 residual_full.template segment<9>(offset + 3).noalias() =
776 const JacobianComputationParams<T> jacParams{
783 rotationResidualSize,
788 computeStateJacobian<T>(
796 jacobian_full.block(offset, 0, blockSize, nParam));
798 computeStateJacobian<T>(
806 jacobian_full.block(offset, nParam, blockSize, nParam));
811 usedRows = gsl::narrow_cast<int>(offset);
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
const ParameterTransform & parameterTransform_
VectorX< bool > activeJointParams_
const Skeleton & skeleton_
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Error function that penalizes the difference between the current skeleton state and the next skeleton...
void setRotationTargetWeights(const Eigen::VectorX< T > &rotWeight)
Set the target rotation weights for each joint.
static constexpr T kPositionWeight
double getError(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > >) const final
double getGradient(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > >, Eigen::Ref< Eigen::VectorX< T > > gradient) const final
void setTargetState(TransformListT< T > target)
Set the target state for the skeleton.
double getJacobian(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > >, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) const final
size_t getJacobianSize() const final
void setTargetWeights(const Eigen::VectorX< T > &posWeight, const Eigen::VectorX< T > &rotWeight)
Set the target weights for each joint's position and rotation.
static constexpr T kOrientationWeight
StateSequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, RotationErrorType rotationErrorType=RotationErrorType::RotationMatrixDifference)
void setPositionTargetWeights(const Eigen::VectorX< T > &posWeight)
Set the target position weights for each joint.
Eigen::Matrix< T, 3, 3 > crossProductMatrix(const Eigen::Vector3< T > &v)
Creates a skew-symmetric matrix for computing cross products.
RotationErrorType
Defines different methods for computing rotation error between two quaternions.
@ RotationMatrixDifference
Frobenius norm of rotation matrix difference: ||R1 - R2||_F^2.
@ QuaternionLogMap
Logarithmic map of relative rotation: ||log(R1^{-1} * R2)||^2.
SkeletonT< float > Skeleton
std::vector< TransformT< T > > TransformListT
Per-joint transform list describing the state of all joints in a skeleton (one entry per joint,...
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)
Vector3< T > quaternionLogMap(const Quaternion< T > &q)
Computes the logarithmic map from a quaternion to a rotation vector (axis-angle in R^3).
FloatP dot(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes dot product of Eigen::Vector3f and 3-vector of packets.
ParameterTransformT< float > ParameterTransform
Eigen::Quaternion< T > Quaternion
Unit quaternion representing a 3D rotation.
void gradient_jointParams_to_modelParams(const T &grad_jointParams, const Eigen::Index iJointParam, const ParameterTransform ¶meterTransform, Eigen::Ref< Eigen::VectorX< T > > gradient)
constexpr size_t kParametersPerJoint
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Eigen::Matrix< T, 3, 4 > quaternionLogMapDerivative(const Quaternion< T > &q)
Computes the derivative of the logarithmic map with respect to quaternion components.
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.
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.