Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::VertexNormalErrorFunctionT< T > Class Template Reference

Error function for vertex Normal and SymmetricNormal constraints. More...

#include <vertex_normal_error_function.h>

Inheritance diagram for momentum::VertexNormalErrorFunctionT< T >:

Public Types

using Base = VertexErrorFunctionT<T, VertexNormalDataT<T>, 1>
Public Types inherited from momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >
using FuncType
using DfdvType

Public Member Functions

 VertexNormalErrorFunctionT (const Character &character, const ParameterTransform &parameterTransform, T sourceNormalWeight=T(1), T targetNormalWeight=T(0), const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1), bool computeAccurateNormalDerivatives=false)
 Constructor.
 ~VertexNormalErrorFunctionT () override=default
void evalFunction (size_t constrIndex, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, std::span< const Eigen::Vector3< T > > worldVecs, FuncType &f, std::span< DfdvType > dfdv) const final
 Computes the residual and derivatives.
double getGradient (const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) override
 Override to handle normal rotation correction terms via SkeletonDerivativeT.
double getJacobian (const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) override
 Override to handle normal rotation correction terms via SkeletonDerivativeT.
void setComputeAccurateNormalDerivatives (bool enable)
 Enable or disable the expensive d(normal)/d(blendShapeWeight) computation.
bool computeAccurateNormalDerivatives () const
 Returns whether accurate normal blend shape derivatives are enabled.
Public Member Functions inherited from momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >
 VertexErrorFunctionT (const Character &character, const ParameterTransform &parameterTransform, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
 Constructor.
 ~VertexErrorFunctionT () override=default
const Character * getCharacter () const final
 Returns the character (needed for mesh-based computations).
bool needsMesh () const final
 Returns true (vertex constraints always need mesh).
void addConstraint (const VertexNormalDataT< T > &constraint)
 Adds a constraint.
void addConstraints (std::span< const VertexNormalDataT< T > > constraints)
 Appends a list of constraints.
void setConstraints (std::span< const VertexNormalDataT< T > > constraints)
 Sets all constraints, replacing existing ones.
void clearConstraints ()
 Clears all constraints.
const std::vector< VertexNormalDataT< T > > & getConstraints () const
 Returns the current constraints.
size_t getNumConstraints () const
 Returns the number of constraints.
size_t getJacobianSize () const final
 Returns the Jacobian size.
void setMaxThreads (size_t maxThreads)
 Sets the maximum number of threads for parallel execution.
double getError (const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) override
 Computes the error.
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
void setWeight (T w)
T getWeight () const
void setActiveJoints (const VectorX< bool > &aj)
void setEnabledParameters (const ParameterSet &ps)
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)

Additional Inherited Members

Static Public Attributes inherited from momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >
static constexpr size_t kFuncDim
static constexpr T kLegacyWeight
 Default constant weight for backwards compatibility with the legacy VertexErrorFunctionT.
Protected Attributes inherited from momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >
const Character & character_
std::vector< VertexNormalDataT< T > > constraints_
GeneralizedLossT< T > loss_
size_t maxThreads_
T legacyWeight_
 Legacy weight multiplied into error/gradient/Jacobian for backwards compatibility.
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 momentum::VertexNormalErrorFunctionT< T >

Error function for vertex Normal and SymmetricNormal constraints.

Computes point-to-plane distance using a mix of source (mesh) and target normals: f = (sW * sourceNormal + tW * targetNormal) ยท (vertexPos - targetPosition)

The sourceNormalWeight and targetNormalWeight control the mix:

  • Normal type: sW=1, tW=0 (pure source normal)
  • SymmetricNormal type: sW=0.5, tW=0.5 (50/50 mix)

The source normal rotates with the mesh, which requires special derivative handling. This is handled automatically by the normal correction methods in SkeletonDerivativeT.

Template Parameters
TScalar type (float or double)

Definition at line 50 of file vertex_normal_error_function.h.

Member Typedef Documentation

◆ Base

