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-2022 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_POSITION_LAT)
42 ));
43
44// ===========================================================================
45// static members
46// ===========================================================================
47std::set<const MSEdge*> MSDevice_FCD::myEdgeFilter;
48std::vector<PositionVector> MSDevice_FCD::myShape4Filters;
52long long int MSDevice_FCD::myWrittenAttributes(myDefaultMask);
53
54// ===========================================================================
55// method definitions
56// ===========================================================================
57// ---------------------------------------------------------------------------
58// static initialisation methods
59// ---------------------------------------------------------------------------
60void
62 oc.addOptionSubTopic("FCD Device");
63 insertDefaultAssignmentOptions("fcd", "FCD Device", oc);
64
65 oc.doRegister("device.fcd.begin", new Option_String("-1"));
66 oc.addDescription("device.fcd.begin", "FCD Device", "Recording begin time for FCD-data");
67
68 oc.doRegister("device.fcd.period", new Option_String("0"));
69 oc.addDescription("device.fcd.period", "FCD Device", "Recording period for FCD-data");
70
71 oc.doRegister("device.fcd.radius", new Option_Float(0));
72 oc.addDescription("device.fcd.radius", "FCD Device", "Record objects in a radius around equipped vehicles");
73}
74
75
76void
77MSDevice_FCD::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
79 if (equippedByDefaultAssignmentOptions(oc, "fcd", v, oc.isSet("fcd-output"))) {
80 MSDevice_FCD* device = new MSDevice_FCD(v, "fcd_" + v.getID());
81 into.push_back(device);
82 initOnce();
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
96}
97
98bool
100 // lazily build the shape filter in the case where route file is loaded as an additional file
103 }
104 const MSVehicle* msVeh = dynamic_cast<const MSVehicle*>(veh);
105 for (auto shape : myShape4Filters) {
106 if (shape.around(veh->getPosition()) || ((msVeh != nullptr) && shape.around(msVeh->getBackPosition()))) {
107 return true;
108 }
109 }
110 return false;
111}
112
113
114void
117 if (oc.isSet("fcd-output.filter-shapes")) {
118 const ShapeContainer& loadedShapes = MSNet::getInstance()->getShapeContainer();
119 if (loadedShapes.getPolygons().size() > 0) {
120 for (std::string attrName : oc.getStringVector("fcd-output.filter-shapes")) {
121 if (loadedShapes.getPolygons().get(attrName) == 0) {
122 WRITE_ERROR("Specified shape '" + attrName + "' for filtering fcd-output could not be found.");
123 } else {
124 // store the PositionVector, not reference, as traci can manipulate / detete the polygons
125 myShape4Filters.push_back(loadedShapes.getPolygons().get(attrName)->getShape());
126 }
127 }
129 }
130 } else {
132 }
133}
134
135
136void
139 return;
140 }
143 if (oc.isSet("fcd-output.filter-edges.input-file")) {
144 const std::string file = oc.getString("fcd-output.filter-edges.input-file");
145 std::ifstream strm(file.c_str());
146 if (!strm.good()) {
147 throw ProcessError("Could not load names of edges for filtering fcd-output from '" + file + "'.");
148 }
149 while (strm.good()) {
150 std::string name;
151 strm >> name;
152 // maybe we're loading an edge-selection
153 if (StringUtils::startsWith(name, "edge:")) {
154 name = name.substr(5);
155 }
156 myEdgeFilter.insert(MSEdge::dictionary(name));
157 }
158 }
159 if (oc.isSet("fcd-output.attributes")) {
161 for (std::string attrName : oc.getStringVector("fcd-output.attributes")) {
162 if (!SUMOXMLDefinitions::Attrs.hasString(attrName)) {
163 if (attrName == "all") {
164 myWrittenAttributes = std::numeric_limits<long long int>::max() - 1;
165 } else {
166 WRITE_ERROR("Unknown attribute '" + attrName + "' to write in fcd output.");
167 }
168 continue;
169 }
170 int attr = SUMOXMLDefinitions::Attrs.get(attrName);
171 assert(attr < 63);
172 myWrittenAttributes |= ((long long int)1 << attr);
173 }
174 }
175
176 if (oc.isSet("fcd-output.filter-shapes")) {
177 // build the shape filter if it is desired
180 }
181 //std::cout << "mask=" << myWrittenAttributes << " binary=" << std::bitset<64>(myWrittenAttributes) << "\n";
182}
183
184
185void
187 myEdgeFilter.clear();
188 myShape4Filters.clear();
191 myShapeFilterDesired = false;
193}
194
195
196/****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
@ SUMO_ATTR_POSITION_LAT
@ SUMO_ATTR_ODOMETER
@ SUMO_ATTR_VEHICLE
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:502
Abstract in-vehicle device.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
const Position getBackPosition() const
Definition: MSVehicle.cpp:1456
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.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:76
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 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: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.