Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSDevice_FCD.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2013-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/****************************************************************************/
20// A device which stands as an implementation FCD and which outputs movereminder calls
21/****************************************************************************/
22#include <config.h>
23
24#include <bitset>
31#include <microsim/MSNet.h>
32#include <microsim/MSLane.h>
33#include <microsim/MSEdge.h>
34#include <microsim/MSVehicle.h>
35#include "MSDevice_FCD.h"
36
37
38// ===========================================================================
39// static members
40// ===========================================================================
45std::vector<std::string> MSDevice_FCD::myParamsToWrite;
47std::set<const MSEdge*> MSDevice_FCD::myEdgeFilter;
48std::vector<PositionVector> MSDevice_FCD::myShape4Filters;
53
54
55// ===========================================================================
56// method definitions
57// ===========================================================================
58// ---------------------------------------------------------------------------
59// static initialisation methods
60// ---------------------------------------------------------------------------
61void
63 oc.addOptionSubTopic("FCD Device");
64 insertDefaultAssignmentOptions("fcd", "FCD Device", oc);
65
66 oc.doRegister("device.fcd.begin", new Option_String("-1"));
67 oc.addDescription("device.fcd.begin", "FCD Device", TL("Recording begin time for FCD-data"));
68
69 oc.doRegister("device.fcd.period", new Option_String("0"));
70 oc.addDescription("device.fcd.period", "FCD Device", TL("Recording period for FCD-data"));
71
72 oc.doRegister("device.fcd.radius", new Option_Float(0));
73 oc.addDescription("device.fcd.radius", "FCD Device", TL("Record objects in a radius around equipped vehicles"));
74}
75
76
77void
78MSDevice_FCD::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
80 if (equippedByDefaultAssignmentOptions(oc, "fcd", v, oc.isSet("fcd-output"))) {
81 MSDevice_FCD* device = new MSDevice_FCD(v, "fcd_" + v.getID());
82 into.push_back(device);
83 }
84}
85
86
87// ---------------------------------------------------------------------------
88// MSDevice_FCD-methods
89// ---------------------------------------------------------------------------
90MSDevice_FCD::MSDevice_FCD(SUMOVehicle& holder, const std::string& id) :
91 MSVehicleDevice(holder, id) {
92}
93
94
97
98
101 SumoXMLAttrMask mask;
102 mask.set(SUMO_ATTR_X);
103 mask.set(SUMO_ATTR_Y);
104 if (MSNet::getInstance()->hasElevation()) {
105 mask.set(SUMO_ATTR_Z);
106 }
107 mask.set(SUMO_ATTR_ANGLE);
108 mask.set(SUMO_ATTR_TYPE);
109 mask.set(SUMO_ATTR_SPEED);
110 mask.set(SUMO_ATTR_POSITION);
111 mask.set(SUMO_ATTR_LANE); // for micro vehicles only
112 mask.set(SUMO_ATTR_EDGE); // for persons and meso vehicles
113 mask.set(SUMO_ATTR_SLOPE);
114 if (!MSGlobals::gUseMesoSim && OptionsCont::getOptions().getFloat("fcd-output.max-leader-distance") > 0.) {
115 mask.set(SUMO_ATTR_LEADER_ID);
116 mask.set(SUMO_ATTR_LEADER_SPEED);
117 mask.set(SUMO_ATTR_LEADER_GAP);
118 }
119 return mask;
120}
121
122
123bool
125 // lazily build the shape filter in the case where route file is loaded as an additional file
128 }
129 const MSVehicle* msVeh = dynamic_cast<const MSVehicle*>(veh);
130 for (auto shape : myShape4Filters) {
131 if (shape.around(veh->getPosition()) || ((msVeh != nullptr) && shape.around(msVeh->getBackPosition()))) {
132 return true;
133 }
134 }
135 return false;
136}
137
138
139void
142 if (oc.isSet("fcd-output.filter-shapes")) {
143 const ShapeContainer& loadedShapes = MSNet::getInstance()->getShapeContainer();
144 if (loadedShapes.getPolygons().size() > 0) {
145 for (std::string attrName : oc.getStringVector("fcd-output.filter-shapes")) {
146 if (loadedShapes.getPolygons().get(attrName) == 0) {
147 WRITE_ERRORF(TL("Specified shape '%' for filtering fcd-output could not be found."), attrName);
148 } else {
149 // store the PositionVector, not reference, as traci can manipulate / detete the polygons
150 myShape4Filters.push_back(loadedShapes.getPolygons().get(attrName)->getShape());
151 }
152 }
154 }
155 } else {
157 }
158}
159
160
161void
164 return;
165 }
168 myPeriod = string2time(oc.getString("device.fcd.period"));
169 myBegin = string2time(oc.getString("device.fcd.begin"));
170 myUseGeo = oc.getBool("fcd-output.geo");
171 myMaxLeaderDistance = oc.getFloat("fcd-output.max-leader-distance");
172 myParamsToWrite = oc.getStringVector("fcd-output.params");
173 myRadius = oc.getFloat("device.fcd.radius");
174 if (oc.isSet("fcd-output.filter-edges.input-file")) {
175 const std::string file = oc.getString("fcd-output.filter-edges.input-file");
176 std::ifstream strm(file.c_str());
177 if (!strm.good()) {
178 throw ProcessError(TLF("Could not load names of edges for filtering fcd-output from '%'.", file));
179 }
180 while (strm.good()) {
181 std::string name;
182 strm >> name;
183 // maybe we're loading an edge-selection
184 if (StringUtils::startsWith(name, "edge:")) {
185 name = name.substr(5);
186 }
187 myEdgeFilter.insert(MSEdge::dictionary(name));
188 }
189 }
190 SumoXMLAttrMask emissions;
191 emissions.set(SUMO_ATTR_ECLASS);
192 emissions.set(SUMO_ATTR_CO2);
193 emissions.set(SUMO_ATTR_CO);
194 emissions.set(SUMO_ATTR_HC);
195 emissions.set(SUMO_ATTR_PMX);
196 emissions.set(SUMO_ATTR_FUEL);
197 emissions.set(SUMO_ATTR_ELECTRICITY);
198 SumoXMLAttrMask misc;
199 misc.set(SUMO_ATTR_SIGNALS);
200 misc.set(SUMO_ATTR_ACCELERATION);
202 misc.set(SUMO_ATTR_DISTANCE);
203 misc.set(SUMO_ATTR_ODOMETER);
204 misc.set(SUMO_ATTR_POSITION_LAT);
205 misc.set(SUMO_ATTR_SPEED_LAT);
206 misc.set(SUMO_ATTR_LEADER_ID);
207 misc.set(SUMO_ATTR_LEADER_SPEED);
208 misc.set(SUMO_ATTR_ARRIVALDELAY);
209 misc.set(SUMO_ATTR_SEGMENT);
210 misc.set(SUMO_ATTR_QUEUE);
211 misc.set(SUMO_ATTR_ENTRYTIME);
212 misc.set(SUMO_ATTR_EVENTTIME);
213 misc.set(SUMO_ATTR_BLOCKTIME);
214 const std::map<std::string, SumoXMLAttrMask> special = {{"location", getDefaultMask()}, {"emissions", emissions}, {"misc", misc}};
215 if (oc.isSet("fcd-output.attributes")) {
216 myWrittenAttributes = OutputDevice::parseWrittenAttributes(oc.getStringVector("fcd-output.attributes"), "fcd output", special);
217 } else {
219 }
220 // need to store this because some attributes are reset later
221 const bool all = myWrittenAttributes.all();
223 if (!MSNet::getInstance()->hasElevation()) {
225 }
226 if (oc.getBool("fcd-output.signals")) {
228 }
229 if (oc.getBool("fcd-output.acceleration")) {
231 }
233 if (oc.getBool("fcd-output.distance")) {
235 }
236
237 if (oc.isSet("fcd-output.filter-shapes")) {
238 // build the shape filter if it is desired
241 }
243}
244
245
246void
248 myEdgeFilter.clear();
249 myShape4Filters.clear();
252 myShapeFilterDesired = false;
253 myWrittenAttributes.reset();
254}
255
256
257/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_ERRORF(...)
Definition MsgHandler.h:297
#define TL(string)
Definition MsgHandler.h:305
#define TLF(string,...)
Definition MsgHandler.h:307
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
#define SUMOTime_MAX
Definition SUMOTime.h:34
std::bitset< 96 > SumoXMLAttrMask
@ SUMO_ATTR_LANE
@ SUMO_ATTR_SPEED
@ SUMO_ATTR_PMX
@ SUMO_ATTR_Y
@ SUMO_ATTR_Z
@ SUMO_ATTR_EDGE
@ SUMO_ATTR_X
@ SUMO_ATTR_HC
@ SUMO_ATTR_POSITION_LAT
@ SUMO_ATTR_FUEL
@ SUMO_ATTR_ODOMETER
@ SUMO_ATTR_ELECTRICITY
@ SUMO_ATTR_SIGNALS
@ SUMO_ATTR_LEADER_GAP
@ SUMO_ATTR_QUEUE
@ SUMO_ATTR_CO
@ SUMO_ATTR_ACCELERATION_LAT
@ SUMO_ATTR_ARRIVALDELAY
@ SUMO_ATTR_BLOCKTIME
@ SUMO_ATTR_SLOPE
@ SUMO_ATTR_ANGLE
@ SUMO_ATTR_ACCELERATION
@ SUMO_ATTR_LEADER_SPEED
@ SUMO_ATTR_DISTANCE
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_EVENTTIME
@ SUMO_ATTR_SEGMENT
@ SUMO_ATTR_CO2
@ SUMO_ATTR_ID
@ SUMO_ATTR_SPEED_LAT
@ SUMO_ATTR_ENTRYTIME
@ SUMO_ATTR_ECLASS
@ SUMO_ATTR_LEADER_ID
@ SUMO_ATTR_POSITION
A device which collects info on the vehicle trip (mainly on departure and arrival)
static void cleanup()
resets the edge filter
static SumoXMLAttrMask getDefaultMask()
MSDevice_FCD(SUMOVehicle &holder, const std::string &id)
Constructor.
static SUMOTime myBegin
begin time
~MSDevice_FCD()
Destructor.
static bool myShapeFilterInitialized
static std::vector< PositionVector > myShape4Filters
polygon spatial filter for FCD output
static SumoXMLAttrMask myWrittenAttributes
bit mask for checking attributes to be written
static std::set< const MSEdge * > myEdgeFilter
edge filter for FCD output
static std::vector< std::string > myParamsToWrite
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
static double myRadius
static bool myEdgeFilterInitialized
static bool myShapeFilterDesired
static void initOnce()
initialize edge filter and attribute mask (once)
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static double myMaxLeaderDistance
static SUMOTime myPeriod
static void buildShapeFilter()
static bool shapeFilter(const SUMOTrafficObject *veh)
checks if in polygon
static bool myUseGeo
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc, const bool isPerson=false)
Adds common command options that allow to assign devices to vehicles.
Definition MSDevice.cpp:157
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition MSDevice.h:200
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary....
Definition MSEdge.cpp:1053
static bool gUseMesoSim
Definition MSGlobals.h:106
static bool gSublane
whether sublane simulation is enabled (sublane model or continuous lanechanging)
Definition MSGlobals.h:165
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition MSNet.h:507
Abstract in-vehicle device.
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
const Position getBackPosition() const
const std::string & getID() const
Returns the id.
Definition Named.h:74
T get(const std::string &id) const
Retrieves an item.
int size() const
Returns the number of stored items within the container.
A storage for options typed value containers)
Definition OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
static const SumoXMLAttrMask parseWrittenAttributes(const std::vector< std::string > &attrList, const std::string &desc, const std::map< std::string, SumoXMLAttrMask > &special=std::map< std::string, SumoXMLAttrMask >())
Parses a list of strings for attribute names and sets the relevant bits in the returned mask.
void setExpectedAttributes(const SumoXMLAttrMask &expected, const int depth=2)
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
const PositionVector & getShape() const
Returns the shape of the polygon.
Representation of a vehicle, person, or container.
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
Representation of a vehicle.
Definition SUMOVehicle.h:62
Storage for geometrical objects.
const Polygons & getPolygons() const
Returns all polygons.
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.