Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
blend_shape_io.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/common/log.h"
14
15#include <gsl/gsl>
16
17#include <fstream>
18#include <istream>
19#include <limits>
20#include <vector>
21
22namespace momentum {
23
24namespace {
25
26std::istringstream readOrThrow(const filesystem::path& filepath) {
27 std::ifstream file(filepath, std::ios::binary | std::ios::ate);
28 MT_THROW_IF(!file, "Failed to open the file.");
29
30 std::size_t size = static_cast<std::size_t>(file.tellg());
31 file.seekg(0, std::ios::beg);
32
33 std::string buffer(size, '\0');
34 MT_THROW_IF(!file.read(buffer.data(), size), "Failed to read the file.");
35
36 return std::istringstream(std::move(buffer));
37}
38
39void readShapeVectors(
40 std::istream& data,
41 MatrixXf& shapeVectors,
42 uint64_t numRows,
43 uint64_t numCols,
44 int expectedShapes,
45 int expectedVertices) {
46 // Guard against integer overflow in numRows * numCols
48 numRows != 0 && numCols > std::numeric_limits<uint64_t>::max() / numRows,
49 "Integer overflow in shape vectors size calculation.");
50
51 // load shapeVectors
52 shapeVectors.resize(numRows, numCols);
53 data.read((char*)shapeVectors.data(), sizeof(float) * numRows * numCols);
54 MT_THROW_IF(!data.good(), "Failed to read shape vectors data.");
55
56 // resize to few shapes
57 if (expectedShapes > 0) {
58 shapeVectors.conservativeResize(
59 shapeVectors.rows(),
60 std::min(gsl::narrow_cast<Eigen::Index>(expectedShapes), shapeVectors.cols()));
61 }
62 // resize vertices
63 if (expectedVertices > 0) {
64 shapeVectors.conservativeResize(
65 std::min(shapeVectors.rows(), gsl::narrow_cast<Eigen::Index>(expectedVertices * 3)),
66 shapeVectors.cols());
67 }
68}
69
70} // namespace
71
73loadBlendShapeBase(const filesystem::path& filename, int expectedShapes, int expectedVertices) {
74 std::istringstream istr = readOrThrow(filename);
75 return loadBlendShapeBase(istr, expectedShapes, expectedVertices);
76}
77
78BlendShapeBase loadBlendShapeBase(std::istream& data, int expectedShapes, int expectedVertices) {
79 MatrixXf shapeVectors;
80
81 // read dimensions
82 uint64_t numRows = 0;
83 uint64_t numCols = 0;
84 data.read((char*)&numRows, sizeof(numRows));
85 data.read((char*)&numCols, sizeof(numCols));
86 MT_THROW_IF(!data.good(), "Failed to read blend shape base dimensions.");
87
88 constexpr uint64_t kMaxDimension = 10'000'000;
90 numRows > kMaxDimension || numCols > kMaxDimension,
91 "Unreasonable blend shape base dimensions: numRows={}, numCols={}.",
92 numRows,
93 numCols);
94
95 // read shape vectors
96 readShapeVectors(data, shapeVectors, numRows, numCols, expectedShapes, expectedVertices);
97
98 BlendShapeBase res(shapeVectors.rows() / 3, shapeVectors.cols());
99 res.setShapeVectors(shapeVectors);
100 return res;
101}
102
103BlendShape
104loadBlendShape(const filesystem::path& filename, int expectedShapes, int expectedVertices) {
105 std::istringstream istr = readOrThrow(filename);
106 return loadBlendShape(istr, expectedShapes, expectedVertices);
107}
108
109BlendShape loadBlendShape(std::istream& data, int expectedShapes, int expectedVertices) {
110 std::vector<Vector3f> baseShape;
111 MatrixXf shapeVectors;
112
113 // read dimensions
114 uint64_t numRows = 0;
115 uint64_t numCols = 0;
116 data.read((char*)&numRows, sizeof(numRows));
117 data.read((char*)&numCols, sizeof(numCols));
118 MT_THROW_IF(!data.good(), "Failed to read blend shape dimensions.");
119
120 constexpr uint64_t kMaxDimension = 10'000'000;
122 numRows > kMaxDimension || numCols > kMaxDimension,
123 "Unreasonable blend shape dimensions: numRows={}, numCols={}.",
124 numRows,
125 numCols);
126
127 // read mean shape
128 baseShape.resize(numRows / 3);
129 data.read((char*)baseShape.data(), sizeof(float) * numRows);
130 MT_THROW_IF(!data.good(), "Failed to read blend shape base shape data.");
131
132 if (expectedVertices > 0) {
133 baseShape.resize(std::min(baseShape.size(), gsl::narrow<size_t>(expectedVertices)));
134 }
135
136 // read shape vectors
137 readShapeVectors(data, shapeVectors, numRows, numCols, expectedShapes, expectedVertices);
138
139 BlendShape res(baseShape, shapeVectors.cols());
140 res.setShapeVectors(shapeVectors);
141 return res;
142}
143
144void saveBlendShapeBase(const filesystem::path& filename, const BlendShapeBase& blendShapeBase) {
145 std::ofstream data(filename, std::ios::out | std::ios::binary);
146 if (!data.is_open()) {
147 MT_LOGW("Failed to open file for writing: {}", filename.string());
148 return;
149 }
150 saveBlendShapeBase(data, blendShapeBase);
151}
152
153void saveBlendShape(const filesystem::path& filename, const BlendShape& blendShape) {
154 std::ofstream data(filename, std::ios::out | std::ios::binary);
155 if (!data.is_open()) {
156 MT_LOGW("Failed to open file for writing: {}", filename.string());
157 return;
158 }
159 saveBlendShape(data, blendShape);
160}
161
162void saveBlendShapeBase(std::ostream& os, const BlendShapeBase& blendShapeBase) {
163 // write dimensions
164 const uint64_t numRows = blendShapeBase.getShapeVectors().rows();
165 const uint64_t numCols = blendShapeBase.getShapeVectors().cols();
166
167 os.write((char const*)&numRows, sizeof(numRows));
168 os.write((char const*)&numCols, sizeof(numCols));
169
170 // write coefficient matrix
171 os.write((char const*)blendShapeBase.getShapeVectors().data(), sizeof(float) * numRows * numCols);
172}
173
174void saveBlendShape(std::ostream& os, const BlendShape& blendShape) {
175 // write dimensions
176 const uint64_t numRows = blendShape.getShapeVectors().rows();
177 const uint64_t numCols = blendShape.getShapeVectors().cols();
178 MT_CHECK(
179 numRows == blendShape.getBaseShape().size() * 3,
180 "{}, {}",
181 numRows,
182 blendShape.getBaseShape().size() * 3);
183
184 os.write((char const*)&numRows, sizeof(numRows));
185 os.write((char const*)&numCols, sizeof(numCols));
186
187 // write mean shape
188 os.write((char const*)blendShape.getBaseShape().data(), sizeof(float) * numRows);
189
190 // write coefficient matrix
191 os.write((char const*)blendShape.getShapeVectors().data(), sizeof(float) * numRows * numCols);
192}
193
194} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
#define MT_LOGW(...)
Definition log.h:180
void saveBlendShape(const filesystem::path &filename, const BlendShape &blendShape)
Saves a blend shape to a file.
BlendShapeBase loadBlendShapeBase(const filesystem::path &filename, int expectedShapes, int expectedVertices)
Loads blend shape vectors from a file without base shape.
BlendShape loadBlendShape(const filesystem::path &filename, int expectedShapes, int expectedVertices)
Loads a blend shape from a file, including base shape and shape vectors.
MatrixX< float > MatrixXf
Definition types.h:82
void saveBlendShapeBase(const filesystem::path &filename, const BlendShapeBase &blendShapeBase)
Saves a blend shape base (shape vectors) to a file.
Skinning class that manages blend shape vectors/offsets.
const MatrixXf & getShapeVectors() const
void setShapeVectors(const MatrixXf &shapeVectors, momentum::span< const std::string > shapeNames={})
Skinning class that combines blend shape vectors with a base shape.
Definition blend_shape.h:19
const std::vector< Vector3f > & getBaseShape() const
Definition blend_shape.h:36