Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_projection_error_function.cpp
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
9
12
13namespace momentum {
14
15template <typename T>
17 const Character& character,
18 const ParameterTransform& parameterTransform,
19 const T& lossAlpha,
20 const T& lossC)
22 character,
23 parameterTransform,
24 lossAlpha,
25 lossC) {}
26
27template <typename T>
29 size_t constrIndex,
30 const SkeletonStateT<T>& /*state*/,
31 const MeshStateT<T>& /*meshState*/,
32 std::span<const Eigen::Vector3<T>> worldVecs,
33 FuncType& f,
34 std::span<DfdvType> dfdv) const {
35 MT_CHECK(constrIndex < this->constraints_.size(), "Constraint index out of range");
36 MT_CHECK(worldVecs.size() == 1, "Expected exactly one world vector for vertex projection");
37
38 const auto& constraint = this->constraints_[constrIndex];
39 const Eigen::Vector3<T>& v = worldVecs[0];
40
41 // Project 3D point to 2D using the projection matrix
42 // p_hom = P * [v; 1]
43 Eigen::Vector4<T> vHom;
44 vHom << v, T(1);
45 const Eigen::Vector3<T> pHom = constraint.projectionMatrix * vHom;
46
47 // Near-clip: skip vertices behind the camera (matching legacy _nearClip = 1.0)
48 if (pHom(2) < T(1)) {
49 f.setZero();
50 return;
51 }
52
53 // Perspective divide
54 const T invZ = T(1) / pHom(2);
55 const Eigen::Vector2<T> projected(pHom(0) * invZ, pHom(1) * invZ);
56
57 // Residual: f = projected - target
58 f = projected - constraint.target;
59
60 // Derivative: df/dv = d(p/w)/dv where p = Proj * v_hom
61 if (!dfdv.empty()) {
62 MT_CHECK(dfdv.size() == 1, "Expected exactly one dfdv for vertex projection");
63
64 // df/dv = [d(x/z)/dv; d(y/z)/dv]
65 // Using quotient rule: d(a/b)/dx = (da/dx * b - a * db/dx) / b^2
66 const T invZ2 = invZ * invZ;
67
68 // P[:3, :3] is the 3x3 submatrix of projection matrix
69 const Eigen::Matrix<T, 3, 3> P33 = constraint.projectionMatrix.template leftCols<3>();
70
71 // df/dv row 0: d(x/z)/dv = (P[0,:3] * z - x * P[2,:3]) / z^2
72 // df/dv row 1: d(y/z)/dv = (P[1,:3] * z - y * P[2,:3]) / z^2
73 dfdv[0].row(0) = (P33.row(0) * pHom(2) - pHom(0) * P33.row(2)) * invZ2;
74 dfdv[0].row(1) = (P33.row(1) * pHom(2) - pHom(1) * P33.row(2)) * invZ2;
75 }
76}
77
78// Explicit template instantiations
81
82} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
VertexErrorFunctionT(const Character &character, const ParameterTransform &parameterTransform, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
Error function that constrains mesh vertices to 2D projected positions.
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.
VertexProjectionErrorFunctionT(const Character &character, const ParameterTransform &parameterTransform, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
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
Represents the complete state of a skeleton.
Constraint data for a vertex 2D projection target.