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 "MSVehicleControl.h"
40 #include "MSTransportableControl.h"
41 #include "MSTransportable.h"
42 
43 /* -------------------------------------------------------------------------
44 * static member definitions
45 * ----------------------------------------------------------------------- */
46 const double MSTransportable::ROADSIDE_OFFSET(3);
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 /* -------------------------------------------------------------------------
53  * MSTransportable::Stage - methods
54  * ----------------------------------------------------------------------- */
55 MSTransportable::Stage::Stage(const MSEdge& destination, MSStoppingPlace* toStop, const double arrivalPos, StageType type)
56  : myDestination(destination), myDestinationStop(toStop), myArrivalPos(arrivalPos), myDeparted(-1), myArrived(-1), myType(type) {}
57 
59 
60 const MSEdge&
62  return myDestination;
63 }
64 
65 
66 void
68  if (myDeparted < 0) {
69  myDeparted = now;
70  }
71 }
72 
73 void
75  myArrived = now;
76 }
77 
78 bool
79 MSTransportable::Stage::isWaitingFor(const std::string& /*line*/) const {
80  return false;
81 }
82 
84 MSTransportable::Stage::getEdgePosition(const MSEdge* e, double at, double offset) const {
85  return getLanePosition(e->getLanes()[0], at, offset);
86 }
87 
89 MSTransportable::Stage::getLanePosition(const MSLane* lane, double at, double offset) const {
90  return lane->getShape().positionAtOffset(lane->interpolateLanePosToGeometryPos(at), offset);
91 }
92 
93 double
94 MSTransportable::Stage::getEdgeAngle(const MSEdge* e, double at) const {
95  return e->getLanes()[0]->getShape().rotationAtOffset(at);
96 }
97 
98 
99 /* -------------------------------------------------------------------------
100 * MSTransportable::Stage_Waiting - methods
101 * ----------------------------------------------------------------------- */
103  SUMOTime duration, SUMOTime until, double pos, const std::string& actType,
104  const bool initial) :
105  MSTransportable::Stage(destination, 0, SUMOVehicleParameter::interpretEdgePos(
106  pos, destination.getLength(), SUMO_ATTR_DEPARTPOS, "stopping at " + destination.getID()),
107  initial ? WAITING_FOR_DEPART : WAITING),
108  myWaitingDuration(duration),
109  myWaitingUntil(until),
110  myActType(actType) {
111 }
112 
113 
115 
116 
117 const MSEdge*
119  return &myDestination;
120 }
121 
122 
123 const MSEdge*
125  return &myDestination;
126 }
127 
128 
129 double
131  return myArrivalPos;
132 }
133 
134 
135 SUMOTime
137  return myWaitingUntil;
138 }
139 
140 
141 Position
144  ROADSIDE_OFFSET * (MSNet::getInstance()->lefthand() ? -1 : 1));
145 }
146 
147 
148 double
150  return getEdgeAngle(&myDestination, myArrivalPos) + M_PI / 2 * (MSNet::getInstance()->lefthand() ? -1 : 1);
151 }
152 
153 
154 void
156  myDeparted = now;
157  const SUMOTime until = MAX3(now, now + myWaitingDuration, myWaitingUntil);
158  if (dynamic_cast<MSPerson*>(transportable) != 0) {
159  previous->getEdge()->addPerson(transportable);
160  net->getPersonControl().setWaitEnd(until, transportable);
161  } else {
162  previous->getEdge()->addContainer(transportable);
163  net->getContainerControl().setWaitEnd(until, transportable);
164  }
165 }
166 
167 
168 void
170  if (myType != WAITING_FOR_DEPART) {
171  os.openTag("stop");
172  os.writeAttr("duration", time2string(myArrived - myDeparted));
173  os.writeAttr("arrival", time2string(myArrived));
174  os.writeAttr("arrivalPos", toString(myArrivalPos));
175  os.writeAttr("actType", toString(myActType));
176  os.closeTag();
177  }
178 }
179 
180 
181 void
183  if (myType != WAITING_FOR_DEPART) {
185  if (myWaitingDuration >= 0) {
187  }
188  if (myWaitingUntil >= 0) {
190  }
191  os.closeTag();
192  }
193 }
194 
195 
196 void
198  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actstart " + myActType)
199  .writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
200 }
201 
202 
203 void
205  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "actend " + myActType).writeAttr("agent", p.getID())
206  .writeAttr("link", getEdge()->getID()).closeTag();
207 }
208 
209 
210 SUMOTime
212  return now - myDeparted;
213 }
214 
215 
216 double
218  return 0;
219 }
220 
221 
224  ConstMSEdgeVector result;
225  result.push_back(&getDestination());
226  return result;
227 }
228 
229 void
231  MSTransportableControl& tc = (dynamic_cast<MSPerson*>(t) != 0 ?
234  tc.abortWaiting(t);
235 }
236 
237 
238 /* -------------------------------------------------------------------------
239 * MSTransportable::Stage_Driving - methods
240 * ----------------------------------------------------------------------- */
242  MSStoppingPlace* toStop, const double arrivalPos, const std::vector<std::string>& lines) :
243  MSTransportable::Stage(destination, toStop, arrivalPos, DRIVING),
244  myLines(lines.begin(), lines.end()),
245  myVehicle(0),
246  myVehicleID("NULL"),
247  myStopWaitPos(Position::INVALID) {}
248 
249 
251 
252 const MSEdge*
254  if (myVehicle != 0) {
255  return &myVehicle->getLane()->getEdge();
256  }
257  return myWaitingEdge;
258 }
259 
260 
261 const MSEdge*
263  return myWaitingEdge;
264 }
265 
266 
267 double
269  if (isWaiting4Vehicle()) {
270  return myWaitingPos;
271  }
272  // vehicle may already have passed the lane (check whether this is correct)
274 }
275 
276 
277 Position
279  if (isWaiting4Vehicle()) {
281  return myStopWaitPos;
282  }
284  ROADSIDE_OFFSET * (MSNet::getInstance()->lefthand() ? -1 : 1));
285  }
286  return myVehicle->getPosition();
287 }
288 
289 
290 double
292  if (!isWaiting4Vehicle()) {
293  MSVehicle* veh = dynamic_cast<MSVehicle*>(myVehicle);
294  if (veh != 0) {
295  return veh->getAngle();
296  } else {
297  return 0;
298  }
299  }
300  return getEdgeAngle(myWaitingEdge, myWaitingPos) + M_PI / 2. * (MSNet::getInstance()->lefthand() ? -1 : 1);
301 }
302 
303 
304 bool
305 MSTransportable::Stage_Driving::isWaitingFor(const std::string& line) const {
306  return myLines.count(line) > 0;
307 }
308 
309 
310 bool
312  return myVehicle == 0;
313 }
314 
315 
316 SUMOTime
318  return isWaiting4Vehicle() ? now - myDeparted : 0;
319 }
320 
321 
322 double
324  return isWaiting4Vehicle() ? 0 : myVehicle->getSpeed();
325 }
326 
327 
330  ConstMSEdgeVector result;
331  result.push_back(getFromEdge());
332  result.push_back(&getDestination());
333  return result;
334 }
335 
336 
337 void
339  myVehicle = v;
340  myVehicleID = v->getID();
341 }
342 
343 
344 void
346  if (myVehicle != 0) {
347  // jumping out of a moving vehicle!
348  dynamic_cast<MSVehicle*>(myVehicle)->removeTransportable(t);
349  }
350 }
351 
352 
353 
354 void
356  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
357 }
358 
359 
360 void
362  os.openTag("event").writeAttr("time", time2string(t)).writeAttr("type", "arrival").writeAttr("agent", p.getID()).writeAttr("link", getEdge()->getID()).closeTag();
363 }
364 
365 
366 
367 /* -------------------------------------------------------------------------
368  * MSTransportable - methods
369  * ----------------------------------------------------------------------- */
371  : myParameter(pars), myVType(vtype), myPlan(plan) {
372  myStep = myPlan->begin();
373 }
374 
376  if (myPlan != 0) {
377  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
378  delete *i;
379  }
380  delete myPlan;
381  myPlan = 0;
382  }
383  delete myParameter;
384  if (myVType->isVehicleSpecific()) {
386  }
387 }
388 
389 const std::string&
391  return myParameter->id;
392 }
393 
394 SUMOTime
396  return myParameter->depart;
397 }
398 
399 void
401  (*myStep)->setDeparted(now);
402 }
403 
404 double
406  return (*myStep)->getEdgePos(MSNet::getInstance()->getCurrentTimeStep());
407 }
408 
409 Position
411  return (*myStep)->getPosition(MSNet::getInstance()->getCurrentTimeStep());
412 }
413 
414 double
416  return (*myStep)->getAngle(MSNet::getInstance()->getCurrentTimeStep());
417 }
418 
419 double
421  return STEPS2TIME((*myStep)->getWaitingTime(MSNet::getInstance()->getCurrentTimeStep()));
422 }
423 
424 double
426  return (*myStep)->getSpeed();
427 }
428 
429 
430 int
432  return (int)(myPlan->end() - myStep);
433 }
434 
435 int
437  return (int)myPlan->size();
438 }
439 
440 void
442  // myStep is invalidated upon modifying myPlan
443  const int stepIndex = (int)(myStep - myPlan->begin());
444  if (next < 0) {
445  myPlan->push_back(stage);
446  } else {
447  if (stepIndex + next > (int)myPlan->size()) {
448  throw ProcessError("invalid index '" + toString(next) + "' for inserting new stage into plan of '" + getID() + "'");
449  }
450  myPlan->insert(myPlan->begin() + stepIndex + next, stage);
451  }
452  myStep = myPlan->begin() + stepIndex;
453 }
454 
455 
456 void
458  assert(myStep + next < myPlan->end());
459  assert(next >= 0);
460  if (next > 0) {
461  // myStep is invalidated upon modifying myPlan
462  int stepIndex = (int)(myStep - myPlan->begin());
463  delete *(myStep + next);
464  myPlan->erase(myStep + next);
465  myStep = myPlan->begin() + stepIndex;
466  } else {
467  if (myStep + 1 == myPlan->end()) {
468  // stay in the simulation until the start of simStep to allow appending new stages (at the correct position)
469  appendStage(new Stage_Waiting(*getEdge(), 0, 0, getEdgePos(), "last stage removed", false));
470  }
471  (*myStep)->abort(this);
472  proceed(MSNet::getInstance(), MSNet::getInstance()->getCurrentTimeStep());
473  }
474 }
475 
476 
477 void
479  for (MSTransportablePlan::const_iterator i = myPlan->begin(); i != myPlan->end(); ++i) {
480  (*i)->setSpeed(speed);
481  }
482 }
483 
484 
485 void
487  if (myVType->isVehicleSpecific()) {
489  }
490  myVType = type;
491 }
492 
493 
496  if (myVType->isVehicleSpecific()) {
497  return *myVType;
498  }
499  MSVehicleType* type = myVType->buildSingularType(myVType->getID() + "@" + getID());
500  replaceVehicleType(type);
501  return *type;
502 }
503 
504 
507  PositionVector centerLine;
508  const Position p = getPosition();
509  const double angle = getAngle();
510  const double length = getVehicleType().getLength();
511  const Position back = p + Position(-cos(angle) * length, -sin(angle) * length);
512  centerLine.push_back(p);
513  centerLine.push_back(back);
514  centerLine.move2side(0.5 * getVehicleType().getWidth());
515  PositionVector result = centerLine;
516  centerLine.move2side(-getVehicleType().getWidth());
517  result.append(centerLine.reverse(), POSITION_EPS);
518  //std::cout << " transp=" << getID() << " p=" << p << " angle=" << GeomHelper::naviDegree(angle) << " back=" << back << " result=" << result << "\n";
519  return result;
520 }
521 
522 /****************************************************************************/
void abortWaiting(MSTransportable *t)
aborts waiting stage of transportable
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
double getLength() const
return the length of the edge
Definition: MSEdge.h:570
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:261
void setArrived(SUMOTime now)
logs end of the step
void abort(MSTransportable *)
abort this stage (TraCI)
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
bool lefthand() const
return whether the network was built for lefthand traffic
Definition: MSNet.h:688
virtual double getAngle() const
return the current angle of the transportable
virtual double getEdgePos() const
Return the position on the edge.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:83
std::string myActType
The type of activity.
double getEdgePos(SUMOTime now) const
void append(const PositionVector &v, double sameThreshold=2.0)
double getSpeed() const
the speed of the transportable
A lane area vehicles can halt at.
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
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:168
void setSpeed(double speed)
sets the walking speed (ignored in other stages)
virtual void addContainer(MSTransportable *container) const
Add a container to myContainers.
Definition: MSEdge.h:608
void abort(MSTransportable *)
abort this stage (TraCI)
#define INVALID
double getSpeed() const
the speed 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
double interpolateLanePosToGeometryPos(double lanePos) const
Definition: MSLane.h:442
int getNumRemainingStages() const
Return the number of remaining stages (including the current)
Stage_Waiting(const MSEdge &destination, SUMOTime duration, SUMOTime until, double pos, const std::string &actType, const bool initial)
constructor
Position getEdgePosition(const MSEdge *e, double at, double offset) const
get position on edge e at length at with orthogonal offset
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
virtual double getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
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
double getAngle() const
Returns the vehicle&#39;s direction in radians.
Definition: MSVehicle.h:605
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:79
virtual bool proceed(MSNet *net, SUMOTime time)=0
virtual Position getPosition(const double offset=0) const =0
Return current position (x/y, cartesian)
MSTransportablePlan::iterator myStep
the iterator over the route
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:252
double getAngle(SUMOTime now) const
returns the angle of the transportable
T MAX3(T a, T b, T c)
Definition: StdDefs.h:88
The simulated network and simulation perfomer.
Definition: MSNet.h:89
The car-following model and parameter.
Definition: MSVehicleType.h:73
virtual double getSpeed() const
the current speed of the transportable
SUMOTime getDesiredDepart() const
Returns the desired departure time.
PositionVector reverse() const
reverse position vector
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:775
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
const MSEdge * getFromEdge() const
MSVehicleType & getSingularType()
Replaces the current vehicle type with a new one used by this vehicle only.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A road/street connecting two junctions.
Definition: MSEdge.h:81
std::vector< MSTransportable::Stage * > MSTransportablePlan
the structure holding the plan of a transportable
PositionVector getBoundingBox() const
return the bounding box of the person
const MSVehicleType & getVehicleType() const
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
MSVehicleType * myVType
This transportable&#39;s type. (mainly used for drawing related information Note sure if it is really nec...
SUMOTime myArrived
the time at which this stage ended
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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
void setDeparted(SUMOTime now)
logs depart time of the current stage
virtual void addPerson(MSTransportable *p) const
Definition: MSEdge.h:596
Representation of a vehicle.
Definition: SUMOVehicle.h:67
virtual bool isWaitingFor(const std::string &line) const
Whether the transportable waits for a vehicle of the line specified.
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:767
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
A list of positions.
const MSEdge * getEdge() const
Returns the current edge.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:305
ConstMSEdgeVector getEdges() const
the edges of the current stage
virtual void routeOutput(OutputDevice &os) const
Called on writing vehroute output.
void removeVType(const MSVehicleType *vehType)
virtual ~MSTransportable()
destructor
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
T MIN2(T a, T b)
Definition: StdDefs.h:68
Position getPosition(SUMOTime now) const
returns the position of the transportable
#define POSITION_EPS
Definition: config.h:176
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)
double getEdgePos(SUMOTime now) const
bool isVehicleSpecific() const
Returns whether this type belongs to a single vehicle only (was modified)
Stage_Driving(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, const std::vector< std::string > &lines)
constructor
void move2side(double amount)
move position vector to side using certain ammount
SUMOTime getWaitingTime(SUMOTime now) const
time spent waiting for a ride
void appendStage(Stage *stage, int next=-1)
Appends the given stage to the current plan.
double getLength() const
Get vehicle&#39;s length [m].
StageType myType
The type of this stage.
Structure representing possible vehicle parameter.
virtual const MSEdge * getEdge() const =0
Returns the current edge.
MSTransportable(const SUMOVehicleParameter *pars, MSVehicleType *vtype, MSTransportablePlan *plan)
constructor
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
#define M_PI
Definition: odrSpiral.cpp:40
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
const MSEdge * getFromEdge() const
double getAngle(SUMOTime now) const
returns the angle of the transportable
static const double ROADSIDE_OFFSET
the offset for computing positions when standing at an edge
const std::string & getID() const
Returns the name of the vehicle type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
void replaceVehicleType(MSVehicleType *type)
Replaces the current vehicle type by the one given.
virtual Position getPosition() const
Return the Network coordinate of the transportable.
MSVehicleType * buildSingularType(const std::string &id) const
Duplicates the microsim vehicle type giving the newly created type the given id, marking it as vehicl...
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:426
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.
virtual void beginEventOutput(const MSTransportable &p, SUMOTime t, OutputDevice &os) const
Called for writing the events output.
MSTransportablePlan * myPlan
the plan of the transportable
long long int SUMOTime
Definition: TraCIDefs.h:52
virtual ~Stage_Waiting()
destructor
Position getPosition(SUMOTime now) const
returns the position of the transportable
virtual ~Stage()
destructor
Stage(const MSEdge &destination, MSStoppingPlace *toStop, const double arrivalPos, StageType type)
constructor
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.
Position getLanePosition(const MSLane *lane, double at, double offset) const
get position on lane at length at with orthogonal offset
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
double myArrivalPos
the position at which we want to arrive