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