Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
projection_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
11
12namespace momentum {
13
34
35template <typename T>
37 // Project a 3d point (after skinning) to (rxz, ryz, z) where
38 // (rx,ry) is the residual.
39 //
40 // This encodes the target 2d point, the view and camera transforms, and the
41 // camera distortion (if any).
42 //
43 // This projection model was copied from the hand IK FullProjectionConstraint.
44 // Ideally we would refactor somehow to make them share more code but
45 // the kinematic models are so different this would take some work.
46 Eigen::Matrix<T, 3, 4> projection; // Projection matrix
47
48 size_t parent{}; // parent joint of the constraint
49 Eigen::Vector3<T> offset = Eigen::Vector3<T>::Zero(); // relative offset to the parent
50 T weight = 1; // constraint weight
51
52 Eigen::Vector2<T> target = Eigen::Vector2<T>::Zero();
53
55};
56
57template <typename T>
59 public:
61 const momentum::Skeleton& skel,
63 T nearClip = T(1));
64
65 [[nodiscard]] double getError(
67 const momentum::SkeletonStateT<T>& state,
68 const momentum::MeshStateT<T>& meshState) final;
71 const momentum::SkeletonStateT<T>& state,
72 const momentum::MeshStateT<T>& meshState,
73 Eigen::Ref<Eigen::VectorX<T>> gradient) final;
76 const momentum::SkeletonStateT<T>& state,
77 const momentum::MeshStateT<T>& meshState,
78 Eigen::Ref<Eigen::MatrixX<T>> jacobian,
79 Eigen::Ref<Eigen::VectorX<T>> residual,
80 int& usedRows) final;
81 [[nodiscard]] size_t getJacobianSize() const final;
82
84
86 constraints_.clear();
87 }
88 [[nodiscard]] bool empty() const {
89 return constraints_.empty();
90 }
91 [[nodiscard]] size_t getNumConstraints() const {
92 return constraints_.size();
93 }
94 void setConstraints(std::vector<ProjectionConstraintDataT<T>> constraints) {
95 constraints_ = std::move(constraints);
96 }
97
98 [[nodiscard]] const std::vector<ProjectionConstraintDataT<T>>& getConstraints() const {
99 return constraints_;
100 }
101
105 return constraints_.at(index);
106 }
107
110 static constexpr T kLegacyWeight = 1e-4;
111
112 protected:
113 std::vector<ProjectionConstraintDataT<T>> constraints_;
114
115 // Projection error is roughly in radians, so a value near 1 is reasonable here:
116 static constexpr T kProjectionWeight = 1.0;
117
118 // Ignore projection constraints involving joints closer than this distance.
119 // Prevents divide-by-zero in the projection matrix and bad behavior close to
120 // the camera.
121 T _nearClip = 1.0f;
122};
123
124} // namespace momentum
void setConstraints(std::vector< ProjectionConstraintDataT< T > > constraints)
ProjectionErrorFunctionT(const momentum::Skeleton &skel, const momentum::ParameterTransform &pt, T nearClip=T(1))
std::vector< ProjectionConstraintDataT< float > > constraints_
double getError(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState) final
ProjectionConstraintDataT< T > & getConstraint(size_t index)
Returns a mutable reference to the constraint at the given index, for in-place target/weight updates.
double getJacobian(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState, Eigen::Ref< Eigen::MatrixX< T > > jacobian, Eigen::Ref< Eigen::VectorX< T > > residual, int &usedRows) final
double getGradient(const momentum::ModelParametersT< T > &params, const momentum::SkeletonStateT< T > &state, const momentum::MeshStateT< T > &meshState, Eigen::Ref< Eigen::VectorX< T > > gradient) final
void addConstraint(const ProjectionConstraintDataT< float > &data)
const std::vector< ProjectionConstraintDataT< T > > & getConstraints() const
SkeletonT< float > Skeleton
Definition fwd.h:263
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
Represents a point attached to a joint in a skeleton.
Definition locator.h:19
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
static ProjectionConstraintDataT< T > createFromLocator(const momentum::Locator &locator)
Represents the complete state of a skeleton.