26std::istringstream readOrThrow(
const filesystem::path& filepath) {
27 std::ifstream file(filepath, std::ios::binary | std::ios::ate);
30 std::size_t size =
static_cast<std::size_t
>(file.tellg());
31 file.seekg(0, std::ios::beg);
33 std::string buffer(size,
'\0');
34 MT_THROW_IF(!file.read(buffer.data(), size),
"Failed to read the file.");
36 return std::istringstream(std::move(buffer));
45 int expectedVertices) {
48 numRows != 0 && numCols > std::numeric_limits<uint64_t>::max() / numRows,
49 "Integer overflow in shape vectors size calculation.");
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.");
57 if (expectedShapes > 0) {
58 shapeVectors.conservativeResize(
60 std::min(gsl::narrow_cast<Eigen::Index>(expectedShapes), shapeVectors.cols()));
63 if (expectedVertices > 0) {
64 shapeVectors.conservativeResize(
65 std::min(shapeVectors.rows(), gsl::narrow_cast<Eigen::Index>(expectedVertices * 3)),
74 std::istringstream istr = readOrThrow(filename);
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.");
88 constexpr uint64_t kMaxDimension = 10'000'000;
90 numRows > kMaxDimension || numCols > kMaxDimension,
91 "Unreasonable blend shape base dimensions: numRows={}, numCols={}.",
96 readShapeVectors(data, shapeVectors, numRows, numCols, expectedShapes, expectedVertices);
104loadBlendShape(
const filesystem::path& filename,
int expectedShapes,
int expectedVertices) {
105 std::istringstream istr = readOrThrow(filename);
110 std::vector<Vector3f> baseShape;
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.");
120 constexpr uint64_t kMaxDimension = 10'000'000;
122 numRows > kMaxDimension || numCols > kMaxDimension,
123 "Unreasonable blend shape dimensions: numRows={}, numCols={}.",
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.");
132 if (expectedVertices > 0) {
133 baseShape.resize(std::min(baseShape.size(), gsl::narrow<size_t>(expectedVertices)));
137 readShapeVectors(data, shapeVectors, numRows, numCols, expectedShapes, expectedVertices);
139 BlendShape res(baseShape, shapeVectors.cols());
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());
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());
167 os.write((
char const*)&numRows,
sizeof(numRows));
168 os.write((
char const*)&numCols,
sizeof(numCols));
171 os.write((
char const*)blendShapeBase.
getShapeVectors().data(),
sizeof(
float) * numRows * numCols);
184 os.write((
char const*)&numRows,
sizeof(numRows));
185 os.write((
char const*)&numCols,
sizeof(numCols));
188 os.write((
char const*)blendShape.
getBaseShape().data(),
sizeof(
float) * numRows);
191 os.write((
char const*)blendShape.
getShapeVectors().data(),
sizeof(
float) * numRows * numCols);
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
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
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.
const std::vector< Vector3f > & getBaseShape() const