template<typename T>
using momentum::VertexNormalErrorFunctionT< T >::Base = VertexErrorFunctionT<T, VertexNormalDataT<T>, 1>

Definition at line 52 of file vertex_normal_error_function.h.

Constructor & Destructor Documentation

◆ VertexNormalErrorFunctionT()

template<typename T>
momentum::VertexNormalErrorFunctionT< T >::VertexNormalErrorFunctionT ( const Character & character,
const ParameterTransform & parameterTransform,
T sourceNormalWeight = T(1),
T targetNormalWeight = T(0),
const T & lossAlpha = GeneralizedLossT<T>::kL2,
const T & lossC = T(1),
bool computeAccurateNormalDerivatives = false )
explicit

Constructor.

Parameters
characterThe character (needed for mesh access)
parameterTransformMaps joint parameters to model parameters
sourceNormalWeightWeight for source (mesh) normal (1.0 for Normal, 0.5 for SymmetricNormal)
targetNormalWeightWeight for target normal (0.0 for Normal, 0.5 for SymmetricNormal)
lossAlphaAlpha parameter for the generalized loss function (default: L2)
lossCC parameter for the generalized loss function (default: 1)
computeAccurateNormalDerivativesIf true, compute expensive d(normal)/d(blendShapeWeight) terms in the gradient and Jacobian. Default false for better performance.

Definition at line 23 of file vertex_normal_error_function.cpp.

References computeAccurateNormalDerivatives(), momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >::kLegacyWeight, momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >::legacyWeight_, and momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >::VertexErrorFunctionT().

◆ ~VertexNormalErrorFunctionT()

template<typename T>
momentum::VertexNormalErrorFunctionT< T >::~VertexNormalErrorFunctionT ( )
overridedefault

Member Function Documentation

◆ computeAccurateNormalDerivatives()

template<typename T>
bool momentum::VertexNormalErrorFunctionT< T >::computeAccurateNormalDerivatives ( ) const
inlinenodiscard

Returns whether accurate normal blend shape derivatives are enabled.

Definition at line 109 of file vertex_normal_error_function.h.

Referenced by VertexNormalErrorFunctionT().

◆ evalFunction()

template<typename T>
void momentum::VertexNormalErrorFunctionT< T >::evalFunction ( size_t constrIndex,
const SkeletonStateT< T > & state,
const MeshStateT< T > & meshState,
std::span< const Eigen::Vector3< T > > worldVecs,
FuncType & f,
std::span< DfdvType > dfdv ) const
finalvirtual

Computes the residual and derivatives.

Derived classes implement this with their specific constraint logic. worldVecs[0] is the pre-computed vertex world position.

Parameters
constrIndexIndex of the constraint
stateCurrent skeleton state
meshStateCurrent mesh state
worldVecsPre-computed world position (size 1 for vertex constraints)
fOutput: residual (FuncDim x 1)
dfdvOutput: derivative df/dv (must be filled if non-empty)

Implements momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >.

Definition at line 43 of file vertex_normal_error_function.cpp.

References momentum::cast(), momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >::constraints_, MT_CHECK, MT_CHECK_NOTNULL, momentum::normal(), and momentum::MeshStateT< T >::posedMesh_.

◆ getGradient()

◆ getJacobian()

template<typename T>
double momentum::VertexNormalErrorFunctionT< T >::getJacobian ( const ModelParametersT< T > & params,
const SkeletonStateT< T > & state,
const MeshStateT< T > & meshState,
Eigen::Ref< Eigen::MatrixX< T > > jacobian,
Eigen::Ref< Eigen::VectorX< T > > residual,
int & usedRows )
overridevirtual

◆ setComputeAccurateNormalDerivatives()

template<typename T>
void momentum::VertexNormalErrorFunctionT< T >::setComputeAccurateNormalDerivatives ( bool enable)
inline

Enable or disable the expensive d(normal)/d(blendShapeWeight) computation.

Definition at line 104 of file vertex_normal_error_function.h.


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