-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c01d4d
commit ab3c9fc
Showing
5 changed files
with
141 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include "Types.hpp" | ||
|
||
namespace nlohmann | ||
{ | ||
template <typename T, int nrows, int ncols, int maxdim1, int maxdim2> | ||
struct adl_serializer<Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2>> | ||
{ | ||
static void to_json(json &j, const Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2> &matrix) | ||
{ | ||
if (matrix.rows() > 1) | ||
{ | ||
for (int row = 0; row < matrix.rows(); ++row) | ||
{ | ||
json column = json::array(); | ||
for (int col = 0; col < matrix.cols(); ++col) | ||
{ | ||
column.push_back(matrix(row, col)); | ||
} | ||
j.push_back(column); | ||
} | ||
} | ||
else if (matrix.rows() == 1) | ||
{ | ||
j = json::array(); | ||
for (int col = 0; col < matrix.cols(); ++col) | ||
{ | ||
j.push_back(matrix(0, col)); | ||
} | ||
} | ||
} | ||
|
||
static void from_json(const json &j, Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2> &matrix) | ||
{ | ||
using Scalar = typename Eigen::Matrix<T, nrows, ncols, Eigen::ColMajor, maxdim1, maxdim2>::Scalar; | ||
assert(j.size() > 0); | ||
assert(nrows >= j.size() || nrows == -1); | ||
assert(j.at(0).is_number() || ncols == -1 || ncols >= j.at(0).size()); | ||
|
||
const int n_cols = j.at(0).is_number() ? 1 : j.at(0).size(); | ||
if (nrows == -1 || ncols == -1) | ||
matrix.setZero(j.size(), n_cols); | ||
|
||
for (std::size_t row = 0; row < j.size(); ++row) | ||
{ | ||
const auto& jrow = j.at(row); | ||
if (jrow.is_number()) | ||
matrix(row) = jrow; | ||
else | ||
for (std::size_t col = 0; col < jrow.size(); ++col) | ||
{ | ||
const auto& value = jrow.at(col); | ||
matrix(row, col) = value.get<Scalar>(); | ||
} | ||
} | ||
} | ||
}; | ||
} // namespace nlohmann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters