Line data Source code
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 : /****************************************************************************/ 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 0 : MSVTKExport::write(OutputDevice& of, SUMOTime /* timestep */) { 42 : 43 0 : std::vector<double> speed = getSpeed(); 44 0 : std::vector<double> points = getPositions(); 45 : 46 0 : of << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; 47 0 : of << "<VTKFile type=\"PolyData\" version=\"0.1\" order=\"LittleEndian\">\n"; 48 0 : of << "<PolyData>\n"; 49 0 : of << " <Piece NumberOfPoints=\"" << speed.size() << "\" NumberOfVerts=\"1\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n"; 50 0 : of << "<PointData>\n"; 51 0 : of << " <DataArray type=\"Float64\" Name=\"speed\" format=\"ascii\">" << List2String(getSpeed()) << "</DataArray>\n"; 52 0 : of << "</PointData>\n"; 53 0 : of << "<CellData/>\n"; 54 0 : of << "<Points>\n"; 55 0 : of << " <DataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">" << List2String(getPositions()) << "</DataArray>\n"; 56 0 : of << "</Points>\n"; 57 0 : of << "<Verts>\n"; 58 0 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">" << getOffset((int) speed.size()) << "</DataArray>\n"; 59 0 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">" << speed.size() << "</DataArray>\n"; 60 0 : of << "</Verts>\n"; 61 0 : of << "<Lines>\n"; 62 0 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n"; 63 0 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n"; 64 0 : of << "</Lines>\n"; 65 0 : of << "<Stripes>\n"; 66 0 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n"; 67 0 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n"; 68 0 : of << "</Stripes>\n"; 69 0 : of << "<Polys>\n"; 70 0 : of << " <DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\"/>\n"; 71 0 : of << " <DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\"/>\n"; 72 0 : of << "</Polys>\n"; 73 0 : of << "</Piece>\n"; 74 0 : of << "</PolyData>\n"; 75 0 : of << "</VTKFile>"; 76 : 77 0 : } 78 : 79 : std::vector<double> 80 0 : MSVTKExport::getSpeed() { 81 : 82 : std::vector<double> output; 83 : 84 0 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl(); 85 : MSVehicleControl::constVehIt it = vc.loadedVehBegin(); 86 : MSVehicleControl::constVehIt end = vc.loadedVehEnd(); 87 : 88 : 89 0 : for (; it != end; ++it) { 90 0 : const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second); 91 : 92 0 : if (veh->isOnRoad()) { 93 : 94 0 : Position pos = veh->getLane()->getShape().positionAtOffset(veh->getPositionOnLane()); 95 0 : output.push_back(veh->getSpeed()); 96 : } 97 : 98 : } 99 : 100 0 : return output; 101 : } 102 : 103 : std::vector<double> 104 0 : MSVTKExport::getPositions() { 105 : 106 : std::vector<double> output; 107 : 108 0 : MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl(); 109 : MSVehicleControl::constVehIt it = vc.loadedVehBegin(); 110 : MSVehicleControl::constVehIt end = vc.loadedVehEnd(); 111 : 112 : 113 0 : for (; it != end; ++it) { 114 0 : const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second); 115 : 116 0 : if (veh->isOnRoad()) { 117 : 118 0 : output.push_back(veh->getPosition().x()); 119 0 : output.push_back(veh->getPosition().y()); 120 0 : output.push_back(veh->getPosition().z()); 121 : 122 : } 123 : 124 : } 125 : 126 0 : return output; 127 : } 128 : 129 : std::string 130 0 : MSVTKExport::List2String(std::vector<double> input) { 131 : 132 0 : std::string output = ""; 133 0 : for (int i = 0; i < (int)input.size(); i++) { 134 : 135 0 : 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 0 : ss << input[i] << " "; 143 0 : output += ss.str(); 144 0 : } 145 : 146 0 : return trim(output); 147 : } 148 : 149 : std::string 150 0 : MSVTKExport::getOffset(int nr) { 151 : 152 0 : std::string output = ""; 153 0 : for (int i = 0; i < nr; i++) { 154 : 155 0 : std::stringstream ss; 156 0 : ss << i << " "; 157 0 : output += ss.str(); 158 0 : } 159 : 160 0 : return trim(output); 161 : } 162 : 163 : bool 164 0 : MSVTKExport::ctype_space(const char c) { 165 0 : if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) { 166 0 : return true; 167 : } 168 : return false; 169 : } 170 : 171 : std::string 172 0 : MSVTKExport::trim(std::string istring) { 173 : bool trimmed = false; 174 : 175 0 : if (ctype_space(istring[istring.length() - 1])) { 176 0 : istring.erase(istring.length() - 1); 177 : trimmed = true; 178 : } 179 : 180 0 : if (ctype_space(istring[0])) { 181 0 : istring.erase(0, 1); 182 : trimmed = true; 183 : } 184 : 185 0 : if (!trimmed) { 186 : return istring; 187 : } else { 188 0 : return trim(istring); 189 : } 190 : 191 : } 192 : 193 : 194 : /****************************************************************************/