LCOV - code coverage report
Current view: top level - src/router - ROVehicle.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 84.0 % 187 157
Test Date: 2025-11-13 15:38:19 Functions: 100.0 % 9 9

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2002-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    ROVehicle.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Axel Wegener
      17              : /// @author  Michael Behrisch
      18              : /// @author  Jakob Erdmann
      19              : /// @date    Sept 2002
      20              : ///
      21              : // A vehicle as used by router
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <string>
      26              : #include <iostream>
      27              : #include <utils/common/StringUtils.h>
      28              : #include <utils/common/ToString.h>
      29              : #include <utils/common/MsgHandler.h>
      30              : #include <utils/geom/GeoConvHelper.h>
      31              : #include <utils/vehicle/SUMOVTypeParameter.h>
      32              : #include <utils/options/OptionsCont.h>
      33              : #include <utils/iodevices/OutputDevice.h>
      34              : #include "RORouteDef.h"
      35              : #include "RORoute.h"
      36              : #include "ROHelper.h"
      37              : #include "RONet.h"
      38              : #include "ROLane.h"
      39              : #include "ROVehicle.h"
      40              : 
      41              : // ===========================================================================
      42              : // static members
      43              : // ===========================================================================
      44              : std::map<ConstROEdgeVector, std::string> ROVehicle::mySavedRoutes;
      45              : 
      46              : // ===========================================================================
      47              : // method definitions
      48              : // ===========================================================================
      49       297779 : ROVehicle::ROVehicle(const SUMOVehicleParameter& pars,
      50              :                      RORouteDef* route, const SUMOVTypeParameter* type,
      51       297779 :                      const RONet* net, MsgHandler* errorHandler):
      52              :     RORoutable(pars, type),
      53       297779 :     myRoute(route),
      54       297779 :     myJumpTime(-1) {
      55              :     getParameter().stops.clear();
      56       594645 :     if (route != nullptr && route->getFirstRoute() != nullptr) {
      57       594056 :         for (StopParVector::const_iterator s = route->getFirstRoute()->getStops().begin(); s != route->getFirstRoute()->getStops().end(); ++s) {
      58          324 :             addStop(*s, net, errorHandler);
      59              :         }
      60              :     }
      61       298232 :     for (StopParVector::const_iterator s = pars.stops.begin(); s != pars.stops.end(); ++s) {
      62          453 :         addStop(*s, net, errorHandler);
      63              :     }
      64       297779 :     if (pars.via.size() != 0) {
      65              :         // via takes precedence over stop edges
      66              :         // XXX check for inconsistencies #2275
      67              :         myStopEdges.clear();
      68        48261 :         for (std::vector<std::string>::const_iterator it = pars.via.begin(); it != pars.via.end(); ++it) {
      69              :             assert(net->getEdge(*it) != 0);
      70        67200 :             myStopEdges.push_back(net->getEdge(*it));
      71              :         }
      72              :     }
      73       297779 : }
      74              : 
      75              : 
      76              : void
      77          777 : ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
      78         1554 :     const ROEdge* stopEdge = net->getEdge(stopPar.edge);
      79              :     assert(stopEdge != 0); // was checked when parsing the stop
      80          777 :     if (stopEdge->prohibits(this)) {
      81            0 :         if (errorHandler != nullptr) {
      82            0 :             errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
      83              :         }
      84            0 :         return;
      85              :     }
      86              :     // where to insert the stop
      87              :     StopParVector::iterator iter = getParameter().stops.begin();
      88              :     ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
      89          777 :     if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(getParameter().stops.size())) {
      90          777 :         if (getParameter().stops.size() > 0) {
      91              :             iter = getParameter().stops.end();
      92              :             edgeIter = myStopEdges.end();
      93              :         }
      94              :     } else {
      95            0 :         if (stopPar.index == STOP_INDEX_FIT) {
      96            0 :             const ConstROEdgeVector edges = myRoute->getFirstRoute()->getEdgeVector();
      97            0 :             ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
      98            0 :             if (stopEdgeIt == edges.end()) {
      99              :                 iter = getParameter().stops.end();
     100              :                 edgeIter = myStopEdges.end();
     101              :             } else {
     102            0 :                 while (iter != getParameter().stops.end()) {
     103            0 :                     if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
     104              :                         break;
     105              :                     }
     106              :                     ++iter;
     107              :                     ++edgeIter;
     108              :                 }
     109              :             }
     110            0 :         } else {
     111              :             iter += stopPar.index;
     112              :             edgeIter += stopPar.index;
     113              :         }
     114              :     }
     115          777 :     getParameter().stops.insert(iter, stopPar);
     116          777 :     myStopEdges.insert(edgeIter, stopEdge);
     117          777 :     if (stopPar.jump >= 0) {
     118           24 :         if (stopEdge->isInternal()) {
     119            0 :             if (errorHandler != nullptr) {
     120            0 :                 errorHandler->inform("Jumps are not supported from internal stop edge '" + stopEdge->getID() + "'.");
     121              :             }
     122              :         } else {
     123           24 :             if (myJumpTime < 0) {
     124           20 :                 myJumpTime = 0;
     125              :             }
     126           24 :             myJumpTime += stopPar.jump;
     127              :         }
     128              :     }
     129              : }
     130              : 
     131              : 
     132       595471 : ROVehicle::~ROVehicle() {}
     133              : 
     134              : 
     135              : const ROEdge*
     136          511 : ROVehicle:: getDepartEdge() const {
     137          511 :     return myRoute->getFirstRoute()->getFirst();
     138              : }
     139              : 
     140              : 
     141              : void
     142       296642 : ROVehicle::computeRoute(const RORouterProvider& provider,
     143              :                         const bool removeLoops, MsgHandler* errorHandler) {
     144              :     SUMOAbstractRouter<ROEdge, ROVehicle>& router = provider.getVehicleRouter(getVClass());
     145       593284 :     std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
     146              :     RORouteDef* const routeDef = getRouteDefinition();
     147              :     // check if the route definition is valid
     148       296642 :     if (routeDef == nullptr) {
     149            0 :         errorHandler->inform(noRouteMsg);
     150            0 :         myRoutingSuccess = false;
     151            0 :         return;
     152              :     }
     153       296642 :     routeDef->validateAlternatives(this, errorHandler);
     154       296642 :     RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
     155       296642 :     if (current == nullptr || current->size() == 0) {
     156        19113 :         delete current;
     157         1925 :         if (current == nullptr || !routeDef->discardSilent()) {
     158        51564 :             errorHandler->inform(noRouteMsg);
     159              :         }
     160        19113 :         myRoutingSuccess = false;
     161        19113 :         return;
     162              :     }
     163              :     // check whether we have to evaluate the route for not containing loops
     164       277529 :     if (removeLoops) {
     165         5218 :         const ROEdge* requiredStart = (getParameter().departPosProcedure == DepartPosDefinition::GIVEN
     166         5218 :                                        || getParameter().departLaneProcedure == DepartLaneDefinition::GIVEN ? current->getEdgeVector().front() : 0);
     167         5218 :         const ROEdge* requiredEnd = (getParameter().arrivalPosProcedure == ArrivalPosDefinition::GIVEN
     168         5218 :                                      || getParameter().arrivalLaneProcedure == ArrivalLaneDefinition::GIVEN ? current->getEdgeVector().back() : 0);
     169         5218 :         current->recheckForLoops(getMandatoryEdges(requiredStart, requiredEnd));
     170              :         // check whether the route is still valid
     171         5218 :         if (current->size() == 0) {
     172            0 :             delete current;
     173           16 :             errorHandler->inform(noRouteMsg + " (after removing loops)");
     174            0 :             myRoutingSuccess = false;
     175            0 :             return;
     176              :         }
     177              :     }
     178              :     // add built route
     179       277529 :     routeDef->addAlternative(router, this, current, getDepartureTime(), errorHandler);
     180       277513 :     myRoutingSuccess = true;
     181              : }
     182              : 
     183              : 
     184              : ConstROEdgeVector
     185       294441 : ROVehicle::getMandatoryEdges(const ROEdge* requiredStart, const ROEdge* requiredEnd) const {
     186              :     ConstROEdgeVector mandatory;
     187       294441 :     if (requiredStart) {
     188       289231 :         mandatory.push_back(requiredStart);
     189              :     }
     190       328682 :     for (const ROEdge* e : getStopEdges()) {
     191        34241 :         if (e->isInternal()) {
     192              :             // the edges before and after the internal edge are mandatory
     193            8 :             const ROEdge* before = e->getNormalBefore();
     194            8 :             const ROEdge* after = e->getNormalAfter();
     195            8 :             if (mandatory.size() == 0 || after != mandatory.back()) {
     196            8 :                 mandatory.push_back(before);
     197            8 :                 mandatory.push_back(after);
     198              :             }
     199              :         } else {
     200        34233 :             if (mandatory.size() == 0 || e != mandatory.back()) {
     201        28447 :                 mandatory.push_back(e);
     202              :             }
     203              :         }
     204              :     }
     205       294441 :     if (requiredEnd) {
     206       289231 :         if (mandatory.size() < 2 || mandatory.back() != requiredEnd) {
     207       287549 :             mandatory.push_back(requiredEnd);
     208              :         }
     209              :     }
     210       294441 :     return mandatory;
     211            0 : }
     212              : 
     213              : 
     214              : void
     215       289223 : ROVehicle::collectJumps(const ConstROEdgeVector& mandatory, std::set<ConstROEdgeVector::const_iterator>& jumpStarts) const {
     216       289223 :     auto itM = mandatory.begin();
     217              :     auto itS = getParameter().stops.begin();
     218              :     auto itSEnd = getParameter().stops.end();
     219       290106 :     while (itM != mandatory.end() && itS != itSEnd) {
     220              :         bool repeatMandatory = false;
     221              :         // if we stop twice on the same edge, we must treat this as a repeated
     222              :         // mandatory edge (even though the edge appears only once in the mandatory vector)
     223          883 :         if ((*itM)->getID() == itS->edge) {
     224          621 :             if (itS->jump >= 0) {
     225              :                 jumpStarts.insert(itM);
     226              :             }
     227              :             itS++;
     228          621 :             if (itS != itSEnd && itS->edge == (itS - 1)->edge) {
     229              :                 repeatMandatory = true;
     230              :             }
     231              :         }
     232              :         if (!repeatMandatory) {
     233              :             itM++;
     234              :         }
     235              :     }
     236       289223 : }
     237              : 
     238              : 
     239              : void
     240       390534 : ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options, int cloneIndex) const {
     241       390534 :     if (typeos != nullptr && getType() != nullptr && !getType()->saved) {
     242          378 :         getType()->write(*typeos);
     243          378 :         getType()->saved = true;
     244              :     }
     245       390534 :     if (getType() != nullptr && !getType()->saved) {
     246         3996 :         getType()->write(os);
     247         3996 :         getType()->saved = asAlternatives;
     248              :     }
     249              : 
     250       776801 :     const bool writeTrip = options.exists("write-trips") && options.getBool("write-trips");
     251       390534 :     const bool writeGeoTrip = writeTrip && options.getBool("write-trips.geo");
     252       390534 :     const bool writeJunctions = writeTrip && options.getBool("write-trips.junctions");
     253       390534 :     const bool writeNamedRoute = !asAlternatives && options.getBool("named-routes");
     254       776801 :     const bool writeCosts = options.exists("write-costs") && options.getBool("write-costs");
     255       781068 :     const bool writeExit = options.exists("exit-times") && options.getBool("exit-times");
     256       776801 :     const bool writeLength = options.exists("route-length") && options.getBool("route-length");
     257       776801 :     const bool writeFlow = options.exists("keep-flows") && options.getBool("keep-flows") && isPartOfFlow();
     258              : 
     259              :     std::string routeID;
     260       390534 :     if (writeNamedRoute) {
     261           21 :         ConstROEdgeVector edges = myRoute->getUsedRoute()->getNormalEdges();
     262              :         auto it = mySavedRoutes.find(edges);
     263           21 :         if (it == mySavedRoutes.end()) {
     264           14 :             routeID = "r" + toString(mySavedRoutes.size());
     265            7 :             myRoute->getUsedRoute()->writeXMLDefinition(os, this, writeCosts, false, writeExit,
     266              :                     writeLength, routeID);
     267            7 :             mySavedRoutes[edges] = routeID;
     268              :         } else {
     269           14 :             routeID = it->second;
     270              :         }
     271           21 :     }
     272       390534 :     const SumoXMLTag tag = writeFlow ? SUMO_TAG_FLOW : (writeTrip ? SUMO_TAG_TRIP : SUMO_TAG_VEHICLE);
     273              :     // write the vehicle (new style, with included routes)
     274       390534 :     if (cloneIndex == 0) {
     275       780828 :         getParameter().write(os, options, tag);
     276              :     } else {
     277          120 :         SUMOVehicleParameter p = getParameter();
     278              :         // @note id collisions may occur if scale-suffic occurs in other vehicle ids
     279          240 :         p.id += options.getString("scale-suffix") + toString(cloneIndex);
     280          120 :         p.write(os, options, tag);
     281          120 :     }
     282              :     // save the route
     283       390534 :     if (writeTrip) {
     284       158652 :         const ConstROEdgeVector edges = myRoute->getFirstRoute()->getEdgeVector();
     285              :         const ROEdge* from = nullptr;
     286              :         const ROEdge* to = nullptr;
     287        79326 :         if (edges.size() > 0) {
     288        79326 :             if (edges.front()->isTazConnector()) {
     289         7284 :                 if (edges.size() > 1) {
     290         7284 :                     from = edges[1];
     291         7284 :                     if (from->isTazConnector() && writeJunctions && edges.front()->getSuccessors().size() > 0) {
     292              :                         // routing was skipped
     293            8 :                         from = edges.front()->getSuccessors(getVClass()).front();
     294              :                     }
     295              :                 }
     296              :             } else {
     297              :                 from = edges[0];
     298              :             }
     299        79326 :             if (edges.back()->isTazConnector()) {
     300         7284 :                 if (edges.size() > 1) {
     301         7284 :                     to = edges[edges.size() - 2];
     302         7284 :                     if (to->isTazConnector() && writeJunctions && edges.back()->getPredecessors().size() > 0) {
     303              :                         // routing was skipped
     304            4 :                         to = edges.back()->getPredecessors().front();
     305              :                     }
     306              :                 }
     307              :             } else {
     308        72042 :                 to = edges[edges.size() - 1];
     309              :             }
     310              :         }
     311        79326 :         if (from != nullptr) {
     312        79326 :             if (writeGeoTrip) {
     313           16 :                 Position fromPos = from->getLanes()[0]->getShape().positionAtOffset2D(0);
     314           16 :                 if (GeoConvHelper::getFinal().usingGeoProjection()) {
     315            0 :                     os.setPrecision(gPrecisionGeo);
     316            0 :                     GeoConvHelper::getFinal().cartesian2geo(fromPos);
     317            0 :                     os.writeAttr(SUMO_ATTR_FROMLONLAT, fromPos);
     318            0 :                     os.setPrecision(gPrecision);
     319              :                 } else {
     320           16 :                     os.writeAttr(SUMO_ATTR_FROMXY, fromPos);
     321              :                 }
     322        79310 :             } else if (writeJunctions) {
     323         7244 :                 os.writeAttr(SUMO_ATTR_FROM_JUNCTION, from->getFromJunction()->getID());
     324              :             } else {
     325        72066 :                 os.writeAttr(SUMO_ATTR_FROM, from->getID());
     326              :             }
     327              :         }
     328        79326 :         if (to != nullptr) {
     329        79326 :             if (writeGeoTrip) {
     330           16 :                 Position toPos = to->getLanes()[0]->getShape().positionAtOffset2D(to->getLanes()[0]->getShape().length2D());
     331           16 :                 if (GeoConvHelper::getFinal().usingGeoProjection()) {
     332            0 :                     os.setPrecision(gPrecisionGeo);
     333            0 :                     GeoConvHelper::getFinal().cartesian2geo(toPos);
     334            0 :                     os.writeAttr(SUMO_ATTR_TOLONLAT, toPos);
     335            0 :                     os.setPrecision(gPrecision);
     336              :                 } else {
     337           16 :                     os.writeAttr(SUMO_ATTR_TOXY, toPos);
     338              :                 }
     339        79310 :             } else if (writeJunctions) {
     340         7244 :                 os.writeAttr(SUMO_ATTR_TO_JUNCTION, to->getToJunction()->getID());
     341              :             } else {
     342        72066 :                 os.writeAttr(SUMO_ATTR_TO, to->getID());
     343              :             }
     344              :         }
     345        79326 :         if (getParameter().via.size() > 0) {
     346              :             std::vector<std::string> viaOut;
     347              :             SumoXMLAttr viaAttr = (writeGeoTrip
     348         7236 :                                    ? (GeoConvHelper::getFinal().usingGeoProjection() ? SUMO_ATTR_VIALONLAT : SUMO_ATTR_VIAXY)
     349         7228 :                                    : (writeJunctions ? SUMO_ATTR_VIAJUNCTIONS : SUMO_ATTR_VIA));
     350        21672 :             for (const std::string& viaID : getParameter().via) {
     351        14436 :                 const ROEdge* viaEdge = RONet::getInstance()->getEdge(viaID);
     352        14436 :                 if (viaEdge->isTazConnector()) {
     353           20 :                     if (viaEdge->getPredecessors().size() == 0) {
     354            0 :                         continue;
     355              :                     }
     356              :                     // XXX used edge that was used in route
     357           20 :                     viaEdge = viaEdge->getPredecessors().front();
     358              :                 }
     359              :                 assert(viaEdge != nullptr);
     360        14436 :                 if (writeGeoTrip) {
     361            8 :                     Position viaPos = viaEdge->getLanes()[0]->getShape().positionAtOffset2D(viaEdge->getLanes()[0]->getShape().length2D() / 2);
     362            8 :                     if (GeoConvHelper::getFinal().usingGeoProjection()) {
     363            0 :                         GeoConvHelper::getFinal().cartesian2geo(viaPos);
     364            0 :                         viaOut.push_back(toString(viaPos, gPrecisionGeo));
     365              :                     } else {
     366           16 :                         viaOut.push_back(toString(viaPos, gPrecision));
     367              :                     }
     368        14428 :                 } else if (writeJunctions) {
     369            8 :                     viaOut.push_back(viaEdge->getToJunction()->getID());
     370              :                 } else {
     371        14420 :                     viaOut.push_back(viaEdge->getID());
     372              :                 }
     373              :             }
     374         7236 :             os.writeAttr(viaAttr, viaOut);
     375         7236 :         }
     376       390534 :     } else if (writeNamedRoute) {
     377           21 :         os.writeAttr(SUMO_ATTR_ROUTE, routeID);
     378              :     } else {
     379       311187 :         myRoute->writeXMLDefinition(os, this, asAlternatives, writeExit, writeCosts, writeLength);
     380              :     }
     381       391780 :     for (StopParVector::const_iterator stop = getParameter().stops.begin(); stop != getParameter().stops.end(); ++stop) {
     382         1246 :         stop->write(os);
     383              :     }
     384       390534 :     getParameter().writeParams(os);
     385       781068 :     os.closeTag();
     386       390534 : }
     387              : 
     388              : 
     389              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1