Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2026 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 : /****************************************************************************/
14 : /// @file MSVTKExport.cpp
15 : /// @author Mario Krumnow
16 : /// @author Daniel Krajzewicz
17 : /// @author Jakob Erdmann
18 : /// @author Michael Behrisch
19 : /// @date 2012-04-26
20 : ///
21 : // Realises VTK Export
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <utils/iodevices/OutputDevice.h>
26 : #include <microsim/MSEdgeControl.h>
27 : #include <microsim/MSJunctionControl.h>
28 : #include <microsim/MSVehicle.h>
29 : #include <microsim/MSVehicleControl.h>
30 : #include <microsim/MSEdge.h>
31 : #include <microsim/MSLane.h>
32 : #include <microsim/MSGlobals.h>
33 : #include <microsim/traffic_lights/MSTLLogicControl.h>
34 : #include "MSVTKExport.h"
35 :
36 :
37 : // ===========================================================================
38 : // method definitions
39 : // ===========================================================================
40 : void
41 54 : MSVTKExport::write(OutputDevice& of, SUMOTime /* timestep */) {
42 :
43 54 : std::vector<double> speed = getSpeed();
44 54 : std::vector<double> points = getPositions();
45 :
46 54 : of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
47 54 : of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n";
48 54 : of << "<PolyData>\n";
49 54 : of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
50 54 : of << "<PointData>\n";
51 108 : of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n";
52 54 : of << "</PointData>\n";
53 54 : of << "<CellData/>\n";
54 54 : of << "<Points>\n";
55 108 : of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n";
56 54 : of << "</Points>\n";
57 54 : of << "<Verts>\n";
58 54 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n";
59 54 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n";
60 54 : of << "</Verts>\n";
61 54 : of << "<Lines>\n";
62 54 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
63 54 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
64 54 : of << "</Lines>\n";
65 54 : of << "<Stripes>\n";
66 54 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
67 54 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
68 54 : of << "</Stripes>\n";
69 54 : of << "<Polys>\n";
70 54 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n";
71 54 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n";
72 54 : of << "</Polys>\n";
73 54 : of << "</Piece>\n";
74 54 : of << "</PolyData>\n";
75 54 : of << "</VTKFile>";
76 :
77 54 : }
78 :
79 : std::vector<double>
80 108 : MSVTKExport::getSpeed() {
81 :
82 : std::vector<double> output;
83 :
84 108 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
85 : MSVehicleControl::constVehIt it = vc.loadedVehBegin();
86 : MSVehicleControl::constVehIt end = vc.loadedVehEnd();
87 :
88 :
89 324 : for (; it != end; ++it) {
90 216 : const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
91 :
92 216 : if (veh->isOnRoad()) {
93 :
94 : //Position pos = veh->getLane()->getShape().positionAtOffset(veh->getPositionOnLane());
95 216 : output.push_back(veh->getSpeed());
96 : }
97 :
98 : }
99 :
100 108 : return output;
101 0 : }
102 :
103 : std::vector<double>
104 108 : MSVTKExport::getPositions() {
105 :
106 : std::vector<double> output;
107 :
108 108 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
109 : MSVehicleControl::constVehIt it = vc.loadedVehBegin();
110 : MSVehicleControl::constVehIt end = vc.loadedVehEnd();
111 :
112 :
113 324 : for (; it != end; ++it) {
114 216 : const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
115 :
116 216 : if (veh->isOnRoad()) {
117 :
118 216 : output.push_back(veh->getPosition().x());
119 216 : output.push_back(veh->getPosition().y());
120 216 : output.push_back(veh->getPosition().z());
121 :
122 : }
123 :
124 : }
125 :
126 108 : return output;
127 0 : }
128 :
129 : std::string
130 108 : MSVTKExport::List2String(std::vector<double> input) {
131 :
132 108 : std::string output = "";
133 540 : for (int i = 0; i < (int)input.size(); i++) {
134 :
135 432 : 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 432 : ss << input[i] << " ";
143 432 : output += ss.str();
144 432 : }
145 :
146 216 : return trim(output);
147 : }
148 :
149 : std::string
150 54 : MSVTKExport::getOffset(int nr) {
151 :
152 54 : std::string output = "";
153 162 : for (int i = 0; i < nr; i++) {
154 :
155 108 : std::stringstream ss;
156 108 : ss << i << " ";
157 108 : output += ss.str();
158 108 : }
159 :
160 108 : return trim(output);
161 : }
162 :
163 : bool
164 648 : MSVTKExport::ctype_space(const char c) {
165 648 : if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) {
166 162 : return true;
167 : }
168 : return false;
169 : }
170 :
171 : std::string
172 324 : MSVTKExport::trim(std::string istring) {
173 : bool trimmed = false;
174 :
175 324 : if (ctype_space(istring[istring.length() - 1])) {
176 162 : istring.erase(istring.length() - 1);
177 : trimmed = true;
178 : }
179 :
180 324 : if (ctype_space(istring[0])) {
181 0 : istring.erase(0, 1);
182 : trimmed = true;
183 : }
184 :
185 324 : if (!trimmed) {
186 162 : return istring;
187 : } else {
188 324 : return trim(istring);
189 : }
190 :
191 : }
192 :
193 :
194 : /****************************************************************************/
|