#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 &mat) { using Scalar = typename Derived::Scalar; auto rows = j["rows"].get(); auto cols = j["cols"].get(); auto vals = j["vals"].get>(); mat = Eigen::Map(vals.data(), rows, cols); } } namespace ml { static void to_json(nlohmann::json &json, const ml::MeshBase &mesh) { json = { {"points", mesh.points()}, {"cells", mesh.cells()}, }; } static void from_json(const nlohmann::json &j, ml::DynamicMesh &mesh) { mesh = { j["points"].get(), j["cells"].get(), }; } void write(const ml::MeshBase &mesh, std::ostream &out) { nlohmann::json json = mesh; nlohmann::json::to_msgpack(json, out); } void write(const ml::MeshBase &mesh, const std::string &path) { std::ofstream file(path, std::ios::out | std::ios::binary); write(mesh, file); } ml::DynamicMesh read(std::istream &in) { nlohmann::json json = nlohmann::json::from_msgpack(in); return json.get(); } ml::DynamicMesh read(const std::string &path) { std::ifstream file(path, std::ios::in | std::ios::binary); return read(file); } }