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