Conscience Core
CommandAreaAnalysis.h
Go to the documentation of this file.
1 
2 #ifndef CommandAreaAnalysis_h
3 #define CommandAreaAnalysis_h
4 
5 #include <limits>
6 #include <unordered_map>
7 #include <unordered_set>
8 
10 #include "./Command.h"
12 
13 #define DOUBLE_PRECISION 0.001
14 // MAXIMUM_RATIO is used to limit the number of points in the sequence returned at the end of the command. If the number of points is too much (660 000 points in the sequence was too much during tests, 130 000 points was not), the result was an error failed to parse
15 #define MAXIMUM_RATIO 200000
16 #define MIN_DIST 50
17 
18 using std::vector, std::unordered_map, std::unordered_set;
19 
28 
29 public:
30  inline static const CommandTypeId COMMAND_ID = "AREA_ANALYSIS";
37  CommandAreaAnalysis(ptr<CscEntityReflexion> entityReflexion, vector<const CscPoint3d *> areaVertices, vector<vector<const CscPoint3d *>> holes, double areaOfPerceptionWidth, const CscPoint3d *entityPosition, optional<bool> doNotPlay);
38  ~CommandAreaAnalysis() override;
39 
40 protected:
43  return entityReflexion;
44  }
45  const CscCommandExecutionResultSequence * doExecute(CscEnvironmentSimulator &environmentSimulator) override;
46 
47 private:
48 
49  bool doNotPlaySequence = false;
51  struct CSC_DLL_IMPORTEXPORT Polygon;
52  struct CSC_DLL_IMPORTEXPORT Vertex;
53  struct CSC_DLL_IMPORTEXPORT Endpoint;
54  struct CSC_DLL_IMPORTEXPORT Edge;
55  struct CSC_DLL_IMPORTEXPORT Cell;
56  struct CSC_DLL_IMPORTEXPORT EdgeCell;
57  struct CSC_DLL_IMPORTEXPORT CellGraph;
58  struct CSC_DLL_IMPORTEXPORT CellAdjacentList;
59  struct CSC_DLL_IMPORTEXPORT PriorityCell;
60  struct CSC_DLL_IMPORTEXPORT Tile;
61  struct CSC_DLL_IMPORTEXPORT Area;
62 
63  vector<const CscPoint3d *> areaVertices;
64  vector<vector<const CscPoint3d *>> holes;
65  vector<vector<CommandAreaAnalysis::Tile *>> tiles;
66  double areaOfPerceptionWidth;
67  const CscPoint3d *entityPosition;
68 
72  vector<CscPoint3d *> tilePathToPointPath(vector<CommandAreaAnalysis::Tile *> tilePath, const CscPoint3d *bottomLeftCorner);
73 
77  CscSequencePositions *generateSequenceWithPositions(CscEnvironmentSimulator &environmentSimulator, vector<CscPoint3d *> positions);
81  vector<CscPoint3d *> filterPointInsidePolygon(vector<CscPoint3d *> toFilter);
82 
89  void trapezoidalDecomposition(CommandAreaAnalysis::Area polygon, double tileSideLength, vector<Polygon *> &cells, vector<Edge *> edges);
90 
94  void processEvent(CommandAreaAnalysis::Area polygon, double tileSideLength, Vertex *eventVertex, vector<Edge *> edges, vector<Polygon *> &cells, vector<Vertex *> vertices);
95 
104  vector<CscPoint3d *> explore(vector<CommandAreaAnalysis::Cell *> spanningTree, vector<Cell *> graphCells, CommandAreaAnalysis::Area polygon, double tileSideLength, CommandAreaAnalysis::Cell *firstCell, vector<Edge *> edges);
105 
106  void showPointPathOnSceneFloor(vector<CscPoint3d *> sequence, CscEnvironmentSimulator &environmentSimulator, bool clear, bool line);
107  void createAreaTiles(int numberOfLines, int numberOfColumns, const CscPoint3d *firstPoint, CommandAreaAnalysis::Polygon polygon);
108  void freeMemory();
109 
113  static bool isFirstBeforeSecond(const CscPoint3d *first, const CscPoint3d *second);
114 
118  static bool equalsTwoDouble(double lhs, double rhs);
122  static bool equalsTwoPoints(const CscPoint3d *lhs, const CscPoint3d *rhs);
123  static void siftDown(const CscPoint3d *lexicalOrder[], int length, int i);
124 
128  static void heapSort(const CscPoint3d *lexicalOrder[], int length);
129 
134  static CscPoint3d *convertTileToPoint(Tile *tile, const CscPoint3d *firstPoint, double areaOfPerceptionWidth);
135 
139  static CommandAreaAnalysis::Polygon *createTrapezoid(const CscPoint3d *first, const CscPoint3d *second, const CscPoint3d *third, const CscPoint3d *fourth, CscEnvironmentSimulator *envi);
140 
141  struct CSC_DLL_IMPORTEXPORT Area {
142  private:
143  vector<Polygon *> forbiddenAreas;
144  vector<const CscPoint3d *> intersections;
146 
147  public:
148  Polygon *areaToExplore;
149 
150  Area(Polygon *areaToExplore, vector<Polygon *> forbiddenAreas, double &areaOfPerceptionWidth, CscEnvironmentSimulator *envi);
151 
152  vector<const CscPoint3d *> areaVertices();
153 
154  vector<Edge *> getEdges();
155 
156  bool isPointInside(const CscPoint3d *pointToCheck);
157 
158  vector<const CscPoint3d *> getLexicalOrder();
159 
163  const CscPoint3d *getNearestVisibleVertex(vector<Vertex *> vertices, vector<Edge *> edges, const CscPoint3d *triggerVertex, const CscPoint3d *vertexEndPoint);
168  bool isVertexVisible(vector<Edge *> edges, const CscPoint3d *triggerVertex, const CscPoint3d *otherVertex);
169 
175  Endpoint *getEndpoint(const CscPoint3d *vertex, double tileSideLength, vector<Edge *> edges, bool isAbove);
176 
177  Polygon *continueEvent(Vertex *triggerVertex, double tileSideLength, vector<Edge *> edges, vector<Vertex *> vertices);
178 
179  Polygon *mergeEvent(Vertex *triggerVertex, double tileSideLength, vector<Edge *> edges, vector<Vertex *> vertices);
180 
181  vector<Polygon *> outEvent(Vertex *triggerVertex, double tileSideLength, vector<Edge *> edges, vector<Vertex *> vertices);
182 
183  Polygon *splitEvent(Vertex *triggerVertex, double tileSideLength, vector<Edge *> edges, vector<Vertex *> vertices);
184  };
185 
186  struct CSC_DLL_IMPORTEXPORT Tile {
187  public:
188  const int indexX;
189  const int indexY;
190  bool isVisited = false;
191  bool accessible;
192 
193  Tile(int indexX,
194  int indexY,
195  bool accessible = true) : indexX(indexX), indexY(indexY), accessible(accessible) {}
196  };
197 
198  struct CSC_DLL_IMPORTEXPORT Edge {
199  const CscPoint3d *firstPoint;
200  const CscPoint3d *secondPoint;
201 
202  Edge(const CscPoint3d *first, const CscPoint3d *second) : firstPoint(first), secondPoint(second) {}
203 
205  std::size_t operator()(const Edge edge) const {
206  std::string concat = std::to_string(edge.firstPoint->getX()) +
207  std::to_string(edge.firstPoint->getZ()) +
208  std::to_string(edge.secondPoint->getX()) +
209  std::to_string(edge.secondPoint->getZ());
210  return std::hash<std::string>()(concat);
211  }
212  };
213 
215  bool operator()(const Edge lhs, const Edge rhs) const {
216  return (equalsTwoPoints(lhs.firstPoint, rhs.firstPoint) && equalsTwoPoints(lhs.secondPoint, rhs.secondPoint)) || (equalsTwoPoints(lhs.firstPoint, rhs.secondPoint) && equalsTwoPoints(lhs.secondPoint, rhs.firstPoint));
217  }
218  };
219 
220  const CscPoint3d *createEndPointOnEdge(const CscPoint3d *point);
221  const CscPoint3d *intersect(Edge *other);
222  bool isOnEdge(const CscPoint3d *point);
223  };
224 
225  struct CSC_DLL_IMPORTEXPORT Endpoint {
226  const CscPoint3d *point;
227  Edge *edge;
228 
229  Endpoint(const CscPoint3d *point, Edge *edge) : point(point), edge(edge) {}
230 
231  ~Endpoint() {
232  delete point;
233  }
234  };
235 
236  struct CSC_DLL_IMPORTEXPORT Vertex {
237  const CscPoint3d *point;
238  Edge *firstEdge;
239  Edge *secondEdge;
240 
241  Vertex(const CscPoint3d *point, Edge *firstEdge, Edge *secondEdge) : point(point), firstEdge(firstEdge), secondEdge(secondEdge) {}
242 
244  size_t operator()(const Vertex *v) const {
245  size_t h1 = std::hash<std::string>()(to_string(v->point->getX()) + to_string(v->point->getY()) + to_string(v->point->getZ()));
246  return h1;
247  }
248  };
250  bool operator()(const Vertex *lhs, const Vertex *rhs) const {
251  return equalsTwoPoints(lhs->point, rhs->point);
252  }
253  };
254  };
255 
256  struct CSC_DLL_IMPORTEXPORT Polygon {
257  public:
259  vector<const CscPoint3d *> polygonVertices;
260  vector<Edge *> edges;
261 
262  Polygon(vector<const CscPoint3d *> areaVertices, CscEnvironmentSimulator *envi) : polygonVertices(areaVertices), envi(envi) {
263  int j = areaVertices.size() - 1;
264  int i = 0;
265  // creates edges of the polygon according to the position of vertices in the vector
266  while (i < areaVertices.size()) {
267  Edge *firstEdge;
268  if (isFirstBeforeSecond(areaVertices.at(j), areaVertices.at(i))) {
269  firstEdge = new Edge(areaVertices.at(j), areaVertices.at(i));
270  } else {
271  firstEdge = new Edge(areaVertices.at(i), areaVertices.at(j));
272  }
273  edges.push_back(firstEdge);
274  j = i;
275  i++;
276  }
277  }
278 
282  vector<const CscPoint3d *> getLexicalOrder();
283 
284  const CscPoint3d *getPointHighestX();
285  const CscPoint3d *getPointHighestZ();
286  const CscPoint3d *getPointLowestX();
287  const CscPoint3d *getPointLowestZ();
288  const CscPoint3d *getFirstPoint();
294  vector<CommandAreaAnalysis::Tile *> boustrophedonPath(vector<vector<CommandAreaAnalysis::Tile *>> polygon, CscPoint3d *position);
299  vector<vector<CommandAreaAnalysis::Tile *>> getTranspose(vector<vector<CommandAreaAnalysis::Tile *>> polygon);
300 
304  void createAreaTiles(int numberOfLines, int numberOfColumns, double tileSideLength, vector<vector<CommandAreaAnalysis::Tile *>> &tiless);
308  bool isValid();
312  bool isPointInside(const CscPoint3d *pointToCheck);
313  bool isAdjacentTo(Polygon *other);
314  bool overlaps(Polygon *other);
315 
316  CscPoint3d *adjacencyPointWith(Polygon *other);
317  };
318 
319  struct CSC_DLL_IMPORTEXPORT Cell {
320  CommandAreaAnalysis::Polygon *polygonCell;
321  string name;
322  vector<EdgeCell *> adjacentCells;
323 
324  Cell(Polygon *polygonCell, string name) : polygonCell(polygonCell), name(name) {}
325 
326  Cell(Cell cell, vector<EdgeCell *> adjacentCells) : adjacentCells(adjacentCells) {
327  name = cell.name;
328  polygonCell = cell.polygonCell;
329  }
330 
332  std::size_t operator()(const Cell *cell) const {
333  return std::hash<std::string>()(cell->name);
334  }
335  };
336 
338  bool operator()(const Cell *lhs, const Cell *rhs) const {
339  return lhs->name == rhs->name;
340  }
341  };
342 
344  std::size_t operator()(const Cell &cell) const {
345  return std::hash<std::string>()(cell.name);
346  }
347  };
348 
350  bool operator()(const Cell &lhs, const Cell &rhs) const {
351  return lhs.name == rhs.name;
352  }
353  };
354  void initializeAdjacencyList(vector<Cell *> cellsOfGraph);
355  };
356  struct CSC_DLL_IMPORTEXPORT EdgeCell {
357  Cell *cell;
358  double weight;
359 
360  EdgeCell(Cell *cell, double weight) : cell(cell), weight(weight) {}
361  };
362 
363  struct CSC_DLL_IMPORTEXPORT CellNode {
364  public:
365  Cell *cell;
366  CellNode *parent;
367  CellNode(Cell *cell, CellNode *parent) : cell(cell), parent(parent) {}
368 
370  std::size_t operator()(const CellNode *cellNode) const {
371  return std::hash<std::string>()(cellNode->cell->name);
372  }
373  };
374 
376  bool operator()(const CellNode *lhs, const CellNode *rhs) const {
377  return lhs->cell->name == rhs->cell->name;
378  }
379  };
380  };
381  // Define a struct to represent the adjacency list of a cell
382  struct CSC_DLL_IMPORTEXPORT CellAdjacentList {
383  Cell cell;
384  vector<Cell> adjacencyList;
385  };
386 
387  struct CSC_DLL_IMPORTEXPORT PriorityCell {
388  double priority;
389  Cell *cell;
390 
391  PriorityCell(Cell *cell, double priority) : cell(cell), priority(priority) {}
392  };
393 
394  struct CSC_DLL_IMPORTEXPORT CellGraph {
395  vector<CommandAreaAnalysis::Polygon *> polygonsOfGraph;
396  vector<Cell *> cellsOfGraph;
397 
398  ~CellGraph() {
399  deleteItems(cellsOfGraph);
400  }
401 
402  CellGraph(vector<Polygon *> polygonsOfGraph) : polygonsOfGraph(polygonsOfGraph) {
403  int i = 0;
404  for (Polygon *polygon : polygonsOfGraph) {
405  cellsOfGraph.push_back(new Cell(polygon, to_string(i)));
406  i++;
407  }
408  for (Cell *cell : cellsOfGraph) {
409  cell->initializeAdjacencyList(cellsOfGraph);
410  }
411  }
412 
416  vector<Cell *> makeTree(unordered_map<Cell, optional<Cell>, Cell::CellHash, Cell::CellEqual> primMap);
417 
421  unordered_map<Cell, optional<Cell>, Cell::CellHash, Cell::CellEqual> generateSpanningTree(Cell *source);
422  };
423 };
424 
425 COMMAND_REGISTER(CommandAreaAnalysis, ptr<CscEntityReflexion>, vector<const CscPoint3d *>, vector<vector<const CscPoint3d *>> , double, const CscPoint3d *, optional<bool>)
426 
427 }
428 #endif
conscience_core::bridging::commands::CommandAreaAnalysis::Edge::EdgeHash
Definition: CommandAreaAnalysis.h:204
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellEqualPointer::operator()
bool operator()(const Cell *lhs, const Cell *rhs) const
Definition: CommandAreaAnalysis.h:338
nlohmann::to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:26470
conscience_core::axiomes::CscPoint3d
The CscPoint3d class represents a point in three-dimensional space. It is primarily used to denote a ...
Definition: Csc3dTypes.h:24
conscience_core::bridging::commands::CommandAreaAnalysis::Edge::EdgeHash::operator()
std::size_t operator()(const Edge edge) const
Definition: CommandAreaAnalysis.h:205
polygon
boost::geometry::model::polygon< boost::geometry::model::d2::point_xy< double > > polygon
Definition: CommandGenerateCompletePath.cpp:15
CSC_DLL_IMPORTEXPORT
#define CSC_DLL_IMPORTEXPORT
Definition: os.h:31
conscience_core::bridging::commands::CommandAreaAnalysis::Vertex::VertexHash::operator()
size_t operator()(const Vertex *v) const
Definition: CommandAreaAnalysis.h:244
CscSequencePositions
Definition: CscSequencePositions.h:23
Command.h
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellEqual
Definition: CommandAreaAnalysis.h:349
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellEqualPointer
Definition: CommandAreaAnalysis.h:337
conscience_core::bridging::commands::CommandAreaAnalysis::CellNode::CellNodeEqual::operator()
bool operator()(const CellNode *lhs, const CellNode *rhs) const
Definition: CommandAreaAnalysis.h:376
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellHash
Definition: CommandAreaAnalysis.h:343
conscience_core::bridging::commands::result::CscCommandExecutionResultSequence
Definition: CommandExecutionResultSequence.h:15
conscience_core::bridging::commands::CommandTypeId
string CommandTypeId
Definition: Command.h:29
conscience_core::bridging::commands::CommandAreaAnalysis::Vertex::VertexEqual
Definition: CommandAreaAnalysis.h:249
CscEntityReflexion.h
conscience_core::bridging::commands::CommandAreaAnalysis::Vertex::VertexEqual::operator()
bool operator()(const Vertex *lhs, const Vertex *rhs) const
Definition: CommandAreaAnalysis.h:250
conscience_core::bridging::commands
Definition: cartographyCommands.cpp:4
conscience_core::bridging::commands::CommandAreaAnalysis::CellNode::CellNodeHash
Definition: CommandAreaAnalysis.h:369
conscience_core::bridging::commands::CscCommand
Definition: Command.h:40
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellEqual::operator()
bool operator()(const Cell &lhs, const Cell &rhs) const
Definition: CommandAreaAnalysis.h:350
conscience_core::bridging::commands::CommandAreaAnalysis::Edge::EdgeEqual::operator()
bool operator()(const Edge lhs, const Edge rhs) const
Definition: CommandAreaAnalysis.h:215
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellHashPointer
Definition: CommandAreaAnalysis.h:331
conscience_core::bridging::commands::CommandAreaAnalysis::entityReflexion
ptr< CscEntityReflexion > entityReflexion
Definition: CommandAreaAnalysis.h:41
conscience_core::bridging::commands::CommandAreaAnalysis::CellNode::CellNodeHash::operator()
std::size_t operator()(const CellNode *cellNode) const
Definition: CommandAreaAnalysis.h:370
conscience_core::bridging::commands::CommandAreaAnalysis::CellNode::CellNodeEqual
Definition: CommandAreaAnalysis.h:375
CscEnvironmentSimulator
Definition: CscEnvironmentSimulator.h:35
conscience_core::bridging::commands::COMMAND_REGISTER
COMMAND_REGISTER(CommandFollowKinematicTrajectory, ptr< CscEntityReflexion >, int, const CscState *, string, string, string, string, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float)
CscCommandAutoParse.h
conscience_core::bridging::commands::CommandAreaAnalysis::Vertex::VertexHash
Definition: CommandAreaAnalysis.h:243
deleteItems
#define deleteItems(vectorOfItems)
Definition: conscience_util.h:33
conscience_core::bridging::commands::CommandAreaAnalysis
This class is made to allow entities to explore a given area. An entity will follow a "boustrophedon ...
Definition: CommandAreaAnalysis.h:27
conscience_core::bridging::commands::CommandAreaAnalysis::Edge::EdgeEqual
Definition: CommandAreaAnalysis.h:214
ptr
std::shared_ptr< T > ptr
Definition: CscCommon.h:29
conscience_core::bridging::commands::CommandAreaAnalysis::getEntityReflexion
ptr< CscEntityReflexion > getEntityReflexion() const override
Definition: CommandAreaAnalysis.h:42
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellHashPointer::operator()
std::size_t operator()(const Cell *cell) const
Definition: CommandAreaAnalysis.h:332
j
int j
Definition: HybridAStar.cpp:192
conscience_core::bridging::commands::CommandAreaAnalysis::Cell::CellHash::operator()
std::size_t operator()(const Cell &cell) const
Definition: CommandAreaAnalysis.h:344
i
int i
Definition: HybridAStar.cpp:191