Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
mesh_state.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
17
18namespace momentum {
19
20template <typename T>
22 const ModelParametersT<T>& parameters,
23 const SkeletonStateT<T>& state,
24 const Character& character) noexcept {
25 update(parameters, state, character);
26}
27
28template <typename T>
29MeshStateT<T>::~MeshStateT() noexcept = default;
30
31template <typename T>
32MeshStateT<T>::MeshStateT(const MeshStateT& other) {
33 if (other.neutralMesh_) {
34 neutralMesh_ = std::make_unique<MeshT<T>>(*other.neutralMesh_);
35 }
36 if (other.restMesh_) {
37 restMesh_ = std::make_unique<MeshT<T>>(*other.restMesh_);
38 }
39 if (other.posedMesh_) {
40 posedMesh_ = std::make_unique<MeshT<T>>(*other.posedMesh_);
41 }
42}
43
44template <typename T>
46 if (this != &other) {
47 *this = MeshStateT(other);
48 }
49 return *this;
50}
51
52template <typename T>
54 const ModelParametersT<T>& parameters,
55 const SkeletonStateT<T>& state,
56 const Character& character) {
58
59 // Lazily allocate the three mesh buffers on the first update; subsequent calls
60 // reuse them in place.
61 if (!restMesh_ && character.mesh) {
62 neutralMesh_ = std::make_unique<MeshT<T>>(character.mesh->template cast<T>());
63 restMesh_ = std::make_unique<MeshT<T>>(character.mesh->template cast<T>());
64 posedMesh_ = std::make_unique<MeshT<T>>(character.mesh->template cast<T>());
65 }
66
67 if (!restMesh_ || !posedMesh_) {
68 return;
69 }
70
71 bool doUpdateNormals = false;
72
73 if (character.blendShape) {
74 const BlendWeightsT<T> blendWeights =
75 extractBlendWeights(character.parameterTransform, parameters);
76 character.blendShape->computeShape(blendWeights, restMesh_->vertices);
77 doUpdateNormals = true;
78 }
79
80 if (character.faceExpressionBlendShape) {
81 if (!character.blendShape) {
82 // Reset restMesh to neutral so previously applied face expressions don't accumulate.
83 // When a shape blendShape is present, the block above already overwrites restMesh
84 // and this reset is unnecessary.
85 Eigen::Map<Eigen::VectorX<T>> outputVec(
86 &restMesh_->vertices[0][0], restMesh_->vertices.size() * 3);
87 const Eigen::Map<Eigen::VectorX<T>> baseVec(
88 &neutralMesh_->vertices[0][0], neutralMesh_->vertices.size() * 3);
89 outputVec = baseVec.template cast<T>();
90 }
91 const BlendWeightsT<T> faceExpressionBlendWeights =
93 character.faceExpressionBlendShape->applyDeltas(
94 faceExpressionBlendWeights, restMesh_->vertices);
95 doUpdateNormals = true;
96 }
97
98 if (doUpdateNormals) {
99 restMesh_->updateNormals();
100 }
101
102 applySSD(
103 cast<T>(character.inverseBindPose), *character.skinWeights, *restMesh_, state, *posedMesh_);
104}
105
106template struct MeshStateT<float>;
107template struct MeshStateT<double>;
108
109} // namespace momentum
This file provides functions for applying blend shape deformations and skeletal transformations to ch...
Functions for linear blend skinning (Skeletal Subspace Deformation).
BlendWeightsT< T > extractFaceExpressionBlendWeights(const ParameterTransform &paramTransform, const ModelParametersT< T > &modelParams)
Extracts facial expression blend shape weights from model parameters.
std::vector< Vector3< T > > applySSD(const SkinWeights &skin, typename DeduceSpanType< const Vector3< T > >::type points, typename DeduceSpanType< const Eigen::Matrix4< T > >::type skinningTransforms)
Applies forward SSD (linear blend skinning) to points using precomputed skinning transforms.
BlendWeightsT< T > extractBlendWeights(const ParameterTransform &paramTransform, const ModelParametersT< T > &modelParams)
Extracts blend shape weights from model parameters.
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
Profiling macros that dispatch to one of three backends, selected at compile time:
#define MT_PROFILE_FUNCTION()
Definition profile.h:110
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
TransformationList inverseBindPose
Inverse of the bind pose transformations for each joint.
Definition character.h:76
SkinWeights_u skinWeights
Defines how mesh vertices are influenced by joints.
Definition character.h:58
BlendShapeBase_const_p faceExpressionBlendShape
Facial expression blend shapes.
Definition character.h:73
Mesh_u mesh
3D mesh representing the character's surface
Definition character.h:55
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
Definition character.h:39
BlendShape_const_p blendShape
Shape variations that can be blended.
Definition character.h:70
Represents the complete state of a model's mesh.
Definition mesh_state.h:24
std::unique_ptr< MeshT< T > > neutralMesh_
Rest mesh without the facial expression basis, used to restore the neutral shape after facial express...
Definition mesh_state.h:27
void update(const ModelParametersT< T > &parameters, const SkeletonStateT< T > &state, const Character &character)
Updates the mesh state based on model parameters and skeleton state.
~MeshStateT() noexcept
MeshStateT & operator=(const MeshStateT &other)
std::unique_ptr< MeshT< T > > posedMesh_
Posed mesh after the skeleton transforms have been applied.
Definition mesh_state.h:32
std::unique_ptr< MeshT< T > > restMesh_
Rest positions of the mesh after the shape basis (and potentially facial expression basis) has been a...
Definition mesh_state.h:30
MeshStateT() noexcept=default
Creates an empty mesh state.
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.