Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skinned_locator_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/fwd.h>
17
18namespace momentum {
19
24template <typename T>
26 int locatorIndex = -1;
27 T weight = 1;
28 Eigen::Vector3<T> targetPosition;
29
30 template <typename T2>
32 return {
33 this->locatorIndex,
34 static_cast<T2>(this->weight),
35 this->targetPosition.template cast<T2>()};
36 }
37};
38
46template <typename T>
48 public:
50 explicit SkinnedLocatorErrorFunctionT(const Character& character_in);
52
57
63 [[nodiscard]] double getError(
64 const ModelParametersT<T>& modelParameters,
65 const SkeletonStateT<T>& state,
66 const MeshStateT<T>& /* meshState */) final;
67
75 const ModelParametersT<T>& modelParameters,
76 const SkeletonStateT<T>& state,
77 const MeshStateT<T>& /* meshState */,
78 Eigen::Ref<Eigen::VectorX<T>> gradient) final;
79
89 const ModelParametersT<T>& modelParameters,
90 const SkeletonStateT<T>& state,
91 const MeshStateT<T>& /* meshState */,
92 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
93 Eigen::Ref<Eigen::VectorX<T>> residual,
94 int& usedRows) final;
95
97 [[nodiscard]] size_t getJacobianSize() const final;
98
104 void addConstraint(int locatorIndex, T weight, const Eigen::Vector3<T>& targetPosition);
105
108
112 void setConstraints(const std::vector<SkinnedLocatorConstraintT<T>>& constraints);
113
115 [[nodiscard]] const std::vector<SkinnedLocatorConstraintT<T>>& getConstraints() const {
116 return constraints_;
117 }
118
120 [[nodiscard]] const Character* getCharacter() const override {
121 return &character_;
122 }
123
124 private:
125 // Get the parameter index for a skinned locator, or -1 if not parameterized
126 [[nodiscard]] int getSkinnedLocatorParameterIndex(int locatorIndex) const {
127 if (locatorIndex <
128 static_cast<int>(character_.parameterTransform.skinnedLocatorParameters.size())) {
129 return character_.parameterTransform.skinnedLocatorParameters[locatorIndex];
130 }
131 return -1;
132 }
133
134 // Calculate the derivative of the world position with respect to the rest position
135 void calculateDWorldPos(
136 const SkeletonStateT<T>& state,
137 const SkinnedLocatorConstraintT<T>& constr,
138 const Eigen::Vector3<T>& d_restPos,
139 Eigen::Vector3<T>& d_worldPos) const;
140 double calculatePositionJacobian(
141 const ModelParametersT<T>& modelParameters,
142 const SkeletonStateT<T>& state,
143 const SkinnedLocatorConstraintT<T>& constr,
144 Ref<Eigen::MatrixX<T>> jac,
145 Ref<Eigen::VectorX<T>> res) const;
146
147 double calculatePositionGradient(
148 const ModelParametersT<T>& modelParameters,
149 const SkeletonStateT<T>& state,
150 const SkinnedLocatorConstraintT<T>& constr,
151 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
152
153 // Calculate the skinned locator position in world space
154 Eigen::Vector3<T> calculateSkinnedLocatorPosition(
155 const SkeletonStateT<T>& state,
156 int locatorIndex,
157 const Eigen::Vector3<T>& locatorRestPos) const;
158 Eigen::Vector3<T> getLocatorRestPosition(const ModelParametersT<T>& modelParams, int locatorIndex)
159 const;
160
161 const Character& character_;
162
163 std::vector<SkinnedLocatorConstraintT<T>> constraints_;
164};
165
166} // namespace momentum
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &, Eigen::Ref< Eigen::VectorX< T > > gradient) final
Computes the gradient of the error function with respect to model parameters.
SkinnedLocatorErrorFunctionT(const SkinnedLocatorErrorFunctionT< T > &other)=delete
SkinnedLocatorErrorFunctionT< T > & operator=(SkinnedLocatorErrorFunctionT< T > &&other)=delete
SkinnedLocatorErrorFunctionT(SkinnedLocatorErrorFunctionT< T > &&other)=delete
size_t getJacobianSize() const final
Returns the number of rows needed for the Jacobian matrix.
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &) final
Computes the total error for all skinned locator constraints.
SkinnedLocatorErrorFunctionT< T > & operator=(const SkinnedLocatorErrorFunctionT< T > &other)=delete
double getJacobian(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
Computes the Jacobian matrix and residual vector for the error function.
void setConstraints(const std::vector< SkinnedLocatorConstraintT< float > > &constraints)
SkinnedLocatorErrorFunctionT(const Character &character_in)
Constructs a skinned locator error function for the given character.
void addConstraint(int locatorIndex, float weight, const Eigen::Vector3< float > &targetPosition)
const Character * getCharacter() const override
Returns the character associated with this error function.
const std::vector< SkinnedLocatorConstraintT< float > > & getConstraints() const
Definition types.h:28
Eigen::Ref< T > Ref
Definition types.h:43
Vector< T, 3 > Vector3
Definition types.h:236
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
Definition character.h:39
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
VectorXi skinnedLocatorParameters
Parameters that control the rest-space positions of the skinned locators.
Represents the complete state of a skeleton.
Constraint structure for skinned locator error function.
SkinnedLocatorConstraintT< T2 > cast() const