SUMO - Simulation of Urban MObility
MSTransportable.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The common superclass for modelling transportable objects like persons and containers
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <utils/geom/GeomHelper.h>
35 #include "MSEdge.h"
36 #include "MSLane.h"
37 #include "MSNet.h"
39 #include "MSTransportableControl.h"
40 #include "MSTransportable.h"
41 
42 /* -------------------------------------------------------------------------
43 * static member definitions
44 * ----------------------------------------------------------------------- */
45 const double MSTransportable::ROADSIDE_OFFSET(3);
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 /* -------------------------------------------------------------------------
51  * MSTransportable::Stage - methods
52  * ----------------------------------------------------------------------- */
53 MSTransportable::Stage::Stage(const MSEdge& destination, MSStoppingPlace* toStop, const double arrivalPos, StageType type)
54  : myDestination(destination), myDestinationStop(toStop), myArrivalPos(arrivalPos), myDeparted(-1), myArrived(-1), myType(type) {}
55 
57 
58 const MSEdge&
60  return myDestination;
61 }
62 
63 
64 void
66  if (myDeparted < 0) {
67  myDeparted = now;
68  }
69 }
70 
71 void
73  myArrived = now;
74 }
75 
76 bool
77 MSTransportable::Stage::isWaitingFor(const std::string& /*line*/) const {
78  return false;
79 }
80 
82 MSTransportable::Stage::getEdgePosition(const MSEdge* e, double at, double offset) const {
83  return getLanePosition(e->getLanes()[0], at, offset);
84 }
85 
87 MSTransportable::Stage::getLanePosition(const MSLane* lane, double at, double offset) const {
88  return lane->getShape().positionAtOffset(lane->interpolateLanePosToGeometryPos(at), offset);
89 }
90 
91 double
92 MSTransportable::Stage::getEdgeAngle(const MSEdge* e, double at) const {
93  return e->getLanes()[0]->getShape().rotationAtOffset(at);
94 }
95 
96 
97 /* -------------------------------------------------------------------------
98 * MSTransportable::Stage_Waiting - methods
99 * ----------------------------------------------------------------------- */
101  SUMOTime duration, SUMOTime until, double pos, const std::string& actType,
102  const bool initial) :
103  MSTransportable::Stage(destination, 0, SUMOVehicleParameter::interpretEdgePos(
104  pos, destination.getLength(), SUMO_ATTR_DEPARTPOS, "stopping at " + destination.getID()),
105  initial ? WAITING_FOR_DEPART : WAITING),
106  myWaitingDuration(duration),
107  myWaitingUntil(until),
108  myActType(actType) {
109 }
110 
111 
113 
114 
115 const MSEdge*
117  return &myDestination;
118 }
119 
120 
121 const MSEdge*
123  return &myDestination;
124 }
125 
126 
127 double
129  return myArrivalPos;
130 }
131 
132 
133 SUMOTime
135  return myWaitingUntil;
136 }
137 
138 
139 Position
142 }
143 
144 
145 double
148 }
149 
150 
151 void
153  myWaitingStart = now;
154  const SUMOTime until = MAX3(now, now + myWaitingDuration, myWaitingUntil);
155  if (dynamic_cast<MSPerson*>(transportable) != 0) {
156  previous->getEdge()->addPerson(transportable);
157  net->getPersonControl().setWaitEnd(until, transportable);
158  } else {
159  previous->getEdge()->addContainer(transportable);
160  net->getContainerControl().setWaitEnd(until, transportable);
161  }
162 }
163 
164 
165 void
167  if (myType != WAITING_FOR_DEPART) {
168  os.openTag("stop").writeAttr("arrival", time2string(myArrived)).closeTag();
169  }
170 }
171 
172 
173 void
175  if (myType != WAITING_FOR_DEPART) {
177  if (myWaitingDuration >= 0) {
179  }
180  if (myWaitingUntil >= 0) {
182  }
183  os.closeTag();
184  }
185 }
186 
187 
188 void
190  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actstart " + myActType)
191  .writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
192 }
193 
194 
195 void
197  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actend " + myActType).writeAttr("agent", p.getID())
198  .writeAttr("link", getEdge()->getID()).closeTag();
199 }
200 
201 
202 SUMOTime
204  return now - myWaitingStart;
205 }
206 
207 
208 double
210  return 0;
211 }
212 
213 
216  ConstMSEdgeVector result;
217  result.push_back(&getDestination());
218  return result;
219 }
220 
221 
222 
223 /* -------------------------------------------------------------------------
224 * MSTransportable::Stage_Driving - methods
225 * ----------------------------------------------------------------------- */
227  MSStoppingPlace* toStop, const double arrivalPos, const std::vector<std::string>& lines)
228  : MSTransportable::Stage(destination, toStop, arrivalPos, DRIVING), myLines(lines.begin(), lines.end()),
229  myVehicle(0), myStopWaitPos(Position::INVALID) {}
230 
231 
233 
234 
235 const MSEdge*
237  if (myVehicle != 0) {
238  return &myVehicle->getLane()->getEdge();
239  }
240  return myWaitingEdge;
241 }
242 
243 
244 const MSEdge*
246  return myWaitingEdge;
247 }
248 
249 
250 double
252  if (isWaiting4Vehicle()) {
253  return myWaitingPos;
254  }
255  // vehicle may already have passed the lane (check whether this is correct)
257 }
258 
259 
260 Position
262  if (isWaiting4Vehicle()) {
264  return myStopWaitPos;
265  }
267  }
268  return myVehicle->getPosition();
269 }
270 
271 
272 double
274  if (!isWaiting4Vehicle()) {
275  MSVehicle* veh = dynamic_cast<MSVehicle*>(myVehicle);
276  if (veh != 0) {
277  return veh->getAngle();
278  } else {
279  return 0;
280  }
281  }
283 }
284 
285 
286 bool
287 MSTransportable::Stage_Driving::isWaitingFor(const std::string& line) const {
288  return myLines.count(line) > 0;
289 }
290 
291 
292 bool
294  return myVehicle == 0;
295 }
296 
297 
298 SUMOTime
300  return isWaiting4Vehicle() ? now - myWaitingSince : 0;
301 }
302 
303 
304 double
306  return isWaiting4Vehicle() ? 0 : myVehicle->getSpeed();
307 }
308 
309 
312  ConstMSEdgeVector result;
313  result.push_back(getFromEdge());
314  result.push_back(&getDestination());
315  return result;
316 }
317 
318 void
320  if (myVehicle != 0) {
321  // jumping out of a moving vehicle!
322  dynamic_cast<MSVehicle*>(myVehicle)->removeTransportable(t);
323  }
324 }
325 
326 
327 
328 void
330  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
331 }
332 
333 
334 void
336  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
337 }
338 
339 
340 
341 /* -------------------------------------------------------------------------
342  * MSTransportable - methods
343  * ----------------------------------------------------------------------- */
345  : myParameter(pars), myVType(vtype), myPlan(plan) {
346  myStep = myPlan->begin();
347 }
348 
350  if (myPlan != 0) {
351  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
352  delete *i;
353  }
354  delete myPlan;
355  myPlan = 0;
356  }
357  delete myParameter;
358 }
359 
360 const std::string&
362  return myParameter->id;
363 }
364 
365 SUMOTime
367  return myParameter->depart;
368 }
369 
370 void
372  (*myStep)->setDeparted(now);
373 }
374 
375 double
377  return (*myStep)->getEdgePos(MSNet::getInstance()->getCurrentTimeStep());
378 }
379 
380 Position
382  return (*myStep)->getPosition(MSNet::getInstance()->getCurrentTimeStep());
383 }
384 
385 double
387  return (*myStep)->getAngle(MSNet::getInstance()->getCurrentTimeStep());
388 }
389 
390 double
392  return STEPS2TIME((*myStep)->getWaitingTime(MSNet::getInstance()->getCurrentTimeStep()));
393 }
394 
395 double
397  return (*myStep)->getSpeed();
398 }
399 
400 
401 int
403  return (int)(myPlan->end() - myStep);
404 }
405 
406 int
408  return (int)myPlan->size();
409 }
410 
411 void
413  // myStep is invalidated upon modifying myPlan
414  const int stepIndex = (int)(myStep - myPlan->begin());
415  myPlan->push_back(stage);
416  myStep = myPlan->begin() + stepIndex;
417 }
418 
419 
420 void
422  assert(myStep + next < myPlan->end());
423  assert(next >= 0);
424  if (next > 0) {
425  // myStep is invalidated upon modifying myPlan
426  int stepIndex = (int)(myStep - myPlan->begin());
427  delete *(myStep + next);
428  myPlan->erase(myStep + next);
429  myStep = myPlan->begin() + stepIndex;
430  } else {
431  if (myStep + 1 == myPlan->end()) {
432  // stay in the simulation until the start of simStep to allow appending new stages (at the correct position)
433  appendStage(new Stage_Waiting(*getEdge(), 0, 0, getEdgePos(), "last stage removed", false));
434  }
435  (*myStep)->abort(this);
436  proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep());
437  }
438 }
439 
440 
441 void
443  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
444  (*i)->setSpeed(speed);
445  }
446 }
447 
448 
449 void
451  myVType = type;
452 }
453 
454 /****************************************************************************/
virtual void proceed(MSNet *net, MSTransportable *transportable, SUMOTime now, Stage *previous)
proceeds to the next step
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
void setArrived(SUMOTime now)
logs end of the step
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
std::string myActType
The type of activity.
double getAngle(SUMOTime now) const
returns the angle of the transportable
double getAngle() const
Returns the vehicle&#39;s direction in radians.
Definition: MSVehicle.h:594
const MSEdge * getEdge() const
Returns the current edge.
A lane area vehicles can halt at.
void setSpeed(double speed)
sets the walking speed (ignored in other stages)
virtual double getEdgePos() const
Return the position on the edge.
#define M_PI
Definition: angles.h:37
virtual bool isWaitingFor(const std::string &line) const
Whether the transportable waits for a vehicle of the line specified.
virtual const MSEdge * getEdge() const =0
Returns the current edge.
void abort(MSTransportable *)
abort this stage (TraCI)
const MSEdge * getEdge() const
Returns the current edge.
#define INVALID
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
void setWaitEnd(SUMOTime time, MSTransportable *transportable)
sets the arrival time for a waiting transportable
const MSEdge * getFromEdge() const
ConstMSEdgeVector getEdges() const
the edges of the current stage
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
Stage_Waiting(const MSEdge &destination, SUMOTime duration, SUMOTime until, double pos, const std::string &actType, const bool initial)
constructor
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:426
const std::set< std::string > myLines
the lines to choose from
double getSpeed() const
the speed of the transportable
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
virtual bool proceed(MSNet *net, SUMOTime time)=0
const std::string & getID() const
Returns the id.
Definition: Named.h:66
Position getLanePosition(const MSLane *lane, double at, double offset) const
get position on lane at length at with orthogonal offset
virtual double getSpeed() const
the current speed of the transportable
MSTransportablePlan::iterator myStep
the iterator over the route
double getEdgePos(SUMOTime now) const
double getLength() const
return the length of the edge
Definition: MSEdge.h:586
MSTransportable(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
const MSEdge * getFromEdge() const
int getNumRemainingStages() const
Return the number of remaining stages (including the current)
T MAX3(T a, T b, T c)
Definition: StdDefs.h:84
double getEdgePos(SUMOTime now) const
const MSEdge * getEdge() const
Returns the current edge.
The simulated network and simulation perfomer.
Definition: MSNet.h:94
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:738
const SUMOVehicleParameter * myParameter
the plan of the transportable
void removeStage(int next)
removes the nth next stage
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
SUMOTime myWaitingSince
The time since which this person is waiting for a ride.
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
SUMOTime myArrived
the time at which this stage ended
double getSpeed() const
the speed of the transportable
SUMOTime myDeparted
the time at which this stage started
void setDeparted(SUMOTime now)
logs depart time of the current stage
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:730
ConstMSEdgeVector getEdges() const
the edges of the current stage
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOTime getDesiredDepart() const
Returns the desired departure time.
void appendStage(Stage *stage)
Appends the given stage to the current plan.
Position getPosition(SUMOTime now) const
returns the position of the transportable
const MSVehicleType * myVType
This transportable&#39;s type. (mainly used for drawing related information Note sure if it is really nec...
virtual double getAngle() const
return the current angle of the transportable
virtual void addPerson(MSTransportable *p) const
Definition: MSEdge.h:612
virtual ~MSTransportable()
destructor
SUMOTime getWaitingTime(SUMOTime now) const
time spent waiting for a ride
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
T MIN2(T a, T b)
Definition: StdDefs.h:64
const std::string & getID() const
returns the id of the transportable
virtual ~Stage_Driving()
destructor
SUMOTime myWaitingUntil
the time until the person is waiting
SUMOVehicle * myVehicle
The taken vehicle.
Position getPosition(SUMOTime now) const
returns the position of the transportable
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
Stage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, const std::vector< std::string > &lines)
constructor
Position getEdgePosition(const MSEdge *e, double at, double offset) const
get position on edge e at length at with orthogonal offset
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
double getAngle(SUMOTime now) const
returns the angle of the transportable
StageType myType
The type of this stage.
Structure representing possible vehicle parameter.
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:624
virtual Position getPosition() const
Return the Network coordinate of the transportable.
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
double interpolateLanePosToGeometryPos(double lanePos) const
Definition: MSLane.h:442
static const double ROADSIDE_OFFSET
the offset for computing positions when standing at an edge
void replaceVehicleType(MSVehicleType *type)
replace myVType
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
SUMOTime getWaitingTime(SUMOTime now) const
the time this transportable spent waiting
const MSEdge & getDestination() const
returns the destination edge
double getEdgeAngle(const MSEdge *e, double at) const
get angle of the edge at a certain position
virtual double getWaitingSeconds() const
the time this transportable spent waiting in seconds
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
void setDeparted(SUMOTime now)
logs end of the step
bool closeTag()
Closes the most recently opened tag.
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
MSTransportablePlan * myPlan
the plan of the transportable
long long int SUMOTime
Definition: TraCIDefs.h:52
virtual ~Stage_Waiting()
destructor
virtual ~Stage()
destructor
Stage(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, StageType type)
constructor
bool isWaitingFor(const std::string &line) const
Whether the person waits for a vehicle of the line specified.
const MSEdge & myDestination
the next edge to reach by getting transported
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
SUMOTime myWaitingDuration
the time the person is waiting
int getNumStages() const
Return the total number stages in this persons plan.
std::string id
The vehicle&#39;s id.
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:278
double myArrivalPos
the position at which we want to arrive
SUMOTime myWaitingStart
the time the person is waiting