#pragma once #include "meshlib.hpp" #include #include namespace Eigen { template void to_json(nlohmann::json &json, const Eigen::PlainObjectBase &mat) { using Scalar = typename Derived::Scalar; auto rows = mat.rows(); auto cols = mat.cols(); std::vector vals(mat.size()); Map(vals.data(), rows, cols) = mat; json = { {"rows", rows}, {"cols", cols}, {"vals", vals}, }; } template void from_json(const nlohmann::json &j, Derived &d) { using Scalar = typename Derived::Scalar; auto rows = j["rows"].get(); auto cols = j["cols"].get(); auto vals = j["vals"].get>(); d = Map(vals.data(), rows, cols); } } namespace nlohmann { template struct adl_serializer> { static void to_json(json &j, const ml::Mesh &m) { j = { {"points", m.points}, {"cells", m.cells}, }; } static ml::Mesh from_json(const json &j) { return ml::Mesh( j["points"].get(), j["cells"].get() ); } }; } namespace ml { template void write(const ml::Mesh &mesh, std::ostream &&out) { nlohmann::json json = mesh; nlohmann::json::to_msgpack(json, out); } template M_ read(std::istream &&in) { return nlohmann::json::from_msgpack(in).get(); } }