Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_sequence_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
15#include <momentum/math/mesh.h>
16
17#include <span>
18
19namespace momentum {
20
22template <typename T>
24 int vertexIndex = -1;
25 T weight = 1;
26 Eigen::Vector3<T> targetVelocity;
27
28 template <typename T2>
30 return {
31 this->vertexIndex, static_cast<T2>(this->weight), this->targetVelocity.template cast<T2>()};
32 }
33};
34
39template <typename T>
41 public:
42 explicit VertexSequenceErrorFunctionT(const Character& character);
43
44 [[nodiscard]] size_t numFrames() const final {
45 return 2;
46 }
47
48 double getError(
49 std::span<const ModelParametersT<T>> modelParameters,
50 std::span<const SkeletonStateT<T>> skelStates,
51 std::span<const MeshStateT<T>> meshStates) const final;
53 std::span<const ModelParametersT<T>> modelParameters,
54 std::span<const SkeletonStateT<T>> skelStates,
55 std::span<const MeshStateT<T>> meshStates,
56 Eigen::Ref<Eigen::VectorX<T>> gradient) const final;
57
58 // modelParameters: [numFrames() * parameterTransform] parameter vector
59 // skelStates: [numFrames()] array of skeleton states
60 // jacobian: [getJacobianSize()] x [numFrames() * parameterTransform] Jacobian matrix
61 // residual: [getJacobianSize()] residual vector.
63 std::span<const ModelParametersT<T>> modelParameters,
64 std::span<const SkeletonStateT<T>> skelStates,
65 std::span<const MeshStateT<T>> meshStates,
66 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
67 Eigen::Ref<Eigen::VectorX<T>> residual,
68 int& usedRows) const final;
69
70 [[nodiscard]] bool needsMesh() const final {
71 return true;
72 }
73
74 [[nodiscard]] size_t getJacobianSize() const final;
75
80 void addConstraint(int vertexIndex, T weight, const Eigen::Vector3<T>& targetVelocity);
81
84
86 [[nodiscard]] const std::vector<VertexVelocityConstraintT<T>>& getConstraints() const {
87 return constraints_;
88 }
89
91 [[nodiscard]] size_t getNumConstraints() const {
92 return constraints_.size();
93 }
94
96 [[nodiscard]] const Character& getCharacter() const {
97 return character_;
98 }
99
100 static constexpr T kVelocityWeight = 1e-3;
101
102 private:
104 double calculateVelocityGradient(
105 std::span<const SkeletonStateT<T>> skelStates,
106 std::span<const MeshStateT<T>> meshStates,
107 const VertexVelocityConstraintT<T>& constraint,
108 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
109
111 double calculateVelocityJacobian(
112 std::span<const SkeletonStateT<T>> skelStates,
113 std::span<const MeshStateT<T>> meshStates,
114 const VertexVelocityConstraintT<T>& constraint,
115 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
116 Eigen::Ref<Eigen::VectorX<T>> residual,
117 Eigen::Index startRow) const;
118
119 const Character& character_;
120 std::vector<VertexVelocityConstraintT<T>> constraints_;
121};
122
123} // namespace momentum
SequenceErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
double getError(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates) const final
void addConstraint(int vertexIndex, float weight, const Eigen::Vector3< float > &targetVelocity)
size_t getNumConstraints() const
Get the number of constraints.
const Character & getCharacter() const
Get the character reference.
double getJacobian(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) const final
double getGradient(std::span< const ModelParametersT< T > > modelParameters, std::span< const SkeletonStateT< T > > skelStates, std::span< const MeshStateT< T > > meshStates, Eigen::Ref< Eigen::VectorX< T > > gradient) const final
const std::vector< VertexVelocityConstraintT< float > > & getConstraints() const
Shared types used across error function hierarchies.
Definition types.h:28
Vector< T, 3 > Vector3
Definition types.h:236
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.
Constraint structure for vertex velocity.
VertexVelocityConstraintT< T2 > cast() const