Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
pose_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
12#include "momentum/common/log.h"
13#include "momentum/math/mesh.h"
14
15#include <fstream>
16#include <limits>
17
18namespace momentum {
19
20PoseShape loadPoseShape(const std::string& filename, const Character& character) {
21 PoseShape result;
22
23 MT_CHECK(character.mesh);
24
25 std::ifstream data(filename, std::ios::in | std::ios::binary);
26 if (!data.is_open()) {
27 return result;
28 }
29
30 // read dimensions
31 uint64_t numRows = 0;
32 uint64_t numJoints = 0;
33 data.read((char*)&numRows, sizeof(numRows));
34 data.read((char*)&numJoints, sizeof(numJoints));
35
36 if (!data.good()) {
37 MT_LOGW("{}: Failed to read dimensions from file {}", __func__, filename);
38 return result;
39 }
40
41 // Validate sizes to prevent excessive allocation from malformed files
42 constexpr uint64_t kMaxDimension = 10'000'000;
43 constexpr uint64_t kMaxStringLength = 10'000;
44 if (numRows > kMaxDimension || numJoints > kMaxDimension) {
45 MT_LOGW(
46 "{}: Unreasonable dimensions in file {}: numRows={}, numJoints={}",
47 __func__,
48 filename,
49 numRows,
50 numJoints);
51 return result;
52 }
53
54 // Guard against integer overflow in numJoints * 4
55 if (numJoints > std::numeric_limits<uint64_t>::max() / 4) {
56 MT_LOGW("{}: Integer overflow in numCols calculation for file {}", __func__, filename);
57 return result;
58 }
59 const uint64_t numCols = numJoints * 4;
60
61 // Guard against integer overflow in numRows * numCols
62 if (numRows != 0 && numCols != 0 && numRows > std::numeric_limits<uint64_t>::max() / numCols) {
63 MT_LOGW("{}: Integer overflow in shape matrix size for file {}", __func__, filename);
64 return result;
65 }
66
68 character.mesh->vertices.size() * 3 == numRows,
69 "{}, {}",
70 character.mesh->vertices.size() * 3,
71 numRows);
72
73 uint64_t count = 0;
74 data.read((char*)&count, sizeof(uint64_t));
75 if (!data.good() || count > kMaxStringLength) {
76 MT_LOGW("{}: Invalid base joint name length in file {}", __func__, filename);
77 return result;
78 }
79 std::string base;
80 base.resize(count);
81 data.read((char*)base.data(), count);
82 if (!data.good()) {
83 MT_LOGW("{}: Failed to read base joint name data from file {}", __func__, filename);
84 return result;
85 }
86 result.baseJoint = character.skeleton.getJointIdByName(base);
89 0 <= result.baseJoint && result.baseJoint < character.skeleton.joints.size(),
90 "Invalid joint index");
91 // NOLINTNEXTLINE(facebook-hte-ParameterUncheckedArrayBounds)
92 result.baseRot = character.skeleton.joints[result.baseJoint].preRotation;
93
94 // load names
95 std::vector<std::string> names(numJoints);
96 for (size_t i = 0; i < numJoints; i++) {
97 data.read((char*)&count, sizeof(uint64_t));
98 if (!data.good() || count > kMaxStringLength) {
99 MT_LOGW("{}: Invalid joint name length in file {}", __func__, filename);
100 return result;
101 }
102 names[i].resize(count);
103 data.read((char*)names[i].data(), count);
104 if (!data.good()) {
105 MT_LOGW("{}: Failed to read joint name data from file {}", __func__, filename);
106 return result;
107 }
108 }
109
110 // read mean shape
111 result.baseShape.resize(numRows);
112 data.read((char*)result.baseShape.data(), sizeof(float) * numRows);
113 if (!data.good()) {
114 MT_LOGW("{}: Failed to read base shape data from file {}", __func__, filename);
115 return result;
116 }
117
118 // add character vertices
119 const Map<const VectorXf> mesh(
120 &character.mesh->vertices[0][0], character.mesh->vertices.size() * 3);
121 result.baseShape += mesh;
122
123 // load shapeVectors
124 result.shapeVectors.resize(numRows, numCols);
125 data.read((char*)result.shapeVectors.data(), sizeof(float) * numRows * numCols);
126 if (!data.good()) {
127 MT_LOGW("{}: Failed to read shape vectors data from file {}", __func__, filename);
128 return result;
129 }
130
131 // generate mapping from names
132 result.jointMap.resize(numJoints);
133 for (size_t i = 0; i < names.size(); i++) {
134 result.jointMap[i] = character.skeleton.getJointIdByName(names[i]);
135 MT_CHECK(result.jointMap[i] != kInvalidIndex);
136 }
137
138 return result;
139}
140
141} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_LOGW(...)
Definition log.h:180
PoseShape loadPoseShape(const std::string &filename, const Character &character)
Loads pose shape data from a binary file.
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Definition types.h:182
Eigen::Map< T > Map
Definition types.h:40
A character model with skeletal structure, mesh, and optional components.
Definition character.h:32
Mesh_u mesh
3D mesh representing the character's surface
Definition character.h:55
Skeleton skeleton
Skeletal structure defining the character's joints and hierarchy.
Definition character.h:36
Computes shape deformations based on joint rotations.
Definition pose_shape.h:20
size_t baseJoint
Index of the joint used as reference for computing relative rotations.
Definition pose_shape.h:22
MatrixXf shapeVectors
Shape deformation vectors for each quaternion component of each joint.
Definition pose_shape.h:41
VectorXf baseShape
Base shape vertices stored as a flat vector [x1,y1,z1,x2,y2,z2,...].
Definition pose_shape.h:35
std::vector< size_t > jointMap
Indices of joints whose rotations drive the shape deformation.
Definition pose_shape.h:30
Quaternionf baseRot
Base rotation used as reference for computing rotation differences.
Definition pose_shape.h:27
size_t getJointIdByName(std::string_view name) const
Returns the index of a joint with the given name, or kInvalidIndex if not found.
Definition skeleton.cpp:25
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
Definition skeleton.h:25