Skip to content

Commit

Permalink
refactored reader
Browse files Browse the repository at this point in the history
  • Loading branch information
teseoch committed Oct 20, 2023
1 parent 0b7a2ce commit 5d02099
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 120 deletions.
3 changes: 1 addition & 2 deletions components/wmtk_components/delaunay/delaunay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void delaunay_exec(
PointMesh point_cloud;
{
const std::filesystem::path& file = files[options.input];
MeshReader reader(file);
reader.read(point_cloud);
MeshReader::read(file, point_cloud);
}

// make sure dimensions fit
Expand Down
14 changes: 3 additions & 11 deletions components/wmtk_components/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ void input(const nlohmann::json& j, std::map<std::string, std::filesystem::path>
case 0: {
// point-cloud
PointMesh mesh;
if (options.file.extension() == ".hdf5") {
MeshReader reader(options.file);
reader.read(mesh);
} else {
throw std::runtime_error(std::string("Unknown file type: ") + options.file.string());
{
MeshReader::read(options.file, mesh);
}

const std::filesystem::path cache_dir = "cache";
Expand All @@ -50,12 +47,7 @@ void input(const nlohmann::json& j, std::map<std::string, std::filesystem::path>
case 2: {
// triangle mesh
TriMesh mesh;
if (options.file.extension() == ".hdf5") {
MeshReader reader(options.file);
reader.read(mesh);
} else {
throw std::runtime_error(std::string("Unknown file type: ") + options.file.string());
}
MeshReader::read(options.file, mesh);

const std::filesystem::path cache_dir = "cache";
std::filesystem::create_directory(cache_dir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void isotropic_remeshing(
TriMesh mesh;
{
const std::filesystem::path& file = files[options.input];
MeshReader reader(file);
reader.read(mesh);
MeshReader::read(file, mesh);
}

if (options.length_abs < 0) {
Expand Down
5 changes: 1 addition & 4 deletions components/wmtk_components/mesh_info/mesh_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ void mesh_info(const nlohmann::json& j, std::map<std::string, std::filesystem::p
const std::filesystem::path& file = files[options.input];

TriMesh mesh;
{
MeshReader reader(file);
reader.read(mesh);
}
MeshReader::read(file, mesh);

const auto v_tuples = mesh.get_all(PrimitiveType::Vertex);
const auto f_tuples = mesh.get_all(PrimitiveType::Face);
Expand Down
6 changes: 2 additions & 4 deletions components/wmtk_components/output/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ void output(const nlohmann::json& j, std::map<std::string, std::filesystem::path
TriMesh mesh;
{
const std::filesystem::path& file = files[options.input];
MeshReader reader(file);
reader.read(mesh);
MeshReader::read(file, mesh);
}

if (options.file.extension().empty()) {
Expand All @@ -39,8 +38,7 @@ void output(const nlohmann::json& j, std::map<std::string, std::filesystem::path
TetMesh mesh;
{
const std::filesystem::path& file = files[options.input];
MeshReader reader(file);
reader.read(mesh);
MeshReader::read(file, mesh);
}

if (options.file.extension().empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/wmtk/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Mesh : public std::enable_shared_from_this<Mesh>
template <typename T>
friend class attribute::TupleAccessor;
friend class ParaviewWriter;
friend class MeshReader;
friend class HDF5Reader;
friend class MultiMeshManager;

virtual long top_cell_dimension() const = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/wmtk/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

set(SRC_FILES
HDF5Reader.hpp
HDF5Reader.cpp
HDF5Writer.hpp
HDF5Writer.cpp
MeshWriter.hpp
Expand Down
83 changes: 83 additions & 0 deletions src/wmtk/io/HDF5Reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "HDF5Reader.hpp"

#include <wmtk/Mesh.hpp>
#include <wmtk/utils/Logger.hpp>
#include <wmtk/utils/Rational.hpp>

#include <h5pp/h5pp.h>

#include <regex>


namespace wmtk {
HDF5Reader::HDF5Reader() {}


void HDF5Reader::read_aux(const std::filesystem::path& filename, Mesh& mesh)
{
h5pp::File m_hdf5_file(filename, h5pp::FileAccess::READONLY);

std::vector<long> capacities =
m_hdf5_file.readAttribute<std::vector<long>>("WMTK", "capacities");

mesh.set_capacities(capacities);

const auto dsets = m_hdf5_file.findDatasets("", "WMTK");
for (auto& s : dsets) {
const std::string dataset = "WMTK/" + s;
const long stride = m_hdf5_file.readAttribute<long>(dataset, "stride");
const long dimension = m_hdf5_file.readAttribute<long>(dataset, "dimension");
const std::string type = m_hdf5_file.readAttribute<std::string>(dataset, "type");
const std::string name =
std::regex_replace(s, std::regex(std::to_string(dimension) + "/"), "");

auto pt = PrimitiveType(dimension);

if (type == "long") {
auto v = m_hdf5_file.readDataset<std::vector<long>>(dataset);
set_attribute<long>(name, pt, stride, v, mesh);
} else if (type == "char") {
auto tmp = m_hdf5_file.readDataset<std::vector<short>>(dataset);
std::vector<char> v;
v.reserve(tmp.size());
for (auto val : tmp) v.push_back(char(val));

set_attribute<char>(name, pt, stride, v, mesh);
} else if (type == "double") {
auto v = m_hdf5_file.readDataset<std::vector<double>>(dataset);

set_attribute<double>(name, pt, stride, v, mesh);
} else if (type == "rational") {
logger().error("We currently do not support reading rationals");
assert(false); //
auto tmp = m_hdf5_file.readDataset<std::vector<std::array<std::string, 2>>>(dataset);

std::vector<Rational> v;
v.reserve(tmp.size());
for (auto val : tmp) v.emplace_back(val[0], val[1]);

// set_attribute<Rational>(name, pt, stride, v, mesh);

} else {
logger().error("We currently do not support reading the type \"{}\"", type);
assert(false);
}
}
}

template <typename T>
void HDF5Reader::set_attribute(
const std::string& name,
PrimitiveType pt,
long stride,
const std::vector<T>& v,
Mesh& mesh)
{
auto handle = mesh.register_attribute<T>(name, pt, stride, true);
auto accessor = attribute::AccessorBase<T>(mesh, handle);

accessor.set_attribute(v);
}


} // namespace wmtk
31 changes: 31 additions & 0 deletions src/wmtk/io/HDF5Reader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "MeshReader.hpp"

#include <wmtk/Primitive.hpp>

#include <filesystem>
#include <vector>

namespace wmtk {

class Mesh;

class HDF5Reader : public MeshReader
{
public:
HDF5Reader();

protected:
void read_aux(const std::filesystem::path& filename, Mesh& mesh) override;

private:
template <typename T>
void set_attribute(
const std::string& name,
PrimitiveType pt,
long stride,
const std::vector<T>& v,
Mesh& mesh);
};
} // namespace wmtk
84 changes: 12 additions & 72 deletions src/wmtk/io/MeshReader.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,25 @@
#include "MeshReader.hpp"

#include <wmtk/Mesh.hpp>
#include <wmtk/utils/Logger.hpp>
#include <wmtk/utils/Rational.hpp>

#include <h5pp/h5pp.h>

#include <regex>
#include "HDF5Reader.hpp"

#include <memory>

namespace wmtk {
MeshReader::MeshReader(const std::filesystem::path& filename)
: m_filename(filename)
{}


void MeshReader::read(Mesh& mesh)
void MeshReader::read(const std::filesystem::path& filename, Mesh& mesh)
{
h5pp::File m_hdf5_file(m_filename, h5pp::FileAccess::READONLY);

std::vector<long> capacities =
m_hdf5_file.readAttribute<std::vector<long>>("WMTK", "capacities");

mesh.set_capacities(capacities);

const auto dsets = m_hdf5_file.findDatasets("", "WMTK");
for (auto& s : dsets) {
const std::string dataset = "WMTK/" + s;
const long stride = m_hdf5_file.readAttribute<long>(dataset, "stride");
const long dimension = m_hdf5_file.readAttribute<long>(dataset, "dimension");
const std::string type = m_hdf5_file.readAttribute<std::string>(dataset, "type");
const std::string name =
std::regex_replace(s, std::regex(std::to_string(dimension) + "/"), "");

auto pt = PrimitiveType(dimension);

if (type == "long") {
auto v = m_hdf5_file.readDataset<std::vector<long>>(dataset);
set_attribute<long>(name, pt, stride, v, mesh);
} else if (type == "char") {
auto tmp = m_hdf5_file.readDataset<std::vector<short>>(dataset);
std::vector<char> v;
v.reserve(tmp.size());
for (auto val : tmp) v.push_back(char(val));

set_attribute<char>(name, pt, stride, v, mesh);
} else if (type == "double") {
auto v = m_hdf5_file.readDataset<std::vector<double>>(dataset);

set_attribute<double>(name, pt, stride, v, mesh);
} else if (type == "rational") {
logger().error("We currently do not support reading rationals");
assert(false); //
auto tmp = m_hdf5_file.readDataset<std::vector<std::array<std::string, 2>>>(dataset);

std::vector<Rational> v;
v.reserve(tmp.size());
for (auto val : tmp) v.emplace_back(val[0], val[1]);

// set_attribute<Rational>(name, pt, stride, v, mesh);

} else {
logger().error("We currently do not support reading the type \"{}\"", type);
assert(false);
}
std::unique_ptr<MeshReader> reader = nullptr;
const auto extension = filename.extension().string();
if (extension == ".hdf5") {
reader = std::make_unique<HDF5Reader>();
}
}

template <typename T>
void MeshReader::set_attribute(
const std::string& name,
PrimitiveType pt,
long stride,
const std::vector<T>& v,
Mesh& mesh)
{
auto handle = mesh.register_attribute<T>(name, pt, stride, true);
auto accessor = attribute::AccessorBase<T>(mesh, handle);

accessor.set_attribute(v);
if (!reader)
throw std::runtime_error(extension + " not supported");
else
reader->read_aux(filename, mesh);
}


} // namespace wmtk
} // namespace wmtk
19 changes: 4 additions & 15 deletions src/wmtk/io/MeshReader.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#pragma once

#include <wmtk/Primitive.hpp>

#include <filesystem>
#include <vector>

namespace wmtk {

Expand All @@ -12,19 +9,11 @@ class Mesh;
class MeshReader
{
public:
MeshReader(const std::filesystem::path& filename);

void read(Mesh& mesh);
static void read(const std::filesystem::path& filename, Mesh& mesh);

private:
const std::filesystem::path m_filename;
virtual ~MeshReader() {}

template <typename T>
void set_attribute(
const std::string& name,
PrimitiveType pt,
long stride,
const std::vector<T>& v,
Mesh& mesh);
protected:
virtual void read_aux(const std::filesystem::path& filename, Mesh& mesh) = 0;
};
} // namespace wmtk
7 changes: 2 additions & 5 deletions tests/components/test_component_isotropic_remeshing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ TEST_CASE("smoothing_bunny", "[components][isotropic_remeshing][2D]")
}

wmtk::TriMesh mesh;
{
const std::filesystem::path& file = files["input_mesh"];
wmtk::MeshReader reader(file);
reader.read(mesh);
}
const std::filesystem::path& file = files["input_mesh"];
wmtk::MeshReader::read(file, mesh);

OperationSettings<tri_mesh::VertexLaplacianSmooth> op_settings;
op_settings.position = mesh.get_attribute_handle<double>("position", PrimitiveType::Vertex);
Expand Down
5 changes: 1 addition & 4 deletions tests/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ TEST_CASE("hdf5_2d_read", "[io]")
HDF5Writer writer("test.hdf5");
mesh.serialize(writer);

MeshReader reader("test.hdf5");
reader.read(mesh1);
MeshReader::read("test.hdf5", mesh1);

CHECK(mesh1 == mesh);
}
Expand Down Expand Up @@ -68,8 +67,6 @@ TEST_CASE("hdf5_3d", "[io]")

HDF5Writer writer("test.hdf5");
mesh.serialize(writer);

MeshReader reader("test.hdf5");
}

TEST_CASE("paraview_3d", "[io]")
Expand Down

0 comments on commit 5d02099

Please sign in to comment.