Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
skeleton_derivative.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 <vector>
21
22namespace momentum {
23
51template <typename T>
53 public:
61 const Skeleton& skeleton,
62 const ParameterTransform& parameterTransform,
63 const VectorX<bool>& activeJointParams,
64 const ParameterSet& enabledParameters);
65
80 template <size_t FuncDim>
82 size_t jointIndex,
83 std::span<const Eigen::Vector3<T>> worldVecs,
84 std::span<const Eigen::Matrix<T, FuncDim, 3>> dfdvs,
85 std::span<const uint8_t> isPoints,
86 const Eigen::Matrix<T, FuncDim, 1>& weightedResidual,
87 const JointStateListT<T>& jointStates,
88 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
89
105 template <size_t FuncDim>
107 size_t jointIndex,
108 std::span<const Eigen::Vector3<T>> worldVecs,
109 std::span<const Eigen::Matrix<T, FuncDim, 3>> dfdvs,
110 std::span<const uint8_t> isPoints,
111 T scale,
112 const JointStateListT<T>& jointStates,
113 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
114 size_t rowIndex) const;
115
141 size_t vertexIndex,
142 const Eigen::Vector3<T>& sourceNormal,
143 const Eigen::Vector3<T>& targetPosition,
144 T scale,
145 const SkeletonStateT<T>& skeletonState,
146 const MeshStateT<T>& meshState,
147 const Character& character,
148 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
149
153 size_t vertexIndex,
154 const Eigen::Vector3<T>& sourceNormal,
155 const Eigen::Vector3<T>& targetPosition,
156 T scale,
157 const SkeletonStateT<T>& skeletonState,
158 const MeshStateT<T>& meshState,
159 const Character& character,
160 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
161 size_t rowIndex) const;
162
183 size_t vertexIndex,
184 const Eigen::Vector3<T>& leverArm,
185 T scale,
186 const SkeletonStateT<T>& skeletonState,
187 const MeshStateT<T>& meshState,
188 const Character& character,
189 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
190
195 size_t vertexIndex,
196 const Eigen::Vector3<T>& leverArm,
197 T scale,
198 std::span<const size_t> adjacentFaces,
199 const SkeletonStateT<T>& skeletonState,
200 const MeshStateT<T>& meshState,
201 const Character& character,
202 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
203
207 size_t vertexIndex,
208 const Eigen::Vector3<T>& leverArm,
209 T scale,
210 const SkeletonStateT<T>& skeletonState,
211 const MeshStateT<T>& meshState,
212 const Character& character,
213 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
214 size_t rowIndex) const;
215
220 size_t vertexIndex,
221 const Eigen::Vector3<T>& leverArm,
222 T scale,
223 std::span<const size_t> adjacentFaces,
224 const SkeletonStateT<T>& skeletonState,
225 const MeshStateT<T>& meshState,
226 const Character& character,
227 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
228 size_t rowIndex) const;
229
249 template <size_t FuncDim>
251 size_t vertexIndex,
252 const Eigen::Vector3<T>& worldPos,
253 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
254 const Eigen::Matrix<T, FuncDim, 1>& weightedResidual,
255 const Eigen::Vector3<T>& sourceNormal,
256 const Eigen::Vector3<T>& targetPosition,
257 T normalCorrectionScale,
258 const SkeletonStateT<T>& skeletonState,
259 const MeshStateT<T>& meshState,
260 const Character& character,
261 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
262
269 template <size_t FuncDim>
271 size_t vertexIndex,
272 const Eigen::Vector3<T>& worldPos,
273 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
274 T scale,
275 const Eigen::Vector3<T>& sourceNormal,
276 const Eigen::Vector3<T>& targetPosition,
277 T normalCorrectionScale,
278 const SkeletonStateT<T>& skeletonState,
279 const MeshStateT<T>& meshState,
280 const Character& character,
281 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
282 size_t rowIndex) const;
283
301 size_t vertexIndex,
302 const Eigen::Vector3<T>& worldPos,
303 const Eigen::Vector3<T>& normal,
304 T weightedResidual,
305 const Eigen::Vector3<T>& sourceNormal,
306 const Eigen::Vector3<T>& targetPosition,
307 T normalCorrectionScale,
308 const SkeletonStateT<T>& skeletonState,
309 const MeshStateT<T>& meshState,
310 const Character& character,
311 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
312
331 size_t vertexIndex,
332 const Eigen::Vector3<T>& worldPos,
333 const Eigen::Vector3<T>& normal,
334 T scale,
335 const Eigen::Vector3<T>& sourceNormal,
336 const Eigen::Vector3<T>& targetPosition,
337 T normalCorrectionScale,
338 const SkeletonStateT<T>& skeletonState,
339 const MeshStateT<T>& meshState,
340 const Character& character,
341 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
342 size_t rowIndex) const;
343
362 template <size_t FuncDim>
364 size_t vertexIndex,
365 const Eigen::Vector3<T>& worldPos,
366 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
367 const Eigen::Matrix<T, FuncDim, 1>& weightedResidual,
368 const SkeletonStateT<T>& skeletonState,
369 const MeshStateT<T>& meshState,
370 const Character& character,
371 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
372
386 size_t vertexIndex,
387 const Eigen::Vector3<T>& weightedResidual,
388 const SkeletonStateT<T>& skeletonState,
389 const MeshStateT<T>& meshState,
390 const Character& character,
391 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
392
407 template <size_t FuncDim>
409 size_t vertexIndex,
410 const Eigen::Vector3<T>& worldPos,
411 const Eigen::Matrix<T, FuncDim, 3>& dfdv,
412 T scale,
413 const SkeletonStateT<T>& skeletonState,
414 const MeshStateT<T>& meshState,
415 const Character& character,
416 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
417 size_t rowIndex) const;
418
432 size_t vertexIndex,
433 T scale,
434 const SkeletonStateT<T>& skeletonState,
435 const MeshStateT<T>& meshState,
436 const Character& character,
437 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
438 size_t rowIndex) const;
439
440 private:
441 const Skeleton& skeleton_;
442 const ParameterTransform& parameterTransform_;
443 const VectorX<bool>& activeJointParams_;
444 const ParameterSet& enabledParameters_;
445};
446
447} // namespace momentum
void accumulateVertexGradient(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, const Eigen::Matrix< T, FuncDim, 1 > &weightedResidual, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates gradient for a skinned mesh vertex.
void accumulateJointGradient(size_t jointIndex, std::span< const Eigen::Vector3< T > > worldVecs, std::span< const Eigen::Matrix< T, FuncDim, 3 > > dfdvs, std::span< const uint8_t > isPoints, const Eigen::Matrix< T, FuncDim, 1 > &weightedResidual, const JointStateListT< T > &jointStates, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates gradient for vectors attached to a single joint.
void accumulateVertexNormalBlendShapeGradient(size_t vertexIndex, const Eigen::Vector3< T > &leverArm, T scale, std::span< const size_t > adjacentFaces, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Overload with pre-built adjacency list to avoid O(numFaces) linear scan.
void accumulateVertexNormalBlendShapeJacobian(size_t vertexIndex, const Eigen::Vector3< T > &leverArm, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Jacobian version of accumulateVertexNormalBlendShapeGradient.
void accumulateVertexGradientWithNormalCorrectionDirect(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Vector3< T > &normal, T weightedResidual, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Scalar-specialized combined vertex gradient + normal rotation correction.
void accumulateVertexNormalCorrectionGradient(size_t vertexIndex, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates the gradient CORRECTION for how a mesh normal rotates with joints.
void accumulateVertexJacobianIdentityDfdv(size_t vertexIndex, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Specialized vertex Jacobian for identity dfdv (FuncDim=3, dfdv=I).
void accumulateVertexJacobianWithNormalCorrectionDirect(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Vector3< T > &normal, T scale, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Scalar-specialized combined vertex Jacobian + normal rotation correction.
void accumulateVertexNormalCorrectionJacobian(size_t vertexIndex, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Jacobian version of accumulateVertexNormalCorrectionGradient.
void accumulateVertexGradientWithNormalCorrection(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, const Eigen::Matrix< T, FuncDim, 1 > &weightedResidual, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Combined vertex gradient + normal rotation correction in a single skinning walk.
void accumulateJointJacobian(size_t jointIndex, std::span< const Eigen::Vector3< T > > worldVecs, std::span< const Eigen::Matrix< T, FuncDim, 3 > > dfdvs, std::span< const uint8_t > isPoints, T scale, const JointStateListT< T > &jointStates, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Accumulates Jacobian for vectors attached to a single joint.
void accumulateVertexJacobianWithNormalCorrection(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, T scale, const Eigen::Vector3< T > &sourceNormal, const Eigen::Vector3< T > &targetPosition, T normalCorrectionScale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Jacobian version of the combined vertex gradient + normal correction method.
void accumulateVertexNormalBlendShapeGradient(size_t vertexIndex, const Eigen::Vector3< T > &leverArm, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Accumulates the gradient contribution from how mesh normals change with blend shape weights.
void accumulateVertexJacobian(size_t vertexIndex, const Eigen::Vector3< T > &worldPos, const Eigen::Matrix< T, FuncDim, 3 > &dfdv, T scale, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Accumulates Jacobian for a skinned mesh vertex.
void accumulateVertexNormalBlendShapeJacobian(size_t vertexIndex, const Eigen::Vector3< T > &leverArm, T scale, std::span< const size_t > adjacentFaces, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::MatrixX< T > > jacobian, size_t rowIndex) const
Overload with pre-built adjacency list to avoid O(numFaces) linear scan.
SkeletonDerivativeT(const Skeleton &skeleton, const ParameterTransform &parameterTransform, const VectorX< bool > &activeJointParams, const ParameterSet &enabledParameters)
Constructs the derivative utility.
void accumulateVertexGradientIdentityDfdv(size_t vertexIndex, const Eigen::Vector3< T > &weightedResidual, const SkeletonStateT< T > &skeletonState, const MeshStateT< T > &meshState, const Character &character, Eigen::Ref< Eigen::VectorX< T > > gradient) const
Specialized vertex gradient for identity dfdv (FuncDim=3, dfdv=I).
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Definition random-inl.h:382
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
std::vector< JointStateT< T > > JointStateListT
Structure describing the state of all joints in a skeleton.
std::bitset< kMaxModelParams > ParameterSet
Bitset for model parameter enablement.
Definition types.h:430
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
Represents the complete state of a skeleton.