Eclipse SUMO - Simulation of Urban MObility
MSParkingArea.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2015-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 area where vehicles can park next to the road
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <cassert>
27 #include <utils/geom/Position.h>
28 #include <utils/geom/GeomHelper.h>
29 #include <microsim/MSNet.h>
30 #include <microsim/MSVehicleType.h>
31 #include "MSLane.h"
32 #include "MSTransportable.h"
33 #include "MSParkingArea.h"
34 
35 //#define DEBUG_RESERVATIONS
36 //#define DEBUG_COND2(obj) (obj.getID() == "v.3")
37 #define DEBUG_COND2(obj) (obj.isSelected())
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 MSParkingArea::MSParkingArea(const std::string& id,
44  const std::vector<std::string>& lines,
45  MSLane& lane,
46  double begPos, double endPos,
47  int capacity,
48  double width, double length, double angle, const std::string& name,
49  bool onRoad) :
50  MSStoppingPlace(id, lines, lane, begPos, endPos, name),
51  myCapacity(0),
52  myOnRoad(onRoad),
53  myWidth(width),
54  myLength(length),
55  myAngle(angle),
56  myEgressBlocked(false),
57  myReservationTime(-1),
58  myReservations(0),
59  myReservationMaxLength(0),
60  myNumAlternatives(0) {
61  // initialize unspecified defaults
62  if (myWidth == 0) {
64  }
65  const double spaceDim = capacity > 0 ? myLane.interpolateLanePosToGeometryPos((myEndPos - myBegPos) / capacity) : 7.5;
66  if (myLength == 0) {
67  myLength = spaceDim;
68  }
69 
70  const double offset = MSNet::getInstance()->lefthand() ? -1 : 1;
71  myShape = lane.getShape().getSubpart(
73  lane.interpolateLanePosToGeometryPos(endPos));
74  if (!myOnRoad) {
75  myShape.move2side((lane.getWidth() / 2. + myWidth / 2.) * offset);
76  }
77  // Initialize space occupancies if there is a road-side capacity
78  // The overall number of lots is fixed and each lot accepts one vehicle regardless of size
79  for (int i = 0; i < capacity; ++i) {
80  const Position f = myShape.positionAtOffset(spaceDim * (i));
81  const Position s = myShape.positionAtOffset(spaceDim * (i + 1));
82  Position pos = myAngle == 0 ? s : (f + s) * 0.5;
83  addLotEntry(pos.x(), pos.y(), pos.z(),
85  ((double) atan2((s.x() - f.x()), (f.y() - s.y())) * (double) 180.0 / (double) M_PI) + myAngle);
86  mySpaceOccupancies.back().myEndPos = myBegPos + MAX2(POSITION_EPS, spaceDim * (i + 1));
87  }
89 }
90 
92 
93 void
94 MSParkingArea::addLotEntry(double x, double y, double z,
95  double width, double length, double angle) {
97  lsd.index = (int)mySpaceOccupancies.size();
98  lsd.vehicle = nullptr;
99  lsd.myPosition = Position(x, y, z);
100  lsd.myWidth = width;
101  lsd.myLength = length;
102  lsd.myRotation = angle;
103  lsd.myEndPos = myEndPos;
104  mySpaceOccupancies.push_back(lsd);
105  myCapacity++;
107 }
108 
109 
110 
111 
112 double
113 MSParkingArea::getLastFreePos(const SUMOVehicle& forVehicle) const {
114  if (myCapacity == (int)myEndPositions.size()) {
115  // keep enough space so that parking vehicles can leave
116  return myLastFreePos - forVehicle.getVehicleType().getMinGap() - POSITION_EPS;
117  } else {
118  // XXX if (forVehicle.getLane() == myLane && forVehicle.getPositionOnLane() > myLastFreePos) {
119  // find freePos beyond vehicle position }
120  return myLastFreePos;
121  }
122 }
123 
124 Position
126  for (const auto& lsd : mySpaceOccupancies) {
127  if (lsd.vehicle == &forVehicle) {
128  return lsd.myPosition;
129  }
130  }
131  return Position::INVALID;
132 }
133 
134 
135 double
137  for (const auto& lsd : mySpaceOccupancies) {
138  if (lsd.vehicle == &forVehicle) {
139  return lsd.myEndPos;
140  }
141  }
142  return -1;
143 }
144 
145 
146 double
147 MSParkingArea::getVehicleAngle(const SUMOVehicle& forVehicle) const {
148  for (const auto& lsd : mySpaceOccupancies) {
149  if (lsd.vehicle == &forVehicle) {
150  return (lsd.myRotation - 90.) * (double) M_PI / (double) 180.0;
151  }
152  }
153  return 0;
154 }
155 
156 
157 void
158 MSParkingArea::enter(SUMOVehicle* what, double beg, double end) {
159  assert(myLastFreePos >= 0);
160  assert(myLastFreeLot < (int)mySpaceOccupancies.size());
161  mySpaceOccupancies[myLastFreeLot].vehicle = what;
162  myEndPositions[what] = std::pair<double, double>(beg, end);
164 }
165 
166 
167 void
169  assert(myEndPositions.find(what) != myEndPositions.end());
170  for (auto& lsd : mySpaceOccupancies) {
171  if (lsd.vehicle == what) {
172  lsd.vehicle = nullptr;
173  break;
174  }
175  }
176  myEndPositions.erase(myEndPositions.find(what));
178 }
179 
180 
181 void
183  myLastFreeLot = -1;
185  myEgressBlocked = false;
186  for (auto& lsd : mySpaceOccupancies) {
187  if (lsd.vehicle == nullptr
188  || (getOccupancy() == getCapacity()
189  && lsd.vehicle->remainingStopDuration() <= 0
190  && !lsd.vehicle->isStoppedTriggered())) {
191  if (lsd.vehicle == nullptr) {
192  myLastFreeLot = lsd.index;
193  myLastFreePos = lsd.myEndPos;
194  } else {
195  // vehicle wants to exit the parking area
196  myLastFreeLot = lsd.index;
197  myLastFreePos = lsd.myEndPos - lsd.vehicle->getVehicleType().getLength() - POSITION_EPS;
198  myEgressBlocked = true;
199  }
200  break;
201  } else {
203  lsd.myEndPos - lsd.vehicle->getVehicleType().getLength() - NUMERICAL_EPS);
204  }
205  }
206 }
207 
208 
209 double
211  if (forVehicle.getLane() != &myLane) {
212  // for different lanes, do not consider reservations to avoid lane-order
213  // dependency in parallel simulation
214 #ifdef DEBUG_RESERVATIONS
215  if (DEBUG_COND2(forVehicle)) {
216  std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " other lane\n";
217  }
218 #endif
219  if (myNumAlternatives > 0 && getOccupancy() == getCapacity()) {
220  // ensure that the vehicle reaches the rerouter lane
222  } else {
223  return getLastFreePos(forVehicle);
224  }
225  }
226  if (t > myReservationTime) {
227 #ifdef DEBUG_RESERVATIONS
228  if (DEBUG_COND2(forVehicle)) {
229  std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " first reservation\n";
230  }
231 #endif
232  myReservationTime = t;
233  myReservations = 1;
235  for (const auto& lsd : mySpaceOccupancies) {
236  if (lsd.vehicle != nullptr) {
237  myReservationMaxLength = MAX2(myReservationMaxLength, lsd.vehicle->getVehicleType().getLength());
238  }
239  }
240  return getLastFreePos(forVehicle);
241  } else {
243 #ifdef DEBUG_RESERVATIONS
244  if (DEBUG_COND2(forVehicle)) {
245  std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " res=" << myReservations << " enough space\n";
246  }
247 #endif
248  myReservations++;
250  return getLastFreePos(forVehicle);
251  } else {
252  if (myCapacity == 0) {
253  return getLastFreePos(forVehicle);
254  } else {
255 #ifdef DEBUG_RESERVATIONS
256  if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID()
257  << " res=" << myReservations << " resTime=" << myReservationTime << " reserved full, maxLen=" << myReservationMaxLength << " endPos=" << mySpaceOccupancies[0].myEndPos << "\n";
258 #endif
259  return (mySpaceOccupancies[0].myEndPos
261  - forVehicle.getVehicleType().getMinGap()
262  - NUMERICAL_EPS);
263  }
264  }
265  }
266 }
267 
268 
269 double
271  return myWidth;
272 }
273 
274 
275 double
277  return myLength;
278 }
279 
280 
281 double
283  return myAngle;
284 }
285 
286 
287 int
289  return myCapacity;
290 }
291 
292 
293 int
295  return (int)myEndPositions.size() - (myEgressBlocked ? 1 : 0);
296 }
297 
298 
299 int
301  return (int)myEndPositions.size();
302 }
303 
304 void
307 }
308 
309 /****************************************************************************/
MSParkingArea::LotSpaceDefinition::myRotation
double myRotation
The rotation.
Definition: MSParkingArea.h:232
MSStoppingPlace
A lane area vehicles can halt at.
Definition: MSStoppingPlace.h:60
MIN2
T MIN2(T a, T b)
Definition: StdDefs.h:74
MSParkingArea::enter
void enter(SUMOVehicle *what, double beg, double end)
Called if a vehicle enters this stop.
Definition: MSParkingArea.cpp:158
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSParkingArea::myLastFreeLot
int myLastFreeLot
Last free lot number (-1 no free lot)
Definition: MSParkingArea.h:251
MSParkingArea::myReservationMaxLength
double myReservationMaxLength
Definition: MSParkingArea.h:281
MSParkingArea::notifyEgressBlocked
void notifyEgressBlocked()
update state so that vehicles wishing to enter cooperate with exiting vehicles
Definition: MSParkingArea.cpp:305
NUMERICAL_EPS
#define NUMERICAL_EPS
Definition: config.h:145
Position::z
double z() const
Returns the z-position.
Definition: Position.h:67
Position::INVALID
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:285
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
MSParkingArea::LotSpaceDefinition::myWidth
double myWidth
The width.
Definition: MSParkingArea.h:234
PositionVector::getSubpart
PositionVector getSubpart(double beginOffset, double endOffset) const
get subpart of a position vector
Definition: PositionVector.cpp:698
MSParkingArea::myReservations
int myReservations
Definition: MSParkingArea.h:280
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:61
MSParkingArea::LotSpaceDefinition::myPosition
Position myPosition
The position of the vehicle when parking in this space.
Definition: MSParkingArea.h:230
MSParkingArea::LotSpaceDefinition::index
int index
the running index
Definition: MSParkingArea.h:226
MSParkingArea::getInsertionPosition
double getInsertionPosition(const SUMOVehicle &forVehicle) const
Returns the insertion position of a parked vehicle.
Definition: MSParkingArea.cpp:136
SUMO_const_laneWidth
const double SUMO_const_laneWidth
Definition: StdDefs.h:50
MSParkingArea::getVehiclePosition
Position getVehiclePosition(const SUMOVehicle &forVehicle) const
Returns the position of parked vehicle.
Definition: MSParkingArea.cpp:125
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
MSParkingArea::getVehicleAngle
double getVehicleAngle(const SUMOVehicle &forVehicle) const
Returns the angle of parked vehicle.
Definition: MSParkingArea.cpp:147
MSParkingArea::myAngle
double myAngle
The default angle of each parking space.
Definition: MSParkingArea.h:266
MSParkingArea::LotSpaceDefinition::myLength
double myLength
The length.
Definition: MSParkingArea.h:236
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
MSParkingArea::myReservationTime
SUMOTime myReservationTime
track parking reservations from the lane for the current time step
Definition: MSParkingArea.h:279
SUMOVehicle.h
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:64
MSNet::lefthand
bool lefthand() const
return whether the network was built for lefthand traffic
Definition: MSNet.h:662
MSStoppingPlace::myEndPos
const double myEndPos
The end position this bus stop is located at.
Definition: MSStoppingPlace.h:229
MSParkingArea::myShape
PositionVector myShape
The roadside shape of this parkingArea.
Definition: MSParkingArea.h:273
MSParkingArea::getCapacity
int getCapacity() const
Returns the area capacity.
Definition: MSParkingArea.cpp:288
DEBUG_COND2
#define DEBUG_COND2(obj)
Definition: MSParkingArea.cpp:37
SUMOVehicle::getLane
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
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
MSParkingArea::LotSpaceDefinition::myEndPos
double myEndPos
The position along the lane that the vehicle needs to reach for entering this lot.
Definition: MSParkingArea.h:238
MSParkingArea::mySpaceOccupancies
std::vector< LotSpaceDefinition > mySpaceOccupancies
All the spaces in this parking area.
Definition: MSParkingArea.h:270
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
Position::x
double x() const
Returns the x-position.
Definition: Position.h:57
MSParkingArea::MSParkingArea
MSParkingArea(const std::string &id, const std::vector< std::string > &lines, MSLane &lane, double begPos, double endPos, int capacity, double width, double length, double angle, const std::string &name, bool onRoad)
Constructor.
Definition: MSParkingArea.cpp:43
MSVehicleType::getMinGap
double getMinGap() const
Get the free space in front of vehicles of this class.
Definition: MSVehicleType.h:126
MSParkingArea::myLength
double myLength
The default length of each parking space.
Definition: MSParkingArea.h:263
MSParkingArea.h
MSParkingArea::addLotEntry
virtual void addLotEntry(double x, double y, double z, double width, double length, double angle)
Add a lot entry to parking area.
Definition: MSParkingArea.cpp:94
MSParkingArea::LotSpaceDefinition
Representation of a single lot space.
Definition: MSParkingArea.h:224
MSParkingArea::getLength
double getLength() const
Returns the lot rectangle length.
Definition: MSParkingArea.cpp:276
MSParkingArea::myCapacity
int myCapacity
Stop area capacity.
Definition: MSParkingArea.h:254
MSParkingArea::leaveFrom
void leaveFrom(SUMOVehicle *what)
Called if a vehicle leaves this stop.
Definition: MSParkingArea.cpp:168
MSParkingArea::computeLastFreePos
void computeLastFreePos()
Computes the last free position on this stop.
Definition: MSParkingArea.cpp:182
MSParkingArea::myOnRoad
bool myOnRoad
Whether vehicles stay on the road.
Definition: MSParkingArea.h:257
Position.h
MSLane::getShape
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:478
MSParkingArea::getOccupancy
int getOccupancy() const
Returns the area occupancy.
Definition: MSParkingArea.cpp:294
MSParkingArea::getWidth
double getWidth() const
Returns the lot rectangle width.
Definition: MSParkingArea.cpp:270
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
Position::y
double y() const
Returns the y-position.
Definition: Position.h:62
M_PI
#define M_PI
Definition: odrSpiral.cpp:40
MSParkingArea::getLastFreePosWithReservation
double getLastFreePosWithReservation(SUMOTime t, const SUMOVehicle &forVehicle)
Returns the last free position on this stop including reservatiosn from the current lane and time ste...
Definition: MSParkingArea.cpp:210
MSVehicleType::getLength
double getLength() const
Get vehicle's length [m].
Definition: MSVehicleType.h:110
MSParkingArea::getOccupancyIncludingBlocked
int getOccupancyIncludingBlocked() const
Returns the area occupancy.
Definition: MSParkingArea.cpp:300
MSTransportable.h
MSStoppingPlace::myLastFreePos
double myLastFreePos
The last free position at this stop (variable)
Definition: MSStoppingPlace.h:232
MSParkingArea::~MSParkingArea
virtual ~MSParkingArea()
Destructor.
Definition: MSParkingArea.cpp:91
MSStoppingPlace::getLastFreePos
double getLastFreePos() const
Definition: MSStoppingPlace.h:173
MSLane::getWidth
double getWidth() const
Returns the lane's width.
Definition: MSLane.h:557
config.h
GeomHelper.h
MSParkingArea::myNumAlternatives
int myNumAlternatives
the number of alternative parkingAreas that are assigned to parkingAreaRerouter
Definition: MSParkingArea.h:284
MSLane.h
MSParkingArea::LotSpaceDefinition::vehicle
SUMOVehicle * vehicle
The last parked vehicle or 0.
Definition: MSParkingArea.h:228
MSParkingArea::getAngle
double getAngle() const
Returns the lot rectangle angle.
Definition: MSParkingArea.cpp:282
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
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
PositionVector::move2side
void move2side(double amount, double maxExtension=100)
move position vector to side using certain ammount
Definition: PositionVector.cpp:1086
MSParkingArea::myEgressBlocked
bool myEgressBlocked
whether a vehicle wants to exit but is blocked
Definition: MSParkingArea.h:276
MSParkingArea::myWidth
double myWidth
The default width of each parking space.
Definition: MSParkingArea.h:260