-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.cpp
More file actions
177 lines (155 loc) · 6.52 KB
/
pattern.cpp
File metadata and controls
177 lines (155 loc) · 6.52 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "pattern.h"
#include "patternselection.h"
#include <opencv2/highgui.hpp>
#define JSON_KEY_PATTERN_ID ("patternId")
#define JSON_KEY_FILE_NAME ("fileName")
#define JSON_KEY_FOLD_SYMMETRY ("foldSymmetry")
#define JSON_KEY_SELECTIONS ("selections")
// -----------------------------------------------------------------------------
const std::array<base::Color, NUM_COLORS> Pattern::color_map_ = {
base::Color(141,211,199),
base::Color(255,255,179),
base::Color(190,186,218),
base::Color(251,128,114),
base::Color(128,177,211),
base::Color(253,180,98 ),
base::Color(179,222,105),
base::Color(252,205,229)
};
// -----------------------------------------------------------------------------
const std::vector<std::pair<uint, std::string>> Pattern::fold_symmetries_ = {
{0, "∞"},
{1, "1"},
{2, "2"},
{3, "3"},
{4, "4"},
};
// -----------------------------------------------------------------------------
Pattern::Pattern(std::size_t pattern_id, const base::Path &img_path) :
pattern_id_(pattern_id), color_(color_map_[pattern_id%NUM_COLORS]), img_path_(img_path),
fold_symmetry_(DEFAULT_FOLD_SYMMETRY),
pattern_selections_(std::vector<PatternSelection*>())
{
assert(img_path_.exists());
pattern_img_ = cv::imread(img_path_.path(), cv::IMREAD_COLOR);
}
// -----------------------------------------------------------------------------
Pattern::Pattern(const base::json::JsonObject::ptr json_obj, const base::Path &base_path) :
pattern_selections_(std::vector<PatternSelection*>())
{
assert(json_obj->has(JSON_KEY_FILE_NAME) && json_obj->getValue(JSON_KEY_FILE_NAME)->type() == base::json::JsonElement::STRING);
img_path_ = base::Path::join(std::vector<std::string>{base_path, json_obj->getValue(JSON_KEY_FILE_NAME)->toString()});
assert(img_path_.exists());
pattern_img_ = cv::imread(img_path_.path(), cv::IMREAD_COLOR);
assert(json_obj->has(JSON_KEY_PATTERN_ID) && json_obj->getValue(JSON_KEY_PATTERN_ID)->type() == base::json::JsonElement::NUMBER);
pattern_id_ = *json_obj->getValue(JSON_KEY_PATTERN_ID)->number();
color_ = color_map_[pattern_id_%NUM_COLORS];
if(json_obj->has(JSON_KEY_FOLD_SYMMETRY))
{
const std::size_t val = *json_obj->getValue(JSON_KEY_FOLD_SYMMETRY)->number();
std::vector<std::pair<uint, std::string>>::const_iterator it = std::find_if(
fold_symmetries_.begin(),
fold_symmetries_.end(),
[val](const std::pair<uint, std::string> &fold_symmetry_pair){ return fold_symmetry_pair.first == val; });
assert(it != fold_symmetries_.end() && "invalid fold symmetry");
fold_symmetry_ = val;
}
else
{
LOG_WARN("Pattern has no fold symmetry");
fold_symmetry_ = DEFAULT_FOLD_SYMMETRY;
}
const base::json::JsonArray *json_selections = json_obj->getValue(JSON_KEY_SELECTIONS)->array();
for(base::json::JsonElement::ptr elem : *json_selections)
{
pattern_selections_.push_back(new PatternSelection(this, std::static_pointer_cast<base::json::JsonObject>(elem)));
}
}
// -----------------------------------------------------------------------------
Pattern::Pattern(const Pattern &pattern)
{
pattern_id_ = pattern.pattern_id_;
color_ = pattern.color_;
img_path_ = pattern.img_path_;
pattern_img_ = pattern.pattern_img_;
fold_symmetry_ = pattern.fold_symmetry_;
pattern_selections_ = std::vector<PatternSelection*>(pattern.pattern_selections_.size());
for(std::size_t idx = 0; idx < pattern.pattern_selections_.size(); idx++)
pattern_selections_[idx] = new PatternSelection(*pattern.pattern_selections_[idx]);
}
// -----------------------------------------------------------------------------
Pattern::~Pattern()
{
for(const PatternSelection *pattern_selection : pattern_selections_) delete pattern_selection;
}
// -----------------------------------------------------------------------------
base::Color Pattern::color() const
{
return color_;
}
// -----------------------------------------------------------------------------
std::string Pattern::title() const
{
return img_path_.fileName();
}
// -----------------------------------------------------------------------------
base::Path Pattern::imgPath() const
{
return img_path_;
}
// -----------------------------------------------------------------------------
cv::Mat3b Pattern::image() const
{
return pattern_img_;
}
// -----------------------------------------------------------------------------
uint Pattern::foldSymmetry() const
{
return fold_symmetry_;
}
// -----------------------------------------------------------------------------
const std::vector<PatternSelection*> Pattern::patternSelections() const
{
return pattern_selections_;
}
// -----------------------------------------------------------------------------
void Pattern::addPatternSelection(PatternSelection *pattern_selection)
{
pattern_selections_.push_back(pattern_selection);
}
// -----------------------------------------------------------------------------
void Pattern::removePatternSelection(const PatternSelection *pattern_selection)
{
std::vector<PatternSelection*>::const_iterator erase_it = std::find(pattern_selections_.begin(), pattern_selections_.end(), pattern_selection);
assert(erase_it != pattern_selections_.end());
pattern_selections_.erase(erase_it);
}
// -----------------------------------------------------------------------------
void Pattern::setFoldSymmetry(uint fold_symmetry)
{
fold_symmetry_ = fold_symmetry;
}
// -----------------------------------------------------------------------------
std::array<base::Color, NUM_COLORS> Pattern::colorMap()
{
return color_map_;
}
// -----------------------------------------------------------------------------
std::vector<std::pair<uint, std::string>> Pattern::foldSymmetries()
{
return fold_symmetries_;
}
// -----------------------------------------------------------------------------
std::string Pattern::createJsonString(bool expand, uint) const
{
std::vector<base::json::JsonElement::ptr> selections_json = {};
for(const PatternSelection *pattern_selection : pattern_selections_)
selections_json.push_back(std::make_shared<PatternSelection>(*pattern_selection));
base::json::JsonObject::ptr obj = std::make_shared<base::json::JsonObject>(std::map<Key, base::json::JsonElement::ptr>{
{JSON_KEY_PATTERN_ID, std::make_shared<base::json::JsonNumber>(pattern_id_)},
{JSON_KEY_FILE_NAME, std::make_shared<base::json::JsonString>(img_path_.fileName())},
{JSON_KEY_SELECTIONS, std::make_shared<base::json::JsonArray>(selections_json)},
{JSON_KEY_FOLD_SYMMETRY, std::make_shared<base::json::JsonNumber>(fold_symmetry_)}
});
return obj->jsonString(expand);
}