Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
fully_differentiable_position_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
13
14#include <vector>
15
16namespace momentum {
17
18template <typename T>
22 size_t parent;
23 float weight;
24 std::string name;
25
27 const Vector3<T>& offset,
28 const Vector3<T>& target,
29 size_t parent,
30 float w,
31 const std::string& n = "")
33 // Empty
34 }
35};
36
37template <typename T>
39 std::vector<Eigen::Vector3<T>>
40 position; // the current position of all locators according to a given skeleton state
41
43
45 const SkeletonStateT<T>& skeletonState,
46 const std::vector<PositionConstraintT<T>>& referenceConstraints) {
47 update(skeletonState, referenceConstraints);
48 }
49
50 void update(
51 const SkeletonStateT<T>& skeletonState,
52 const std::vector<PositionConstraintT<T>>& referenceConstraints);
53};
54
55template <typename T>
58 public SkeletonErrorFunctionT<T> {
59 public:
60 static constexpr T kPositionWeight = 1e-4f;
61 static constexpr const char* kParents = "parents";
62 static constexpr const char* kOffsets = "offsets";
63 static constexpr const char* kWeights = "weights";
64 static constexpr const char* kTargets = "targets";
65
68
69 [[nodiscard]] std::vector<std::string> inputs() const final;
70 [[nodiscard]] Eigen::Index getInputSize(const std::string& name) const final;
71 [[nodiscard]] const char* name() const final {
72 return "PositionErrorFunction";
73 }
74
75 Eigen::VectorX<T> d_gradient_d_input_dot(
76 const std::string& inputName,
77 const ModelParametersT<T>& params,
78 const SkeletonStateT<T>& state,
79 Eigen::Ref<const Eigen::VectorX<T>> inputVec) final;
80
81 double getError(
82 const ModelParametersT<T>& params,
83 const SkeletonStateT<T>& state,
84 const MeshStateT<T>& /* meshState */) final;
85
87 const ModelParametersT<T>& params,
88 const SkeletonStateT<T>& state,
89 const MeshStateT<T>& /* meshState */,
90 Ref<Eigen::VectorX<T>> gradient) final;
91
93 const ModelParametersT<T>& params,
94 const SkeletonStateT<T>& state,
95 const MeshStateT<T>& /* meshState */,
96 Ref<Eigen::MatrixX<T>> jacobian,
97 Ref<Eigen::VectorX<T>> residual,
98 int& usedRows) final;
99
100 [[nodiscard]] size_t getJacobianSize() const final;
101
102 void addConstraint(const PositionConstraintT<T>& constr);
103
104 [[nodiscard]] const std::vector<PositionConstraintT<T>>& getConstraints() const;
105
106 private:
107 void getInputImp(const std::string& name, Eigen::Ref<Eigen::VectorX<T>> value) const final;
108 void setInputImp(const std::string& name, Eigen::Ref<const Eigen::VectorX<T>> value) final;
109
110 T calculatePositionGradient(
111 const SkeletonStateT<T>& state,
112 const PositionConstraintT<T>& constr,
113 const Eigen::Vector3<T>& pos,
114 Eigen::VectorX<T>& jGrad) const;
115
116 template <typename JetType>
117 [[nodiscard]] JetType calculatePositionGradient_dot(
118 const SkeletonStateT<T>& state,
119 size_t iConstr,
120 size_t constrParent,
121 const JetType& constrWeight,
122 const Eigen::Vector3<JetType>& constr_offset,
123 const Eigen::Vector3<JetType>& constr_target,
124 Eigen::Ref<const Eigen::VectorX<T>> vec) const;
125
126 [[nodiscard]] T calculatePositionJacobian(
127 const SkeletonStateT<T>& state,
128 const PositionConstraintT<T>& constr,
129 const Eigen::Vector3<T>& pos,
130 Ref<Eigen::MatrixX<T>> jac,
131 Ref<Eigen::VectorX<T>> res) const;
132
133 std::vector<PositionConstraintT<T>> constraints_;
134 PositionConstraintStateT<T> constraintsState_;
135 Eigen::VectorX<T> jointGrad_;
136};
137
138} // namespace momentum
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &, Ref< Eigen::VectorX< T > > gradient) final
Eigen::VectorX< T > d_gradient_d_input_dot(const std::string &inputName, const ModelParametersT< T > &params, const SkeletonStateT< T > &state, Eigen::Ref< const Eigen::VectorX< T > > inputVec) final
const std::vector< PositionConstraintT< float > > & getConstraints() const
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &) final
FullyDifferentiablePositionErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Eigen::Index getInputSize(const std::string &name) const final
double getJacobian(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &, Ref< Eigen::MatrixX< T > > jacobian, Ref< Eigen::VectorX< T > > residual, int &usedRows) final
void addConstraint(const PositionConstraintT< float > &constr)
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Definition types.h:28
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
Vector< T, 3 > Vector3
Definition types.h:236
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
PositionConstraintStateT(const SkeletonStateT< T > &skeletonState, const std::vector< PositionConstraintT< T > > &referenceConstraints)
void update(const SkeletonStateT< T > &skeletonState, const std::vector< PositionConstraintT< T > > &referenceConstraints)
PositionConstraintT(const Vector3< T > &offset, const Vector3< T > &target, size_t parent, float w, const std::string &n="")
Represents the complete state of a skeleton.