Version: 1.0
forest_handler.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_FOREST_HANDLER_H_
8 #define GRAPHGEN_FOREST_HANDLER_H_
9 
10 #include "output_generator.h"
11 
12 
14  CENTER_LINES = 1,
15  FIRST_LINE = 2,
16  LAST_LINE = 4,
17  SINGLE_LINE = 8,
18 )
19 
20 
41 public:
42  std::map<ForestHandlerFlags, LineForestHandler> f_;
43  std::map<ForestHandlerFlags, std::string> names = {
48  };
49  std::map<ForestHandlerFlags, std::string> prefixs = {
54  };
55 public:
56 
58  const pixel_set& ps,
60  {
61  // Center lines forest generation
64  }
65 
66  // First line forest generation
67  if (ForestHandlerFlags::FIRST_LINE & flag) {
68  constraints first_line_constr;
69  for (const auto& p : ps) {
70  if (p.GetDy() < 0)
71  first_line_constr[p.name_] = 0;
72  }
73  f_[ForestHandlerFlags::FIRST_LINE] = LineForestHandler(bd, ps, first_line_constr);
74  }
75  // Last line forest generation
76  if (ForestHandlerFlags::LAST_LINE & flag) {
77  constraints last_line_constr;
78  for (const auto& p : ps) {
79  if (p.GetDy() > 0)
80  last_line_constr[p.name_] = 0;
81  }
82  f_[ForestHandlerFlags::LAST_LINE] = LineForestHandler(bd, ps, last_line_constr);
83  }
84  // Single line forest generation
86  constraints single_line_constr;
87  for (const auto& p : ps) {
88  if (p.GetDy() != 0)
89  single_line_constr[p.name_] = 0;
90  }
91  f_[ForestHandlerFlags::SINGLE_LINE] = LineForestHandler(bd, ps, single_line_constr);
92  }
93  }
94 
95  void DrawOnFile(const std::string& prefix, DrawDagFlags flags = DrawDagFlags::NONE)
96  {
97  for (auto& x : f_) {
98  DrawForestOnFile(prefix + "_" + names[x.first], x.second, flags);
99  }
100  }
101 
130  BEFORE_AFTER_FUNC(after_main) = DefaultEmptyFunc,
131  BEFORE_AFTER_FUNC(before_end) = BeforeEnd,
132  BEFORE_AFTER_FUNC(after_end) = AfterEnd,
133  BEFORE_AFTER_FUNC(after_end_no_loop) = AfterEndNoLoop,
134  int flags = 0 /* no flags available right now */)
135  {
136  size_t last_id = 0;
137  for (const auto& i : f_) {
138  std::filesystem::path filepath = conf.GetForestCodePath(names[i.first] + "_line");
139  std::ofstream os(filepath);
140  if (!os) {
141  std::cout << "Something went wrong during the generation of " << conf.algorithm_name_ << "forest code\n";
142  return;
143  }
144  if (i.first != ForestHandlerFlags::CENTER_LINES) {
145  last_id = GenerateLineForestCode(os, i.second, prefixs[i.first] + "_", last_id, before_main, after_main, before_end, after_end_no_loop);
146  os << prefixs[i.first] + "_" << ":;\n";
147  }
148  else {
149  last_id = GenerateLineForestCode(os, i.second, prefixs[i.first] + "_", last_id, before_main, after_main, before_end, after_end);
150  }
151  }
152  }
153 
164  for (auto& x : f_) {
165  DragCompressor(x.second, iterations, flags);
166  }
167  }
168 
170  return f_.at(forest_id);
171  }
172 };
173 
174 
175 #endif // FOREST_HANDLER_H_
This class allows to generate the forests associated to an algorithm when the pixel prediction is app...
void GenerateCode(BEFORE_AFTER_FUNC(before_main)=BeforeMainShiftOne, BEFORE_AFTER_FUNC(after_main)=DefaultEmptyFunc, BEFORE_AFTER_FUNC(before_end)=BeforeEnd, BEFORE_AFTER_FUNC(after_end)=AfterEnd, BEFORE_AFTER_FUNC(after_end_no_loop)=AfterEndNoLoop, int flags=0)
Generates the code for the all the forests groups, each of them in a separate file.
void DrawOnFile(const std::string &prefix, DrawDagFlags flags=DrawDagFlags::NONE)
std::map< ForestHandlerFlags, LineForestHandler > f_
void Compress(DragCompressorFlags flags=DragCompressorFlags::PRINT_STATUS_BAR|DragCompressorFlags::IGNORE_LEAVES, int iterations=- 1)
Compresses all the drags of the forest using an exhaustive approach.
ForestHandler(const BinaryDrag< conact > &bd, const pixel_set &ps, ForestHandlerFlags flag=ForestHandlerFlags::CENTER_LINES|ForestHandlerFlags::FIRST_LINE)
LineForestHandler & GetLineForestHandler(ForestHandlerFlags forest_id)
DragCompressorFlags
This enum class defines the available flags for the DragCompression class.
@ IGNORE_LEAVES
Whether to ignore leaves or not during the compression. Please note that compressing the leaves will ...
@ PRINT_STATUS_BAR
Whether to print a sort of progress bar or not.
std::map< std::string, int > constraints
Definition: forest.h:46
ForestHandlerFlags
@ FIRST_LINE
Whether to generate the forest for the first line of the image.
@ CENTER_LINES
Whether to generate the forest for the center lines of the image.
@ SINGLE_LINE
Whether to generate the forest for an image composed by a single line.
@ LAST_LINE
Whether to generate the forest for the last line of the image.
std::string BeforeMainShiftOne(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string AfterEndNoLoop(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string AfterEnd(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
std::string BeforeEnd(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
size_t GenerateLineForestCode(std::ostream &os, const LineForestHandler &lfh, std::string prefix, size_t start_id, std::string before_main(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string after_main(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string before_end(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id), std::string after_end(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id))
Generate the C++ code for the given Forest.
std::string DefaultEmptyFunc(size_t index, const std::string &prefix, const std::vector< std::vector< size_t >> &mapping, size_t end_group_id)
#define BEFORE_AFTER_FUNC(func_name)
Macro used to define the base signature of before and after functions.
bool DrawForestOnFile(const string &output_file, const LineForestHandler &lfh, DrawDagFlags flags)
DrawDagFlags
Flags for the DrawDagOnFile function.
@ NONE
No flags.
std::filesystem::path GetForestCodePath(const std::string &out_base_name)
Definition: config_data.h:100
std::string algorithm_name_
Definition: config_data.h:30
Generates all the forests needed to handle one line of the image.
Definition: forest.h:51
ConfigData conf
Definition: utilities.cpp:9
#define DEFINE_ENUM_CLASS_FLAGS(class_name,...)
Definition: utilities.h:41