SUMO - Simulation of Urban MObility
MSPModel_NonInteracting.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // The pedestrian following model (prototype)
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2014-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <cmath>
31 #include <algorithm>
33 #include <utils/geom/GeomHelper.h>
35 #include <microsim/MSNet.h>
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSJunction.h>
41 
42 
43 // ===========================================================================
44 // DEBUGGING HELPERS
45 // ===========================================================================
46 //
47 #define DEBUG1 "disabled"
48 #define DEBUG2 "disabled"
49 #define DEBUGCOND(PEDID) (PEDID == DEBUG1 || PEDID == DEBUG2)
50 
51 // ===========================================================================
52 // named (internal) constants
53 // ===========================================================================
54 
55 
56 // ===========================================================================
57 // static members
58 // ===========================================================================
59 
60 
61 // ===========================================================================
62 // MSPModel_NonInteracting method definitions
63 // ===========================================================================
64 
66  myNet(net) {
67  assert(myNet != 0);
68  UNUSED_PARAMETER(oc);
69 }
70 
71 
73 }
74 
75 
78  MoveToNextEdge* cmd = new MoveToNextEdge(person, *stage);
79  PState* state = new PState(cmd);
80  const SUMOTime firstEdgeDuration = state->computeWalkingTime(0, *stage, now);
81  myNet->getBeginOfTimestepEvents()->addEvent(cmd, now + firstEdgeDuration);
82 
83  //if DEBUGCOND(person->getID()) std::cout << SIMTIME << " " << person->getID() << " inserted on " << stage->getEdge()->getID() << "\n";
84  return state;
85 }
86 
87 
88 void
90  dynamic_cast<PState*>(state)->getCommand()->abortWalk();
91 }
92 
93 
96  if (myPerson == 0) {
97  return 0; // descheduled
98  }
99  PState* state = dynamic_cast<PState*>(myParent.getPedestrianState());
100  const MSEdge* old = myParent.getEdge();
101  const bool arrived = myParent.moveToNextEdge(myPerson, currentTime);
102  if (arrived) {
103  // walk finished. clean up state
104  delete state;
105  //if DEBUGCOND(myPerson->getID()) std::cout << SIMTIME << " " << myPerson->getID() << " arrived on " << old->getID() << "\n";
106  return 0;
107  } else {
108  //if DEBUGCOND(myPerson->getID()) std::cout << SIMTIME << " " << myPerson->getID() << " moves to " << myParent.getEdge()->getID() << "\n";
109  return state->computeWalkingTime(old, myParent, currentTime);
110  }
111 }
112 
113 
114 SUMOTime
116  myLastEntryTime = currentTime;
117  const MSEdge* edge = stage.getEdge();
118  const MSEdge* next = stage.getNextRouteEdge();
119  int dir = UNDEFINED_DIRECTION;
120  if (prev == 0) {
121  myCurrentBeginPos = stage.getDepartPos();
122  } else {
123  // default to FORWARD if not connected
124  dir = (edge->getToJunction() == prev->getToJunction() || edge->getToJunction() == prev->getFromJunction()) ? BACKWARD : FORWARD;
125  myCurrentBeginPos = dir == FORWARD ? 0 : edge->getLength();
126  }
127  if (next == 0) {
128  myCurrentEndPos = stage.getArrivalPos();
129  } else {
130  if (dir == UNDEFINED_DIRECTION) {
131  // default to FORWARD if not connected
132  dir = (edge->getFromJunction() == next->getFromJunction() || edge->getFromJunction() == next->getToJunction()) ? BACKWARD : FORWARD;
133  }
134  myCurrentEndPos = dir == FORWARD ? edge->getLength() : 0;
135  }
136  // ensure that a result > 0 is returned even if the walk ends immediately
137  myCurrentDuration = MAX2((SUMOTime)1, TIME2STEPS(fabs(myCurrentEndPos - myCurrentBeginPos) / stage.getMaxSpeed()));
138  //std::cout << SIMTIME << " dir=" << dir << " curBeg=" << myCurrentBeginPos << " curEnd=" << myCurrentEndPos << " speed=" << stage.getMaxSpeed() << " dur=" << myCurrentDuration << "\n";
139  return myCurrentDuration;
140 }
141 
142 
143 double
145  //std::cout << SIMTIME << " pos=" << (myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime)) << "\n";
146  return myCurrentBeginPos + (myCurrentEndPos - myCurrentBeginPos) / myCurrentDuration * (now - myLastEntryTime);
147 }
148 
149 
150 Position
152  const MSLane* lane = getSidewalk<MSEdge, MSLane>(stage.getEdge());
153  if (lane == 0) {
154  //std::string error = "Pedestrian '" + myCommand->myPerson->getID() + "' could not find sidewalk on edge '" + state.getEdge()->getID() + "', time="
155  // + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".";
156  //if (!OptionsCont::getOptions().getBool("ignore-route-errors")) {
157  // throw ProcessError(error);
158  //}
159  lane = stage.getEdge()->getLanes().front();
160  }
161  const double lateral_offset = (lane->allowsVehicleClass(SVC_PEDESTRIAN) ? 0 : SIDEWALK_OFFSET
162  * (MSNet::getInstance()->lefthand() ? -1 : 1));
163  return stage.getLanePosition(lane, getEdgePos(stage, now), lateral_offset);
164 }
165 
166 
167 double
169  //std::cout << SIMTIME << " rawAngle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) << " angle=" << stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? 180 : 0) << "\n";
170  double angle = stage.getEdgeAngle(stage.getEdge(), getEdgePos(stage, now)) + (myCurrentEndPos < myCurrentBeginPos ? M_PI : 0);
171  if (angle > M_PI) {
172  angle -= 2 * M_PI;
173  }
174  return angle;
175 }
176 
177 
178 SUMOTime
180  return 0;
181 }
182 
183 
184 double
186  return stage.getMaxSpeed();
187 }
188 
189 
190 const MSEdge*
192  return stage.getNextRouteEdge();
193 }
194 
195 /****************************************************************************/
const MSEdge * getNextEdge(const MSPerson::MSPersonStage_Walking &stage) const
return the list of internal edges if the pedestrian is on an intersection
double getLength() const
return the length of the edge
Definition: MSEdge.h:570
double getDepartPos() const
Definition: MSPerson.h:152
bool lefthand() const
return whether the network was built for lefthand traffic
Definition: MSNet.h:688
const MSEdge * getNextRouteEdge() const
Definition: MSPerson.h:167
is a pedestrian
double getEdgeAngle(const MSEdge *e, double at) const
get angle of the edge at a certain position
double getArrivalPos() const
Definition: MSPerson.h:160
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:168
SUMOTime computeWalkingTime(const MSEdge *prev, const MSPerson::MSPersonStage_Walking &stage, SUMOTime currentTime)
compute walking time on edge and update state members
static const int FORWARD
Definition: MSPModel.h:106
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
T MAX2(T a, T b)
Definition: StdDefs.h:74
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:117
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
PedestrianState * add(MSPerson *person, MSPerson::MSPersonStage_Walking *stage, SUMOTime now)
register the given person as a pedestrian
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:40
void remove(PedestrianState *state)
remove the specified person from the pedestrian simulation
The simulated network and simulation perfomer.
Definition: MSNet.h:89
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSPerson.cpp:83
A road/street connecting two junctions.
Definition: MSEdge.h:81
static const int UNDEFINED_DIRECTION
Definition: MSPModel.h:111
bool allowsVehicleClass(SUMOVehicleClass vclass) const
Definition: MSLane.h:714
double getAngle(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the direction in which the person faces in degrees
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:46
SUMOTime getWaitingTime(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the time the person spent standing
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:398
MSNet * myNet
the net to which to issue moveToNextEdge commands
Position getPosition(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
return the network coordinate of the person
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
abstract base class for managing callbacks to retrieve various state information from the model ...
Definition: MSPModel.h:129
double getEdgePos(const MSPerson::MSPersonStage_Walking &stage, SUMOTime now) const
abstract methods inherited from PedestrianState
double getMaxSpeed() const
accessors to be used by MSPModel
Definition: MSPerson.h:149
#define M_PI
Definition: odrSpiral.cpp:40
A storage for options typed value containers)
Definition: OptionsCont.h:99
static const int BACKWARD
Definition: MSPModel.h:110
double getSpeed(const MSPerson::MSPersonStage_Walking &stage) const
return the current speed of the person
const MSJunction * getFromJunction() const
Definition: MSEdge.h:349
long long int SUMOTime
Definition: TraCIDefs.h:52
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
Position getLanePosition(const MSLane *lane, double at, double offset) const
get position on lane at length at with orthogonal offset
SUMOTime execute(SUMOTime currentTime)
Executes the command.
const MSJunction * getToJunction() const
Definition: MSEdge.h:353