34Vector3<T> quaternionLogMapRelativeDerivativeQ1(
38 const Eigen::Matrix<T, 3, 4>& dLogDq) {
51 const Vector3<T> q1_vec(q1.x(), q1.y(), q1.z());
52 const T dq1_w = -vHalf.dot(q1_vec);
53 const Vector3<T> dq1_vec = vHalf * q1.w() + vHalf.cross(q1_vec);
57 const Vector3<T> q2_vec(q2.x(), q2.y(), q2.z());
58 const T dqRel_w = q2.w() * dq1_w - (-q2_vec).
dot(dq1_vec);
59 const Vector3<T> dqRel_vec = q2.w() * dq1_vec + dq1_w * (-q2_vec) + (-q2_vec).cross(dq1_vec);
63 Eigen::Vector4<T> dqRelDt;
64 dqRelDt << dqRel_vec, dqRel_w;
65 return dLogDq * dqRelDt;
71T computeLogMapRotationGradient(
75 const Eigen::Matrix<T, 3, 4>& dLogDq) {
83 const Vector3<T> jacobian1 = quaternionLogMapRelativeDerivativeQ1(rot, target, axis, dLogDq);
86 return T(2) * logmapVec.dot(jacobian1);
92T computeMatrixDiffRotationGradient(
97 const Eigen::Matrix3<T> rotDiff = rot.toRotationMatrix() - target.toRotationMatrix();
98 return T(2) * rotD.cwiseProduct(rotDiff).sum();
104Eigen::Matrix<T, 3, 1> computeLogMapRotationJacobian(
109 const Eigen::Matrix<T, 3, 4>& dLogDq) {
111 return weight * quaternionLogMapRelativeDerivativeQ1(rot, target, axis, dLogDq);
117Eigen::Matrix<T, 9, 1>
131 targetPositionWeights_.setOnes(this->
skeleton_.joints.size());
132 targetRotationWeights_.setOnes(this->skeleton_.joints.size());
141 :
StateErrorFunctionT<T>(character.skeleton, character.parameterTransform, rotationErrorType) {}
145 targetPositionWeights_.setOnes(this->
skeleton_.joints.size());
146 targetRotationWeights_.setOnes(this->
skeleton_.joints.size());
163 MT_CHECK(target.size() == this->skeleton_.joints.size());
164 this->targetState_ = std::move(target);
169 MT_CHECK(weights.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
171 targetPositionWeights_ = weights;
172 targetRotationWeights_ = weights;
177 const Eigen::VectorX<T>& posWeight,
178 const Eigen::VectorX<T>& rotWeight) {
179 MT_CHECK(posWeight.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
180 MT_CHECK(rotWeight.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
182 targetPositionWeights_ = posWeight;
183 targetRotationWeights_ = rotWeight;
188 MT_CHECK(posWeight.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
189 targetPositionWeights_ = posWeight;
194 MT_CHECK(rotWeight.size() ==
static_cast<Eigen::Index
>(this->skeleton_.joints.size()));
195 targetRotationWeights_ = rotWeight;
218 if (targetState_.size() != state.
jointState.size()) {
224 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
226 T rotationError = T(0);
228 const Eigen::Quaternion<T>& target = targetState_[i].rotation.normalized();
229 const Eigen::Quaternion<T>& rot = state.
jointState[i].rotation();
231 switch (rotationErrorType_) {
234 const Eigen::Quaternion<T> qRel = target.conjugate() * rot;
240 rotationError = logmapVec.squaredNorm();
247 const Eigen::Matrix3<T> rotDiff = rot.toRotationMatrix() - target.toRotationMatrix();
248 rotationError = rotDiff.squaredNorm();
256 const Eigen::Vector3<T> diff = state.
jointState[i].translation() - targetState_[i].translation;
257 error += diff.squaredNorm() *
kPositionWeight * posWgt_ * targetPositionWeights_[i];
269 Ref<Eigen::VectorX<T>> gradient) {
285 if (targetState_.size() != state.
jointState.size()) {
291 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
292 if (targetRotationWeights_[i] == 0 && targetPositionWeights_[i] == 0) {
297 const Eigen::Quaternion<T>& target = targetState_[i].rotation.normalized();
298 const Eigen::Quaternion<T>& rot = state.
jointState[i].rotation();
300 T rotationError = T(0);
301 Eigen::Matrix<T, 3, 4> dLogDq;
303 switch (rotationErrorType_) {
306 const Eigen::Quaternion<T> qRel = target.conjugate() * rot;
316 rotationError = logmapVec.squaredNorm();
320 const Eigen::Matrix3<T> rotDiff = rot.toRotationMatrix() - target.toRotationMatrix();
321 rotationError = rotDiff.squaredNorm();
327 error += rotationError * rwgt;
330 const Eigen::Vector3<T> diff = state.
jointState[i].translation() - targetState_[i].translation;
332 error += diff.squaredNorm() * pwgt;
335 size_t jointIndex = i;
343 const Eigen::Vector3<T> posd =
346 for (
size_t d = 0; d < 3; d++) {
351 T(2) * diff.dot(state.
jointState[jointIndex].getTranslationDerivative(d)) * pwgt;
357 const Eigen::Vector3<T> axis = state.
jointState[jointIndex].rotationAxis.col(d);
361 switch (rotationErrorType_) {
363 rotGradient = computeLogMapRotationGradient(rot, target, axis, dLogDq);
366 rotGradient = computeMatrixDiffRotationGradient(rot, target, axis);
371 T(2) * diff.dot(state.
jointState[jointIndex].getRotationDerivative(d, posd)) * pwgt +
379 const T val = T(2) * diff.dot(state.
jointState[jointIndex].getScaleDerivative(posd)) * pwgt;
385 jointIndex = this->
skeleton_.joints[jointIndex].parent;
395 const auto nActiveJoints =
396 (targetPositionWeights_.array() != 0 || targetRotationWeights_.array() != 0).count();
399 return nActiveJoints * 6;
402 return nActiveJoints * 12;
411 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
412 Eigen::Ref<Eigen::VectorX<T>> residual,
420 if (targetState_.size() != state.
jointState.size()) {
424 Eigen::Index offset = 0;
427 for (
size_t i = 0; i < this->
skeleton_.joints.size(); ++i) {
428 if (targetRotationWeights_[i] == 0 && targetPositionWeights_[i] == 0) {
435 const size_t rotationResidualSize =
437 const size_t blockSize = 3 + rotationResidualSize;
440 const auto transDiff = (state.
jointState[i].translation() - targetState_[i].translation).eval();
443 const Eigen::Quaternion<T>& target = targetState_[i].rotation.normalized();
444 const Eigen::Quaternion<T>& rot = state.
jointState[i].rotation();
449 const T wgt = std::sqrt(pwgt);
450 const T awgt = std::sqrt(rwgt);
451 error += transDiff.squaredNorm() * pwgt;
452 residual.template segment<3>(offset).noalias() = transDiff * wgt;
454 Eigen::Matrix<T, 3, 4> dLogDq;
455 switch (rotationErrorType_) {
458 const Eigen::Quaternion<T> qRel = target.conjugate() * rot;
468 error += logmapVec.squaredNorm() * rwgt;
472 residual.template segment<3>(offset + 3).noalias() = logmapVec * awgt;
477 const Eigen::Matrix3<T> rotDiff = rot.toRotationMatrix() - target.toRotationMatrix();
478 error += rotDiff.squaredNorm() * rwgt;
479 residual.template segment<9>(offset + 3).noalias() =
486 size_t jointIndex = i;
491 const auto& jointState = state.
jointState[jointIndex];
495 const Eigen::Vector3<T> posd = state.
jointState[i].translation() - jointState.translation();
498 for (
size_t d = 0; d < 3; d++) {
501 jointState.getTranslationDerivative(d) * wgt,
503 this->parameterTransform_,
504 jacobian.template middleRows<3>(offset));
509 const Eigen::Vector3<T> jc = jointState.getRotationDerivative(d, posd) * wgt;
514 jacobian.template middleRows<3>(offset));
517 const Eigen::Vector3<T> axis = state.
jointState[jointIndex].rotationAxis.col(d);
520 switch (rotationErrorType_) {
523 computeLogMapRotationJacobian(rot, target, axis, awgt, dLogDq),
526 jacobian.middleRows(offset + 3, rotationResidualSize));
530 computeMatrixDiffRotationJacobian(rot, axis, awgt),
533 jacobian.middleRows(offset + 3, rotationResidualSize));
541 const Eigen::Vector3<T> jc = jointState.getScaleDerivative(posd) * wgt;
547 jointIndex = this->
skeleton_.joints[jointIndex].parent;
554 usedRows = gsl::narrow_cast<int>(offset);
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
const Skeleton & skeleton_
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
VectorX< bool > activeJointParams_
const ParameterTransform & parameterTransform_
void setTargetWeights(const Eigen::VectorX< T > &posWeight, const Eigen::VectorX< T > &rotWeight)
static constexpr T kPositionWeight
double getJacobian(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
void setTargetState(const SkeletonStateT< T > *target)
StateErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, RotationErrorType rotationErrorType=RotationErrorType::RotationMatrixDifference)
static constexpr T kOrientationWeight
size_t getJacobianSize() const final
void setTargetWeight(const Eigen::VectorX< T > &weights)
double getError(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
double getGradient(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
void setRotationTargetWeights(const Eigen::VectorX< T > &rotWeight)
Set the target rotation weights for each joint.
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,...
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.
JointParametersT< T > jointParameters
Joint parameters for all joints in the skeleton.
TransformListT< T > toTransforms() const
Extracts global transforms for all joints in the skeleton.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.