Wildmeshing Toolkit
Loading...
Searching...
No Matches
HDF5Writer.cpp
Go to the documentation of this file.
1#if defined(__GNUG__) && !defined(__clang__)
2#pragma GCC diagnostic push
3#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
4#endif
5#include <string>
6#if defined(__GNUG__) && !defined(__clang__)
7#pragma GCC diagnostic pop
8#endif
9#include <fmt/format.h>
10#include <fmt/ranges.h>
11#include "HDF5Writer.hpp"
12
14
15#include <h5pp/h5pp.h>
16
17#include <sstream>
18
19namespace wmtk {
20namespace {
21template <typename T>
22std::string get_type()
23{
24 assert(false);
25 return "";
26}
27
28template <>
29std::string get_type<int64_t>()
30{
31 return "int64_t";
32}
33
34template <>
35std::string get_type<double>()
36{
37 return "double";
38}
39
40template <>
41std::string get_type<char>()
42{
43 return "char";
44}
45template <>
46std::string get_type<short>()
47{
48 return "char";
49}
50
51template <>
52std::string get_type<wmtk::Rational>()
53{
54 return "rational";
55}
56template <>
57std::string get_type<std::string>()
58{
59 return "rational";
60}
61} // namespace
62
63HDF5Writer::HDF5Writer(const std::filesystem::path& filename)
64{
65 m_hdf5_file = std::make_shared<h5pp::File>(filename, h5pp::FileAccess::REPLACE);
66}
67
69 const std::string& name,
70 const int64_t type,
71 const int64_t stride,
72 const std::vector<int64_t>& val,
73 const int64_t default_val)
74{
75 write_internal(name, type, stride, val, default_val);
76}
77
79 const std::string& name,
80 const int64_t type,
81 const int64_t stride,
82 const std::vector<double>& val,
83 const double default_val)
84{
85 write_internal(name, type, stride, val, default_val);
86}
87
89 const std::string& name,
90 const int64_t type,
91 const int64_t stride,
92 const std::vector<char>& val,
93 const char default_val)
94{
95 std::vector<short> tmp;
96 tmp.reserve(val.size());
97 for (const auto& v : val) tmp.push_back(v);
98
99 write_internal(name, type, stride, tmp, short(default_val));
100}
101
102
104 const std::string& name,
105 const int64_t type,
106 const int64_t stride,
107 const std::vector<Rational>& val,
108 const Rational& default_val)
109{
110 std::vector<std::string> tmp;
111 tmp.reserve(val.size());
112 int64_t max_size = -1;
113 for (const auto& v : val) {
114 tmp.emplace_back(v.serialize());
115 max_size = std::max(max_size, int64_t(tmp.back().size()));
116 }
117
118 Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> data(tmp.size(), max_size);
119 data.setConstant(int(' '));
120 for (int64_t i = 0; i < data.rows(); ++i) {
121 for (int64_t j = 0; j < tmp[i].size(); ++j) {
122 data(i, j) = int(tmp[i][j]);
123 }
124 }
125
126 write_internal(name, type, stride, data, default_val.to_binary());
127}
128
129void HDF5Writer::write_capacities(const std::vector<int64_t>& capacities)
130{
131 m_hdf5_file->writeAttribute(capacities, dataset_path(), "capacities");
132}
133
134template <typename Data, typename T>
136 const std::string& name,
137 const int64_t type,
138 const int64_t stride,
139 const Data& val,
140 const T& default_val)
141{
142 std::string path = fmt::format("{}/{}/{}", dataset_path(), type, name);
143
144 m_hdf5_file->writeDataset(val, path);
145 m_hdf5_file->writeAttribute(stride, path, "stride");
146 m_hdf5_file->writeAttribute(default_val, path, "default_value");
147 m_hdf5_file->writeAttribute(type, path, "dimension");
148 m_hdf5_file->writeAttribute(get_type<T>(), path, "type");
149}
150
152{
153 m_hdf5_file->writeAttribute(type, dataset_path(), "top_simplex_type");
154}
155
156void HDF5Writer::write_absolute_id(const std::vector<int64_t>& id)
157{
158 if (id.empty() || m_mm_level == 0) {
159 m_name = "";
160 } else {
161 m_name = fmt::format("mesh_{}", fmt::join(id, ""));
162 }
163
164 ++m_mm_level;
165
166 m_hdf5_file->createGroup(dataset_path());
167
168 if (!id.empty()) m_hdf5_file->writeAttribute(id, dataset_path(), "absolute_id");
169}
170
171std::string HDF5Writer::dataset_path() const
172{
173 std::string res = "WMTK";
174
175 if (!m_name.empty()) res += "/multimesh/" + m_name;
176
177 return res;
178}
179
180
181template <typename T>
182void HDF5Writer::write_attribute_names(int dim, const std::vector<std::string>& names)
183{
184 if (names.empty()) {
185 return;
186 }
187 const static std::string& name = get_type<T>();
188 const std::string path =
189 fmt::format("{}/{}_{}/{}", dataset_path(), "ATTRIBUTE_LIST", name, dim);
190
191 m_hdf5_file->writeDataset(names, path);
192}
193
194template void HDF5Writer::write_attribute_names<double>(int dim, const std::vector<std::string>&);
195template void HDF5Writer::write_attribute_names<int64_t>(int dim, const std::vector<std::string>&);
196template void HDF5Writer::write_attribute_names<char>(int dim, const std::vector<std::string>&);
197template void HDF5Writer::write_attribute_names<wmtk::Rational>(
198 int dim,
199 const std::vector<std::string>&);
200} // namespace wmtk
std::string dataset_path() const
std::string m_name
HDF5Writer(const std::filesystem::path &filename)
std::shared_ptr< h5pp::File > m_hdf5_file
void write_internal(const std::string &name, const int64_t type, const int64_t stride, const Data &val, const T &default_val)
void write_capacities(const std::vector< int64_t > &capacities) override
void write_attribute_names(int dim, const std::vector< std::string > &names)
void write_absolute_id(const std::vector< int64_t > &id) override
void write_top_simplex_type(const PrimitiveType type) override
bool write(const int) override
std::string to_binary() const
Definition Rational.cpp:350