-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotationio.cpp
More file actions
59 lines (52 loc) · 3.08 KB
/
annotationio.cpp
File metadata and controls
59 lines (52 loc) · 3.08 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
#include "annotationio.h"
#include "jsonio.h"
#include "pattern.h"
#include "abstracthelper.h"
#define JSON_KEY_VERSION ("version")
#define JSON_KEY_MESH_FILE ("meshName")
#define JSON_KEY_ANNOTATIONS ("annotations")
#define VERSION_DELIMITER (".")
// -----------------------------------------------------------------------------
AnnotationIO::AnnotationIO()
{
}
// -----------------------------------------------------------------------------
std::tuple<base::Path, const std::vector<Pattern*>> AnnotationIO::import(const base::Path &file_path)
{
const base::json::JsonElement::ptr json_elem = base::json::JsonIO::import(file_path.path());
assert(json_elem->type() == base::json::JsonElement::OBJECT);
const base::json::JsonObject::ptr json_obj = std::static_pointer_cast<base::json::JsonObject>(json_elem);
assert((*json_obj).has(JSON_KEY_MESH_FILE) && (*json_obj).has(JSON_KEY_ANNOTATIONS) && (*json_obj).has(JSON_KEY_VERSION));
const base::Path mesh_path = (*json_obj).getValue(JSON_KEY_MESH_FILE)->toString();
base::json::JsonArray::ptr patterns_array = std::static_pointer_cast<base::json::JsonArray>((*json_obj).getValue(JSON_KEY_ANNOTATIONS));
const std::string version_string = (*json_obj).getValue(JSON_KEY_VERSION)->toString();
const std::vector<std::string> version_strings = base::AbstractHelper::explode(version_string, std::string(VERSION_DELIMITER));
const int version_major = std::stoi(version_strings[0]);
const int version_minor = std::stoi(version_strings[1]);
if(version_major < VERSION_MAJOR || (version_major == VERSION_MAJOR && version_minor < VERSION_MINOR))
LOG_WARN("Annotation version '"+std::to_string(version_major)+VERSION_DELIMITER+std::to_string(version_minor)
+"' below current version '"+std::to_string(VERSION_MAJOR)+VERSION_DELIMITER+std::to_string(VERSION_MINOR)+"'");
std::vector<Pattern*> patterns = {};
for(base::json::JsonElement::ptr elem : *patterns_array)
{
assert(elem->type() == base::json::JsonElement::OBJECT);
patterns.push_back(new Pattern(std::static_pointer_cast<base::json::JsonObject>(elem), file_path.directory()));
}
return {mesh_path, patterns};
}
// -----------------------------------------------------------------------------
void AnnotationIO::exportToFile(const base::Path &file_path, const base::Path &mesh_path, const std::vector<Pattern*> &patterns)
{
std::vector<base::json::JsonElement::ptr> pattern_elements = {};
for(Pattern *pattern : patterns)
{
if(!pattern->patternSelections().size()) continue;
pattern_elements.push_back(std::make_shared<Pattern>(*pattern));
}
base::json::JsonObject::ptr out_obj = std::make_shared<base::json::JsonObject>(std::map<std::string, base::json::JsonElement::ptr>({
{JSON_KEY_VERSION, std::make_shared<base::json::JsonString>(std::to_string(VERSION_MAJOR)+VERSION_DELIMITER+std::to_string(VERSION_MINOR))},
{JSON_KEY_MESH_FILE, std::make_shared<base::json::JsonString>(mesh_path.path())},
{JSON_KEY_ANNOTATIONS, std::make_shared<base::json::JsonArray>(pattern_elements)}
}));
base::json::JsonIO::exportToFile(file_path, *out_obj, EXPAND_JSON_STRING);
}