Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
fully_differentiable_orientation_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 Quaternion<T>& offset,
28 const Quaternion<T>& target,
29 size_t parent,
30 float w,
31 const std::string& n = "")
33 // Empty
34 }
35};
36
37template <typename T>
40 public SkeletonErrorFunctionT<T> {
41 public:
42 static constexpr T kOrientationWeight = 1e-1f;
43 static constexpr const char* kParents = "parents";
44 static constexpr const char* kOffsets = "offsets";
45 static constexpr const char* kWeights = "weights";
46 static constexpr const char* kTargets = "targets";
47
50
51 [[nodiscard]] std::vector<std::string> inputs() const final;
52 [[nodiscard]] Eigen::Index getInputSize(const std::string& name) const final;
53 [[nodiscard]] const char* name() const final {
54 return "OrientationErrorFunction";
55 }
56
57 Eigen::VectorX<T> d_gradient_d_input_dot(
58 const std::string& inputName,
59 const ModelParametersT<T>& params,
60 const SkeletonStateT<T>& state,
61 Eigen::Ref<const Eigen::VectorX<T>> inputVec) final;
62
63 double getError(
64 const ModelParametersT<T>& params,
65 const SkeletonStateT<T>& state,
66 const MeshStateT<T>& /* meshState */) final;
67
69 const ModelParametersT<T>& params,
70 const SkeletonStateT<T>& state,
71 const MeshStateT<T>& /* meshState */,
72 Ref<Eigen::VectorX<T>> gradient) final;
73
75 const ModelParametersT<T>& params,
76 const SkeletonStateT<T>& state,
77 const MeshStateT<T>& /* meshState */,
78 Ref<Eigen::MatrixX<T>> jacobian,
79 Ref<Eigen::VectorX<T>> residual,
80 int& usedRows) final;
81
82 [[nodiscard]] size_t getJacobianSize() const final;
83
84 void addConstraint(const OrientationConstraintT<T>& constr);
85
86 private:
87 void getInputImp(const std::string& name, Eigen::Ref<Eigen::VectorX<T>> value) const final;
88 void setInputImp(const std::string& name, Eigen::Ref<const Eigen::VectorX<T>> value) final;
89
90 T calculateOrientationGradient(
91 const SkeletonStateT<T>& state,
92 const OrientationConstraintT<T>& constr,
93 Eigen::VectorX<T>& jGrad) const;
94
95 template <typename JetType>
96 JetType calculateOrientationGradient_dot(
97 const SkeletonStateT<T>& state,
98 size_t constrParent,
99 const JetType& constrWeight,
100 const Eigen::Quaternion<JetType>& constrOrientationOffset,
101 const Eigen::Quaternion<JetType>& constrOrientationTarget,
102 Eigen::Ref<const Eigen::VectorX<T>> vec) const;
103
104 [[nodiscard]] T calculateOrientationJacobian(
105 const SkeletonStateT<T>& state,
106 const OrientationConstraintT<T>& constr,
107 Ref<Eigen::MatrixX<T>> jac,
108 Ref<Eigen::VectorX<T>> res) const;
109
110 std::vector<OrientationConstraintT<T>> constraints_;
111 Eigen::VectorX<T> jointGrad_;
112};
113
114} // namespace momentum
void addConstraint(const OrientationConstraintT< float > &constr)
FullyDifferentiableOrientationErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &, Ref< Eigen::VectorX< T > > gradient) final
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &) 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
Eigen::Index getInputSize(const std::string &name) const 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
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::Quaternion< T > Quaternion
Unit quaternion representing a 3D rotation.
Definition types.h:385
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
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
OrientationConstraintT(const Quaternion< T > &offset, const Quaternion< T > &target, size_t parent, float w, const std::string &n="")
Represents the complete state of a skeleton.