Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
distance_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>
22
23template <typename T>
25 const ModelParametersT<T>& /*parameters*/,
26 const SkeletonStateT<T>& skeletonState,
27 const MeshStateT<T>& /* meshState */) {
28 double error = 0;
29
30 const auto& jointState = skeletonState.jointState;
31
32 for (const auto& cons : constraints_) {
33 const auto& js = jointState[cons.parent];
34 const Eigen::Vector3<T> p_world_cm = js.transform * cons.offset;
35 const Eigen::Vector3<T> diff_vec = p_world_cm - cons.origin;
36
37 const T distance = diff_vec.norm();
38 error += cons.weight * kDistanceWeight * this->weight_ * sqr(distance - cons.target);
39 }
40
41 return error;
42}
43
44template <typename T>
46 const ModelParametersT<T>& /*params*/,
47 const SkeletonStateT<T>& skeletonState,
48 const MeshStateT<T>& /* meshState */,
49 Eigen::Ref<Eigen::VectorX<T>> gradient) {
50 double error = 0;
51
52 for (size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
53 const auto& cons = constraints_[iCons];
54
55 const auto& jsCons = skeletonState.jointState[cons.parent];
56 const Eigen::Vector3<T> p_world_cm = jsCons.transform * cons.offset;
57 const Eigen::Vector3<T> diff_vec = p_world_cm - cons.origin;
58
59 const T distance = diff_vec.norm();
60 if (distance == 0) {
61 // Gradients are undefined in this case, skip to avoid NaNs in gradient.
62 continue;
63 }
64
65 const Eigen::Vector3<T> diff_distance_diff_p_world = diff_vec / distance;
66
67 const T diff = distance - cons.target;
68 error += cons.weight * kDistanceWeight * this->weight_ * sqr(diff);
69 const T wgt = T(2) * cons.weight * kDistanceWeight * this->weight_;
70
71 auto addGradient = [&](const size_t jFullBodyDOF, const Eigen::Vector3<T>& diff_p_world_cm) {
72 // d(distance)/dTheta = d(distance)/d(p_world_cm) * d(p_world_cm)/dTheta
73 // distance is defined as ||p_world_cm - cons.origin||, so
74 // d(distance)/d(p_world_cm) =
75 // (p_world_cm - cons.origin) / ||p_world_cm - cons.origin|| . p_world_cm
76 const T diff_p_distance = diff_distance_diff_p_world.dot(diff_p_world_cm);
77 const auto gradFull = diff_p_distance * diff;
78
79 // multiply by the parameter transform:
80 for (momentum::SparseRowMatrixf::InnerIterator it(
81 this->parameterTransform_.transform, jFullBodyDOF);
82 it;
83 ++it) {
84 assert(it.row() == (Eigen::Index)jFullBodyDOF);
85 const auto jReducedDOF = it.col();
86 gradient(jReducedDOF) += it.value() * wgt * gradFull;
87 }
88 };
89
90 // loop over all joints the constraint is attached to and calculate gradient
91 size_t jointIndex = cons.parent;
92 while (jointIndex != kInvalidIndex) {
93 // check for valid index
94 assert(jointIndex < this->skeleton_.joints.size());
95
96 const auto& jointState = skeletonState.jointState[jointIndex];
97 const size_t paramIndex = jointIndex * 7;
98 const Eigen::Vector3<T> posd_cm = p_world_cm - jointState.translation();
99
100 // calculate derivatives based on active joints
101 for (size_t d = 0; d < 3; d++) {
102 if (this->activeJointParams_[paramIndex + d]) {
103 addGradient(paramIndex + d, jointState.getTranslationDerivative(d));
104 }
105 if (this->activeJointParams_[paramIndex + 3 + d]) {
106 addGradient(paramIndex + 3 + d, jointState.getRotationDerivative(d, posd_cm));
107 }
108 }
109 if (this->activeJointParams_[paramIndex + 6]) {
110 addGradient(paramIndex + 6, jointState.getScaleDerivative(posd_cm));
111 }
112
113 // go to the next joint
114 jointIndex = this->skeleton_.joints[jointIndex].parent;
115 }
116 }
117
118 return error;
119}
120
121template <typename T>
123 const ModelParametersT<T>& /*parameters*/,
124 const SkeletonStateT<T>& skeletonState,
125 const MeshStateT<T>& /* meshState */,
126 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
127 Eigen::Ref<Eigen::VectorX<T>> residual,
128 int& usedRows) {
129 double error = 0;
130
131 for (size_t iCons = 0; iCons < constraints_.size(); ++iCons) {
132 const auto& cons = constraints_[iCons];
133
134 const auto& jsCons = skeletonState.jointState[cons.parent];
135 const Eigen::Vector3<T> p_world_cm = jsCons.transform * cons.offset;
136 const Eigen::Vector3<T> diff_vec = p_world_cm - cons.origin;
137
138 const T distance = diff_vec.norm();
139 if (distance == 0) {
140 // Gradients are undefined in this case, skip to avoid NaNs in gradient.
141 continue;
142 }
143
144 const Eigen::Vector3<T> diff_vec_normalized = diff_vec / distance;
145
146 const T diff = distance - cons.target;
147 error += cons.weight * kDistanceWeight * this->weight_ * sqr(diff);
148 const T wgt = std::sqrt(cons.weight * kDistanceWeight * this->weight_);
149 residual(iCons) = wgt * diff;
150
151 auto addJacobian = [&](const size_t jFullBodyDOF, const Eigen::Vector3<T>& diff_p_world_cm) {
152 const T diff_p_distance = diff_vec_normalized.dot(diff_p_world_cm);
153
154 // multiply by the parameter transform:
155 for (momentum::SparseRowMatrixf::InnerIterator it(
156 this->parameterTransform_.transform, (int)jFullBodyDOF);
157 it;
158 ++it) {
159 assert(it.row() == (Eigen::Index)jFullBodyDOF);
160 const auto jReducedDOF = it.col();
161 jacobian(iCons, jReducedDOF) += it.value() * wgt * diff_p_distance;
162 }
163 };
164
165 // loop over all joints the constraint is attached to and calculate gradient
166 size_t jointIndex = cons.parent;
167 while (jointIndex != kInvalidIndex) {
168 // check for valid index
169 assert(jointIndex < this->skeleton_.joints.size());
170
171 const auto& jointState = skeletonState.jointState[jointIndex];
172 const size_t paramIndex = jointIndex * 7;
173 const Eigen::Vector3<T> posd_cm = p_world_cm - jointState.translation();
174
175 // calculate derivatives based on active joints
176 for (size_t d = 0; d < 3; d++) {
177 if (this->activeJointParams_[paramIndex + d]) {
178 addJacobian(paramIndex + d, jointState.getTranslationDerivative(d));
179 }
180 if (this->activeJointParams_[paramIndex + 3 + d]) {
181 addJacobian(paramIndex + 3 + d, jointState.getRotationDerivative(d, posd_cm));
182 }
183 }
184 if (this->activeJointParams_[paramIndex + 6]) {
185 addJacobian(paramIndex + 6, jointState.getScaleDerivative(posd_cm));
186 }
187
188 // go to the next joint
189 jointIndex = this->skeleton_.joints[jointIndex].parent;
190 }
191 }
192
193 usedRows = static_cast<int>(jacobian.rows());
194
195 return error;
196}
197
198template <typename T>
200 return constraints_.size();
201}
202
203template <typename T>
205 const momentum::Locator& locator) {
207 result.parent = locator.parent;
208 result.offset = locator.offset.template cast<T>();
209 result.weight = 1;
210 result.target = 0;
211 return result;
212}
213
214template class DistanceErrorFunctionT<float>;
215template class DistanceErrorFunctionT<double>;
216
217template struct DistanceConstraintDataT<float>;
218template struct DistanceConstraintDataT<double>;
219
220} // namespace momentum
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 getError(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState) final
std::vector< DistanceConstraintDataT< T > > constraints_
DistanceErrorFunctionT(const momentum::Skeleton &skel, const momentum::ParameterTransform &pt)
double getGradient(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
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
static DistanceConstraintDataT< T > createFromLocator(const momentum::Locator &locator)
Represents a point attached to a joint in a skeleton.
Definition locator.h:19
size_t parent
Index of the parent joint in the skeleton.
Definition locator.h:23
Vector3f offset
Position in the parent joint's local coordinate frame.
Definition locator.h:26
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.