Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
union_error_function.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
11
12namespace momentum {
13
14template <typename T>
16 const Skeleton& skel,
17 const ParameterTransform& pt,
18 const std::vector<std::shared_ptr<SkeletonErrorFunctionT<T>>>& errorFunctions)
19 : SkeletonErrorFunctionT<T>(skel, pt), errorFunctions_(errorFunctions) {
20 // Should this be allowed?
21 MT_THROW_IF(errorFunctions_.empty(), "No error functions in union?");
22
23 for (size_t i = 0; i < errorFunctions_.size(); ++i) {
24 auto diff_e =
25 std::dynamic_pointer_cast<FullyDifferentiableSkeletonErrorFunctionT<T>>(errorFunctions_[i]);
26 diffErrorFunctions_.push_back(diff_e);
27
28 if (diff_e) {
29 for (const auto& input : diff_e->inputs()) {
30 inputs_[input].push_back(i);
31 }
32 }
33 }
34
35 {
36 std::vector<std::string> names;
37 for (const auto& e : diffErrorFunctions_) {
38 if (e) {
39 names.emplace_back(e->name());
40 } else {
41 names.emplace_back("Unknown");
42 }
43 }
44
45 std::ostringstream name;
46 name << "Union[";
47 if (std::adjacent_find(std::begin(names), std::end(names), std::not_equal_to<>()) ==
48 std::end(names)) {
49 // All the same:
50 name << names.front() << " x " << names.size();
51 } else {
52 bool first = true;
53 for (const auto& n : names) {
54 if (!first) {
55 name << ", ";
56 }
57 name << n;
58 first = false;
59 }
60 }
61 name << "]";
62 name_ = name.str();
63 }
64}
65
66template <typename T>
68 const ModelParametersT<T>& params,
69 const SkeletonStateT<T>& state,
70 const MeshStateT<T>& meshState) {
71 double error = 0;
72 for (auto& fun : errorFunctions_) {
73 error += fun->getError(params, state, meshState);
74 }
75 return this->weight_ * error;
76}
77
78template <typename T>
80 const ModelParametersT<T>& params,
81 const SkeletonStateT<T>& state,
82 const MeshStateT<T>& meshState,
83 Eigen::Ref<Eigen::VectorX<T>> gradient) {
84 double error = 0;
85 for (auto& fun : errorFunctions_) {
86 error += fun->getGradient(params, state, meshState, gradient);
87 }
88 gradient *= this->weight_;
89 return this->weight_ * error;
90}
91
92template <typename T>
94 const ModelParametersT<T>& params,
95 const SkeletonStateT<T>& state,
96 const MeshStateT<T>& meshState,
97 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
98 Eigen::Ref<Eigen::VectorX<T>> residual,
99 int& usedRows) {
100 double error = 0;
101 size_t jacobianStart = 0;
102 for (auto& fun : errorFunctions_) {
103 const size_t memberJacobianSize = fun->getJacobianSize();
104
105 error += fun->getJacobian(
106 params,
107 state,
108 meshState,
109 jacobian.block(jacobianStart, 0, memberJacobianSize, jacobian.cols()),
110 residual.segment(jacobianStart, memberJacobianSize),
111 usedRows);
112
113 jacobianStart += memberJacobianSize;
114 }
115
116 jacobian *= std::sqrt(this->weight_);
117 residual *= std::sqrt(this->weight_);
118
119 usedRows = static_cast<int>(jacobian.rows());
120
121 return this->weight_ * error;
122}
123
124template <typename T>
126 size_t result = 0;
127 for (const auto& e : errorFunctions_) {
128 result += e->getJacobianSize();
129 }
130 return result;
131}
132
133template <typename T>
134std::vector<std::string> UnionErrorFunctionT<T>::inputs() const {
135 std::vector<std::string> result;
136 result.reserve(inputs_.size());
137 for (const auto& i : inputs_) {
138 result.push_back(i.first);
139 }
140 return result;
141}
142
143template <typename T>
144Eigen::Index UnionErrorFunctionT<T>::getInputSize(const std::string& name) const {
145 auto itr = inputs_.find(name);
147 itr == inputs_.end(), "Invalid input '{}' for UnionErrorFunction::getInputSize", name);
148
149 Eigen::Index result = 0;
150 for (const auto& iErrFun : itr->second) {
151 const auto errorFun = diffErrorFunctions_[iErrFun];
152 result += errorFun->getInputSize(name);
153 }
154 return result;
155}
156
157template <typename T>
159 const std::string& name,
160 Eigen::Ref<Eigen::VectorX<T>> result) const {
161 auto itr = inputs_.find(name);
162 MT_THROW_IF(itr == inputs_.end(), "Invalid input '{}' for UnionErrorFunction::getInput", name);
163
164 Eigen::Index currentOffset = 0;
165 for (const auto& iErrFun : itr->second) {
166 const auto errorFun = diffErrorFunctions_[iErrFun];
167 const Eigen::Index sz = errorFun->getInputSize(name);
168 errorFun->getInput(name, result.segment(currentOffset, sz));
169 currentOffset += sz;
170 }
171}
172
173template <typename T>
175 const std::string& name,
176 Eigen::Ref<const Eigen::VectorX<T>> value) {
177 auto itr = inputs_.find(name);
178 MT_THROW_IF(itr == inputs_.end(), "Invalid input '{}' for UnionErrorFunction::setInput", name);
179
180 Eigen::Index currentOffset = 0;
181 for (const auto& iErrFun : itr->second) {
182 const auto errorFun = diffErrorFunctions_[iErrFun];
183 const Eigen::Index sz = errorFun->getInputSize(name);
184 errorFun->setInput(name, value.segment(currentOffset, sz));
185 currentOffset += sz;
186 }
187}
188
189template <typename T>
191 const std::string& name,
192 const ModelParametersT<T>& modelParams,
193 const SkeletonStateT<T>& state,
194 Eigen::Ref<const Eigen::VectorX<T>> inputVec) {
195 auto itr = inputs_.find(name);
197 itr == inputs_.end(),
198 "Invalid input '{}' for UnionErrorFunction::d_gradient_d_input_dot",
199 name);
200
201 Eigen::Index currentOffset = 0;
202 Eigen::VectorX<T> result = Eigen::VectorX<T>::Zero(getInputSize(name));
203
204 for (const auto& iErrFun : itr->second) {
205 const auto errFun = diffErrorFunctions_[iErrFun];
206 const Eigen::Index sz = errFun->getInputSize(name);
207 result.segment(currentOffset, sz) =
208 errFun->d_gradient_d_input_dot(name, modelParams, state, inputVec);
209 currentOffset += sz;
210 }
211
212 result *= this->weight_;
213
214 return result;
215}
216
217template class UnionErrorFunctionT<float>;
218template class UnionErrorFunctionT<double>;
219
220} // namespace momentum
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
void setInputImp(const std::string &name, Eigen::Ref< const Eigen::VectorX< T > > value) override
void getInputImp(const std::string &name, Eigen::Ref< Eigen::VectorX< T > > result) const override
UnionErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const std::vector< std::shared_ptr< SkeletonErrorFunctionT< T > > > &errorFunctions)
Eigen::VectorX< T > d_gradient_d_input_dot(const std::string &inputName, const ModelParametersT< T > &modelParams, const SkeletonStateT< T > &state, Eigen::Ref< const Eigen::VectorX< T > > inputVec) override
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
Eigen::Index getInputSize(const std::string &name) const override
double getJacobian(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
const char * name() const override
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
std::vector< std::string > inputs() const override
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
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.