Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_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
17#include <Eigen/Core>
18
19#include <span>
20#include <string>
21#include <vector>
22
23namespace momentum {
24
31
33 float weight = 0.0f;
34
36 std::string name;
37
39 VertexConstraintData(size_t vIndex, float w, const std::string& n = "")
40 : vertexIndex(vIndex), weight(w), name(n) {}
41};
42
59template <typename T, class Data, size_t FuncDim = 3>
61 public:
62 using FuncType = Eigen::Matrix<T, FuncDim, 1>;
63 using DfdvType = Eigen::Matrix<T, FuncDim, 3>;
64
65 static constexpr size_t kFuncDim = FuncDim;
66
70 static constexpr T kLegacyWeight = T(1e-4);
71
79 const Character& character,
80 const ParameterTransform& parameterTransform,
81 const T& lossAlpha = GeneralizedLossT<T>::kL2,
82 const T& lossC = T(1));
83
84 ~VertexErrorFunctionT() override = default;
85
87 [[nodiscard]] const Character* getCharacter() const final;
88
90 [[nodiscard]] bool needsMesh() const final;
91
93 void addConstraint(const Data& constraint);
94
96 void addConstraints(std::span<const Data> constraints) {
97 for (const auto& c : constraints) {
99 }
100 }
101
103 void setConstraints(std::span<const Data> constraints);
104
107
109 [[nodiscard]] const std::vector<Data>& getConstraints() const;
110
112 [[nodiscard]] size_t getNumConstraints() const {
113 return constraints_.size();
114 }
115
117 [[nodiscard]] size_t getJacobianSize() const final {
118 return FuncDim * constraints_.size();
119 }
120
122 void setMaxThreads(size_t maxThreads) {
123 maxThreads_ = maxThreads;
124 }
125
127 [[nodiscard]] double getError(
128 const ModelParametersT<T>& params,
129 const SkeletonStateT<T>& state,
130 const MeshStateT<T>& meshState) override;
131
134 const ModelParametersT<T>& params,
135 const SkeletonStateT<T>& state,
136 const MeshStateT<T>& meshState,
137 Eigen::Ref<Eigen::VectorX<T>> gradient) override;
138
141 const ModelParametersT<T>& params,
142 const SkeletonStateT<T>& state,
143 const MeshStateT<T>& meshState,
144 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
145 Eigen::Ref<Eigen::VectorX<T>> residual,
146 int& usedRows) override;
147
159 virtual void evalFunction(
160 size_t constrIndex,
161 const SkeletonStateT<T>& state,
162 const MeshStateT<T>& meshState,
163 std::span<const Eigen::Vector3<T>> worldVecs,
164 FuncType& f,
165 std::span<DfdvType> dfdv) const = 0;
166
167 protected:
169 std::vector<Data> constraints_;
171 size_t maxThreads_ = 0;
172
176};
177
178} // namespace momentum
Implementation of "A General and Adaptive Robust Loss Function" (https://arxiv.org/abs/1701....
static constexpr T kL2
Special-case alpha values that are either at a singularity in the general formula or simplify to a cl...
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
size_t getJacobianSize() const final
Returns the Jacobian size.
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) override
Computes the error.
virtual void evalFunction(size_t constrIndex, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, std::span< const Eigen::Vector3< T > > worldVecs, FuncType &f, std::span< DfdvType > dfdv) const =0
Computes the residual and derivatives.
void addConstraints(std::span< const VertexNormalDataT< T > > constraints)
VertexErrorFunctionT(const Character &character, const ParameterTransform &parameterTransform, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
Constructor.
void setMaxThreads(size_t maxThreads)
Sets the maximum number of threads for parallel execution.
size_t getNumConstraints() const
Returns the number of constraints.
const std::vector< Data > & getConstraints() const
Returns the current constraints.
Eigen::Matrix< T, FuncDim, 1 > FuncType
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) override
Computes the gradient.
void setConstraints(std::span< const Data > constraints)
Sets all constraints, replacing existing ones.
Eigen::Matrix< T, FuncDim, 3 > DfdvType
void addConstraint(const VertexNormalDataT< T > &constraint)
void clearConstraints()
Clears all constraints.
const Character * getCharacter() const final
Returns the character (needed for mesh-based computations).
double getJacobian(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) override
Computes the Jacobian.
~VertexErrorFunctionT() override=default
Shared types used across error function hierarchies.
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
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.
VertexConstraintData(size_t vIndex, float w, const std::string &n="")
std::string name
Name of the constraint (for debugging).
float weight
Weight of the constraint.
size_t vertexIndex
Vertex index in the mesh.