Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
height_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#include <momentum/math/fwd.h>
14
15#include <Eigen/Dense>
16
17#include <queue>
18#include <vector>
19
20namespace momentum {
21
35template <typename T>
37 public:
47 const Character& character,
48 T targetHeight,
49 const Eigen::Vector3<T>& upDirection = Eigen::Vector3<T>::UnitY(),
50 size_t k = 1);
52
54 HeightErrorFunctionT(HeightErrorFunctionT&& other) noexcept = delete;
57
58 [[nodiscard]] double getError(
59 const ModelParametersT<T>& modelParameters,
60 const SkeletonStateT<T>& state,
61 const MeshStateT<T>& meshState) final;
62
64 const ModelParametersT<T>& modelParameters,
65 const SkeletonStateT<T>& state,
66 const MeshStateT<T>& meshState,
67 Eigen::Ref<Eigen::VectorX<T>> gradient) final;
68
70 const ModelParametersT<T>& modelParameters,
71 const SkeletonStateT<T>& state,
72 const MeshStateT<T>& meshState,
73 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
74 Eigen::Ref<Eigen::VectorX<T>> residual,
75 int& usedRows) final;
76
77 [[nodiscard]] size_t getJacobianSize() const final;
78
81 void setTargetHeight(T height);
82
84 [[nodiscard]] T getTargetHeight() const {
85 return targetHeight_;
86 }
87
90 void setUpDirection(const Eigen::Vector3<T>& upDirection);
91
93 [[nodiscard]] const Eigen::Vector3<T>& getUpDirection() const {
94 return upDirection_;
95 }
96
98 [[nodiscard]] const Character* getCharacter() const override {
99 return &character_;
100 }
101
104 [[nodiscard]] bool needsMesh() const override {
105 return false;
106 }
107
108 private:
112 [[nodiscard]] ModelParametersT<T> applyActiveParameters(
113 const ModelParametersT<T>& modelParameters) const;
114
116 struct HeightResult {
117 T height{};
118 std::vector<size_t> minVertexIndices;
119 std::vector<T> minVertexWeights;
120 std::vector<size_t> maxVertexIndices;
121 std::vector<T> maxVertexWeights;
122 };
123
126 [[nodiscard]] HeightResult calculateHeight() const;
127
129 template <typename Derived>
130 void calculateVertexJacobian(
131 size_t vertexIndex,
132 const Eigen::Vector3<T>& jacobianDirection,
133 Eigen::MatrixBase<Derived>& jacobian) const;
134
136 void calculateVertexGradient(
137 size_t vertexIndex,
138 const Eigen::Vector3<T>& gradientDirection,
139 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
140
142 void calculateDWorldPos(
143 size_t vertexIndex,
144 const Eigen::Vector3<T>& d_restPos,
145 Eigen::Vector3<T>& d_worldPos) const;
146
147 const Character& character_;
148
149 T targetHeight_;
150 Eigen::Vector3<T> upDirection_;
151 size_t k_;
152
158 ParameterSet activeModelParams_;
159
165 mutable SkeletonStateT<T> skeletonState_;
166
173 mutable MeshStateT<T> meshState_;
174};
175
176} // namespace momentum
bool needsMesh() const override
Override to indicate this function does NOT require mesh state (we maintain our own meshes).
HeightErrorFunctionT(HeightErrorFunctionT &&other) noexcept=delete
double getJacobian(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
void setUpDirection(const Eigen::Vector3< T > &upDirection)
Set the up direction for height measurement.
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
const Eigen::Vector3< T > & getUpDirection() const
Get the up direction.
HeightErrorFunctionT(const Character &character, T targetHeight, const Eigen::Vector3< T > &upDirection=Eigen::Vector3< T >::UnitY(), size_t k=1)
Construct a height error function.
const Character * getCharacter() const override
Get the character.
HeightErrorFunctionT & operator=(const HeightErrorFunctionT &other)=delete
HeightErrorFunctionT & operator=(HeightErrorFunctionT &&other)=delete
HeightErrorFunctionT(const HeightErrorFunctionT &other)=delete
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
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
Represents the complete state of a skeleton.