LCOV - code coverage report
Current view: top level - src/microsim/output - MSFCDExport.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 98.1 % 156 153
Test Date: 2025-11-13 15:38:19 Functions: 100.0 % 10 10

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2012-2025 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    MSFCDExport.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Mario Krumnow
      18              : /// @author  Michael Behrisch
      19              : /// @date    2012-04-26
      20              : ///
      21              : // Realises dumping Floating Car Data (FCD) Data
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <utils/iodevices/OutputDevice.h>
      26              : #include <utils/options/OptionsCont.h>
      27              : #include <utils/geom/GeoConvHelper.h>
      28              : #include <utils/geom/GeomHelper.h>
      29              : #include <utils/shapes/SUMOPolygon.h>
      30              : #include <libsumo/Helper.h>
      31              : #include <microsim/devices/MSDevice_FCD.h>
      32              : #include <microsim/devices/MSTransportableDevice_FCD.h>
      33              : #include <microsim/MSEdgeControl.h>
      34              : #include <microsim/MSEdge.h>
      35              : #include <microsim/MSLane.h>
      36              : #include <microsim/MSGlobals.h>
      37              : #include <microsim/MSNet.h>
      38              : #include <microsim/MSVehicle.h>
      39              : #include <microsim/lcmodels/MSAbstractLaneChangeModel.h>
      40              : #include <microsim/transportables/MSPerson.h>
      41              : #include <microsim/transportables/MSTransportableControl.h>
      42              : #include <microsim/MSVehicleControl.h>
      43              : #include <mesosim/MEVehicle.h>
      44              : #include "MSEmissionExport.h"
      45              : #include "MSFCDExport.h"
      46              : 
      47              : 
      48              : // ===========================================================================
      49              : // method definitions
      50              : // ===========================================================================
      51              : void
      52      2106307 : MSFCDExport::write(OutputDevice& of, const SUMOTime timestep, const SumoXMLTag tag) {
      53      2106307 :     MSDevice_FCD::initOnce();
      54              :     const SUMOTime period = MSDevice_FCD::getPeriod();
      55              :     const SUMOTime begin = MSDevice_FCD::getBegin();
      56      2106307 :     if ((period > 0 && (timestep - begin) % period != 0) || timestep < begin) {
      57        57700 :         return;
      58              :     }
      59              :     const SumoXMLAttrMask& mask = MSDevice_FCD::getWrittenAttributes();
      60              :     const bool useGeo = MSDevice_FCD::useGeo();
      61              :     const double maxLeaderDistance = MSDevice_FCD::getMaxLeaderDistance();
      62              :     const std::vector<std::string>& params = MSDevice_FCD::getParamsToWrite();
      63      2048607 :     MSNet* net = MSNet::getInstance();
      64              :     MSVehicleControl& vc = net->getVehicleControl();
      65              :     const double radius = MSDevice_FCD::getRadius();
      66      2048607 :     const bool filter = MSDevice_FCD::getEdgeFilter().size() > 0;
      67              :     const bool shapeFilter = MSDevice_FCD::hasShapeFilter();
      68              :     std::set<const Named*> inRadius;
      69      2048607 :     if (radius > 0) {
      70              :         // collect all vehicles in radius around equipped vehicles
      71         4152 :         for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
      72         2738 :             const SUMOVehicle* veh = it->second;
      73         2738 :             if (isVisible(veh) && hasOwnOutput(veh, filter, shapeFilter)) {
      74         1342 :                 PositionVector shape;
      75         1342 :                 shape.push_back(veh->getPosition());
      76         1342 :                 libsumo::Helper::collectObjectsInRange(libsumo::CMD_GET_VEHICLE_VARIABLE, shape, radius, inRadius);
      77         1342 :                 libsumo::Helper::collectObjectsInRange(libsumo::CMD_GET_PERSON_VARIABLE, shape, radius, inRadius);
      78         1342 :             }
      79              :         }
      80              :     }
      81              : 
      82      4097214 :     of.openTag("timestep").writeTime(SUMO_ATTR_TIME, timestep);
      83      5757042 :     for (MSVehicleControl::constVehIt it = vc.loadedVehBegin(); it != vc.loadedVehEnd(); ++it) {
      84      3708435 :         const SUMOVehicle* const veh = it->second;
      85      3708435 :         if (isVisible(veh)) {
      86      5741318 :             const bool hasOutput = (tag == SUMO_TAG_NOTHING || tag == SUMO_TAG_VEHICLE) && hasOwnOutput(veh, filter, shapeFilter, (radius > 0 && inRadius.count(veh) > 0));
      87              :             if (hasOutput) {
      88      2297172 :                 const MSVehicle* const microVeh = MSGlobals::gUseMesoSim ? nullptr : static_cast<const MSVehicle*>(veh);
      89      2297172 :                 Position pos = veh->getPosition();
      90      2297172 :                 if (useGeo) {
      91         2696 :                     of.setPrecision(gPrecisionGeo);
      92         2696 :                     GeoConvHelper::getFinal().cartesian2geo(pos);
      93              :                 }
      94      2297172 :                 of.openTag(SUMO_TAG_VEHICLE);
      95      2297172 :                 of.writeAttr(SUMO_ATTR_ID, veh->getID());
      96      2297172 :                 of.writeOptionalAttr(SUMO_ATTR_X, pos.x(), mask);
      97      2297172 :                 of.writeOptionalAttr(SUMO_ATTR_Y, pos.y(), mask);
      98      2297172 :                 of.setPrecision(gPrecision);
      99      2297172 :                 of.writeOptionalAttr(SUMO_ATTR_Z, pos.z(), mask);
     100      2297172 :                 of.writeFuncAttr(SUMO_ATTR_ANGLE, [ = ]() {
     101       800085 :                     return GeomHelper::naviDegree(veh->getAngle());
     102              :                 }, mask);
     103      2297172 :                 of.writeFuncAttr(SUMO_ATTR_TYPE, [ = ]() {
     104      2123193 :                     return veh->getVehicleType().getID();
     105              :                 }, mask);
     106      2297172 :                 of.writeFuncAttr(SUMO_ATTR_SPEED, [ = ]() {
     107       864204 :                     return veh->getSpeed();
     108              :                 }, mask);
     109      2297172 :                 of.writeFuncAttr(SUMO_ATTR_POSITION, [ = ]() {
     110       806933 :                     return veh->getPositionOnLane();
     111              :                 }, mask);
     112      2297172 :                 of.writeFuncAttr(SUMO_ATTR_LANE, [ = ]() {
     113       786375 :                     return MSGlobals::gUseMesoSim ? "" : microVeh->getLane()->getID();
     114              :                 }, mask, MSGlobals::gUseMesoSim);
     115      4647994 :                 of.writeFuncAttr(SUMO_ATTR_EDGE, [ = ]() {
     116        53650 :                     return veh->getEdge()->getID();
     117      2297172 :                 }, mask, !MSGlobals::gUseMesoSim);
     118      2297172 :                 of.writeFuncAttr(SUMO_ATTR_SLOPE, [ = ]() {
     119       800085 :                     return veh->getSlope();
     120              :                 }, mask);
     121      2297172 :                 if (!MSGlobals::gUseMesoSim) {
     122      2239454 :                     of.writeFuncAttr(SUMO_ATTR_SIGNALS, [ = ]() {
     123        14817 :                         return microVeh->getSignals();
     124              :                     }, mask);
     125      2239454 :                     of.writeFuncAttr(SUMO_ATTR_ACCELERATION, [ = ]() {
     126       151929 :                         return microVeh->getAcceleration();
     127              :                     }, mask);
     128      2239454 :                     of.writeFuncAttr(SUMO_ATTR_ACCELERATION_LAT, [ = ]() {
     129        55339 :                         return microVeh->getLaneChangeModel().getAccelerationLat();
     130              :                     }, mask);
     131              :                 }
     132      2297172 :                 of.writeFuncAttr(SUMO_ATTR_DISTANCE, [ = ]() {
     133         2134 :                     double lanePos = veh->getPositionOnLane();
     134         2134 :                     if (!MSGlobals::gUseMesoSim && microVeh->getLane()->isInternal()) {
     135            4 :                         lanePos = microVeh->getRoute().getDistanceBetween(0., lanePos, microVeh->getEdge()->getLanes()[0], microVeh->getLane(),
     136            2 :                                   microVeh->getRoutePosition());
     137              :                     }
     138         2134 :                     return veh->getEdge()->getDistanceAt(lanePos);
     139              :                 }, mask);
     140      2297172 :                 of.writeFuncAttr(SUMO_ATTR_ODOMETER, [ = ]() {
     141          806 :                     return veh->getOdometer();
     142              :                 }, mask);
     143      2297172 :                 of.writeFuncAttr(SUMO_ATTR_POSITION_LAT, [ = ]() {
     144        33086 :                     return veh->getLateralPositionOnLane();
     145              :                 }, mask);
     146      2297172 :                 if (!MSGlobals::gUseMesoSim) {
     147      2239454 :                     of.writeFuncAttr(SUMO_ATTR_SPEED_LAT, [ = ]() {
     148          784 :                         return microVeh->getLaneChangeModel().getSpeedLat();
     149              :                     }, mask);
     150              :                 }
     151      2297172 :                 if (maxLeaderDistance >= 0 && !MSGlobals::gUseMesoSim) {
     152        18071 :                     const std::pair<const MSVehicle* const, double> leader = microVeh->getLeader(maxLeaderDistance);
     153        18071 :                     if (leader.first != nullptr) {
     154        13157 :                         of.writeFuncAttr(SUMO_ATTR_LEADER_ID, [ = ]() {
     155        12557 :                             return leader.first->getID();
     156              :                         }, mask);
     157        13157 :                         of.writeFuncAttr(SUMO_ATTR_LEADER_SPEED, [ = ]() {
     158        12557 :                             return leader.first->getSpeed();
     159              :                         }, mask);
     160        13157 :                         of.writeFuncAttr(SUMO_ATTR_LEADER_GAP, [ = ]() {
     161        13157 :                             return leader.second + microVeh->getVehicleType().getMinGap();
     162              :                         }, mask);
     163              :                     } else {
     164         4914 :                         of.writeFuncAttr(SUMO_ATTR_LEADER_ID, [ = ]() {
     165              :                             return "";
     166              :                         }, mask);
     167         4914 :                         of.writeFuncAttr(SUMO_ATTR_LEADER_SPEED, [ = ]() {
     168              :                             return -1;
     169              :                         }, mask);
     170         4914 :                         of.writeFuncAttr(SUMO_ATTR_LEADER_GAP, [ = ]() {
     171              :                             return -1;
     172              :                         }, mask);
     173              :                     }
     174              :                 }
     175      2316311 :                 for (const std::string& key : params) {
     176              :                     std::string error;
     177        19139 :                     const std::string value = static_cast<const MSBaseVehicle*>(veh)->getPrefixedParameter(key, error);
     178        19139 :                     if (value != "") {
     179        28656 :                         of.writeAttr(StringUtils::escapeXML(key), StringUtils::escapeXML(value));
     180              :                     }
     181              :                 }
     182      2297172 :                 of.writeFuncAttr(SUMO_ATTR_ARRIVALDELAY, [ = ]() {
     183         5258 :                     const double arrivalDelay = static_cast<const MSBaseVehicle*>(veh)->getStopArrivalDelay();
     184         5258 :                     if (arrivalDelay == INVALID_DOUBLE) {
     185              :                         // no upcoming stop also means that there is no delay
     186         2378 :                         return 0.;
     187              :                     }
     188              :                     return arrivalDelay;
     189              :                 }, mask);
     190      2297172 :                 if (MSGlobals::gUseMesoSim) {
     191              :                     const MEVehicle* mesoVeh = static_cast<const MEVehicle*>(veh);
     192        57718 :                     of.writeFuncAttr(SUMO_ATTR_SEGMENT, [ = ]() {
     193         5732 :                         return mesoVeh->getSegmentIndex();
     194              :                     }, mask);
     195        57718 :                     of.writeFuncAttr(SUMO_ATTR_QUEUE, [ = ]() {
     196         5732 :                         return mesoVeh->getQueIndex();
     197              :                     }, mask);
     198        57718 :                     of.writeFuncAttr(SUMO_ATTR_ENTRYTIME, [ = ]() {
     199         5732 :                         return mesoVeh->getLastEntryTimeSeconds();
     200              :                     }, mask);
     201        57718 :                     of.writeFuncAttr(SUMO_ATTR_EVENTTIME, [ = ]() {
     202         5732 :                         return mesoVeh->getEventTimeSeconds();
     203              :                     }, mask);
     204        57718 :                     of.writeFuncAttr(SUMO_ATTR_BLOCKTIME, [ = ]() {
     205         5732 :                         return mesoVeh->getBlockTime() == SUMOTime_MAX ? -1.0 : mesoVeh->getBlockTimeSeconds();
     206              :                     }, mask);
     207              :                 }
     208      2297172 :                 of.writeFuncAttr(SUMO_ATTR_TAG, [ = ]() {
     209           30 :                     return toString(SUMO_TAG_VEHICLE);
     210              :                 }, mask);
     211      2297172 :                 MSEmissionExport::writeEmissions(of, static_cast<const MSBaseVehicle*>(veh), false, mask);
     212      4594344 :                 of.closeTag();
     213              :             }
     214              :             // write persons and containers in the vehicle
     215      2870671 :             if (tag == SUMO_TAG_NOTHING || tag == SUMO_TAG_PERSON) {
     216      2870647 :                 const MSEdge* edge = MSGlobals::gUseMesoSim ? veh->getEdge() : &veh->getLane()->getEdge();
     217      2915448 :                 for (const MSTransportable* const person : veh->getPersons()) {
     218        89602 :                     writeTransportable(of, edge, person, veh, filter, shapeFilter, inRadius.count(person) > 0, SUMO_TAG_PERSON, useGeo, mask);
     219              :                 }
     220      2873521 :                 for (const MSTransportable* const container : veh->getContainers()) {
     221         5748 :                     writeTransportable(of, edge, container, veh, filter, shapeFilter, inRadius.count(container) > 0, SUMO_TAG_CONTAINER, useGeo, mask);
     222              :                 }
     223              :             }
     224              :         }
     225              :     }
     226      2048607 :     if (tag == SUMO_TAG_NOTHING || tag == SUMO_TAG_PERSON) {
     227      2048583 :         if (net->hasPersons() && net->getPersonControl().hasTransportables()) {
     228              :             // write persons who are not in a vehicle
     229      2282106 :             for (const MSEdge* const e : net->getEdgeControl().getEdges()) {
     230      2233521 :                 if (filter && MSDevice_FCD::getEdgeFilter().count(e) == 0) {
     231          104 :                     continue;
     232              :                 }
     233      2437659 :                 for (const MSTransportable* const person : e->getSortedPersons(timestep)) {
     234       408484 :                     writeTransportable(of, e, person, nullptr, filter, shapeFilter, inRadius.count(person) > 0, SUMO_TAG_PERSON, useGeo, mask);
     235      2233417 :                 }
     236              :             }
     237              :         }
     238      2048583 :         if (net->hasContainers() && net->getContainerControl().hasTransportables()) {
     239              :             // write containers which are not in a vehicle
     240     19873520 :             for (const MSEdge* const e : net->getEdgeControl().getEdges()) {
     241     18547872 :                 if (filter && MSDevice_FCD::getEdgeFilter().count(e) == 0) {
     242            0 :                     continue;
     243              :                 }
     244     18551392 :                 for (MSTransportable* container : e->getSortedContainers(timestep)) {
     245         7040 :                     writeTransportable(of, e, container, nullptr, filter, shapeFilter, inRadius.count(container) > 0, SUMO_TAG_CONTAINER, useGeo, mask);
     246     18547872 :                 }
     247              :             }
     248              :         }
     249              :     }
     250      4097214 :     of.closeTag();
     251              : }
     252              : 
     253              : 
     254              : bool
     255      3711173 : MSFCDExport::isVisible(const SUMOVehicle* veh) {
     256      3711173 :     return veh->isOnRoad() || veh->isParking() || veh->isRemoteControlled();
     257              : }
     258              : 
     259              : 
     260              : bool
     261      2873355 : MSFCDExport::hasOwnOutput(const SUMOVehicle* veh, bool filter, bool shapeFilter, bool isInRadius) {
     262           52 :     return ((!filter || MSDevice_FCD::getEdgeFilter().count(veh->getEdge()) > 0)
     263      2873331 :             && (!shapeFilter || MSDevice_FCD::shapeFilter(veh))
     264      5744500 :             && ((veh->getDevice(typeid(MSDevice_FCD)) != nullptr) || isInRadius));
     265              : }
     266              : 
     267              : 
     268              : bool
     269       255437 : MSFCDExport::hasOwnOutput(const MSTransportable* p, bool filter, bool shapeFilter, bool isInRadius) {
     270           52 :     return ((!filter || MSDevice_FCD::getEdgeFilter().count(p->getEdge()) > 0)
     271       255409 :             && (!shapeFilter || MSDevice_FCD::shapeFilter(p))
     272       510846 :             && ((p->getDevice(typeid(MSTransportableDevice_FCD)) != nullptr) || isInRadius));
     273              : }
     274              : 
     275              : 
     276              : void
     277       255437 : MSFCDExport::writeTransportable(OutputDevice& of, const MSEdge* const e, const MSTransportable* const p, const SUMOVehicle* const v,
     278              :                                 const bool filter, const bool shapeFilter, const bool inRadius,
     279              :                                 const SumoXMLTag tag, const bool useGeo, const SumoXMLAttrMask mask) {
     280       255437 :     if (!hasOwnOutput(p, filter, shapeFilter, inRadius)) {
     281         1688 :         return;
     282              :     }
     283       253749 :     Position pos = p->getPosition();
     284       253749 :     if (useGeo) {
     285            0 :         of.setPrecision(gPrecisionGeo);
     286            0 :         GeoConvHelper::getFinal().cartesian2geo(pos);
     287              :     }
     288       253749 :     of.openTag(tag);
     289       253749 :     of.writeAttr(SUMO_ATTR_ID, p->getID());
     290       253749 :     of.writeOptionalAttr(SUMO_ATTR_X, pos.x(), mask);
     291       253749 :     of.writeOptionalAttr(SUMO_ATTR_Y, pos.y(), mask);
     292       253749 :     of.setPrecision(gPrecision);
     293       253749 :     of.writeOptionalAttr(SUMO_ATTR_Z, pos.z(), mask);
     294       253749 :     of.writeOptionalAttr(SUMO_ATTR_ANGLE, GeomHelper::naviDegree(p->getAngle()), mask);
     295       253749 :     of.writeOptionalAttr(SUMO_ATTR_TYPE, p->getVehicleType().getID(), mask);
     296       253749 :     of.writeOptionalAttr(SUMO_ATTR_SPEED, p->getSpeed(), mask);
     297       253749 :     of.writeOptionalAttr(SUMO_ATTR_POSITION, p->getEdgePos(), mask);
     298       253749 :     of.writeOptionalAttr(SUMO_ATTR_LANE, "", mask, true);
     299       253749 :     of.writeOptionalAttr(SUMO_ATTR_EDGE, e->getID(), mask);
     300       253749 :     of.writeOptionalAttr(SUMO_ATTR_SLOPE, e->getLanes()[0]->getShape().slopeDegreeAtOffset(p->getEdgePos()), mask);
     301       300558 :     of.writeOptionalAttr(SUMO_ATTR_VEHICLE, v == nullptr ? "" : v->getID(), mask);
     302       253749 :     of.writeOptionalAttr(SUMO_ATTR_TAG, toString(tag), mask);
     303       507498 :     of.closeTag();
     304              : }
     305              : 
     306              : 
     307              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1