19 const std::string& filename,
20 std::span<const VectorXf> poses,
26 MT_LOGW(
"{}: Cannot save data: motion data is either empty or of wrong dimension", __func__);
31 MatrixXf data(poses[0].size(), poses.size());
32 for (
size_t i = 0; i < poses.size(); i++) {
33 if (poses[i].size() != poses[0].size()) {
34 MT_LOGW(
"{}: Cannot save data: not all poses are of the same dimension", __func__);
38 data.col(i) = poses[i];
41 saveMmo(filename, data, scale, character);
45 const std::string& filename,
49 const MatrixXf& additionalParameters,
50 std::span<const std::string> additionalParameterNames) {
54 "{}: Cannot save data: poses are of wrong dimension. Expected: {}, actual: {}",
63 "{}: Cannot save data: model scale is of wrong dimension. Expected: {}, actual: {}",
69 if (additionalParameters.rows() != gsl::narrow<Eigen::Index>(additionalParameterNames.size())) {
71 "{}: Cannot save data: number of additional parameters don't match. Names: {}, data: {}",
73 additionalParameterNames.size(),
74 additionalParameters.rows());
77 if (additionalParameters.cols() != 0 && additionalParameters.cols() != poses.cols()) {
79 "{}: Cannot save data: additional parameters don't match pose dimensions. Expected: {}, actual {}",
82 additionalParameters.cols());
87 std::vector<std::string> parameterNames;
88 parameterNames.reserve(
91 parameterNames.emplace_back(name);
93 for (
const auto& name : additionalParameterNames) {
94 parameterNames.emplace_back(
"__" + name +
"__");
98 std::vector<std::string> jointNames;
100 jointNames.emplace_back(joint.name);
104 MatrixXf pc =
MatrixXf(poses.rows() + additionalParameters.rows(), poses.cols());
105 pc.topRows(poses.rows()) = poses;
107 if (additionalParameters.rows() > 0) {
108 pc.bottomRows(additionalParameters.rows()) = additionalParameters;
111 saveMmo(filename, pc, scale, parameterNames, jointNames);
115 const std::string& filename,
118 std::span<const std::string> parameterNames,
119 std::span<const std::string> jointNames) {
120 if (poses.rows() != gsl::narrow<Eigen::Index>(parameterNames.size())) {
122 "{}: Cannot save data: poses are of wrong dimension. Expected: {}, actual: {}",
124 parameterNames.size(),
130 "{}: Cannot save data: model scale is of wrong dimension. Expected: {}, actual: {}",
137 std::ofstream fs(filename, std::ios::out | std::ios::binary);
139 MT_LOGW(
"{}: Failed to open file for writing: {}", __func__, filename);
144 data = parameterNames.size();
145 fs.write((
const char*)&data,
sizeof(
size_t));
147 data = jointNames.size();
148 fs.write((
const char*)&data,
sizeof(
size_t));
151 fs.write((
const char*)&data,
sizeof(
size_t));
154 for (
const auto& name : parameterNames) {
156 fs.write((
const char*)&data,
sizeof(
size_t));
157 fs.write((
const char*)name.data(), data);
161 for (
const auto& name : jointNames) {
163 fs.write((
const char*)&data,
sizeof(
size_t));
164 fs.write((
const char*)name.data(), data);
168 fs.write((
const char*)scale.data(), scale.size() *
sizeof(
float));
171 fs.write((
const char*)poses.data(), poses.size() *
sizeof(
float));
177 std::span<const std::string> parameterNames,
178 std::span<const std::string> jointNames,
180 std::tuple<MatrixXf, VectorXf> result;
181 auto& outPoses = std::get<0>(result);
182 auto& outOffset = std::get<1>(result);
185 std::vector<size_t> pMap;
186 for (
const auto& name : parameterNames) {
187 auto iter = std::find(
193 pMap.push_back(index);
197 std::vector<size_t> jMap;
198 jMap.reserve(jointNames.size());
199 for (
const auto& name : jointNames) {
205 for (
size_t i = 0; i < jMap.size(); i++) {
214 for (
size_t i = 0; i < pMap.size(); i++) {
216 outPoses.row(pMap[i]) = poses.row(i);
223std::tuple<MatrixXf, VectorXf>
loadMmo(
const std::string& filename,
const Character& character) {
224 const auto [iposes, iscale, pNames, jNames] =
loadMmo(filename);
231 std::span<const std::string> parameterNames) {
232 std::tuple<MatrixXf, std::vector<std::string>> result;
233 auto& values = std::get<0>(result);
234 auto& names = std::get<1>(result);
236 std::vector<size_t> map;
237 for (
size_t i = 0; i < parameterNames.size(); i++) {
238 const auto& n = parameterNames[i];
239 const auto l = n.length();
240 if (n.size() > 4 && n[0] ==
'_' && n[1] ==
'_' && n[l - 1] ==
'_' && n[l - 2] ==
'_') {
242 names.push_back(n.substr(2, l - 4));
246 values.resize(gsl::narrow<Eigen::Index>(names.size()), poses.cols());
247 for (
size_t i = 0; i < map.size(); i++) {
248 values.row(i) = poses.row(map[i]);
254std::tuple<MatrixXf, VectorXf, std::vector<std::string>, std::vector<std::string>>
loadMmo(
255 const std::string& filename) {
256 std::tuple<MatrixXf, VectorXf, std::vector<std::string>, std::vector<std::string>> result;
257 auto& poses = std::get<0>(result);
258 auto& scale = std::get<1>(result);
259 auto& parameterNames = std::get<2>(result);
260 auto& jointNames = std::get<3>(result);
262 std::ifstream fs(filename, std::ios::in | std::ios::binary);
265 MT_LOGW(
"{}: Failed to open file {}", __func__, filename);
273 fs.read((
char*)&nParams,
sizeof(
size_t));
274 fs.read((
char*)&nJoints,
sizeof(
size_t));
275 fs.read((
char*)&nFrames,
sizeof(
size_t));
278 MT_LOGW(
"{}: Failed to read header from file {}", __func__, filename);
283 constexpr size_t kMaxDimension = 10'000'000;
284 constexpr size_t kMaxStringLength = 10'000;
285 if (nParams > kMaxDimension || nJoints > kMaxDimension || nFrames > kMaxDimension) {
287 "{}: Unreasonable dimensions in file {}: nParams={}, nJoints={}, nFrames={}",
298 MT_LOGW(
"{}: Integer overflow in scale size for file {}", __func__, filename);
301 if (nParams != 0 && nFrames != 0 && nParams > std::numeric_limits<size_t>::max() / nFrames) {
302 MT_LOGW(
"{}: Integer overflow in pose matrix size for file {}", __func__, filename);
307 for (
size_t i = 0; i < nParams; i++) {
310 fs.read((
char*)&count,
sizeof(
size_t));
311 if (!fs.good() || count > kMaxStringLength) {
312 MT_LOGW(
"{}: Invalid parameter name length in file {}", __func__, filename);
317 fs.read((
char*)name.data(), count);
319 MT_LOGW(
"{}: Failed to read parameter name data from file {}", __func__, filename);
322 parameterNames.push_back(name);
326 for (
size_t i = 0; i < nJoints; i++) {
329 fs.read((
char*)&count,
sizeof(
size_t));
330 if (!fs.good() || count > kMaxStringLength) {
331 MT_LOGW(
"{}: Invalid joint name length in file {}", __func__, filename);
336 fs.read((
char*)name.data(), count);
338 MT_LOGW(
"{}: Failed to read joint name data from file {}", __func__, filename);
341 jointNames.push_back(name);
346 fs.read((
char*)scale.data(), scale.size() *
sizeof(
float));
348 MT_LOGW(
"{}: Failed to read scale data from file {}", __func__, filename);
354 fs.read((
char*)poses.data(), poses.size() *
sizeof(
float));
356 MT_LOGW(
"{}: Failed to read pose data from file {}", __func__, filename);
std::tuple< MatrixXf, VectorXf > loadMmo(const std::string &filename, const Character &character)
Load motion data and map to character parameter space.
std::tuple< MatrixXf, std::vector< std::string > > getAuxiliaryDataFromMotion(const MatrixXf &poses, std::span< const std::string > parameterNames)
Extract auxiliary data from motion parameters.
void saveMmo(const std::string &filename, std::span< const VectorXf > poses, const VectorXf &scale, const Character &character)
Save motion data using character parameter mapping.
MatrixXf mapMotionToCharacter(const MotionParameters &inputMotion, const Character &targetCharacter)
Maps the input ModelParameter motion to a target character by matching model parameter names.
constexpr size_t kParametersPerJoint
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
VectorX< float > VectorXf
MatrixX< float > MatrixXf
A character model with skeletal structure, mesh, and optional components.
Skeleton skeleton
Skeletal structure defining the character's joints and hierarchy.
ParameterTransform parameterTransform
Maps model parameters to joint parameters.
size_t getJointIdByName(std::string_view name) const
Returns the index of a joint with the given name, or kInvalidIndex if not found.
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...