huc::Grid< CellInfo > Class Template Reference

#include <grid.hxx>

Classes

class  Cell
 
class  Edge
 Edge struct is a convenient struct use to store two cell pointers connected by a link/edge. More...
 
struct  Point
 Point struct is a convenient struct use to represent a 2D point for a grid (index position). More...
 

Public Member Functions

 Grid (uint32_t width, uint32_t height, bool isConnected=false)
 
void Connect (const std::shared_ptr< Cell > first, const std::shared_ptr< Cell > second)
 
void Connect (const std::shared_ptr< Cell > root, const std::vector< std::shared_ptr< Cell >> &neighbours)
 
void Disconnect (const std::shared_ptr< Cell > first, const std::shared_ptr< Cell > second)
 
void DisconnectCol (const Point &origin, const uint32_t idx, const uint32_t height, const uint32_t pathIdx)
 
void DisconnectRow (const Point &origin, const uint32_t idx, const uint32_t width, const uint32_t pathIdx)
 
uint32_t Width () const
 
uint32_t Height () const
 
std::vector< std::shared_ptr< Cell > > & operator[] (size_t n)
 
const std::vector< std::shared_ptr< Cell > > & operator[] (size_t n) const
 

Private Member Functions

Grid operator= (Grid &)
 
void Init (uint32_t width, uint32_t height, bool isConneccted)
 

Private Attributes

std::vector< std::vector< std::shared_ptr< Cell > > > data
 

Detailed Description

template<typename CellInfo = CellInfoBase>
class huc::Grid< CellInfo >

Grid - Conveniently wraps a double std::vector<Grid::Cell> and add cell connection managment. The grid class may be used to generate all kind of networks/graphs/mazes with uniform and othonormal cell positionning.

Template Parameters
CellInfoa struct used to store extra information associated with each Grid::Cell

Definition at line 44 of file grid.hxx.

Constructor & Destructor Documentation

template<typename CellInfo = CellInfoBase>
huc::Grid< CellInfo >::Grid ( uint32_t  width,
uint32_t  height,
bool  isConnected = false 
)
inlineexplicit

Grid constructor.

Parameters
widththe desired width for the Grid.
heightthe desired height for the Grid.
isConnectedwhether or not the cells of the grid are connected at the initialization.

Definition at line 52 of file grid.hxx.

53  { Init(width, height, isConnected); }
void Init(uint32_t width, uint32_t height, bool isConneccted)
Definition: grid.hxx:203

Member Function Documentation

template<typename CellInfo = CellInfoBase>
void huc::Grid< CellInfo >::Connect ( const std::shared_ptr< Cell first,
const std::shared_ptr< Cell second 
)
inline

Connect with a bidirectionnal link first and second.

Parameters
firstthe cell to be connected to the second cell.
secondthe cell to be connected to the first cell.
Returns
void.

Definition at line 102 of file grid.hxx.

103  {
104  first->connectedCells.insert(second);
105  second->connectedCells.insert(first);
106  }
template<typename CellInfo = CellInfoBase>
void huc::Grid< CellInfo >::Connect ( const std::shared_ptr< Cell root,
const std::vector< std::shared_ptr< Cell >> &  neighbours 
)
inline

Connect with bidirectionnal links the root with the neighbours.

Parameters
rootthe root to be connected to the neighbours.
neighbourslist of cells to be connected to the root.
Returns
void.

Definition at line 114 of file grid.hxx.

115  {
116  if (neighbours.size() < 1)
117  return;
118 
119  for (auto it = neighbours.begin(); it != neighbours.end(); ++it)
120  {
121  root->connectedCells.insert(*it);
122  (*it)->connectedCells.insert(root);
123  }
124  }
template<typename CellInfo = CellInfoBase>
void huc::Grid< CellInfo >::Disconnect ( const std::shared_ptr< Cell first,
const std::shared_ptr< Cell second 
)
inline

Disconnect the bidirectionnal link between first and second.

Parameters
firstthe cell to be disconnected to the second cell.
secondthe cell to be disconnected to the first cell.
Returns
void.

Definition at line 132 of file grid.hxx.

133  {
134  first->connectedCells.erase(second);
135  second->connectedCells.erase(first);
136  }
template<typename CellInfo = CellInfoBase>
void huc::Grid< CellInfo >::DisconnectCol ( const Point origin,
const uint32_t  idx,
const uint32_t  height,
const uint32_t  pathIdx 
)
inline

Disconnect a column of cells - Build a wall between connected cells. Each cell of this column is disconnected to its right (East) neighboor except the one at the pathIdx. If the cells does not have a east neighboor connected : nothing append.

