Version: 1.0
forest.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_H_
8 #define GRAPHGEN_FOREST_H_
9 
10 #include <algorithm>
11 #include <map>
12 #include <numeric>
13 
14 #include "conact_tree.h"
15 #include "pixel_set.h"
16 #include "utilities.h"
17 
18 struct Equivalences {
19  using eq_type = std::map<std::string, std::string>;
21 
23  Equivalences(const pixel_set& ps) {
24  using namespace std;
25  for (auto p : ps) {
26  p.ShiftX(ps.GetShiftX());
27  auto it = find(begin(ps), end(ps), p);
28  if (it != end(ps)) {
29  eq_[it->name_] = p.name_;
30  }
31  }
32  }
33 
34  struct FindType {
35  const eq_type& eq_;
36  eq_type::iterator it_;
37  FindType(const eq_type& eq, eq_type::iterator it) : eq_{ eq }, it_{ it } {}
38  operator bool() { return it_ != end(eq_); }
39  operator std::string() { return it_->second; }
40  };
41  FindType Find(const std::string& s) {
42  return { eq_, eq_.find(s) };
43  }
44 };
45 
46 using constraints = std::map<std::string, int>;
47 static std::vector<size_t> DEFAULT_VECTOR; // Dummy vector for the default value of DeleteTree member function
48 
52 
65 
66  // bd is the original binary drag from which to generate the forest and f is where to write the trees
67  CreateReducedDrag(const BinaryDrag<conact>& bd, BinaryDrag<conact>& f, const pixel_set& ps) : bd_{ bd }, f_{ f }, eq_{ps} {
68  constraints constr;
69  CreateReducedTreesRec(bd_.roots_[0], constr);
70  }
71 
72  // See CreateReducedTrees
74  if (n->isleaf()) {
75  // Create a reduced version of the tree based on what we learned on the path to this leaf. The new tree is stored in t_ as well.
76  f_.AddRoot(Reduce(bd_.roots_[0], f_, constr));
77  }
78  else {
79  constraints constrNew = constr;
80  auto ft = eq_.Find(n->data.condition);
81  if (ft)
82  constrNew[ft] = 0;
83  CreateReducedTreesRec(n->left, constrNew);
84  if (ft)
85  constrNew[ft] = 1;
86  CreateReducedTreesRec(n->right, constrNew);
87  }
88  }
89  };
90 
91 
93  std::vector<BinaryDrag<conact>> end_forests_;
94 
95  std::vector<size_t> next_tree_; // This vector contains the equivalences between main trees
96 
97  std::vector<std::vector<size_t>> end_next_tree_; // This vectors contain the equivalences between end trees
98  std::vector<std::vector<size_t>> main_end_tree_mapping_; // This is the mapping between main trees and end trees
99 
101  LineForestHandler(const BinaryDrag<conact>& t, const pixel_set& ps, const constraints& initial_constraints = {}); // Initial_constraints are useful to create particular forests such as the first line forest
102 
103  void RemoveUselessConditions();
104  void RemoveEndTreesUselessConditions();
105 
106  void UpdateNext(BinaryDrag<conact>::node* n);
107 
108  // Removes duplicate trees
109  bool RemoveEqualTrees();
110  bool RemoveEquivalentTrees();
111 
112  // Removes duplicate end trees. This action is performed in each end forest separately, different
113  // groups cannot contain equal end trees. Moreover, experimental result have proven that merging
114  // end forest together is useless since they will never be used together when working on an image.
115  bool RemoveEqualEndTrees();
116  bool RemoveEquivalentEndTrees();
117 
118  void InitNextRec(BinaryDrag<conact>::node* n);
119 
120  static BinaryDrag<conact>::node* Reduce(const BinaryDrag<conact>::node* n, BinaryDrag<conact>& t, const constraints& constr);
121 
122  void CreateReducedTreesRec(const BinaryDrag<conact>::node* n, const constraints& constr = {});
123  void CreateReducedTrees(const BinaryDrag<conact>& t, const constraints& initial_constr);
124 
125 private:
126  bool RemoveEqualEndTreesSeparately();
127  bool RemoveEqualEndTreesJointly();
128 
129  void RebuildDisjointTrees();
130  void RebuildDisjointEndTrees();
131 
132  // Actual implementation of RemoveEqualTrees, RemoveEquivalentTrees, RemoveEqualEndTrees, RemoveEquivalentEndTrees
133  bool RemoveTrees(bool(*FunctionPrt)(const BinaryDrag<conact>::node* n1, const BinaryDrag<conact>::node* n2),
134  std::vector<size_t>& next_tree,
136  bool are_end_trees = false,
137  std::vector<size_t>& mapping = DEFAULT_VECTOR);
138 };
139 
140 #endif // !GRAPHGEN_FOREST_H_
void AddRoot(node *r)
Adds a new root to the vector of roots.
Definition: drag.h:61
std::vector< node * > roots_
Definition: drag.h:58
std::map< std::string, int > constraints
Definition: forest.h:46
FindType(const eq_type &eq, eq_type::iterator it)
Definition: forest.h:37
eq_type::iterator it_
Definition: forest.h:36
const eq_type & eq_
Definition: forest.h:35
std::map< std::string, std::string > eq_type
Definition: forest.h:19
Equivalences()
Definition: forest.h:22
eq_type eq_
Definition: forest.h:20
FindType Find(const std::string &s)
Definition: forest.h:41
Equivalences(const pixel_set &ps)
Definition: forest.h:23
Creates forest of trees pruning original tree.
Definition: forest.h:61
void CreateReducedTreesRec(const BinaryDrag< conact >::node *n, const constraints &constr)
Definition: forest.h:73
const BinaryDrag< conact > & bd_
Definition: forest.h:62
BinaryDrag< conact > & f_
Definition: forest.h:63
CreateReducedDrag(const BinaryDrag< conact > &bd, BinaryDrag< conact > &f, const pixel_set &ps)
Definition: forest.h:67
Generates all the forests needed to handle one line of the image.
Definition: forest.h:51
void CreateReducedTrees(const BinaryDrag< conact > &t, const constraints &initial_constr)
BinaryDrag< conact > f_
Definition: forest.h:92
std::vector< size_t > next_tree_
Definition: forest.h:95
std::vector< std::vector< size_t > > main_end_tree_mapping_
Definition: forest.h:98
std::vector< BinaryDrag< conact > > end_forests_
Definition: forest.h:93
void CreateReducedTreesRec(const BinaryDrag< conact >::node *n, const constraints &constr={})
std::vector< std::vector< size_t > > end_next_tree_
Definition: forest.h:97
int GetShiftX() const
Definition: pixel_set.h:134