Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
floor_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
12#include <momentum/math/fwd.h>
13
14#include <Eigen/Dense>
15
16#include <vector>
17
18namespace momentum {
19
34template <typename T>
36 public:
46 const Character& character,
47 std::vector<size_t> vertexIndices,
48 T targetHeight = T(0),
49 const Eigen::Vector3<T>& upDirection = Eigen::Vector3<T>::UnitY(),
50 size_t k = 5);
52
54 FloorErrorFunctionT(FloorErrorFunctionT&& 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
80 void setTargetHeight(T height);
81
83 [[nodiscard]] T getTargetHeight() const {
84 return targetHeight_;
85 }
86
90 void setUpDirection(const Eigen::Vector3<T>& upDirection);
91
93 [[nodiscard]] const Eigen::Vector3<T>& getUpDirection() const {
94 return upDirection_;
95 }
96
98 void setVertexIndices(std::vector<size_t> indices);
99
101 [[nodiscard]] const std::vector<size_t>& getVertexIndices() const {
102 return vertexIndices_;
103 }
104
106 [[nodiscard]] const Character* getCharacter() const override {
107 return &character_;
108 }
109
111 [[nodiscard]] bool needsMesh() const override {
112 return true;
113 }
114
115 private:
117 struct MinHeightResult {
118 T avgProjection{};
119 std::vector<size_t> vertexIndices;
120 std::vector<T> vertexWeights;
121 };
122
124 [[nodiscard]] MinHeightResult calculateMinHeight(const MeshStateT<T>& meshState) const;
125
127 template <typename Derived>
128 void calculateVertexJacobian(
129 size_t vertexIndex,
130 const SkeletonStateT<T>& state,
131 const MeshStateT<T>& meshState,
132 const Eigen::Vector3<T>& jacobianDirection,
133 Eigen::MatrixBase<Derived>& jacobian) const;
134
136 void calculateVertexGradient(
137 size_t vertexIndex,
138 const SkeletonStateT<T>& state,
139 const MeshStateT<T>& meshState,
140 const Eigen::Vector3<T>& gradientDirection,
141 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
142
143 const Character& character_;
144
145 std::vector<size_t> vertexIndices_;
146 T targetHeight_;
147 Eigen::Vector3<T> upDirection_;
148 size_t k_;
149};
150
151} // namespace momentum
const Character * getCharacter() const override
Get the character.
bool needsMesh() const override
This function requires the solver to provide a posed mesh.
FloorErrorFunctionT(const Character &character, std::vector< size_t > vertexIndices, T targetHeight=T(0), const Eigen::Vector3< T > &upDirection=Eigen::Vector3< T >::UnitY(), size_t k=5)
Construct a floor error function.
const std::vector< size_t > & getVertexIndices() const
Get the candidate vertex indices.
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
FloorErrorFunctionT(FloorErrorFunctionT &&other) noexcept=delete
FloorErrorFunctionT(const FloorErrorFunctionT &other)=delete
void setUpDirection(const Eigen::Vector3< T > &upDirection)
Set the up direction for height measurement.
FloorErrorFunctionT & operator=(FloorErrorFunctionT &&other)=delete
void setVertexIndices(std::vector< size_t > indices)
Set the candidate vertex indices.
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
FloorErrorFunctionT & operator=(const FloorErrorFunctionT &other)=delete
const Eigen::Vector3< T > & getUpDirection() const
Get the up direction.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
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.