SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MSVTKExport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Realises VTK Export
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2012-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <microsim/MSEdgeControl.h>
36 
37 #include <microsim/MSVehicle.h>
39 
40 #include <microsim/MSEdge.h>
41 #include <microsim/MSLane.h>
42 #include <microsim/MSGlobals.h>
44 #include "MSVTKExport.h"
45 
46 #ifdef HAVE_MESOSIM
47 #include <mesosim/MELoop.h>
48 #include <mesosim/MESegment.h>
49 #endif
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 void
61 
62  std::vector<double> speed = getSpeed();
63  std::vector<double> points = getPositions();
64 
65  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
66  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
67  of << "<PolyData>\n";
68  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
69  of << "<PointData>\n";
70  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
71  of << "</PointData>\n";
72  of << "<CellData/>\n";
73  of << "<Points>\n";
74  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
75  of << "</Points>\n";
76  of << "<Verts>\n";
77  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
78  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
79  of << "</Verts>\n";
80  of << "<Lines>\n";
81  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
82  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
83  of << "</Lines>\n";
84  of << "<Stripes>\n";
85  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
86  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
87  of << "</Stripes>\n";
88  of << "<Polys>\n";
89  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
90  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
91  of << "</Polys>\n";
92  of << "</Piece>\n";
93  of << "</PolyData>\n";
94  of << "</VTKFile>";
95 
96 }
97 
98 std::vector<double>
100 
101  std::vector<double> output;
102 
106 
107 
108  for (; it != end; ++it) {
109  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
110 
111  if (veh->isOnRoad()) {
112 
114  output.push_back(veh->getSpeed());
115  }
116 
117  }
118 
119  return output;
120 }
121 
122 std::vector<double>
124 
125  std::vector<double> output;
126 
130 
131 
132  for (; it != end; ++it) {
133  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
134 
135  if (veh->isOnRoad()) {
136 
137  output.push_back(veh->getPosition().x());
138  output.push_back(veh->getPosition().y());
139  output.push_back(veh->getPosition().z());
140 
141  }
142 
143  }
144 
145  return output;
146 }
147 
148 std::string
149 MSVTKExport::List2String(std::vector<double> input) {
150 
151  std::string output = "";
152  for (unsigned i = 0; i < input.size(); i++) {
153 
154  std::stringstream ss;
155 
156  //for a high precision
157  //ss.precision(::std::numeric_limits<double>::digits10);
158  //ss.unsetf(::std::ios::dec);
159  //ss.setf(::std::ios::scientific);
160 
161  ss << input[i] << " ";
162  output += ss.str();
163  }
164 
165  return trim(output);
166 }
167 
168 std::string
170 
171  std::string output = "";
172  for (int i = 0; i < nr; i++) {
173 
174  std::stringstream ss;
175  ss << i << " ";
176  output += ss.str();
177  }
178 
179  return trim(output);
180 }
181 
182 bool
184  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
185  return true;
186  }
187  return false;
188 }
189 
190 std::string
191 MSVTKExport::trim(std::string istring) {
192  bool trimmed = false;
193 
194  if (ctype_space(istring[istring.length() - 1])) {
195  istring.erase(istring.length() - 1);
196  trimmed = true;
197  }
198 
199  if (ctype_space(istring[0])) {
200  istring.erase(0, 1);
201  trimmed = true;
202  }
203 
204  if (!trimmed) {
205  return istring;
206  } else {
207  return trim(istring);
208  }
209 
210 }
211 
212 /****************************************************************************/
static bool ctype_space(const char c)
Checks if there is a whitespace.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
Position getPosition(const SUMOReal offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:603
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
SUMOReal getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:286
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:288
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:60
Position positionAtOffset(SUMOReal pos, SUMOReal lateralOffset=0) const
Returns the position at the given length.
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:99
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's current speed.
Definition: MSVehicle.h:294
int SUMOTime
Definition: SUMOTime.h:43
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:323
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
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:339
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:331