|
Momentum
A library for solving nonlinear optimization problems related to human body kinematics
|
The JointErrorFunction is a base class for constraint errors of the form l = w * loss(f^2), where w is the weight (could be a product of different weighting terms), loss() is the generalized loss function (see math/generalized_loss.h), and f is a difference vector we want to minimize. More...
#include <joint_error_function.h>
Public Types | |
| using | FuncType = Vector<T, FuncDim> |
| using | VType = Vector3<T> |
| using | DfdvType = Eigen::Matrix<T, FuncDim, 3> |
Public Member Functions | |
| JointErrorFunctionT (const Skeleton &skel, const ParameterTransform &pt, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1)) | |
| Constructor. | |
| 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 > ¶ms, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final |
| Computes the error function value l = w * loss(f^2). | |
| double | getGradient (const ModelParametersT< T > ¶ms, 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. | |
| double | getJacobian (const ModelParametersT< T > ¶ms, 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. | |
| size_t | getJacobianSize () const final |
| The number of rows in the jacobian is the dimension of f, FuncDim, times the number of constraints. | |
| void | addConstraint (const Data &constr) |
| Adds a constraint to the list. | |
| void | addConstraints (std::span< const Data > constrs) |
| Appends a list of constraints. | |
| void | setConstraints (std::span< const Data > constrs) |
| Replace the current list of constraints with the input. | |
| const std::vector< Data > & | getConstraints () const |
| size_t | getNumConstraints () const |
| void | clearConstraints () |
| Clear the current list of constraints. | |
| Data & | getConstraint (size_t index) |
| Mutable access to a constraint by index, for in-place updates. | |
| Public Member Functions inherited from momentum::SkeletonErrorFunctionT< T > | |
| SkeletonErrorFunctionT (const Skeleton &skel, const ParameterTransform &pt) | |
| virtual | ~SkeletonErrorFunctionT ()=default |
| const Skeleton & | getSkeleton () const |
| const ParameterTransform & | getParameterTransform () const |
| virtual const Character * | getCharacter () const |
| For mesh-based error functions, get access to the underlying character. | |
| void | setWeight (T w) |
| T | getWeight () const |
| void | setActiveJoints (const VectorX< bool > &aj) |
| void | setEnabledParameters (const ParameterSet &ps) |
| virtual double | getGradient (const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &, Eigen::Ref< Eigen::VectorX< T > >) |
| virtual double | getJacobian (const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &, Eigen::Ref< Eigen::MatrixX< T > >, Eigen::Ref< Eigen::VectorX< T > >, int &usedRows) |
| virtual void | getHessian (const ModelParametersT< T > &, const SkeletonStateT< T > &, const MeshStateT< T > &, Eigen::Ref< Eigen::MatrixX< T > >) |
| virtual double | getSolverDerivatives (const ModelParametersT< T > ¶meters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, const size_t actualParameters, Eigen::MatrixX< T > &hess, Eigen::VectorX< T > &grad) |
| virtual bool | needsMesh () const |
Static Public Attributes | |
| static constexpr size_t | kFuncDim = FuncDim |
| static constexpr size_t | kNumVec = NumVec |
| static constexpr size_t | kNumPos = NumPos |
Protected Member Functions | |
| 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. | |
Protected Attributes | |
| std::vector< Data > | constraints_ |
| List of constraints. | |
| const GeneralizedLossT< T > | loss_ |
| The generalized loss function that transforms f^2. | |
| VectorX< T > | jointGrad_ |
| Intermediate storage of the gradient from this error function. | |
| Protected Attributes inherited from momentum::SkeletonErrorFunctionT< T > | |
| const Skeleton & | skeleton_ |
| const ParameterTransform & | parameterTransform_ |
| T | weight_ |
| VectorX< bool > | activeJointParams_ |
| ParameterSet | enabledParameters_ |
| bool | useHessian_ {false} |
The JointErrorFunction is a base class for constraint errors of the form l = w * loss(f^2), where w is the weight (could be a product of different weighting terms), loss() is the generalized loss function (see math/generalized_loss.h), and f is a difference vector we want to minimize.
f takes the form of f(v, target), where v = T(q)*source. T is the global transformation of the parent joint of the source, and target is the desired value of source in global space. f computes the differences between v and target.
Based on the above, we have Jacobian: df/dq = df/dv * dv/dT * dT/dq, and Gradient: dl/dq = dl/df * Jac
The derivative computation (hierarchy walk from joint to root) is delegated to SkeletonDerivativeT, keeping the per-constraint logic minimal. Derived classes implement evalFunction() to define the constraint residual and its derivatives.
This should work for a point (eg. PositionErrorFunction), or an axis (eg. FixedAxisErrorFunction), but it can also work for a rotation matrix, or a 3x4 transformation matrix, by applying the transformation one axis/point at a time. The number of 3-vectors to be transformed in a constraint is NumVec.
Definition at line 54 of file joint_error_function.h.
| using momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::DfdvType = Eigen::Matrix<T, FuncDim, 3> |
Definition at line 62 of file joint_error_function.h.
| using momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::FuncType = Vector<T, FuncDim> |
Definition at line 60 of file joint_error_function.h.
| using momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::VType = Vector3<T> |
Definition at line 61 of file joint_error_function.h.
|
explicit |
Constructor.
| [in] | skel | character skeleton |
| [in] | pt | parameter transformation |
| [in] | lossAlpha | alpha parameter for the loss function |
| [in] | lossC | c parameter for the loss function |
Definition at line 20 of file joint_error_function-inl.h.
References jointGrad_, loss_, and momentum::SkeletonErrorFunctionT< T >::SkeletonErrorFunctionT().
Referenced by momentum::AimDirErrorFunctionT< float >::AimDirErrorFunctionT(), momentum::AimDistErrorFunctionT< float >::AimDistErrorFunctionT(), momentum::FixedAxisAngleErrorFunctionT< float >::FixedAxisAngleErrorFunctionT(), momentum::FixedAxisCosErrorFunctionT< float >::FixedAxisCosErrorFunctionT(), momentum::FixedAxisDiffErrorFunctionT< float >::FixedAxisDiffErrorFunctionT(), JointErrorFunctionT(), momentum::NormalErrorFunctionT< float >::NormalErrorFunctionT(), momentum::OrientationErrorFunctionT< float >::OrientationErrorFunctionT(), momentum::OrientationRotDiffErrorFunctionT< float >::OrientationRotDiffErrorFunctionT(), momentum::PlaneErrorFunctionT< float >::PlaneErrorFunctionT(), and momentum::PositionErrorFunctionT< float >::PositionErrorFunctionT().
|
inlineexplicit |
A convenience constructor where character contains info of the skeleton and parameter transform.
| [in] | character | character definition |
| [in] | lossAlpha | alpha parameter for the loss function |
| [in] | lossC | c parameter for the loss function |
Definition at line 83 of file joint_error_function.h.
References JointErrorFunctionT(), and momentum::GeneralizedLossT< T >::kL2.
|
inline |
Adds a constraint to the list.
| [in] | constr | the constraint to be added |
Definition at line 155 of file joint_error_function.h.
References addConstraint(), and constraints_.
Referenced by addConstraint().
|
inline |
Appends a list of constraints.
| [in] | constrs | a list of constraints to be added |
Definition at line 162 of file joint_error_function.h.
References constraints_.
|
inline |
Clear the current list of constraints.
Definition at line 183 of file joint_error_function.h.
References constraints_.
|
protectedpure virtual |
Evaluate the constraint residual and derivatives for a single constraint.
The leaf class computes the world-space vectors v, the residual f, and the derivatives dfdv from the constraint data and the parent joint state. The first NumPos entries of v are points (w=1), the rest are directions (w=0).
| [in] | constrIndex | index of the constraint |
| [in] | state | the joint state of the constraint's parent joint |
| [out] | f | output residual vector of dimension FuncDim |
| [out] | v | output world-space vectors (NumVec entries) |
| [out] | dfdv | output df/dv matrices (one per world vector) |
Referenced by getError(), and getJacobian().
|
inline |
Mutable access to a constraint by index, for in-place updates.
| [in] | index | index of the constraint |
Definition at line 191 of file joint_error_function.h.
References constraints_.
|
inlinenodiscard |
Definition at line 174 of file joint_error_function.h.
References constraints_.
|
nodiscardfinalvirtual |
Computes the error function value l = w * loss(f^2).
It gets f from the derived class, and implements the rest.
| [in] | params | current model parameters |
| [in] | state | current global skeleton joint states computed from the model parameters |
| [in] | meshState | current mesh state (unused by this base class) |
Reimplemented from momentum::SkeletonErrorFunctionT< T >.
Definition at line 35 of file joint_error_function-inl.h.
References constraints_, evalFunction(), momentum::SkeletonStateT< T >::jointState, loss_, MT_PROFILE_FUNCTION, and momentum::SkeletonErrorFunctionT< T >::weight_.
|
final |
The gradient of the error function: dl/dq = dl/d[f^2] * 2f * df/dv * dv/dq.
It gets df/dv from the derived class, and implements the rest.
| [in] | params | current model parameters |
| [in] | state | current global skeleton joint states computed from the model parameters |
| [in] | meshState | current mesh state (unused by this base class) |
| [out] | gradient | the gradient vector to accumulate into |
Definition at line 57 of file joint_error_function-inl.h.
References jointGrad_, MT_PROFILE_FUNCTION, and momentum::SkeletonErrorFunctionT< T >::parameterTransform_.
|
final |
For least-square problems, we assume l is the square of a vector function F.
The jacobian is then dF/dq. (A factor 2 is implemented in the solver.) With l2 loss, we have F = sqrt(w) * f, and the jacobian is sqrt(w) * df/dv * dv/dq. With the generalized loss, the jacobian becomes sqrt(w * d[loss]/d[f^2]) * df/dv * dv/dq. It gets df/dv from the derived class, and implements the rest.
| [in] | params | current model parameters |
| [in] | state | current global skeleton joint states computed from the model parameters |
| [in] | meshState | current mesh state (unused by this base class) |
| [out] | jacobian | the output jacobian matrix |
| [out] | residual | the output function residual (ie. f scaled by the loss gradient) |
| [out] | usedRows | number of rows in the jacobian/residual used by this error function |
Definition at line 179 of file joint_error_function-inl.h.
References momentum::SkeletonErrorFunctionT< T >::activeJointParams_, constraints_, momentum::SkeletonErrorFunctionT< T >::enabledParameters_, momentum::Eps(), evalFunction(), getJacobianSize(), momentum::isApprox(), momentum::SkeletonStateT< T >::jointState, momentum::kInvalidIndex, momentum::kParametersPerJoint, loss_, MT_CHECK, MT_PROFILE_FUNCTION, momentum::SkeletonErrorFunctionT< T >::parameterTransform_, momentum::SkeletonErrorFunctionT< T >::skeleton_, and momentum::SkeletonErrorFunctionT< T >::weight_.
|
nodiscardfinalvirtual |
The number of rows in the jacobian is the dimension of f, FuncDim, times the number of constraints.
Reimplemented from momentum::SkeletonErrorFunctionT< T >.
Definition at line 300 of file joint_error_function-inl.h.
References constraints_.
Referenced by getJacobian().
|
inlinenodiscard |
Definition at line 178 of file joint_error_function.h.
References constraints_.
|
inline |
Replace the current list of constraints with the input.
| [in] | constrs | the new list of constraints |
Definition at line 169 of file joint_error_function.h.
References constraints_.
|
protected |
List of constraints.
Definition at line 197 of file joint_error_function.h.
Referenced by addConstraint(), addConstraints(), clearConstraints(), getConstraint(), getConstraints(), getError(), getJacobian(), getJacobianSize(), getNumConstraints(), and setConstraints().
|
protected |
Intermediate storage of the gradient from this error function.
We can allocate the space in the constructor to save some dynamic allocation.
Definition at line 202 of file joint_error_function.h.
Referenced by getGradient(), and JointErrorFunctionT().
|
staticconstexpr |
Definition at line 56 of file joint_error_function.h.
|
staticconstexpr |
Definition at line 58 of file joint_error_function.h.
|
staticconstexpr |
Definition at line 57 of file joint_error_function.h.
|
protected |
The generalized loss function that transforms f^2.
Definition at line 199 of file joint_error_function.h.
Referenced by getError(), getJacobian(), and JointErrorFunctionT().