Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
multipose_solver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
17
18namespace momentum {
19
20template <typename T>
22 const SolverOptions& options,
24 : SolverT<T>(options, function), meshState_(std::make_unique<MeshStateT<T>>()) {
25 // Set default values from MultiposeSolverOptions
26 regularization_ = MultiposeSolverOptions().regularization;
27
28 // Update values based on provided options
29 setOptions(options);
30}
31
32template <typename T>
34
35template <typename T>
36std::string_view MultiposeSolverT<T>::getName() const {
37 return "MultiposeSolver";
38}
39
40template <typename T>
43
44 if (const auto* derivedOptions = dynamic_cast<const MultiposeSolverOptions*>(&options)) {
45 regularization_ = derivedOptions->regularization;
46 }
47}
48
49template <typename T>
51
52template <typename T>
55
56 auto* fn = dynamic_cast<MultiposeSolverFunctionT<T>*>(this->solverFunction_);
57
58 const std::vector<Eigen::Index> genericParameters(
59 fn->genericParameters_.begin(), fn->genericParameters_.end());
60 const std::vector<Eigen::Index> universalParameters(
61 fn->universalParameters_.begin(), fn->universalParameters_.end());
62
63 fn->setFrameParametersFromJoinedParameterVector(this->parameters_);
64
65 OnlineBlockHouseholderQR<T> qrSolver(fn->universalParameters_.size(), std::sqrt(regularization_));
66 const auto nFullParameters = fn->parameterTransform_->numAllModelParameters();
67
68 this->error_ = 0;
69 for (size_t f = 0; f < fn->getNumFrames(); ++f) {
70 const auto& frameParameters = fn->frameParameters_[f];
71 auto& skelState = fn->states_[f];
72
73 skelState.set(fn->parameterTransform_->apply(frameParameters), *fn->skeleton_);
74
75 // TODO: Update mesh state for functions that need it once we have character access
76 // meshState_->update(frameParameters, skelState, character);
77
78 for (auto&& solvable : fn->errorFunctions_[f]) {
79 if (solvable->getWeight() <= 0.0f) {
80 continue;
81 }
82
83 const size_t n = solvable->getJacobianSize();
84 const size_t jacobianSize = padToSimdAlignment(n);
85
86 jacobianBlock_.resize(jacobianSize, nFullParameters);
87 jacobianBlock_.setZero();
88
89 residualBlock_.resize(jacobianSize);
90 residualBlock_.setZero();
91 int rows = 0;
92 this->error_ += solvable->getJacobian(
93 frameParameters, skelState, *meshState_, jacobianBlock_, residualBlock_, rows);
94
95 qrSolver.addMutating(
96 f,
97 ColumnIndexedMatrix<Eigen::MatrixX<T>>(jacobianBlock_, genericParameters),
98 ColumnIndexedMatrix<Eigen::MatrixX<T>>(jacobianBlock_, universalParameters),
99 residualBlock_);
100 }
101 }
102
103 // Now, extract the delta:
104 // TODO line search?
105 const Eigen::VectorX<T> delta = qrSolver.x_dense();
106 this->solverFunction_->updateParameters(this->parameters_, delta);
107}
108
109template class MultiposeSolverT<float>;
110template class MultiposeSolverT<double>;
111
112} // namespace momentum
View over a matrix that exposes a (possibly reordered) subset of its columns.
void initializeSolver() final
Initializes solver state before optimization begins.
std::string_view getName() const override
Returns the name of the solver implementation.
void doIteration() final
Performs a single iteration of the optimization algorithm.
MultiposeSolverT(const SolverOptions &options, MultiposeSolverFunctionT< T > *function)
void setOptions(const SolverOptions &options) final
Updates solver configuration with new options.
Online Householder QR solver for least-squares problems with block-arrowhead structure.
VectorType x_dense() const
Retrieves the full dense result [x_0; x_1; ...; x_{B-1}; x_n].
void addMutating(size_t iBlock, Eigen::Ref< MatrixType > A_diag, Eigen::Ref< MatrixType > A_common, Eigen::Ref< VectorType > b)
In-place addition; convenience wrapper that wraps the matrices in ColumnIndexedMatrix views without r...
virtual void setOptions(const SolverOptions &options)
Updates solver configuration with new options.
Definition solver.cpp:32
Eigen::VectorX< T > parameters_
Current parameter values.
Definition solver.h:109
double error_
Current objective function value.
Definition solver.h:126
SolverFunctionT< T > * solverFunction_
Function to be optimized.
Definition solver.h:106
SolverT(const SolverOptions &options, SolverFunctionT< T > *solver)
Constructor with solver options and function to optimize.
Definition solver.cpp:20
constexpr size_t padToSimdAlignment(size_t size)
Pads a row count to be a multiple of kJacobianRowAlignment for SIMD alignment.
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
Definition profile.h:110
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
Multipose solver specific options.
float regularization
Regularization parameter for QR decomposition.
Common options for numerical optimization solvers.
Definition solver.h:19