Skip to content

Commit

Permalink
move json utils to cpp file
Browse files Browse the repository at this point in the history
  • Loading branch information
Huangzizhou committed Nov 23, 2023
1 parent 95f949d commit 3c01d4d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
45 changes: 0 additions & 45 deletions src/polysolve/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,48 +61,3 @@ namespace polysolve
Eigen::SparseMatrix<double> sparse_identity(int rows, int cols);

} // namespace polysolve

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)
{
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);
}
}

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
45 changes: 45 additions & 0 deletions src/polysolve/nonlinear/BoxConstraintSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@

#include <fstream>

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)
{
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);
}
}

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)

Check warning on line 45 in src/polysolve/nonlinear/BoxConstraintSolver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/BoxConstraintSolver.cpp#L45

Added line #L45 was not covered by tests
{
const auto& value = jrow.at(col);
matrix(row, col) = value.get<Scalar>();

Check warning on line 48 in src/polysolve/nonlinear/BoxConstraintSolver.cpp

View check run for this annotation

Codecov / codecov/patch

src/polysolve/nonlinear/BoxConstraintSolver.cpp#L47-L48

Added lines #L47 - L48 were not covered by tests
}
}
}
};
} // namespace nlohmann

namespace polysolve::nonlinear
{

Expand Down

0 comments on commit 3c01d4d

Please sign in to comment.