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