-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh_metadata.cpp
More file actions
86 lines (63 loc) · 1.96 KB
/
mesh_metadata.cpp
File metadata and controls
86 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "mesh_metadata.h"
using json = nlohmann::json;
json MeshMetadata::MetadataAttribute::to_json() {
json j;
j["name"] = name;
j["type"] = type;
j["where"] = where;
return j;
}
MeshMetadata::MetadataAttribute MeshMetadata::MetadataAttribute::from_json(json &j) {
MetadataAttribute obj;
obj.name = j["name"].get<std::string>();
obj.type = j["type"].get<std::string>();
obj.where = j["where"].get<GEO::MeshElementsFlags>();
return obj;
}
json MeshMetadata::to_json() {
std::vector<json> json_attributes(attributes.size());
std::transform(attributes.begin(), attributes.end(), json_attributes.begin(), [](auto &x) {
return x.to_json();
});
json j;
j["filename"] = filename;
j["tet_filename"] = tet_filename;
j["cell_type"] = cell_type;
j["attributes"] = json_attributes;
return j;
}
MeshMetadata MeshMetadata::from_json(json &j) {
MeshMetadata obj;
obj.filename = j["filename"].get<std::string>();
obj.tet_filename = j["tet_filename"].get<std::string>();
obj.cell_type = j["cell_type"].get<GEO::MeshCellType>();
std::vector<json> json_attributes = j["attributes"].get<std::vector<json>>();
std::vector<MetadataAttribute> metadata_attributes(json_attributes.size());
std::transform(json_attributes.begin(), json_attributes.end(), metadata_attributes.begin(), [](auto &sj) {
return MeshMetadata::MetadataAttribute::from_json(sj);
});
obj.attributes = metadata_attributes;
return obj;
}
bool MeshMetadata::has_attr(std::string attr_name) {
for (auto attr : attributes) {
if (attr.name == attr_name)
return true;
}
return false;
}
std::optional<MeshMetadata::MetadataAttribute> MeshMetadata::get_attr(std::string attr_name) {
for (auto attr : attributes) {
if (attr.name == attr_name)
return attr;
}
return std::nullopt;
}
void MeshMetadata::save() {
// TODO check with filesystem::path if its valid path
assert(filename != "");
std::string j = to_json().dump();
std::ofstream ofs(filename + ".json");
ofs << j;
ofs.close();
}