Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_normal_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
11
12namespace momentum {
13
18template <typename T>
21 Eigen::Vector3<T> targetPosition = Eigen::Vector3<T>::Zero();
22
24 Eigen::Vector3<T> targetNormal = Eigen::Vector3<T>::Zero();
25
26 VertexNormalDataT() = default;
28 size_t vIndex,
29 const Eigen::Vector3<T>& targetPos,
30 const Eigen::Vector3<T>& targetNorm,
31 float w,
32 const std::string& n = "")
33 : VertexConstraintData(vIndex, w, n), targetPosition(targetPos), targetNormal(targetNorm) {}
34};
35
49template <typename T>
50class VertexNormalErrorFunctionT : public VertexErrorFunctionT<T, VertexNormalDataT<T>, 1> {
51 public:
53 using typename Base::DfdvType;
54 using typename Base::FuncType;
55
69 const Character& character,
70 const ParameterTransform& parameterTransform,
71 T sourceNormalWeight = T(1),
72 T targetNormalWeight = T(0),
73 const T& lossAlpha = GeneralizedLossT<T>::kL2,
74 const T& lossC = T(1),
76
77 ~VertexNormalErrorFunctionT() override = default;
78
80 size_t constrIndex,
81 const SkeletonStateT<T>& state,
82 const MeshStateT<T>& meshState,
83 std::span<const Eigen::Vector3<T>> worldVecs,
84 FuncType& f,
85 std::span<DfdvType> dfdv) const final;
86
89 const ModelParametersT<T>& params,
90 const SkeletonStateT<T>& state,
91 const MeshStateT<T>& meshState,
92 Eigen::Ref<Eigen::VectorX<T>> gradient) override;
93
96 const ModelParametersT<T>& params,
97 const SkeletonStateT<T>& state,
98 const MeshStateT<T>& meshState,
99 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
100 Eigen::Ref<Eigen::VectorX<T>> residual,
101 int& usedRows) override;
102
105 computeAccurateNormalDerivatives_ = enable;
106 }
107
109 [[nodiscard]] bool computeAccurateNormalDerivatives() const {
110 return computeAccurateNormalDerivatives_;
111 }
112
113 private:
116 void buildVertexToFaceAdjacency(const MeshStateT<T>& meshState) const;
117
118 T sourceNormalWeight_;
119 T targetNormalWeight_;
120 bool computeAccurateNormalDerivatives_ = false;
121
124 mutable std::vector<std::vector<size_t>> vertexToFaceAdjacency_;
125 mutable bool vertexToFaceAdjacencyValid_ = false;
126};
127
128} // namespace momentum
static constexpr T kL2
Special-case alpha values that are either at a singularity in the general formula or simplify to a cl...
VertexErrorFunctionT(const Character &character, const ParameterTransform &parameterTransform, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
Eigen::Matrix< T, FuncDim, 1 > FuncType
Eigen::Matrix< T, FuncDim, 3 > DfdvType
double getGradient(const ModelParametersT< T > &params, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) override
Override to handle normal rotation correction terms via SkeletonDerivativeT.
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
Override to handle normal rotation correction terms via SkeletonDerivativeT.
VertexNormalErrorFunctionT(const Character &character, const ParameterTransform &parameterTransform, T sourceNormalWeight=T(1), T targetNormalWeight=T(0), const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1), bool computeAccurateNormalDerivatives=false)
Constructor.
void evalFunction(size_t constrIndex, const SkeletonStateT< T > &state, const MeshStateT< T > &meshState, std::span< const Eigen::Vector3< T > > worldVecs, FuncType &f, std::span< DfdvType > dfdv) const final
Computes the residual and derivatives.
~VertexNormalErrorFunctionT() override=default
VertexErrorFunctionT< float, VertexNormalDataT< float >, 1 > Base
void setComputeAccurateNormalDerivatives(bool enable)
Enable or disable the expensive d(normal)/d(blendShapeWeight) computation.
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
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.
Eigen::Vector3< T > targetPosition
Target position in world space.
VertexNormalDataT(size_t vIndex, const Eigen::Vector3< T > &targetPos, const Eigen::Vector3< T > &targetNorm, float w, const std::string &n="")
Eigen::Vector3< T > targetNormal
Target normal direction (will be flipped if facing away from source normal).