20 slice_id =
"slice" + string(p.
coords_[2] < 0 ?
"1" :
"0") + to_string(abs(p.
coords_[2])) +
"_";
23 string row_id =
"row" + string(p.
coords_[1] < 0 ?
"1" :
"0") + to_string(abs(p.
coords_[1]));
26 col +=
" + " + to_string(abs(p.
coords_[0]));
29 col +=
" - " + to_string(abs(p.
coords_[0]));
32 return img_name + slice_id + row_id +
"[c" + col +
"]";
36 if (pixels_names.size() == 1) {
37 pixel p = names[pixels_names.front()];
41 std::vector<std::string> pixels_names_seta(pixels_names.size() / 2), pixels_names_setb(pixels_names.size() - (pixels_names.size() / 2));
43 std::copy_n(pixels_names.begin(), pixels_names_seta.size(), pixels_names_seta.begin());
44 std::copy_n(pixels_names.begin() + pixels_names_seta.size(), pixels_names_setb.size(), pixels_names_setb.begin());
50 if (action ==
"nothing") {
54 string action_ = action.substr(3);
55 if (action_ ==
"newlabel") {
56 return "LabelsSolver::NewLabel()";
59 std::vector<std::string> pixels_names;
62 assert(pixels_names.size() > 0 &&
"Something wrong with actions");
69 if (action ==
"nothing") {
73 string action_ = action.substr(3);
74 if (action_ ==
"newlabel") {
75 return assignment_variable +
" LabelsSolver::NewLabel()";
78 std::vector<std::string> pixels_names;
81 assert(pixels_names.size() > 0 &&
"Something wrong with actions");
107 os <<
"//Pointers:\n";
110 size_t n_dims = shifts.size();
112 stringstream global_ss, in_ss, out_ss;
113 string type_in_prefix_string =
"const unsigned char* const ";
114 string type_out_prefix_string =
"unsigned* const ";
121 string base_row_in_name =
"img_row00";
122 string base_row_out_name =
"img_labels_row00";
123 string base_row_in = type_in_prefix_string + base_row_in_name +
" = img_.ptr<unsigned char>(r);";
124 string base_row_out = type_out_prefix_string + base_row_out_name +
" = img_labels_.ptr<unsigned>(r);";
126 in_ss <<
"// Row pointers for the input image \n";
127 in_ss << base_row_in +
"\n";
129 out_ss <<
"// Row pointers for the output image \n";
130 out_ss << base_row_out +
"\n";
132 for (
int j = -shifts[1]; j < shifts[1]; ++j) {
138 string complete_string_in =
139 type_in_prefix_string +
140 "img_row" + to_string(j < 0) + to_string(abs(j)) +
141 " = (unsigned char *)(((char *)" + base_row_in_name +
") + img_.step.p[0] * " + to_string(j) +
");";
142 in_ss << complete_string_in +
"\n";
145 string complete_string_out =
146 type_out_prefix_string +
147 "img_labels_row" + to_string(j < 0) + to_string(abs(j)) +
148 " = (unsigned *)(((char *)" + base_row_out_name +
") + img_labels_.step.p[0] * " + to_string(j) +
");";
149 out_ss << complete_string_out +
"\n";
159 string base_row_in_name =
"img_slice00_row00";
160 string base_row_out_name =
"img_labels_slice00_row00";
161 string base_row_in = type_in_prefix_string + base_row_in_name +
" = img_.ptr<unsigned char>(s, r);";
162 string base_row_out = type_out_prefix_string + base_row_out_name +
" = img_labels_.ptr<unsigned>(s, r);";
164 in_ss <<
"// Row pointers for the input image (current slice) \n";
165 in_ss << base_row_in +
"\n";
167 out_ss <<
"// Row pointers for the output image (current slice)\n";
168 out_ss << base_row_out +
"\n";
170 for (
int j = -shifts[1]; j < shifts[1]; ++j) {
176 string complete_string_in =
177 type_in_prefix_string +
178 "img_slice00_row" + to_string(j < 0) + to_string(abs(j)) +
179 " = (unsigned char *)(((char *)" + base_row_in_name +
") + img_.step.p[1] * " + to_string(j) +
");";
180 in_ss << complete_string_in +
"\n";
182 string complete_string_out =
183 type_out_prefix_string +
184 "img_labels_slice00_row" + to_string(j < 0) + to_string(abs(j)) +
185 " = (unsigned *)(((char *)" + base_row_out_name +
") + img_labels_.step.p[1] * " + to_string(j) +
");";
186 out_ss << complete_string_out +
"\n";
190 in_ss <<
"\n// Row pointers for the input image (previous slice) \n";
191 out_ss <<
"\n// Row pointers for the output image (previous slice)\n";
194 base_row_in = type_in_prefix_string + base_row_in_name +
" = img_.ptr<unsigned char>(s, r);";
195 base_row_out = type_out_prefix_string + base_row_out_name +
" = img_labels_.ptr<unsigned>(s, r);";
197 for (
int j = -shifts[1]; j <= shifts[1]; ++j) {
199 string complete_string_in =
200 type_in_prefix_string +
201 "img_slice11_row" + to_string(j < 0) + to_string(abs(j)) +
202 " = (unsigned char *)(((char *)" + base_row_in_name +
") - img_.step.p[0] + img_.step.p[1] * " + to_string(j) +
");";
203 in_ss << complete_string_in +
"\n";
205 string complete_string_out =
206 type_out_prefix_string +
207 "img_labels_slice11_row" + to_string(j < 0) + to_string(abs(j)) +
208 " = (unsigned *)(((char *)" + base_row_out_name +
") - img_labels_.step.p[0] + img_labels_.step.p[1] * " + to_string(j) +
");";
209 out_ss << complete_string_out +
"\n";
215 global_ss << in_ss.str() +
"\n" + out_ss.str();
216 os << global_ss.str();
222 size_t n_dims = shifts.size();
224 os <<
"//Conditions:\n";
226 vector<string> counters_names = {
"c",
"r",
"s" };
227 vector<string> sizes_names = {
"w",
"h",
"d" };
228 for (
const auto& p : rs.
ps_) {
229 string uppercase_name(p.name_);
230 transform(p.name_.begin(), p.name_.end(), uppercase_name.begin(), ::toupper);
231 os <<
"#define CONDITION_" + uppercase_name +
" ";
233 if (with_conditions) {
234 for (
size_t i = 0; i < n_dims; ++i) {
235 if (p.coords_[i] < 0) {
236 os << counters_names[i] <<
" > " << -p.coords_[i] - 1 <<
" && ";
238 else if (p.coords_[i] > 0) {
239 os << counters_names[i] <<
" < " << sizes_names[i] <<
" - " << p.coords_[i] <<
" && ";
252 size_t n_dims = shifts.size();
255 os <<
"\n\n//Actions:\n";
256 for (
size_t a = 0; a < rs.
actions.size(); ++a) {
258 string cur_action = rs.
actions[a];
260 os <<
"// Action " << a + 1 <<
": " << cur_action <<
"\n";
261 os <<
"#define ACTION_" << a + 1 <<
" ";
263 string where_to_write =
"img_labels_" + string(n_dims > 2 ?
"slice00_" :
"") +
"row00[c] = ";
267 if (with_continues) {
278 os <<
"\n\n//Actions:\n";
279 for (
size_t a = 0; a < rs.
actions.size(); ++a) {
280 const auto& action = rs.
actions[a];
282 os <<
"#define ACTION_" + std::to_string(a + 1);
284 if (action ==
"keep0") {
285 os <<
" ; // keep0\n";
288 if (action ==
"keep1") {
289 os <<
" img_row00[c] = 1; // keep1\n";
292 if (action ==
"change0") {
293 os <<
" modified = true; // change0\n";
301 os <<
"\n\n//Actions:\n";
302 for (
size_t a = 0; a < rs.
actions.size(); ++a) {
303 const auto& action = rs.
actions[a];
305 os <<
"#define ACTION_" << (a + 1) <<
"\tpos = ProcessPixel<" << action <<
"\t>(r, c, rccode, chains, pos);";
307 if (with_continues) {
319 std::optional<pixel_set> names)
347 std::cout <<
"WARNING: the specified algorithms type is not valid. The ACTION code won't be generated\n";
bool GeneratePointersConditionsActionsCode(const rule_set &rs, GenerateConditionActionCodeFlags flag, GenerateActionCodeTypes type, std::optional< pixel_set > names)
TODO fix documentation names contains the position in the labels image corresponding to the names use...
string GenerateAccessPixelCode(const string &img_name, const pixel &p)
void GenerateThinningActionsCode(ofstream &os, const rule_set &rs, bool with_continues=true)
string CreateAssignmentCodeRec(const std::vector< std::string > &pixels_names, const pixel_set &names)
void GenerateActionsCode(ofstream &os, const rule_set &rs, const pixel_set &names, bool with_continues=true)
void GeneratePointersCode(ofstream &os, const rule_set &rs)
string CreateAssignmentCode(const string &action, const pixel_set &names)
string CreateActionCodeCtbe(const string &action, const pixel_set &names, const string &assignment_variable)
void GenerateConditionsCode(ofstream &os, const rule_set &rs, bool with_conditions)
void GenerateChaincodeActionsCode(ofstream &os, const rule_set &rs, bool with_continues=true)
GenerateConditionActionCodeFlags
This is the enum class that defines the available flags for the GeneratePointersConditionsActionsCode...
@ ACTIONS_WITH_CONTINUE
Whether to add continues at the end of each action or not.
@ CONDITIONS_WITH_IFS
Whether to add if statements or not when generating conditions code. They serve to check if the pixel...
GenerateActionCodeTypes
This is the enum class that defines the available algorithms types for the GenerateActionsCode functi...
@ LABELING
Connected Components Labeling.
std::filesystem::path code_path_
std::vector< uint8_t > shifts_
std::vector< int > coords_
std::vector< std::string > actions
void StringSplit(const std::string &str, T &cont, char delim='+')