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.org/sumo
3// Copyright (C) 2013-2023 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// some attributes are not written by default and must be enabled via option fcd-output.attributes
38const long long int MSDevice_FCD::myDefaultMask(~(
39 ((long long int)1 << SUMO_ATTR_VEHICLE) |
40 ((long long int)1 << SUMO_ATTR_ODOMETER) |
41 ((long long int)1 << SUMO_ATTR_SPEED_LAT) |
42 ((long long int)1 << SUMO_ATTR_POSITION_LAT)
43 ));
44
45// ===========================================================================
46// static members
47// ===========================================================================
48std::set<const MSEdge*> MSDevice_FCD::myEdgeFilter;
49std::vector<PositionVector> MSDevice_FCD::myShape4Filters;
53long long int MSDevice_FCD::myWrittenAttributes(myDefaultMask);
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 initOnce();
84 }
85}
86
87
88// ---------------------------------------------------------------------------
89// MSDevice_FCD-methods
90// ---------------------------------------------------------------------------
91MSDevice_FCD::MSDevice_FCD(SUMOVehicle& holder, const std::string& id) :
92 MSVehicleDevice(holder, id) {
93}
94
95
97}
98
99bool
101 // lazily build the shape filter in the case where route file is loaded as an additional file
104 }
105 const MSVehicle* msVeh = dynamic_cast<const MSVehicle*>(veh);
106 for (auto shape : myShape4Filters) {
107 if (shape.around(veh->getPosition()) || ((msVeh != nullptr) && shape.around(msVeh->getBackPosition()))) {
108 return true;
109 }
110 }
111 return false;
112}
113
114
115void
118 if (oc.isSet("fcd-output.filter-shapes")) {
119 const ShapeContainer& loadedShapes = MSNet::getInstance()->getShapeContainer();
120 if (loadedShapes.getPolygons().size() > 0) {
121 for (std::string attrName : oc.getStringVector("fcd-output.filter-shapes")) {
122 if (loadedShapes.getPolygons().get(attrName) == 0) {
123 WRITE_ERRORF(TL("Specified shape '%' for filtering fcd-output could not be found."), attrName);
124 } else {
125 // store the PositionVector, not reference, as traci can manipulate / detete the polygons
126 myShape4Filters.push_back(loadedShapes.getPolygons().get(attrName)->getShape());
127 }
128 }
130 }
131 } else {
133 }
134}
135
136
137void
140 return;
141 }
144 if (oc.isSet("fcd-output.filter-edges.input-file")) {
145 const std::string file = oc.getString("fcd-output.filter-edges.input-file");
146 std::ifstream strm(file.c_str());
147 if (!strm.good()) {
148 throw ProcessError(TLF("Could not load names of edges for filtering fcd-output from '%'.", file));
149 }
150 while (strm.good()) {
151 std::string name;
152 strm >> name;
153 // maybe we're loading an edge-selection
154 if (StringUtils::startsWith(name, "edge:")) {
155 name = name.substr(5);
156 }
157 myEdgeFilter.insert(MSEdge::dictionary(name));
158 }
159 }
160 if (oc.isSet("fcd-output.attributes")) {
162 for (std::string attrName : oc.getStringVector("fcd-output.attributes")) {
163 if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
164 if (attrName == "all") {
166 } else {
167 WRITE_ERRORF(TL("Unknown attribute '%' to write in fcd output."), attrName);
168 }
169 continue;
170 }
171 int attr = SUMOXMLDefinitions::Attrs.get(attrName);
172 assert(attr <= 63);
173 myWrittenAttributes |= ((long long int)1 << attr);
174 }
175 }
176
177 if (oc.isSet("fcd-output.filter-shapes")) {
178 // build the shape filter if it is desired
181 }
182 //std::cout << "mask=" << myWrittenAttributes << " binary=" << std::bitset<64>(myWrittenAttributes) << "\n";
183}
184
185
186void
188 myEdgeFilter.clear();
189 myShape4Filters.clear();
192 myShapeFilterDesired = false;
194}
195
196
197/****************************************************************************/
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:277
#define TL(string)
Definition: MsgHandler.h:284
#define TLF(string,...)
Definition: MsgHandler.h:285
@ SUMO_ATTR_POSITION_LAT
@ SUMO_ATTR_ODOMETER
@ 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 long long int myWrittenAttributes
bit mask for checking attributes to be written
Definition: MSDevice_FCD.h:125
static const long long int myDefaultMask
Definition: MSDevice_FCD.h:126
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 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:144
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:202
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:945
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
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:1469
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:75
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:59
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.cpp:52
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
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.