Eclipse 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-2019 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 /****************************************************************************/
15 // A lane area vehicles can halt at
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
25 #include <map>
27 #include <utils/geom/Position.h>
28 #include <microsim/MSVehicleType.h>
29 #include <microsim/MSNet.h>
30 #include "MSLane.h"
31 #include "MSTransportable.h"
32 #include "MSStoppingPlace.h"
33 
34 // ===========================================================================
35 // method definitions
36 // ===========================================================================
37 MSStoppingPlace::MSStoppingPlace(const std::string& id,
38  const std::vector<std::string>& lines,
39  MSLane& lane,
40  double begPos, double endPos, const std::string name,
41  int capacity) :
42  Named(id), myLines(lines), myLane(lane),
43  myBegPos(begPos), myEndPos(endPos), myLastFreePos(endPos),
44  myName(name),
45  myTransportableCapacity(capacity) {
47  for (int i = 0; i < capacity; i++) {
48  myWaitingSpots.insert(i);
49  }
50 }
51 
52 
54 
55 
56 const MSLane&
58  return myLane;
59 }
60 
61 
62 double
64  return myBegPos;
65 }
66 
67 
68 double
70  return myEndPos;
71 }
72 
73 
74 void
75 MSStoppingPlace::enter(SUMOVehicle* what, double beg, double end) {
76  myEndPositions[what] = std::pair<double, double>(beg, end);
78 }
79 
80 
81 double
82 MSStoppingPlace::getLastFreePos(const SUMOVehicle& forVehicle) const {
83  if (myLastFreePos != myEndPos) {
84  const double vehGap = forVehicle.getVehicleType().getMinGap();
85  double pos = myLastFreePos - vehGap;
86  if (!fits(pos, forVehicle)) {
87  // try to find a place ahead of the waiting vehicles
88  const double vehLength = forVehicle.getVehicleType().getLength();
89  std::vector<std::pair<double, std::pair<double, const SUMOVehicle*> > > spaces;
90  for (auto it : myEndPositions) {
91  spaces.push_back(std::make_pair(it.second.first, std::make_pair(it.second.second, it.first)));
92  }
93  // sorted from myEndPos towars myBegPos
94  std::sort(spaces.begin(), spaces.end());
95  std::reverse(spaces.begin(), spaces.end());
96  double prev = myEndPos;
97  for (auto it : spaces) {
98  //if (forVehicle.isSelected()) {
99  // std::cout << SIMTIME << " fitPosFor " << forVehicle.getID() << " l=" << vehLength << " prev=" << prev << " vehBeg=" << it.first << " vehEnd=" << it.second.first << " found=" << (prev - it.first >= vehLength) << "\n";
100  //}
101  if (prev - it.first + NUMERICAL_EPS >= vehLength && (
102  it.second.second->isParking()
103  || it.second.second->remainingStopDuration() > TIME2STEPS(10))) {
104  return prev;
105  }
106  prev = it.second.first - vehGap;
107  }
108  }
109  return pos;
110  }
111  return myLastFreePos;
112 }
113 
114 bool
115 MSStoppingPlace::fits(double pos, const SUMOVehicle& veh) const {
116  // always fit at the default position or if at least half the vehicle length
117  // is within the stop range (debatable)
118  return pos + POSITION_EPS >= myEndPos || (pos - myBegPos >= veh.getVehicleType().getLength() / 2);
119 }
120 
121 double
123  auto it = myWaitingTransportables.find(t);
124  if (it != myWaitingTransportables.end() && it->second >= 0) {
125  return myEndPos - (0.5 + (it->second) % getPersonsAbreast()) * SUMO_const_waitingPersonWidth;
126  } else {
127  return (myEndPos + myBegPos) / 2;
128  }
129 }
130 
131 
132 int
134  return MAX2(1, (int)floor(length / SUMO_const_waitingPersonWidth));
135 }
136 
137 int
140 }
141 
142 Position
144  double lanePos = getWaitingPositionOnLane(t);
145  int row = 0;
146  auto it = myWaitingTransportables.find(t);
147  if (it != myWaitingTransportables.end()) {
148  if (it->second >= 0) {
149  row = int(it->second / getPersonsAbreast());
150  } else {
151  // invalid position, draw outside bounds
153  }
154  }
157 }
158 
159 
160 double
162  std::map<const SUMOVehicle*, std::pair<double, double> >::const_iterator i = myEndPositions.find(veh);
163  if (i != myEndPositions.end()) {
164  return i->second.second;
165  } else {
166  return getLastFreePos(*veh);
167  }
168 }
169 
170 std::vector<MSTransportable*>
172  std::vector<MSTransportable*> result;
173  for (std::map<MSTransportable*, int>::const_iterator it = myWaitingTransportables.begin(); it != myWaitingTransportables.end(); it++) {
174  result.push_back(it->first);
175  }
176  return result;
177 }
178 
179 bool
181  return myWaitingSpots.size() > 0;
182 }
183 
184 bool
186  int spot = -1;
187  if (!hasSpaceForTransportable()) {
188  return false;
189  }
190  spot = *myWaitingSpots.begin();
191  myWaitingSpots.erase(myWaitingSpots.begin());
192  myWaitingTransportables[p] = spot;
193  return true;
194 }
195 
196 
197 void
199  auto i = myWaitingTransportables.find(p);
200  if (i != myWaitingTransportables.end()) {
201  if (i->second >= 0) {
202  myWaitingSpots.insert(i->second);
203  }
204  myWaitingTransportables.erase(i);
205  }
206 }
207 
208 
209 void
211  assert(myEndPositions.find(what) != myEndPositions.end());
212  myEndPositions.erase(myEndPositions.find(what));
214 }
215 
216 
217 void
220  std::map<const SUMOVehicle*, std::pair<double, double> >::iterator i;
221  for (i = myEndPositions.begin(); i != myEndPositions.end(); i++) {
222  if (myLastFreePos > (*i).second.second) {
223  myLastFreePos = (*i).second.second;
224  }
225  }
226 }
227 
228 
229 double
231  if (edge == &myLane.getEdge()) {
232  return (myBegPos + myEndPos) / 2.;
233  }
234  for (const auto& access : myAccessPos) {
235  if (edge == &std::get<0>(access)->getEdge()) {
236  return std::get<1>(access);
237  }
238  }
239  return -1.;
240 }
241 
242 
243 double
245  if (edge == &myLane.getEdge()) {
246  return 0.;
247  }
248  for (const auto& access : myAccessPos) {
249  const MSLane* const accLane = std::get<0>(access);
250  if (edge == &accLane->getEdge()) {
251  const double length = std::get<2>(access);
252  if (length >= 0.) {
253  return length;
254  }
255  const Position accPos = accLane->geometryPositionAtOffset(std::get<1>(access));
256  const Position stopPos = myLane.geometryPositionAtOffset((myBegPos + myEndPos) / 2.);
257  return accPos.distanceTo(stopPos);
258  }
259  }
260  return -1.;
261 }
262 
263 
264 const std::string&
266  return myName;
267 }
268 
269 
270 bool
271 MSStoppingPlace::addAccess(MSLane* lane, const double pos, const double length) {
272  // prevent multiple accesss on the same lane
273  for (const auto& access : myAccessPos) {
274  if (lane == std::get<0>(access)) {
275  return false;
276  }
277  }
278  myAccessPos.push_back(std::make_tuple(lane, pos, length));
279  return true;
280 }
281 
282 
283 /****************************************************************************/
MSStoppingPlace::getLane
const MSLane & getLane() const
Returns the lane this stop is located at.
Definition: MSStoppingPlace.cpp:57
MSStoppingPlace::hasSpaceForTransportable
bool hasSpaceForTransportable() const
whether there is still capacity for more transportables
Definition: MSStoppingPlace.cpp:180
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
Named
Base class for objects which have an id.
Definition: Named.h:56
MSStoppingPlace::addTransportable
bool addTransportable(MSTransportable *p)
adds a transportable to this stop
Definition: MSStoppingPlace.cpp:185
MSStoppingPlace::getEndLanePosition
double getEndLanePosition() const
Returns the end position of this stop.
Definition: MSStoppingPlace.cpp:69
NUMERICAL_EPS
#define NUMERICAL_EPS
Definition: config.h:148
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
MSStoppingPlace::getBeginLanePosition
double getBeginLanePosition() const
Returns the begin position of this stop.
Definition: MSStoppingPlace.cpp:63
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
MSStoppingPlace::MSStoppingPlace
MSStoppingPlace(const std::string &id, const std::vector< std::string > &lines, MSLane &lane, double begPos, double endPos, const std::string name="", int capacity=0)
Constructor.
Definition: MSStoppingPlace.cpp:37
MSStoppingPlace::addAccess
virtual bool addAccess(MSLane *lane, const double pos, const double length)
adds an access point to this stop
Definition: MSStoppingPlace.cpp:271
SUMO_const_waitingPersonDepth
const double SUMO_const_waitingPersonDepth
Definition: StdDefs.h:57
MSStoppingPlace::removeTransportable
void removeTransportable(MSTransportable *p)
Removes a transportable from this stop.
Definition: MSStoppingPlace.cpp:198
MSStoppingPlace::myEndPositions
std::map< const SUMOVehicle *, std::pair< double, double > > myEndPositions
A map from objects (vehicles) to the areas they acquire after entering the stop.
Definition: MSStoppingPlace.h:219
MSTransportable
Definition: MSTransportable.h:58
MSVehicleType.h
MSStoppingPlace::myBegPos
const double myBegPos
The begin position this bus stop is located at.
Definition: MSStoppingPlace.h:225
MAX2
T MAX2(T a, T b)
Definition: StdDefs.h:79
SUMOVehicle.h
SUMO_const_waitingPersonWidth
const double SUMO_const_waitingPersonWidth
Definition: StdDefs.h:56
MSStoppingPlace::myName
const std::string myName
The name of the stopping place.
Definition: MSStoppingPlace.h:234
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:58
MSStoppingPlace::myEndPos
const double myEndPos
The end position this bus stop is located at.
Definition: MSStoppingPlace.h:228
MSStoppingPlace::getTransportables
std::vector< MSTransportable * > getTransportables() const
Returns the tranportables waiting on this stop.
Definition: MSStoppingPlace.cpp:171
Position::distanceTo
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimension
Definition: Position.h:233
MSStoppingPlace::getStoppingPosition
double getStoppingPosition(const SUMOVehicle *veh) const
For vehicles at the stop this gives the the actual stopping position of the vehicle....
Definition: MSStoppingPlace.cpp:161
PositionVector::positionAtOffset
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
Definition: PositionVector.cpp:248
MSLane::interpolateLanePosToGeometryPos
double interpolateLanePosToGeometryPos(double lanePos) const
Definition: MSLane.h:498
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
MSStoppingPlace::myAccessPos
std::vector< std::tuple< MSLane *, double, double > > myAccessPos
lanes and positions connected to this stop
Definition: MSStoppingPlace.h:246
MSVehicleType::getMinGap
double getMinGap() const
Get the free space in front of vehicles of this class.
Definition: MSVehicleType.h:125
MSEdge
A road/street connecting two junctions.
Definition: MSEdge.h:78
MSStoppingPlace::getAccessDistance
double getAccessDistance(const MSEdge *edge) const
the distance from the access on the given edge to the stop, -1 on failure
Definition: MSStoppingPlace.cpp:244
MSStoppingPlace::computeLastFreePos
void computeLastFreePos()
Computes the last free position on this stop.
Definition: MSStoppingPlace.cpp:218
MSStoppingPlace::enter
void enter(SUMOVehicle *what, double beg, double end)
Called if a vehicle enters this stop.
Definition: MSStoppingPlace.cpp:75
MSStoppingPlace::getMyName
const std::string & getMyName() const
Definition: MSStoppingPlace.cpp:265
MSLane::getEdge
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:669
MSStoppingPlace::myTransportableCapacity
const int myTransportableCapacity
The number of transportables that can wait here.
Definition: MSStoppingPlace.h:237
Position.h
MSLane::getShape
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:477
MSStoppingPlace::~MSStoppingPlace
virtual ~MSStoppingPlace()
Destructor.
Definition: MSStoppingPlace.cpp:53
MSStoppingPlace::myWaitingSpots
std::set< int > myWaitingSpots
Definition: MSStoppingPlace.h:243
MSVehicleType::getLength
double getLength() const
Get vehicle's length [m].
Definition: MSVehicleType.h:109
MSStoppingPlace::myWaitingTransportables
std::map< MSTransportable *, int > myWaitingTransportables
Persons waiting at this stop (mapped to waiting position)
Definition: MSStoppingPlace.h:242
MSTransportable.h
MSStoppingPlace::leaveFrom
void leaveFrom(SUMOVehicle *what)
Called if a vehicle leaves this stop.
Definition: MSStoppingPlace.cpp:210
MSStoppingPlace::getAccessPos
double getAccessPos(const MSEdge *edge) const
the position on the given edge which is connected to this stop, -1 on failure
Definition: MSStoppingPlace.cpp:230
MSStoppingPlace::myLastFreePos
double myLastFreePos
The last free position at this stop (variable)
Definition: MSStoppingPlace.h:231
MSStoppingPlace::getLastFreePos
double getLastFreePos() const
Definition: MSStoppingPlace.h:172
MSStoppingPlace::getWaitingPositionOnLane
double getWaitingPositionOnLane(MSTransportable *t) const
Returns the lane position corresponding to getWaitPosition()
Definition: MSStoppingPlace.cpp:122
MSLane::getWidth
double getWidth() const
Returns the lane's width.
Definition: MSLane.h:556
config.h
MSStoppingPlace::getWaitPosition
virtual Position getWaitPosition(MSTransportable *person) const
Returns the next free waiting place for pedestrians / containers.
Definition: MSStoppingPlace.cpp:143
MSLane::geometryPositionAtOffset
const Position geometryPositionAtOffset(double offset, double lateralOffset=0) const
Definition: MSLane.h:504
MSStoppingPlace::getPersonsAbreast
int getPersonsAbreast() const
Definition: MSStoppingPlace.cpp:138
MSLane.h
MSStoppingPlace::fits
bool fits(double pos, const SUMOVehicle &veh) const
return whether the given vehicle fits at the given position
Definition: MSStoppingPlace.cpp:115
MSStoppingPlace.h
POSITION_EPS
#define POSITION_EPS
Definition: config.h:172
MSStoppingPlace::myLane
const MSLane & myLane
The lane this bus stop is located at.
Definition: MSStoppingPlace.h:222