5 #include <Utils/Exceptions.h> 6 #include <Utils/GeometryStructures.h> 16 template <
typename Type>
27 width_ = source.width_;
28 height_ = source.height_;
33 Grid(
const size_t width,
const size_t height)
37 data_ = std::vector<std::vector<Type>>(width, std::vector<Type>(height));
41 Grid(
const size_t width,
const size_t height,
const Type &init_value)
45 data_ = std::vector<std::vector<Type>>(width, std::vector<Type>(height, init_value));
49 void Resize(
const size_t width,
const size_t height,
const Type &init_value)
52 for (
auto &column : data_)
54 column.resize(height, init_value);
61 void Resize(
const size_t width,
const size_t height)
64 for (
auto &column : data_)
66 column.resize(height);
81 void Fill(
const Type value)
83 for (
auto &column : data_)
85 std::fill(column.begin(), column.end(), value);
90 void Fill(
const geom::URect &area,
const Type value)
92 if (width_ == 0 || height_ == 0)
94 ulog::Warn(
"Nothing to fill. Grid size is zero.",
"cogs::Grid::Fill");
98 const glm::uvec2 kMaxValue = glm::uvec2(width_, height_) - 1u;
100 glm::uvec2 begin = (glm::min)(area.position, kMaxValue);
101 glm::uvec2 end = (glm::min)(area.position2, kMaxValue);
103 for (
auto &column : data_)
105 std::fill(column.begin() + begin.y, column.begin() + end.y, value);
110 [[nodiscard]]
bool IsInBounds(
const size_t x,
const size_t y)
const 112 return x < width_ && y < height_;
116 [[nodiscard]] Type &
At(
const size_t x,
const size_t y)
118 #ifndef COGS_NO_ERROR_CHECKING 121 throw std_ext::OutOfBounds(x, y,
"cogs::Grid::At");
128 [[nodiscard]]
const Type &
At(
const size_t x,
const size_t y)
const 130 #ifndef COGS_NO_ERROR_CHECKING 133 throw std_ext::OutOfBounds(x, y,
"cogs::Grid::At");
140 [[nodiscard]] Type &
operator()(
const size_t x,
const size_t y)
142 #ifndef COGS_NO_ERROR_CHECKING 145 throw std_ext::OutOfBounds(x, y,
"cogs::Grid::operator()");
166 for (
auto &line : data_)
168 std::reverse(line.begin(), line.end());
175 std::reverse(data_.begin(), data_.end());
179 void Foreach(std::function<
void(
size_t x,
size_t y,
const Type &value)> func)
const 183 for (
size_t x = 0; x <
GetWidth(); x++)
185 func(x, y,
At(x, y));
191 void Foreach(std::function<
void(
size_t x,
size_t y, Type &value)> func)
195 for (
size_t x = 0; x <
GetWidth(); x++)
197 func(x, y,
At(x, y));
203 std::vector<std::vector<Type>> data_;
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