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