Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2015-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSParkingArea.cpp
15 : /// @author Mirco Sturari
16 : /// @author Jakob Erdmann
17 : /// @author Mirko Barthauer
18 : /// @date Tue, 19.01.2016
19 : ///
20 : // A area where vehicles can park next to the road
21 : /****************************************************************************/
22 : #include <config.h>
23 :
24 : #include <cassert>
25 : #include <utils/common/WrappingCommand.h>
26 : #include <utils/vehicle/SUMOVehicle.h>
27 : #include <utils/geom/Position.h>
28 : #include <utils/geom/GeomHelper.h>
29 : #include <microsim/MSEventControl.h>
30 : #include <microsim/MSNet.h>
31 : #include <microsim/MSEdge.h>
32 : #include <microsim/MSVehicle.h>
33 : #include <microsim/MSVehicleType.h>
34 : #include "MSLane.h"
35 : #include <microsim/transportables/MSTransportable.h>
36 : #include "MSParkingArea.h"
37 : #include "MSGlobals.h"
38 : #include <utils/common/MsgHandler.h>
39 :
40 : //#define DEBUG_RESERVATIONS
41 : //#define DEBUG_GET_LAST_FREE_POS
42 : //#define DEBUG_COND2(obj) (obj.getID() == "v.3")
43 : #define DEBUG_COND2(obj) (obj.isSelected())
44 :
45 :
46 : // ===========================================================================
47 : // method definitions
48 : // ===========================================================================
49 17896 : MSParkingArea::MSParkingArea(const std::string& id, const std::vector<std::string>& lines,
50 : const std::vector<std::string>& badges, MSLane& lane,
51 : double begPos, double endPos, int capacity, double width, double length,
52 : double angle, const std::string& name, bool onRoad,
53 17896 : const std::string& departPos, bool lefthand, bool reservable) :
54 : MSStoppingPlace(id, SUMO_TAG_PARKING_AREA, lines, lane, begPos, endPos, name),
55 17896 : myRoadSideCapacity(capacity),
56 17896 : myCapacity(0),
57 17896 : myOnRoad(onRoad),
58 17896 : myReservable(reservable),
59 17896 : myWidth(width),
60 17896 : myLength(length),
61 17896 : myAngle(lefthand ? -angle : angle),
62 17896 : myAcceptedBadges(badges.begin(), badges.end()),
63 17896 : myEgressBlocked(false),
64 17896 : myReservationTime(-1),
65 17896 : myLastReservationTime(-1),
66 17896 : myReservations(0),
67 17896 : myLastReservations(0),
68 17896 : myReservationMaxLength(0),
69 17896 : myLastReservationMaxLength(0),
70 17896 : myMaxVehLength(0),
71 17896 : myNumAlternatives(0),
72 17896 : myLastStepOccupancy(0),
73 17896 : myDepartPos(-1),
74 17896 : myDepartPosDefinition(DepartPosDefinition::DEFAULT),
75 35806 : myUpdateEvent(nullptr) {
76 : // initialize unspecified defaults
77 17896 : if (myWidth == 0) {
78 15223 : myWidth = SUMO_const_laneWidth;
79 : }
80 :
81 17896 : if (departPos != "") {
82 : std::string error;
83 60 : if (!SUMOVehicleParameter::parseDepartPos(departPos, toString(myElement), getID(), myDepartPos, myDepartPosDefinition, error)) {
84 0 : throw ProcessError(error);
85 : }
86 30 : if (myDepartPosDefinition != DepartPosDefinition::GIVEN) {
87 : // maybe allow other methods at a later time
88 0 : throw ProcessError(TLF("Only a numerical departPos is supported for % '%'", toString(myElement), getID()));
89 30 : } else if (myDepartPos < 0 || myDepartPos > lane.getLength()) {
90 0 : throw ProcessError(TLF("Invalid departPos for % '%'", toString(myElement), getID()));
91 : }
92 : }
93 :
94 17896 : const double offset = (MSGlobals::gLefthand != lefthand) ? -1 : 1;
95 35792 : myShape = lane.getShape().getSubpart(
96 : lane.interpolateLanePosToGeometryPos(begPos),
97 : lane.interpolateLanePosToGeometryPos(endPos));
98 17896 : if (!myOnRoad) {
99 17750 : myShape.move2side((lane.getWidth() / 2. + myWidth / 2.) * offset);
100 : }
101 17896 : setRoadsideCapacity(capacity);
102 17896 : }
103 :
104 :
105 52708 : MSParkingArea::~MSParkingArea() {}
106 :
107 :
108 : void
109 59970 : MSParkingArea::addLotEntry(double x, double y, double z, double width, double length, double angle, double slope) {
110 : // create LotSpaceDefinition
111 59970 : LotSpaceDefinition lsd((int)mySpaceOccupancies.size(), nullptr, x, y, z, angle, slope, width, length);
112 : // If we are modelling parking set the end position to the lot position relative to the lane
113 : // rather than the end of the parking area - this results in vehicles stopping nearer the space
114 : // and re-entering the lane nearer the space. (If we are not modelling parking the vehicle will usually
115 : // enter the space and re-enter at the end of the parking area.)
116 59970 : if (MSGlobals::gModelParkingManoeuver) {
117 36 : const double offset = this->getLane().getShape().nearest_offset_to_point2D(lsd.position);
118 36 : if (offset < getBeginLanePosition()) {
119 0 : lsd.endPos = getBeginLanePosition() + POSITION_EPS;
120 : } else {
121 36 : if (this->getLane().getLength() > offset) {
122 36 : lsd.endPos = offset;
123 : } else {
124 0 : lsd.endPos = this->getLane().getLength() - POSITION_EPS;
125 : }
126 : }
127 : // Work out the angle of the lot relative to the lane (-90 adjusts for the way the bay is drawn )
128 36 : double relativeAngle = fmod(lsd.rotation - 90., 360) - fmod(RAD2DEG(this->getLane().getShape().rotationAtOffset(lsd.endPos)), 360) + 0.5;
129 36 : if (relativeAngle < 0.) {
130 12 : relativeAngle += 360.;
131 : }
132 36 : lsd.manoeuverAngle = relativeAngle;
133 :
134 : // if p2.y is -ve the lot is on LHS of lane relative to lane direction
135 : // we need to know this because it inverts the complexity of the parking manoeuver
136 36 : Position p2 = this->getLane().getShape().transformToVectorCoordinates(lsd.position);
137 36 : if (p2.y() < (0. + POSITION_EPS)) {
138 36 : lsd.sideIsLHS = true;
139 : } else {
140 0 : lsd.sideIsLHS = false;
141 : }
142 : } else {
143 59934 : lsd.endPos = myEndPos;
144 59934 : lsd.manoeuverAngle = int(angle); // unused unless gModelParkingManoeuver is true
145 59934 : lsd.sideIsLHS = true;
146 : }
147 59970 : mySpaceOccupancies.push_back(lsd);
148 59970 : myCapacity++;
149 59970 : computeLastFreePos();
150 59970 : }
151 :
152 : int
153 36 : MSParkingArea::getLastFreeLotAngle() const {
154 : assert(myLastFreeLot >= 0);
155 : assert(myLastFreeLot < (int)mySpaceOccupancies.size());
156 :
157 36 : const LotSpaceDefinition& lsd = mySpaceOccupancies[myLastFreeLot];
158 36 : if (lsd.sideIsLHS) {
159 36 : return abs(int(lsd.manoeuverAngle)) % 180;
160 : } else {
161 0 : return abs(abs(int(lsd.manoeuverAngle)) % 180 - 180) % 180;
162 : }
163 : }
164 :
165 : double
166 36 : MSParkingArea::getLastFreeLotGUIAngle() const {
167 : assert(myLastFreeLot >= 0);
168 : assert(myLastFreeLot < (int)mySpaceOccupancies.size());
169 :
170 36 : const LotSpaceDefinition& lsd = mySpaceOccupancies[myLastFreeLot];
171 36 : if (lsd.manoeuverAngle > 180.) {
172 18 : return DEG2RAD(lsd.manoeuverAngle - 360.);
173 : } else {
174 18 : return DEG2RAD(lsd.manoeuverAngle);
175 : }
176 : }
177 :
178 :
179 : double
180 2828301 : MSParkingArea::getLastFreePos(const SUMOVehicle& forVehicle, double brakePos) const {
181 2828301 : if (myCapacity == (int)myEndPositions.size()) {
182 : // keep enough space so that parking vehicles can leave
183 : #ifdef DEBUG_GET_LAST_FREE_POS
184 : if (DEBUG_COND2(forVehicle)) {
185 : std::cout << SIMTIME << " getLastFreePos veh=" << forVehicle.getID() << " allOccupied\n";
186 : }
187 : #endif
188 1924645 : return myLastFreePos - forVehicle.getVehicleType().getMinGap() - POSITION_EPS;
189 903656 : } else if (mustAdvance(forVehicle.getVClass())) {
190 : // vehicles cannot overtake so we must fill from the downstream end
191 144487 : int skipN = SIMSTEP == myReservationTime ? myReservations - 1 : 0;
192 : //std::cout << SIMTIME << " v=" << forVehicle.getID() << " t=" << SIMTIME << " resTime=" << STEPS2TIME(myReservationTime) << " myR=" << myReservations << " skip=" << skipN << " rV=" << toString(myReservedVehicles) << "\n";
193 376222 : for (auto it_lsd = mySpaceOccupancies.rbegin(); it_lsd != mySpaceOccupancies.rend(); it_lsd++) {
194 375982 : if (it_lsd->vehicle == nullptr) {
195 149828 : if (skipN > 0) {
196 : // skip reservations
197 5581 : skipN--;
198 5581 : continue;
199 : }
200 : #ifdef DEBUG_GET_LAST_FREE_POS
201 : if (DEBUG_COND2(forVehicle)) {
202 : std::cout << SIMTIME << " getLastFreePos (onRoad-upstream) veh=" << forVehicle.getID() << " brakePos=" << brakePos << " myEndPos=" << myEndPos << " nextFreePos=" << it_lsd->endPos << "\n";
203 : }
204 : #endif
205 144247 : return it_lsd->endPos;
206 : }
207 : }
208 : // should not happen
209 : return brakePos;
210 : } else {
211 759169 : const double minPos = MIN2(myEndPos, brakePos);
212 759169 : if (myLastFreePos >= minPos) {
213 : #ifdef DEBUG_GET_LAST_FREE_POS
214 : if (DEBUG_COND2(forVehicle)) {
215 : std::cout << SIMTIME << " getLastFreePos veh=" << forVehicle.getID() << " brakePos=" << brakePos << " myEndPos=" << myEndPos << " using myLastFreePos=" << myLastFreePos << "\n";
216 : }
217 : #endif
218 : return myLastFreePos;
219 : } else {
220 : // find free pos after minPos
221 58483 : for (const auto& lsd : mySpaceOccupancies) {
222 58276 : if (lsd.vehicle == nullptr && lsd.endPos >= minPos) {
223 : #ifdef DEBUG_GET_LAST_FREE_POS
224 : if (DEBUG_COND2(forVehicle)) {
225 : std::cout << SIMTIME << " getLastFreePos veh=" << forVehicle.getID() << " brakePos=" << brakePos << " myEndPos=" << myEndPos << " nextFreePos=" << lsd.endPos << "\n";
226 : }
227 : #endif
228 : return lsd.endPos;
229 : }
230 : }
231 : // shouldn't happen. No good solution seems possible
232 : #ifdef DEBUG_GET_LAST_FREE_POS
233 : if (DEBUG_COND2(forVehicle)) {
234 : std::cout << SIMTIME << " getLastFreePos veh=" << forVehicle.getID() << " brakePos=" << brakePos << " myEndPos=" << myEndPos << " noGoodFreePos blockedAt=" << brakePos << "\n";
235 : }
236 : #endif
237 : return brakePos;
238 : }
239 : }
240 : }
241 :
242 : bool
243 1168448 : MSParkingArea::mustAdvance(SUMOVehicleClass svc) const {
244 1168448 : return myOnRoad && cannotChange(svc);
245 : }
246 :
247 : bool
248 446454 : MSParkingArea::cannotChange(SUMOVehicleClass svc) const {
249 446454 : const MSLane* left = myLane.getParallelLane(1, false);
250 446454 : const MSLane* right = myLane.getParallelLane(-1, false);
251 37279 : return ((left == nullptr || !left->allowsVehicleClass(svc) || !myLane.allowsChangingLeft(svc))
252 446454 : && (right == nullptr || !right->allowsVehicleClass(svc) || !myLane.allowsChangingRight(svc)));
253 : }
254 :
255 :
256 : Position
257 18814 : MSParkingArea::getVehiclePosition(const SUMOVehicle& forVehicle) const {
258 44344 : for (const auto& lsd : mySpaceOccupancies) {
259 44344 : if (lsd.vehicle == &forVehicle) {
260 18814 : return lsd.position;
261 : }
262 : }
263 0 : return Position::INVALID;
264 : }
265 :
266 :
267 : double
268 55460 : MSParkingArea::getInsertionPosition(const SUMOVehicle& forVehicle) const {
269 55460 : if (myDepartPosDefinition == DepartPosDefinition::GIVEN) {
270 250 : return myDepartPos;
271 : }
272 143193 : for (const auto& lsd : mySpaceOccupancies) {
273 143183 : if (lsd.vehicle == &forVehicle) {
274 55200 : return lsd.endPos;
275 : }
276 : }
277 : return -1;
278 : }
279 :
280 :
281 : double
282 16593 : MSParkingArea::getVehicleAngle(const SUMOVehicle& forVehicle) const {
283 41568 : for (const auto& lsd : mySpaceOccupancies) {
284 41547 : if (lsd.vehicle == &forVehicle) {
285 16572 : return (lsd.rotation - 90.) * (double) M_PI / (double) 180.0;
286 : }
287 : }
288 : return 0;
289 : }
290 :
291 : double
292 4510 : MSParkingArea::getVehicleSlope(const SUMOVehicle& forVehicle) const {
293 14830 : for (const auto& lsd : mySpaceOccupancies) {
294 14830 : if (lsd.vehicle == &forVehicle) {
295 4510 : return lsd.slope;
296 : }
297 : }
298 : return 0;
299 : }
300 :
301 : double
302 36 : MSParkingArea::getGUIAngle(const SUMOVehicle& forVehicle) const {
303 72 : for (const auto& lsd : mySpaceOccupancies) {
304 72 : if (lsd.vehicle == &forVehicle) {
305 36 : if (lsd.manoeuverAngle > 180.) {
306 18 : return DEG2RAD(lsd.manoeuverAngle - 360.);
307 : } else {
308 18 : return DEG2RAD(lsd.manoeuverAngle);
309 : }
310 : }
311 : }
312 : return 0.;
313 : }
314 :
315 : int
316 36 : MSParkingArea::getManoeuverAngle(const SUMOVehicle& forVehicle) const {
317 72 : for (const auto& lsd : mySpaceOccupancies) {
318 72 : if (lsd.vehicle == &forVehicle) {
319 36 : if (lsd.sideIsLHS) {
320 36 : return abs(int(lsd.manoeuverAngle)) % 180;
321 : } else {
322 0 : return abs(abs(int(lsd.manoeuverAngle)) % 180 - 180) % 180;
323 : }
324 : }
325 : }
326 : return 0;
327 : }
328 :
329 : int
330 39929 : MSParkingArea::getLotIndex(const SUMOVehicle* veh) const {
331 39929 : if (veh->getPositionOnLane() > myLastFreePos) {
332 : // vehicle has gone past myLastFreePos and we need to find the actual lot
333 : int closestLot = -1;
334 19029 : for (int i = 0; i < (int)mySpaceOccupancies.size(); i++) {
335 18944 : const LotSpaceDefinition lsd = mySpaceOccupancies[i];
336 18944 : if (lsd.vehicle == nullptr) {
337 : closestLot = i;
338 13935 : if (lsd.endPos >= veh->getPositionOnLane()) {
339 5145 : return i;
340 : }
341 : }
342 : }
343 85 : return closestLot;
344 : }
345 34699 : if (myOnRoad && myLastFreePos - veh->getPositionOnLane() > POSITION_EPS) {
346 : // for on-road parking we need to be precise
347 : return -1;
348 : }
349 9800 : return myLastFreeLot;
350 : }
351 :
352 : void
353 10422 : MSParkingArea::enter(SUMOVehicle* veh, const bool /* parking */) {
354 10422 : removeSpaceReservation(veh);
355 10422 : double beg = veh->getPositionOnLane() + veh->getVehicleType().getMinGap();
356 10422 : double end = veh->getPositionOnLane() - veh->getVehicleType().getLength();
357 10422 : if (myUpdateEvent == nullptr) {
358 0 : myUpdateEvent = new WrappingCommand<MSParkingArea>(this, &MSParkingArea::updateOccupancy);
359 0 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myUpdateEvent);
360 : }
361 10422 : int lotIndex = getLotIndex(veh);
362 10422 : if (lotIndex < 0) {
363 0 : WRITE_WARNING("Unsuitable parking position for vehicle '" + veh->getID() + "' at parkingArea '" + getID() + "' time=" + time2string(SIMSTEP));
364 0 : lotIndex = myLastFreeLot;
365 : }
366 : #ifdef DEBUG_GET_LAST_FREE_POS
367 : ((SUMOVehicleParameter&)veh->getParameter()).setParameter("lotIndex", toString(lotIndex));
368 : #endif
369 : assert(myLastFreePos >= 0);
370 : assert(lotIndex < (int)mySpaceOccupancies.size());
371 10422 : mySpaceOccupancies[lotIndex].vehicle = veh;
372 10422 : myEndPositions[veh] = std::pair<double, double>(beg, end);
373 10422 : computeLastFreePos();
374 10422 : myMaxVehLength = MAX2(myMaxVehLength, veh->getLength());
375 : // current search ends here
376 10422 : veh->setNumberParkingReroutes(0);
377 10422 : }
378 :
379 :
380 : void
381 36 : MSParkingArea::addSpaceReservation(const SUMOVehicle* veh) {
382 : myRemoteReservedVehicles.insert(veh);
383 36 : if (myUpdateEvent == nullptr) {
384 36 : myUpdateEvent = new WrappingCommand<MSParkingArea>(this, &MSParkingArea::updateOccupancy);
385 36 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myUpdateEvent);
386 : }
387 36 : }
388 :
389 : void
390 10440 : MSParkingArea::removeSpaceReservation(const SUMOVehicle* veh) {
391 : myRemoteReservedVehicles.erase(veh);
392 10440 : if (myUpdateEvent == nullptr) {
393 9629 : myUpdateEvent = new WrappingCommand<MSParkingArea>(this, &MSParkingArea::updateOccupancy);
394 9629 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myUpdateEvent);
395 : }
396 10440 : }
397 :
398 : void
399 8728 : MSParkingArea::leaveFrom(SUMOVehicle* what) {
400 : assert(myEndPositions.find(what) != myEndPositions.end());
401 8728 : if (myUpdateEvent == nullptr) {
402 8471 : myUpdateEvent = new WrappingCommand<MSParkingArea>(this, &MSParkingArea::updateOccupancy);
403 8471 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(myUpdateEvent);
404 : }
405 23197 : for (auto& lsd : mySpaceOccupancies) {
406 23197 : if (lsd.vehicle == what) {
407 8728 : lsd.vehicle = nullptr;
408 8728 : break;
409 : }
410 : }
411 8728 : if (what->getLength() == myMaxVehLength) {
412 8638 : myMaxVehLength = 0;
413 36737 : for (auto item : myEndPositions) {
414 56114 : myMaxVehLength = MAX2(myMaxVehLength, item.first->getLength());
415 : }
416 : }
417 : myEndPositions.erase(myEndPositions.find(what));
418 8728 : computeLastFreePos();
419 8728 : }
420 :
421 :
422 : SUMOTime
423 18136 : MSParkingArea::updateOccupancy(SUMOTime /* currentTime */) {
424 18136 : myLastStepOccupancy = getOccupancy();
425 : myLastRemoteReservedVehicles = myRemoteReservedVehicles;
426 18136 : myUpdateEvent = nullptr;
427 18136 : return 0;
428 : }
429 :
430 :
431 0 : MSParkingArea::LotSpaceDefinition::LotSpaceDefinition() :
432 0 : index(-1),
433 0 : vehicle(nullptr),
434 0 : rotation(0),
435 0 : slope(0),
436 0 : width(0),
437 0 : length(0),
438 0 : endPos(0),
439 0 : manoeuverAngle(0),
440 0 : sideIsLHS(false) {
441 0 : }
442 :
443 :
444 59970 : MSParkingArea::LotSpaceDefinition::LotSpaceDefinition(int index_, SUMOVehicle* vehicle_, double x, double y, double z, double rotation_, double slope_, double width_, double length_) :
445 59970 : index(index_),
446 59970 : vehicle(vehicle_),
447 : position(Position(x, y, z)),
448 59970 : rotation(rotation_),
449 59970 : slope(slope_),
450 59970 : width(width_),
451 59970 : length(length_),
452 59970 : endPos(0),
453 59970 : manoeuverAngle(0),
454 59970 : sideIsLHS(false) {
455 59970 : }
456 :
457 :
458 : void
459 111389 : MSParkingArea::computeLastFreePos() {
460 111389 : myLastFreeLot = -1;
461 111389 : myLastFreePos = myBegPos;
462 111389 : myEgressBlocked = false;
463 144728 : for (auto& lsd : mySpaceOccupancies) {
464 126594 : if (lsd.vehicle == nullptr
465 126594 : || (getOccupancy() == getCapacity()
466 12803 : && lsd.vehicle->remainingStopDuration() <= 0
467 1670 : && !lsd.vehicle->isStoppedTriggered())) {
468 93255 : if (lsd.vehicle == nullptr) {
469 92306 : myLastFreeLot = lsd.index;
470 92306 : myLastFreePos = lsd.endPos;
471 : } else {
472 : // vehicle wants to exit the parking area
473 949 : myLastFreeLot = lsd.index;
474 949 : myLastFreePos = lsd.endPos - lsd.vehicle->getVehicleType().getLength() - POSITION_EPS;
475 949 : myEgressBlocked = true;
476 : }
477 : break;
478 : } else {
479 33339 : myLastFreePos = MIN2(myLastFreePos,
480 33339 : lsd.endPos - lsd.vehicle->getVehicleType().getLength() - NUMERICAL_EPS);
481 : }
482 : }
483 111389 : }
484 :
485 :
486 : double
487 1702156 : MSParkingArea::getLastFreePosWithReservation(SUMOTime t, const SUMOVehicle& forVehicle, double brakePos) {
488 : // do not perform reservation when far away
489 1702156 : if (forVehicle.getLane() != &myLane || forVehicle.getPositionOnLane() < (myBegPos - myMaxVehLength - forVehicle.getVehicleType().getMinGap())) {
490 : // for different lanes, do not consider reservations to avoid lane-order
491 : // dependency in parallel simulation
492 : #ifdef DEBUG_RESERVATIONS
493 : if (DEBUG_COND2(forVehicle)) {
494 : std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " other lane\n";
495 : }
496 : #endif
497 1488875 : if (myNumAlternatives > 0 && getOccupancy() == getCapacity()) {
498 : // ensure that the vehicle reaches the rerouter lane
499 207820 : if (mySpaceOccupancies.size() > 0) {
500 : assert(mySpaceOccupancies[0].vehicle != nullptr);
501 206799 : const double waitPos = (mySpaceOccupancies[0].endPos
502 206799 : - mySpaceOccupancies[0].vehicle->getLength()
503 206799 : - forVehicle.getVehicleType().getMinGap()
504 206799 : - NUMERICAL_EPS);
505 206799 : return MAX2(waitPos, MIN2(POSITION_EPS, myEndPos));
506 : } else {
507 1021 : return MAX2(myBegPos, MIN2(POSITION_EPS, myEndPos));
508 : }
509 : } else {
510 : // check if there is a reservation from the last time step
511 : // (this could also be in myReserations, if myLane wasn't processed before the forVehicle-lane)
512 1281055 : const SUMOTime last = t - DELTA_T;
513 : #ifdef DEBUG_RESERVATIONS
514 : if (DEBUG_COND2(forVehicle)) {
515 : std::cout << SIMTIME << " last=" << time2string(last) << " lastRes=" << time2string(myLastReservationTime) << " resTime=" << toString(myReservationTime) << "\n";
516 : }
517 : #endif
518 1281055 : if (myLastReservationTime == last || myReservationTime == last) {
519 843453 : int res = myLastReservationTime == last ? myLastReservations : myReservations;
520 843453 : if (myCapacity <= getOccupancy() + res) {
521 814760 : double maxLen = myLastReservationTime == last ? myLastReservationMaxLength : myReservationMaxLength;
522 : #ifdef DEBUG_RESERVATIONS
523 : if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID()
524 : << " lastRes=" << res << " resTime=" << myReservationTime << " reserved full, maxLen=" << maxLen << " endPos=" << mySpaceOccupancies[0].endPos << "\n";
525 : #endif
526 814760 : return (mySpaceOccupancies[0].endPos
527 814760 : - maxLen
528 814760 : - forVehicle.getVehicleType().getMinGap()
529 814760 : - NUMERICAL_EPS);
530 : }
531 : }
532 466295 : return getLastFreePos(forVehicle, brakePos);
533 : }
534 : }
535 213281 : if (t > myReservationTime) {
536 : #ifdef DEBUG_RESERVATIONS
537 : if (DEBUG_COND2(forVehicle)) {
538 : std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " first reservation\n";
539 : }
540 : #endif
541 161995 : myLastReservationTime = myReservationTime;
542 161995 : myLastReservations = myReservations;
543 161995 : myLastReservationMaxLength = myReservationMaxLength;
544 161995 : myReservationTime = t;
545 161995 : myReservations = 1;
546 161995 : myReservationMaxLength = forVehicle.getVehicleType().getLength();
547 : myReservedVehicles.clear();
548 161995 : myReservedVehicles.insert(&forVehicle);
549 794599 : for (const auto& lsd : mySpaceOccupancies) {
550 632604 : if (lsd.vehicle != nullptr) {
551 702804 : myReservationMaxLength = MAX2(myReservationMaxLength, lsd.vehicle->getVehicleType().getLength());
552 : }
553 : }
554 161995 : return getLastFreePos(forVehicle, brakePos);
555 : } else {
556 51286 : if (myCapacity > getOccupancy() + myReservations) {
557 : #ifdef DEBUG_RESERVATIONS
558 : if (DEBUG_COND2(forVehicle)) {
559 : std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID() << " res=" << myReservations << " enough space\n";
560 : }
561 : #endif
562 8179 : myReservations++;
563 8179 : myReservationMaxLength = MAX2(myReservationMaxLength, forVehicle.getVehicleType().getLength());
564 8179 : myReservedVehicles.insert(&forVehicle);
565 8179 : return getLastFreePos(forVehicle, brakePos);
566 : } else {
567 43107 : if (myCapacity == 0) {
568 0 : return getLastFreePos(forVehicle, brakePos);
569 : } else {
570 : #ifdef DEBUG_RESERVATIONS
571 : if (DEBUG_COND2(forVehicle)) std::cout << SIMTIME << " pa=" << getID() << " freePosRes veh=" << forVehicle.getID()
572 : << " res=" << myReservations << " resTime=" << myReservationTime << " reserved full, maxLen=" << myReservationMaxLength << " endPos=" << mySpaceOccupancies[0].endPos << "\n";
573 : #endif
574 43107 : return (mySpaceOccupancies[0].endPos
575 43107 : - myReservationMaxLength
576 43107 : - forVehicle.getVehicleType().getMinGap()
577 43107 : - NUMERICAL_EPS);
578 : }
579 : }
580 : }
581 : }
582 :
583 :
584 : double
585 699 : MSParkingArea::getWidth() const {
586 699 : return myWidth;
587 : }
588 :
589 :
590 : double
591 699 : MSParkingArea::getLength() const {
592 699 : return myLength;
593 : }
594 :
595 :
596 : double
597 699 : MSParkingArea::getAngle() const {
598 699 : return myAngle;
599 : }
600 :
601 :
602 : int
603 1591025 : MSParkingArea::getCapacity() const {
604 1591025 : return myCapacity;
605 : }
606 :
607 :
608 : bool
609 1340162 : MSParkingArea::parkOnRoad() const {
610 1340162 : return myOnRoad;
611 : }
612 :
613 :
614 : int
615 2053607 : MSParkingArea::getOccupancy() const {
616 2053607 : return (int)myEndPositions.size() - (myEgressBlocked ? 1 : 0);
617 : }
618 :
619 :
620 : int
621 5 : MSParkingArea::getOccupancyIncludingBlocked() const {
622 5 : return (int)myEndPositions.size();
623 : }
624 :
625 : int
626 247144 : MSParkingArea::getOccupancyIncludingReservations(const SUMOVehicle* forVehicle) const {
627 : const bool reservedLocal = myReservedVehicles.count(forVehicle) != 0;
628 : const bool reservedRemote = myRemoteReservedVehicles.count(forVehicle) != 0;
629 : return ((int)myEndPositions.size()
630 247144 : + (reservedLocal ? 0 : myReservations)
631 247144 : + (reservedRemote ? 0 : (int)myRemoteReservedVehicles.size()));
632 : }
633 :
634 :
635 : int
636 115406 : MSParkingArea::getOccupancyIncludingRemoteReservations(const SUMOVehicle* forVehicle) const {
637 115406 : const int reservedRemote = myRemoteReservedVehicles.count(forVehicle) != 0;
638 115406 : return getOccupancy() + (int)myRemoteReservedVehicles.size() - reservedRemote;
639 : }
640 :
641 :
642 : int
643 0 : MSParkingArea::getLastStepOccupancy() const {
644 0 : return myLastStepOccupancy;
645 : }
646 :
647 :
648 : int
649 100234 : MSParkingArea::getLastStepOccupancyIncludingRemoteReservations(const SUMOVehicle* forVehicle) const {
650 100234 : const int reservedRemote = myLastRemoteReservedVehicles.count(forVehicle) != 0;
651 100234 : return myLastStepOccupancy + (int)myLastRemoteReservedVehicles.size() - reservedRemote;
652 : }
653 :
654 :
655 : void
656 0 : MSParkingArea::accept(std::string badge) {
657 : myAcceptedBadges.insert(badge);
658 0 : }
659 :
660 :
661 : void
662 0 : MSParkingArea::accept(std::vector<std::string> badges) {
663 : myAcceptedBadges.insert(badges.begin(), badges.end());
664 0 : }
665 :
666 :
667 : void
668 0 : MSParkingArea::refuse(std::string badge) {
669 : myAcceptedBadges.erase(badge);
670 0 : }
671 :
672 :
673 : bool
674 260729 : MSParkingArea::accepts(SUMOVehicle* veh) const {
675 260729 : if (myAcceptedBadges.size() == 0) {
676 : return true;
677 : } else {
678 1983 : std::vector<std::string> vehicleBadges = veh->getParkingBadges();
679 1983 : for (auto badge : vehicleBadges) {
680 : if (myAcceptedBadges.count(badge) != 0) {
681 : return true;
682 : }
683 : }
684 823 : return false;
685 1983 : }
686 : }
687 :
688 :
689 : void
690 1425 : MSParkingArea::notifyEgressBlocked() {
691 1425 : computeLastFreePos();
692 1425 : }
693 :
694 :
695 : int
696 0 : MSParkingArea::getNumAlternatives() const {
697 0 : return myNumAlternatives;
698 : }
699 :
700 :
701 : void
702 15642 : MSParkingArea::setNumAlternatives(int alternatives) {
703 15642 : myNumAlternatives = MAX2(myNumAlternatives, alternatives);
704 15642 : }
705 :
706 :
707 : std::vector<std::string>
708 28 : MSParkingArea::getAcceptedBadges() const {
709 28 : std::vector<std::string> result(myAcceptedBadges.begin(), myAcceptedBadges.end());
710 28 : return result;
711 : }
712 :
713 :
714 : void
715 10 : MSParkingArea::setAcceptedBadges(const std::vector<std::string>& badges) {
716 : myAcceptedBadges.clear();
717 : myAcceptedBadges.insert(badges.begin(), badges.end());
718 10 : }
719 :
720 :
721 : const std::vector<MSParkingArea::LotSpaceDefinition>&
722 90 : MSParkingArea::getSpaceOccupancies() const {
723 90 : return mySpaceOccupancies;
724 : }
725 :
726 :
727 : const PositionVector&
728 5 : MSParkingArea::getShape() const {
729 5 : return myShape;
730 : }
731 :
732 :
733 : void
734 30844 : MSParkingArea::setRoadsideCapacity(int capacity) {
735 : // reinit parking lot generation process
736 30844 : myRoadSideCapacity = capacity;
737 :
738 : // Initialize space occupancies if there is a road-side capacity
739 : // The overall number of lots is fixed and each lot accepts one vehicle regardless of size
740 30844 : const double spaceDim = myRoadSideCapacity > 0 ? myLane.interpolateLanePosToGeometryPos((myEndPos - myBegPos) / myRoadSideCapacity) : 7.5;
741 30844 : const double spaceOffset = myRoadSideCapacity > 0 ? (myEndPos - myBegPos) / myRoadSideCapacity : 7.5;
742 30844 : if (myLength == 0) {
743 14288 : myLength = spaceDim;
744 : }
745 : mySpaceOccupancies.clear();
746 30844 : myCapacity = 0;
747 90115 : for (int i = 0; i < myRoadSideCapacity; ++i) {
748 : // calculate pos, angle and slope of parking lot space
749 59271 : const Position pos = GeomHelper::calculateLotSpacePosition(myShape, i, spaceDim, myAngle, myWidth, myLength);
750 59271 : double spaceAngle = GeomHelper::calculateLotSpaceAngle(myShape, i, spaceDim, myAngle);
751 59271 : double spaceSlope = GeomHelper::calculateLotSpaceSlope(myShape, i, spaceDim);
752 : // add lotEntry
753 59271 : addLotEntry(pos.x(), pos.y(), pos.z(), myWidth, myLength, spaceAngle, spaceSlope);
754 : // update endPos
755 176277 : mySpaceOccupancies.back().endPos = MIN2(myEndPos, myBegPos + MAX2(POSITION_EPS, spaceOffset * (i + 1)));
756 : }
757 : // recompute after modifying the last endPos
758 30844 : computeLastFreePos();
759 30844 : }
760 :
761 : /****************************************************************************/
|