Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
json_utils.h
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
8#pragma once
9
14
15#include <nlohmann/json.hpp>
16#include <Eigen/Core>
17
18#include <optional>
19#include <string>
20
21namespace momentum {
22
23// Converts Eigen matrix or vector types to nlohmann::json
24//
25// Note: We intentionally avoid defining nlohmann::adl_serializers for type conversion between Eigen
26// types and nlohmann::json to prevent multiple definitions of the same type in different projects
27// that are compiled in a single translation unit. Instead, we recommend using explicit conversion
28// between the types using the toJson() and fromJson() functions provided in this header.
29template <typename Derived>
30void toJson(const Eigen::MatrixBase<Derived>& mat, nlohmann::json& j) {
31 // always save new format that's numpy compatible
32 // this works for both dynamic and statically sized matrices
33 j = nlohmann::json::array();
34 for (int col_i = 0; col_i < mat.cols(); ++col_i) {
35 nlohmann::json jcol = nlohmann::json::array();
36 for (int row_i = 0; row_i < mat.rows(); ++row_i) {
37 jcol.push_back(mat(row_i, col_i));
38 }
39 j.push_back(jcol);
40 }
41}
42
43// Converts nlohmann::json to Eigen matrix or vector types
44template <typename T>
45T fromJson(const nlohmann::json& j) {
46 T result;
47
48 // partially dynamic matrix
49 if constexpr (T::RowsAtCompileTime == Eigen::Dynamic || T::ColsAtCompileTime == Eigen::Dynamic) {
50 // dynamic size matrix
51 // reads dimensions from the json
52 const auto numCols = j.size();
53 if (numCols > 0) {
54 const auto numRows = j.at(0).size();
55 result.resize(numRows, numCols);
56
57 std::vector<typename T::Scalar> coef;
58 for (int col_i = 0; col_i < numCols; ++col_i) {
59 coef = j[col_i];
60 std::copy(coef.begin(), coef.end(), result.col(col_i).begin());
61 }
62 }
63 }
64 // fixed size matrix or vector
65 else {
66 const int Rows = T::RowsAtCompileTime;
67 const int Cols = T::ColsAtCompileTime;
68
69 // vector stored as array
70 if (Rows == 1 && Cols != -1 && j.size() == Cols &&
71 j[0].type() != nlohmann::json::value_t::array) {
72 std::vector<typename T::Scalar> coef = j;
73 std::copy(coef.begin(), coef.end(), result.row(0).begin());
74 } else if (
75 Cols == 1 && Rows != -1 && j.size() == Rows &&
76 j[0].type() != nlohmann::json::value_t::array) {
77 std::vector<typename T::Scalar> coef = j;
78 std::copy(coef.begin(), coef.end(), result.col(0).begin());
79 }
80 // fixed size matrix
81 else {
82 const auto numCols = j.size();
83 if (numCols > 0) {
84 for (int col_i = 0; col_i < Cols; ++col_i) {
85 std::vector<typename T::Scalar> coef = j[col_i];
86 std::copy(coef.begin(), coef.end(), result.col(col_i).begin());
87 }
88 }
89 }
90 }
91
92 return result;
93}
94
95// to json conversion functions
96void parameterLimitsToJson(const Character& character, nlohmann::json& j);
97void parameterSetsToJson(const Character& character, nlohmann::json& j);
98void poseConstraintsToJson(const Character& character, nlohmann::json& j);
99void parameterTransformToJson(const Character& character, nlohmann::json& j);
100
108 const JointPhysicalProperties& jointProperties,
109 nlohmann::json& j);
110
115[[nodiscard]] std::string jointPhysicalPropertiesToJsonString(
116 const JointPhysicalProperties& jointProperties);
117
118// from json conversion functions
119ParameterLimits parameterLimitsFromJson(const Character& character, const nlohmann::json& j);
120ParameterSets parameterSetsFromJson(const Character& character, const nlohmann::json& j);
121PoseConstraints poseConstraintsFromJson(const Character& character, const nlohmann::json& j);
122ParameterTransform parameterTransformFromJson(const Character& character, const nlohmann::json& j);
123
129JointPhysicalProperties jointPhysicalPropertiesFromJson(
130 const nlohmann::json& j,
131 const std::string& jointName,
132 size_t jointIndex);
133
140[[nodiscard]] std::optional<JointPhysicalProperties> jointPhysicalPropertiesFromJsonString(
141 const std::string& json,
142 const std::string& jointName,
143 size_t jointIndex);
144
145} // namespace momentum
ParameterLimits parameterLimitsFromJson(const Character &character, const nlohmann::json &j)
std::unordered_map< std::string, ParameterSet > ParameterSets
T fromJson(const nlohmann::json &j)
Definition json_utils.h:45
std::vector< ParameterLimit > ParameterLimits
void toJson(const Eigen::MatrixBase< Derived > &mat, nlohmann::json &j)
Definition json_utils.h:30
void jointPhysicalPropertiesToJson(const JointPhysicalProperties &jointProperties, nlohmann::json &j)
Serializes one joint's physical mass properties.
JointPhysicalProperties jointPhysicalPropertiesFromJson(const nlohmann::json &j, const std::string &jointName, const size_t jointIndex)
Parses one joint's physical mass properties from JSON.
ParameterTransformT< float > ParameterTransform
Definition fwd.h:244
std::optional< JointPhysicalProperties > jointPhysicalPropertiesFromJsonString(const std::string &json, const std::string &jointName, const size_t jointIndex)
Parses one joint's physical mass properties from its canonical on-disk JSON string.
void parameterSetsToJson(const Character &character, nlohmann::json &j)
void poseConstraintsToJson(const Character &character, nlohmann::json &j)
ParameterTransform parameterTransformFromJson(const Character &character, const nlohmann::json &j)
std::string jointPhysicalPropertiesToJsonString(const JointPhysicalProperties &jointProperties)
Serializes one joint's physical mass properties to its canonical on-disk JSON string.
void parameterTransformToJson(const Character &character, nlohmann::json &j)
ParameterSets parameterSetsFromJson(const Character &character, const nlohmann::json &j)
std::unordered_map< std::string, PoseConstraint > PoseConstraints
void parameterLimitsToJson(const Character &character, nlohmann::json &j)
PoseConstraints poseConstraintsFromJson(const Character &character, const nlohmann::json &j)