Parameters
originthe origin point on the grid setting the current relative position.
idxthe relative index of the column of cells to be disconnected.
heightthe size of wall constructed. If it exceeds the size of the Grid: wall will be construted until the border.
pathIdxthe relative index of the path (door) within the wall. No path is created if pathIdx >= height.
Returns
void.

Definition at line 150 of file grid.hxx.

151  {
152  if (origin.x + idx >= this->Width() - 1 || origin.y >= this->Height())
153  return;
154 
155  for (uint32_t y = 0; y < std::min(height, this->Height() - origin.y); ++y)
156  {
157  if (y == pathIdx)
158  continue;
159 
160  this->Disconnect(this->data[origin.x + idx][origin.y + y],
161  this->data[origin.x + idx + 1][origin.y + y]);
162  }
163  }
void Disconnect(const std::shared_ptr< Cell > first, const std::shared_ptr< Cell > second)
Definition: grid.hxx:132
uint32_t Height() const
Definition: grid.hxx:193
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201
template<typename CellInfo = CellInfoBase>
void huc::Grid< CellInfo >::DisconnectRow ( const Point origin,
const uint32_t  idx,
const uint32_t  width,
const uint32_t  pathIdx 
)
inline

Disconnect a row of cells - Build a wall between connected cells. Each cell of this row is disconnected to its right (East) neighboor except the one at the pathIdx. If the cells does not have a east neighboor connected : nothing append.

Parameters
originthe origin point on the grid setting the current relative position.
idxthe relative index of the row of cells to be disconnected.
widththe size of wall constructed. If it exceeds the size of the Grid: wall will be construted until the border.
pathIdxthe relative index of the path (door) within the wall. No path is created if pathIdx >= width.
Returns
void.

Definition at line 177 of file grid.hxx.

178  {
179  if (origin.y + idx >= this->Height() - 1 || origin.x >= this->Width())
180  return;
181 
182  for (uint32_t x = 0; x < std::min(width, this->Width() - origin.x); ++x)
183  {
184  if (x == pathIdx)
185  continue;
186 
187  this->Disconnect(this->data[origin.x + x][origin.y + idx],
188  this->data[origin.x + x][origin.y + idx + 1]);
189  }
190  }
void Disconnect(const std::shared_ptr< Cell > first, const std::shared_ptr< Cell > second)
Definition: grid.hxx:132
uint32_t Width() const
Definition: grid.hxx:192
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201
template<typename CellInfo = CellInfoBase>
uint32_t huc::Grid< CellInfo >::Height ( ) const
inline

Definition at line 193 of file grid.hxx.

193 { return (data.size() > 0) ? data[0].size() : 0; }
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201
template<typename CellInfo = CellInfoBase>
void huc::Grid< CellInfo >::Init ( uint32_t  width,
uint32_t  height,
bool  isConneccted 
)
inlineprivate

Definition at line 203 of file grid.hxx.

204  {
205  // Generate the Grid
206  this->data.resize(width);
207  for (uint32_t x = 0; x < width; ++x)
208  {
209  this->data[x].reserve(height);
210  for (uint32_t y = 0; y < height; ++y)
211  {
212  this->data[x].push_back(std::shared_ptr<Cell>(new Cell(x, y)));
213 
214  // Connect West
215  if (isConneccted && x > 0)
216  this->Connect(this->data[x][y], this->data[x-1][y]);
217  // Connect North
218  if (isConneccted && y > 0)
219  this->Connect(this->data[x][y], this->data[x][y-1]);
220  }
221  }
222  }
void Connect(const std::shared_ptr< Cell > first, const std::shared_ptr< Cell > second)
Definition: grid.hxx:102
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201
template<typename CellInfo = CellInfoBase>
Grid huc::Grid< CellInfo >::operator= ( Grid< CellInfo > &  )
private
template<typename CellInfo = CellInfoBase>
std::vector<std::shared_ptr<Cell> >& huc::Grid< CellInfo >::operator[] ( size_t  n)
inline

Definition at line 196 of file grid.hxx.

196 { return this->data[n]; }
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201
template<typename CellInfo = CellInfoBase>
const std::vector<std::shared_ptr<Cell> >& huc::Grid< CellInfo >::operator[] ( size_t  n) const
inline

Definition at line 197 of file grid.hxx.

197 { return this->data[n]; }
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201
template<typename CellInfo = CellInfoBase>
uint32_t huc::Grid< CellInfo >::Width ( ) const
inline

Definition at line 192 of file grid.hxx.

192 { return data.size(); }
std::vector< std::vector< std::shared_ptr< Cell > > > data
Definition: grid.hxx:201

Member Data Documentation

template<typename CellInfo = CellInfoBase>
std::vector<std::vector<std::shared_ptr<Cell> > > huc::Grid< CellInfo >::data
private

Definition at line 201 of file grid.hxx.


The documentation for this class was generated from the following file: