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 /****************************************************************************/ 10 /// @file MSVTKExport.cpp 11 /// @author Mario Krumnow 12 /// @author Daniel Krajzewicz 13 /// @author Jakob Erdmann 14 /// @author Michael Behrisch 15 /// @date 2012-04-26 16 /// @version $Id$ 17 /// 18 // Realises VTK Export 19 /****************************************************************************/ 20 21 22 // =========================================================================== 23 // included modules 24 // =========================================================================== 25 #include <config.h> 26 27 #include <utils/iodevices/OutputDevice.h> 28 #include <microsim/MSEdgeControl.h> 29 #include <microsim/MSJunctionControl.h> 30 #include <microsim/MSVehicle.h> 31 #include <microsim/MSVehicleControl.h> 32 #include <microsim/MSEdge.h> 33 #include <microsim/MSLane.h> 34 #include <microsim/MSGlobals.h> 35 #include <microsim/traffic_lights/MSTLLogicControl.h> 36 #include "MSVTKExport.h" 37 38 39 // =========================================================================== 40 // method definitions 41 // =========================================================================== 42 void write(OutputDevice & of,SUMOTime)43MSVTKExport::write(OutputDevice& of, SUMOTime /* timestep */) { 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> getSpeed()82MSVTKExport::getSpeed() { 83 84 std::vector<double> output; 85 86 MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl(); 87 MSVehicleControl::constVehIt it = vc.loadedVehBegin(); 88 MSVehicleControl::constVehIt end = vc.loadedVehEnd(); 89 90 91 for (; it != end; ++it) { 92 const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second); 93 94 if (veh->isOnRoad()) { 95 96 Position pos = veh->getLane()->getShape().positionAtOffset(veh->getPositionOnLane()); 97 output.push_back(veh->getSpeed()); 98 } 99 100 } 101 102 return output; 103 } 104 105 std::vector<double> getPositions()106MSVTKExport::getPositions() { 107 108 std::vector<double> output; 109 110 MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl(); 111 MSVehicleControl::constVehIt it = vc.loadedVehBegin(); 112 MSVehicleControl::constVehIt end = vc.loadedVehEnd(); 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 List2String(std::vector<double> input)132MSVTKExport::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 getOffset(int nr)152MSVTKExport::getOffset(int nr) { 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 ctype_space(const char c)166MSVTKExport::ctype_space(const char c) { 167 if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11) { 168 return true; 169 } 170 return false; 171 } 172 173 std::string trim(std::string istring)174MSVTKExport::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 /****************************************************************************/ 196