Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
point_triangle_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#include <momentum/math/fwd.h>
17
18namespace momentum {
19
20template <typename T>
23 Eigen::Vector3i tgtTriangleIndices;
24 Eigen::Vector3<T> tgtTriangleBaryCoords;
25 T depth = 0;
26 T weight = 1;
27
28 template <typename T2>
30 return {
31 this->srcVertexIndex,
32 this->tgtTriangleIndices,
33 this->tgtTriangleBaryCoords.template cast<T2>(),
34 static_cast<T2>(this->depth),
35 static_cast<T2>(this->weight)};
36 }
37};
38
44template <typename T>
46 public:
48 const Character& character,
55 default;
56
57 [[nodiscard]] double getError(
58 const ModelParametersT<T>& modelParameters,
59 const SkeletonStateT<T>& state,
60 const MeshStateT<T>& meshState) final;
61
63 const ModelParametersT<T>& modelParameters,
64 const SkeletonStateT<T>& state,
65 const MeshStateT<T>& meshState,
66 Eigen::Ref<Eigen::VectorX<T>> gradient) final;
67
69 const ModelParametersT<T>& modelParameters,
70 const SkeletonStateT<T>& state,
71 const MeshStateT<T>& meshState,
72 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
73 Eigen::Ref<Eigen::VectorX<T>> residual,
74 int& usedRows) final;
75
76 [[nodiscard]] size_t getJacobianSize() const final;
77
79 int vertexIndex,
80 const Eigen::Vector3i& triangleIndices,
81 const Eigen::Vector3<T>& triangleBaryCoords,
82 float depth = 0,
83 T weight = 1);
85
86 [[nodiscard]] const std::vector<PointTriangleVertexConstraintT<T>>& getConstraints() const {
87 return constraints_;
88 }
89
92
93 [[nodiscard]] size_t getNumVertices() const;
94 [[nodiscard]] const Character* getCharacter() const override {
95 return &character_;
96 }
97
99 [[nodiscard]] bool needsMesh() const override {
100 return true;
101 }
102
103 private:
104 double calculatePositionJacobian(
105 const ModelParametersT<T>& modelParameters,
106 const SkeletonStateT<T>& state,
107 const MeshStateT<T>& meshState,
109 Ref<Eigen::MatrixX<T>> jac,
110 Ref<Eigen::VectorX<T>> res) const;
111
112 double calculateNormalJacobian(
113 const ModelParametersT<T>& modelParameters,
114 const SkeletonStateT<T>& state,
115 const MeshStateT<T>& meshState,
117 T sourceNormalWeight,
118 T targetNormalWeight,
119 Ref<Eigen::MatrixX<T>> jac,
120 T& res) const;
121
122 double calculatePositionGradient(
123 const ModelParametersT<T>& modelParameters,
124 const SkeletonStateT<T>& state,
125 const MeshStateT<T>& meshState,
127 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
128
129 double calculateNormalGradient(
130 const ModelParametersT<T>& modelParameters,
131 const SkeletonStateT<T>& state,
132 const MeshStateT<T>& meshState,
134 T sourceNormalWeight,
135 T targetNormalWeight,
136 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
137
138 std::pair<T, T> computeNormalWeights() const;
139
140 const Character& character_;
141
142 std::vector<PointTriangleVertexConstraintT<T>> constraints_;
143
144 const VertexConstraintType constraintType_;
145};
146
147} // namespace momentum
static constexpr T kLegacyWeight
Default constant weight in MarkerErrorFunction.
PointTriangleVertexErrorFunctionT(const Character &character, VertexConstraintType type=VertexConstraintType::Position)
double getError(const ModelParametersT< float > &modelParameters, const SkeletonStateT< float > &state, const MeshStateT< float > &meshState) final
bool needsMesh() const override
Override to indicate this function requires mesh state.
const Character * getCharacter() const override
For mesh-based error functions, get access to the underlying character.
const std::vector< PointTriangleVertexConstraintT< float > > & getConstraints() const
double getGradient(const ModelParametersT< float > &modelParameters, const SkeletonStateT< float > &state, const MeshStateT< float > &meshState, Eigen::Ref< Eigen::VectorX< float > > gradient) final
double getJacobian(const ModelParametersT< float > &modelParameters, const SkeletonStateT< float > &state, const MeshStateT< float > &meshState, Eigen::Ref< Eigen::MatrixX< float > > jacobian, Eigen::Ref< Eigen::VectorX< float > > residual, int &usedRows) final
PointTriangleVertexErrorFunctionT & operator=(const PointTriangleVertexErrorFunctionT &)=default
PointTriangleVertexErrorFunctionT(PointTriangleVertexErrorFunctionT &&) noexcept=default
PointTriangleVertexErrorFunctionT(const PointTriangleVertexErrorFunctionT &)=default
void addConstraint(int vertexIndex, const Eigen::Vector3i &triangleIndices, const Eigen::Vector3< float > &triangleBaryCoords, float depth=0, float weight=1)
static constexpr T kLegacyWeight
Default constant weight in MarkerErrorFunction.
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
Shared types used across error function hierarchies.
Definition types.h:28
Eigen::Ref< T > Ref
Definition types.h:43
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
Vector3< std::int32_t > Vector3i
Definition types.h:293
Vector< T, 3 > Vector3
Definition types.h:236
VertexConstraintType
Type of vertex constraint mode.
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
PointTriangleVertexConstraintT< T2 > cast() const
Represents the complete state of a skeleton.