Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
joint_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
16#include <momentum/math/types.h>
17
18#include <optional>
19#include <span>
20
21namespace momentum {
22
44template <
45 typename T, // float or double
46 class Data, // derived types from ConstraintData
47 size_t FuncDim = 3, // dimension of f
48 size_t NumVec =
49 1, // how many 3-vector v in one constraint, eg. a point is 1, and a rotation matrix is 3
50 size_t NumPos =
51 1> // we assume a constraint can be a function of both points and axes, and points come
52 // before axes in the NumVec of "v"s. This specifies how many "v"s are points. For
53 // example, it's 1 for a point constraint, and 0 for a rotation matrix.
55 public:
56 static constexpr size_t kFuncDim = FuncDim;
57 static constexpr size_t kNumVec = NumVec;
58 static constexpr size_t kNumPos = NumPos;
59
60 using FuncType = Vector<T, FuncDim>; // vector type for f
61 using VType = Vector3<T>; // vector type for v
62 using DfdvType = Eigen::Matrix<T, FuncDim, 3>; // type for dfdv - it's effectively a vector if f
63 // is a scalar (FuncDim=1, eg. PlaneErrorFunction)
64
71 explicit JointErrorFunctionT(
72 const Skeleton& skel,
73 const ParameterTransform& pt,
74 const T& lossAlpha = GeneralizedLossT<T>::kL2,
75 const T& lossC = T(1));
76
84 const Character& character,
85 const T& lossAlpha = GeneralizedLossT<T>::kL2,
86 const T& lossC = T(1))
87 : JointErrorFunctionT<T, Data, FuncDim, NumVec, NumPos>(
88 character.skeleton,
89 character.parameterTransform,
90 lossAlpha,
91 lossC) {}
92
93 // The functions below should just work for most constraints. When we have new constraints that
94 // don't fit these implementations, we can remove the "final" annotation to allow override.
95
104 [[nodiscard]] double getError(
105 const ModelParametersT<T>& params,
106 const SkeletonStateT<T>& state,
107 const MeshStateT<T>& meshState) final;
108
118 double getGradient(
119 const ModelParametersT<T>& params,
120 const SkeletonStateT<T>& state,
121 const MeshStateT<T>& meshState,
122 Ref<VectorX<T>> gradient) final;
123
138 double getJacobian(
139 const ModelParametersT<T>& params,
140 const SkeletonStateT<T>& state,
141 const MeshStateT<T>& meshState,
142 Ref<MatrixX<T>> jacobian,
143 Ref<VectorX<T>> residual,
144 int& usedRows) final;
145
150 [[nodiscard]] size_t getJacobianSize() const final;
151
155 void addConstraint(const Data& constr) {
156 constraints_.push_back(constr);
157 }
158
162 void addConstraints(std::span<const Data> constrs) {
163 constraints_.insert(constraints_.end(), constrs.begin(), constrs.end());
164 }
165
169 void setConstraints(std::span<const Data> constrs) {
170 constraints_.assign(constrs.begin(), constrs.end());
171 }
172
174 [[nodiscard]] const std::vector<Data>& getConstraints() const {
175 return constraints_;
176 }
177
178 [[nodiscard]] size_t getNumConstraints() const {
179 return constraints_.size();
180 }
181
184 constraints_.clear();
185 }
186
191 Data& getConstraint(size_t index) {
192 return constraints_.at(index);
193 }
194
195 protected:
197 std::vector<Data> constraints_;
203
215 virtual void evalFunction(
216 size_t constrIndex,
217 const JointStateT<T>& state,
218 FuncType& f,
219 std::array<VType, NumVec>& v,
220 std::array<DfdvType, NumVec>& dfdv) const = 0;
221};
222
223} // namespace momentum
224
Implementation of "A General and Adaptive Robust Loss Function" (https://arxiv.org/abs/1701....
static constexpr T kL2
Special-case alpha values that are either at a singularity in the general formula or simplify to a cl...
virtual void evalFunction(size_t constrIndex, const JointStateT< T > &state, FuncType &f, std::array< VType, NumVec > &v, std::array< DfdvType, NumVec > &dfdv) const =0
Evaluate the constraint residual and derivatives for a single constraint.
JointErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
Constructor.
size_t getJacobianSize() const final
The number of rows in the jacobian is the dimension of f, FuncDim, times the number of constraints.
static constexpr size_t kNumPos
static constexpr size_t kNumVec
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< VectorX< T > > gradient) final
The gradient of the error function: dl/dq = dl/d[f^2] * 2f * df/dv * dv/dq.
std::vector< Data > constraints_
List of constraints.
void addConstraints(std::span< const Data > constrs)
Appends a list of constraints.
Eigen::Matrix< T, FuncDim, 3 > DfdvType
const GeneralizedLossT< T > loss_
The generalized loss function that transforms f^2.
const std::vector< Data > & getConstraints() const
Data & getConstraint(size_t index)
Mutable access to a constraint by index, for in-place updates.
static constexpr size_t kFuncDim
JointErrorFunctionT(const Character &character, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
A convenience constructor where character contains info of the skeleton and parameter transform.
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
Computes the error function value l = w * loss(f^2).
void addConstraint(const Data &constr)
Adds a constraint to the list.
void clearConstraints()
Clear the current list of constraints.
VectorX< T > jointGrad_
Intermediate storage of the gradient from this error function.
double getJacobian(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Ref< MatrixX< T > > jacobian, Ref< VectorX< T > > residual, int &usedRows) final
For least-square problems, we assume l is the square of a vector function F.
void setConstraints(std::span< const Data > constrs)
Replace the current list of constraints with the input.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Shared types used across error function hierarchies.
SkeletonT< float > Skeleton
Definition fwd.h:263
Eigen::Ref< T > Ref
Definition types.h:43
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Eigen::Matrix< T, Dynamic, Dynamic > MatrixX
Dynamic-size, column-major dense matrix.
Definition types.h:80
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
Eigen::Matrix< T, Dim, 1, Options > Vector
Column vector alias (an Nx1 Eigen matrix).
Definition types.h:203
Vector< T, 3 > Vector3
Definition types.h:236
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
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.