LCOV - code coverage report
Current view: top level - src/libsumo - InductionLoop.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 156 161 96.9 %
Date: 2024-04-27 15:34:54 Functions: 34 39 87.2 %

          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    InductionLoop.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/MSInductLoop.h>
      27             : #include <mesosim/MEInductLoop.h>
      28             : #include <microsim/MSNet.h>
      29             : #include <microsim/MSEdge.h>
      30             : #include <libsumo/Helper.h>
      31             : #include <libsumo/TraCIDefs.h>
      32             : #include <libsumo/TraCIConstants.h>
      33             : #include "InductionLoop.h"
      34             : 
      35             : 
      36             : namespace libsumo {
      37             : // ===========================================================================
      38             : // static member initializations
      39             : // ===========================================================================
      40             : SubscriptionResults InductionLoop::mySubscriptionResults;
      41             : ContextSubscriptionResults InductionLoop::myContextSubscriptionResults;
      42             : NamedRTree* InductionLoop::myTree(nullptr);
      43             : 
      44             : 
      45             : // ===========================================================================
      46             : // member definitions
      47             : // ===========================================================================
      48             : std::vector<std::string>
      49         401 : InductionLoop::getIDList() {
      50             :     std::vector<std::string> ids;
      51         401 :     MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).insertIDs(ids);
      52         399 :     return ids;
      53           2 : }
      54             : 
      55             : 
      56             : int
      57          26 : InductionLoop::getIDCount() {
      58             :     std::vector<std::string> ids;
      59          52 :     return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).size();
      60          26 : }
      61             : 
      62             : 
      63             : double
      64          21 : InductionLoop::getPosition(const std::string& loopID) {
      65          21 :     return getDetector(loopID)->getPosition();
      66             : }
      67             : 
      68             : 
      69             : std::string
      70          21 : InductionLoop::getLaneID(const std::string& loopID) {
      71          21 :     return getDetector(loopID)->getLane()->getID();
      72             : }
      73             : 
      74             : 
      75             : int
      76        5120 : InductionLoop::getLastStepVehicleNumber(const std::string& loopID) {
      77        5120 :     return (int)getDetector(loopID)->getEnteredNumber((int)DELTA_T);
      78             : }
      79             : 
      80             : 
      81             : double
      82         162 : InductionLoop::getLastStepMeanSpeed(const std::string& loopID) {
      83         162 :     return getDetector(loopID)->getSpeed((int)DELTA_T);
      84             : }
      85             : 
      86             : 
      87             : std::vector<std::string>
      88        1232 : InductionLoop::getLastStepVehicleIDs(const std::string& loopID) {
      89        1232 :     return getDetector(loopID)->getVehicleIDs((int)DELTA_T);
      90             : }
      91             : 
      92             : 
      93             : double
      94         872 : InductionLoop::getLastStepOccupancy(const std::string& loopID) {
      95         872 :     return getDetector(loopID)->getOccupancy();
      96             : }
      97             : 
      98             : 
      99             : double
     100         162 : InductionLoop::getLastStepMeanLength(const std::string& loopID) {
     101         162 :     return getDetector(loopID)->getVehicleLength((int)DELTA_T);
     102             : }
     103             : 
     104             : 
     105             : double
     106         163 : InductionLoop::getTimeSinceDetection(const std::string& loopID) {
     107         163 :     return getDetector(loopID)->getTimeSinceLastDetection();
     108             : }
     109             : 
     110             : std::vector<libsumo::TraCIVehicleData>
     111       67160 : InductionLoop::getVehicleData(const std::string& loopID) {
     112       67160 :     const std::vector<MSInductLoop::VehicleData> vd = getDetector(loopID)->collectVehiclesOnDet(SIMSTEP - DELTA_T, true, true);
     113             :     std::vector<libsumo::TraCIVehicleData> tvd;
     114       77858 :     for (const MSInductLoop::VehicleData& vdi : vd) {
     115       10698 :         tvd.push_back(libsumo::TraCIVehicleData());
     116       10698 :         tvd.back().id = vdi.idM;
     117       10698 :         tvd.back().length = vdi.lengthM;
     118       10698 :         tvd.back().entryTime = vdi.entryTimeM;
     119       10698 :         tvd.back().leaveTime = vdi.leaveTimeM;
     120       10698 :         tvd.back().typeID = vdi.typeIDM;
     121             :     }
     122       67160 :     return tvd;
     123       67160 : }
     124             : 
     125             : 
     126             : double
     127         720 : InductionLoop::getIntervalOccupancy(const std::string& loopID) {
     128         720 :     if (MSGlobals::gUseMesoSim) {
     129         120 :         const MEInductLoop* det = getMEDetector(loopID);
     130             :         const auto& meanData = det->getMeanData();
     131         120 :         return meanData.getOccupancy(SIMSTEP - meanData.getResetTime(), det->getEdge().getNumLanes());
     132             :     } else {
     133         600 :         return getDetector(loopID)->getIntervalOccupancy();
     134             :     }
     135             : }
     136             : 
     137             : 
     138             : double
     139         720 : InductionLoop::getIntervalMeanSpeed(const std::string& loopID) {
     140         720 :     if (MSGlobals::gUseMesoSim) {
     141         120 :         const MEInductLoop* det = getMEDetector(loopID);
     142             :         const auto& meanData = det->getMeanData();
     143         120 :         if (meanData.getSamples() != 0) {
     144          69 :             return meanData.getTravelledDistance() / meanData.getSamples();
     145             :         } else {
     146          51 :             const double defaultTravelTime = det->getEdge().getLength() / det->getEdge().getSpeedLimit();
     147          51 :             return meanData.getLaneLength() / defaultTravelTime;
     148             :         }
     149             :     } else {
     150         600 :         return getDetector(loopID)->getIntervalMeanSpeed();
     151             :     }
     152             : }
     153             : 
     154             : 
     155             : int
     156         720 : InductionLoop::getIntervalVehicleNumber(const std::string& loopID) {
     157         720 :     if (MSGlobals::gUseMesoSim) {
     158         120 :         const auto& meanData = getMEDetector(loopID)->getMeanData();
     159         120 :         return meanData.nVehDeparted + meanData.nVehEntered;
     160             :     } else {
     161         600 :         return getDetector(loopID)->getIntervalVehicleNumber();
     162             :     }
     163             : }
     164             : 
     165             : 
     166             : std::vector<std::string>
     167         720 : InductionLoop::getIntervalVehicleIDs(const std::string& loopID) {
     168         720 :     if (MSGlobals::gUseMesoSim) {
     169         120 :         WRITE_ERROR("getIntervalVehicleIDs not applicable for meso");
     170         120 :         return std::vector<std::string>();
     171             :     }
     172         600 :     return getDetector(loopID)->getIntervalVehicleIDs();
     173             : }
     174             : 
     175             : 
     176             : double
     177         720 : InductionLoop::getLastIntervalOccupancy(const std::string& loopID) {
     178         720 :     if (MSGlobals::gUseMesoSim) {
     179         120 :         WRITE_ERROR("getLastIntervalOccupancy not applicable for meso");
     180         120 :         return INVALID_DOUBLE_VALUE;
     181             :     }
     182         600 :     return getDetector(loopID)->getIntervalOccupancy(true);
     183             : }
     184             : 
     185             : 
     186             : double
     187         720 : InductionLoop::getLastIntervalMeanSpeed(const std::string& loopID) {
     188         720 :     if (MSGlobals::gUseMesoSim) {
     189         120 :         WRITE_ERROR("getLastIntervalMeanSpeed not applicable for meso");
     190         120 :         return INVALID_DOUBLE_VALUE;
     191             :     }
     192         600 :     return getDetector(loopID)->getIntervalMeanSpeed(true);
     193             : }
     194             : 
     195             : 
     196             : int
     197         720 : InductionLoop::getLastIntervalVehicleNumber(const std::string& loopID) {
     198         720 :     if (MSGlobals::gUseMesoSim) {
     199         120 :         WRITE_ERROR("getLastIntervalVehicleNumber not applicable for meso");
     200         120 :         return INVALID_INT_VALUE;
     201             :     }
     202         600 :     return getDetector(loopID)->getIntervalVehicleNumber(true);
     203             : }
     204             : 
     205             : 
     206             : std::vector<std::string>
     207         720 : InductionLoop::getLastIntervalVehicleIDs(const std::string& loopID) {
     208         720 :     if (MSGlobals::gUseMesoSim) {
     209         120 :         WRITE_ERROR("getLastIntervalVehicleIDs not applicable for meso");
     210         120 :         return std::vector<std::string>();
     211             :     }
     212         600 :     return getDetector(loopID)->getIntervalVehicleIDs(true);
     213             : }
     214             : 
     215             : 
     216             : void
     217          25 : InductionLoop::overrideTimeSinceDetection(const std::string& loopID, double time) {
     218          25 :     getDetector(loopID)->overrideTimeSinceDetection(time);
     219          25 : }
     220             : 
     221             : 
     222             : MSInductLoop*
     223       79983 : InductionLoop::getDetector(const std::string& id) {
     224      159965 :     MSInductLoop* il = dynamic_cast<MSInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id));
     225       79983 :     if (il == nullptr) {
     226          10 :         throw TraCIException("Induction loop '" + id + "' is not known");
     227             :     }
     228       79978 :     return il;
     229             : }
     230             : 
     231             : 
     232             : MEInductLoop*
     233         360 : InductionLoop::getMEDetector(const std::string& id) {
     234         720 :     MEInductLoop* il = dynamic_cast<MEInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id));
     235         360 :     if (il == nullptr) {
     236           0 :         throw TraCIException("Induction loop '" + id + "' is not known");
     237             :     }
     238         360 :     return il;
     239             : }
     240             : 
     241             : 
     242             : 
     243             : std::string
     244          40 : InductionLoop::getParameter(const std::string& loopID, const std::string& param) {
     245          80 :     return getDetector(loopID)->getParameter(param, "");
     246             : }
     247             : 
     248             : 
     249           0 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(InductionLoop)
     250             : 
     251             : 
     252             : void
     253          20 : InductionLoop::setParameter(const std::string& loopID, const std::string& name, const std::string& value) {
     254          20 :     getDetector(loopID)->setParameter(name, value);
     255          20 : }
     256             : 
     257             : 
     258         192 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(InductionLoop, INDUCTIONLOOP)
     259             : 
     260             : 
     261             : NamedRTree*
     262         212 : InductionLoop::getTree() {
     263         212 :     if (myTree == nullptr) {
     264          10 :         myTree = new NamedRTree();
     265          40 :         for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
     266          30 :             MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
     267          30 :             Position p = il->getLane()->getShape().positionAtOffset(il->getPosition());
     268          30 :             const float cmin[2] = {(float) p.x(), (float) p.y()};
     269          30 :             const float cmax[2] = {(float) p.x(), (float) p.y()};
     270          30 :             myTree->Insert(cmin, cmax, il);
     271             :         }
     272             :     }
     273         212 :     return myTree;
     274             : }
     275             : 
     276             : void
     277       35156 : InductionLoop::cleanup() {
     278       35156 :     delete myTree;
     279       35156 :     myTree = nullptr;
     280       35156 : }
     281             : 
     282             : void
     283         185 : InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
     284         185 :     MSInductLoop* const il = getDetector(id);
     285         185 :     shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
     286         185 : }
     287             : 
     288             : 
     289             : std::shared_ptr<VariableWrapper>
     290         267 : InductionLoop::makeWrapper() {
     291         267 :     return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
     292             : }
     293             : 
     294             : 
     295             : bool
     296       60067 : InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
     297       60067 :     switch (variable) {
     298         259 :         case TRACI_ID_LIST:
     299         259 :             return wrapper->wrapStringList(objID, variable, getIDList());
     300          18 :         case ID_COUNT:
     301          18 :             return wrapper->wrapInt(objID, variable, getIDCount());
     302          13 :         case VAR_POSITION:
     303          13 :             return wrapper->wrapDouble(objID, variable, getPosition(objID));
     304          13 :         case VAR_LANE_ID:
     305          26 :             return wrapper->wrapString(objID, variable, getLaneID(objID));
     306        4712 :         case LAST_STEP_VEHICLE_NUMBER:
     307        4712 :             return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
     308         118 :         case LAST_STEP_MEAN_SPEED:
     309         118 :             return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
     310         776 :         case LAST_STEP_VEHICLE_ID_LIST:
     311         776 :             return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
     312         544 :         case LAST_STEP_OCCUPANCY:
     313         544 :             return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
     314         118 :         case LAST_STEP_LENGTH:
     315         118 :             return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
     316         119 :         case LAST_STEP_TIME_SINCE_DETECTION:
     317         119 :             return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
     318         480 :         case VAR_INTERVAL_OCCUPANCY:
     319         480 :             return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
     320         480 :         case VAR_INTERVAL_SPEED:
     321         480 :             return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
     322         480 :         case VAR_INTERVAL_NUMBER:
     323         480 :             return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
     324         480 :         case VAR_INTERVAL_IDS:
     325         480 :             return wrapper->wrapStringList(objID, variable, getIntervalVehicleIDs(objID));
     326         480 :         case VAR_LAST_INTERVAL_OCCUPANCY:
     327         480 :             return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
     328         480 :         case VAR_LAST_INTERVAL_SPEED:
     329         480 :             return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
     330         480 :         case VAR_LAST_INTERVAL_NUMBER:
     331         480 :             return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
     332         480 :         case VAR_LAST_INTERVAL_IDS:
     333         480 :             return wrapper->wrapStringList(objID, variable, getLastIntervalVehicleIDs(objID));
     334          24 :         case libsumo::VAR_PARAMETER:
     335          24 :             paramData->readUnsignedByte();
     336          48 :             return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
     337           0 :         case libsumo::VAR_PARAMETER_WITH_KEY:
     338           0 :             paramData->readUnsignedByte();
     339           0 :             return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
     340             :         default:
     341             :             return false;
     342             :     }
     343             : }
     344             : 
     345             : 
     346             : }
     347             : 
     348             : 
     349             : /****************************************************************************/

Generated by: LCOV version 1.14