SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
145  return getEdgePosition(&myDestination, myArrivalPos, ROADSIDE_OFFSET);
146 }
147 
148 
149 SUMOReal
151  return getEdgeAngle(&myDestination, myArrivalPos) + M_PI / 2;
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) {
182  os.writeAttr(SUMO_ATTR_DURATION, time2string(myWaitingDuration));
183  }
184  if (myWaitingUntil >= 0) {
185  os.writeAttr(SUMO_ATTR_UNTIL, time2string(myWaitingUntil));
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)
252  return MIN2(myVehicle->getPositionOnLane(), getEdge()->getLength());
253 }
254 
255 
256 Position
258  if (isWaiting4Vehicle()) {
259  if (myStopWaitPos != Position::INVALID) {
260  return myStopWaitPos;
261  }
262  return getEdgePosition(myWaitingEdge, myWaitingPos, ROADSIDE_OFFSET);
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  }
278  return getEdgeAngle(myWaitingEdge, myWaitingPos) + M_PI / 2.;
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 /****************************************************************************/
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: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
SUMOReal getSpeed() const
the speed of the transportable
A lane area vehicles can halt at.
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:192
bool isWaiting4Vehicle() const
Whether the transportable waits for a vehicle.
#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:626
virtual const MSEdge * getEdge() const =0
Returns the current edge.
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:59
void setWaitEnd(SUMOTime time, MSTransportable *transportable)
sets the arrival time for a waiting transportable
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
const std::string & getID() const
returns the id of the transportable
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:254
MSTransportable(const SUMOVehicleParameter *pars, const MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
T MAX3(T a, T b, T c)
Definition: StdDefs.h:89
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:93
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:707
const SUMOVehicleParameter * myParameter
the plan of the transportable
SUMOTime getWaitingTime(SUMOTime now) const
the time this transportable spent waiting
const MSEdge & getDestination() const
returns the destination edge
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:591
Position getEdgePosition(const MSEdge *e, SUMOReal at, SUMOReal 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)
const MSEdge * getEdge() const
Returns the current edge.
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:614
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:699
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
const MSVehicleType * myVType
This transportable's type. (mainly used for drawing related information Note sure if it is really nec...
const MSEdge * getEdge() const
Returns the current edge.
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
virtual ~MSTransportable()
destructor
SUMOTime depart
The vehicle'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:69
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
virtual void endEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output (end of an action)
const MSEdge & getDestination() const
Returns the current destination.
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
Structure representing possible vehicle parameter.
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
virtual Position getPosition() const
Return the Network coordinate of the transportable.
const PositionVector & getShape() const
Returns this lane'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
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:214
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 * 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.
std::string id
The vehicle's id.
static const Position INVALID
Definition: Position.h:261
SUMOReal getAngle() const
Returns the vehicle's direction in radians.
Definition: MSVehicle.h:574