Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos > Class Template Referenceabstract

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>

Inheritance diagram for momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >:

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 > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
 Computes the error function value l = w * loss(f^2).
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.
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.
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 > &parameters, 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}

Detailed Description

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
class momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >

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.

Member Typedef Documentation

◆ DfdvType

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
using momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::DfdvType = Eigen::Matrix<T, FuncDim, 3>

Definition at line 62 of file joint_error_function.h.

◆ FuncType

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
using momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::FuncType = Vector<T, FuncDim>

Definition at line 60 of file joint_error_function.h.

◆ VType

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
using momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::VType = Vector3<T>

Definition at line 61 of file joint_error_function.h.

Constructor & Destructor Documentation

◆ JointErrorFunctionT() [1/2]

◆ JointErrorFunctionT() [2/2]

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::JointErrorFunctionT ( const Character & character,
const T & lossAlpha = GeneralizedLossT<T>::kL2,
const T & lossC = T(1) )
inlineexplicit

A convenience constructor where character contains info of the skeleton and parameter transform.

Parameters
[in]charactercharacter definition
[in]lossAlphaalpha parameter for the loss function
[in]lossCc parameter for the loss function

Definition at line 83 of file joint_error_function.h.

References JointErrorFunctionT(), and momentum::GeneralizedLossT< T >::kL2.

Member Function Documentation

◆ addConstraint()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
void momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::addConstraint ( const Data & constr)
inline

Adds a constraint to the list.

Parameters
[in]constrthe constraint to be added

Definition at line 155 of file joint_error_function.h.

References addConstraint(), and constraints_.

Referenced by addConstraint().

◆ addConstraints()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
void momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::addConstraints ( std::span< const Data > constrs)
inline

Appends a list of constraints.

Parameters
[in]constrsa list of constraints to be added

Definition at line 162 of file joint_error_function.h.

References constraints_.

◆ clearConstraints()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
void momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::clearConstraints ( )
inline

Clear the current list of constraints.

Definition at line 183 of file joint_error_function.h.

References constraints_.

◆ evalFunction()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
virtual void momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::evalFunction ( size_t constrIndex,
const JointStateT< T > & state,
FuncType & f,
std::array< VType, NumVec > & v,
std::array< DfdvType, NumVec > & dfdv ) const
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).

Parameters
[in]constrIndexindex of the constraint
[in]statethe joint state of the constraint's parent joint
[out]foutput residual vector of dimension FuncDim
[out]voutput world-space vectors (NumVec entries)
[out]dfdvoutput df/dv matrices (one per world vector)

Referenced by getError(), and getJacobian().

◆ getConstraint()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
Data & momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::getConstraint ( size_t index)
inline

Mutable access to a constraint by index, for in-place updates.

Parameters
[in]indexindex of the constraint
Returns
mutable reference to the constraint data

Definition at line 191 of file joint_error_function.h.

References constraints_.

◆ getConstraints()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
const std::vector< Data > & momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::getConstraints ( ) const
inlinenodiscard
Returns
the current list of constraints immutable

Definition at line 174 of file joint_error_function.h.

References constraints_.

◆ getError()

template<typename T, class Data, size_t FuncDim, size_t NumVec, size_t NumPos>
double momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::getError ( const ModelParametersT< T > & params,
const SkeletonStateT< T > & state,
const MeshStateT< T > & meshState )
nodiscardfinalvirtual

Computes the error function value l = w * loss(f^2).

It gets f from the derived class, and implements the rest.

Parameters
[in]paramscurrent model parameters
[in]statecurrent global skeleton joint states computed from the model parameters
[in]meshStatecurrent mesh state (unused by this base class)
Returns
the error function value l

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_.

◆ getGradient()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
double momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::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.

It gets df/dv from the derived class, and implements the rest.

Parameters
[in]paramscurrent model parameters
[in]statecurrent global skeleton joint states computed from the model parameters
[in]meshStatecurrent mesh state (unused by this base class)
[out]gradientthe gradient vector to accumulate into
Returns
the error function value l

Definition at line 57 of file joint_error_function-inl.h.

References jointGrad_, MT_PROFILE_FUNCTION, and momentum::SkeletonErrorFunctionT< T >::parameterTransform_.

◆ getJacobian()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
double momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::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.

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.

Parameters
[in]paramscurrent model parameters
[in]statecurrent global skeleton joint states computed from the model parameters
[in]meshStatecurrent mesh state (unused by this base class)
[out]jacobianthe output jacobian matrix
[out]residualthe output function residual (ie. f scaled by the loss gradient)
[out]usedRowsnumber of rows in the jacobian/residual used by this error function
Returns
the error function l

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_.

◆ getJacobianSize()

template<typename T, class Data, size_t FuncDim, size_t NumVec, size_t NumPos>
size_t momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::getJacobianSize ( ) const
nodiscardfinalvirtual

The number of rows in the jacobian is the dimension of f, FuncDim, times the number of constraints.

Returns
number of rows in the jacobian

Reimplemented from momentum::SkeletonErrorFunctionT< T >.

Definition at line 300 of file joint_error_function-inl.h.

References constraints_.

Referenced by getJacobian().

◆ getNumConstraints()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
size_t momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::getNumConstraints ( ) const
inlinenodiscard

Definition at line 178 of file joint_error_function.h.

References constraints_.

◆ setConstraints()

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
void momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::setConstraints ( std::span< const Data > constrs)
inline

Replace the current list of constraints with the input.

Parameters
[in]constrsthe new list of constraints

Definition at line 169 of file joint_error_function.h.

References constraints_.

Member Data Documentation

◆ constraints_

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
std::vector<Data> momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::constraints_
protected

◆ jointGrad_

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
VectorX<T> momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::jointGrad_
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().

◆ kFuncDim

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
size_t momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::kFuncDim = FuncDim
staticconstexpr

Definition at line 56 of file joint_error_function.h.

◆ kNumPos

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
size_t momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::kNumPos = NumPos
staticconstexpr

Definition at line 58 of file joint_error_function.h.

◆ kNumVec

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
size_t momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::kNumVec = NumVec
staticconstexpr

Definition at line 57 of file joint_error_function.h.

◆ loss_

template<typename T, class Data, size_t FuncDim = 3, size_t NumVec = 1, size_t NumPos = 1>
const GeneralizedLossT<T> momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::loss_
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().


The documentation for this class was generated from the following files: