Skip to content

Commit

Permalink
refactoring refs #12 to enable larger attribute mask. refs #14447
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed Mar 1, 2024
1 parent 6f8e371 commit c5ca904
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
27 changes: 18 additions & 9 deletions src/microsim/devices/MSDevice_FCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@
#include <microsim/MSVehicle.h>
#include "MSDevice_FCD.h"

// some attributes are not written by default and must be enabled via option fcd-output.attributes
const long long int MSDevice_FCD::myDefaultMask(~(
((long long int)1 << SUMO_ATTR_VEHICLE) |
((long long int)1 << SUMO_ATTR_ODOMETER) |
((long long int)1 << SUMO_ATTR_SPEED_LAT) |
((long long int)1 << SUMO_ATTR_POSITION_LAT)
));

// ===========================================================================
// static members
Expand All @@ -50,7 +43,7 @@ std::vector<PositionVector> MSDevice_FCD::myShape4Filters;
bool MSDevice_FCD::myEdgeFilterInitialized(false);
bool MSDevice_FCD::myShapeFilterInitialized(false);
bool MSDevice_FCD::myShapeFilterDesired(false);
long long int MSDevice_FCD::myWrittenAttributes(myDefaultMask);
SumoXMLAttrMask MSDevice_FCD::myWrittenAttributes(getDefaultMask());

// ===========================================================================
// method definitions
Expand Down Expand Up @@ -96,6 +89,22 @@ MSDevice_FCD::MSDevice_FCD(SUMOVehicle& holder, const std::string& id) :
MSDevice_FCD::~MSDevice_FCD() {
}


SumoXMLAttrMask
MSDevice_FCD::getDefaultMask() {
SumoXMLAttrMask mask;
// set all bits to 1
mask.set();
// some attributes are not written by default and must be enabled via option fcd-output.attributes
// (or with an explicit attribute list)
mask.reset(SUMO_ATTR_VEHICLE);
mask.reset(SUMO_ATTR_ODOMETER);
mask.reset(SUMO_ATTR_SPEED_LAT);
mask.reset(SUMO_ATTR_POSITION_LAT);
return mask;
}


bool
MSDevice_FCD::shapeFilter(const SUMOTrafficObject* veh) {
// lazily build the shape filter in the case where route file is loaded as an additional file
Expand Down Expand Up @@ -190,7 +199,7 @@ MSDevice_FCD::cleanup() {
myEdgeFilterInitialized = false;
myShapeFilterInitialized = false;
myShapeFilterDesired = false;
myWrittenAttributes = myDefaultMask;
myWrittenAttributes = getDefaultMask();
}


Expand Down
6 changes: 3 additions & 3 deletions src/microsim/devices/MSDevice_FCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class MSDevice_FCD : public MSVehicleDevice {
return myEdgeFilter;
}

static long long int getWrittenAttributes() {
static SumoXMLAttrMask getWrittenAttributes() {
return myWrittenAttributes;
}

Expand Down Expand Up @@ -122,8 +122,8 @@ class MSDevice_FCD : public MSVehicleDevice {
static bool myShapeFilterDesired;

/// @brief bit mask for checking attributes to be written
static long long int myWrittenAttributes;
static const long long int myDefaultMask;
static SumoXMLAttrMask myWrittenAttributes;
static SumoXMLAttrMask getDefaultMask();

private:
/// @brief Invalidated copy constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/microsim/output/MSFCDExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ MSFCDExport::write(OutputDevice& of, SUMOTime timestep, bool elevation) {
if ((period > 0 && (timestep - begin) % period != 0) || timestep < begin) {
return;
}
const long long int mask = MSDevice_FCD::getWrittenAttributes();
const SumoXMLAttrMask mask = MSDevice_FCD::getWrittenAttributes();
const bool maskSet = oc.isSet("fcd-output.attributes");
const bool useGeo = oc.getBool("fcd-output.geo");
const bool signals = oc.getBool("fcd-output.signals") || (maskSet && of.useAttribute(SUMO_ATTR_SIGNALS, mask));
Expand Down Expand Up @@ -223,7 +223,7 @@ MSFCDExport::hasOwnOutput(const MSTransportable* p, bool filter, bool shapeFilte
void
MSFCDExport::writeTransportable(OutputDevice& of, const MSEdge* e, MSTransportable* p, const SUMOVehicle* v,
bool filter, bool shapeFilter, bool inRadius,
SumoXMLTag tag, bool useGeo, bool elevation, long long int mask) {
SumoXMLTag tag, bool useGeo, bool elevation, SumoXMLAttrMask mask) {
if (!hasOwnOutput(p, filter, shapeFilter, inRadius)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/microsim/output/MSFCDExport.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MSFCDExport {
/// @brief write transportable
static void writeTransportable(OutputDevice& of, const MSEdge* e, MSTransportable* p, const SUMOVehicle* v,
bool filter, bool shapeFilter, bool inRadius,
SumoXMLTag tag, bool useGeo, bool elevation, long long int mask);
SumoXMLTag tag, bool useGeo, bool elevation, SumoXMLAttrMask mask);

static bool isVisible(const SUMOVehicle* veh);
static bool hasOwnOutput(const SUMOVehicle* veh, bool filter, bool shapeFilter, bool isInRadius = false);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/iodevices/OutputDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ class OutputDevice {
return (attributeMask & ((long long int)1 << attr)) != 0;
}

inline bool useAttribute(const SumoXMLAttr attr, SumoXMLAttrMask attributeMask) const {
return attributeMask.test(attr);
}

/** @brief writes a named attribute unless filtered
*
* @param[in] attr The attribute (name)
Expand All @@ -275,6 +279,14 @@ class OutputDevice {
}
return *this;
}
template <typename T>
OutputDevice& writeOptionalAttr(const SumoXMLAttr attr, const T& val, SumoXMLAttrMask attributeMask) {
assert((int)attr <= (int)attributeMask.size());
if (attributeMask.none() || useAttribute(attr, attributeMask)) {
PlainXMLFormatter::writeAttr(getOStream(), attr, val);
}
return *this;
}


/** @brief writes an arbitrary attribute
Expand Down
2 changes: 2 additions & 0 deletions src/utils/xml/SUMOXMLDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
/****************************************************************************/
#pragma once
#include <config.h>
#include <bitset>

#include <utils/common/StringBijection.h>

Expand Down Expand Up @@ -534,6 +535,7 @@ enum SumoXMLTag {
/// @}
};

typedef std::bitset<96> SumoXMLAttrMask;

/**
* @enum SumoXMLAttr
Expand Down

0 comments on commit c5ca904

Please sign in to comment.