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 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 /* -------------------------------------------------------------------------
47 * static member definitions
48 * ----------------------------------------------------------------------- */
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 /* -------------------------------------------------------------------------
55  * MSTransportable::Stage - methods
56  * ----------------------------------------------------------------------- */
57 MSTransportable::Stage::Stage(const MSEdge& destination, MSStoppingPlace* toStop, const SUMOReal arrivalPos, StageType type)
58  : myDestination(destination), myDestinationStop(toStop), myArrivalPos(arrivalPos), myDeparted(-1), myArrived(-1), myType(type) {}
59 
61 
62 const MSEdge&
64  return myDestination;
65 }
66 
67 
68 void
70  if (myDeparted < 0) {
71  myDeparted = now;
72  }
73 }
74 
75 void
77  myArrived = now;
78 }
79 
80 bool
81 MSTransportable::Stage::isWaitingFor(const std::string& /*line*/) const {
82  return false;
83 }
84 
87  return getLanePosition(e->getLanes()[0], at, offset);
88 }
89 
92  return lane->getShape().positionAtOffset(lane->interpolateLanePosToGeometryPos(at), offset);
93 }
94 
97  return e->getLanes()[0]->getShape().rotationAtOffset(at);
98 }
99 
100 
101 /* -------------------------------------------------------------------------
102 * MSTransportable::Stage_Waiting - methods
103 * ----------------------------------------------------------------------- */
105  SUMOTime duration, SUMOTime until, SUMOReal pos, const std::string& actType,
106  const bool initial) :
107  MSTransportable::Stage(destination, 0, SUMOVehicleParameter::interpretEdgePos(
108  pos, destination.getLength(), SUMO_ATTR_DEPARTPOS, "stopping at " + destination.getID()),
109  initial ? WAITING_FOR_DEPART : WAITING),
110  myWaitingDuration(duration),
111  myWaitingUntil(until),
112  myActType(actType) {
113 }
114 
115 
117 
118 
119 const MSEdge*
121  return &myDestination;
122 }
123 
124 
125 const MSEdge*
127  return &myDestination;
128 }
129 
130 
131 SUMOReal
133  return myArrivalPos;
134 }
135 
136 
137 SUMOTime
139  return myWaitingUntil;
140 }
141 
142 
143 Position
146 }
147 
148 
149 SUMOReal
152 }
153 
154 
155 void
157  myWaitingStart = now;
158  const SUMOTime until = MAX3(now, now + myWaitingDuration, myWaitingUntil);
159  if (dynamic_cast<MSPerson*>(transportable) != 0) {
160  previous->getEdge()->addPerson(transportable);
161  net->getPersonControl().setWaitEnd(until, transportable);
162  } else {
163  previous->getEdge()->addContainer(transportable);
164  net->getContainerControl().setWaitEnd(until, transportable);
165  }
166 }
167 
168 
169 void
171  if (myType != WAITING_FOR_DEPART) {
172  os.openTag("stop").writeAttr("arrival", time2string(myArrived)).closeTag();
173  }
174 }
175 
176 
177 void
179  if (myType != WAITING_FOR_DEPART) {
181  if (myWaitingDuration >= 0) {
183  }
184  if (myWaitingUntil >= 0) {
186  }
187  os.closeTag();
188  }
189 }
190 
191 
192 void
194  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actstart " + myActType)
195  .writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
196 }
197 
198 
199 void
201  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actend " + myActType).writeAttr("agent", p.getID())
202  .writeAttr("link", getEdge()->getID()).closeTag();
203 }
204 
205 
206 SUMOTime
208  return now - myWaitingStart;
209 }
210 
211 
212 SUMOReal
214  return 0;
215 }
216 
217 
220  ConstMSEdgeVector result;
221  result.push_back(&getDestination());
222  return result;
223 }
224 
225 
226 
227 /* -------------------------------------------------------------------------
228 * MSTransportable::Stage_Driving - methods
229 * ----------------------------------------------------------------------- */
231  MSStoppingPlace* toStop, const SUMOReal arrivalPos, const std::vector<std::string>& lines)
232  : MSTransportable::Stage(destination, toStop, arrivalPos, DRIVING), myLines(lines.begin(), lines.end()),
233  myVehicle(0), myStopWaitPos(Position::INVALID) {}
234 
235 
237 
238 
239 const MSEdge*
241  if (myVehicle != 0) {
242  return &myVehicle->getLane()->getEdge();
243  }
244  return myWaitingEdge;
245 }
246 
247 
248 const MSEdge*
250  return myWaitingEdge;
251 }
252 
253 
254 SUMOReal
256  if (isWaiting4Vehicle()) {
257  return myWaitingPos;
258  }
259  // vehicle may already have passed the lane (check whether this is correct)
261 }
262 
263 
264 Position
266  if (isWaiting4Vehicle()) {
268  return myStopWaitPos;
269  }
271  }
272  return myVehicle->getPosition();
273 }
274 
275 
276 SUMOReal
278  if (!isWaiting4Vehicle()) {
279  MSVehicle* veh = dynamic_cast<MSVehicle*>(myVehicle);
280  if (veh != 0) {
281  return veh->getAngle();
282  } else {
283  return 0;
284  }
285  }
287 }
288 
289 
290 bool
291 MSTransportable::Stage_Driving::isWaitingFor(const std::string& line) const {
292  return myLines.count(line) > 0;
293 }
294 
295 
296 bool
298  return myVehicle == 0;
299 }
300 
301 
302 SUMOTime
304  return isWaiting4Vehicle() ? now - myWaitingSince : 0;
305 }
306 
307 
308 SUMOReal
310  return isWaiting4Vehicle() ? 0 : myVehicle->getSpeed();
311 }
312 
313 
316  ConstMSEdgeVector result;
317  result.push_back(getFromEdge());
318  result.push_back(&getDestination());
319  return result;
320 }
321 
322 void
324  if (myVehicle != 0) {
325  // jumping out of a moving vehicle!
326  dynamic_cast<MSVehicle*>(myVehicle)->removeTransportable(t);
327  }
328 }
329 
330 
331 
332 void
334  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
335 }
336 
337 
338 void
340  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
341 }
342 
343 
344 
345 /* -------------------------------------------------------------------------
346  * MSTransportable - methods
347  * ----------------------------------------------------------------------- */
349  : myParameter(pars), myVType(vtype), myPlan(plan) {
350  myStep = myPlan->begin();
351 }
352 
354  if (myPlan != 0) {
355  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
356  delete *i;
357  }
358  delete myPlan;
359  myPlan = 0;
360  }
361  delete myParameter;
362 }
363 
364 const std::string&
366  return myParameter->id;
367 }
368 
369 SUMOTime
371  return myParameter->depart;
372 }
373 
374 void
376  (*myStep)->setDeparted(now);
377 }
378 
379 SUMOReal
381  return (*myStep)->getEdgePos(MSNet::getInstance()->getCurrentTimeStep());
382 }
383 
384 Position
386  return (*myStep)->getPosition(MSNet::getInstance()->getCurrentTimeStep());
387 }
388 
389 SUMOReal
391  return (*myStep)->getAngle(MSNet::getInstance()->getCurrentTimeStep());
392 }
393 
394 SUMOReal
396  return STEPS2TIME((*myStep)->getWaitingTime(MSNet::getInstance()->getCurrentTimeStep()));
397 }
398 
399 SUMOReal
401  return (*myStep)->getSpeed();
402 }
403 
404 
405 int
407  return (int)(myPlan->end() - myStep);
408 }
409 
410 int
412  return (int)myPlan->size();
413 }
414 
415 void
417  // myStep is invalidated upon modifying myPlan
418  const int stepIndex = (int)(myStep - myPlan->begin());
419  myPlan->push_back(stage);
420  myStep = myPlan->begin() + stepIndex;
421 }
422 
423 
424 void
426  assert(myStep + next < myPlan->end());
427  assert(next >= 0);
428  if (next > 0) {
429  // myStep is invalidated upon modifying myPlan
430  int stepIndex = (int)(myStep - myPlan->begin());
431  delete *(myStep + next);
432  myPlan->erase(myStep + next);
433  myStep = myPlan->begin() + stepIndex;
434  } else {
435  if (myStep + 1 == myPlan->end()) {
436  // stay in the simulation until the start of simStep to allow appending new stages (at the correct position)
437  appendStage(new Stage_Waiting(*getEdge(), 0, 0, getEdgePos(), "last stage removed", false));
438  }
439  (*myStep)->abort(this);
440  proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep());
441  }
442 }
443 
444 
445 void
447  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
448  (*i)->setSpeed(speed);
449  }
450 }
451 
452 
453 void
455  if (myVType->amVehicleSpecific()) {
456  delete myVType;
457  }
458  myVType = type;
459 }
460 
461 /****************************************************************************/
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
virtual void proceed(MSNet *net, MSTransportable *transportable, SUMOTime now, Stage *previous)
proceeds to the next step
bool isWaitingFor(const std::string &line) const
Whether the person waits for a vehicle of the line specified.
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
virtual Position getPosition(const SUMOReal offset=0) const =0
Return current position (x/y, cartesian)
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:571
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
long long int SUMOTime
Definition: SUMOTime.h:43
bool amVehicleSpecific() const
Returns whether this type belongs to a single vehicle only (was modified)
std::string myActType
The type of activity.
SUMOReal getSpeed() const
the speed of the transportable
A lane area vehicles can halt at.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
#define M_PI
Definition: angles.h:37
Stage(const MSEdge &destination, MSStoppingPlace *toStop, const SUMOReal arrivalPos, StageType type)
constructor
virtual SUMOReal getWaitingSeconds() const
the time this transportable spent waiting in seconds
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:615
void abort(MSTransportable *)
abort this stage (TraCI)
SUMOReal getAngle(SUMOTime now) const
returns the angle of the transportable
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:60
void setWaitEnd(SUMOTime time, MSTransportable *transportable)
sets the arrival time for a waiting transportable
int getNumRemainingStages() const
Return the number of remaining stages (including the current)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
const std::set< std::string > myLines
the lines to choose from
ConstMSEdgeVector getEdges() const
the edges of the current stage
const std::string & getID() const
returns the id of the transportable
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
virtual bool proceed(MSNet *net, SUMOTime time)=0
virtual SUMOReal getEdgePos() const
Return the position on the edge.
SUMOReal getAngle(SUMOTime now) const
returns the angle of the transportable
MSTransportablePlan::iterator myStep
the iterator over the route
virtual SUMOReal getAngle() const
return the current angle of the transportable
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
MSTransportable(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
T MAX3(T a, T b, T c)
Definition: StdDefs.h:84
SUMOReal getEdgeAngle(const MSEdge *e, SUMOReal at) const
get angle of the edge at a certain position
The simulated network and simulation perfomer.
Definition: MSNet.h:94
The car-following model and parameter.
Definition: MSVehicleType.h:74
SUMOTime getDesiredDepart() const
Returns the desired departure time.
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:718
const SUMOVehicleParameter * myParameter
the plan of the transportable
SUMOTime getWaitingTime(SUMOTime now) const
the time this transportable spent waiting
void removeStage(int next)
removes the nth next stage
const MSEdge & getDestination() const
returns the destination edge
SUMOTime myWaitingSince
The time since which this person is waiting for a ride.
Stage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const SUMOReal arrivalPos, const std::vector< std::string > &lines)
constructor
const MSEdge * getFromEdge() const
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A road/street connecting two junctions.
Definition: MSEdge.h:80
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
Position getLanePosition(const MSLane *lane, SUMOReal at, SUMOReal offset) const
get position on lane at length at with orthogonal offset
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:580
Position getEdgePosition(const MSEdge *e, SUMOReal at, SUMOReal offset) const
get position on edge e at length at with orthogonal offset
SUMOTime myArrived
the time at which this stage ended
virtual SUMOReal getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
const MSEdge * getEdge() const
Returns the current edge.
SUMOTime myDeparted
the time at which this stage started
static const SUMOReal ROADSIDE_OFFSET
the offset for computing positions when standing at an edge
void setDeparted(SUMOTime now)
logs depart time of the current stage
virtual void addPerson(MSTransportable *p) const
Definition: MSEdge.h:603
virtual bool isWaitingFor(const std::string &line) const
Whether the transportable waits for a vehicle of the line specified.
SUMOReal getEdgePos(SUMOTime now) const
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:710
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
void appendStage(Stage *stage)
Appends the given stage to the current plan.
const MSVehicleType * myVType
This transportable&#39;s type. (mainly used for drawing related information Note sure if it is really nec...
const MSEdge * getEdge() const
Returns the current edge.
virtual SUMOReal getSpeed() const =0
Returns the vehicle&#39;s current speed.
ConstMSEdgeVector getEdges() const
the edges of the current stage
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
virtual ~MSTransportable()
destructor
SUMOReal myArrivalPos
the position at which we want to arrive
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
Position positionAtOffset(SUMOReal pos, SUMOReal lateralOffset=0) const
Returns the position at the given length.
T MIN2(T a, T b)
Definition: StdDefs.h:64
Stage_Waiting(const MSEdge &destination, SUMOTime duration, SUMOTime until, SUMOReal pos, const std::string &actType, const bool initial)
constructor
Position getPosition(SUMOTime now) const
returns the position of the transportable
virtual ~Stage_Driving()
destructor
SUMOTime myWaitingUntil
the time until the person is waiting
SUMOVehicle * myVehicle
The taken vehicle.
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
SUMOTime getWaitingTime(SUMOTime now) const
time spent waiting for a ride
SUMOReal getEdgePos(SUMOTime now) const
virtual SUMOReal getSpeed() const
the current speed of the transportable
StageType myType
The type of this stage.
Structure representing possible vehicle parameter.
virtual const MSEdge * getEdge() const =0
Returns the current edge.
SUMOReal getSpeed() const
the speed of the transportable
SUMOReal interpolateLanePosToGeometryPos(SUMOReal lanePos) const
Definition: MSLane.h:438
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
const MSEdge * getFromEdge() const
void setSpeed(SUMOReal speed)
sets the walking speed (ignored in other stages)
void replaceVehicleType(MSVehicleType *type)
replace myVType
virtual Position getPosition() const
Return the Network coordinate of the transportable.
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:427
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
void setDeparted(SUMOTime now)
logs end of the step
int getNumStages() const
Return the total number stages in this persons plan.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:208
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
MSTransportablePlan * myPlan
the plan of the transportable
virtual ~Stage_Waiting()
destructor
Position getPosition(SUMOTime now) const
returns the position of the transportable
virtual ~Stage()
destructor
const MSEdge & myDestination
the next edge to reach by getting transported
const MSEdge * getEdge() const
Returns the current edge.
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
std::string id
The vehicle&#39;s id.
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:278
SUMOReal getAngle() const
Returns the vehicle&#39;s direction in radians.
Definition: MSVehicle.h:594
SUMOTime myWaitingStart
the time the person is waiting