O --- O
libPEDSIM Documentation
[HOME] [EXAMPLES] [ECOSYSTEM] [DOCUMENTATION] [DOWNLOAD]
O --- O
ped_outputwriter.cpp
1 //
2 // pedsim - A microscopic pedestrian simulation system.
3 // Copyright (c) by Christian Gloor
4 //
5 
6 #include "ped_outputwriter.h"
7 #include "ped_obstacle.h"
8 
9 #include <unordered_map>
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 #include <string>
14 #include <memory>
15 
16 #ifndef WIN32
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <sys/types.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #else
25 #include <WinSock2.h>
26 #pragma comment(lib, "Ws2_32.lib")
27 #define socklen_t int
28 #endif // !WIN32
29 
30 
31 using namespace std;
32 
39 
43  // outfile.open("pedsim_out.txt");
44  // outfile << "# PedSim output generated using libpedsim version " << Ped::LIBPEDSIM_VERSION << endl;
45  // outfile << "" << endl;
46 }
47 
51  outfile_.open("pedsim_out.txt");
52  outfile_ << "" << endl;
53 }
54 
58  outfile_.close();
59 }
60 
64  // socket.close()???
65 }
66 
70 #ifdef WIN32
71  WSADATA wsaData;
72  int result;
73  result = WSAStartup(MAKEWORD(2, 2), &wsaData);
74  if (result != 0) {
75  cout << "WSAStartup failed: " << result << endl;
76  }
77 #endif
78 
79  socket_ = socket(AF_INET, SOCK_DGRAM, 0);
80 
81  socklen_t optlen = sizeof(int);
82  int optval = 1;
83  getsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, (char*)&optval, &optlen);
84 
85  write("<reset/>");
86 }
87 
88 
89 void Ped::UDPOutputWriter::write(string message) {
90  struct sockaddr_in to;
91  int bytes_sent;
92  const char *msg;
93 
94  memset(&to, 0, sizeof(to));
95  to.sin_family = AF_INET;
96  to.sin_addr.s_addr = inet_addr("127.0.0.1");
97  to.sin_port = htons(2222);
98 
99  string ext_message = "<message>" + message + "</message>";
100  msg = ext_message.c_str();
101  bytes_sent = sendto(socket_, msg, strlen(msg), 0, (struct sockaddr*)&to, sizeof(to));
102 }
103 
104 
105 void Ped::FileOutputWriter::write(string message) {
106  outfile_ << message << endl;
107 }
108 
109 
114  std::ostringstream msg;
115  msg << "" << endl;
116  msg << "<scenario name=\"" << name << "\" />" << endl;
117  write(msg.str());
118 }
119 
120 
131 
132 // reset not implemented as a API command yet. However, it is sent
133 // during initialization by the constructor to reset the scene.
134 
135 
140  std::ostringstream msg;
141  msg << "# End of PedSim output."<< endl;
142  write(msg.str());
143 }
144 
145 
156 
159 void Ped::XMLOutputWriter::writeTimeStep (long int timestep) {
160  std::ostringstream msg;
161  msg << "<timestep value=\"" << timestep << "\" />" << endl;
162  write(msg.str());
163 }
164 
178 
183  std::ostringstream msg;
184  msg << "<position type=\"agent\" ";
185  msg << "id=\"" << a.getid() << "\" ";
186  msg << "x=\"" << a.getPosition().x << "\" ";
187  msg << "y=\"" << a.getPosition().y << "\" ";
188  msg << "/>" << endl;
189  write(msg.str());
190 }
191 
203 
204 
209  std::ostringstream msg;
210  msg << "<remove type=\"agent\" ";
211  msg << "id=\"" << a.getid() << "\" ";
212  msg << "/>" << endl;
213  write(msg.str());
214 }
215 
216 
221  std::ostringstream msg;
222  msg << "<position type=\"obstacle\" ";
223  msg << "id=\"" << o.getid() << "\" ";
224  msg << "x=\"" << o.getStartPoint().x << "\" ";
225  msg << "y=\"" << o.getStartPoint().y << "\" ";
226  msg << "dx=\"" << o.getEndPoint().x - o.getStartPoint().x << "\" ";
227  msg << "dy=\"" << o.getEndPoint().y - o.getStartPoint().y << "\" ";
228  msg << "/>" << endl;
229  write(msg.str());
230 }
231 
238  std::ostringstream msg;
239  msg << "<position type=\"camera\" ";
240  msg << "id=\"" << id << "\" ";
241  msg << "x=\"" << pos.x << "\" ";
242  msg << "y=\"" << pos.y << "\" ";
243  msg << "z=\"" << pos.z << "\" ";
244  msg << "rx=\"" << direction.x << "\" ";
245  msg << "ry=\"" << direction.y << "\" ";
246  msg << "rz=\"" << direction.z << "\" ";
247  msg << "/>" << endl;
248  write(msg.str());
249 }
250 
255  std::ostringstream msg;
256  msg << "<position type=\"waypoint\" ";
257  msg << "id=\"" << w.getid() << "\" ";
258  msg << "x=\"" << w.getx() << "\" ";
259  msg << "y=\"" << w.gety() << "\" ";
260  msg << "radius=\"" << w.getr() << "\" ";
261  msg << "/>" << endl;
262  write(msg.str());
263 }
264 
265 
276 
281  std::ostringstream msg;
282  msg << "<scenario ";
283  msg << "name=\"" << name << "\" ";
284  msg << "/>" << endl;
285  write(msg.str());
286 }
287 
288 
307 
318 void Ped::XMLOutputWriter::drawLine(const Tvector &start, const Tvector &end, int duration, double red, double green, double blue) {
319  std::ostringstream msg;
320  msg << "<draw type=\"line\" ";
321  // msg << "name=\"" << name << "\" ";
322  msg << "sx=\"" << start.x << "\" ";
323  msg << "sy=\"" << start.y << "\" ";
324  msg << "ex=\"" << end.x << "\" ";
325  msg << "ey=\"" << end.y << "\" ";
326  msg << "duration=\"" << duration << "\" ";
327  msg << "red=\"" << red << "\" ";
328  msg << "green=\"" << green << "\" ";
329  msg << "blue=\"" << blue << "\" ";
330  msg << "/>" << endl;
331  write(msg.str());
332 }
333 
334 
348 
352 void Ped::XMLOutputWriter::writeMetrics (std::unordered_map<std::string,std::string> hash) {
353  std::ostringstream msg;
354  msg << "<metrics> ";
355  for (auto it = hash.begin(); it != hash.end(); ++it) msg << "<metric key=\"" << it->first << "\" value=\"" << it->second << "\" />";
356  msg << "</metrics>" << endl;
357  write(msg.str());
358 }
virtual void removeAgent(Tagent &a)
virtual void drawAgent(Tagent &a)
virtual void drawWaypoint(Twaypoint &w)
virtual void setScenarioName(string name)
virtual void setCamera(Ped::Tvector pos, Ped::Tvector direction, string id="")
virtual void writeMetrics(std::unordered_map< std::string, std::string > hash)
virtual void drawLine(const Tvector &s, const Tvector &e, int duration=1, double red=1.0, double green=0.0, double blue=0.0)
virtual void drawObstacle(Tobstacle &o)
virtual void writeTimeStep(long int timestep)
O --- O

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