Eclipse SUMO - Simulation of Urban MObility
MSPModel_NonInteracting.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2014-2019 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 /****************************************************************************/
14 // The pedestrian following model (prototype)
15 /****************************************************************************/
16 
17 // ===========================================================================
18 // included modules
19 // ===========================================================================
20 #include <config.h>
21 
22 #include <cmath>
23 #include <algorithm>
25 #include <utils/geom/GeomHelper.h>
28 #include <microsim/MSNet.h>
29 #include <microsim/MSEdge.h>
30 #include <microsim/MSLane.h>
31 #include <microsim/MSJunction.h>
34 
35 
36 // ===========================================================================
37 // DEBUGGING HELPERS
38 // ===========================================================================
39 //
40 #define DEBUG1 "disabled"
41 #define DEBUG2 "disabled"
42 #define DEBUGCOND(PEDID) (PEDID == DEBUG1 || PEDID == DEBUG2)
43 
44 
45 // ===========================================================================
46 // MSPModel_NonInteracting method definitions
47 // ===========================================================================
48 
50  myNet(net) {
51  assert(myNet != 0);
52  UNUSED_PARAMETER(oc);
53 }
54 
55 
57 }
58 
59 
62  MoveToNextEdge* cmd = new MoveToNextEdge(person, *stage);
63  PState* state = new PState(cmd);
64  const SUMOTime firstEdgeDuration = state->computeWalkingTime(nullptr, *stage, now);
65  myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + firstEdgeDuration);
66 
67  //if DEBUGCOND(person->getID()) std::cout << SIMTIME << " " << person->getID() << " inserted on " << stage->getEdge()->getID() << "\n";
68  return state;
69 }
70 
71 
72 void
74  dynamic_cast<PState*>(state)->getCommand()->abortWalk();
75 }
76 
77 
80  if (myPerson == nullptr) {
81  return 0; // descheduled
82  }
83  PState* state = dynamic_cast<PState*>(myParent.getPedestrianState());
84  const MSEdge* old = myParent.getEdge();
85  const bool arrived = myParent.moveToNextEdge(myPerson, currentTime);
86  if (arrived) {
87  // walk finished
88  //if DEBUGCOND(myPerson->getID()) std::cout << SIMTIME << " " << myPerson->getID() << " arrived on " << old->getID() << "\n";
89  return 0;
90  } else {
91  //if DEBUGCOND(myPerson->getID()) std::cout << SIMTIME << " " << myPerson->getID() << " moves to " << myParent.getEdge()->getID() << "\n";
92  return state->computeWalkingTime(old, myParent, currentTime);
93  }
94 }
95 
96 
99  myLastEntryTime = currentTime;
100  const MSEdge* edge = stage.getEdge();
101  const MSEdge* next = stage.getNextRouteEdge();
102  int dir = UNDEFINED_DIRECTION;
103  if (prev == nullptr) {
104  myCurrentBeginPos = stage.getDepartPos();
105  } else {
106  // default to FORWARD if not connected
107  dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
108  myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
109  }
110  if (next == nullptr) {
111  myCurrentEndPos = stage.getArrivalPos();
112  } else {
113  if (dir == UNDEFINED_DIRECTION) {
114  // default to FORWARD if not connected
115  dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
116  }
117  myCurrentEndPos = dir == FORWARD ? edge->getLength() : 0;
118  }
119  // ensure that a result > 0 is returned even if the walk ends immediately
120  // adding 0.5ms is done to ensure proper rounding
121  myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPos - myCurrentBeginPos) / stage.getMaxSpeed(myCommand->getPerson())));
122  //std::cout << std::setprecision(8) << SIMTIME << " curBeg=" << myCurrentBeginPos << " curEnd=" << myCurrentEndPos << " speed=" << stage.getMaxSpeed(myCommand->getPerson()) << " dur=" << myCurrentDuration << "\n";
123  // round to the next timestep to avoid systematic higher walking speed
124  if ((myCurrentDuration % DELTA_T) > 0) {
125  myCurrentDuration += DELTA_T;
126  }
127  return myCurrentDuration;
128 }
129 
130 
131 double
133  //std::cout << SIMTIME << " lastEntryTime=" << myLastEntryTime << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
134  return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime);
135 }
136 
137 
138 Position
140  const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
141  if (lane == nullptr) {
142  //std::string error = "Pedestrian '" + myCommand->myPerson->getID() + "' could not find sidewalk on edge '" + state.getEdge()->getID() + "', time="
143  // + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".";
144  //if (!OptionsCont::getOptions().getBool("ignore-route-errors")) {
145  // throw ProcessError(error);
146  //}
147  lane = stage.getEdge()->getLanes().front();
148  }
149  const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
150  * (MSNet::getInstance()->lefthand() ? -1 : 1));
151  return stage.getLanePosition(lane, getEdgePos(stage, now), lateral_offset);
152 }
153 
154 
155 double
157  //std::cout << SIMTIME << " rawAngle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) << " angle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 180 : 0) << "\n";
158  double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
159  if (angle > M_PI) {
160  angle -= 2 * M_PI;
161  }
162  return angle;
163 }
164 
165 
166 SUMOTime
168  return 0;
169 }
170 
171 
172 double
174  return stage.getMaxSpeed(myCommand->getPerson());
175 }
176 
177 
178 const MSEdge*
180  return stage.getNextRouteEdge();
181 }
182 
183 /****************************************************************************/
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
SVC_PEDESTRIAN
pedestrian
Definition: SUMOVehicleClass.h:156
MSPModel_NonInteracting::~MSPModel_NonInteracting
~MSPModel_NonInteracting()
Definition: MSPModel_NonInteracting.cpp:56
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
MSPModel_NonInteracting.h
MSPModel_NonInteracting::PState::getSpeed
double getSpeed(const MSPerson::MSPersonStage_Walking &stage) const
return the current speed of the person
Definition: MSPModel_NonInteracting.cpp:173
MSNet.h
MSPModel::BACKWARD
static const int BACKWARD
Definition: MSPModel.h:106
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
MSTransportable::Stage::getLanePosition
Position getLanePosition(const MSLane *lane, double at, double offset) const
get position on lane at length at with orthogonal offset
Definition: MSTransportable.cpp:128
OptionsCont.h
MSPModel_NonInteracting::PState::getPosition
Position getPosition(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the network coordinate of the person
Definition: MSPModel_NonInteracting.cpp:139
MSPerson::MSPersonStage_Walking::getDepartPos
double getDepartPos() const
Definition: MSPerson.h:152
MSPModel_NonInteracting::MoveToNextEdge::myPerson
MSPerson * myPerson
Definition: MSPModel_NonInteracting.h:82
MSNet::getBeginOfTimestepEvents
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:429
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:91
MSPerson
Definition: MSPerson.h:63
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
MSPModel_NonInteracting::PState::computeWalkingTime
SUMOTime computeWalkingTime(const MSEdge *prev, const MSPerson::MSPersonStage_Walking &stage, SUMOTime currentTime)
compute walking time on edge and update state members
Definition: MSPModel_NonInteracting.cpp:98
MSEdge.h
MSPModel_NonInteracting::MoveToNextEdge::myParent
MSPerson::MSPersonStage_Walking & myParent
Definition: MSPModel_NonInteracting.h:81
MSEdge::getLength
double getLength() const
return the length of the edge
Definition: MSEdge.h:589
MSPModel_NonInteracting::PState::getWaitingTime
SUMOTime getWaitingTime(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the time the person spent standing
Definition: MSPModel_NonInteracting.cpp:167
PedestrianState
abstract base class for managing callbacks to retrieve various state information from the model
Definition: MSPModel.h:137
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:79
MSPModel_NonInteracting::PState::getAngle
double getAngle(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the direction in which the person faces in degrees
Definition: MSPModel_NonInteracting.cpp:156
MSEdge::getFromJunction
const MSJunction * getFromJunction() const
Definition: MSEdge.h:359
MSPModel_NonInteracting::PState::getNextEdge
const MSEdge * getNextEdge(const MSPerson::MSPersonStage_Walking &stage) const
return the list of internal edges if the pedestrian is on an intersection
Definition: MSPModel_NonInteracting.cpp:179
MSPModel_NonInteracting::PState
abstract base class for managing callbacks to retrieve various state information from the model
Definition: MSPModel_NonInteracting.h:89
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
MSJunction.h
MSPModel_NonInteracting::MoveToNextEdge::execute
SUMOTime execute(SUMOTime currentTime)
Executes the command.
Definition: MSPModel_NonInteracting.cpp:79
MSNet::lefthand
bool lefthand() const
return whether the network was built for lefthand traffic
Definition: MSNet.h:663
MSPerson::MSPersonStage_Walking::getMaxSpeed
double getMaxSpeed(const MSTransportable *const person) const
accessors to be used by MSPModel
Definition: MSPerson.cpp:378
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
MSPModel_NonInteracting::remove
void remove(PedestrianState *state)
remove the specified person from the pedestrian simulation
Definition: MSPModel_NonInteracting.cpp:73
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
MSEdge::getToJunction
const MSJunction * getToJunction() const
Definition: MSEdge.h:363
MSPerson::MSPersonStage_Walking::getArrivalPos
double getArrivalPos() const
Definition: MSPerson.h:160
MSLane::allowsVehicleClass
bool allowsVehicleClass(SUMOVehicleClass vclass) const
Definition: MSLane.h:805
MSPModel::FORWARD
static const int FORWARD
Definition: MSPModel.h:102
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
M_PI
#define M_PI
Definition: odrSpiral.cpp:40
MSPerson::MSPersonStage_Walking::moveToNextEdge
bool moveToNextEdge(MSPerson *person, SUMOTime currentTime, MSEdge *nextInternal=nullptr)
move forward and return whether the person arrived
Definition: MSPerson.cpp:327
MSPModel::SIDEWALK_OFFSET
static const double SIDEWALK_OFFSET
the offset for computing person positions when walking on edges without a sidewalk
Definition: MSPModel.h:113
MSPModel_NonInteracting::PState::getEdgePos
double getEdgePos(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
abstract methods inherited from PedestrianState
Definition: MSPModel_NonInteracting.cpp:132
MSPModel::UNDEFINED_DIRECTION
static const int UNDEFINED_DIRECTION
Definition: MSPModel.h:107
MSTransportable::Stage::getEdgeAngle
double getEdgeAngle(const MSEdge *e, double at) const
get angle of the edge at a certain position
Definition: MSTransportable.cpp:133
MSPerson::MSPersonStage_Walking
Definition: MSPerson.h:70
MSPModel_NonInteracting::MoveToNextEdge
Definition: MSPModel_NonInteracting.h:68
MSEdge::getLanes
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:167
MSPModel_NonInteracting::MSPModel_NonInteracting
MSPModel_NonInteracting(const OptionsCont &oc, MSNet *net)
Constructor (it should not be necessary to construct more than one instance)
Definition: MSPModel_NonInteracting.cpp:49
config.h
GeomHelper.h
RandHelper.h
MSEventControl.h
MSLane.h
MSPModel_NonInteracting::myNet
MSNet * myNet
the net to which to issue moveToNextEdge commands
Definition: MSPModel_NonInteracting.h:120
MSPerson::MSPersonStage_Walking::getEdge
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSPerson.cpp:91
MSPerson::MSPersonStage_Walking::getNextRouteEdge
const MSEdge * getNextRouteEdge() const
Definition: MSPerson.h:171
MSPModel_NonInteracting::add
PedestrianState * add(MSPerson *person, MSPerson::MSPersonStage_Walking *stage, SUMOTime now)
register the given person as a pedestrian
Definition: MSPModel_NonInteracting.cpp:61
IntermodalNetwork.h
MSPerson::MSPersonStage_Walking::getPedestrianState
PedestrianState * getPedestrianState() const
Definition: MSPerson.h:178