CellModules
Body2DGrid.hpp
Go to the documentation of this file.
1#ifndef BODY2DGRID_HPP
2#define BODY2DGRID_HPP
3
11#include <mecacell/mecacell.h>
12#include <mecacell/movable.h>
13#include "CellGrid.hpp"
14#include "../../../../src/core/BaseBody.hpp"
15#define MOVABLE
16
21namespace Grid2D {
22
30 template<typename cell_t, class plugin_t>
31 class Body2DGrid : public MecaCell::Movable, public virtual BaseBody<plugin_t> {
32 private:
34
42
43 public:
44
48 inline explicit Body2DGrid() = default;
49
56 inline explicit Body2DGrid(const MecaCell::Vector3D &pos){
58 }
59
65
71
76 inline int getNbNeighbouringCells() const { return nbNeighbouringCells; }
77
82 inline void setNbNeighbouringCells (int n) { nbNeighbouringCells = n; }
83
90 inline double getDensity() const { return (double)(nbNeighbouringCells+1) / (double)nbNeighbouringLocations; }
91
96 inline double getBoundingBoxRadius() const { return radius; }
97
102 inline void setRadius(double _radius) { radius = _radius; }
103
108 inline Vec2D get2DPosition() const { return position2D; }
109
117
122 inline void set2DPosition(const Vec2D &p) {
123 int i = p.x();
124 int j = p.y();
125
126 int x = position2D.x();
127 int y = position2D.y();
128
129 position2D = p;
130 position = MecaCell::Vector3D(i, j, 0.);
131
132 if(this->cellPlugin != nullptr){
133 auto cellGrid = getCellGrid();
134 int c = 0;
135 int nbCellSamePosition = cellGrid->grid[x][y].size();
136 while(c < nbCellSamePosition && &(cellGrid->grid[x][y][c]->getBody()) != this) c++;
137 if(c < nbCellSamePosition){
138 cellGrid->grid[i][j].push_back(cellGrid->grid[x][y][c]);
139 cellGrid->grid[x][y].erase(cellGrid->grid[x][y].begin() + c);
140 }
141 }
142 }
143
149 inline void set2DPosition(int i, int j){ set2DPosition(Vec2D(i, j)); }
150
155 inline void setPosition(const MecaCell::Vector3D &p) { set2DPosition(Vec2D((int)p.x(), (int)p.y())); }
156
161 inline CellGrid<cell_t> * getCellGrid() { return this->cellPlugin->plugin2DGrid.getCellGrid(); }
162
169
170 std::vector<int> posibilityX;
171 std::vector<int> posibilityY;
172 int x = position2D.x();
173 int y = position2D.y();
174 auto cellGrid = getCellGrid();
175
176 //find min density in neighbour positions
177 int min = std::numeric_limits<int>::max();
178 int currentSize;
179 for (int i = -range; i <= range; ++i) {
180 for (int j = -range; j <= range; ++j) {
181 int xi = x + i;
182 int yj = y + j;
183
184 if (cellGrid->toreX){
185 if(xi < 0) xi = cellGrid->width - xi;
186 if(xi >= cellGrid->width) xi = xi - cellGrid->width;
187 }
188 if(cellGrid->toreY){
189 if(yj < 0) yj = cellGrid->height - yj;
190 if(yj >= cellGrid->height) yj = yj - cellGrid->height;
191 }
192
193 if ((xi >= 0) && (xi < cellGrid->width) && (yj >= 0) && (yj < cellGrid->height)) {
194 currentSize = cellGrid->grid[xi][yj].size();
195 if (currentSize < min) {
196 min = currentSize;
197 posibilityX.clear();
198 posibilityY.clear();
199 posibilityX.push_back(xi);
200 posibilityY.push_back(yj);
201 } else if (currentSize == min) {
202 posibilityX.push_back(xi);
203 posibilityY.push_back(yj);
204 }
205 }
206 }
207 }
208
209 //choose the position
210 if (posibilityX.size() > 1) {
211 std::uniform_int_distribution<> dis(0, posibilityX.size() - 1);
212 int choice = dis(MecaCell::Config::globalRand());
213 x = posibilityX[choice];
214 y = posibilityY[choice];
215 } else {
216 x = posibilityX[0];
217 y = posibilityY[0];
218 }
219 return Vec2D(x, y);
220 }
221 };
222}
223#endif
MecaCell::Vec pos
Definition: CellBody.hpp:34
plugin_t * cellPlugin
Definition: BaseBody.hpp:9
Template class for a 2D grid physics-based body.
Definition: Body2DGrid.hpp:31
Body2DGrid(const MecaCell::Vector3D &pos)
Constructor with position.
Definition: Body2DGrid.hpp:56
Vec2D get2DPosition() const
Gets the 2D position of the cell.
Definition: Body2DGrid.hpp:108
Body2DGrid()=default
Default constructor.
Vec2D findLowestDensityPosition(int range)
Finds one of the lowest density positions within a range. if there are multiple positions with the sa...
Definition: Body2DGrid.hpp:168
CellGrid< cell_t > * cellGrid
Definition: Body2DGrid.hpp:39
MecaCell::Vector3D position
Definition: Body2DGrid.hpp:41
double getBoundingBoxRadius() const
Gets the bounding box radius.
Definition: Body2DGrid.hpp:96
void set2DPosition(int i, int j)
Sets the 2D position of the cell.
Definition: Body2DGrid.hpp:149
void setRadius(double _radius)
Sets the bounding box radius.
Definition: Body2DGrid.hpp:102
void setPosition(const MecaCell::Vector3D &p)
Sets the 3D position of the cell.
Definition: Body2DGrid.hpp:155
void setNbNeighbouringCells(int n)
Sets the number of neighbouring cells.
Definition: Body2DGrid.hpp:82
double getDensity() const
Gets the density of the cell.
Definition: Body2DGrid.hpp:90
int getNbNeighbouringCells() const
Gets the number of neighbouring cells.
Definition: Body2DGrid.hpp:76
CellGrid< cell_t > * getCellGrid()
Gets the cell grid.
Definition: Body2DGrid.hpp:161
void setNbNeighbouringLocations(int n)
Sets the number of neighbouring locations.
Definition: Body2DGrid.hpp:70
void set2DPosition(const Vec2D &p)
Sets the 2D position of the cell.
Definition: Body2DGrid.hpp:122
MecaCell::Vector3D getPosition() const
Gets the 3D position of the cell.
Definition: Body2DGrid.hpp:116
int getNbNeighbouringLocations() const
Gets the number of neighbouring locations.
Definition: Body2DGrid.hpp:64
general purpose 3D vector/point class.
Definition: vector3D.h:20
double x() const
Definition: vector3D.h:94
double y() const
Definition: vector3D.h:100
A simple vector class template.
Definition: std.hpp:290
void push_back(const T &value)
Adds an element to the end of the vector.
Definition: std.hpp:304
void clear()
Clears the contents of the vector.
Definition: std.hpp:354
size_t size() const
Returns the number of elements in the vector.
Definition: std.hpp:320
Namespace for 2D grid-related structures and classes.
Template class representing a 2D grid of cell pointers.
Definition: CellGrid.hpp:52
Structure representing a 2D vector.
Definition: CellGrid.hpp:14
int x() const
Get the X-coordinate.
Definition: CellGrid.hpp:29
int y() const
Get the Y-coordinate.
Definition: CellGrid.hpp:35
static random_engine_t & globalRand()
access to the static global random engine.This pseudo - random generator is* used in random 3D vector...
Definition: config.hpp:30
static constexpr double DEFAULT_CELL_RADIUS
Definition: config.hpp:12