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 /****************************************************************************/
17 // Realises VTK Export
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
27 #include <microsim/MSEdgeControl.h>
29 #include <microsim/MSVehicle.h>
31 #include <microsim/MSEdge.h>
32 #include <microsim/MSLane.h>
33 #include <microsim/MSGlobals.h>
35 #include "MSVTKExport.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
41 void
43 
44  std::vector<double> speed = getSpeed();
45  std::vector<double> points = getPositions();
46 
47  of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
48  of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
49  of << "<PolyData>\n";
50  of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
51  of << "<PointData>\n";
52  of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
53  of << "</PointData>\n";
54  of << "<CellData/>\n";
55  of << "<Points>\n";
56  of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
57  of << "</Points>\n";
58  of << "<Verts>\n";
59  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
60  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
61  of << "</Verts>\n";
62  of << "<Lines>\n";
63  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
64  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
65  of << "</Lines>\n";
66  of << "<Stripes>\n";
67  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
68  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
69  of << "</Stripes>\n";
70  of << "<Polys>\n";
71  of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
72  of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
73  of << "</Polys>\n";
74  of << "</Piece>\n";
75  of << "</PolyData>\n";
76  of << "</VTKFile>";
77 
78 }
79 
80 std::vector<double>
82 
83  std::vector<double> output;
84 
88 
89 
90  for (; it != end; ++it) {
91  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
92 
93  if (veh->isOnRoad()) {
94 
96  output.push_back(veh->getSpeed());
97  }
98 
99  }
100 
101  return output;
102 }
103 
104 std::vector<double>
106 
107  std::vector<double> output;
108 
112 
113 
114  for (; it != end; ++it) {
115  const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
116 
117  if (veh->isOnRoad()) {
118 
119  output.push_back(veh->getPosition().x());
120  output.push_back(veh->getPosition().y());
121  output.push_back(veh->getPosition().z());
122 
123  }
124 
125  }
126 
127  return output;
128 }
129 
130 std::string
131 MSVTKExport::List2String(std::vector<double> input) {
132 
133  std::string output = "";
134  for (int i = 0; i < (int)input.size(); i++) {
135 
136  std::stringstream ss;
137 
138  //for a high precision
139  //ss.precision(::std::numeric_limits<double>::digits10);
140  //ss.unsetf(::std::ios::dec);
141  //ss.setf(::std::ios::scientific);
142 
143  ss << input[i] << " ";
144  output += ss.str();
145  }
146 
147  return trim(output);
148 }
149 
150 std::string
152 
153  std::string output = "";
154  for (int i = 0; i < nr; i++) {
155 
156  std::stringstream ss;
157  ss << i << " ";
158  output += ss.str();
159  }
160 
161  return trim(output);
162 }
163 
164 bool
166  if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
167  return true;
168  }
169  return false;
170 }
171 
172 std::string
173 MSVTKExport::trim(std::string istring) {
174  bool trimmed = false;
175 
176  if (ctype_space(istring[istring.length() - 1])) {
177  istring.erase(istring.length() - 1);
178  trimmed = true;
179  }
180 
181  if (ctype_space(istring[0])) {
182  istring.erase(0, 1);
183  trimmed = true;
184  }
185 
186  if (!trimmed) {
187  return istring;
188  } else {
189  return trim(istring);
190  }
191 
192 }
193 
194 /****************************************************************************/
MSTLLogicControl.h
MSVehicle::isOnRoad
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
Definition: MSVehicle.h:582
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
Position::z
double z() const
Returns the z-position.
Definition: Position.h:66
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSJunctionControl.h
MSEdge.h
MSVehicleControl::constVehIt
std::map< std::string, SUMOVehicle * >::const_iterator constVehIt
Definition of the internal vehicles map iterator.
Definition: MSVehicleControl.h:74
MSVTKExport::getPositions
static std::vector< double > getPositions()
Get a Vector of the Positions (x,y,z) of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:105
MSVehicle.h
MSVehicle::getPosition
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
Definition: MSVehicle.cpp:1269
MSVehicle::getPositionOnLane
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:392
MSVehicleControl::loadedVehEnd
constVehIt loadedVehEnd() const
Returns the end of the internal vehicle map.
Definition: MSVehicleControl.h:185
PositionVector::positionAtOffset
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
Definition: PositionVector.cpp:248
OutputDevice.h
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
Position::x
double x() const
Returns the x-position.
Definition: Position.h:56
MSGlobals.h
MSVTKExport::write
static void write(OutputDevice &of, SUMOTime timestep)
Produce a VTK output to use with Tools like ParaView.
Definition: MSVTKExport.cpp:42
MSVehicle::getLane
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:560
MSVTKExport.h
MSVTKExport::getSpeed
static std::vector< double > getSpeed()
Get a Vector with the speed values of each vehicle in the actual timestep.
Definition: MSVTKExport.cpp:81
MSEdgeControl.h
MSLane::getShape
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:477
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
Position::y
double y() const
Returns the y-position.
Definition: Position.h:61
MSVTKExport::List2String
static std::string List2String(std::vector< double > input)
Get a comma separated String from a Vector.
Definition: MSVTKExport.cpp:131
MSVTKExport::getOffset
static std::string getOffset(int nr)
Get a String with the indexes of all vehicles (needed in the VTk File)
Definition: MSVTKExport.cpp:151
config.h
MSVehicleControl
The class responsible for building and deletion of vehicles.
Definition: MSVehicleControl.h:71
MSVTKExport::trim
static std::string trim(std::string istring)
Deletes the whitespaces at the end of a String.
Definition: MSVTKExport.cpp:173
MSVehicle::getSpeed
double getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:476
MSVTKExport::ctype_space
static bool ctype_space(const char c)
Checks if there is a whitespace.
Definition: MSVTKExport.cpp:165
MSVehicleControl::loadedVehBegin
constVehIt loadedVehBegin() const
Returns the begin of the internal vehicle map.
Definition: MSVehicleControl.h:177
MSLane.h
MSVehicleControl.h
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:336
MSVehicle
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79