Agent.cpp
Go to the documentation of this file.
1 
6 #include "Agent.h"
7 
8 
10 {
11  id = -1;
12  start = Point();
13  goal = Point();
14  planner = nullptr;
15  options = nullptr;
16  map = nullptr;
17  Neighbours = std::vector <std::pair<float, Agent*>>();
18  NeighboursObst = std::vector <std::pair<float, ObstacleSegment>>();
19  ORCALines = std::vector <Line>();
20  position = Point();
21  prefV = Point();
22  newV = Point();
23  currV = Point();
24  param = AgentParam();
25  invTimeBoundaryObst = 0;
26  invTimeBoundary = 0;
27  collisions = 0;
28  collisionsObst = 0;
29  maxSqObstDist = 0;
30 }
31 
32 
33 Agent::Agent(const int &id, const Point &start, const Point &goal, const Map &map, const EnvironmentOptions &options, AgentParam param)
34 {
35 
36  this->id = id;
37  this->start = start;
38  this->goal = goal;
39  this->map = &map;
40  this->options = &options;
41  this->param = param;
42  Neighbours = std::vector <std::pair<float, Agent*>>();
43  Neighbours.reserve((unsigned int)param.agentsMaxNum);
44  NeighboursObst = std::vector <std::pair<float, ObstacleSegment>>();
45  ORCALines = std::vector <Line>();
46  ORCALines.reserve((unsigned int)param.agentsMaxNum);
47  position = start;
48  prefV = Point();
49  newV = Point();
50  currV = Point();
51  invTimeBoundaryObst = 1/param.timeBoundaryObst;
52  invTimeBoundary = 1/param.timeBoundary;
53  collisions = 0;
54  collisionsObst = 0;
55  maxSqObstDist = std::pow((param.timeBoundaryObst * param.maxSpeed + param.radius), 2.0f);
56 
57 }
58 
59 
60 Agent::Agent(const Agent &obj)
61 {
62  this->id = obj.id;
63  this->start = obj.start;
64  this->goal = obj.goal;
65  this->map = obj.map;
66  this->options = obj.options;
67  this->param = obj.param;
68 
69  if(obj.planner != nullptr)
70  {
71  this->planner = obj.planner->Clone();
72  }
73 
74  Neighbours = obj.Neighbours;
75  NeighboursObst = obj.NeighboursObst;
76  ORCALines = obj.ORCALines;
77  position = obj.position;
78  prefV = obj.prefV;
79  newV = obj.newV;
80  currV = obj.currV;
81  invTimeBoundaryObst = obj.invTimeBoundaryObst;
82  invTimeBoundary = obj.invTimeBoundary;
83  collisions = obj.collisions;
84  collisionsObst = obj.collisionsObst;
85  maxSqObstDist = obj.maxSqObstDist;
86 }
87 
88 
90 {
91  if(planner != nullptr)
92  {
93  delete planner;
94  planner = nullptr;
95  }
96  options= nullptr;
97  map = nullptr;
98  Neighbours.clear();
99 }
100 
101 
103 {
104  return ((this->position - this->goal).EuclideanNorm() < options->delta);
105 }
106 
107 
108 bool Agent::operator == (const Agent &another) const
109 {
110  return this->id == another.id;
111 }
112 
113 
114 bool Agent::operator != (const Agent &another) const
115 {
116  return this->id != another.id;
117 }
118 
119 
120 void Agent::AddNeighbour(Agent &neighbour, float distSq)
121 {
122  float sightSq = param.sightRadius * param.sightRadius;
123 
124  if(distSq >= sightSq)
125  {
126  return;
127  }
128  int i = 0;
129  auto tmpit = Neighbours.begin();
130  while(tmpit != Neighbours.end() && i < param.agentsMaxNum && Neighbours[i].first < distSq)
131  {
132  i++;
133  tmpit++;
134  }
135  if(i < param.agentsMaxNum)
136  {
137  Neighbours.insert(tmpit,std::pair<float, Agent *>(distSq, &neighbour));
138  }
139 
140 }
141 
142 
143 void Agent::SetPosition(const Point &pos)
144 {
145  position = pos;
146 }
147 
148 
150 {
151  return position;
152 }
153 
154 
156 {
157  NeighboursObst.clear();
158  std::vector<std::vector<ObstacleSegment>> tmpObstacles = map->GetObstacles();
159  float distSq = 0;
160 
161  for(int i = 0; i < tmpObstacles.size(); i++)
162  {
163  for(int j = 0; j < tmpObstacles[i].size(); j++)
164  {
165 
166  distSq = Utils::SqPointSegDistance(tmpObstacles[i][j].left, tmpObstacles[i][j].right, position);
167  if(distSq < maxSqObstDist)
168  {
169  NeighboursObst.push_back({distSq, tmpObstacles[i][j]});
170  }
171  }
172  }
173 
174  std::sort(NeighboursObst.begin(), NeighboursObst.end(), Utils::Less<ObstacleSegment>);
175 }
176 
177 
179 {
180  return currV;
181 }
182 
183 
184 std::pair<unsigned int, unsigned int> Agent::GetCollision() const
185 {
186  return {collisions, collisionsObst};
187 }
188 
189 
191 {
192  return planner->CreateGlobalPath();
193 }
194 
195 
196 int Agent::GetID() const
197 {
198  return id;
199 }
200 
201 
202 float Agent::GetRadius() const
203 {
204  return param.radius;
205 }
206 
207 
209 {
210 
211  if(this != &obj)
212  {
213  start = obj.start;
214  goal = obj.goal;
215  id = obj.id;
216  position = obj.position;
217  prefV = obj.prefV;
218  newV = obj.newV;
219  currV = obj.currV;
220  param = obj.param;
221  invTimeBoundaryObst = obj.invTimeBoundaryObst;
222  invTimeBoundary = obj.invTimeBoundary;
223  maxSqObstDist = obj.maxSqObstDist;
224  collisions = obj.collisions;
225  collisionsObst = obj.collisionsObst;
226  ORCALines = obj.ORCALines;
227  NeighboursObst = obj.NeighboursObst;
228  Neighbours = obj.Neighbours;
229  options = obj.options;
230  map = obj.map;
231  if(planner != nullptr)
232  {
233  delete planner;
234  planner = nullptr;
235  }
236  planner = (obj.planner == nullptr) ? nullptr : obj.planner->Clone();;
237  }
238  return *this;
239 }
240 
241 
243 {
244  return nextForLog;
245 }
246 
247 void Agent::SetVelocity(const Point &vel)
248 {
249  currV = vel;
250 }
251 
252 
254 {
255  return goal;
256 }
257 
258 
260 {
261  return start;
262 }
263 
264 
266 {
267  return param;
268 }
bool operator==(const Agent &another) const
Comparisons operator. Compares id of agents.
Definition: Agent.cpp:108
Agent & operator=(const Agent &obj)
Assignment operator.
Definition: Agent.cpp:208
virtual ~Agent()
Virtual destructor.
Definition: Agent.cpp:89
Point GetStart() const
Returns start of agent. For ROS Simulation purposes.
Definition: Agent.cpp:259
virtual bool isFinished()
Checks, what the agent is on finish. To achieve finish agent should be at least in delta distance fro...
Definition: Agent.cpp:102
float radius
Size of the agent (radius of the agent).
Definition: Agent.h:75
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
float timeBoundaryObst
Time within which ORCA algorithm ensures collision avoidance with neighbor obstacles.
Definition: Agent.h:74
int agentsMaxNum
Number of neighbors, that the agent takes into account.
Definition: Agent.h:78
int GetID() const
Returns identifier of agent.
Definition: Agent.cpp:196
Class AgentParam contains agent and algoritms parameters.
Definition: Agent.h:38
virtual Agent * Clone() const =0
Abstract method for cloning inheritors objects. Implementations of this method should create copy of ...
float GetRadius() const
Returns radius of agent.
Definition: Agent.cpp:202
Agent class implements base agent interface and behavior.
Definition: Agent.h:95
bool operator!=(const Agent &another) const
Comparisons operator. Compares id of agents.
Definition: Agent.cpp:114
float timeBoundary
Time within which ORCA algorithm ensures collision avoidance with neighbor agents.
Definition: Agent.h:73
Point GetPosition() const
Returns current position of Agent.
Definition: Agent.cpp:149
void UpdateNeighbourObst()
Computes neighbouring obstacles.
Definition: Agent.cpp:155
Class EnvironmentOptions contains environment and algoritms parameters.
virtual void SetVelocity(const Point &vel)
Sets current velocity. Should be used if the new velocity may not coincide with the computed...
Definition: Agent.cpp:247
Map class describes static environment.
Definition: Map.h:36
float maxSpeed
Maximum speed of agent.
Definition: Agent.h:77
AgentParam GetParam() const
Returns parameters of agent. For ROS Simulation purposes.
Definition: Agent.cpp:265
std::pair< unsigned int, unsigned int > GetCollision() const
Returns collisions count. For debug purposes.
Definition: Agent.cpp:184
Point GetGoal() const
Returns goal of agent. For ROS Simulation purposes.
Definition: Agent.cpp:253
bool InitPath()
Starts pathfinding from start to goal position.
Definition: Agent.cpp:190
File contains Agent class and AgentParam class.
Agent()
Agent default constructor.
Definition: Agent.cpp:9
virtual void AddNeighbour(Agent &neighbour, float distSq)
Adds new neighbour. Only agents inside sight radius will be adeed. Can be ovveriden.
Definition: Agent.cpp:120
float SqPointSegDistance(Point L1, Point L2, Point P)
Computes squared euclidean distance from point to line segment.
Definition: Geom.cpp:68
Point GetNext() const
Returns current goal of agent. For debug purposes.
Definition: Agent.cpp:242
virtual void SetPosition(const Point &pos)
SetsS current position of agent. Can be ovveriden.
Definition: Agent.cpp:143
Point GetVelocity() const
Returns current velocity of agent.
Definition: Agent.cpp:178


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