Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
sequence_solver_function.h
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
8#pragma once
9
17
18#include <atomic>
19
20namespace momentum {
21
22template <typename T>
27
28inline constexpr size_t kAllFrames = SIZE_MAX;
29
30template <typename T>
32 public:
34 const Character& character,
35 const ParameterTransformT<T>& parameterTransform,
36 const ParameterSet& universal,
37 size_t nFrames);
43
44 double getError(const Eigen::VectorX<T>& parameters) final;
45
46 double getGradient(const Eigen::VectorX<T>& parameters, Eigen::VectorX<T>& gradient) final;
47
48 // Block-wise Jacobian interface
49 void initializeJacobianComputation(const Eigen::VectorX<T>& parameters) override;
50 [[nodiscard]] size_t getJacobianBlockCount() const override;
51 [[nodiscard]] size_t getJacobianBlockSize(size_t blockIndex) const override;
53 const Eigen::VectorX<T>& parameters,
54 size_t blockIndex,
55 Eigen::Ref<Eigen::MatrixX<T>> jacobianBlock,
56 Eigen::Ref<Eigen::VectorX<T>> residualBlock,
57 size_t& actualRows) override;
59
60 void updateParameters(Eigen::VectorX<T>& parameters, const Eigen::VectorX<T>& gradient) final;
61 void setEnabledParameters(const ParameterSet& parameterSet) final;
62
63 [[nodiscard]] const ParameterSet& getUniversalParameterSet() const {
64 return universalParameters_;
65 }
66
68 [[nodiscard]] bool needsMeshPerFrame() const {
69 return needsMeshPerFrame_;
70 }
71
73 [[nodiscard]] bool needsMeshSequence() const {
74 return needsMeshSequence_;
75 }
76
77 // Passing in the special frame index kAllFrames will add the error function to every frame; this
78 // is convenient for e.g. limit errors but requires that the error function be stateless. Note:
79 // you are allowed to call this in a multithreaded context but you must ensure the frame indices
80 // are different between the different threads.
81 void addErrorFunction(size_t frame, std::shared_ptr<SkeletonErrorFunctionT<T>> errorFunction);
83 size_t startFrame,
84 std::shared_ptr<SequenceErrorFunctionT<T>> errorFunction);
85
86 [[nodiscard]] size_t getNumFrames() const {
87 return frameParameters_.size();
88 }
89
90 [[nodiscard]] const ModelParametersT<T>& getFrameParameters(size_t frame) const {
91 return frameParameters_[frame];
92 }
93
94 [[nodiscard]] const std::vector<ModelParametersT<T>>& getFrameParameters() const {
95 return frameParameters_;
96 }
97
98 [[nodiscard]] const std::vector<Eigen::Index>& getPerFrameParameterIndices() const {
99 return perFrameParameterIndices_;
100 }
101
102 [[nodiscard]] const std::vector<Eigen::Index>& getUniversalParameterIndices() const {
103 return universalParameterIndices_;
104 }
105
106 void setFrameParameters(size_t frame, const ModelParametersT<T>& parameters);
108 [[nodiscard]] Eigen::VectorX<T> getJoinedParameterVector() const;
109
116 [[nodiscard]] Eigen::VectorX<T> getJoinedParameterVectorFromFrameParameters(
117 std::span<const ModelParametersT<T>> frameParameters) const;
118
119 void setJoinedParameterVector(const Eigen::VectorX<T>& joinedParameters);
120
121 [[nodiscard]] const Skeleton* getSkeleton() const;
122 [[nodiscard]] const Character& getCharacter() const;
123
124 [[nodiscard]] const ParameterTransformT<T>* getParameterTransform() const {
125 return &parameterTransform_;
126 }
127
128 [[nodiscard]] const auto& getErrorFunctions(size_t iFrame) const {
129 return perFrameErrorFunctions_.at(iFrame);
130 }
131
132 [[nodiscard]] const auto& getErrorFunctions() const {
133 return perFrameErrorFunctions_;
134 }
135
136 [[nodiscard]] const auto& getSequenceErrorFunctions(size_t iFrame) const {
137 return sequenceErrorFunctions_.at(iFrame);
138 }
139
140 [[nodiscard]] const auto& getSequenceErrorFunctions() const {
141 return sequenceErrorFunctions_;
142 }
143
144 private:
145 void setFrameParametersFromJoinedParameterVector(const Eigen::VectorX<T>& parameters);
146
148 double
149 getPerFrameJacobian(Eigen::MatrixX<T>& jacobian, Eigen::VectorX<T>& residual, size_t& position);
150
152 double getSequenceErrorFunctionsJacobian(
153 Eigen::MatrixX<T>& jacobian,
154 Eigen::VectorX<T>& residual,
155 size_t& position);
156
157 private:
158 const Character& character_;
159 const ParameterTransformT<T>& parameterTransform_;
160 VectorX<bool> activeJointParams_;
161
162 std::vector<ModelParametersT<T>> frameParameters_;
163 size_t nFrames_;
164
165 void updateParameterSets(const ParameterSet& activeParams);
166
167 // Indices of parameters that are active and solved per-frame:
168 std::vector<Eigen::Index> perFrameParameterIndices_;
169
170 // Indices of parameters that are active and solved universally:
171 std::vector<Eigen::Index> universalParameterIndices_;
172
173 const ParameterSet universalParameters_;
174
175 std::vector<std::vector<std::shared_ptr<SkeletonErrorFunctionT<T>>>> perFrameErrorFunctions_;
176 std::vector<std::vector<std::shared_ptr<SequenceErrorFunctionT<T>>>> sequenceErrorFunctions_;
177
179 ResizeableMatrix<T> tempJac_;
180
181 std::atomic<size_t> numTotalPerFrameErrorFunctions_ = 0;
182 std::atomic<size_t> numTotalSequenceErrorFunctions_ = 0;
183
184 // Whether per-frame error functions need the mesh
185 std::atomic<bool> needsMeshPerFrame_{false};
186 // Whether sequence error functions need the mesh
187 std::atomic<bool> needsMeshSequence_{false};
188
189 friend class SequenceCholeskySolverT<T>;
190 friend class SequenceSolverT<T>;
191};
192
193} // namespace momentum
A dense column-major matrix whose underlying storage capacity grows monotonically and is reused acros...
Sequence solver based on banded normal equations and banded factorization.
const std::vector< Eigen::Index > & getUniversalParameterIndices() const
double computeJacobianBlock(const Eigen::VectorX< T > &parameters, size_t blockIndex, Eigen::Ref< Eigen::MatrixX< T > > jacobianBlock, Eigen::Ref< Eigen::VectorX< T > > residualBlock, size_t &actualRows) override
double getGradient(const Eigen::VectorX< T > &parameters, Eigen::VectorX< T > &gradient) final
const ModelParametersT< T > & getFrameParameters(size_t frame) const
const std::vector< Eigen::Index > & getPerFrameParameterIndices() const
const std::vector< ModelParametersT< T > > & getFrameParameters() const
void finalizeJacobianComputation() override
Optional cleanup after all Jacobian blocks have been computed.
const auto & getErrorFunctions(size_t iFrame) const
void setJoinedParameterVector(const Eigen::VectorX< T > &joinedParameters)
size_t getJacobianBlockCount() const override
Returns the number of Jacobian blocks available.
bool needsMeshSequence() const
Returns whether the sequence error functions need the mesh.
Eigen::VectorX< T > getJoinedParameterVectorFromFrameParameters(std::span< const ModelParametersT< T > > frameParameters) const
Returns a joined parameter vector from a span of frame parameters.
SequenceSolverFunctionT(const SequenceSolverFunctionT &)=delete
SequenceSolverFunctionT(SequenceSolverFunctionT &&)=delete
void setEnabledParameters(const ParameterSet &parameterSet) final
Specifies which parameters should be optimized.
const ParameterTransformT< T > * getParameterTransform() const
ModelParametersT< T > getUniversalParameters() const
size_t getJacobianBlockSize(size_t blockIndex) const override
Returns the maximum number of rows for a specific Jacobian block.
void setFrameParameters(size_t frame, const ModelParametersT< T > &parameters)
Eigen::VectorX< T > getJoinedParameterVector() const
SequenceSolverFunctionT & operator=(SequenceSolverFunctionT &&)=delete
const ParameterSet & getUniversalParameterSet() const
void addSequenceErrorFunction(size_t startFrame, std::shared_ptr< SequenceErrorFunctionT< T > > errorFunction)
SequenceSolverFunctionT(const Character &character, const ParameterTransformT< T > &parameterTransform, const ParameterSet &universal, size_t nFrames)
void initializeJacobianComputation(const Eigen::VectorX< T > &parameters) override
bool needsMeshPerFrame() const
Returns whether the per-frame error functions need the mesh.
void updateParameters(Eigen::VectorX< T > &parameters, const Eigen::VectorX< T > &gradient) final
SequenceSolverFunctionT & operator=(const SequenceSolverFunctionT &)=delete
const auto & getSequenceErrorFunctions(size_t iFrame) const
double getError(const Eigen::VectorX< T > &parameters) final
void addErrorFunction(size_t frame, std::shared_ptr< SkeletonErrorFunctionT< T > > errorFunction)
Abstract base class for optimization objective functions.
SkeletonT< float > Skeleton
Definition fwd.h:263
constexpr size_t kAllFrames
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
A vector of size numModelParams holding the values of all model parameters for a character.
Definition types.h:99
A parameter transform is an abstraction of the joint parameters that maps a model_parameter vector to...
Represents the complete state of a skeleton.