58 const auto parameterTransform = sf->getParameterTransform();
62 skeletonState_.set(parameterTransform->apply(this->parameters_), *skeleton);
65 if (sf->needsMeshState()) {
67 meshState_.update(this->
parameters_, skeletonState_, sf->getCharacter());
70 const auto nFullParams = gsl::narrow_cast<Eigen::Index>(this->
parameters_.size());
72 std::vector<Eigen::Index> enabledParameters;
73 enabledParameters.reserve(nFullParams);
74 for (Eigen::Index iFullParam = 0; iFullParam < nFullParams; ++iFullParam) {
76 enabledParameters.push_back(iFullParam);
80 const auto nSubsetParams = gsl::narrow_cast<Eigen::Index>(enabledParameters.size());
86 this->qrSolver_.reset(nSubsetParams, lambda);
89 double error_orig = 0.0;
90 for (
const auto& errorFunction : sf->getErrorFunctions()) {
91 if (errorFunction->getWeight() <= 0) {
97 jacobian_.resizeAndSetZero(rows, nFullParams);
98 residual_.resizeAndSetZero(rows);
101 error_orig += errorFunction->getJacobian(
102 this->
parameters_, skeletonState_, meshState_, jacobian_.mat(), residual_.mat(), usedRows);
107 this->qrSolver_.addMutating(
109 jacobian_.mat().topRows(usedRows), enabledParameters),
110 residual_.mat().topRows(usedRows));
113 this->
error_ = error_orig;
117 this->gradientSub_ = T(2) * qrSolver_.At_times_b();
120 this->Rmatrix_ = this->qrSolver_.R();
131 auto evalQuadraticModel = [&](Eigen::Ref<const Eigen::VectorX<T>> p) -> T {
133 result -= this->gradientSub_.dot(p);
136 result += (this->Rmatrix_.template triangularView<Eigen::Upper>() * p).squaredNorm();
140 auto subsetToFullVector = [&](Eigen::Ref<const Eigen::VectorX<T>> v_subset) -> Eigen::VectorX<T> {
141 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(nFullParams);
142 for (Eigen::Index iSubsetParam = 0; iSubsetParam < nSubsetParams; ++iSubsetParam) {
143 const Eigen::Index iFullParam = enabledParameters[iSubsetParam];
144 result[iFullParam] = v_subset[iSubsetParam];
152 for (
size_t iTrustStep = 0; iTrustStep < 10; ++iTrustStep) {
153 Eigen::VectorX<T> searchDir_sub = this->qrSolver_.result();
159 if (searchDir_sub.dot(gradientSub_) < FLT_EPSILON * (T(1.0) + this->
error_)) {
175 for (
size_t iIter = 0; iIter < 3; ++iIter) {
176 if (searchDir_sub.norm() < T(1.05) * this->curTrustRegionRadius_) {
180 const Eigen::MatrixX<T>& Rmat = this->qrSolver_.R();
186 Eigen::VectorX<T> p_l = Rmat.template triangularView<Eigen::Upper>().solve(
187 Rmat.template triangularView<Eigen::Upper>().transpose().solve(-T(0.5) * gradientSub_));
188 Eigen::VectorX<T> q_l = Rmat.template triangularView<Eigen::Upper>().transpose().solve(p_l);
190 const T p_l_norm2 = p_l.squaredNorm();
191 const T q_l_norm2 = q_l.squaredNorm();
193 if (q_l_norm2 < FLT_EPSILON) {
197 const T p_l_norm = std::sqrt(p_l_norm2);
198 const T deltaLambda = (p_l_norm2 / q_l_norm2) *
199 ((p_l_norm - this->curTrustRegionRadius_) / (this->curTrustRegionRadius_));
202 if (deltaLambda <= 0) {
210 const T lambdaNew = lambda + deltaLambda;
211 const T y = std::sqrt(lambdaNew - lambda);
215 this->lambdaDiag_.resize(nSubsetParams, nSubsetParams);
216 this->lambdaDiag_.setZero();
217 this->lambdaDiag_.diagonal().setConstant(y);
219 this->lambdaZero_.resize(nSubsetParams);
220 this->lambdaZero_.setZero();
222 this->qrSolver_.addMutating(this->lambdaDiag_, this->lambdaZero_);
227 searchDir_sub = this->qrSolver_.result();
231 " Iter: {}; Search dir cur norm: {}; delta: {}; lambda: {}",
233 searchDir_sub.norm(),
234 this->curTrustRegionRadius_,
238 const Eigen::VectorX<T> parameters_orig = this->
parameters_;
244 const T quadraticModelEval = evalQuadraticModel(searchDir_sub);
245 const T rho = (this->
error_ - error_new) / (this->
error_ - quadraticModelEval);
248 "Error orig: {}; error new: {}; quadratic model: {}; rho: {}",
256 this->curTrustRegionRadius_ = T(0.25) * this->curTrustRegionRadius_;
257 }
else if (rho > T(0.75) && lambda > 0) {
259 this->curTrustRegionRadius_ =
260 std::min(T(2) * this->curTrustRegionRadius_, this->maxTrustRegionRadius_);