SUMO - Simulation of Urban MObility
MSVTKExport.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // Realises VTK Export
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
34 #include <microsim/MSEdgeControl.h>
36 #include <microsim/MSVehicle.h>
38 #include <microsim/MSEdge.h>
39 #include <microsim/MSLane.h>
40 #include <microsim/MSGlobals.h>
42 #include "MSVTKExport.h"
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 void
50 
51  std::vector<double> speed = getSpeed();
52  std::vector<double> points = getPositions();
53 
54  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
55  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
56  of << "<PolyData>\n";
57  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
58  of << "<PointData>\n";
59  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
60  of << "</PointData>\n";
61  of << "<CellData/>\n";
62  of << "<Points>\n";
63  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
64  of << "</Points>\n";
65  of << "<Verts>\n";
66  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
67  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
68  of << "</Verts>\n";
69  of << "<Lines>\n";
70  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
71  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
72  of << "</Lines>\n";
73  of << "<Stripes>\n";
74  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
75  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
76  of << "</Stripes>\n";
77  of << "<Polys>\n";
78  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
79  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
80  of << "</Polys>\n";
81  of << "</Piece>\n";
82  of << "</PolyData>\n";
83  of << "</VTKFile>";
84 
85 }
86 
87 std::vector<double>
89 
90  std::vector<double> output;
91 
95 
96 
97  for (; it != end; ++it) {
98  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
99 
100  if (veh->isOnRoad()) {
101 
103  output.push_back(veh->getSpeed());
104  }
105 
106  }
107 
108  return output;
109 }
110 
111 std::vector<double>
113 
114  std::vector<double> output;
115 
119 
120 
121  for (; it != end; ++it) {
122  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
123 
124  if (veh->isOnRoad()) {
125 
126  output.push_back(veh->getPosition().x());
127  output.push_back(veh->getPosition().y());
128  output.push_back(veh->getPosition().z());
129 
130  }
131 
132  }
133 
134  return output;
135 }
136 
137 std::string
138 MSVTKExport::List2String(std::vector<double> input) {
139 
140  std::string output = "";
141  for (int i = 0; i < (int)input.size(); i++) {
142 
143  std::stringstream ss;
144 
145  //for a high precision
146  //ss.precision(::std::numeric_limits<double>::digits10);
147  //ss.unsetf(::std::ios::dec);
148  //ss.setf(::std::ios::scientific);
149 
150  ss << input[i] << " ";
151  output += ss.str();
152  }
153 
154  return trim(output);
155 }
156 
157 std::string
159 
160  std::string output = "";
161  for (int i = 0; i < nr; i++) {
162 
163  std::stringstream ss;
164  ss << i << " ";
165  output += ss.str();
166  }
167 
168  return trim(output);
169 }
170 
171 bool
173  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
174  return true;
175  }
176  return false;
177 }
178 
179 std::string
180 MSVTKExport::trim(std::string istring) {
181  bool trimmed = false;
182 
183  if (ctype_space(istring[istring.length() - 1])) {
184  istring.erase(istring.length() - 1);
185  trimmed = true;
186  }
187 
188  if (ctype_space(istring[0])) {
189  istring.erase(0, 1);
190  trimmed = true;
191  }
192 
193  if (!trimmed) {
194  return istring;
195  } else {
196  return trim(istring);
197  }
198 
199 }
200 
201 /****************************************************************************/
static bool ctype_space(const char c)
Checks if there is a whitespace.
double z() const
Returns the z-position.
Definition: Position.h:72
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:62
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:901
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:402
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:45
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:306
double y() const
Returns the y-position.
Definition: Position.h:67
static void write(OutputDevice &of, SUMOTime timestep)
Produce a VTK output to use with Tools like ParaView.
Definition: MSVTKExport.cpp:49
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:88
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:439
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
long long int SUMOTime
Definition: TraCIDefs.h:51
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:586
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:564
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:482