Copy infrastructure from tc
This commit is contained in:
83
include/ml/meshlib.hpp
Normal file
83
include/ml/meshlib.hpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include <Eigen/Eigen>
|
||||
|
||||
namespace ml {
|
||||
using Matrix1Xui = Eigen::Matrix<unsigned int, 1, Eigen::Dynamic>;
|
||||
using Matrix2Xui = Eigen::Matrix<unsigned int, 2, Eigen::Dynamic>;
|
||||
using Matrix3Xui = Eigen::Matrix<unsigned int, 3, Eigen::Dynamic>;
|
||||
using Matrix4Xui = Eigen::Matrix<unsigned int, 4, Eigen::Dynamic>;
|
||||
|
||||
template<typename PT_, typename CT_>
|
||||
class Mesh {
|
||||
public:
|
||||
using Points = PT_;
|
||||
using Cells = CT_;
|
||||
|
||||
Points points;
|
||||
Cells cells;
|
||||
|
||||
Mesh(Points points, Cells cells)
|
||||
: points(std::move(points)), cells(std::move(cells)) {}
|
||||
};
|
||||
|
||||
auto make_cube(float radius) {
|
||||
Eigen::Matrix3Xf points(3, 8);
|
||||
points.fill(radius);
|
||||
for (int i = 0; i < points.cols(); ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if ((i >> j) & 1) {
|
||||
points(j, i) *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Matrix3Xui cells(3, 12);
|
||||
cells.transpose()
|
||||
<< 0b000, 0b001, 0b010, 0b001, 0b010, 0b011,
|
||||
0b100, 0b101, 0b110, 0b101, 0b110, 0b111,
|
||||
|
||||
0b000, 0b001, 0b100, 0b001, 0b100, 0b101,
|
||||
0b010, 0b011, 0b110, 0b011, 0b110, 0b111,
|
||||
|
||||
0b000, 0b010, 0b100, 0b010, 0b100, 0b110,
|
||||
0b001, 0b011, 0b101, 0b011, 0b101, 0b111;
|
||||
|
||||
return Mesh(points, cells);
|
||||
}
|
||||
|
||||
template<size_t Dim>
|
||||
auto make_cube_wire(float radius) {
|
||||
constexpr size_t NPoints = 1 << Dim;
|
||||
constexpr size_t NCells = Dim * (NPoints >> 1);
|
||||
|
||||
Eigen::Matrix<float, Dim, NPoints> points;
|
||||
points.fill(radius);
|
||||
for (int i = 0; i < points.cols(); ++i) {
|
||||
for (int j = 0; j < Dim; ++j) {
|
||||
if ((i >> j) & 1) {
|
||||
points(j, i) *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Eigen::Matrix<unsigned int, 2, NCells> cells;
|
||||
int k = 0;
|
||||
for (int i = 0; i < NPoints; ++i) {
|
||||
for (int j = 0; j < Dim; ++j) {
|
||||
if ((i >> j) & 1) {
|
||||
cells(0, k) = i;
|
||||
cells(1, k) = i ^ (1 << j);
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Mesh(points, cells);
|
||||
}
|
||||
}
|
||||
68
include/ml/meshlib_json.hpp
Normal file
68
include/ml/meshlib_json.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include "meshlib.hpp"
|
||||
|
||||
#include <Eigen/Eigen>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace Eigen {
|
||||
template<class Derived>
|
||||
void to_json(nlohmann::json &json, const Eigen::PlainObjectBase<Derived> &mat) {
|
||||
using Scalar = typename Derived::Scalar;
|
||||
|
||||
auto rows = mat.rows();
|
||||
auto cols = mat.cols();
|
||||
|
||||
std::vector<Scalar> vals(mat.size());
|
||||
Map<Derived>(vals.data(), rows, cols) = mat;
|
||||
|
||||
json = {
|
||||
{"rows", rows},
|
||||
{"cols", cols},
|
||||
{"vals", vals},
|
||||
};
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
void from_json(const nlohmann::json &j, Derived &d) {
|
||||
using Scalar = typename Derived::Scalar;
|
||||
|
||||
auto rows = j["rows"].get<Index>();
|
||||
auto cols = j["cols"].get<Index>();
|
||||
auto vals = j["vals"].get<std::vector<Scalar>>();
|
||||
|
||||
d = Map<Derived>(vals.data(), rows, cols);
|
||||
}
|
||||
}
|
||||
|
||||
namespace nlohmann {
|
||||
template<typename PT_, typename CT_>
|
||||
struct adl_serializer<ml::Mesh<PT_, CT_>> {
|
||||
static void to_json(json &j, const ml::Mesh<PT_, CT_> &m) {
|
||||
j = {
|
||||
{"points", m.points},
|
||||
{"cells", m.cells},
|
||||
};
|
||||
}
|
||||
|
||||
static ml::Mesh<PT_, CT_> from_json(const json &j) {
|
||||
return ml::Mesh<PT_, CT_>(
|
||||
j["points"].get<PT_>(),
|
||||
j["cells"].get<CT_>()
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace ml {
|
||||
template<typename PT_, typename CT_>
|
||||
void write(const ml::Mesh<PT_, CT_> &mesh, std::ostream &&out) {
|
||||
nlohmann::json json = mesh;
|
||||
nlohmann::json::to_msgpack(json, out);
|
||||
}
|
||||
|
||||
template<typename M_>
|
||||
M_ read(std::istream &&in) {
|
||||
return nlohmann::json::from_msgpack(in).get<M_>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user