SUMO - Simulation of Urban MObility
MSStoppingPlace.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2005-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A lane area vehicles can halt at
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <cassert>
26 #include <map>
28 #include <utils/geom/Position.h>
29 #include <microsim/MSVehicleType.h>
30 #include <microsim/MSNet.h>
31 #include "MSLane.h"
32 #include "MSTransportable.h"
33 #include "MSStoppingPlace.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 MSStoppingPlace::MSStoppingPlace(const std::string& id,
40  const std::vector<std::string>& lines,
41  MSLane& lane,
42  double begPos, double endPos, const std::string name)
43  : Named(id), myLines(lines), myLane(lane),
44  myBegPos(begPos), myEndPos(endPos), myLastFreePos(endPos), myWaitingPos(endPos), myName(name) {
46 }
47 
48 
50 
51 
52 const MSLane&
54  return myLane;
55 }
56 
57 
58 double
60  return myBegPos;
61 }
62 
63 
64 double
66  return myEndPos;
67 }
68 
69 
70 void
71 MSStoppingPlace::enter(SUMOVehicle* what, double beg, double end) {
72  myEndPositions[what] = std::pair<double, double>(beg, end);
74 }
75 
76 
77 double
78 MSStoppingPlace::getLastFreePos(const SUMOVehicle& forVehicle) const {
79  if (myLastFreePos != myEndPos) {
80  const double vehGap = forVehicle.getVehicleType().getMinGap();
81  double pos = myLastFreePos - vehGap;
82  if (!fits(pos, forVehicle)) {
83  // try to find a place ahead of the waiting vehicles
84  const double vehLength = forVehicle.getVehicleType().getLength();
85  std::vector<std::pair<double, std::pair<double, const SUMOVehicle*> > > spaces;
86  for (auto it : myEndPositions) {
87  spaces.push_back(std::make_pair(it.second.first, std::make_pair(it.second.second, it.first)));
88  }
89  // sorted from myEndPos towars myBegPos
90  std::sort(spaces.begin(), spaces.end());
91  std::reverse(spaces.begin(), spaces.end());
92  double prev = myEndPos;
93  for (auto it : spaces) {
94  //if (forVehicle.isSelected()) {
95  // std::cout << SIMTIME << " fitPosFor " << forVehicle.getID() << " l=" << vehLength << " prev=" << prev << " vehBeg=" << it.first << " vehEnd=" << it.second.first << " found=" << (prev - it.first >= vehLength) << "\n";
96  //}
97  if (prev - it.first + NUMERICAL_EPS >= vehLength && (
98  it.second.second->isParking()
99  || it.second.second->remainingStopDuration() > TIME2STEPS(10))) {
100  return prev;
101  }
102  prev = it.second.first - vehGap;
103  }
104  }
105  return pos;
106  }
107  return myLastFreePos;
108 }
109 
110 bool
111 MSStoppingPlace::fits(double pos, const SUMOVehicle& veh) const {
112  // always fit at the default position or if at least half the vehicle length
113  // is within the stop range (debatable)
114  return pos + POSITION_EPS >= myEndPos || (pos - myBegPos >= veh.getVehicleType().getLength() / 2);
115 }
116 
117 Position
120 }
121 
122 
123 double
125  std::map<const SUMOVehicle*, std::pair<double, double> >::const_iterator i = myEndPositions.find(veh);
126  if (i != myEndPositions.end()) {
127  return i->second.second;
128  } else {
129  return getLastFreePos(*veh);
130  }
131 }
132 
133 void
135  myWaitingTransportables.push_back(p);
138 }
139 
140 
141 void
143  std::vector<MSTransportable*>::iterator i = std::find(myWaitingTransportables.begin(), myWaitingTransportables.end(), p);
144  if (i != myWaitingTransportables.end()) {
145  if (i == myWaitingTransportables.end() - 1) {
147  }
148  if (i == myWaitingTransportables.begin()) {
150  }
151  myWaitingTransportables.erase(i);
152  }
153 }
154 
155 
156 void
158  assert(myEndPositions.find(what) != myEndPositions.end());
159  myEndPositions.erase(myEndPositions.find(what));
161 }
162 
163 
164 void
167  std::map<const SUMOVehicle*, std::pair<double, double> >::iterator i;
168  for (i = myEndPositions.begin(); i != myEndPositions.end(); i++) {
169  if (myLastFreePos > (*i).second.second) {
170  myLastFreePos = (*i).second.second;
171  }
172  }
173 }
174 
175 
176 double
178  if (edge == &myLane.getEdge()) {
179  return (myBegPos + myEndPos) / 2.;
180  }
181  for (const auto& access : myAccessPos) {
182  if (edge == &std::get<0>(access)->getEdge()) {
183  return std::get<1>(access);
184  }
185  }
186  return -1.;
187 }
188 
189 
190 double
192  if (edge == &myLane.getEdge()) {
193  return 0.;
194  }
195  for (const auto& access : myAccessPos) {
196  const MSLane* const accLane = std::get<0>(access);
197  if (edge == &accLane->getEdge()) {
198  const double length = std::get<2>(access);
199  if (length >= 0.) {
200  return length;
201  }
202  const Position accPos = accLane->geometryPositionAtOffset(std::get<1>(access));
203  const Position stopPos = myLane.geometryPositionAtOffset((myBegPos + myEndPos) / 2.);
204  return accPos.distanceTo(stopPos);
205  }
206  }
207  return -1.;
208 }
209 
210 
211 const std::string&
213  return myName;
214 }
215 
216 
217 bool
218 MSStoppingPlace::addAccess(MSLane* lane, const double pos, const double length) {
219  // prevent multiple accesss on the same lane
220  for (const auto& access : myAccessPos) {
221  if (lane == std::get<0>(access)) {
222  return false;
223  }
224  }
225  myAccessPos.push_back(std::make_tuple(lane, pos, length));
226  return true;
227 }
228 
229 
230 /****************************************************************************/
Position getWaitPosition() const
Returns the next free waiting place for pedestrians / containers.
std::map< const SUMOVehicle *, std::pair< double, double > > myEndPositions
A map from objects (vehicles) to the areas they acquire after entering the stop.
double getAccessPos(const MSEdge *edge) const
the position on the given edge which is connected to this stop, -1 on failure
MSStoppingPlace(const std::string &id, const std::vector< std::string > &lines, MSLane &lane, double begPos, double endPos, const std::string name="")
Constructor.
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:640
double getBeginLanePosition() const
Returns the begin position of this stop.
std::vector< MSTransportable * > myWaitingTransportables
Persons waiting at this stop.
const double myEndPos
The end position this bus stop is located at.
void enter(SUMOVehicle *what, double beg, double end)
Called if a vehicle enters this stop.
T MAX2(T a, T b)
Definition: StdDefs.h:76
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:456
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
const std::string & getMyName() const
void addTransportable(MSTransportable *p)
adds a transportable to this stop
virtual ~MSStoppingPlace()
Destructor.
A road/street connecting two junctions.
Definition: MSEdge.h:75
const MSLane & myLane
The lane this bus stop is located at.
double getEndLanePosition() const
Returns the end position of this stop.
bool fits(double pos, const SUMOVehicle &veh) const
return whether the given vehicle fits at the given position
Representation of a vehicle.
Definition: SUMOVehicle.h:60
double myLastFreePos
The last free position at this stop (variable)
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
#define POSITION_EPS
Definition: config.h:172
double getMinGap() const
Get the free space in front of vehicles of this class.
void removeTransportable(MSTransportable *p)
Removes a transportable from this stop.
Base class for objects which have an id.
Definition: Named.h:58
virtual bool addAccess(MSLane *lane, const double pos, const double length)
adds an access point to this stop
double getAccessDistance(const MSEdge *edge) const
the distance from the access on the given edge to the stop, -1 on failure
void leaveFrom(SUMOVehicle *what)
Called if a vehicle leaves this stop.
const std::string myName
The name of the stopping place.
const double myBegPos
The begin position this bus stop is located at.
std::vector< std::tuple< MSLane *, double, double > > myAccessPos
lanes and positions connected to this stop
double interpolateLanePosToGeometryPos(double lanePos) const
Definition: MSLane.h:472
const Position geometryPositionAtOffset(double offset, double lateralOffset=0) const
Definition: MSLane.h:478
double getLength() const
Get vehicle&#39;s length [m].
void computeLastFreePos()
Computes the last free position on this stop.
const MSVehicleType & getVehicleType() const
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:234
double myWaitingPos
The next free position for persons / containers.
#define NUMERICAL_EPS
Definition: config.h:148
const MSLane & getLane() const
Returns the lane this stop is located at.
double getLastFreePos() const
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
double getStoppingPosition(const SUMOVehicle *veh) const
For vehicles at the stop this gives the the actual stopping position of the vehicle. For all others the last free stopping position.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.