Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_sdf_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
13#include <momentum/math/fwd.h>
14
15#include <vector>
16
17namespace momentum {
18
20template <typename T>
22 int vertexIndex = -1;
23 T targetDistance = T(0);
24 T weight = T(1);
25};
26
32template <typename T>
34 public:
36 static constexpr T kVertexSDFWeight = T(5e-3);
37
43 const Character& character,
44 const SDFColliderT<float>& sdfCollider);
45
47 ~VertexSDFErrorFunctionT() override = default;
48
50 void addConstraint(const VertexSDFConstraintT<T>& constraint);
51
54
56 [[nodiscard]] const std::vector<VertexSDFConstraintT<T>>& getConstraints() const;
57
59 [[nodiscard]] size_t getNumConstraints() const;
60
62 [[nodiscard]] double getError(
63 const ModelParametersT<T>& params,
64 const SkeletonStateT<T>& state,
65 const MeshStateT<T>& meshState) override;
66
69 const ModelParametersT<T>& params,
70 const SkeletonStateT<T>& state,
71 const MeshStateT<T>& meshState,
72 Eigen::Ref<Eigen::VectorX<T>> gradient) override;
73
76 const ModelParametersT<T>& params,
77 const SkeletonStateT<T>& state,
78 const MeshStateT<T>& meshState,
79 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
80 Eigen::Ref<Eigen::VectorX<T>> residual,
81 int& usedRows) override;
82
84 [[nodiscard]] size_t getJacobianSize() const override;
85
87 [[nodiscard]] bool needsMesh() const override {
88 return true;
89 }
90
91 private:
93 [[nodiscard]] TransformT<T> computeWorldToSDFTransform(const SkeletonStateT<T>& state) const;
94
96 [[nodiscard]] T sampleSDFDistanceWithTransform(
97 const Eigen::Vector3<T>& worldPos,
98 const TransformT<T>& worldToSdfTransform) const;
99
101 [[nodiscard]] std::pair<T, Eigen::Vector3<T>> sampleSDFDistanceAndGradientWithTransform(
102 const Eigen::Vector3<T>& worldPos,
103 const TransformT<T>& worldToSdfTransform,
104 const TransformT<T>& sdfToWorldTransform) const;
105
107 void accumulateJointGradient(
108 const JointStateT<T>& jointState,
109 size_t paramIndex,
110 const Eigen::Vector3<T>& posd,
111 const Eigen::Vector3<T>& direction,
112 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
113
115 void accumulateJointJacobian(
116 const JointStateT<T>& jointState,
117 size_t paramIndex,
118 const Eigen::Vector3<T>& posd,
119 const Eigen::Vector3<T>& direction,
120 Eigen::Ref<Eigen::MatrixX<T>> jacobianRow) const;
121
123 void accumulateColliderHierarchyGradient(
124 const SkeletonStateT<T>& state,
125 size_t colliderParent,
126 size_t commonAncestor,
127 const Eigen::Vector3<T>& sdfSurfacePoint,
128 const Eigen::Vector3<T>& direction,
129 Eigen::Ref<Eigen::VectorX<T>> gradient) const;
130
132 void accumulateColliderHierarchyJacobian(
133 const SkeletonStateT<T>& state,
134 size_t colliderParent,
135 size_t commonAncestor,
136 const Eigen::Vector3<T>& sdfSurfacePoint,
137 const Eigen::Vector3<T>& direction,
138 Eigen::Ref<Eigen::MatrixX<T>> jacobianRow) const;
139
140 const Character& character_;
141 SDFColliderT<float> sdfCollider_;
142 std::vector<VertexSDFConstraintT<T>> constraints_;
143};
144
145using VertexSDFErrorFunction = VertexSDFErrorFunctionT<float>;
146using VertexSDFErrorFunctiond = VertexSDFErrorFunctionT<double>;
147
148} // namespace momentum
SkeletonErrorFunctionT(const Skeleton &skel, const ParameterTransform &pt)
~VertexSDFErrorFunctionT() override=default
Destructor.
VertexSDFErrorFunctionT(const Character &character, const SDFColliderT< float > &sdfCollider)
Constructor.
size_t getJacobianSize() const override
Returns the maximum number of Jacobian rows needed.
void clearConstraints()
Clear all constraints.
size_t getNumConstraints() const
Returns the number of constraints.
void addConstraint(const VertexSDFConstraintT< T > &constraint)
Add a constraint specifying a target distance for a vertex.
double getError(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState) override
Computes the error for the given model parameters and skeleton state.
bool needsMesh() const override
Returns true because this error function requires mesh state.
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) override
Computes the error and its gradient.
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 and residual.
const std::vector< VertexSDFConstraintT< T > > & getConstraints() const
Returns the current constraints.
VertexSDFErrorFunctionT< double > VertexSDFErrorFunctiond
Definition fwd.h:1291
VertexSDFErrorFunctionT< float > VertexSDFErrorFunction
Definition fwd.h:1290
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Represents the state of a joint with local and global transformations.
Definition joint_state.h:50
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
SDF collision geometry for character collision detection.
Represents the complete state of a skeleton.
Lightweight transform with separate rotation, translation, and uniform scaling.
Definition transform.h:36
Constraint specifying that a vertex should be at a target signed distance from the SDF.