Map.cpp
Go to the documentation of this file.
1 
7 #include "Map.h"
8 
10 {
11  cellSize = 1;
12  height = 0;
13  width = 0;
14  grid = nullptr;
15  obstacles = nullptr;
16 }
17 
18 
19 Map::Map(float cellSize, std::vector<std::vector<int>> &grid, std::vector<std::vector<Point>> &obstacles)
20 {
21  this->cellSize = cellSize;
22  this->grid = new std::vector<std::vector<int>>(grid);
23  this->obstacles = new std::vector<std::vector<ObstacleSegment>>();
24 
25  height = this->grid->size();
26  width = 0;
27  if(height)
28  {
29  width = (*this->grid)[0].size();
30  }
31 
32  int idCounter = 0;
33 
34  for(auto obstacle : obstacles)
35  {
36  if(obstacle.size() < 2)
37  {
38  continue;
39  }
40 
41  bool rCvx = true, lCvx;
42  std::vector<ObstacleSegment> tmpObstacle;
43  for(int i = 0; i < obstacle.size(); i++)
44  {
45  Vertex left = obstacle[(i == 0 ? obstacle.size() - 1 : i - 1)];
46  Vertex right = obstacle[i];
47  Vertex next = obstacle[(i == obstacle.size() - 1 ? 0 : i + 1)];
48  lCvx = rCvx;
49  rCvx = true;
50  if (obstacle.size() > 2)
51  {
52  rCvx = (left-next).Det(right - left) >= 0.0f;
53  }
54  right.SetConvex(rCvx);
55  left.SetConvex(lCvx);
56 
57  tmpObstacle.emplace_back(ObstacleSegment(idCounter, left, right));
58  idCounter++;
59  }
60  tmpObstacle[0].left.SetConvex(rCvx);
61  this->obstacles->push_back(tmpObstacle);
62  tmpObstacle.clear();
63  }
64 
65  for(auto &obstacle : (*this->obstacles))
66  {
67  for(int i = 0; i < obstacle.size(); i++)
68  {
69  obstacle[i].next = (i == obstacle.size()-1) ? &obstacle.at(0) : &obstacle.at(i+1);
70  obstacle[i].prev = (i == 0) ? &obstacle.at(obstacle.size()-1) : &obstacle.at(i-1);
71  }
72  }
73 }
74 
75 
76 
77 Map::Map(const Map &obj)
78 {
79  cellSize = obj.cellSize;
80  grid = (obj.grid == nullptr) ? nullptr : new std::vector<std::vector<int>>(*obj.grid);
81  obstacles = (obj.obstacles == nullptr) ? nullptr : new std::vector<std::vector<ObstacleSegment>>(*obj.obstacles);
82  height = obj.height;
83  width = obj.width;
84 }
85 
86 
88 {
89  if(grid != nullptr)
90  {
91  delete grid;
92  grid = nullptr;
93  }
94 
95  if(obstacles != nullptr)
96  {
97  delete obstacles;
98  obstacles = nullptr;
99  }
100 }
101 
102 
103 bool Map::CellIsObstacle(int i, int j) const
104 {
105  return ((*grid)[i][j] != CN_GC_NOOBS);
106 }
107 
108 
109 bool Map::CellIsTraversable(int i, int j) const
110 {
111  return ((*grid)[i][j] == CN_GC_NOOBS);
112 }
113 
114 
115 bool Map::CellOnGrid(int i, int j) const
116 {
117  return (i < height && i >= 0 && j < width && j >= 0);
118 }
119 
120 
121 unsigned int Map::GetHeight() const
122 {
123  return height;
124 }
125 
126 
127 unsigned int Map::GetWidth() const
128 {
129  return width;
130 }
131 
132 
133 float Map::GetCellSize() const
134 {
135  return cellSize;
136 }
137 
138 
139 Node Map::GetClosestNode(const Point &point) const
140 {
141  Node res;
142  res.i = height - 1 - (int) (point.Y() / cellSize);
143  res.j = (int) (point.X() / cellSize);
144 
145  if(res.i < 0)
146  {
147  res.i = 0;
148  }
149  if(res.i > height - 1)
150  {
151  res.i = height - 1;
152  }
153  if(res.j < 0)
154  {
155  res.j = 0;
156  }
157  if(res.j > width - 1)
158  {
159  res.j = width - 1;
160  }
161 
162  return res;
163 }
164 
165 
166 Point Map::GetPoint(const Node &node) const
167 {
168  return {(node.j * cellSize + cellSize/2), (height - 1 - node.i) * cellSize + cellSize/2};
169 }
170 
171 const std::vector<std::vector<ObstacleSegment>> &Map::GetObstacles() const
172 {
173  return *obstacles;
174 }
175 
176 Map &Map::operator =(const Map &obj)
177 {
178  if(this != &obj)
179  {
180  cellSize = obj.cellSize;
181  height = obj.height;
182  width = obj.width;
183 
184  if(grid != nullptr)
185  {
186  delete grid;
187  }
188  grid = (obj.grid == nullptr) ? nullptr : new std::vector<std::vector<int>>(*obj.grid);
189 
190  if(obstacles != nullptr)
191  {
192  delete obstacles;
193  }
194  obstacles = (obj.obstacles == nullptr) ? nullptr : new std::vector<std::vector<ObstacleSegment>>(*obj.obstacles);
195  }
196  return *this;
197 }
198 
199 
200 
201 
unsigned int GetWidth() const
Return width of grid.
Definition: Map.cpp:127
The Node class defines a cell of grid (see Map class)
Definition: Geom.h:21
void SetConvex(bool cvx)
Sets convexity of vertex.
Definition: Geom.cpp:61
bool CellIsTraversable(int i, int j) const
Check Cell (i,j) is traversable.
Definition: Map.cpp:109
bool CellOnGrid(int i, int j) const
Check Cell (i,j) is inside grid.
Definition: Map.cpp:115
File contains Map class.
float Y() const
Returns Y-coordinate of the point.
Definition: Geom.cpp:30
The ObstacleSegment class defines an edge of an obstacle polygon.
Definition: Geom.h:274
float GetCellSize() const
Return size of cell side.
Definition: Map.cpp:133
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
~Map()
Map destructor.
Definition: Map.cpp:87
const std::vector< std::vector< ObstacleSegment > > & GetObstacles() const
Return obstacles in form of polygons.
Definition: Map.cpp:171
Point GetPoint(const Node &node) const
Return center of grid cell in orthogonal coordinates.
Definition: Map.cpp:166
bool CellIsObstacle(int i, int j) const
Check Cell (i,j) is obstacle.
Definition: Map.cpp:103
#define CN_GC_NOOBS
Not obstacle cell in XML.
Definition: Const.h:143
Map class describes static environment.
Definition: Map.h:36
int i
The number of row. Part of (i,j) address of cell on grid.
Definition: Geom.h:24
Map & operator=(const Map &obj)
Assignment operator.
Definition: Map.cpp:176
Map()
Map default constructor.
Definition: Map.cpp:9
int j
The number of column. Part of (i,j) address of cell on grid.
Definition: Geom.h:25
float X() const
Returns X-coordinate of the point.
Definition: Geom.cpp:24
The Vertex class defines a vertex of an obstacle polygon.
Definition: Geom.h:213
Node GetClosestNode(const Point &point) const
Return cell to which point in orthogonal coordinates belong.
Definition: Map.cpp:139
unsigned int GetHeight() const
Return height of grid.
Definition: Map.cpp:121


ORCAStar
Author(s): Stepan Drgachev
autogenerated on Wed Jul 15 2020 16:13:14