Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
vertex_plane_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
13#include "momentum/math/mesh.h"
14
15namespace momentum {
16
17template <typename T>
19 const Character& character,
20 const ParameterTransform& parameterTransform,
21 const T& lossAlpha,
22 const T& lossC)
24 character,
25 parameterTransform,
26 lossAlpha,
27 lossC) {
28 this->legacyWeight_ = this->kLegacyWeight;
29}
30
31template <typename T>
33 size_t constrIndex,
34 const SkeletonStateT<T>& /*state*/,
35 const MeshStateT<T>& meshState,
36 std::span<const Eigen::Vector3<T>> worldVecs,
37 FuncType& f,
38 std::span<DfdvType> dfdv) const {
39 MT_CHECK(constrIndex < this->constraints_.size(), "Constraint index out of range");
40 MT_CHECK(worldVecs.size() == 1, "Expected exactly one world vector for vertex plane");
42
43 const auto& constraint = this->constraints_[constrIndex];
44
45 // Match legacy sign convention: flip the plane normal if it faces away from
46 // the posed mesh normal at this vertex. This ensures that the signed distance
47 // is consistent regardless of whether the caller provided a normal pointing
48 // toward or away from the mesh surface.
49 const Eigen::Vector3<T> sourceNormal =
50 meshState.posedMesh_->normals[constraint.vertexIndex].template cast<T>();
51 Eigen::Vector3<T> normal = constraint.normal;
52 if (sourceNormal.dot(normal) < T(0)) {
53 normal *= T(-1);
54 }
55
56 // Signed distance from vertex to plane: d = (v - point) ยท normal
57 const T distance = (worldVecs[0] - constraint.point).dot(normal);
58
59 // Half-space constraint: clamp to zero when above the plane
60 if (constraint.above && distance > T(0)) {
61 f(0) = T(0);
62 if (!dfdv.empty()) {
63 dfdv[0].setZero();
64 }
65 } else {
66 f(0) = distance;
67 if (!dfdv.empty()) {
68 MT_CHECK(dfdv.size() == 1, "Expected exactly one dfdv for vertex plane");
69 dfdv[0] = normal.transpose();
70 }
71 }
72}
73
74// Explicit template instantiations
77
78} // namespace momentum
#define MT_CHECK_NOTNULL(ptr,...)
Asserts that ptr is not null; on failure, throws std::runtime_error with the optional fmt-style messa...
Definition checks.h:45
#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 a plane.
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.
VertexPlaneErrorFunctionT(const Character &character, const ParameterTransform &parameterTransform, const T &lossAlpha=GeneralizedLossT< T >::kL2, const T &lossC=T(1))
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Definition random-inl.h:382
FloatP dot(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes dot product of Eigen::Vector3f and 3-vector of packets.
Definition simd.h:191
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Definition types.h:466
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
std::unique_ptr< MeshT< T > > posedMesh_
Posed mesh after the skeleton transforms have been applied.
Definition mesh_state.h:32
Represents the complete state of a skeleton.
Constraint data for a vertex plane target.