SUMO - Simulation of Urban MObility
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-2017 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 
35 #include <microsim/MSEdgeControl.h>
37 #include <microsim/MSVehicle.h>
39 #include <microsim/MSEdge.h>
40 #include <microsim/MSLane.h>
41 #include <microsim/MSGlobals.h>
43 #include "MSVTKExport.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 void
51 
52  std::vector<double> speed = getSpeed();
53  std::vector<double> points = getPositions();
54 
55  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
56  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
57  of << "<PolyData>\n";
58  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
59  of << "<PointData>\n";
60  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
61  of << "</PointData>\n";
62  of << "<CellData/>\n";
63  of << "<Points>\n";
64  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
65  of << "</Points>\n";
66  of << "<Verts>\n";
67  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
68  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
69  of << "</Verts>\n";
70  of << "<Lines>\n";
71  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
72  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
73  of << "</Lines>\n";
74  of << "<Stripes>\n";
75  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
76  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
77  of << "</Stripes>\n";
78  of << "<Polys>\n";
79  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
80  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
81  of << "</Polys>\n";
82  of << "</Piece>\n";
83  of << "</PolyData>\n";
84  of << "</VTKFile>";
85 
86 }
87 
88 std::vector<double>
90 
91  std::vector<double> output;
92 
96 
97 
98  for (; it != end; ++it) {
99  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
100 
101  if (veh->isOnRoad()) {
102 
104  output.push_back(veh->getSpeed());
105  }
106 
107  }
108 
109  return output;
110 }
111 
112 std::vector<double>
114 
115  std::vector<double> output;
116 
120 
121 
122  for (; it != end; ++it) {
123  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
124 
125  if (veh->isOnRoad()) {
126 
127  output.push_back(veh->getPosition().x());
128  output.push_back(veh->getPosition().y());
129  output.push_back(veh->getPosition().z());
130 
131  }
132 
133  }
134 
135  return output;
136 }
137 
138 std::string
139 MSVTKExport::List2String(std::vector<double> input) {
140 
141  std::string output = "";
142  for (int i = 0; i < (int)input.size(); i++) {
143 
144  std::stringstream ss;
145 
146  //for a high precision
147  //ss.precision(::std::numeric_limits<double>::digits10);
148  //ss.unsetf(::std::ios::dec);
149  //ss.setf(::std::ios::scientific);
150 
151  ss << input[i] << " ";
152  output += ss.str();
153  }
154 
155  return trim(output);
156 }
157 
158 std::string
160 
161  std::string output = "";
162  for (int i = 0; i < nr; i++) {
163 
164  std::stringstream ss;
165  ss << i << " ";
166  output += ss.str();
167  }
168 
169  return trim(output);
170 }
171 
172 bool
174  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
175  return true;
176  }
177  return false;
178 }
179 
180 std::string
181 MSVTKExport::trim(std::string istring) {
182  bool trimmed = false;
183 
184  if (ctype_space(istring[istring.length() - 1])) {
185  istring.erase(istring.length() - 1);
186  trimmed = true;
187  }
188 
189  if (ctype_space(istring[0])) {
190  istring.erase(0, 1);
191  trimmed = true;
192  }
193 
194  if (!trimmed) {
195  return istring;
196  } else {
197  return trim(istring);
198  }
199 
200 }
201 
202 /****************************************************************************/
static bool ctype_space(const char c)
Checks if there is a whitespace.
double z() const
Returns the z-position.
Definition: Position.h:73
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
double x() const
Returns the x-position.
Definition: Position.h:63
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:806
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:375
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:305
double y() const
Returns the y-position.
Definition: Position.h:68
static void write(OutputDevice &of, SUMOTime timestep)
Produce a VTK output to use with Tools like ParaView.
Definition: MSVTKExport.cpp:50
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:89
static std::string List2String(std::vector< double > input)
Get a comma separated String from a Vector.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
static std::string trim(std::string istring)
Deletes the whitespaces at the end of a String.
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:426
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
long long int SUMOTime
Definition: TraCIDefs.h:52
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:521
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:499
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:443