CellModules
Plugin2DGrid.hpp
Go to the documentation of this file.
1#ifndef PLUGIN2DGRID_HPP
2#define PLUGIN2DGRID_HPP
3
11#include <vector>
12#include <math.h>
13#include "Body2DGrid.hpp"
14
19namespace Grid2D {
20
27 template<typename cell_t>
29
30 private:
42
43 int x = c->getBody().get2DPosition().x();
44 int y = c->getBody().get2DPosition().y();
45
46 if(!cellGrid.toreX && !cellGrid.toreY){
47 if ((x == 0 || x == cellGrid.width - 1) && (y == 0 || y == cellGrid.height - 1)) {
48 c->getBody().setNbNeighbouringLocations(4);
49 } else if (x == 0 || x == cellGrid.width - 1 || y == 0 || y == cellGrid.height - 1) {
50 c->getBody().setNbNeighbouringLocations(6);
51 } else {
52 c->getBody().setNbNeighbouringLocations(9);
53 }
54 }else{
55 c->getBody().setNbNeighbouringLocations(9);
56 }
57 }
58
63 void computeNeighbouringCells(cell_t * c) {
65 int x = c->getBody().get2DPosition().x();
66 int y = c->getBody().get2DPosition().y();
67 c->clearConnectedCells();
68 for (int i = -1; i <= 1; ++i) {
69 for (int j = -1; j <= 1; ++j) {
70 int xi = x + i;
71 int yj = y + j;
72 if ((xi >= 0) && (xi < cellGrid.width) && (yj >= 0) && (yj < cellGrid.height)) {
73 for(auto cell : cellGrid.grid[xi][yj]){
74 if(cell != c) c->addConnectedCell(cell);
75 }
76 }else if (cellGrid.toreX && cellGrid.toreY){
77 if(xi < 0) xi = cellGrid.width -1;
78 if(xi >= cellGrid.width) xi = 0;
79 if(yj < 0) yj = cellGrid.height -1;
80 if(yj >= cellGrid.height) yj = 0;
81 for(auto cell : cellGrid.grid[xi][yj]){
82 if(cell != c) c->addConnectedCell(cell);
83 }
84 }else if(cellGrid.toreX && (yj >= 0) && (yj < cellGrid.height)){
85 if(xi < 0) xi = cellGrid.width -1;
86 if(xi >= cellGrid.width) xi = 0;
87 for(auto cell : cellGrid.grid[xi][yj]){
88 if(cell != c) c->addConnectedCell(cell);
89 }
90 }else if(cellGrid.toreY && (xi >= 0) && (xi < cellGrid.width)){
91 if(yj < 0) yj = cellGrid.height -1;
92 if(yj >= cellGrid.height) yj = 0;
93 for(auto cell : cellGrid.grid[xi][yj]){
94 if(cell != c) c->addConnectedCell(cell);
95 }
96 }
97 }
98 }
99 c->getBody().setNbNeighbouringCells(c->getConnectedCells().size());
100 }
101
102 public:
107
108
114 void resizeCellGrid(int w, int h){
115 cellGrid.resizeGrid(w,h);
116 }
117
122 void resizeCellGrid(int size){
123 resizeCellGrid(size,size);
124 }
125
130 void setToreX(bool b){
131 cellGrid.setToreX(b);
132 }
133
138 void setToreY(bool b){
139 cellGrid.setToreY(b);
140 }
141
147 void setTore(bool x, bool y){ cellGrid.setTore(x,y); }
148
153 inline double getNbOccupiedPositions() const{
154 return nbOccupiedPositions;
155 }
156
163 inline double getOccupationSurfaceRatio() const{
164 return ((double) nbOccupiedPositions)/(cellGrid.width * cellGrid.height);
165 }
166
173 template<typename world_t>
174 inline double getGlobalDensity(world_t *w) const{
175 return (double) w->cells.size()/(cellGrid.width * cellGrid.height);
176 }
177
182 inline CellGrid<cell_t> *getCellGrid() { return &cellGrid; }
183
189 template<typename world_t>
190 void onAddCell(world_t *w) {
191 for (cell_t *c : w->newCells) {
192 int x = c->getBody().get2DPosition().x();
193 int y = c->getBody().get2DPosition().y();
194 cellGrid.grid[x][y].push_back(c);
195 }
196 }
197
203 template<typename world_t>
204 void preBehaviorUpdate(world_t *w){
205 //updatePositions();
206 for (cell_t *c : w->cells) {
208 }
209 }
210
216 template<typename world_t>
217 void preDeleteDeadCellsUpdate(world_t *w) {
218 for (cell_t *c : w->cells) {
219 if (c->isDead()) {
220 int x = c->getBody().get2DPosition().x();
221 int y = c->getBody().get2DPosition().y();
222 for (int i = 0; i < cellGrid.grid[x][y].size(); ++i) {
223 if (cellGrid.grid[x][y][i] == c) {
224 cellGrid.grid[x][y].erase(cellGrid.grid[x][y].begin() + i);
225 }
226 }
227 }
228 }
229 }
230 };
231}
232#endif
Defines the Body2DGrid class for 2D grid physics-based bodies.
Template class for a 2D grid physics-based plugin.
double getOccupationSurfaceRatio() const
Gets the occupation surface ratio.
void computeNeighbouringCells(cell_t *c)
Computes the neighbouring cells for a given cell.
void resizeCellGrid(int w, int h)
Resizes the cell grid to the given width and height.
double getGlobalDensity(world_t *w) const
Gets the global density of the grid.
void onAddCell(world_t *w)
Hook called when cells is added to the world.
double getNbOccupiedPositions() const
Gets the number of occupied positions.
void setToreX(bool b)
Sets the toric property on the X-axis.
void preDeleteDeadCellsUpdate(world_t *w)
Hook called before deleting dead cells from the world.
void resizeCellGrid(int size)
Resizes the cell grid to a square of the given size.
void setTore(bool x, bool y)
Sets the toric properties on both X and Y axes.
CellGrid< cell_t > * getCellGrid()
Gets the cell grid.
void updateNeighbouringLocation(cell_t *c)
Updates the number of neighbouring locations for a cell.
CellGrid< cell_t > cellGrid
Plugin2DGrid()
Default constructor.
void setToreY(bool b)
Sets the toric property on the Y-axis.
void preBehaviorUpdate(world_t *w)
Hook called before the behavior update of the world.
Namespace for 2D grid-related structures and classes.
Template class representing a 2D grid of cell pointers.
Definition: CellGrid.hpp:52