21constexpr float kPositionWeight = 1e-4f;
31template <
typename T,
bool IsL2>
38 if (!enabledParameters.test(data.parameterIndex)) {
42 if (params(data.parameterIndex) < data.limits[0]) {
43 const T val = data.limits[0] - params(data.parameterIndex);
44 const T sqrError = val * val;
46 error = limitWeight * sqrError;
48 error = limitWeight * loss.value(sqrError);
51 if (params(data.parameterIndex) > data.limits[1]) {
52 const T val = data.limits[1] - params(data.parameterIndex);
53 const T sqrError = val * val;
55 error = limitWeight * sqrError;
57 error = limitWeight * loss.value(sqrError);
63template <
typename T,
bool IsL2>
64T computeMinMaxJointError(
69 const Eigen::VectorX<bool>& activeJointParams) {
71 MT_CHECK(parameterIndex <
static_cast<size_t>(state.jointParameters.size()));
72 if (!activeJointParams[parameterIndex]) {
76 if (state.jointParameters(parameterIndex) < data.limits[0]) {
77 const T val = data.limits[0] - state.jointParameters[parameterIndex];
78 const T sqrError = val * val;
80 error = limitWeight * sqrError;
82 error = limitWeight * loss.value(sqrError);
85 if (state.jointParameters(parameterIndex) > data.limits[1]) {
86 const T val = data.limits[1] - state.jointParameters[parameterIndex];
87 const T sqrError = val * val;
89 error = limitWeight * sqrError;
91 error = limitWeight * loss.value(sqrError);
97template <
typename T,
bool IsL2>
104 MT_CHECK(data.referenceIndex <
static_cast<size_t>(params.size()));
105 MT_CHECK(data.targetIndex <
static_cast<size_t>(params.size()));
106 if ((!enabledParameters.test(data.targetIndex) && !enabledParameters.test(data.referenceIndex)) ||
107 !
isInRange(data, params(data.targetIndex))) {
111 params(data.targetIndex) * data.scale - data.offset - params(data.referenceIndex);
112 const T sqrError = residual * residual;
113 if constexpr (IsL2) {
114 return limitWeight * sqrError;
116 return limitWeight * loss.value(sqrError);
120template <
typename T,
bool IsL2>
121T computeLinearJointError(
126 const Eigen::VectorX<bool>& activeJointParams) {
127 const size_t referenceParameterIndex =
129 const size_t targetParameterIndex =
131 MT_CHECK(referenceParameterIndex <
static_cast<size_t>(state.jointParameters.size()));
132 MT_CHECK(targetParameterIndex <
static_cast<size_t>(state.jointParameters.size()));
133 if ((!activeJointParams[referenceParameterIndex] && !activeJointParams[targetParameterIndex]) ||
134 !
isInRange(data, state.jointParameters[targetParameterIndex])) {
137 const T residual = state.jointParameters(targetParameterIndex) * data.scale - data.offset -
138 state.jointParameters(referenceParameterIndex);
139 const T sqrError = residual * residual;
140 if constexpr (IsL2) {
141 return limitWeight * sqrError;
143 return limitWeight * loss.value(sqrError);
147template <
typename T,
bool IsL2>
148T computeHalfPlaneError(
154 MT_CHECK(data.param1 <
static_cast<size_t>(params.size()));
155 MT_CHECK(data.param2 <
static_cast<size_t>(params.size()));
156 if (!enabledParameters.test(data.param1) && !enabledParameters.test(data.param2)) {
159 const Eigen::Vector2<T> p(params(data.param1), params(data.param2));
160 const T residual = p.dot(data.normal.template
cast<T>()) - data.offset;
162 const T sqrError = residual * residual;
163 if constexpr (IsL2) {
164 return limitWeight * sqrError;
166 return limitWeight * loss.value(sqrError);
172template <
typename T,
bool IsL2>
173T computeEllipsoidError(
178 const Eigen::Vector3<T> position =
179 state.jointState[ct.parent].transform * ct.offset.template
cast<T>();
180 const Eigen::Vector3<T> localPosition =
181 state.jointState[ct.ellipsoidParent].transform.inverse() * position;
182 const Eigen::Vector3<T> ellipsoidPosition = ct.ellipsoidInv.template
cast<T>() * localPosition;
183 const Eigen::Vector3<T> normalizedPosition = ellipsoidPosition.normalized();
184 const Eigen::Vector3<T> projectedPosition = ct.ellipsoid.template
cast<T>() * normalizedPosition;
185 const Eigen::Vector3<T> diff =
186 position - state.jointState[ct.ellipsoidParent].transform * projectedPosition;
187 const T sqrError = diff.squaredNorm();
188 if constexpr (IsL2) {
189 return T(kPositionWeight) * limitWeight * sqrError;
191 return T(kPositionWeight) * limitWeight * loss.value(sqrError);
195template <
typename T,
bool IsL2>
196T computeMinMaxGradient(
203 Ref<Eigen::VectorX<T>> gradient) {
204 if (!enabledParameters.test(data.parameterIndex)) {
208 if (params(data.parameterIndex) < data.limits[0]) {
209 const T val = params(data.parameterIndex) - data.limits[0];
210 const T sqrError = val * val;
211 if constexpr (IsL2) {
212 error = tWeight * limitWeight * sqrError;
213 gradient[data.parameterIndex] += T(2) * val * tWeight * limitWeight;
215 error = tWeight * limitWeight * loss.value(sqrError);
216 gradient[data.parameterIndex] += T(2) * val * tWeight * limitWeight * loss.deriv(sqrError);
219 if (params(data.parameterIndex) > data.limits[1]) {
220 const T val = params(data.parameterIndex) - data.limits[1];
221 const T sqrError = val * val;
222 if constexpr (IsL2) {
223 error = tWeight * limitWeight * sqrError;
224 gradient[data.parameterIndex] += T(2) * val * tWeight * limitWeight;
226 error = tWeight * limitWeight * loss.value(sqrError);
227 gradient[data.parameterIndex] += T(2) * val * tWeight * limitWeight * loss.deriv(sqrError);
233template <
typename T,
bool IsL2>
234T computeMinMaxJointGradient(
240 const Eigen::VectorX<bool>& activeJointParams,
242 Ref<Eigen::VectorX<T>> gradient) {
244 MT_CHECK(parameterIndex <
static_cast<size_t>(state.jointParameters.size()));
245 if (!activeJointParams[parameterIndex]) {
250 if (state.jointParameters(parameterIndex) < data.limits[0]) {
251 const T val = state.jointParameters[parameterIndex] - data.limits[0];
252 const T sqrError = val * val;
254 if constexpr (IsL2) {
255 error = tWeight * limitWeight * sqrError;
256 jGrad = T(2) * val * tWeight * limitWeight;
258 error = tWeight * limitWeight * loss.value(sqrError);
259 jGrad = T(2) * val * tWeight * limitWeight * loss.deriv(sqrError);
263 if (state.jointParameters(parameterIndex) > data.limits[1]) {
264 const T val = state.jointParameters[parameterIndex] - data.limits[1];
265 const T sqrError = val * val;
267 if constexpr (IsL2) {
268 error = tWeight * limitWeight * sqrError;
269 jGrad = T(2) * val * tWeight * limitWeight;
271 error = tWeight * limitWeight * loss.value(sqrError);
272 jGrad = T(2) * val * tWeight * limitWeight * loss.deriv(sqrError);
279template <
typename T,
bool IsL2>
280T computeLinearGradient(
287 Ref<Eigen::VectorX<T>> gradient) {
288 MT_CHECK(data.referenceIndex <
static_cast<size_t>(params.size()));
289 MT_CHECK(data.targetIndex <
static_cast<size_t>(params.size()));
290 if (!
isInRange(data, params(data.targetIndex))) {
294 params(data.targetIndex) * data.scale - data.offset - params(data.referenceIndex);
295 const T sqrError = residual * residual;
298 if constexpr (IsL2) {
299 error = limitWeight * tWeight * sqrError;
300 gradScale = T(2) * residual * limitWeight * tWeight;
302 error = limitWeight * tWeight * loss.value(sqrError);
303 gradScale = T(2) * residual * limitWeight * tWeight * loss.deriv(sqrError);
305 if (enabledParameters.test(data.targetIndex)) {
306 gradient[data.targetIndex] += gradScale * T(data.scale);
308 if (enabledParameters.test(data.referenceIndex)) {
309 gradient[data.referenceIndex] -= gradScale;
314template <
typename T,
bool IsL2>
315T computeLinearJointGradient(
321 const Eigen::VectorX<bool>& activeJointParams,
323 Ref<Eigen::VectorX<T>> gradient) {
324 const size_t referenceParameterIndex =
326 const size_t targetParameterIndex =
328 MT_CHECK(referenceParameterIndex <
static_cast<size_t>(state.jointParameters.size()));
329 MT_CHECK(targetParameterIndex <
static_cast<size_t>(state.jointParameters.size()));
331 if ((!activeJointParams[referenceParameterIndex] && !activeJointParams[targetParameterIndex]) ||
332 !
isInRange(data, state.jointParameters[targetParameterIndex])) {
336 const T residual = state.jointParameters(targetParameterIndex) * data.scale - data.offset -
337 state.jointParameters(referenceParameterIndex);
338 const T sqrError = residual * residual;
341 if constexpr (IsL2) {
342 error = limitWeight * tWeight * sqrError;
343 gradScale = T(2) * residual * limitWeight * tWeight;
345 error = limitWeight * tWeight * loss.value(sqrError);
346 gradScale = T(2) * residual * limitWeight * tWeight * loss.deriv(sqrError);
349 const T targetGrad = gradScale * T(data.scale);
351 targetGrad, targetParameterIndex, parameterTransform, gradient);
353 const T referenceGrad = -gradScale;
355 referenceGrad, referenceParameterIndex, parameterTransform, gradient);
360template <
typename T,
bool IsL2>
361T computeHalfPlaneGradient(
368 Ref<Eigen::VectorX<T>> gradient) {
369 if (!enabledParameters.test(data.param1) && !enabledParameters.test(data.param2)) {
372 const Eigen::Vector2<T> p(params(data.param1), params(data.param2));
373 const T residual = p.dot(data.normal.template
cast<T>()) - data.offset;
374 if (residual >= T(0)) {
377 const T sqrError = residual * residual;
380 if constexpr (IsL2) {
381 error = limitWeight * tWeight * sqrError;
382 gradScale = T(2) * residual * limitWeight * tWeight;
384 error = limitWeight * tWeight * loss.value(sqrError);
385 gradScale = T(2) * residual * limitWeight * tWeight * loss.deriv(sqrError);
387 if (enabledParameters.test(data.param1)) {
388 gradient[data.param1] += gradScale * T(data.normal[0]);
390 if (enabledParameters.test(data.param2)) {
391 gradient[data.param2] += gradScale * T(data.normal[1]);
396template <
typename T,
bool IsL2>
397T computeEllipsoidGradient(
403 const Eigen::VectorX<bool>& activeJointParams,
406 Ref<Eigen::VectorX<T>> gradient) {
407 const Eigen::Vector3<T> position =
408 state.jointState[ct.parent].transform * ct.offset.template
cast<T>();
409 const Eigen::Vector3<T> localPosition =
410 state.jointState[ct.ellipsoidParent].transform.inverse() * position;
411 const Eigen::Vector3<T> ellipsoidPosition = ct.ellipsoidInv.template
cast<T>() * localPosition;
412 const Eigen::Vector3<T> normalizedPosition = ellipsoidPosition.normalized();
413 const Eigen::Vector3<T> projectedPosition = ct.ellipsoid.template
cast<T>() * normalizedPosition;
414 const Eigen::Vector3<T> diff =
415 position - state.jointState[ct.ellipsoidParent].transform * projectedPosition;
416 const T sqrError = diff.squaredNorm();
422 if constexpr (IsL2) {
423 wgt = T(2) * T(kPositionWeight) * limitWeight * tWeight;
425 wgt = T(2) * T(kPositionWeight) * limitWeight * tWeight * loss.deriv(sqrError);
428 size_t jointIndex = ct.parent;
429 while (jointIndex != ct.ellipsoidParent && jointIndex !=
kInvalidIndex) {
430 MT_CHECK(jointIndex < skeleton.joints.size());
431 const auto& jointState = state.jointState[jointIndex];
433 const Eigen::Vector3<T> posd = position - jointState.translation();
435 for (
size_t d = 0; d < 3; d++) {
436 if (activeJointParams[paramIndex + d]) {
437 const T val = diff.dot(jointState.getTranslationDerivative(d)) * wgt;
440 if (activeJointParams[paramIndex + 3 + d]) {
441 const T val = diff.dot(jointState.getRotationDerivative(d, posd)) * wgt;
445 if (activeJointParams[paramIndex + 6]) {
446 const T val = diff.dot(jointState.getScaleDerivative(posd)) * wgt;
449 jointIndex = skeleton.joints[jointIndex].parent;
452 if constexpr (IsL2) {
453 return T(kPositionWeight) * limitWeight * tWeight * sqrError;
455 return T(kPositionWeight) * limitWeight * tWeight * loss.value(sqrError);
459template <
typename T,
bool IsL2>
460T computeMinMaxJacobian(
468 Ref<Eigen::MatrixX<T>> jacobian,
469 Ref<Eigen::VectorX<T>> residual,
471 if (!enabledParameters.test(data.parameterIndex)) {
474 if (params(data.parameterIndex) < data.limits[0]) {
475 const T val = params(data.parameterIndex) - data.limits[0];
476 const T sqrError = val * val;
477 if constexpr (IsL2) {
478 jacobian(row, data.parameterIndex) = wgt;
479 residual(row) = val * wgt;
480 return tWeight * limitWeight * sqrError;
482 const T wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
483 jacobian(row, data.parameterIndex) = wgtLoss;
484 residual(row) = val * wgtLoss;
485 return tWeight * limitWeight * loss.value(sqrError);
488 if (params(data.parameterIndex) > data.limits[1]) {
489 const T val = params(data.parameterIndex) - data.limits[1];
490 const T sqrError = val * val;
491 if constexpr (IsL2) {
492 jacobian(row, data.parameterIndex) = wgt;
493 residual(row) = val * wgt;
494 return tWeight * limitWeight * sqrError;
496 const T wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
497 jacobian(row, data.parameterIndex) = wgtLoss;
498 residual(row) = val * wgtLoss;
499 return tWeight * limitWeight * loss.value(sqrError);
505template <
typename T,
bool IsL2>
506T computeMinMaxJointJacobian(
513 const Eigen::VectorX<bool>& activeJointParams,
515 Ref<Eigen::MatrixX<T>> jacobian,
516 Ref<Eigen::VectorX<T>> residual,
519 MT_CHECK(jointIndex <
static_cast<size_t>(state.jointParameters.size()));
520 if (!activeJointParams[jointIndex]) {
523 if (state.jointParameters(jointIndex) < data.limits[0]) {
524 const T val = state.jointParameters[jointIndex] - data.limits[0];
525 const T sqrError = val * val;
528 if constexpr (IsL2) {
530 error = tWeight * limitWeight * sqrError;
532 wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
533 error = tWeight * limitWeight * loss.value(sqrError);
536 wgtLoss, Eigen::Index(jointIndex), Eigen::Index(row), parameterTransform, jacobian);
537 residual(row) = val * wgtLoss;
540 if (state.jointParameters(jointIndex) > data.limits[1]) {
541 const T val = state.jointParameters[jointIndex] - data.limits[1];
542 const T sqrError = val * val;
545 if constexpr (IsL2) {
547 error = tWeight * limitWeight * sqrError;
549 wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
550 error = tWeight * limitWeight * loss.value(sqrError);
553 wgtLoss, Eigen::Index(jointIndex), Eigen::Index(row), parameterTransform, jacobian);
554 residual(row) = val * wgtLoss;
560template <
typename T,
bool IsL2>
561T computeLinearJacobian(
569 Ref<Eigen::MatrixX<T>> jacobian,
570 Ref<Eigen::VectorX<T>> residual,
572 MT_CHECK(data.referenceIndex <
static_cast<size_t>(params.size()));
573 MT_CHECK(data.targetIndex <
static_cast<size_t>(params.size()));
575 if ((!enabledParameters.test(data.targetIndex) && !enabledParameters.test(data.referenceIndex)) ||
576 !
isInRange(data, params(data.targetIndex))) {
579 const T res = params(data.targetIndex) * data.scale - data.offset - params(data.referenceIndex);
580 const T sqrError = res * res;
583 if constexpr (IsL2) {
585 error = tWeight * limitWeight * sqrError;
587 wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
588 error = tWeight * limitWeight * loss.value(sqrError);
590 residual(row) = res * wgtLoss;
591 if (enabledParameters.test(data.targetIndex)) {
592 jacobian(row, data.targetIndex) = T(data.scale) * wgtLoss;
594 if (enabledParameters.test(data.referenceIndex)) {
595 jacobian(row, data.referenceIndex) = -wgtLoss;
600template <
typename T,
bool IsL2>
601T computeLinearJointJacobian(
608 const Eigen::VectorX<bool>& activeJointParams,
610 Ref<Eigen::MatrixX<T>> jacobian,
611 Ref<Eigen::VectorX<T>> residual,
613 const size_t referenceParameterIndex =
615 const size_t targetParameterIndex =
617 MT_CHECK(referenceParameterIndex <
static_cast<size_t>(state.jointParameters.size()));
618 MT_CHECK(targetParameterIndex <
static_cast<size_t>(state.jointParameters.size()));
620 if ((!activeJointParams[referenceParameterIndex] && !activeJointParams[targetParameterIndex]) ||
621 !
isInRange(data, state.jointParameters(targetParameterIndex))) {
625 const T res = state.jointParameters(targetParameterIndex) * data.scale - data.offset -
626 state.jointParameters(referenceParameterIndex);
627 const T sqrError = res * res;
630 if constexpr (IsL2) {
632 error = tWeight * limitWeight * sqrError;
634 wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
635 error = tWeight * limitWeight * loss.value(sqrError);
637 residual(row) = res * wgtLoss;
639 if (activeJointParams[targetParameterIndex]) {
641 T(data.scale) * wgtLoss,
642 Eigen::Index(targetParameterIndex),
647 if (activeJointParams[referenceParameterIndex]) {
650 Eigen::Index(referenceParameterIndex),
658template <
typename T,
bool IsL2>
659T computeHalfPlaneJacobian(
667 Ref<Eigen::MatrixX<T>> jacobian,
668 Ref<Eigen::VectorX<T>> residual,
670 MT_CHECK(data.param1 <
static_cast<size_t>(params.size()));
671 MT_CHECK(data.param2 <
static_cast<size_t>(params.size()));
673 if (!enabledParameters.test(data.param1) && !enabledParameters.test(data.param2)) {
676 const Eigen::Vector2<T> p(params(data.param1), params(data.param2));
677 const T res = p.dot(data.normal.template
cast<T>()) - data.offset;
681 const T sqrError = res * res;
684 if constexpr (IsL2) {
686 error = tWeight * limitWeight * sqrError;
688 wgtLoss = std::sqrt(tWeight * limitWeight * loss.deriv(sqrError));
689 error = tWeight * limitWeight * loss.value(sqrError);
691 residual(row) = res * wgtLoss;
692 if (enabledParameters.test(data.param1)) {
693 jacobian(row, data.param1) = T(data.normal[0]) * wgtLoss;
695 if (enabledParameters.test(data.param2)) {
696 jacobian(row, data.param2) = T(data.normal[1]) * wgtLoss;
701template <
typename T,
bool IsL2>
702T computeEllipsoidJacobian(
709 const Eigen::VectorX<bool>& activeJointParams,
712 Ref<Eigen::MatrixX<T>> jacobian,
713 Ref<Eigen::VectorX<T>> residual,
715 const Eigen::Vector3<T> position =
716 state.jointState[ct.parent].transform * ct.offset.template
cast<T>();
717 const Eigen::Vector3<T> localPosition =
718 state.jointState[ct.ellipsoidParent].transform.inverse() * position;
719 const Eigen::Vector3<T> ellipsoidPosition = ct.ellipsoidInv.template
cast<T>() * localPosition;
720 const Eigen::Vector3<T> normalizedPosition = ellipsoidPosition.normalized();
721 const Eigen::Vector3<T> projectedPosition = ct.ellipsoid.template
cast<T>() * normalizedPosition;
722 const Eigen::Vector3<T> diff =
723 position - state.jointState[ct.ellipsoidParent].transform * projectedPosition;
724 const T sqrError = diff.squaredNorm();
726 Eigen::Ref<Eigen::MatrixX<T>> jac = jacobian.block(row, 0, 3, params.size());
727 Eigen::Ref<Eigen::VectorX<T>> res = residual.middleRows(row, 3);
728 MT_CHECK(jac.cols() ==
static_cast<Eigen::Index
>(parameterTransform.transform.cols()));
734 if constexpr (IsL2) {
735 jwgt = std::sqrt(totalWeight * T(kPositionWeight) * limitWeight);
737 jwgt = std::sqrt(totalWeight * T(kPositionWeight) * limitWeight * loss.deriv(sqrError));
740 size_t jointIndex = ct.parent;
741 while (jointIndex != ct.ellipsoidParent && jointIndex !=
kInvalidIndex) {
742 MT_CHECK(jointIndex < skeleton.joints.size());
743 const auto& jointState = state.jointState[jointIndex];
745 const Eigen::Vector3<T> posd = position - jointState.translation();
747 for (
size_t d = 0; d < 3; d++) {
748 if (activeJointParams[paramIndex + d]) {
749 const Eigen::Vector3<T> jc = jointState.getTranslationDerivative(d) * jwgt;
750 for (
auto index = parameterTransform.transform.outerIndexPtr()[paramIndex + d];
751 index < parameterTransform.transform.outerIndexPtr()[paramIndex + d + 1];
753 jac.col(parameterTransform.transform.innerIndexPtr()[index]) +=
754 jc * parameterTransform.transform.valuePtr()[index];
757 if (activeJointParams[paramIndex + 3 + d]) {
758 const Eigen::Vector3<T> jc = jointState.getRotationDerivative(d, posd) * jwgt;
759 for (
auto index = parameterTransform.transform.outerIndexPtr()[paramIndex + 3 + d];
760 index < parameterTransform.transform.outerIndexPtr()[paramIndex + 3 + d + 1];
762 jac.col(parameterTransform.transform.innerIndexPtr()[index]) +=
763 jc * parameterTransform.transform.valuePtr()[index];
767 if (activeJointParams[paramIndex + 6]) {
768 const Eigen::Vector3<T> jc = jointState.getScaleDerivative(posd) * jwgt;
769 for (
auto index = parameterTransform.transform.outerIndexPtr()[paramIndex + 6];
770 index < parameterTransform.transform.outerIndexPtr()[paramIndex + 6 + 1];
772 jac.col(parameterTransform.transform.innerIndexPtr()[index]) +=
773 jc * parameterTransform.transform.valuePtr()[index];
776 jointIndex = skeleton.joints[jointIndex].parent;
780 if constexpr (IsL2) {
781 return totalWeight * T(kPositionWeight) * limitWeight * sqrError;
783 return totalWeight * T(kPositionWeight) * limitWeight * loss.value(sqrError);
805 character.parameterTransform,
806 character.parameterLimits,
816 :
LimitErrorFunctionT(character.skeleton, character.parameterTransform, pl, lossAlpha, lossC) {}
820[[nodiscard]]
double LimitErrorFunctionT<T>::getErrorImpl(
825 for (
const auto& limit : limits_) {
826 const T limitWeight = T(limit.weight);
827 switch (limit.type) {
829 error += computeMinMaxError<T, IsL2>(
830 limit.data.minMax, params, limitWeight, loss_, this->enabledParameters_);
833 error += computeMinMaxJointError<T, IsL2>(
834 limit.data.minMaxJoint, state, limitWeight, loss_, this->activeJointParams_);
839 error += computeLinearError<T, IsL2>(
840 limit.data.linear, params, limitWeight, loss_, this->enabledParameters_);
843 error += computeLinearJointError<T, IsL2>(
844 limit.data.linearJoint, state, limitWeight, loss_, this->activeJointParams_);
847 error += computeHalfPlaneError<T, IsL2>(
848 limit.data.halfPlane, params, limitWeight, loss_, this->enabledParameters_);
851 error += computeEllipsoidError<T, IsL2>(limit.data.ellipsoid, state, limitWeight, loss_);
855 MT_THROW(
"Unknown parameter type for joint limit");
862 if constexpr (IsL2) {
863 return error * kLimitWeight * this->weight_ * loss_.invC2();
865 return error * kLimitWeight * this->weight_;
880 return loss_.isL2() ? getErrorImpl<true>(params, state) : getErrorImpl<false>(params, state);
885[[nodiscard]]
double LimitErrorFunctionT<T>::getGradientImpl(
888 Ref<Eigen::VectorX<T>> gradient)
const {
889 const auto& parameterTransform = this->parameterTransform_;
894 T tWeight = kLimitWeight * this->weight_;
895 if constexpr (IsL2) {
896 tWeight *= loss_.invC2();
899 for (
const auto& limit : limits_) {
900 const T limitWeight = T(limit.weight);
901 switch (limit.type) {
903 error += computeMinMaxGradient<T, IsL2>(
909 this->enabledParameters_,
913 error += computeMinMaxJointGradient<T, IsL2>(
914 limit.data.minMaxJoint,
919 this->activeJointParams_,
926 error += computeLinearGradient<T, IsL2>(
932 this->enabledParameters_,
936 error += computeLinearJointGradient<T, IsL2>(
937 limit.data.linearJoint,
942 this->activeJointParams_,
947 error += computeHalfPlaneGradient<T, IsL2>(
948 limit.data.halfPlane,
953 this->enabledParameters_,
957 error += computeEllipsoidGradient<T, IsL2>(
958 limit.data.ellipsoid,
963 this->activeJointParams_,
970 MT_THROW(
"Unknown parameter type for joint limit");
983 Ref<Eigen::VectorX<T>> gradient) {
986 return loss_.isL2() ? getGradientImpl<true>(params, state, gradient)
987 : getGradientImpl<false>(params, state, gradient);
992double LimitErrorFunctionT<T>::getJacobianImpl(
995 Ref<Eigen::MatrixX<T>> jacobian,
996 Ref<Eigen::VectorX<T>> residual,
997 int& usedRows)
const {
998 const auto& parameterTransform = this->parameterTransform_;
1006 T tWeight = kLimitWeight * this->weight_;
1007 if constexpr (IsL2) {
1008 tWeight *= loss_.invC2();
1015 for (
const auto& limit : limits_) {
1016 const T limitWeight = T(limit.weight);
1018 if constexpr (IsL2) {
1019 wgt = std::sqrt(tWeight * limitWeight);
1021 switch (limit.type) {
1023 error += computeMinMaxJacobian<T, IsL2>(
1030 this->enabledParameters_,
1037 error += computeMinMaxJointJacobian<T, IsL2>(
1038 limit.data.minMaxJoint,
1044 this->activeJointParams_,
1054 error += computeLinearJacobian<T, IsL2>(
1061 this->enabledParameters_,
1068 error += computeLinearJointJacobian<T, IsL2>(
1069 limit.data.linearJoint,
1075 this->activeJointParams_,
1083 error += computeHalfPlaneJacobian<T, IsL2>(
1084 limit.data.halfPlane,
1090 this->enabledParameters_,
1097 error += computeEllipsoidJacobian<T, IsL2>(
1098 limit.data.ellipsoid,
1104 this->activeJointParams_,
1114 MT_THROW(
"Unknown parameter type for joint limit");
1123template <
typename T>
1128 Ref<Eigen::MatrixX<T>> jacobian,
1129 Ref<Eigen::VectorX<T>> residual,
1133 return loss_.isL2() ? getJacobianImpl<true>(params, state, jacobian, residual, usedRows)
1134 : getJacobianImpl<false>(params, state, jacobian, residual, usedRows);
1137template <
typename T>
1140 for (
const auto& limit : limits_) {
1141 switch (limit.type) {
1156 MT_THROW(
"Unknown parameter type for joint limit");
1163template <
typename T>
1168template <
typename T>
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Implementation of "A General and Adaptive Robust Loss Function" (https://arxiv.org/abs/1701....
Penalizes model/joint parameters that violate their configured limits.
double getJacobian(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< Eigen::MatrixX< T > > jacobian, Ref< Eigen::VectorX< T > > residual, int &usedRows) final
void setLimits(const ParameterLimits &lm)
LimitErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const ParameterLimits &pl=ParameterLimits(), const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
Constructor.
double getGradient(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< Eigen::VectorX< T > > gradient) final
double getError(const ModelParametersT< T > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
size_t getJacobianSize() const final
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
SkeletonT< float > Skeleton
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)
std::vector< ParameterLimit > ParameterLimits
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)
constexpr size_t kParametersPerJoint
bool isInRange(const LimitLinear &limit, float value)
If rangeMin and rangeMax are both 0, the limit applies to all values.
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
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.
ParameterLimits parameterLimits
Constraints on model parameters.
Constrains a point (offset in parent joint's frame) to lie on the surface of an ellipsoid defined in ...
Constraint: (p1, p2) ยท (normal) - offset >= 0.
Linear relationship between parameters: p_0 = s * p_1 - o.
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.