Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
blend_shape.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
12#include <gsl/narrow>
13
14namespace momentum {
15
16template <typename T>
17std::vector<Eigen::Vector3<T>> BlendShape::computeShape(
18 const BlendWeightsT<T>& coefficients) const {
19 std::vector<Eigen::Vector3<T>> result;
20 computeShape(coefficients, result);
21 return result;
22}
23
24template <typename T>
26 const BlendWeightsT<T>& coefficients,
27 std::vector<Eigen::Vector3<T>>& output) const {
29 static_cast<Eigen::Index>(baseShape_.size()) * 3 == shapeVectors_.rows(),
30 "{} is not {}",
31 baseShape_.size() * 3,
32 shapeVectors_.rows());
33
34 output.resize(baseShape_.size());
35 Eigen::Map<Eigen::VectorX<T>> outputVec(&output[0][0], output.size() * 3);
36 const Eigen::Map<const Eigen::VectorXf> baseVec(&baseShape_[0][0], baseShape_.size() * 3);
37
38 outputVec = baseVec.template cast<T>() + computeDeltas<T>(coefficients);
39}
40
43 const float regularization,
44 const VectorXf& weights) const {
46 static_cast<Eigen::Index>(baseShape_.size()) * 3 == shapeVectors_.rows(),
47 "{} is not {}",
48 baseShape_.size() * 3,
49 shapeVectors_.rows());
51 vertices.size() == baseShape_.size(), "{} is not {}", vertices.size(), baseShape_.size());
52
53 const Map<const VectorXf> baseVec(&baseShape_[0][0], baseShape_.size() * 3);
54 const Map<const VectorXf> vertexVec(&vertices[0][0], vertices.size() * 3);
55
56 // Unweighted path: reuse the cached SVD of shapeVectors_ across calls. Note that
57 // `regularization` is ignored here — the SVD-based pseudoinverse already provides
58 // implicit regularization on near-zero singular values.
59 // TODO: regularization is silently ignored on this branch; either honor it (e.g.
60 // damped least squares) or document it on the header.
61 if (weights.size() != static_cast<Eigen::Index>(vertices.size())) {
62 if (!factorizationValid_) {
63 factorization_ =
64 Eigen::JacobiSVD<MatrixXf>(shapeVectors_, Eigen::ComputeThinU | Eigen::ComputeThinV);
65 factorizationValid_ = true;
66 }
67
68 return factorization_.solve(vertexVec - baseVec);
69 } else {
70 // Weighted path: solve the regularized weighted least-squares problem
71 // min ||W (shapeVectors_ x - (vertexVec - baseVec))||^2 + regularization^2 ||x||^2
72 // by stacking the weighted system on top of an identity block (Tikhonov form)
73 // and solving the resulting normal equations with LDLT. Each per-vertex weight is
74 // replicated 3x to cover x/y/z components. The cached SVD is not used here and is
75 // not invalidated, since shapeVectors_ itself is unchanged.
76 VectorXf wts(weights.size() * 3);
77 Eigen::Map<VectorXf, 0, Eigen::Stride<1, 3>>(&wts(0), weights.size()) = weights;
78 Eigen::Map<VectorXf, 0, Eigen::Stride<1, 3>>(&wts(1), weights.size()) = weights;
79 Eigen::Map<VectorXf, 0, Eigen::Stride<1, 3>>(&wts(2), weights.size()) = weights;
80
81 const auto rows = shapeVectors_.rows();
82 const auto cols = shapeVectors_.cols();
83 MatrixXf A(rows + cols, cols);
84 for (int i = 0; i < wts.size(); i++) {
85 A.row(i) = shapeVectors_.row(i) * wts(i);
86 }
87 A.block(rows, 0, cols, cols) = MatrixXf::Identity(cols, cols) * regularization;
88
89 VectorXf b = VectorXf::Zero(rows + cols);
90 b.topRows(rows) = (vertexVec - baseVec).cwiseProduct(wts);
91
92 return (A.transpose() * A).ldlt().solve(A.transpose() * b);
93 }
94}
95
98 const size_t numShapes,
100 : BlendShapeBase(baseShape.size(), numShapes, shapeNames),
101 baseShape_(baseShape.begin(), baseShape.end()),
102 factorizationValid_(false) {}
103
105 const size_t index,
107 std::string_view name) {
108 BlendShapeBase::setShapeVector(index, shape, name);
109
110 // shapeVectors_ has changed, so the cached SVD used by estimateCoefficients
111 // must be recomputed on next use.
112 factorizationValid_ = false;
113}
114
115bool BlendShape::isApprox(const BlendShape& blendShape) const {
116 if (baseShape_.size() != blendShape.getBaseShape().size()) {
117 return false;
118 }
119 for (size_t i = 0; i < baseShape_.size(); i++) {
120 if (!baseShape_[i].isApprox(blendShape.getBaseShape()[i])) {
121 return false;
122 }
123 }
124
125 return shapeVectors_.isApprox(blendShape.getShapeVectors()) &&
126 (factorizationValid_ == blendShape.getFactorizationValid());
127}
128
129template std::vector<Eigen::Vector3<float>> BlendShape::computeShape<float>(
130 const BlendWeightsT<float>& parameters) const;
131template std::vector<Eigen::Vector3<double>> BlendShape::computeShape<double>(
132 const BlendWeightsT<double>& parameters) const;
133
135 const BlendWeightsT<float>& parameters,
136 std::vector<Eigen::Vector3<float>>& output) const;
138 const BlendWeightsT<double>& parameters,
139 std::vector<Eigen::Vector3<double>>& output) const;
140
141} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
gsl::span< T > span
Span type alias using gsl::span (C++17 fallback).
Definition span_compat.h:54
VectorX< float > VectorXf
Definition types.h:209
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
const MatrixXf & getShapeVectors() const
VectorX< T > computeDeltas(const BlendWeightsT< T > &blendWeights) const
Calculates weighted combination of shape vectors.
void setShapeVector(size_t index, momentum::span< const Vector3f > shapeVector, std::string_view name="")
std::vector< Eigen::Vector3< T > > computeShape(const BlendWeightsT< T > &coefficients) const
Applies blend weights to create a deformed shape.
void setShapeVector(size_t index, momentum::span< const Vector3f > shapeVector, std::string_view name="")
Overrides base method to also invalidate the cached SVD factorization used by estimateCoefficients.
VectorXf estimateCoefficients(momentum::span< const Vector3f > vertices, float regularization=1.0f, const VectorXf &weights=VectorXf()) const
Solves for blend weights that best approximate a target shape.
bool isApprox(const BlendShape &blendShape) const
Compares all components of two blend shapes.
bool getFactorizationValid() const
Whether SVD factorization is up-to-date.
Definition blend_shape.h:41
const std::vector< Vector3f > & getBaseShape() const
Definition blend_shape.h:36