SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSVTKExport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
6 // Realises VTK Export
7 /****************************************************************************/
8 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
9 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
10 /****************************************************************************/
11 //
12 // This file is part of SUMO.
13 // SUMO is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <microsim/MSEdgeControl.h>
32 
33 #include <microsim/MSVehicle.h>
35 
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSGlobals.h>
40 #include "MSVTKExport.h"
41 
42 #ifdef HAVE_MESOSIM
43 #include <mesosim/MELoop.h>
44 #include <mesosim/MESegment.h>
45 #endif
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 void
57 
58  of.openTag("?xml") << " version=\"1.0\" encoding=\"UTF-8\" ?>\n";
59  of.openTag("VTKFile") << " type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\" >\n";
60  of.openTag("PolyData") << ">\n";
61 
62  std::vector<double> speed = getSpeed();
63  std::vector<double> points = getPositions();
64 
65 
66  of.openTag("Piece") << " NumberOfPoints=\"" << speed.size() <<
67  "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
68 
69  of.openTag("PointData") << ">\n";
70  of.openTag("DataArray") << " type=\"Float64\" Name=\"speed\" format=\"ascii\" >" << List2String(getSpeed());
71  of.closeTag();
72 
73  //close PointData
74  of.closeTag();
75 
76  of.openTag("CellData");
77 
78  //close CellData
79  of.closeTag(true);
80 
81  of.openTag("Points") << ">\n";
82  of.openTag("DataArray") << " type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\" >" << List2String(getPositions());
83  of.closeTag();
84 
85  //close Points
86  of.closeTag();
87 
88  of.openTag("Verts") << ">\n";
89  of.openTag("DataArray") << " type=\"Int64\" Name=\"connectivity\" format=\"ascii\" >" << getOffset((int) speed.size());
90  of.closeTag();
91 
92  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\" >" << speed.size();
93  of.closeTag();
94 
95  //Close Verts
96  of.closeTag();
97 
98  of.openTag("Lines") << ">\n";
99  of.openTag("DataArray") << " type=\"Int64\" Name=\"connectivity\" format=\"ascii\"";
100  of.closeTag(true);
101 
102  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
103  of.closeTag(true);
104 
105  //Close Lines
106  of.closeTag();
107 
108  of.openTag("Strips") << ">\n";
109  of.openTag("DataArray") << " type=\"Int64\" Name=\"connectivity\" format=\"ascii\"";
110  of.closeTag(true);
111 
112  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
113  of.closeTag(true);
114 
115  //Close Strips
116  of.closeTag();
117 
118  of.openTag("Polys") << ">\n";
119  of.openTag("DataArray") << " type=\"Int64\" Name=\"connectivity\" format=\"ascii\"";
120  of.closeTag(true);
121 
122  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
123  of.closeTag(true);
124 
125  //close Polys
126  of.closeTag();
127 
128  //close Piece
129  of.closeTag();
130 
131  //close PolyData
132  of.closeTag();
133 
134  //close VTKFile
135  of.closeTag();
136 
137 }
138 
139 std::vector<double>
141 
142  std::vector<double> output;
143 
147 
148 
149  for (; it != end; ++it) {
150  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
151 
152  if (veh->isOnRoad()) {
153 
155  output.push_back(veh->getSpeed() * 3.6);
156  }
157 
158  }
159 
160  return output;
161 }
162 
163 std::vector<double>
165 
166  std::vector<double> output;
167 
171 
172 
173  for (; it != end; ++it) {
174  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
175 
176  if (veh->isOnRoad()) {
177 
178  output.push_back(veh->getPosition().x());
179  output.push_back(veh->getPosition().y());
180  output.push_back(veh->getPosition().z());
181 
182  }
183 
184  }
185 
186  return output;
187 }
188 
189 std::string
190 MSVTKExport::List2String(std::vector<double> input) {
191 
192  std::string output = "";
193  for (unsigned i = 0; i < input.size(); i++) {
194 
195  std::stringstream ss;
196 
197  //for a high precision
198  ss.precision(::std::numeric_limits<double>::digits10);
199  ss.unsetf(::std::ios::dec);
200  ss.setf(::std::ios::scientific);
201 
202  ss << input[i] << " ";
203  output += ss.str();
204  }
205 
206  return trim(output);
207 }
208 
209 std::string
211 
212  std::string output = "";
213  for (int i = 0; i < nr; i++) {
214 
215  std::stringstream ss;
216  ss << i << " ";
217  output += ss.str();
218  }
219 
220  return trim(output);
221 }
222 
223 bool
225  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
226  return true;
227  }
228  return false;
229 }
230 
231 std::string
232 MSVTKExport::trim(std::string istring) {
233  bool trimmed = false;
234 
235  if (ctype_space(istring[istring.length() - 1])) {
236  istring.erase(istring.length() - 1);
237  trimmed = true;
238  }
239 
240  if (ctype_space(istring[0])) {
241  istring.erase(0, 1);
242  trimmed = true;
243  }
244 
245  if (!trimmed) {
246  return istring;
247  } else {
248  return trim(istring);
249  }
250 
251 }
252 
253 /****************************************************************************/