Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Realises VTK Export
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
28 #include <microsim/MSEdgeControl.h>
30 #include <microsim/MSVehicle.h>
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSLane.h>
34 #include <microsim/MSGlobals.h>
36 #include "MSVTKExport.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 void
44 
45  std::vector<double> speed = getSpeed();
46  std::vector<double> points = getPositions();
47 
48  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
49  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
50  of << "<PolyData>\n";
51  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
52  of << "<PointData>\n";
53  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
54  of << "</PointData>\n";
55  of << "<CellData/>\n";
56  of << "<Points>\n";
57  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
58  of << "</Points>\n";
59  of << "<Verts>\n";
60  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
61  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
62  of << "</Verts>\n";
63  of << "<Lines>\n";
64  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
65  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
66  of << "</Lines>\n";
67  of << "<Stripes>\n";
68  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
69  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
70  of << "</Stripes>\n";
71  of << "<Polys>\n";
72  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
73  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
74  of << "</Polys>\n";
75  of << "</Piece>\n";
76  of << "</PolyData>\n";
77  of << "</VTKFile>";
78 
79 }
80 
81 std::vector<double>
83 
84  std::vector<double> output;
85 
89 
90 
91  for (; it != end; ++it) {
92  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
93 
94  if (veh->isOnRoad()) {
95 
97  output.push_back(veh->getSpeed());
98  }
99 
100  }
101 
102  return output;
103 }
104 
105 std::vector<double>
107 
108  std::vector<double> output;
109 
113 
114 
115  for (; it != end; ++it) {
116  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
117 
118  if (veh->isOnRoad()) {
119 
120  output.push_back(veh->getPosition().x());
121  output.push_back(veh->getPosition().y());
122  output.push_back(veh->getPosition().z());
123 
124  }
125 
126  }
127 
128  return output;
129 }
130 
131 std::string
132 MSVTKExport::List2String(std::vector<double> input) {
133 
134  std::string output = "";
135  for (int i = 0; i < (int)input.size(); i++) {
136 
137  std::stringstream ss;
138 
139  //for a high precision
140  //ss.precision(::std::numeric_limits<double>::digits10);
141  //ss.unsetf(::std::ios::dec);
142  //ss.setf(::std::ios::scientific);
143 
144  ss << input[i] << " ";
145  output += ss.str();
146  }
147 
148  return trim(output);
149 }
150 
151 std::string
153 
154  std::string output = "";
155  for (int i = 0; i < nr; i++) {
156 
157  std::stringstream ss;
158  ss << i << " ";
159  output += ss.str();
160  }
161 
162  return trim(output);
163 }
164 
165 bool
167  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
168  return true;
169  }
170  return false;
171 }
172 
173 std::string
174 MSVTKExport::trim(std::string istring) {
175  bool trimmed = false;
176 
177  if (ctype_space(istring[istring.length() - 1])) {
178  istring.erase(istring.length() - 1);
179  trimmed = true;
180  }
181 
182  if (ctype_space(istring[0])) {
183  istring.erase(0, 1);
184  trimmed = true;
185  }
186 
187  if (!trimmed) {
188  return istring;
189  } else {
190  return trim(istring);
191  }
192 
193 }
194 
195 /****************************************************************************/
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
long long int SUMOTime
Definition: SUMOTime.h:35
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
double z() const
Returns the z-position.
Definition: Position.h:67
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:561
double y() const
Returns the y-position.
Definition: Position.h:62
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:397
double x() const
Returns the x-position.
Definition: Position.h:57
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:478
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:1280
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:583
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
static void write(OutputDevice &of, SUMOTime timestep)
Produce a VTK output to use with Tools like ParaView.
Definition: MSVTKExport.cpp:43
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:82
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:64
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:477
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.