LCOV - code coverage report
Current view: top level - src/libsumo - LaneArea.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 138 138
Test Date: 2025-11-13 15:38:19 Functions: 92.7 % 41 38

            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    LaneArea.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Mario Krumnow
      17              : /// @author  Jakob Erdmann
      18              : /// @author  Michael Behrisch
      19              : /// @date    30.05.2012
      20              : ///
      21              : // C++ TraCI client API implementation
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <microsim/output/MSDetectorControl.h>
      26              : #include <microsim/output/MSE2Collector.h>
      27              : #include <microsim/MSNet.h>
      28              : #include <libsumo/Helper.h>
      29              : #include <libsumo/TraCIConstants.h>
      30              : #include "LaneArea.h"
      31              : 
      32              : 
      33              : namespace libsumo {
      34              : // ===========================================================================
      35              : // static member initializations
      36              : // ===========================================================================
      37              : SubscriptionResults LaneArea::mySubscriptionResults;
      38              : ContextSubscriptionResults LaneArea::myContextSubscriptionResults;
      39              : NamedRTree* LaneArea::myTree(nullptr);
      40              : 
      41              : 
      42              : // ===========================================================================
      43              : // static member definitions
      44              : // ===========================================================================
      45              : std::vector<std::string>
      46          857 : LaneArea::getIDList() {
      47              :     std::vector<std::string> ids;
      48          857 :     MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).insertIDs(ids);
      49          855 :     return ids;
      50            2 : }
      51              : 
      52              : 
      53              : int
      54           26 : LaneArea::getIDCount() {
      55              :     std::vector<std::string> ids;
      56           52 :     return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).size();
      57           26 : }
      58              : 
      59              : 
      60              : int
      61          429 : LaneArea::getJamLengthVehicle(const std::string& detID) {
      62          429 :     return getDetector(detID)->getCurrentJamLengthInVehicles();
      63              : }
      64              : 
      65              : 
      66              : double
      67          428 : LaneArea::getJamLengthMeters(const std::string& detID) {
      68          428 :     return getDetector(detID)->getCurrentJamLengthInMeters();
      69              : }
      70              : 
      71              : 
      72              : double
      73          428 : LaneArea::getLastStepMeanSpeed(const std::string& detID) {
      74          428 :     return getDetector(detID)->getCurrentMeanSpeed();
      75              : }
      76              : 
      77              : 
      78              : std::vector<std::string>
      79          628 : LaneArea::getLastStepVehicleIDs(const std::string& detID) {
      80          628 :     return getDetector(detID)->getCurrentVehicleIDs();
      81              : }
      82              : 
      83              : 
      84              : double
      85          428 : LaneArea::getLastStepOccupancy(const std::string& detID) {
      86          428 :     return getDetector(detID)->getCurrentOccupancy();
      87              : }
      88              : 
      89              : 
      90              : double
      91          429 : LaneArea::getPosition(const std::string& detID) {
      92          429 :     return getDetector(detID)->getStartPos();
      93              : }
      94              : 
      95              : 
      96              : std::string
      97          429 : LaneArea::getLaneID(const std::string& detID) {
      98          429 :     return getDetector(detID)->getLane()->getID();
      99              : }
     100              : 
     101              : 
     102              : double
     103          429 : LaneArea::getLength(const std::string& detID) {
     104          429 :     const MSE2Collector* const e2 = getDetector(detID);
     105          429 :     return e2->getLength();
     106              : }
     107              : 
     108              : 
     109              : int
     110         1088 : LaneArea::getLastStepVehicleNumber(const std::string& detID) {
     111         1088 :     return getDetector(detID)->getCurrentVehicleNumber();
     112              : }
     113              : 
     114              : 
     115              : int
     116          428 : LaneArea::getLastStepHaltingNumber(const std::string& detID) {
     117          428 :     return getDetector(detID)->getCurrentHaltingNumber();
     118              : }
     119              : 
     120              : 
     121              : double
     122          608 : LaneArea::getIntervalOccupancy(const std::string& detID) {
     123          608 :     return getDetector(detID)->getIntervalOccupancy();
     124              : }
     125              : 
     126              : double
     127          608 : LaneArea::getIntervalMeanSpeed(const std::string& detID) {
     128          608 :     return getDetector(detID)->getIntervalMeanSpeed();
     129              : }
     130              : 
     131              : double
     132          608 : LaneArea::getIntervalMeanTimeLoss(const std::string& detID) {
     133          608 :     return getDetector(detID)->getIntervalMeanTimeLoss();
     134              : }
     135              : 
     136              : double
     137          608 : LaneArea::getIntervalMaxJamLengthInMeters(const std::string& detID) {
     138          608 :     return getDetector(detID)->getIntervalMaxJamLengthInMeters();
     139              : }
     140              : 
     141              : int
     142          608 : LaneArea::getIntervalVehicleNumber(const std::string& detID) {
     143          608 :     return getDetector(detID)->getIntervalVehicleNumber();
     144              : }
     145              : 
     146              : double
     147          608 : LaneArea::getLastIntervalOccupancy(const std::string& detID) {
     148          608 :     return getDetector(detID)->getLastIntervalOccupancy();
     149              : }
     150              : 
     151              : double
     152          608 : LaneArea::getLastIntervalMeanSpeed(const std::string& detID) {
     153          608 :     return getDetector(detID)->getLastIntervalMeanSpeed();
     154              : }
     155              : 
     156              : double
     157          608 : LaneArea::getLastIntervalMeanTimeLoss(const std::string& detID) {
     158          608 :     return getDetector(detID)->getLastIntervalMeanTimeLoss();
     159              : }
     160              : 
     161              : double
     162          608 : LaneArea::getLastIntervalMaxJamLengthInMeters(const std::string& detID) {
     163          608 :     return getDetector(detID)->getLastIntervalMaxJamLengthInMeters();
     164              : }
     165              : 
     166              : int
     167          608 : LaneArea::getLastIntervalVehicleNumber(const std::string& detID) {
     168          608 :     return getDetector(detID)->getLastIntervalVehicleNumber();
     169              : }
     170              : 
     171              : 
     172              : std::string
     173           46 : LaneArea::getParameter(const std::string& detID, const std::string& param) {
     174           92 :     return getDetector(detID)->getParameter(param, "");
     175              : }
     176              : 
     177              : 
     178           16 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(LaneArea)
     179              : 
     180              : 
     181              : void
     182           15 : LaneArea::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
     183           15 :     getDetector(detID)->setParameter(name, value);
     184           15 : }
     185              : 
     186              : 
     187          398 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(LaneArea, LANEAREA)
     188              : 
     189              : 
     190              : MSE2Collector*
     191        11721 : LaneArea::getDetector(const std::string& id) {
     192        23441 :     MSE2Collector* e2 = dynamic_cast<MSE2Collector*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).get(id));
     193        11721 :     if (e2 == nullptr) {
     194            2 :         throw TraCIException("Lane area detector '" + id + "' is not known");
     195              :     }
     196        11720 :     return e2;
     197              : }
     198              : 
     199              : 
     200              : NamedRTree*
     201          372 : LaneArea::getTree() {
     202          372 :     if (myTree == nullptr) {
     203           13 :         myTree = new NamedRTree();
     204           52 :         for (const std::string& id : getIDList()) {
     205           39 :             PositionVector shape;
     206           39 :             storeShape(id, shape);
     207           39 :             Boundary b = shape.getBoxBoundary();
     208           39 :             const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
     209           39 :             const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
     210           39 :             myTree->Insert(cmin, cmax, getDetector(id));
     211           52 :         }
     212              :     }
     213          372 :     return myTree;
     214              : }
     215              : 
     216              : 
     217              : void
     218        38670 : LaneArea::cleanup() {
     219        38670 :     delete myTree;
     220        38670 :     myTree = nullptr;
     221        38670 : }
     222              : 
     223              : 
     224              : void
     225          372 : LaneArea::storeShape(const std::string& id, PositionVector& shape) {
     226          372 :     MSE2Collector* const det = getDetector(id);
     227          744 :     shape.push_back(det->getLanes().front()->getShape().positionAtOffset(det->getStartPos()));
     228          744 :     shape.push_back(det->getLanes().back()->getShape().positionAtOffset(det->getEndPos()));
     229          372 : }
     230              : 
     231              : 
     232              : std::shared_ptr<VariableWrapper>
     233          270 : LaneArea::makeWrapper() {
     234          270 :     return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
     235              : }
     236              : 
     237              : 
     238              : void
     239           25 : LaneArea::overrideVehicleNumber(const std::string& detID, int vehNum) {
     240           25 :     getDetector(detID)->overrideVehicleNumber(vehNum);
     241           25 : }
     242              : 
     243              : 
     244              : bool
     245         7543 : LaneArea::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
     246         7543 :     switch (variable) {
     247          500 :         case TRACI_ID_LIST:
     248          500 :             return wrapper->wrapStringList(objID, variable, getIDList());
     249           20 :         case ID_COUNT:
     250           20 :             return wrapper->wrapInt(objID, variable, getIDCount());
     251          840 :         case LAST_STEP_VEHICLE_NUMBER:
     252          840 :             return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
     253          260 :         case LAST_STEP_MEAN_SPEED:
     254          260 :             return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
     255          380 :         case LAST_STEP_VEHICLE_ID_LIST:
     256          380 :             return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
     257          260 :         case LAST_STEP_VEHICLE_HALTING_NUMBER:
     258          260 :             return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
     259          261 :         case JAM_LENGTH_VEHICLE:
     260          261 :             return wrapper->wrapInt(objID, variable, getJamLengthVehicle(objID));
     261          260 :         case JAM_LENGTH_METERS:
     262          260 :             return wrapper->wrapDouble(objID, variable, getJamLengthMeters(objID));
     263          260 :         case LAST_STEP_OCCUPANCY:
     264          260 :             return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
     265          261 :         case VAR_POSITION:
     266          261 :             return wrapper->wrapDouble(objID, variable, getPosition(objID));
     267          261 :         case VAR_LANE_ID:
     268          522 :             return wrapper->wrapString(objID, variable, getLaneID(objID));
     269          261 :         case VAR_LENGTH:
     270          261 :             return wrapper->wrapDouble(objID, variable, getLength(objID));
     271          368 :         case VAR_INTERVAL_OCCUPANCY:
     272          368 :             return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
     273          368 :         case VAR_INTERVAL_SPEED:
     274          368 :             return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
     275          368 :         case VAR_INTERVAL_TIMELOSS:
     276          368 :             return wrapper->wrapDouble(objID, variable, getIntervalMeanTimeLoss(objID));
     277          368 :         case VAR_INTERVAL_MAX_JAM_LENGTH_METERS:
     278          368 :             return wrapper->wrapDouble(objID, variable, getIntervalMaxJamLengthInMeters(objID));
     279          368 :         case VAR_INTERVAL_NUMBER:
     280          368 :             return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
     281          368 :         case VAR_LAST_INTERVAL_OCCUPANCY:
     282          368 :             return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
     283          368 :         case VAR_LAST_INTERVAL_SPEED:
     284          368 :             return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
     285          368 :         case VAR_LAST_INTERVAL_TIMELOSS:
     286          368 :             return wrapper->wrapDouble(objID, variable, getLastIntervalMeanTimeLoss(objID));
     287          368 :         case VAR_LAST_INTERVAL_MAX_JAM_LENGTH_METERS:
     288          368 :             return wrapper->wrapDouble(objID, variable, getLastIntervalMaxJamLengthInMeters(objID));
     289          368 :         case VAR_LAST_INTERVAL_NUMBER:
     290          368 :             return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
     291           26 :         case libsumo::VAR_PARAMETER:
     292           26 :             paramData->readUnsignedByte();
     293           52 :             return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
     294            8 :         case libsumo::VAR_PARAMETER_WITH_KEY:
     295            8 :             paramData->readUnsignedByte();
     296           16 :             return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
     297              :         default:
     298              :             return false;
     299              :     }
     300              : }
     301              : 
     302              : 
     303              : }
     304              : 
     305              : 
     306              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1