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-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 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  myReservationTime(-1),
57  myReservations(0),
58  myReservationMaxLength(0)
59 {
60  // initialize unspecified defaults
61  if (myWidth == 0) {
63  }
64  const double spaceDim = capacity > 0 ? myLane.interpolateLanePosToGeometryPos((myEndPos - myBegPos) / capacity) : 7.5;
65  if (myLength == 0) {
66  myLength = spaceDim;
67  }
68 
69  const double offset = MSNet::getInstance()->lefthand() ? -1 : 1;
70  myShape = lane.getShape().getSubpart(
72  lane.interpolateLanePosToGeometryPos(endPos));
73  if (!myOnRoad) {
74  myShape.move2side((lane.getWidth() / 2. + myWidth / 2.) * offset);
75  }
76  // Initialize space occupancies if there is a road-side capacity
77  // The overall number of lots is fixed and each lot accepts one vehicle regardless of size
78  for (int i = 0; i < capacity; ++i) {
79  const Position f = myShape.positionAtOffset(spaceDim * (i));
80  const Position s = myShape.positionAtOffset(spaceDim * (i + 1));
81  Position pos = myAngle == 0 ? s : (f + s) * 0.5;
82  addLotEntry(pos.x(), pos.y(), pos.z(),
84  ((double) atan2((s.x() - f.x()), (f.y() - s.y())) * (double) 180.0 / (double) M_PI) + myAngle);
85  mySpaceOccupancies.back().myEndPos = myBegPos + spaceDim * (i + 1);
86  }
88 }
89 
91 
92 void
93 MSParkingArea::addLotEntry(double x, double y, double z,
94  double width, double length, double angle) {
96  lsd.index = (int)mySpaceOccupancies.size();
97  lsd.vehicle = nullptr;
98  lsd.myPosition = Position(x, y, z);
99  lsd.myWidth = width;
100  lsd.myLength = length;
101  lsd.myRotation = angle;
102  lsd.myEndPos = myEndPos;
103  mySpaceOccupancies.push_back(lsd);
104  myCapacity++;
106 }
107 
108 
109 
110 
111 double
112 MSParkingArea::getLastFreePos(const SUMOVehicle& forVehicle) const {
113  if (myCapacity == getOccupancy()) {
114  // keep enough space so that parking vehicles can leave
115  return myLastFreePos - forVehicle.getVehicleType().getMinGap() - POSITION_EPS;
116  } else {
117  // XXX if (forVehicle.getLane() == myLane && forVehicle.getPositionOnLane() > myLastFreePos) {
118  // find freePos beyond vehicle position }
119  return myLastFreePos;
120  }
121 }
122 
123 Position
125  for (const auto& lsd : mySpaceOccupancies) {
126  if (lsd.vehicle == &forVehicle) {
127  return lsd.myPosition;
128  }
129  }
130  return Position::INVALID;
131 }
132 
133 double
134 MSParkingArea::getVehicleAngle(const SUMOVehicle& forVehicle) const {
135  for (const auto& lsd : mySpaceOccupancies) {
136  if (lsd.vehicle == &forVehicle) {
137  return (lsd.myRotation - 90.) * (double) M_PI / (double) 180.0;
138  }
139  }
140  return 0;
141 }
142 
143 
144 void
145 MSParkingArea::enter(SUMOVehicle* what, double beg, double end) {
146  if (myLastFreeLot >= 0) {
147  assert(myLastFreeLot < (int)mySpaceOccupancies.size());
148  mySpaceOccupancies[myLastFreeLot].vehicle = what;
149  myEndPositions[what] = std::pair<double, double>(beg, end);
151  }
152 }
153 
154 
155 void
157  assert(myEndPositions.find(what) != myEndPositions.end());
158  for (auto& lsd : mySpaceOccupancies) {
159  if (lsd.vehicle == what) {
160  lsd.vehicle = nullptr;
161  break;
162  }
163  }
164  myEndPositions.erase(myEndPositions.find(what));
166 }
167 
168 
169 void
171  myLastFreeLot = -1;
173  for (auto& lsd : mySpaceOccupancies) {
174  if (lsd.vehicle == nullptr) {
175  myLastFreeLot = lsd.index;
176  myLastFreePos = lsd.myEndPos;
177  break;
178  } else {
180  lsd.myEndPos - lsd.vehicle->getVehicleType().getLength() - NUMERICAL_EPS);
181  }
182  }
183 }
184 
185 
186 double
188  if (forVehicle.getLane() != &myLane) {
189  // for different lanes, do not consider reservations to avoid lane-order
190  // dependency in parallel simulation
191 #ifdef DEBUG_RESERVATIONS
192  if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " other lane\n";
193 #endif
194  return getLastFreePos(forVehicle);
195  }
196  if (t > myReservationTime) {
197 #ifdef DEBUG_RESERVATIONS
198  if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " first reservation\n";
199 #endif
200  myReservationTime = t;
201  myReservations = 1;
203  for (const auto& lsd : mySpaceOccupancies) {
204  if (lsd.vehicle != nullptr) {
205  myReservationMaxLength = MAX2(myReservationMaxLength, lsd.vehicle->getVehicleType().getLength());
206  }
207  }
208  return getLastFreePos(forVehicle);
209  } else {
211 #ifdef DEBUG_RESERVATIONS
212  if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " res=" << myReservations << " enough space\n";
213 #endif
214  myReservations++;
216  return getLastFreePos(forVehicle);
217  } else{
218  if (myCapacity == 0) {
219  return getLastFreePos(forVehicle);
220  } else {
221 #ifdef DEBUG_RESERVATIONS
222  if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID()
223  << " res=" << myReservations << " resTime=" << myReservationTime << " reserved full, maxLen=" << myReservationMaxLength << " endPos=" << mySpaceOccupancies[0].myEndPos << "\n";
224 #endif
225  return (mySpaceOccupancies[0].myEndPos
227  - forVehicle.getVehicleType().getMinGap()
228  - NUMERICAL_EPS);
229  }
230  }
231  }
232 }
233 
234 
235 double
237  return myWidth;
238 }
239 
240 
241 double
243  return myLength;
244 }
245 
246 
247 double
249  return myAngle;
250 }
251 
252 
253 int
255  return myCapacity;
256 }
257 
258 
259 int
261  return (int)myEndPositions.size();
262 }
263 
264 
265 /****************************************************************************/
std::map< const SUMOVehicle *, std::pair< double, double > > myEndPositions
A map from objects (vehicles) to the areas they acquire after entering the stop.
Position myPosition
The position of the vehicle when parking in this space.
int myLastFreeLot
Last free lot number (-1 no free lot)
long long int SUMOTime
Definition: SUMOTime.h:36
double z() const
Returns the z-position.
Definition: Position.h:67
A lane area vehicles can halt at.
void enter(SUMOVehicle *what, double beg, double end)
Called if a vehicle enters this stop.
double myReservationMaxLength
const double myEndPos
The end position this bus stop is located at.
const double SUMO_const_laneWidth
Definition: StdDefs.h:51
double y() const
Returns the y-position.
Definition: Position.h:62
Position getVehiclePosition(const SUMOVehicle &forVehicle) const
Returns the position of parked vehicle.
double x() const
Returns the x-position.
Definition: Position.h:57
virtual MSLane * getLane() const =0
Returns the lane the vehicle is on.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
T MAX2(T a, T b)
Definition: StdDefs.h:76
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:456
const std::string & getID() const
Returns the id.
Definition: Named.h:78
double getVehicleAngle(const SUMOVehicle &forVehicle) const
Returns the angle of parked vehicle.
#define DEBUG_COND2(obj)
double getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:530
#define SIMTIME
Definition: SUMOTime.h:65
double myAngle
The default angle of each parking space.
const MSLane & myLane
The lane this bus stop is located at.
SUMOTime myReservationTime
track parking reservations from the lane for the current time step
Representation of a vehicle.
Definition: SUMOVehicle.h:60
double myLastFreePos
The last free position at this stop (variable)
double myEndPos
The position along the lane that the vehicle needs to reach for entering this lot.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
PositionVector myShape
The roadside shape of this parkingArea.
double myLength
The default length of each parking space.
int getCapacity() const
Returns the area capacity.
T MIN2(T a, T b)
Definition: StdDefs.h:70
std::vector< LotSpaceDefinition > mySpaceOccupancies
All the spaces in this parking area.
#define POSITION_EPS
Definition: config.h:172
double getMinGap() const
Get the free space in front of vehicles of this class.
PositionVector getSubpart(double beginOffset, double endOffset) const
get subpart of a position vector
void computeLastFreePos()
Computes the last free position on this stop.
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.
bool lefthand() const
return whether the network was built for lefthand traffic
Definition: MSNet.h:615
int getOccupancy() const
Returns the area occupancy.
void move2side(double amount)
move position vector to side using certain ammount
virtual void addLotEntry(double x, double y, double z, double width, double length, double angle)
Add a lot entry to parking area.
double getLength() const
Returns the lot rectangle length.
const double myBegPos
The begin position this bus stop is located at.
int myCapacity
Stop area capacity.
bool myOnRoad
Whether vehicles stay on the road.
void leaveFrom(SUMOVehicle *what)
Called if a vehicle leaves this stop.
double getWidth() const
Returns the lot rectangle width.
#define M_PI
Definition: odrSpiral.cpp:40
virtual ~MSParkingArea()
Destructor.
double getLastFreePosWithReservation(SUMOTime t, const SUMOVehicle &forVehicle)
Returns the last free position on this stop including reservatiosn from the current lane and time ste...
double interpolateLanePosToGeometryPos(double lanePos) const
Definition: MSLane.h:472
double getLength() const
Get vehicle&#39;s length [m].
#define NUMERICAL_EPS
Definition: config.h:148
Representation of a single lot space.
double getLastFreePos() const
double myWidth
The default width of each parking space.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
double getAngle() const
Returns the lot rectangle angle.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
SUMOVehicle * vehicle
The last parked vehicle or 0.
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:285
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
double myRotation
The rotation.