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 
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 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 void
57 
58  std::vector<double> speed = getSpeed();
59  std::vector<double> points = getPositions();
60 
61  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
62  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
63  of << "<PolyData>\n";
64  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
65  of << "<PointData>\n";
66  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
67  of << "</PointData>\n";
68  of << "<CellData/>\n";
69  of << "<Points>\n";
70  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
71  of << "</Points>\n";
72  of << "<Verts>\n";
73  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
74  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
75  of << "</Verts>\n";
76  of << "<Lines>\n";
77  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
78  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
79  of << "</Lines>\n";
80  of << "<Stripes>\n";
81  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
82  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
83  of << "</Stripes>\n";
84  of << "<Polys>\n";
85  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
86  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
87  of << "</Polys>\n";
88  of << "</Piece>\n";
89  of << "</PolyData>\n";
90  of << "</VTKFile>";
91 
92 }
93 
94 std::vector<double>
96 
97  std::vector<double> output;
98 
102 
103 
104  for (; it != end; ++it) {
105  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
106 
107  if (veh->isOnRoad()) {
108 
110  output.push_back(veh->getSpeed());
111  }
112 
113  }
114 
115  return output;
116 }
117 
118 std::vector<double>
120 
121  std::vector<double> output;
122 
126 
127 
128  for (; it != end; ++it) {
129  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
130 
131  if (veh->isOnRoad()) {
132 
133  output.push_back(veh->getPosition().x());
134  output.push_back(veh->getPosition().y());
135  output.push_back(veh->getPosition().z());
136 
137  }
138 
139  }
140 
141  return output;
142 }
143 
144 std::string
145 MSVTKExport::List2String(std::vector<double> input) {
146 
147  std::string output = "";
148  for (int i = 0; i < (int)input.size(); i++) {
149 
150  std::stringstream ss;
151 
152  //for a high precision
153  //ss.precision(::std::numeric_limits<double>::digits10);
154  //ss.unsetf(::std::ios::dec);
155  //ss.setf(::std::ios::scientific);
156 
157  ss << input[i] << " ";
158  output += ss.str();
159  }
160 
161  return trim(output);
162 }
163 
164 std::string
166 
167  std::string output = "";
168  for (int i = 0; i < nr; i++) {
169 
170  std::stringstream ss;
171  ss << i << " ";
172  output += ss.str();
173  }
174 
175  return trim(output);
176 }
177 
178 bool
180  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
181  return true;
182  }
183  return false;
184 }
185 
186 std::string
187 MSVTKExport::trim(std::string istring) {
188  bool trimmed = false;
189 
190  if (ctype_space(istring[istring.length() - 1])) {
191  istring.erase(istring.length() - 1);
192  trimmed = true;
193  }
194 
195  if (ctype_space(istring[0])) {
196  istring.erase(0, 1);
197  trimmed = true;
198  }
199 
200  if (!trimmed) {
201  return istring;
202  } else {
203  return trim(istring);
204  }
205 
206 }
207 
208 /****************************************************************************/
static bool ctype_space(const char c)
Checks if there is a whitespace.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
double z() const
Returns the z-position.
Definition: Position.h:73
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:488
double y() const
Returns the y-position.
Definition: Position.h:68
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:375
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:158
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:426
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:777
static std::vector< double > getPositions()
Get a Vector of the Positions (x,y,z) of each vehicle in the actual timestep.
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
Definition: MSVehicle.h:510
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:310
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.
Definition: MSVTKExport.cpp:95
static std::string List2String(std::vector< double > input)
Get a comma separated String from a Vector.
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.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
long long int SUMOTime
Definition: TraCIDefs.h:52
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.
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:442
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.