Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skinned_locator_triangle_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
18#include <vector>
19
20namespace momentum {
21
27 size_t triangleIdx = 0;
28 Eigen::Vector3i vertexIndices;
29};
30
31template <typename T>
33 int locatorIndex = -1;
34 Eigen::Vector3i tgtTriangleIndices;
35 Eigen::Vector3<T> tgtTriangleBaryCoords;
36 T depth = 0;
37 T weight = 1;
38
43 std::vector<CandidateTriangle> candidateTriangles;
44
45 template <typename T2>
48 result.locatorIndex = this->locatorIndex;
49 result.tgtTriangleIndices = this->tgtTriangleIndices;
50 result.tgtTriangleBaryCoords = this->tgtTriangleBaryCoords.template cast<T2>();
51 result.depth = static_cast<T2>(this->depth);
52 result.weight = static_cast<T2>(this->weight);
53 result.candidateTriangles = this->candidateTriangles;
54 return result;
55 }
56};
57
63template <typename T>
65 public:
67 const Character& character,
70
73 delete;
76
77 [[nodiscard]] double getError(
78 const ModelParametersT<T>& modelParameters,
79 const SkeletonStateT<T>& state,
80 const MeshStateT<T>& meshState) final;
81
83 const ModelParametersT<T>& modelParameters,
84 const SkeletonStateT<T>& state,
85 const MeshStateT<T>& meshState,
86 Eigen::Ref<Eigen::VectorX<T>> gradient) final;
87
89 const ModelParametersT<T>& modelParameters,
90 const SkeletonStateT<T>& state,
91 const MeshStateT<T>& meshState,
92 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
93 Eigen::Ref<Eigen::VectorX<T>> residual,
94 int& usedRows) final;
95
96 [[nodiscard]] size_t getJacobianSize() const final;
97
99 int locatorIndex,
100 const Eigen::Vector3i& triangleIndices,
101 const Eigen::Vector3<T>& triangleBaryCoords,
102 float depth = 0,
103 T weight = 1);
105 void setConstraints(const std::vector<SkinnedLocatorTriangleConstraintT<T>>& constraints);
106
107 [[nodiscard]] const std::vector<SkinnedLocatorTriangleConstraintT<T>>& getConstraints() const {
108 return constraints_;
109 }
110
111 [[nodiscard]] const Character* getCharacter() const override {
112 return &character_;
113 }
114
116 [[nodiscard]] bool needsMesh() const override {
117 return true;
118 }
119
120 private:
121 // Get the parameter index for a skinned locator, or -1 if not parameterized
122 [[nodiscard]] int getSkinnedLocatorParameterIndex(int locatorIndex) const {
123 if (locatorIndex <
124 static_cast<int>(character_.parameterTransform.skinnedLocatorParameters.size())) {
125 return character_.parameterTransform.skinnedLocatorParameters[locatorIndex];
126 }
127 return -1;
128 }
129
130 double calculatePositionJacobian(
131 const ModelParametersT<T>& modelParameters,
132 const MeshStateT<T>& meshState,
133 const SkinnedLocatorTriangleConstraintT<T>& constr,
134 Ref<Eigen::MatrixX<T>> jac,
135 Ref<Eigen::VectorX<T>> res) const;
136
137 double calculatePlaneJacobian(
138 const ModelParametersT<T>& modelParameters,
139 const MeshStateT<T>& meshState,
140 const SkinnedLocatorTriangleConstraintT<T>& constr,
141 Ref<Eigen::MatrixX<T>> jac,
142 T& res) const;
143
144 double calculatePositionGradient(
145 const ModelParametersT<T>& modelParameters,
146 const MeshStateT<T>& meshState,
147 const SkinnedLocatorTriangleConstraintT<T>& constr,
148 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
149
150 double calculatePlaneGradient(
151 const ModelParametersT<T>& modelParameters,
152 const MeshStateT<T>& meshState,
153 const SkinnedLocatorTriangleConstraintT<T>& constr,
154 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
155
156 Eigen::Vector3<T> getLocatorRestPosition(const ModelParametersT<T>& modelParams, int locatorIndex)
157 const;
158
159 const Character& character_;
160
161 std::vector<SkinnedLocatorTriangleConstraintT<T>> constraints_;
162
163 const VertexConstraintType constraintType_;
164};
165
166} // namespace momentum
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
SkinnedLocatorTriangleErrorFunctionT & operator=(const SkinnedLocatorTriangleErrorFunctionT &)=delete
double getGradient(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
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
double getError(const ModelParametersT< T > &modelParameters, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) final
const Character * getCharacter() const override
For mesh-based error functions, get access to the underlying character.
SkinnedLocatorTriangleErrorFunctionT(const SkinnedLocatorTriangleErrorFunctionT &other)=delete
SkinnedLocatorTriangleErrorFunctionT & operator=(SkinnedLocatorTriangleErrorFunctionT &&)=delete
SkinnedLocatorTriangleErrorFunctionT(SkinnedLocatorTriangleErrorFunctionT &&)=delete
void addConstraint(int locatorIndex, const Eigen::Vector3i &triangleIndices, const Eigen::Vector3< float > &triangleBaryCoords, float depth=0, float weight=1)
bool needsMesh() const override
Override to indicate this function requires mesh state.
SkinnedLocatorTriangleErrorFunctionT(const Character &character, VertexConstraintType type=VertexConstraintType::Position)
void setConstraints(const std::vector< SkinnedLocatorTriangleConstraintT< float > > &constraints)
const std::vector< SkinnedLocatorTriangleConstraintT< float > > & getConstraints() const
Shared types used across error function hierarchies.
Definition types.h:28
Eigen::Ref< T > Ref
Definition types.h:43
Vector3< std::int32_t > Vector3i
Definition types.h:293
Vector< T, 3 > Vector3
Definition types.h:236
VertexConstraintType
Type of vertex constraint mode.
Represents a candidate triangle for sliding constraints.
Eigen::Vector3i vertexIndices
The 3 vertex indices (cached from mesh.faces).
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
Definition character.h:39
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
VectorXi skinnedLocatorParameters
Parameters that control the rest-space positions of the skinned locators.
Represents the complete state of a skeleton.
SkinnedLocatorTriangleConstraintT< T2 > cast() const