SUMO - Simulation of Urban MObility
MSPushButton.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2010-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // The class for a PushButton
18 /****************************************************************************/
19 
20 #define SWARM_DEBUG
22 #include "MSPushButton.h"
23 #include "MSPhaseDefinition.h"
24 #include "../MSEdge.h"
25 #include "../MSLane.h"
26 #include "../MSVehicle.h"
28 
29 MSPushButton::MSPushButton(const MSEdge* edge, const MSEdge* crossingEdge) {
30  m_edge = edge;
31  m_crossingEdge = crossingEdge;
32 }
33 
36 }
37 
38 bool MSPushButton::anyActive(const std::vector<MSPushButton*>& pushButtons) {
39  for (std::vector<MSPushButton*>::const_iterator it = pushButtons.begin(); it != pushButtons.end(); ++it) {
40  if (it.operator * ()->isActivated()) {
41  return true;
42  }
43  }
44  return false;
45 }
46 
47 std::map<std::string, std::vector<std::string> > MSPedestrianPushButton::m_crossingEdgeMap;
49 
50 MSPedestrianPushButton::MSPedestrianPushButton(const MSEdge* walkingEdge, const MSEdge* crossingEdge)
51  : MSPushButton(walkingEdge, crossingEdge) {
52  assert(walkingEdge->isWalkingArea() || ((walkingEdge->getPermissions() & SVC_PEDESTRIAN) != 0));
53  assert(crossingEdge->isCrossing());
54 }
55 
58 }
59 
60 bool MSPedestrianPushButton::isActiveForEdge(const MSEdge* walkingEdge, const MSEdge* crossing) {
61  const std::set<MSTransportable*> persons = walkingEdge->getPersons();
62  if (persons.size() > 0) {
63  for (std::set<MSTransportable*>::const_iterator pIt = persons.begin(); pIt != persons.end(); ++pIt) {
64  const MSPerson* person = (MSPerson*)*pIt;
65  const MSEdge* nextEdge = person->getNextEdgePtr();
68  if (person->getWaitingSeconds() >= 1 && nextEdge && nextEdge->getID() == crossing->getID()) {
69  DBG(
70  std::ostringstream oss;
71  oss << "MSPedestrianPushButton::isActiveForEdge Pushbutton active for edge " << walkingEdge->getID() << " crossing " << crossing->getID()
72  << " for " << person->getID() << " wait " << person->getWaitingSeconds();
73  WRITE_MESSAGE(oss.str());
74  );
75  return true;
76  }
77  }
78  } else {
79  //No person currently on the edge. But there may be some vehicles of class pedestrian
80  for (std::vector<MSLane*>::const_iterator laneIt = walkingEdge->getLanes().begin();
81  laneIt != walkingEdge->getLanes().end(); ++laneIt) {
82  MSLane* lane = *laneIt;
83  MSLane::VehCont vehicles = lane->getVehiclesSecure();
84  for (MSLane::VehCont::const_iterator vehicleIt = vehicles.begin(); vehicleIt != vehicles.end(); ++vehicleIt) {
85  MSVehicle* vehicle = *vehicleIt;
86  if (vehicle->getVClass() == SVC_PEDESTRIAN) {
87  // It's a pedestrian
88  const MSEdge* nextEdge = vehicle->succEdge(1);
89  if (vehicle->getWaitingSeconds() >= 1 && nextEdge) {
90  // Next edge is not internal. Try to find if between the current vehicle edge and the next is the crossing.
91  // To do that check if between the successors (or predecessor) of crossing is the next edge and walking precedes (or ensue) it.
92  if ((std::find(crossing->getPredecessors().begin(), crossing->getPredecessors().end(), walkingEdge) != crossing->getPredecessors().end()
93  && std::find(crossing->getSuccessors().begin(), crossing->getSuccessors().end(), nextEdge) != crossing->getSuccessors().end())
94  || (std::find(crossing->getSuccessors().begin(), crossing->getSuccessors().end(), walkingEdge) != crossing->getSuccessors().end()
95  && std::find(crossing->getPredecessors().begin(), crossing->getPredecessors().end(), nextEdge) != crossing->getPredecessors().end())) {
96  DBG(
97  std::ostringstream oss;
98  oss << "MSPedestrianPushButton::isActiveForEdge Pushbutton active for edge " << walkingEdge->getID() << " crossing " << crossing->getID()
99  << " for " << vehicle->getID() << " wait " << vehicle->getWaitingSeconds(); WRITE_MESSAGE(oss.str()););
100  // Also release the vehicles here
101  lane->releaseVehicles();
102  return true;
103  }
104  }
105  }
106  }
107  lane->releaseVehicles();
108  }
109  }
110  DBG(
111  std::ostringstream oss;
112  oss << "MSPedestrianPushButton::isActiveForEdge Pushbutton not active for edge " << walkingEdge->getID() << " crossing " << crossing->getID()
113  << " num Persons " << persons.size();
114  WRITE_MESSAGE(oss.str());
115  );
116  return false;
117 }
118 
119 
121 void getWalking(const std::vector<MSEdge*>& edges, std::vector< MSEdge*>& walkingEdges) {
122  for (std::vector<MSEdge*>::const_iterator it = edges.begin(); it != edges.end(); ++it) {
123  MSEdge* edge = *it;
124  if (edge->isWalkingArea() || ((edge->getPermissions() & SVC_PEDESTRIAN) != 0)) {
125  walkingEdges.push_back(edge);
126  }
127  }
128 }
129 
131 const std::vector<MSEdge*> getWalkingAreas(const MSEdge* crossing) {
132  std::vector<MSEdge*> walkingEdges;
133  getWalking(crossing->getSuccessors(), walkingEdges);
134  getWalking(crossing->getPredecessors(), walkingEdges);
135  return walkingEdges;
136 
137 }
138 
140  const std::vector<MSEdge*> walkingList = getWalkingAreas(crossing);
141  for (std::vector<MSEdge*>::const_iterator wIt = walkingList.begin(); wIt != walkingList.end(); ++wIt) {
142  MSEdge* walking = *wIt;
143  if (isActiveForEdge(walking, crossing)) {
144  DBG(WRITE_MESSAGE("MSPedestrianPushButton::isActiveOnAnySideOfTheRoad crossing edge " + crossing->getID() + " walking edge" + walking->getID()););
145  return true;
146  }
147  }
148  return false;
149 }
150 
151 std::vector<MSPushButton*> MSPedestrianPushButton::loadPushButtons(const MSPhaseDefinition* phase) {
153  std::vector<MSPushButton*> pushButtons;
154  const std::vector<std::string> lanes = phase->getTargetLaneSet();
155 // Multiple lane can be of the same edge, so I avoid readding them
156  std::set<std::string> controlledEdges;
157  for (std::vector<std::string>::const_iterator lIt = lanes.begin(); lIt != lanes.end(); ++lIt) {
158  MSLane* lane = MSLane::dictionary(*lIt);
159  if (lane) {
160  MSEdge* laneEdge = &lane->getEdge();
161  if (controlledEdges.count(laneEdge->getID()) != 0) {
162  continue;
163  }
164  controlledEdges.insert(laneEdge->getID());
165  if (m_crossingEdgeMap.find(laneEdge->getID()) != m_crossingEdgeMap.end()) {
166  //For every crossing edge that crosses this edge
167  for (std::vector<std::string>::const_iterator cIt = m_crossingEdgeMap[laneEdge->getID()].begin();
168  cIt != m_crossingEdgeMap[laneEdge->getID()].end(); ++cIt) {
169  MSEdge* crossing = MSEdge::dictionary(*cIt);
170  const std::vector<MSEdge*> walkingList = getWalkingAreas(crossing);
171  for (std::vector<MSEdge*>::const_iterator wIt = walkingList.begin(); wIt != walkingList.end(); ++wIt) {
172  MSEdge* walking = *wIt;
173  DBG(WRITE_MESSAGE("MSPedestrianPushButton::loadPushButtons Added pushButton for walking edge " + walking->getID() + " crossing edge "
174  + crossing->getID() + " crossed edge " + laneEdge->getID() + ". Phase state " + phase->getState()););
175  pushButtons.push_back(new MSPedestrianPushButton(walking, crossing));
176  }
177  }
178  }
179  }
180  }
181  return pushButtons;
182 }
183 
187  for (MSEdgeVector::const_iterator eIt = MSEdge::getAllEdges().begin(); eIt != MSEdge::getAllEdges().end(); ++eIt) {
188  const MSEdge* edge = *eIt;
189  if (edge->isCrossing()) {
190  for (std::vector<std::string>::const_iterator cIt = edge->getCrossingEdges().begin();
191  cIt != edge->getCrossingEdges().end(); ++cIt) {
192  m_crossingEdgeMap[*cIt].push_back(edge->getID());
193  }
194  }
195  }
196  }
197 }
198 
const std::string & getState() const
Returns the state within this phase.
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:607
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
is a pedestrian
double getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
Definition: MSVehicle.h:660
static void loadCrossingEdgeMap()
static std::map< std::string, std::vector< std::string > > m_crossingEdgeMap
Definition: MSPushButton.h:100
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:343
const std::vector< MSEdge * > getWalkingAreas(const MSEdge *crossing)
Get the walking areas adjacent to the crossing.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:167
const std::vector< std::string > & getCrossingEdges() const
Gets the crossed edge ids.
Definition: MSEdge.h:294
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:392
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:744
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static bool m_crossingEdgeMapLoaded
Definition: MSPushButton.h:101
A road/street connecting two junctions.
Definition: MSEdge.h:80
const std::set< MSTransportable * > & getPersons() const
Returns this edge&#39;s persons set.
Definition: MSEdge.h:176
static bool isActiveOnAnySideOfTheRoad(const MSEdge *crossing)
Static method to check if the push button is active on both side of the road.
const MSEdge * m_edge
Definition: MSPushButton.h:55
const std::string & getID() const
returns the id of the transportable
const MSEdge * m_crossingEdge
Definition: MSPushButton.h:56
MSPedestrianPushButton(const MSEdge *walkingEdge, const MSEdge *crossingEdge)
#define DBG(X)
Definition: SwarmDebug.h:29
bool isCrossing() const
return whether this edge is a pedestrian crossing
Definition: MSEdge.h:234
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:89
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1639
SUMOVehicleClass getVClass() const
Returns the vehicle&#39;s access class.
static const MSEdgeVector & getAllEdges()
Returns all edges with a numerical id.
Definition: MSEdge.cpp:777
static std::vector< MSPushButton * > loadPushButtons(const MSPhaseDefinition *)
Loads all the pushbuttons for all the controlled lanes of a stage.
const MSEdgeVector & getSuccessors() const
Returns the following edges.
Definition: MSEdge.h:319
static bool isActiveForEdge(const MSEdge *walkingEdge, const MSEdge *crossing)
Static method with the same behavior of isActivated.
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
const MSEdge * getNextEdgePtr() const
returns the next edge ptr if this person is walking and the pedestrian model allows it ...
Definition: MSPerson.cpp:456
bool isWalkingArea() const
return whether this edge is walking area
Definition: MSEdge.h:248
virtual double getWaitingSeconds() const
the time this transportable spent waiting in seconds
void getWalking(const std::vector< MSEdge *> &edges, std::vector< MSEdge *> &walkingEdges)
Checks if any of the edges is a walking area.
const LaneIdVector & getTargetLaneSet() const
SVCPermissions getPermissions() const
Definition: MSEdge.h:540
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:200
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:419
bool isActivated() const
abstract methods inherited from PedestrianState
static bool anyActive(const std::vector< MSPushButton *> &)
Checks if any pushbutton in the vector is active.
const std::string & getID() const
Returns the name of the vehicle.
The definition of a single phase of a tls logic.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
virtual ~MSPushButton()
MSPushButton(const MSEdge *edge, const MSEdge *crossingEdge)