O --- O
libPEDSIM Documentation
[HOME] [EXAMPLES] [ECOSYSTEM] [DOCUMENTATION] [DOWNLOAD]
O --- O
ped_scene.cpp
1 //
2 // pedsim - A microscopic pedestrian simulation system.
3 // Copyright (c) by Christian Gloor
4 //
5 
6 #include "ped_scene.h"
7 #include "ped_agent.h"
8 #include "ped_obstacle.h"
9 #include "ped_tree.h"
10 #include "ped_waypoint.h"
11 #include "ped_outputwriter.h"
12 
13 #include <cstddef>
14 #include <algorithm>
15 #include <stack>
16 
17 using namespace std;
18 
19 
23 Ped::Tscene::Tscene() : tree(NULL), timestep(0) {};
24 
25 
36 Ped::Tscene::Tscene(double left, double top, double width, double height) : Tscene() {
37  tree = new Ped::Ttree(this, 0, left, top, width, height);
38 }
39 
43  delete(tree);
44 }
45 
46 void Ped::Tscene::clear() {
47  // clear tree
48  treehash.clear();
49  tree->clear();
50 
51  // remove all agents
52  for(Ped::Tagent* currentAgent : agents)
53  delete currentAgent;
54  agents.clear();
55 
56  // remove all obstacles
57  for(Ped::Tobstacle* currentObstacle : obstacles)
58  delete currentObstacle;
59  obstacles.clear();
60 
61  // remove all waypoints
62  for(Ped::Twaypoint* currentWaypoint : waypoints)
63  delete currentWaypoint;
64  waypoints.clear();
65 }
66 
72  // add agent to scene
73  // (take responsibility for object deletion)
74  agents.push_back(a);
75  a->setscene(this);
76  if (tree != NULL)
77  tree->addAgent(a);
78 }
79 
85  // add obstacle to scene
86  // (take responsibility for object deletion)
87  obstacles.push_back(o);
88 
89  // then output their new position if an OutputWriter is given.
90  for (auto ow : outputwriters) {
91  ow->drawObstacle(*o);
92  }
93 }
94 
95 void Ped::Tscene::addWaypoint(Ped::Twaypoint* w) {
96  // add waypoint to scene
97  // (take responsibility for object deletion)
98  waypoints.push_back(w);
99 
100  // then output their new position if an OutputWriter is given.
101  for (auto ow : outputwriters) {
102  ow->drawWaypoint(*w);
103  }
104 }
105 
106 
110  auto it = find(agents.begin(), agents.end(), a);
111  if (it == agents.end())
112  return false;
113 
114  // remove agent from the tree
115  if (tree != NULL)
116  tree->removeAgent(a);
117 
118  // remove agent from the scene, report succesful removal
119  agents.erase(it);
120 
121  // notify the outputwriter
122  for (auto ow : outputwriters) {
123  Tagent aa = *a;
124  ow->removeAgent(aa);
125  }
126 
127  return true;
128 }
129 
133  auto it = find(obstacles.begin(), obstacles.end(), o);
134  if (it == obstacles.end())
135  return false;
136 
137  // remove obstacle from the scene, report succesful removal
138  obstacles.erase(it);
139  return true;
140 }
141 
145  /* Not sure we want that!
146  // remove waypoint from all agents
147  for(vector<Tagent*>::iterator iter = agents.begin(); iter != agents.end(); ++iter) {
148  Tagent *a = (*iter);
149  a->removeWaypoint(w);
150  }
151  */
152 
153  auto it = find(waypoints.begin(), waypoints.end(), w);
154  if (it == waypoints.end())
155  return false;
156 
157  // remove waypoint from the scene, report succesful removal
158  waypoints.erase(it);
159  return true;
160 }
161 
162 
167 void Ped::Tscene::moveAgents(double h) {
168  // update timestep
169  timestep++;
170 
171  // first update forces
172  for (Tagent* agent : agents) agent->computeForces();
173 
174  // then move agents according to their forces
175  for (Tagent* agent : agents) agent->move(h);
176 
177  // then output their new position if an OutputWriter is given.
178  for (auto ow : outputwriters) {
179  ow->writeTimeStep(timestep);
180  for (Tagent* agent : agents) ow->drawAgent(*agent);
181  }
182 }
183 
188  if (tree != NULL)
189  tree->addAgent(a);
190 }
191 
198  if (tree != NULL)
199  treehash[a]->moveAgent(a);
200 }
201 
206  if (tree != NULL)
207  tree->cut();
208 }
209 
217 set<const Ped::Tagent*> Ped::Tscene::getNeighbors(double x, double y, double dist) const {
218  // if there is no tree, return all agents
219  if (tree == NULL)
220  return set<const Ped::Tagent*>(agents.begin(), agents.end());
221 
222  // create the output list
223  list<const Ped::Tagent*> neighborList;
224  getNeighbors(neighborList, x, y, dist);
225 
226  // copy the neighbors to a set
227  return set<const Ped::Tagent*>(neighborList.begin(), neighborList.end());
228 }
229 
230 void Ped::Tscene::getNeighbors(list<const Ped::Tagent*>& neighborList, double x, double y, double dist) const {
231  stack<Ped::Ttree*> treestack;
232 
233  treestack.push(tree);
234  while(!treestack.empty()) {
235  Ped::Ttree *t = treestack.top();
236  treestack.pop();
237  if (t->isleaf) {
238  t->getAgents(neighborList);
239  }
240  else {
241  if (t->tree1->intersects(x, y, dist)) treestack.push(t->tree1);
242  if (t->tree2->intersects(x, y, dist)) treestack.push(t->tree2);
243  if (t->tree3->intersects(x, y, dist)) treestack.push(t->tree3);
244  if (t->tree4->intersects(x, y, dist)) treestack.push(t->tree4);
245  }
246  }
247 }
void placeAgent(const Ped::Tagent *a)
Definition: ped_scene.cpp:187
virtual void addObstacle(Tobstacle *o)
Definition: ped_scene.cpp:84
virtual void addAgent(Tagent *a)
Definition: ped_scene.cpp:71
virtual bool removeAgent(Tagent *a)
Definition: ped_scene.cpp:109
void moveAgent(const Ped::Tagent *a)
Definition: ped_scene.cpp:197
virtual void cleanup()
Definition: ped_scene.cpp:205
virtual bool intersects(double px, double py, double pr) const
Definition: ped_tree.cpp:226
virtual bool removeObstacle(Tobstacle *o)
Definition: ped_scene.cpp:132
virtual void moveAgents(double h)
Definition: ped_scene.cpp:167
set< const Ped::Tagent * > getNeighbors(double x, double y, double dist) const
Definition: ped_scene.cpp:217
virtual bool removeWaypoint(Twaypoint *w)
Definition: ped_scene.cpp:144
virtual ~Tscene()
Definition: ped_scene.cpp:42
virtual set< const Ped::Tagent * > getAgents() const
Definition: ped_tree.cpp:188
void setscene(Tscene *s)
Definition: ped_agent.cpp:73
O --- O

⁠(c) Christian Gloor [ c|h|g|l|o|o|r|@|s|i|l|m|a|r|i|l|.|o|r|g| ]