Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
blend_shape_base.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
11#include "momentum/common/log.h"
12
13#include <gsl/narrow>
14
15namespace momentum {
16
17template <typename T>
20 blendWeights.size() <= shapeVectors_.cols(),
21 "{} vs {}",
22 blendWeights.size(),
23 shapeVectors_.cols());
24 return shapeVectors_.leftCols(blendWeights.size()).template cast<T>() * blendWeights.v;
25}
26
27template <typename T>
29 const BlendWeightsT<T>& blendWeights,
30 std::vector<Eigen::Vector3<T>>& result) const {
31 const VectorX<T> deltas = computeDeltas(blendWeights);
32 Eigen::Map<Eigen::VectorX<T>> baseShape(&result[0][0], result.size() * 3);
33 baseShape += deltas;
34}
35
37 const size_t modelSize,
38 const size_t numShapes,
40 : shapeVectors_(MatrixXf::Zero(modelSize * 3, numShapes)) {
41 shapeNames_.assign(shapeNames.begin(), shapeNames.end());
42 if (shapeNames_.size() != numShapes) {
43 MT_LOGW_IF(
44 !shapeNames.empty(),
45 "Provided some but not all blendshape names, all shape names will be auto-generated.");
46 shapeNames_.resize(numShapes);
47 for (size_t i = 0; i < numShapes; ++i) {
48 shapeNames_[i] = std::string("shape_") + std::to_string(i);
49 }
50 }
51}
52
54 const MatrixXf& shapeVectors,
56 shapeVectors_ = shapeVectors;
57
58 if (shapeNames.size() == static_cast<size_t>(shapeVectors_.cols())) {
59 shapeNames_.assign(shapeNames.begin(), shapeNames.end());
60 } else {
62 !shapeNames.empty(),
63 "Provided some but not all blendshape names, all shape names will be auto-generated.");
64 shapeNames_.resize(shapeVectors_.cols());
65 for (int i = 0; i < shapeVectors_.cols(); ++i) {
66 shapeNames_[i] = std::string("shape_") + std::to_string(i);
67 }
68 }
69}
70
72 const size_t index,
74 std::string_view name) {
75 MT_CHECK(modelSize() == shape.size(), "{} is not {}", modelSize(), shape.size());
77 static_cast<Eigen::Index>(shape.size()) * 3 == shapeVectors_.rows(),
78 "{} is not {}",
79 shape.size() * 3,
80 shapeVectors_.rows());
82 index < static_cast<size_t>(shapeVectors_.cols()), "{} vs {}", index, shapeVectors_.cols());
83
84 shapeVectors_.col(index) = Map<const VectorXf>(&shape[0][0], shape.size() * 3);
85 if (name.empty()) {
86 shapeNames_[index] = std::string("shape_") + std::to_string(index);
87 } else {
88 shapeNames_[index] = name;
89 }
90}
91
93 const BlendWeightsT<float>& parameters) const;
95 const BlendWeightsT<double>& parameters) const;
96
97template void BlendShapeBase::applyDeltas(
98 const BlendWeightsT<float>& blendWeights,
99 std::vector<Eigen::Vector3<float>>& result) const;
100template void BlendShapeBase::applyDeltas(
101 const BlendWeightsT<double>& blendWeights,
102 std::vector<Eigen::Vector3<double>>& result) const;
103
104} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_LOGW_IF(condition,...)
Definition log.h:186
Vector< T, Dynamic > VectorX
Dynamic-size column vector. Common return type for solver residuals and parameter vectors.
Definition types.h:207
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
Eigen::Map< T > Map
Definition types.h:40
MatrixX< float > MatrixXf
Definition types.h:82
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
VectorX< T > computeDeltas(const BlendWeightsT< T > &blendWeights) const
Calculates weighted combination of shape vectors.
std::vector< std::string > shapeNames_
void setShapeVectors(const MatrixXf &shapeVectors, momentum::span< const std::string > shapeNames={})
void applyDeltas(const BlendWeightsT< T > &blendWeights, std::vector< Eigen::Vector3< T > > &result) const
Adds weighted shape vectors to existing vertices.
void setShapeVector(size_t index, momentum::span< const Vector3f > shapeVector, std::string_view name="")
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE auto size() const
Definition types.h:87