Maze Builder Docs 8.2.1
Loading...
Searching...
No Matches
grid.h
Go to the documentation of this file.
1#ifndef GRID_H
2#define GRID_H
3
6
7#include <cstdint>
8#include <memory>
9#include <mutex>
10#include <optional>
11#include <string>
12#include <tuple>
13#include <unordered_map>
14#include <vector>
15
18namespace mazes
19{
20 class cell;
21
24 class grid : public grid_interface, public grid_operations
25 {
26 public:
31 explicit grid(unsigned int rows = 1u, unsigned int columns = 1u, unsigned int levels = 1u);
32
35 explicit grid(const std::tuple<unsigned int, unsigned int, unsigned int>& dimens);
36
39 grid(const grid& other);
40
44 grid& operator=(const grid& other);
45
48 grid(grid&& other) noexcept;
49
53 grid& operator=(grid&& other) noexcept;
54
56 ~grid() override;
57
60 grid_operations& operations() noexcept override;
61
64 const grid_operations& operations() const noexcept override;
65
68 std::tuple<unsigned int, unsigned int, unsigned int> get_dimensions() const noexcept override;
69
73 std::string contents_of(std::shared_ptr<cell> const& c) const noexcept override;
74
78 std::uint32_t background_color_for(std::shared_ptr<cell> const& c) const noexcept override;
79
84 std::shared_ptr<cell> get_neighbor(std::shared_ptr<cell> const& c, direction dir) const noexcept override;
85
89 std::vector<std::shared_ptr<cell>> get_neighbors(std::shared_ptr<cell> const& c) const noexcept override;
90
96 void set_neighbor(const std::shared_ptr<cell>& c, direction dir,
97 std::shared_ptr<cell> const& neighbor) noexcept override;
98
99 // Convenience methods for accessing neighbors
100 std::shared_ptr<cell> get_north(const std::shared_ptr<cell>& c) const noexcept override;
101 std::shared_ptr<cell> get_south(const std::shared_ptr<cell>& c) const noexcept override;
102 std::shared_ptr<cell> get_east(const std::shared_ptr<cell>& c) const noexcept override;
103 std::shared_ptr<cell> get_west(const std::shared_ptr<cell>& c) const noexcept override;
104
108 std::shared_ptr<cell> search(int index) const noexcept override;
109
112 int num_cells() const noexcept override;
113
115 void clear_cells() noexcept override;
116
119 void set_str(std::string const& str) noexcept override;
120
122 std::string get_str() const noexcept override;
123
124 void set_file(std::string const& f) noexcept override;
125
126 std::string get_file() const noexcept override;
127
130 std::vector<std::tuple<int, int, int, int>> get_vertices() const noexcept override;
131
134 void set_vertices(const std::vector<std::tuple<int, int, int, int>>& vertices) noexcept override;
135
138 std::vector<std::vector<std::uint32_t>> get_faces() const noexcept override;
139
142 void set_faces(const std::vector<std::vector<std::uint32_t>>& faces) noexcept override;
143
146 std::vector<std::uint8_t> get_pixels() const noexcept override;
147
150 void set_pixels(const std::vector<std::uint8_t>& pixels) noexcept override;
151
153 void resize(unsigned int rows, unsigned int cols, unsigned int levels) noexcept override;
154
155 private:
156 std::unordered_map<int, std::shared_ptr<cell>> m_cells;
157
158 std::tuple<unsigned int, unsigned int, unsigned int> m_dimensions;
159
160 // Store topology - which cell is neighbor to which in what direction
161 // Key: cell index, Value: map of direction to neighbor cell index
162 mutable std::mutex m_topology_mutex;
163 std::unordered_map<int, std::unordered_map<direction, int>> m_topology;
164
165 // Arbitrary data
166 std::string m_file;
167 std::string m_str;
168
169 // 3D data
170 std::vector<std::tuple<int, int, int, int>> m_vertices;
171 std::vector<std::vector<std::uint32_t>> m_faces;
172
173 // Image data
174 std::vector<std::uint8_t> m_pixels;
175 };
176} // namespace mazes
177
178#endif // GRID_H
direction
Directional neighbors for grid topology.
Definition barriers.h:24
Cell class with links to other cells.
Definition cell.h:19
Interface for the grid class.
Definition grid_interface.h:20
Interface for grid navigation and manipulation operations.
Definition grid_operations.h:19
General purpose grid class for 2D maze generation.
Definition grid.h:25
std::string contents_of(std::shared_ptr< cell > const &c) const noexcept override
Get detailed information of a cell in the grid.
grid_operations & operations() noexcept override
grid(const std::tuple< unsigned int, unsigned int, unsigned int > &dimens)
Construct a grid using a tuple of unsigned integers.
grid(const grid &other)
Copy constructor.
void resize(unsigned int rows, unsigned int cols, unsigned int levels) noexcept override
Resize: clear all cells/topology and reset dimensions.
void set_faces(const std::vector< std::vector< std::uint32_t > > &faces) noexcept override
Set the faces.
void set_str(std::string const &str) noexcept override
Set a string value.
void set_vertices(const std::vector< std::tuple< int, int, int, int > > &vertices) noexcept override
Set the vertices.
grid & operator=(const grid &other)
Assignment operator.
void set_pixels(const std::vector< std::uint8_t > &pixels) noexcept override
Set the pixel data for image generation.
grid & operator=(grid &&other) noexcept
Move assignment operator.
std::tuple< unsigned int, unsigned int, unsigned int > get_dimensions() const noexcept override
Get the dimensions of the grid.
std::shared_ptr< cell > get_neighbor(std::shared_ptr< cell > const &c, direction dir) const noexcept override
Get neighbor by the cell's respective location.
void set_neighbor(const std::shared_ptr< cell > &c, direction dir, std::shared_ptr< cell > const &neighbor) noexcept override
Set neighbor for a cell in a given direction.
std::shared_ptr< cell > search(int index) const noexcept override
Search for a cell by index.
std::vector< std::vector< std::uint32_t > > get_faces() const noexcept override
Get the faces.
void set_file(std::string const &f) noexcept override
Set the file name.
std::vector< std::shared_ptr< cell > > get_neighbors(std::shared_ptr< cell > const &c) const noexcept override
Get all the neighbors by the cell.
grid(grid &&other) noexcept
Move constructor.
std::uint32_t background_color_for(std::shared_ptr< cell > const &c) const noexcept override
Get the background color for a cell in the grid.
void clear_cells() noexcept override
Cleanup cells by cleaning up links within cells.
grid(unsigned int rows=1u, unsigned int columns=1u, unsigned int levels=1u)
Construct a grid using unsigned integers.
std::vector< std::uint8_t > get_pixels() const noexcept override
Get the pixel data for image generation.
std::vector< std::tuple< int, int, int, int > > get_vertices() const noexcept override
Get the vertices.
std::string get_str() const noexcept override
Get a string value.
int num_cells() const noexcept override
Get the count of cells in the grid.
~grid() override
Destructor.
std::string get_file() const noexcept override
Get the file name.