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-2016 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 
218 
219 /* -------------------------------------------------------------------------
220 * MSTransportable::Stage_Driving - methods
221 * ----------------------------------------------------------------------- */
223  MSStoppingPlace* toStop, const SUMOReal arrivalPos, const std::vector<std::string>& lines)
224  : MSTransportable::Stage(destination, toStop, arrivalPos, DRIVING), myLines(lines.begin(), lines.end()),
225  myVehicle(0), myStopWaitPos(Position::INVALID) {}
226 
227 
229 
230 
231 const MSEdge*
233  if (myVehicle != 0) {
234  return myVehicle->getEdge();
235  }
236  return myWaitingEdge;
237 }
238 
239 
240 const MSEdge*
242  return myWaitingEdge;
243 }
244 
245 
246 SUMOReal
248  if (isWaiting4Vehicle()) {
249  return myWaitingPos;
250  }
251  // vehicle may already have passed the lane (check whether this is correct)
253 }
254 
255 
256 Position
258  if (isWaiting4Vehicle()) {
260  return myStopWaitPos;
261  }
263  }
264  return myVehicle->getPosition();
265 }
266 
267 
268 SUMOReal
270  if (!isWaiting4Vehicle()) {
271  MSVehicle* veh = dynamic_cast<MSVehicle*>(myVehicle);
272  if (veh != 0) {
273  return veh->getAngle();
274  } else {
275  return 0;
276  }
277  }
279 }
280 
281 
282 bool
283 MSTransportable::Stage_Driving::isWaitingFor(const std::string& line) const {
284  return myLines.count(line) > 0;
285 }
286 
287 
288 bool
290  return myVehicle == 0;
291 }
292 
293 
294 SUMOTime
296  return isWaiting4Vehicle() ? now - myWaitingSince : 0;
297 }
298 
299 
300 SUMOReal
302  return isWaiting4Vehicle() ? 0 : myVehicle->getSpeed();
303 }
304 
305 
306 void
308  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
309 }
310 
311 
312 void
314  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
315 }
316 
317 
318 
319 /* -------------------------------------------------------------------------
320  * MSTransportable - methods
321  * ----------------------------------------------------------------------- */
323  : myParameter(pars), myVType(vtype), myPlan(plan) {
324  myStep = myPlan->begin();
325 }
326 
328  if (myPlan != 0) {
329  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
330  delete *i;
331  }
332  delete myPlan;
333  myPlan = 0;
334  }
335  delete myParameter;
336 }
337 
338 const std::string&
340  return myParameter->id;
341 }
342 
343 SUMOTime
345  return myParameter->depart;
346 }
347 
348 void
350  (*myStep)->setDeparted(now);
351 }
352 
353 SUMOReal
355  return (*myStep)->getEdgePos(MSNet::getInstance()->getCurrentTimeStep());
356 }
357 
358 Position
360  return (*myStep)->getPosition(MSNet::getInstance()->getCurrentTimeStep());
361 }
362 
363 SUMOReal
365  return (*myStep)->getAngle(MSNet::getInstance()->getCurrentTimeStep());
366 }
367 
368 SUMOReal
370  return STEPS2TIME((*myStep)->getWaitingTime(MSNet::getInstance()->getCurrentTimeStep()));
371 }
372 
373 SUMOReal
375  return (*myStep)->getSpeed();
376 }
377 
378 
379 /****************************************************************************/
SUMOReal getSpeed() const
the speed of the transportable
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:257
void setArrived(SUMOTime now)
logs end of the step
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:82
long long int SUMOTime
Definition: SUMOTime.h:43
std::string myActType
The type of activity.
SUMOReal getEdgeAngle(const MSEdge *e, SUMOReal at) const
get angle of the edge at a certain position
A lane area vehicles can halt at.
#define M_PI
Definition: angles.h:37
Stage(const MSEdge &destination, MSStoppingPlace *toStop, const SUMOReal arrivalPos, StageType type)
constructor
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.
SUMOReal getAngle(SUMOTime now) const
returns the angle of the transportable
virtual SUMOReal getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
const MSEdge * getEdge() const
Returns the current edge.
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
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:59
void setWaitEnd(SUMOTime time, MSTransportable *transportable)
sets the arrival time for a waiting transportable
const MSEdge * getFromEdge() const
Position positionAtOffset(SUMOReal pos, SUMOReal lateralOffset=0) const
Returns the position at the given length.
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:159
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:427
const std::set< std::string > myLines
the lines to choose from
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 std::string & getID() const
Returns the id.
Definition: Named.h:66
SUMOReal getSpeed() const
the speed of the transportable
MSTransportablePlan::iterator myStep
the iterator over the route
MSTransportable(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
const MSEdge * getFromEdge() const
T MAX3(T a, T b, T c)
Definition: StdDefs.h:89
const MSEdge * getEdge() const
Returns the current edge.
Position getLanePosition(const MSLane *lane, SUMOReal at, SUMOReal offset) const
get position on lane at length at with orthogonal offset
SUMOReal getEdgePos(SUMOTime now) const
The simulated network and simulation perfomer.
Definition: MSNet.h:93
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:707
const SUMOVehicleParameter * myParameter
the plan of the transportable
virtual SUMOReal getAngle() const
return the current angle of the transportable
bool isWaiting4Vehicle() const
Whether the person waits for a vehicle.
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
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
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 MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:699
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOTime getDesiredDepart() const
Returns the desired departure time.
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 void addPerson(MSTransportable *p) const
Definition: MSEdge.h:614
virtual ~MSTransportable()
destructor
SUMOReal myArrivalPos
the position at which we want to arrive
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
virtual SUMOReal getWaitingSeconds() const
the time this transportable spent waiting in seconds
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
T MIN2(T a, T b)
Definition: StdDefs.h:69
Stage_Waiting(const MSEdge &destination, SUMOTime duration, SUMOTime until, SUMOReal pos, const std::string &actType, const bool initial)
constructor
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.
virtual SUMOReal getEdgePos() const
Return the position on the edge.
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)
Position getEdgePosition(const MSEdge *e, SUMOReal at, SUMOReal offset) const
get position on edge e at length at with orthogonal offset
SUMOReal getEdgePos(SUMOTime now) const
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
SUMOReal getAngle(SUMOTime now) const
returns the angle of the transportable
StageType myType
The type of this stage.
virtual SUMOReal getSpeed() const =0
Returns the vehicle&#39;s current speed.
Structure representing possible vehicle parameter.
SUMOReal getAngle() const
Returns the vehicle&#39;s direction in radians.
Definition: MSVehicle.h:574
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:626
virtual Position getPosition() const
Return the Network coordinate of the transportable.
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:591
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
virtual Position getPosition(const SUMOReal 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
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.
#define SUMOReal
Definition: config.h:213
virtual void tripInfoOutput(OutputDevice &os) const
Called on writing tripinfo output.
MSTransportablePlan * myPlan
the plan of the transportable
virtual ~Stage_Waiting()
destructor
virtual SUMOReal getSpeed() const
the current speed of the transportable
virtual ~Stage()
destructor
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
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
Definition: Position.h:261
SUMOTime myWaitingStart
the time the person is waiting