Maze Builder Docs 8.8.13
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 <vector>
14
17namespace mazes
18{
19 class cell;
20
23 class grid : public grid_interface, public grid_operations
24 {
25 public:
30 explicit grid(unsigned int rows = 1u, unsigned int columns = 1u, unsigned int levels = 1u);
31
34 explicit grid(const std::tuple<unsigned int, unsigned int, unsigned int>& dimensions);
35
38 grid(const grid& other);
39
43 grid& operator=(const grid& other);
44
47 grid(grid&& other) noexcept;
48
52 grid& operator=(grid&& other) noexcept;
53
55 ~grid() override;
56
59 grid_operations& operations() noexcept override;
60
63 const grid_operations& operations() const noexcept override;
64
67 std::tuple<unsigned int, unsigned int, unsigned int> get_dimensions() const noexcept override;
68
72 std::string contents_of(std::shared_ptr<cell> const& c) const noexcept override;
73
77 std::uint32_t background_color_for(std::shared_ptr<cell> const& c) const noexcept override;
78
83 std::shared_ptr<cell> get_neighbor(std::shared_ptr<cell> const& c, Direction dir) const noexcept override;
84
85 std::size_t collect_neighbors(std::shared_ptr<cell> const& c,
86 std::span<std::shared_ptr<cell>, MAX_NEIGHBORS> out) const noexcept override;
87
88 std::span<const std::shared_ptr<cell>> cells() const noexcept override;
89
95 void set_neighbor(const std::shared_ptr<cell>& c, Direction dir,
96 std::shared_ptr<cell> const& neighbor) noexcept override;
97
98 // Convenience methods for accessing neighbors
99 std::shared_ptr<cell> get_north(const std::shared_ptr<cell>& c) const noexcept override;
100 std::shared_ptr<cell> get_south(const std::shared_ptr<cell>& c) const noexcept override;
101 std::shared_ptr<cell> get_east(const std::shared_ptr<cell>& c) const noexcept override;
102 std::shared_ptr<cell> get_west(const std::shared_ptr<cell>& c) const noexcept override;
103
107 std::shared_ptr<cell> search(int index) const noexcept override;
108
111 std::size_t num_cells() const noexcept override;
112
114 void clear_cells() noexcept;
115
118 std::vector<std::tuple<int, int, int, int>> get_vertices() const noexcept override;
119
122 void set_vertices(const std::vector<std::tuple<int, int, int, int>>& vertices) noexcept override;
123
126 std::vector<std::vector<std::uint32_t>> get_faces() const noexcept override;
127
130 void set_faces(const std::vector<std::vector<std::uint32_t>>& faces) noexcept override;
131
134 std::vector<std::uint8_t> get_pixels() const noexcept override;
135
138 void set_pixels(const std::vector<std::uint8_t>& pixels) noexcept override;
139
141 void resize(unsigned int rows, unsigned int cols, unsigned int levels) noexcept override;
142
143 private:
144 std::vector<std::shared_ptr<cell>> m_cells;
145
146 std::tuple<unsigned int, unsigned int, unsigned int> m_dimensions;
147
148 // Store topology - which cell is neighbor to which in what direction
149 // Key: cell index, Value: map of direction to neighbor cell index
150 mutable std::mutex m_topology_mutex;
151 std::unordered_map<int, std::unordered_map<Direction, int>> m_topology;
152
153 // Arbitrary data
154 std::string m_file;
155 std::string m_str;
156
157 // 3D data
158 std::vector<std::tuple<int, int, int, int>> m_vertices;
159 std::vector<std::vector<std::uint32_t>> m_faces;
160
161 // Image data
162 std::vector<std::uint8_t> m_pixels;
163 };
164} // namespace mazes
165
166#endif // GRID_H
Direction
Directional neighbors for grid topology.
Definition barriers.h:30
Cell class with links to other cells.
Definition cell.h:18
Interface for the grid class.
Definition grid_interface.h:20
Interface for grid navigation and manipulation operations.
Definition grid_operations.h:22
static constexpr std::size_t MAX_NEIGHBORS
Maximum number of neighbors a cell can have.
Definition grid_operations.h:48
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 grid &other)
Copy constructor.
std::span< const std::shared_ptr< cell > > cells() const noexcept override
Zero-copy view of all cells, in index order (level-major, then row, then column).
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 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.
std::size_t num_cells() const noexcept override
Get the count of cells in the grid.
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 clear_cells() noexcept
Cleanup cells by cleaning up links within cells.
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 > 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_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.
grid(const std::tuple< unsigned int, unsigned int, unsigned int > &dimensions)
Construct a grid using a tuple of unsigned integers.
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.
std::size_t collect_neighbors(std::shared_ptr< cell > const &c, std::span< std::shared_ptr< cell >, MAX_NEIGHBORS > out) const noexcept override
Fill a caller-provided buffer with the neighbors of a cell, avoiding allocation.
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.
~grid() override
Destructor.