1 /****************************************************************************/ 2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo 3 // Copyright (C) 2001-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 MSDetectorFileOutput.h 11 /// @author Christian Roessel 12 /// @author Daniel Krajzewicz 13 /// @author Sascha Krieg 14 /// @author Michael Behrisch 15 /// @date 2004-11-23 16 /// @version $Id$ 17 /// 18 // Base of value-generating classes (detectors) 19 /****************************************************************************/ 20 #ifndef MSDetectorFileOutput_h 21 #define MSDetectorFileOutput_h 22 23 24 // =========================================================================== 25 // included modules 26 // =========================================================================== 27 #include <config.h> 28 29 #include <string> 30 #include <set> 31 32 #include <utils/common/Named.h> 33 #include <utils/common/SUMOTime.h> 34 #include <utils/common/StringTokenizer.h> 35 #include <utils/iodevices/OutputDevice.h> 36 #include <utils/vehicle/SUMOTrafficObject.h> 37 #include <microsim/MSVehicleType.h> 38 #include <microsim/MSVehicleControl.h> 39 #include <microsim/MSNet.h> 40 41 42 // =========================================================================== 43 // class declarations 44 // =========================================================================== 45 class GUIDetectorWrapper; 46 47 48 // =========================================================================== 49 // class definitions 50 // =========================================================================== 51 enum DetectorUsage { 52 DU_USER_DEFINED, 53 DU_SUMO_INTERNAL, 54 DU_TL_CONTROL 55 }; 56 57 /** 58 * @class MSDetectorFileOutput 59 * @brief Base of value-generating classes (detectors) 60 * 61 * Pure virtual base class for classes (e.g. MSInductLoop) that should produce 62 * XML-output. 63 */ 64 class MSDetectorFileOutput : public Named { 65 public: 66 /// @brief Constructor 67 MSDetectorFileOutput(const std::string& id, const std::string& vTypes, const int detectPersons = false) : Named(id)68 Named(id), 69 myDetectPersons(detectPersons) { 70 const std::vector<std::string> vt = StringTokenizer(vTypes).getVector(); 71 myVehicleTypes.insert(vt.begin(), vt.end()); 72 } 73 74 /// @brief Constructor 75 MSDetectorFileOutput(const std::string& id, const std::set<std::string>& vTypes, const int detectPersons = false) Named(id)76 : Named(id), myVehicleTypes(vTypes), myDetectPersons(detectPersons) 77 { } 78 79 80 /// @brief (virtual) destructor ~MSDetectorFileOutput()81 virtual ~MSDetectorFileOutput() { } 82 83 84 /// @name Virtual methods to implement by derived classes 85 /// @{ 86 87 /** @brief Write the generated output to the given device 88 * @param[in] dev The output device to write the data into 89 * @param[in] startTime First time step the data were gathered 90 * @param[in] stopTime Last time step the data were gathered 91 * @exception IOError If an error on writing occurs 92 */ 93 virtual void writeXMLOutput(OutputDevice& dev, 94 SUMOTime startTime, SUMOTime stopTime) = 0; 95 96 97 /** @brief Open the XML-output 98 * 99 * The implementing function should open an xml element using 100 * OutputDevice::writeXMLHeader. 101 * 102 * @param[in] dev The output device to write the root into 103 * @exception IOError If an error on writing occurs 104 */ 105 virtual void writeXMLDetectorProlog(OutputDevice& dev) const = 0; 106 107 108 /** @brief Resets collected values 109 * 110 * Please note that this is only a "hack" for coupled-tls-outputs. 111 * 112 * @see Command_SaveTLCoupledLaneDet 113 * @todo Reckeck/refactor 114 */ reset()115 virtual void reset() { } 116 117 118 /** @brief Updates the detector (computes values) 119 * 120 * @param[in] step The current time step 121 */ detectorUpdate(const SUMOTime step)122 virtual void detectorUpdate(const SUMOTime step) { 123 UNUSED_PARAMETER(step); 124 } 125 126 127 /** @brief Builds the graphical representation 128 * 129 * Meant to be overridden by graphical versions of the detectors 130 * @return A wrapper for the detector which performs the user I/O within the GUI 131 */ buildDetectorGUIRepresentation()132 virtual GUIDetectorWrapper* buildDetectorGUIRepresentation() { 133 return 0; 134 } 135 136 137 /** @brief Checks whether the detector measures vehicles of the given type. 138 * 139 * @param[in] veh the vehicle of which the type is checked. 140 * @return whether it should be measured 141 */ vehicleApplies(const SUMOTrafficObject & veh)142 bool vehicleApplies(const SUMOTrafficObject& veh) const { 143 if (veh.isVehicle() == detectPersons()) { 144 return false; 145 } else if (myVehicleTypes.empty() || myVehicleTypes.count(veh.getVehicleType().getID()) > 0) { 146 return true; 147 } else { 148 std::set<std::string> vTypeDists = MSNet::getInstance()->getVehicleControl().getVTypeDistributionMembership(veh.getVehicleType().getID()); 149 for (auto vTypeDist : vTypeDists) { 150 if (myVehicleTypes.count(vTypeDist) > 0) { 151 return true; 152 } 153 } 154 return false; 155 } 156 } 157 158 159 /** @brief Checks whether the detector is type specific. 160 * 161 * @return whether vehicle types are considered 162 */ isTyped()163 bool isTyped() const { 164 return !myVehicleTypes.empty(); 165 } 166 detectPersons()167 inline bool detectPersons() const { 168 return myDetectPersons != 0; 169 } 170 171 protected: 172 /// @brief The vehicle types to look for (empty means all) 173 std::set<std::string> myVehicleTypes; 174 175 /// @brief Whether pedestrians shall be detected instead of vehicles 176 const int myDetectPersons; 177 178 private: 179 /// @brief Invalidated copy constructor. 180 MSDetectorFileOutput(const MSDetectorFileOutput&); 181 182 /// @brief Invalidated assignment operator. 183 MSDetectorFileOutput& operator=(const MSDetectorFileOutput&); 184 185 186 }; 187 188 189 #endif 190 191 /****************************************************************************/ 192 193