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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // The class for a PushButton
16 /****************************************************************************/
17 
18 #define SWARM_DEBUG
20 #include "MSPushButton.h"
21 #include "MSPhaseDefinition.h"
22 #include "../MSEdge.h"
23 #include "../MSLane.h"
24 #include "../MSVehicle.h"
26 
27 MSPushButton::MSPushButton(const MSEdge* edge, const MSEdge* crossingEdge) {
28  m_edge = edge;
29  m_crossingEdge = crossingEdge;
30 }
31 
34 }
35 
36 bool MSPushButton::anyActive(const std::vector<MSPushButton*>& pushButtons) {
37  for (std::vector<MSPushButton*>::const_iterator it = pushButtons.begin(); it != pushButtons.end(); ++it) {
38  if (it.operator * ()->isActivated()) {
39  return true;
40  }
41  }
42  return false;
43 }
44 
45 std::map<std::string, std::vector<std::string> > MSPedestrianPushButton::m_crossingEdgeMap;
47 
48 MSPedestrianPushButton::MSPedestrianPushButton(const MSEdge* walkingEdge, const MSEdge* crossingEdge)
49  : MSPushButton(walkingEdge, crossingEdge) {
50  assert(walkingEdge->isWalkingArea() || ((walkingEdge->getPermissions() & SVC_PEDESTRIAN) != 0));
51  assert(crossingEdge->isCrossing());
52 }
53 
56 }
57 
58 bool MSPedestrianPushButton::isActiveForEdge(const MSEdge* walkingEdge, const MSEdge* crossing) {
59  const std::set<MSTransportable*> persons = walkingEdge->getPersons();
60  if (persons.size() > 0) {
61  for (std::set<MSTransportable*>::const_iterator pIt = persons.begin(); pIt != persons.end(); ++pIt) {
62  const MSPerson* person = (MSPerson*)*pIt;
63  const MSEdge* nextEdge = person->getNextEdgePtr();
66  if (person->getWaitingSeconds() >= 1 && nextEdge && nextEdge->getID() == crossing->getID()) {
67  DBG(
68  std::ostringstream oss;
69  oss << "MSPedestrianPushButton::isActiveForEdge Pushbutton active for edge " << walkingEdge->getID() << " crossing " << crossing->getID()
70  << " for " << person->getID() << " wait " << person->getWaitingSeconds();
71  WRITE_MESSAGE(oss.str());
72  );
73  return true;
74  }
75  }
76  } else {
77  //No person currently on the edge. But there may be some vehicles of class pedestrian
78  for (std::vector<MSLane*>::const_iterator laneIt = walkingEdge->getLanes().begin();
79  laneIt != walkingEdge->getLanes().end(); ++laneIt) {
80  MSLane* lane = *laneIt;
81  MSLane::VehCont vehicles = lane->getVehiclesSecure();
82  for (MSLane::VehCont::const_iterator vehicleIt = vehicles.begin(); vehicleIt != vehicles.end(); ++vehicleIt) {
83  MSVehicle* vehicle = *vehicleIt;
84  if (vehicle->getVClass() == SVC_PEDESTRIAN) {
85  // It's a pedestrian
86  const MSEdge* nextEdge = vehicle->succEdge(1);
87  if (vehicle->getWaitingSeconds() >= 1 && nextEdge) {
88  // Next edge is not internal. Try to find if between the current vehicle edge and the next is the crossing.
89  // To do that check if between the successors (or predecessor) of crossing is the next edge and walking precedes (or ensue) it.
90  if ((std::find(crossing->getPredecessors().begin(), crossing->getPredecessors().end(), walkingEdge) != crossing->getPredecessors().end()
91  && std::find(crossing->getSuccessors().begin(), crossing->getSuccessors().end(), nextEdge) != crossing->getSuccessors().end())
92  || (std::find(crossing->getSuccessors().begin(), crossing->getSuccessors().end(), walkingEdge) != crossing->getSuccessors().end()
93  && std::find(crossing->getPredecessors().begin(), crossing->getPredecessors().end(), nextEdge) != crossing->getPredecessors().end())) {
94  DBG(
95  std::ostringstream oss;
96  oss << "MSPedestrianPushButton::isActiveForEdge Pushbutton active for edge " << walkingEdge->getID() << " crossing " << crossing->getID()
97  << " for " << vehicle->getID() << " wait " << vehicle->getWaitingSeconds(); WRITE_MESSAGE(oss.str()););
98  // Also release the vehicles here
99  lane->releaseVehicles();
100  return true;
101  }
102  }
103  }
104  }
105  lane->releaseVehicles();
106  }
107  }
108  DBG(
109  std::ostringstream oss;
110  oss << "MSPedestrianPushButton::isActiveForEdge Pushbutton not active for edge " << walkingEdge->getID() << " crossing " << crossing->getID()
111  << " num Persons " << persons.size();
112  WRITE_MESSAGE(oss.str());
113  );
114  return false;
115 }
116 
117 
119 void getWalking(const std::vector<MSEdge*>& edges, std::vector< MSEdge*>& walkingEdges) {
120  for (std::vector<MSEdge*>::const_iterator it = edges.begin(); it != edges.end(); ++it) {
121  MSEdge* edge = *it;
122  if (edge->isWalkingArea() || ((edge->getPermissions() & SVC_PEDESTRIAN) != 0)) {
123  walkingEdges.push_back(edge);
124  }
125  }
126 }
127 
129 const std::vector<MSEdge*> getWalkingAreas(const MSEdge* crossing) {
130  std::vector<MSEdge*> walkingEdges;
131  getWalking(crossing->getSuccessors(), walkingEdges);
132  getWalking(crossing->getPredecessors(), walkingEdges);
133  return walkingEdges;
134 
135 }
136 
138  const std::vector<MSEdge*> walkingList = getWalkingAreas(crossing);
139  for (std::vector<MSEdge*>::const_iterator wIt = walkingList.begin(); wIt != walkingList.end(); ++wIt) {
140  MSEdge* walking = *wIt;
141  if (isActiveForEdge(walking, crossing)) {
142  DBG(WRITE_MESSAGE("MSPedestrianPushButton::isActiveOnAnySideOfTheRoad crossing edge " + crossing->getID() + " walking edge" + walking->getID()););
143  return true;
144  }
145  }
146  return false;
147 }
148 
149 std::vector<MSPushButton*> MSPedestrianPushButton::loadPushButtons(const MSPhaseDefinition* phase) {
151  std::vector<MSPushButton*> pushButtons;
152  const std::vector<std::string> lanes = phase->getTargetLaneSet();
153 // Multiple lane can be of the same edge, so I avoid readding them
154  std::set<std::string> controlledEdges;
155  for (std::vector<std::string>::const_iterator lIt = lanes.begin(); lIt != lanes.end(); ++lIt) {
156  MSLane* lane = MSLane::dictionary(*lIt);
157  if (lane) {
158  MSEdge* laneEdge = &lane->getEdge();
159  if (controlledEdges.count(laneEdge->getID()) != 0) {
160  continue;
161  }
162  controlledEdges.insert(laneEdge->getID());
163  if (m_crossingEdgeMap.find(laneEdge->getID()) != m_crossingEdgeMap.end()) {
164  //For every crossing edge that crosses this edge
165  for (std::vector<std::string>::const_iterator cIt = m_crossingEdgeMap[laneEdge->getID()].begin();
166  cIt != m_crossingEdgeMap[laneEdge->getID()].end(); ++cIt) {
167  MSEdge* crossing = MSEdge::dictionary(*cIt);
168  const std::vector<MSEdge*> walkingList = getWalkingAreas(crossing);
169  for (std::vector<MSEdge*>::const_iterator wIt = walkingList.begin(); wIt != walkingList.end(); ++wIt) {
170  MSEdge* walking = *wIt;
171  DBG(WRITE_MESSAGE("MSPedestrianPushButton::loadPushButtons Added pushButton for walking edge " + walking->getID() + " crossing edge "
172  + crossing->getID() + " crossed edge " + laneEdge->getID() + ". Phase state " + phase->getState()););
173  pushButtons.push_back(new MSPedestrianPushButton(walking, crossing));
174  }
175  }
176  }
177  }
178  }
179  return pushButtons;
180 }
181 
185  for (MSEdgeVector::const_iterator eIt = MSEdge::getAllEdges().begin(); eIt != MSEdge::getAllEdges().end(); ++eIt) {
186  const MSEdge* edge = *eIt;
187  if (edge->isCrossing()) {
188  for (std::vector<std::string>::const_iterator cIt = edge->getCrossingEdges().begin();
189  cIt != edge->getCrossingEdges().end(); ++cIt) {
190  m_crossingEdgeMap[*cIt].push_back(edge->getID());
191  }
192  }
193  }
194  }
195 }
196 
const std::string & getState() const
Returns the state within this phase.
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:640
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:79
is a pedestrian
double getWaitingSeconds() const
Returns the number of seconds waited (speed was lesser than 0.1m/s)
Definition: MSVehicle.h:661
static void loadCrossingEdgeMap()
static std::map< std::string, std::vector< std::string > > m_crossingEdgeMap
Definition: MSPushButton.h:98
const MSEdgeVector & getPredecessors() const
Definition: MSEdge.h:338
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:162
const std::vector< std::string > & getCrossingEdges() const
Gets the crossed edge ids.
Definition: MSEdge.h:290
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:406
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:788
const std::string & getID() const
Returns the id.
Definition: Named.h:78
static bool m_crossingEdgeMapLoaded
Definition: MSPushButton.h:99
A road/street connecting two junctions.
Definition: MSEdge.h:75
const std::set< MSTransportable * > & getPersons() const
Returns this edge&#39;s persons set.
Definition: MSEdge.h:171
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:53
const std::string & getID() const
returns the id of the transportable
const MSEdge * m_crossingEdge
Definition: MSPushButton.h:54
MSPedestrianPushButton(const MSEdge *walkingEdge, const MSEdge *crossingEdge)
#define DBG(X)
Definition: SwarmDebug.h:27
bool isCrossing() const
return whether this edge is a pedestrian crossing
Definition: MSEdge.h:230
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:90
const MSEdgeVector & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Returns the following edges, restricted by vClass.
Definition: MSEdge.cpp:981
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1730
SUMOVehicleClass getVClass() const
Returns the vehicle&#39;s access class.
static const MSEdgeVector & getAllEdges()
Returns all edges with a numerical id.
Definition: MSEdge.cpp:821
static std::vector< MSPushButton * > loadPushButtons(const MSPhaseDefinition *)
Loads all the pushbuttons for all the controlled lanes of a stage.
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:624
bool isWalkingArea() const
return whether this edge is walking area
Definition: MSEdge.h:244
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:532
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:242
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:436
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:78
virtual ~MSPushButton()
MSPushButton(const MSEdge *edge, const MSEdge *crossingEdge)