COGS
Computer Graphics Structures library
Grid.h
1 #ifndef COGS_GRID_H
2 #define COGS_GRID_H
3 #include <algorithm>
4 
5 #include <Utils/Exceptions.h>
6 #include <Utils/GeometryStructures.h>
7 #include <COGS/API.h>
8 
9 namespace cogs
10 {
11 
16  template <typename Type>
17  class Grid
18  {
19  public:
20 
22  Grid() = default;
23 
25  Grid(const cogs::Grid<Type> &source)
26  {
27  width_ = source.width_;
28  height_ = source.height_;
29  data_ = source.data_;
30  };
31 
33  Grid(const size_t width, const size_t height)
34  {
35  width_ = width;
36  height_ = height;
37  data_ = std::vector<std::vector<Type>>(width, std::vector<Type>(height));
38  };
39 
41  Grid(const size_t width, const size_t height, const Type &init_value)
42  {
43  width_ = width;
44  height_ = height;
45  data_ = std::vector<std::vector<Type>>(width, std::vector<Type>(height, init_value));
46  };
47 
49  void Resize(const size_t width, const size_t height, const Type &init_value)
50  {
51  data_.resize(width);
52  for (auto &column : data_)
53  {
54  column.resize(height, init_value);
55  }
56  width_ = width;
57  height_ = height;
58  };
59 
61  void Resize(const size_t width, const size_t height)
62  {
63  data_.resize(width);
64  for (auto &column : data_)
65  {
66  column.resize(height);
67  }
68  width_ = width;
69  height_ = height;
70  };
71 
73  void Clear()
74  {
75  data_.clear();
76  width_ = 0;
77  height_ = 0;
78  };
79 
81  void Fill(const Type value)
82  {
83  for (auto &column : data_)
84  {
85  std::fill(column.begin(), column.end(), value);
86  }
87  };
88 
90  void Fill(const geom::URect &area, const Type value)
91  {
92  if (width_ == 0 || height_ == 0)
93  {
94  ulog::Warn("Nothing to fill. Grid size is zero.", "cogs::Grid::Fill");
95  return;
96  }
97 
98  const glm::uvec2 kMaxValue = glm::uvec2(width_, height_) - 1u;
99 
100  glm::uvec2 begin = (glm::min)(area.position, kMaxValue);
101  glm::uvec2 end = (glm::min)(area.position2, kMaxValue);
102 
103  for (auto &column : data_)
104  {
105  std::fill(column.begin() + begin.y, column.begin() + end.y, value);
106  }
107  };
108 
110  [[nodiscard]] bool IsInBounds(const size_t x, const size_t y) const
111  {
112  return x < width_ && y < height_;
113  }
114 
116  [[nodiscard]] Type &At(const size_t x, const size_t y)
117  {
118 #ifndef COGS_NO_ERROR_CHECKING
119  if (!IsInBounds(x, y))
120  {
121  throw std_ext::OutOfBounds(x, y, "cogs::Grid::At");
122  }
123 #endif
124  return data_[x][y];
125  };
126 
128  [[nodiscard]] const Type &At(const size_t x, const size_t y) const
129  {
130 #ifndef COGS_NO_ERROR_CHECKING
131  if (!IsInBounds(x, y))
132  {
133  throw std_ext::OutOfBounds(x, y, "cogs::Grid::At");
134  }
135 #endif
136  return data_[x][y];
137  };
138 
140  [[nodiscard]] Type &operator()(const size_t x, const size_t y)
141  {
142 #ifndef COGS_NO_ERROR_CHECKING
143  if (!IsInBounds(x, y))
144  {
145  throw std_ext::OutOfBounds(x, y, "cogs::Grid::operator()");
146  }
147 #endif
148  return data_[x][y];
149  };
150 
152  [[nodiscard]] size_t GetWidth() const
153  {
154  return width_;
155  };
156 
158  [[nodiscard]] size_t GetHeight() const
159  {
160  return height_;
161  };
162 
164  void FlipX()
165  {
166  for (auto &line : data_)
167  {
168  std::reverse(line.begin(), line.end());
169  }
170  }
171 
173  void FlipY()
174  {
175  std::reverse(data_.begin(), data_.end());
176  }
177 
179  void Foreach(std::function<void(size_t x, size_t y, const Type &value)> func) const
180  {
181  for (size_t y = 0; y < GetHeight(); y++)
182  {
183  for (size_t x = 0; x < GetWidth(); x++)
184  {
185  func(x, y, At(x, y));
186  }
187  }
188  }
189 
191  void Foreach(std::function<void(size_t x, size_t y, Type &value)> func)
192  {
193  for (size_t y = 0; y < GetHeight(); y++)
194  {
195  for (size_t x = 0; x < GetWidth(); x++)
196  {
197  func(x, y, At(x, y));
198  }
199  }
200  }
201 
202  private:
203  std::vector<std::vector<Type>> data_;
204  size_t width_{ 0 };
205  size_t height_{ 0 };
206  };
207 
208 }
209 #endif /* !COGS_GRID_H */
Namespace of "Computer Graphic Structures" library, which implement many core classes and structures...
Definition: AabbHierarchy.h:8
bool IsInBounds(const size_t x, const size_t y) const
Checks whether a combination of xy correspond to valid grid cell.
Definition: Grid.h:110
Grid()=default
Creates grid with zero size.
Grid(const size_t width, const size_t height)
Creates grid with specified width and height.
Definition: Grid.h:33
Grid(const size_t width, const size_t height, const Type &init_value)
Creates grid with specified width and height and initializes elements to init_value.
Definition: Grid.h:41
void Clear()
Clears all elements and sets resolution to (0, 0).
Definition: Grid.h:73
void Foreach(std::function< void(size_t x, size_t y, const Type &value)> func) const
Executes a specified function for every element in the grid.
Definition: Grid.h:179
Type & operator()(const size_t x, const size_t y)
Returns a reference to the cell at position xy.
Definition: Grid.h:140
Organizational structure of values in a 2 dimensional grid.
Definition: Grid.h:17
Type & At(const size_t x, const size_t y)
Returns a reference to the cell at position xy.
Definition: Grid.h:116
const Type & At(const size_t x, const size_t y) const
Returns a reference to the cell at position xy.
Definition: Grid.h:128
void Fill(const geom::URect &area, const Type value)
Sets specified area in grid to a defined value.
Definition: Grid.h:90
size_t GetWidth() const
Returns current width of grid.
Definition: Grid.h:152
void Fill(const Type value)
Sets all elements of grid to a defined value.
Definition: Grid.h:81
void Resize(const size_t width, const size_t height, const Type &init_value)
Sets width and height of a grid, and initializes new elements to init_value.
Definition: Grid.h:49
void Foreach(std::function< void(size_t x, size_t y, Type &value)> func)
Executes a specified function for every element in the grid.
Definition: Grid.h:191
void FlipX()
Flips all values in x direction.
Definition: Grid.h:164
Grid(const cogs::Grid< Type > &source)
Creates exact copy of provided grid.
Definition: Grid.h:25
void Resize(const size_t width, const size_t height)
Sets width and height of a grid, and initializes new elements to zeros.
Definition: Grid.h:61
size_t GetHeight() const
Returns current height of grid.
Definition: Grid.h:158
void FlipY()
Flips all values in y direction.
Definition: Grid.h:173