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-sim.org/
9 // Copyright (C) 2001-2013 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();
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();
101 
102  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
103  of.closeTag();
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();
111 
112  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
113  of.closeTag();
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();
121 
122  of.openTag("DataArray") << " type=\"Int64\" Name=\"offsets\" format=\"ascii\"";
123  of.closeTag();
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 /****************************************************************************/
static bool ctype_space(const char c)
Checks if there is a whitespace.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
Position positionAtOffset(SUMOReal pos) const
Returns the position at the given length.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
Position getPosition(SUMOReal offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:484
SUMOReal getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:284
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
static std::vector< double > getPositions()
Get a Vector of the Positions (x,y,z) of each vehicle in the actual timestep.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:248
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
static void write(OutputDevice &of, SUMOTime timestep)
Produce a VTK output to use with Tools like ParaView.
Definition: MSVTKExport.cpp:56
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
static std::string List2String(std::vector< double > input)
Get a comma separated String from a Vector.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
static std::string trim(std::string istring)
Deletes the whitespaces at the end of a String.
SUMOReal getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:292
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:318
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
static std::string getOffset(int nr)
Get a String with the indexes of all vehicles (needed in the VTk File)
The class responsible for building and deletion of vehicles.
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
Definition: MSVehicle.h:331
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:323
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.