Version: 1.0
pixel_set.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_PIXEL_SET_H_
8 #define GRAPHGEN_PIXEL_SET_H_
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <fstream>
13 #include <ostream>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include "yaml-cpp/yaml.h"
19 
20 #include "utilities.h"
21 
22 struct pixel {
23  std::vector<int> coords_;
24  std::string name_;
25 
26  pixel() {}
27  pixel(const YAML::Node& node) {
28  Deserialize(node);
29  }
30  pixel(std::string name, std::vector<int> coords) : name_{ std::move(name) }, coords_{ std::move(coords) } {}
31 
32  auto size() const { return coords_.size(); }
33  auto& operator[](size_t i) { return coords_[i]; }
34  auto& operator[](size_t i) const { return coords_[i]; }
35 
36  // Temporary methods to adapt the pixel to the old 2D-specific code
37  int GetDx() const { return coords_[0]; }
38  int GetDy() const { return coords_[1]; }
39 
40  // It works only on coords_
41  bool operator==(const pixel& rhs) const {
42  assert(rhs.size() == coords_.size() && "Something wrong with pixel's coordinates");
43  for (size_t i = 0; i < coords_.size(); ++i) {
44  if (coords_[i] != rhs[i]) {
45  return false;
46  }
47  }
48  return true;
49  }
50 
51  void ShiftX(int s) {
52  coords_[0] += s;
53  }
54 
55  YAML::Node Serialize() const {
56  YAML::Node pixel_node;
57  pixel_node["name"] = name_;
58 
59  for (const auto& c : coords_) {
60  pixel_node["coords"].push_back(std::to_string(c));
61  }
62 
63  return pixel_node;
64  }
65 
66  void Deserialize(const YAML::Node& node) {
67  name_ = node["name"].as<std::string>();
68  for (unsigned i = 0; i < node["coords"].size(); ++i) {
69  coords_.push_back(node["coords"][i].as<int>());
70  }
71  }
72 };
73 
74 // This function return the ChebyshevDistance between the two pixels p1 and p2.
75 // The Chebyshev distance between two vectors is the greatest of their differences
76 // along any coordinate dimension.
77 static int ChebyshevDistance(const pixel& p1, const pixel& p2)
78 {
79  assert(p1.size() == p2.size());
80  int max = 0;
81  for (size_t i = 0; i < p1.size(); ++i) {
82  max = std::max(max, abs(p1[i] - p2[i]));
83  }
84  return max;
85 }
86 
87 struct pixel_set {
88  std::vector<pixel> pixels_;
89  std::vector<uint8_t> shifts_;
90 
92  {}
93  pixel_set(const YAML::Node& ps_node) {
94  Deserialize(ps_node);
95  }
96  pixel_set(std::initializer_list<pixel> il, std::vector<uint8_t> shifts) : pixels_{ il }, shifts_{std::move(shifts)}
97  {}
98  pixel_set(std::initializer_list<pixel> il) : pixels_{ il } {
99  shifts_.resize(pixels_.front().size());
100  for (size_t i = 0; i < pixels_.front().size(); ++i)
101  shifts_[i] = 1;
102  }
103 
104  void SetShifts(std::vector<uint8_t> shifts) {
105  assert(shifts.size() == shifts_.size() && "'shifts' vector size cannot be changed");
106  shifts_ = shifts;
107  };
108 
109  auto& operator[](size_t i) { return pixels_[i]; }
110  auto& operator[](size_t i) const { return pixels_[i]; }
111 
112  // Search predicate to apply find algorithm on vector of pixels
113  struct find_pixel
114  {
115  std::string name_;
116  find_pixel(std::string name) : name_(name) {}
117  bool operator () (const pixel& p) const
118  {
119  return p.name_ == name_;
120  }
121  };
122 
123  auto& operator[](const std::string& s) { return *std::find_if(pixels_.begin(), pixels_.end(), find_pixel(s)); }
124  auto& operator[](const std::string& s) const { return *std::find_if(pixels_.begin(), pixels_.end(), find_pixel(s)); }
125 
126  auto size() const { return pixels_.size(); }
127 
128  auto begin() { return std::begin(pixels_); }
129  auto end() { return std::end(pixels_); }
130  auto begin() const { return std::begin(pixels_); }
131  auto end() const { return std::end(pixels_); }
132 
133  // Temporary methods to adapt the pixel to the old 2D-specific code
134  int GetShiftX() const { return shifts_[0]; }
135 
136  YAML::Node Serialize() const {
137  YAML::Node ps_node;
138 
139  for (const auto& p : pixels_) {
140  ps_node["pixels"].push_back(p.Serialize());
141  }
142 
143  for (const auto& s : shifts_) {
144  ps_node["shifts"].push_back(static_cast<int>(s));
145  }
146  return ps_node;
147  }
148 
149  void Deserialize(const YAML::Node& node) {
150  for (unsigned i = 0; i < node["shifts"].size(); ++i) {
151  shifts_.push_back(node["shifts"][i].as<int>());
152  }
153 
154  for (unsigned i = 0; i < node["pixels"].size(); ++i) {
155  pixels_.push_back(pixel(node["pixels"][i]));
156  }
157  }
158 
159  //friend std::ostream& operator<<(std::ostream& os, const pixel_set& ps);
160  //friend std::istream& operator>>(std::istream& is, pixel_set& ps);
161 };
162 
163 //inline std::ostream& operator<<(std::ostream& os, const pixel_set& ps) {
164 // os << "pixels: ";
165 // for (unsigned i = 0; i < ps.pixels_.size(); ++i) {
166 // os << ps.pixels_[i];
167 // if (i < ps.pixels_.size() - 1) {
168 // os << "; ";
169 // }
170 // }
171 // os << "\nshifts: ";
172 // for (unsigned i = 0; i < ps.shifts_.size(); ++i) {
173 // os << (int)ps.shifts_[i];
174 // if (i < ps.shifts_.size() - 1) {
175 // os << ", ";
176 // }
177 // }
178 // os << "\n";
179 // return os;
180 //}
181 //
182 //inline std::istream& operator>>(std::istream& is, pixel_set& ps) {
183 // std::string pixels_id = "pixels:";
184 // std::string ps_line;
185 // getline(is, ps_line);
186 // if (ps_line.substr(0, pixels_id.size()) != pixels_id) {
187 // throw std::runtime_error("Bad Rule Set File Format!");
188 // }
189 //
190 // std::vector<std::string> pixels_str;
191 // StringSplit(ps_line, pixels_str, ';');
192 //
193 // ps.pixels_ = std::vector<pixel>(pixels_str.size());
194 // for (unsigned i = 0; i < pixels_str.size(); ++i) {
195 //
196 // unsigned begin_n = pixels_str[i].find_first_of("\"");
197 // unsigned end_n = pixels_str[i].find_last_of("\"");
198 //
199 // ps.pixels_[i].name_ = pixels_str[i].substr(begin_n + 1, end_n - begin_n - 1);
200 // pixels_str[i] = pixels_str[i].substr(end_n);
201 //
202 // std::vector<std::string> coords;
203 // unsigned begin_p = pixels_str[i].find_first_of("{");
204 // unsigned end_p = pixels_str[i].find_first_of("}");
205 // pixels_str[i] = pixels_str[i].substr(begin_p + 1, end_p - begin_p - 1);
206 // StringSplit(pixels_str[i], coords, ',');
207 //
208 // for (unsigned j = 0; j < coords.size(); ++j) {
209 // ps.pixels_[i].coords_.push_back(std::stoi(coords[j]));
210 // }
211 // }
212 //
213 // return is;
214 //}
215 
216 #endif //GRAPHGEN_PIXEL_SET_H_
bool operator()(const pixel &p) const
Definition: pixel_set.h:117
find_pixel(std::string name)
Definition: pixel_set.h:116
std::string name_
Definition: pixel_set.h:115
auto & operator[](const std::string &s) const
Definition: pixel_set.h:124
pixel_set(const YAML::Node &ps_node)
Definition: pixel_set.h:93
auto end()
Definition: pixel_set.h:129
void SetShifts(std::vector< uint8_t > shifts)
Definition: pixel_set.h:104
auto & operator[](const std::string &s)
Definition: pixel_set.h:123
std::vector< pixel > pixels_
Definition: pixel_set.h:88
int GetShiftX() const
Definition: pixel_set.h:134
pixel_set(std::initializer_list< pixel > il, std::vector< uint8_t > shifts)
Definition: pixel_set.h:96
auto begin() const
Definition: pixel_set.h:130
pixel_set()
Definition: pixel_set.h:91
pixel_set(std::initializer_list< pixel > il)
Definition: pixel_set.h:98
void Deserialize(const YAML::Node &node)
Definition: pixel_set.h:149
auto & operator[](size_t i)
Definition: pixel_set.h:109
auto begin()
Definition: pixel_set.h:128
auto size() const
Definition: pixel_set.h:126
auto end() const
Definition: pixel_set.h:131
std::vector< uint8_t > shifts_
Definition: pixel_set.h:89
YAML::Node Serialize() const
Definition: pixel_set.h:136
auto & operator[](size_t i) const
Definition: pixel_set.h:110
pixel(std::string name, std::vector< int > coords)
Definition: pixel_set.h:30
auto & operator[](size_t i) const
Definition: pixel_set.h:34
std::vector< int > coords_
Definition: pixel_set.h:23
pixel(const YAML::Node &node)
Definition: pixel_set.h:27
int GetDy() const
Definition: pixel_set.h:38
bool operator==(const pixel &rhs) const
Definition: pixel_set.h:41
void ShiftX(int s)
Definition: pixel_set.h:51
auto & operator[](size_t i)
Definition: pixel_set.h:33
YAML::Node Serialize() const
Definition: pixel_set.h:55
std::string name_
Definition: pixel_set.h:24
pixel()
Definition: pixel_set.h:26
auto size() const
Definition: pixel_set.h:32
int GetDx() const
Definition: pixel_set.h:37
void Deserialize(const YAML::Node &node)
Definition: pixel_set.h:66