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.dev/sumo
3 // Copyright (C) 2012-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // Realises VTK Export
22 /****************************************************************************/
23 #include <config.h>
24 
26 #include <microsim/MSEdgeControl.h>
28 #include <microsim/MSVehicle.h>
30 #include <microsim/MSEdge.h>
31 #include <microsim/MSLane.h>
32 #include <microsim/MSGlobals.h>
34 #include "MSVTKExport.h"
35 
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
40 void
42 
43  std::vector<double> speed = getSpeed();
44  std::vector<double> points = getPositions();
45 
46  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
47  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
48  of << "<PolyData>\n";
49  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
50  of << "<PointData>\n";
51  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
52  of << "</PointData>\n";
53  of << "<CellData/>\n";
54  of << "<Points>\n";
55  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
56  of << "</Points>\n";
57  of << "<Verts>\n";
58  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
59  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
60  of << "</Verts>\n";
61  of << "<Lines>\n";
62  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
63  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
64  of << "</Lines>\n";
65  of << "<Stripes>\n";
66  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
67  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
68  of << "</Stripes>\n";
69  of << "<Polys>\n";
70  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
71  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
72  of << "</Polys>\n";
73  of << "</Piece>\n";
74  of << "</PolyData>\n";
75  of << "</VTKFile>";
76 
77 }
78 
79 std::vector<double>
81 
82  std::vector<double> output;
83 
87 
88 
89  for (; it != end; ++it) {
90  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
91 
92  if (veh->isOnRoad()) {
93 
95  output.push_back(veh->getSpeed());
96  }
97 
98  }
99 
100  return output;
101 }
102 
103 std::vector<double>
105 
106  std::vector<double> output;
107 
111 
112 
113  for (; it != end; ++it) {
114  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
115 
116  if (veh->isOnRoad()) {
117 
118  output.push_back(veh->getPosition().x());
119  output.push_back(veh->getPosition().y());
120  output.push_back(veh->getPosition().z());
121 
122  }
123 
124  }
125 
126  return output;
127 }
128 
129 std::string
130 MSVTKExport::List2String(std::vector<double> input) {
131 
132  std::string output = "";
133  for (int i = 0; i < (int)input.size(); i++) {
134 
135  std::stringstream ss;
136 
137  //for a high precision
138  //ss.precision(::std::numeric_limits<double>::digits10);
139  //ss.unsetf(::std::ios::dec);
140  //ss.setf(::std::ios::scientific);
141 
142  ss << input[i] << " ";
143  output += ss.str();
144  }
145 
146  return trim(output);
147 }
148 
149 std::string
151 
152  std::string output = "";
153  for (int i = 0; i < nr; i++) {
154 
155  std::stringstream ss;
156  ss << i << " ";
157  output += ss.str();
158  }
159 
160  return trim(output);
161 }
162 
163 bool
165  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
166  return true;
167  }
168  return false;
169 }
170 
171 std::string
172 MSVTKExport::trim(std::string istring) {
173  bool trimmed = false;
174 
175  if (ctype_space(istring[istring.length() - 1])) {
176  istring.erase(istring.length() - 1);
177  trimmed = true;
178  }
179 
180  if (ctype_space(istring[0])) {
181  istring.erase(0, 1);
182  trimmed = true;
183  }
184 
185  if (!trimmed) {
186  return istring;
187  } else {
188  return trim(istring);
189  }
190 
191 }
192 
193 
194 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
virtual const PositionVector & getShape(bool) const
Definition: MSLane.h:294
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:184
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:378
static bool ctype_space(const char c)
Checks if there is a whitespace.
static void write(OutputDevice &of, SUMOTime timestep)
Produce a VTK output to use with Tools like ParaView.
Definition: MSVTKExport.cpp:41
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.
static std::vector< double > getPositions()
Get a Vector of the Positions (x,y,z) of each vehicle in the actual timestep.
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:80
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.
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
Definition: MSVehicle.h:608
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:1244
double getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:493
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:377
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:584
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
double x() const
Returns the x-position.
Definition: Position.h:55
double z() const
Returns the z-position.
Definition: Position.h:65
double y() const
Returns the y-position.
Definition: Position.h:60
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.