Eclipse SUMO - Simulation of Urban MObility
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-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 /****************************************************************************/
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 // ===========================================================================
41 std::set<const MSEdge*> MSDevice_FCD::myEdgeFilter;
42 std::vector<PositionVector> MSDevice_FCD::myShape4Filters;
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 // ---------------------------------------------------------------------------
52 // static initialisation methods
53 // ---------------------------------------------------------------------------
54 void
56  oc.addOptionSubTopic("FCD Device");
57  insertDefaultAssignmentOptions("fcd", "FCD Device", oc);
58 
59  oc.doRegister("device.fcd.begin", new Option_String("-1"));
60  oc.addDescription("device.fcd.begin", "FCD Device", TL("Recording begin time for FCD-data"));
61 
62  oc.doRegister("device.fcd.period", new Option_String("0"));
63  oc.addDescription("device.fcd.period", "FCD Device", TL("Recording period for FCD-data"));
64 
65  oc.doRegister("device.fcd.radius", new Option_Float(0));
66  oc.addDescription("device.fcd.radius", "FCD Device", TL("Record objects in a radius around equipped vehicles"));
67 }
68 
69 
70 void
71 MSDevice_FCD::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
73  if (equippedByDefaultAssignmentOptions(oc, "fcd", v, oc.isSet("fcd-output"))) {
74  MSDevice_FCD* device = new MSDevice_FCD(v, "fcd_" + v.getID());
75  into.push_back(device);
76  initOnce();
77  }
78 }
79 
80 
81 // ---------------------------------------------------------------------------
82 // MSDevice_FCD-methods
83 // ---------------------------------------------------------------------------
84 MSDevice_FCD::MSDevice_FCD(SUMOVehicle& holder, const std::string& id) :
85  MSVehicleDevice(holder, id) {
86 }
87 
88 
90 }
91 
92 
95  SumoXMLAttrMask mask;
96  // set all bits to 1
97  mask.set();
98  // some attributes are not written by default and must be enabled via option fcd-output.attributes
99  // (or with an explicit attribute list)
100  mask.reset(SUMO_ATTR_VEHICLE);
101  mask.reset(SUMO_ATTR_ODOMETER);
102  mask.reset(SUMO_ATTR_SPEED_LAT);
103  mask.reset(SUMO_ATTR_POSITION_LAT);
104  mask.reset(SUMO_ATTR_ARRIVALDELAY);
105  return mask;
106 }
107 
108 
109 bool
111  // lazily build the shape filter in the case where route file is loaded as an additional file
114  }
115  const MSVehicle* msVeh = dynamic_cast<const MSVehicle*>(veh);
116  for (auto shape : myShape4Filters) {
117  if (shape.around(veh->getPosition()) || ((msVeh != nullptr) && shape.around(msVeh->getBackPosition()))) {
118  return true;
119  }
120  }
121  return false;
122 }
123 
124 
125 void
127  const OptionsCont& oc = OptionsCont::getOptions();
128  if (oc.isSet("fcd-output.filter-shapes")) {
129  const ShapeContainer& loadedShapes = MSNet::getInstance()->getShapeContainer();
130  if (loadedShapes.getPolygons().size() > 0) {
131  for (std::string attrName : oc.getStringVector("fcd-output.filter-shapes")) {
132  if (loadedShapes.getPolygons().get(attrName) == 0) {
133  WRITE_ERRORF(TL("Specified shape '%' for filtering fcd-output could not be found."), attrName);
134  } else {
135  // store the PositionVector, not reference, as traci can manipulate / detete the polygons
136  myShape4Filters.push_back(loadedShapes.getPolygons().get(attrName)->getShape());
137  }
138  }
140  }
141  } else {
143  }
144 }
145 
146 
147 void
150  return;
151  }
153  const OptionsCont& oc = OptionsCont::getOptions();
154  if (oc.isSet("fcd-output.filter-edges.input-file")) {
155  const std::string file = oc.getString("fcd-output.filter-edges.input-file");
156  std::ifstream strm(file.c_str());
157  if (!strm.good()) {
158  throw ProcessError(TLF("Could not load names of edges for filtering fcd-output from '%'.", file));
159  }
160  while (strm.good()) {
161  std::string name;
162  strm >> name;
163  // maybe we're loading an edge-selection
164  if (StringUtils::startsWith(name, "edge:")) {
165  name = name.substr(5);
166  }
167  myEdgeFilter.insert(MSEdge::dictionary(name));
168  }
169  }
170  if (oc.isSet("fcd-output.attributes")) {
171  myWrittenAttributes.reset();
172  for (std::string attrName : oc.getStringVector("fcd-output.attributes")) {
173  if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
174  if (attrName == "all") {
175  myWrittenAttributes.set();
176  } else {
177  WRITE_ERRORF(TL("Unknown attribute '%' to write in fcd output."), attrName);
178  }
179  continue;
180  }
181  int attr = SUMOXMLDefinitions::Attrs.get(attrName);
182  myWrittenAttributes.set(attr);
183  }
184  }
185 
186  if (oc.isSet("fcd-output.filter-shapes")) {
187  // build the shape filter if it is desired
188  myShapeFilterDesired = true;
190  }
191  //std::cout << "mask=" << myWrittenAttributes << " binary=" << std::bitset<64>(myWrittenAttributes) << "\n";
192 }
193 
194 
195 void
197  myEdgeFilter.clear();
198  myShape4Filters.clear();
199  myEdgeFilterInitialized = false;
200  myShapeFilterInitialized = false;
201  myShapeFilterDesired = false;
203 }
204 
205 
206 /****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
std::bitset< 96 > SumoXMLAttrMask
@ SUMO_ATTR_POSITION_LAT
@ SUMO_ATTR_ODOMETER
@ SUMO_ATTR_ARRIVALDELAY
@ SUMO_ATTR_VEHICLE
@ SUMO_ATTR_SPEED_LAT
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_FCD.h:47
static void cleanup()
resets the edge filter
static SumoXMLAttrMask getDefaultMask()
MSDevice_FCD(SUMOVehicle &holder, const std::string &id)
Constructor.
~MSDevice_FCD()
Destructor.
static bool myShapeFilterInitialized
Definition: MSDevice_FCD.h:121
static std::vector< PositionVector > myShape4Filters
polygon spatial filter for FCD output
Definition: MSDevice_FCD.h:120
static SumoXMLAttrMask myWrittenAttributes
bit mask for checking attributes to be written
Definition: MSDevice_FCD.h:125
static std::set< const MSEdge * > myEdgeFilter
edge filter for FCD output
Definition: MSDevice_FCD.h:116
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
static bool myEdgeFilterInitialized
Definition: MSDevice_FCD.h:117
static bool myShapeFilterDesired
Definition: MSDevice_FCD.h:122
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 void buildShapeFilter()
static bool shapeFilter(const SUMOTrafficObject *veh)
checks if in polygon
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:155
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:203
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:983
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:501
Abstract in-vehicle device.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
const Position getBackPosition() const
Definition: MSVehicle.cpp:1530
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.
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.
Definition: OptionsCont.cpp:76
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
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.
Definition: OptionsCont.cpp:60
const PositionVector & getShape() const
Returns the shape of the polygon.
Definition: SUMOPolygon.cpp:51
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:60
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
Storage for geometrical objects.
const Polygons & getPolygons() const
Returns all polygons.
T get(const std::string &str) const
static bool startsWith(const std::string &str, const std::string prefix)
Checks whether a given string starts with the prefix.