Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
projection_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
12
13#include <cassert>
14
15namespace momentum {
16
17template <typename T>
23
24template <typename T>
26 const ModelParametersT<T>& /*parameters*/,
27 const SkeletonStateT<T>& skeletonState,
28 const MeshStateT<T>& /* meshState */) {
29 double error = 0;
30
31 const auto& jointState = skeletonState.jointState;
32
33 const auto nCons = constraints_.size();
34 for (size_t iCons = 0; iCons < nCons; ++iCons) {
35 const auto& cons = constraints_[iCons];
36 const auto& js = jointState[cons.parent];
37 const Eigen::Vector3<T> p_world_cm = js.transform * cons.offset;
38 const Eigen::Vector3<T> p_projected_cm =
39 constraints_[iCons].projection * p_world_cm.homogeneous();
40
41 // Behind camera:
42 if (p_projected_cm.z() < _nearClip) {
43 continue;
44 }
45
46 const Eigen::Vector2<T> diff = p_projected_cm.hnormalized().template head<2>() - cons.target;
47 error += cons.weight * kProjectionWeight * this->weight_ * diff.squaredNorm();
48 }
49
50 return error;
51}
52
53template <typename T>
57
58template <typename T>
60 const momentum::ModelParametersT<T>& /*params*/,
61 const momentum::SkeletonStateT<T>& skeletonState,
62 const momentum::MeshStateT<T>& /* meshState */,
63 Eigen::Ref<Eigen::VectorX<T>> gradient) {
64 double error = 0;
65
66 for (size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
67 const auto& cons = constraints_[iCons];
68
69 const auto& jsCons = skeletonState.jointState[cons.parent];
70 const Eigen::Vector3<T> p_world_cm = jsCons.transform * cons.offset;
71 const Eigen::Vector3<T> p_projected_cm = cons.projection * p_world_cm.homogeneous();
72
73 // Behind camera:
74 if (p_projected_cm.z() < _nearClip) {
75 continue;
76 }
77
78 const Eigen::Vector2<T> p_res = p_projected_cm.hnormalized() - cons.target;
79
80 error += cons.weight * kProjectionWeight * this->weight_ * p_res.squaredNorm();
81 const T wgt = 2.0f * cons.weight * kProjectionWeight * this->weight_;
82
83 const T z = p_projected_cm(2);
84 const T z_sqr = sqr(z);
85 const T x_zz = p_projected_cm(0) / z_sqr;
86 const T y_zz = p_projected_cm(1) / z_sqr;
87
88 auto addGradient = [&](const size_t jFullBodyDOF, const Eigen::Vector3<T>& d_p_world_cm) {
89 const Eigen::Vector3<T> d_p_projected =
90 cons.projection.template topLeftCorner<3, 3>() * d_p_world_cm;
91 const T dx = d_p_projected(0);
92 const T dy = d_p_projected(1);
93 const T dz = d_p_projected(2);
94
95 const Eigen::Vector2<T> d_p_res(dx / z - x_zz * dz, dy / z - y_zz * dz);
96 const T gradFull = d_p_res.dot(p_res);
97
98 // multiply by the parameter transform:
99 for (momentum::SparseRowMatrixf::InnerIterator it(
100 this->parameterTransform_.transform, (int)jFullBodyDOF);
101 it;
102 ++it) {
103 assert(static_cast<size_t>(it.row()) == jFullBodyDOF);
104 const auto jReducedDOF = it.col();
105 gradient(jReducedDOF) += it.value() * wgt * gradFull;
106 }
107 };
108
109 // loop over all joints the constraint is attached to and calculate gradient
110 size_t jointIndex = cons.parent;
111 while (jointIndex != kInvalidIndex) {
112 // check for valid index
113 assert(jointIndex < this->skeleton_.joints.size());
114
115 const auto& jointState = skeletonState.jointState[jointIndex];
116 const size_t paramIndex = jointIndex * 7;
117 const Eigen::Vector3<T> posd_cm = p_world_cm - jointState.translation();
118
119 // calculate derivatives based on active joints
120 for (size_t d = 0; d < 3; d++) {
121 if (this->activeJointParams_[paramIndex + d]) {
122 addGradient(paramIndex + d, jointState.getTranslationDerivative(d));
123 }
124 if (this->activeJointParams_[paramIndex + 3 + d]) {
125 addGradient(paramIndex + 3 + d, jointState.getRotationDerivative(d, posd_cm));
126 }
127 }
128 if (this->activeJointParams_[paramIndex + 6]) {
129 addGradient(paramIndex + 6, jointState.getScaleDerivative(posd_cm));
130 }
131
132 // go to the next joint
133 jointIndex = this->skeleton_.joints[jointIndex].parent;
134 }
135 }
136
137 return error;
138}
139
140template <typename T>
142 const ModelParametersT<T>& /*parameters*/,
143 const SkeletonStateT<T>& skeletonState,
144 const MeshStateT<T>& /* meshState */,
145 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
146 Eigen::Ref<Eigen::VectorX<T>> residual,
147 int& usedRows) {
148 double error = 0;
149
150 for (size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
151 const auto& cons = constraints_[iCons];
152
153 const auto& jsCons = skeletonState.jointState[cons.parent];
154 const Eigen::Vector3<T> p_world_cm = jsCons.transform * cons.offset;
155 const Eigen::Vector3<T> p_projected_cm = cons.projection * p_world_cm.homogeneous();
156
157 // Behind camera:
158 if (p_projected_cm.z() < _nearClip) {
159 continue;
160 }
161
162 const Eigen::Vector2<T> p_res = p_projected_cm.hnormalized() - cons.target;
163 error += cons.weight * kProjectionWeight * this->weight_ * p_res.squaredNorm();
164
165 const T z = p_projected_cm(2);
166 const T z_sqr = sqr(z);
167 const T x_zz = p_projected_cm(0) / z_sqr;
168 const T y_zz = p_projected_cm(1) / z_sqr;
169
170 const T wgt = std::sqrt(cons.weight * kProjectionWeight * this->weight_);
171 residual.template segment<2>(2 * iCons) = (wgt * p_res).template cast<T>();
172
173 auto addJacobian = [&](const size_t jFullBodyDOF, const Eigen::Vector3<T>& d_p_world_cm) {
174 const Eigen::Vector3<T> d_p_projected =
175 cons.projection.template topLeftCorner<3, 3>() * d_p_world_cm;
176 const T dx = d_p_projected(0);
177 const T dy = d_p_projected(1);
178 const T dz = d_p_projected(2);
179
180 const Eigen::Vector2<T> d_p_res(dx / z - x_zz * dz, dy / z - y_zz * dz);
181
182 // multiply by the parameter transform:
183 for (momentum::SparseRowMatrixf::InnerIterator it(
184 this->parameterTransform_.transform, (int)jFullBodyDOF);
185 it;
186 ++it) {
187 assert(static_cast<size_t>(it.row()) == jFullBodyDOF);
188 const auto jReducedDOF = it.col();
189 jacobian.template block<2, 1>(2 * iCons, jReducedDOF) += it.value() * wgt * d_p_res;
190 }
191 };
192
193 // loop over all joints the constraint is attached to and calculate gradient
194 size_t jointIndex = cons.parent;
195 while (jointIndex != kInvalidIndex) {
196 // check for valid index
197 assert(jointIndex < this->skeleton_.joints.size());
198
199 const auto& jointState = skeletonState.jointState[jointIndex];
200 const size_t paramIndex = jointIndex * 7;
201 const Eigen::Vector3<T> posd_cm = p_world_cm - jointState.translation();
202
203 // calculate derivatives based on active joints
204 for (size_t d = 0; d < 3; d++) {
205 if (this->activeJointParams_[paramIndex + d]) {
206 addJacobian(paramIndex + d, jointState.getTranslationDerivative(d));
207 }
208 if (this->activeJointParams_[paramIndex + 3 + d]) {
209 addJacobian(paramIndex + 3 + d, jointState.getRotationDerivative(d, posd_cm));
210 }
211 }
212 if (this->activeJointParams_[paramIndex + 6]) {
213 addJacobian(paramIndex + 6, jointState.getScaleDerivative(posd_cm));
214 }
215
216 // go to the next joint
217 jointIndex = this->skeleton_.joints[jointIndex].parent;
218 }
219 }
220
221 usedRows = static_cast<int>(jacobian.rows());
222
223 return error;
224}
225
226template <typename T>
228 return 2 * constraints_.size();
229}
230
233
234} // namespace momentum
ProjectionErrorFunctionT(const momentum::Skeleton &skel, const momentum::ParameterTransform &pt, T nearClip=T(1))
std::vector< ProjectionConstraintDataT< T > > constraints_
double getError(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState) final
double getJacobian(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
double getGradient(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
void addConstraint(const ProjectionConstraintDataT< T > &data)
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
const ParameterTransform & parameterTransform_
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
T sqr(const T &x)
Computes the square of a value.
Definition utility.h:47
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Definition types.h:466
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.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.