Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skeleton_error_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
15
16namespace momentum {
17
18template <typename T>
20 public:
22 : skeleton_(skel),
24 weight_(1.0),
25 activeJointParams_(pt.activeJointParams) {
26 enabledParameters_.flip(); // all parameters enabled by default
27 }
28 virtual ~SkeletonErrorFunctionT() = default;
29
30 [[nodiscard]] const Skeleton& getSkeleton() const {
31 return skeleton_;
32 }
33
34 [[nodiscard]] const ParameterTransform& getParameterTransform() const {
36 }
37
40 [[nodiscard]] virtual const Character* getCharacter() const {
41 return nullptr;
42 }
43
44 void setWeight(T w) {
45 weight_ = w;
46 }
47
48 [[nodiscard]] T getWeight() const {
49 return weight_;
50 }
51
54 }
55
58 }
59
60 virtual double getError(
61 const ModelParametersT<T>& /* params */,
62 const SkeletonStateT<T>& /* state */,
63 const MeshStateT<T>& /* meshState */) {
64 return 0.0f;
65 }
66
67 virtual double getGradient(
68 const ModelParametersT<T>& /* params */,
69 const SkeletonStateT<T>& /* state */,
70 const MeshStateT<T>& /* meshState */,
71 Eigen::Ref<Eigen::VectorX<T>> /* gradient */) {
72 return 0.0f;
73 }
74
75 virtual double getJacobian(
76 const ModelParametersT<T>& /* params */,
77 const SkeletonStateT<T>& /* state */,
78 const MeshStateT<T>& /* meshState */,
79 Eigen::Ref<Eigen::MatrixX<T>> /* jacobian */,
80 Eigen::Ref<Eigen::VectorX<T>> /* residual */,
81 int& usedRows) {
82 usedRows = 0;
83 return 0.0f;
84 }
85
86 virtual void getHessian(
87 const ModelParametersT<T>& /* params */,
88 const SkeletonStateT<T>& /* state */,
89 const MeshStateT<T>& /* meshState */,
90 Eigen::Ref<Eigen::MatrixX<T>> /* hessian */) {
91 MT_THROW("getHessian is not implemented for this error function");
92 }
93
94 virtual double getSolverDerivatives(
95 const ModelParametersT<T>& parameters,
96 const SkeletonStateT<T>& state,
97 const MeshStateT<T>& meshState,
98 const size_t actualParameters,
99 Eigen::MatrixX<T>& hess,
100 Eigen::VectorX<T>& grad) {
101 const int paramSize = static_cast<int>(parameters.size());
102 double error = 0.0;
103
104 if (useHessian_) {
105 // get some ice cream, you deserve it
106 error = getGradient(parameters, state, meshState, grad);
107 getHessian(parameters, state, meshState, hess);
108 } else {
109 const int residualSize = static_cast<int>(getJacobianSize());
110
111 Eigen::MatrixX<T> jacobian = Eigen::MatrixX<T>::Zero(residualSize, paramSize);
112 Eigen::VectorX<T> residual = Eigen::VectorX<T>::Zero(residualSize);
113 int rows = 0;
114 error = getJacobian(parameters, state, meshState, jacobian, residual, rows);
115
116 // Update JtJ
117 if (rows > 0) {
118 // ! In truth, on the the "actualParameters" leftmost block will be used
119 // We take advantage of this here and skip the other computations
120 const auto JtBlock2 =
121 (jacobian.topLeftCorner(rows, actualParameters).transpose() * 2.0).eval();
122
123 // Update Hessian (Hessian += 2.0 * J^T * J) using selfadjointView with rankUpdate,
124 // replacing triangularView
125 hess.template selfadjointView<Eigen::Lower>().rankUpdate(JtBlock2);
126
127 // Update JtR
128 grad.noalias() += JtBlock2 * residual.head(rows);
129 }
130 }
131
132 return error;
133 }
134
135 [[nodiscard]] virtual size_t getJacobianSize() const {
136 return 0;
137 }
138
139 [[nodiscard]] virtual bool needsMesh() const {
140 return false;
141 }
142
143 protected:
148 ParameterSet enabledParameters_; // set to zero by default constr
149 bool useHessian_{false}; // this can be true only if the getHessian is implemented
150};
151
152} // namespace momentum
virtual void getHessian(const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &, Eigen::Ref< Eigen::MatrixX< T > >)
void setActiveJoints(const VectorX< bool > &aj)
virtual double getSolverDerivatives(const ModelParametersT< T > &parameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, const size_t actualParameters, Eigen::MatrixX< T > &hess, Eigen::VectorX< T > &grad)
virtual double getJacobian(const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &, Eigen::Ref< Eigen::MatrixX< T > >, Eigen::Ref< Eigen::VectorX< T > >, int &usedRows)
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
virtual double getGradient(const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &, Eigen::Ref< Eigen::VectorX< T > >)
virtual double getError(const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &)
void setEnabledParameters(const ParameterSet &ps)
virtual const Character * getCharacter() const
For mesh-based error functions, get access to the underlying character.
virtual ~SkeletonErrorFunctionT()=default
const ParameterTransform & getParameterTransform() const
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
Definition exception.h:31
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
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
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto size() const
Definition types.h:87
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
Represents the complete state of a skeleton.